AnyCPU
http://anycpu.org/forum/

Event listeners
http://anycpu.org/forum/viewtopic.php?f=7&t=575
Page 1 of 1

Author:  quadrant [ Sat Feb 16, 2019 8:57 pm ]
Post subject:  Event listeners

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...

Author:  Garth [ Sun Feb 17, 2019 12:50 am ]
Post subject:  Re: Event listeners

The ISR will also need keyboard de-bouncing code.

Many simple systems will just loop and hold up all activity while waiting for the key. Otherwise:

I'm barely able to follow your pseudo-code; but what comes to mind is the "cyclic executive" method of multitasking without a multitasking OS, which I describe starting about 45% of the way down the 6502-oriented page at http://wilsonminesco.com/multitask/, under the "Cyclic executive" heading. If you want one or more tasks to get some processor time while another task is waiting for an event or waiting for a certain time to arrive, ie, you want multitasking, you can have the tasks take the form of subroutines, called in a loop. Each task keeps its own state variables so it knows what it's watching for and where to pick up when the condition is met. If the condition is not met yet, it just returns control to the loop so something else can have some processor time while the wait continues. The test usually takes very few clock cycles before the task gives up control. There's no need to disable interrupts during the task-switching. If the condition is met or the target time is reached, the task acts on it, and runs until it arrives at a convenient stopping point, then returns control to the loop that has all the subroutine calls. Doing it this way, you might even decide to have the keyboard scanning to be a task that takes its turn, rather than run on interrupts. It might make a big improvement in interrupt performance for things that really need it.

Author:  robfinch [ Sun Feb 17, 2019 2:55 am ]
Post subject:  Re: Event listeners

Quote:
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)?
. It typically involves an event or message queue and means to record events in the queue (raise() method). The ISR calls raise() or performs the equivalent which stores an event code (KEYPRESS) and other data (ascii ‘C’) in the queue. In the event listener class there will be a map of the event to a specific event handler. The event listener polls the queue and looks up the handler matching event and calls it. Exact details vary from system to system.
Quote:
I am hoping this can be achieved without an operating system...can it?
Yes it can be done without an OS. Having a working OS would make things easier. Getting a keystroke for an app can range from simple to very complicated to do. The pseudo-code appears to be leading towards a complicated way of doing things. As Garth outlines, each task could simply poll (loop) waiting for the correct data to appear. With event listeners and callback functions the poll loop is moved outside of the task into an explicit event listening class, but the idea’s the same.
Code:
class Keyboard
{
   function keyPressISR ()
   {
      // logic to store the key
      IN  rDst          // get keycode placed on databus by keyboard
      RaiseEvent (KEYPRESS, rDst);   // This will store to an event queue

      RTI

      */
   }
}
class EventListener
{
   function map(event code, handler) {
   }
   function onKeyPress ( callback )
   {
      // call the function waiting for this event
      callback();
   }
}

class Main
{
   function doFancyThing ()
   {
      // stuff
   }

   function setupEventListeners ()
   {
      EventListener.map(KEYPRESS, EventListener.onKeyPress);
      EventListener.onKeyPress( Main.doFancyThing );
   }
}

Author:  Garth [ Sun Feb 17, 2019 3:20 am ]
Post subject:  Re: Event listeners

The attached file was posted (or sent to me? I can't remember) by a member of the 6502.org forum quite a few years ago. It might be longer than you care to read, but it's there in case it helps someone.

Attachments:
event_driven_programming.doc [1.11 MiB]
Downloaded 72 times

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/