View unanswered posts | View active topics It is currently Thu Mar 28, 2024 8:44 pm



Reply to topic  [ 4 posts ] 
 Event listeners 
Author Message

Joined: Sat Jun 16, 2018 2:51 am
Posts: 50
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...


Sat Feb 16, 2019 8:57 pm
Profile

Joined: Tue Dec 11, 2012 8:03 am
Posts: 285
Location: California
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.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources


Sun Feb 17, 2019 12:50 am
Profile WWW

Joined: Sat Feb 02, 2013 9:40 am
Posts: 2095
Location: Canada
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 );
   }
}

_________________
Robert Finch http://www.finitron.ca


Sun Feb 17, 2019 2:55 am
Profile WWW

Joined: Tue Dec 11, 2012 8:03 am
Posts: 285
Location: California
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 291 times

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
Sun Feb 17, 2019 3:20 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group
Designed by ST Software