Sample Interrupt Function
The program below, when run on the emulator, will
print "Ding!" approximately once every two seconds.
The "while (1) sys$smlslp()" loop could be replaced
by any normal program code, which would execute as
normal, just being interrupted every 2 seconds.
As promised in class, I have made the modification
that makes the sytem behave sensibly: you do not
need to re-install interrupt handlers every time
they are called, but the timer interrupt does have
to be re-requested if you want it to be repeating.
In other words, the program below is the correct
way to do it.
#include <stdio.h>
#include <stdsys.h>
void dinger(void) interrupt
{ printf("Ding!\n");
sys$setti(2000); }
void main(void)
{ sys$setiv(dinger, iv$timer);
sys$setti(2000);
while (1)
sys$smlslp();
printf("you won't see this\n"); }
Note: prototypes for interrupt functions must include
the "interrupt" keyword just as complete definitions must.
A prototype for the function given above would be:
void dinger(void) interrupt;
Also note: On this system, the predefined printf
function is re-entrant, meaning it will work
correctly even if it accidentally becomes recursive,
as could hapopen if it is used in an interrupt
function. On other systems you should not expect this,
and if/when you write your own printf, it would take
a lot of extra work to make it re-entrant (which is
why it usually isn't).