cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

irq.c (1059B)


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