RISC World

Modules for Beginners, Part 6: Communicating with Programs

Brian Pickard

Most modules send data to programs by the use of their SWI‘s or star commands, as demonstrated in earlier parts of this series. However sometimes a module is required to check for events happening as described in the last issue. Since these can occur at any time star commands and SWI's cannot be used. It would be nice if this type of module could signal the events it is checking for to a program. The program could then take appropriate action, even if it is only a warning message.

Wimp Poll Word

There is a simple way of doing this using the wimp poll word. This is an extension to the normal Wimp Poll SWI. When this extension is active the wimp returns event 13 (pollword) when the poll word contents become non zero. This occurs before any screen redrawing occurs. The wimp mask word must have bits 22 and 23 set to use the poll word and R3 must point to the poll word.

In the following the pollword variable is pollword% and the wimp block is bk%.

   SYS"Wimp_Poll",mask%,bk%,,pollword% TO action%

To deal with the wimp pollword event the following code is required.

   CASE action% OF
   WHEN 13
   REM do required action

The pollword contents must be made zero since the wimp will continue to issue event 13 causing an infinite loop. If the value of the pollword is required the wimp conveniently stores this at bk%+4 (bk% being the wimp block). The poll word can be any word aligned memory location within the module area. The modules workspace pointer is a convenient location. This can be found by using the following OS_Module SWI

   SYS"OS_Module•,18,"module_name" TO ,,,,pollwordcontents%;flag%
   IF (flag% AND1)>0 THEN ERROR 0,"Module module_name cannot be found"
   =(flag% AND1)

Keyboard checking Example

I have used the keyboard checking module with just minor changes. Eight bytes of workspace are reserved for the pollword and key code. In the key press event detection code the key up/down returned in R1+1 is placed in the first word (pollword) ready for the Basic program to detect and report when the key is pressed. We need to add one to R1 so that both key up and down events will make the pollword non zero.

The key code in R2 is placed in the second word so the Basic program can work out the key. The Basic program then opens a window and reports which key, if any, is pressed. To make sure the Basic program can find the pollword contents the OS_Module SWI is called in the null event code just in case the RMTidy command has been issued and the workspace has been moved. In the module the following code is executed when a key press event happens.

   STMFD R13!,{r1 ,R4,R14} ;store required register on stack
   CMP R0,#11              ;is this event a key press
   BNE passon%             ;if not then pass on the event
   STR R2,[R12,#4]         ;store the key code at workspace+4
   ADD R1,R1,#1            ;add one to the up down flag
   STR R1,[R12]            ;store at workspace (which is the pollword posn)
   LDMFD R13!,{r1 ,R4,PC}  ;restore the registers and pass on the event

In the BASIC program the following code is executed when the pollword becomes non zero:

   pollword%!0=0     :REM cancel pollword back to zero
   keyup%=bk%!4-2    :REM store value pollword-2 in a flag to see 
                     :REM if key up or down
   keycode%=pollword%!4 :REM Store keycode for printing in window
   IF keycode%@lt;&68 THEN :REM If the key code from the keyboard see
                                (Programmers Ref Manual 1-156)
   bk%!0=wdh%:SYS&400C6,,bk% :REM Then close reporting window
   PROCopen(wdh%)     :REM Reopen it to force update

The above code could be cleaned up but it works well enough for a demo. The application is called !Keybrdchk. I have included the source code for the module (Keymodsrc).Well thats it for this time, next time I will round up the series with an over view of all the topics, unless there is anything you the reader would like to know about modules!

Brian Pickard