git.y1.nz

gbdk-2020

GameBoy Development Kit
download: https://git.y1.nz/archives/gbdk.tar.gz
README | Files | Log | Refs | LICENSE

gbdk-lib/libc/targets/z80/sms_int.s

      1         .include        "global.s"
      2 
      3         .title  "INT Handler"
      4         .module INTHandler
      5 
      6         .globl  .sys_time, .vbl_done, _shadow_VDP_R5
      7         .globl  .OUTI128, .OUTI64, __shadow_OAM_base
      8 
      9         .area   _HOME
     10 
     11 _INT_ISR::
     12         push af
     13         push bc
     14         push de
     15         push hl
     16         push iy
     17         push ix
     18 
     19         in a, (.VDP_STAT)
     20         and #.STATF_INT_VBL
     21         jp z, 2$
     22         ;; handle VBlank
     23 
     24         ;; transfer shadow OAM
     25         ld a, (__shadow_OAM_OFF)        ; check transfer is OFF
     26         or a
     27         jp nz, 1$
     28 
     29         ld a, (_shadow_VDP_R5)
     30         srl a
     31         ld d, a                         ; d == high byte of SAT address
     32         ld c, #.VDP_CMD
     33 
     34         ld a, (__sprites_OFF)
     35         or a
     36         jp nz, 4$
     37 
     38         ld hl, #__shadow_OAM_base
     39         ld h, (hl)
     40         ld l, a
     41         cp h
     42         jp z, 1$
     43 
     44         out (c), a
     45         out (c), d
     46         dec c                           ; c == .VDP_DATA
     47         call .OUTI64
     48         inc c                           ; c == .VDP_CMD
     49         ld a, #0x80
     50         out (c), a
     51         out (c), d
     52         dec c                           ; c == .VDP_DATA
     53         call .OUTI128
     54         jp 1$
     55 4$:
     56         xor a
     57         out (c), a
     58         out (c), d
     59         dec c                           ; c == .VDP_DATA
     60         ld a, #0xd0                     ; disable sprites
     61         out (c), a
     62 1$:
     63         ;; tick time
     64         ld hl, (.sys_time)
     65         inc hl
     66         ld (.sys_time), hl
     67 
     68         ;; set VBlank done flag
     69         ld a, #1
     70         ld (.vbl_done), a
     71 
     72         ;; call user-defined VBlank handlers
     73         .irp idx,.VBLANK_HANDLER0,.VBLANK_HANDLER1,.VBLANK_HANDLER2
     74                 ld hl, (idx)
     75                 ld a, h
     76                 or l
     77                 jp z, 5$
     78                 CALL_HL
     79         .endm
     80 5$:
     81         ld hl, #3$
     82         push hl
     83         ld hl, (.TIM_DISPATCHER)
     84         jp (hl)
     85 
     86 2$:
     87         ;; handle HBlank
     88 
     89         ld hl, (.HBLANK_HANDLER0)
     90         CALL_HL
     91 
     92 3$:
     93         pop ix
     94         pop iy
     95         pop hl
     96         pop de
     97         pop bc
     98         pop af
     99         ei
    100         reti
    101 
    102 ; void remove_LCD (int_handler h) __z88dk_fastcall __preserves_regs(b, c, iyh, iyl);
    103 _remove_LCD::
    104 .remove_LCD::
    105         ld hl, #.empty_function
    106 
    107 ; void add_LCD (int_handler h) __z88dk_fastcall __preserves_regs(b, c, iyh, iyl);
    108 _add_LCD::
    109 .add_LCD::
    110         ld (.HBLANK_HANDLER0), hl
    111         ret
    112 
    113 ; void add_VBL(int_handler h) __z88dk_fastcall __preserves_regs(d, e, iyh, iyl);
    114 _add_VBL::
    115         ld b, h
    116         ld c, l
    117 
    118 .add_VBL::
    119         ld hl, #.VBLANK_HANDLER0
    120 
    121         ;; Add interrupt routine in BC to the interrupt list in HL
    122 .add_int::
    123 1$:
    124         ld a, (hl)
    125         inc hl
    126         or (hl)
    127         jr z, 2$
    128         inc hl
    129         jr 1$
    130 2$:
    131         ld (hl), b
    132         dec hl
    133         ld (hl), c
    134         ret
    135 
    136 ; void remove_VBL(int_handler h) __z88dk_fastcall __preserves_regs(iyh, iyl);
    137 _remove_VBL::
    138         ld b, h
    139         ld c, l
    140 
    141         ;; Remove interrupt routine in BC from the VBL interrupt list
    142         ;; falldown to .remove_int
    143 .remove_VBL::
    144         ld hl, #.VBLANK_HANDLER0
    145 
    146         ;; Remove interrupt BC from interrupt list HL if it exists
    147         ;; Abort if a 0000 is found (end of list)
    148 .remove_int::
    149 1$:
    150         ld e, (hl)
    151         inc hl
    152         ld d, (hl)
    153         inc hl
    154         ld a, e
    155         or d
    156         ret z           ; No interrupt found
    157 
    158         ld a, e
    159         cp c
    160         jr nz, 1$
    161 
    162         ld a, d
    163         cp b
    164         jr nz, 1$
    165 
    166         ld d, h
    167         ld e, l
    168         dec de
    169         dec de
    170 
    171         ;; Now do a memcpy from here until the end of the list
    172 2$:
    173         ld a, (hl)
    174         ldi
    175         or (hl)
    176         ldi
    177         jr nz, 2$
    178         ret
    179 
    180 _remove_SIO::
    181 _remove_JOY::
    182 _add_SIO::
    183 _add_JOY::
    184 .empty_function:
    185         ret
    186 
    187         .area   _INITIALIZED
    188 
    189 .TIM_DISPATCHER::
    190         .ds     0x02
    191 .HBLANK_HANDLER0:
    192         .ds     0x02
    193 .VBLANK_HANDLER0:
    194         .ds     0x02
    195 .VBLANK_HANDLER1:
    196         .ds     0x02
    197 .VBLANK_HANDLER2:
    198         .ds     0x02
    199         .ds     0x02
    200 
    201         .area   _INITIALIZER
    202 
    203         .dw     .empty_function
    204         .dw     .empty_function
    205         .dw     0x0000
    206         .dw     0x0000
    207         .dw     0x0000
    208         .dw     0x0000

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.