/* FILE: toggle.c AUTH: P.OH DESC: ISR on IRQ3 for counting toggle switches */ #include #include #include #include void interrupt (*oldIrq3)(void); void interrupt countToggle(void); /* GLOBALS */ int i = 0; long j = 0; #define IRQ3 0x0b /* IRQ3 */ int main(void) { window(5,5,50,75); clrscr(); gotoxy(1,3); cprintf("Do-while loop iteration # "); oldIrq3 = getvect(IRQ3); /* save the old interrupt vector */ setvect(IRQ3, countToggle); /* install the new interrupt handler */ /* Unmask (i.e. enable) IRQ3. This requires turning bit 3 to 0 */ /* and bits 0,1,2,4,5,6,7 to 1. Hence 11110111 binary = F7 hex */ outportb(0x21, ( inportb(0x21) & 0xF7 ) ); /* Unmask (Enable) IRQ3 */ do { j++; gotoxy(27,3); cprintf("%ld\n", j); } while(!kbhit()); /* Key hit, so exit main. But first, be nice and return things back */ /* to original state */ setvect(IRQ3, oldIrq3); outportb(0x21, (inportb(0x21) | 0x08) ); /* disable IRQ3 */ printf("\nswitch presses i = %d\n", i); printf("j = %ld\n", j); return 0; } /* end of main */ /* this ISR should execute each time IRQ3 goes high */ void interrupt countToggle(void) { disable(); i++; outportb(0x20, 0x20); /* send EOI signal */ enable(); }