Consider the following psuedo code:
Code:
class Keyboard
{
function keyPressISR ()
{
// logic to store the key
/* For example (assembly),
IN rDst // get keycode placed on databus by keyboard
STO rDst KEYCODE // store it in memory at address 'KEYCODE',
// memory[ KEYCODE ] = value read
RTI
*/
}
}
class EventListener
{
function onKeyPress ( callback )
{
// call the function waiting for this event
callback();
}
}
class Main
{
function doFancyThing ()
{
// stuff
}
function setupEventListeners ()
{
EventListener.onKeyPress( Main.doFancyThing );
}
}
Consider the instruction set of something like the Z80, 6502, or OPC. Suppose that when the keyboard is pressed, it sends an interrupt signal to the CPU. Suppose then that the CPU calls an appropriate ISR, in this case
Keyboard.keyPressISR.
I assume that
EventListener.onKeyPress is treated as a regular function - that is, an
RTI has already been called by
Keyboard.keyPressISR because who knows how long
callback will run for. So then how does the switch happen? How do we go from the ISR of keyPress to executing the code listening for the keyPress (
callback)?
I am hoping this can be achieved without an operating system...can it? I'm under the impression that computers like the ZX Spectrum, Commodore, BBC Micro etc could do this even if they didn't have an operating system like Windows, Linux etc...But I don't know much about the older operating systems like CPM and DOS...