irq.c (1060B)
1#include <gb/gb.h> 2#include <gbdk/console.h> 3 4#include <stdint.h> 5#include <stdio.h> 6#include <string.h> 7 8// counters are 16-bit so we need a mutual exclusion access 9unsigned int vbl_cnt, tim_cnt; 10 11void vbl() 12{ 13 // Upon IRQ, interrupts are automatically disabled 14 vbl_cnt++; 15} 16 17void tim() 18{ 19 // Upon IRQ, interrupts are automatically disabled 20 tim_cnt++; 21} 22 23void print_counter() 24{ 25 unsigned int cnt; 26 27 // Ensure mutual exclusion 28 CRITICAL { 29 cnt = tim_cnt; 30 } 31 32 printf(" TIM %u", cnt); 33 gotoxy(9, posy()); 34 35 // Ensure mutual exclusion 36 CRITICAL { 37 cnt = vbl_cnt; 38 } 39 40 printf("- VBL %u\n", cnt); 41} 42 43void main() 44{ 45 // Ensure mutual exclusion 46 CRITICAL { 47 vbl_cnt = tim_cnt = 0; 48 add_VBL(vbl); 49 add_TIM(tim); 50 } 51 52 // Set TMA to divide clock by 0x100 53 TMA_REG = 0x00U; 54 // Set clock to 4096 Hertz 55 TAC_REG = 0x04U; 56 // Handle VBL and TIM interrupts 57 set_interrupts(VBL_IFLAG | TIM_IFLAG); 58 59 while(1) { 60 print_counter(); 61 delay(1000UL); 62 } 63}