One common need in code is to be able to protect the atomicity of multiple instructions. Sometimes you have a "critical section" which must always be run atomically, with no interruption. Steve mentioned a need for this in changing the hook table since it dispatches on enable interrupts and the dispatch code effectively assumes the table will not be modified during dispatch.
One way to do this is to DI at the beginning of the critical section and SI at the end. You can always protect a critical section in *foreground code* in that way. Foreground code is generally the main running program and any calls it makes to the ROM. As opposed to *background code* which comprises interrupt handlers, and anything those interrupt handlers call in turn. Timer interrupt, BCR interrupt, serial RX interrupt, serial RX hook, the timer handler, etc. If you have some code that must be accessible to both foreground and background code you have a problem. The DI does what you want, but the balancing SI could enable interrupts at a time that they should still be disabled. To address the typical pattern you use is "Save, Disable and *Restore*". I was curious as to now this is done on the 8085 and I came up with this: //Save+Disable PUSH H RIM PUSH H DI // critical section / protected code // Restore interrupts POP H ANI 8 JZ DONE EI DONE: POP H I believe this is correct. I used the stack to make it general purpose. However, if you can use A and/or H as scratch, it can be made shorter and faster. The general mechanism is to use RIM to read the interrupts enabled/disabled mask, and save it. You really only need bit #3. Then when we want to restore interrupts we call EI only if bit #3 of the interrupt mask was enabled. I assume you keep stack balanced between save and restore. -- John.
