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/sm83/set_tile_submap.s

      1         .include        "global.s"
      2 
      3         .title  "Set tile submap"
      4         .module SetTileSubmap
      5 
      6         .area   _DATA
      7 
      8 .image_tile_width::
      9         .ds     0x01
     10 
     11         .area   _INITIALIZED
     12 
     13 __submap_tile_offset::
     14         .ds     0x01
     15 
     16         .area   _INITIALIZER
     17 
     18         .db     0x00
     19 
     20         .area   _HOME
     21 
     22 _set_bkg_submap::
     23         ldhl    sp, #2
     24         ld      a, (hl+)        ; b = x
     25         ld      b, a
     26         ld      c, (hl)         ; c = y
     27         
     28         ldhl    sp, #8
     29         ld      a, (hl)
     30         ldhl    sp, #4
     31         sub     (hl)
     32         ld      (.image_tile_width), a ; .image_tile_width contains corrected width map width
     33         add     (hl)
     34 
     35         ld      d, #0
     36         ld      e, a
     37         ld      a, c
     38         MUL_DE_BY_A_RET_HL
     39         ld      a, b
     40         ADD_A_REG16 h, l
     41         ld      d, h
     42         ld      e, l
     43 
     44         ldhl    sp, #6
     45         ld      a,(hl+)         
     46         ld      h,(hl)          
     47         ld      l,a             
     48         add     hl, de
     49         ld      b, h
     50         ld      c, l
     51 
     52         ldhl    sp, #2
     53         ld      a, (hl+)        ; d = x
     54         and     #0x1f
     55         ld      d, a
     56         ld      a, (hl)         ; e = y
     57         and     #0x1f
     58         ld      e, a
     59 
     60         ldhl    sp, #5
     61         ld      a,(hl-)         ; a = h
     62         ld      h,(hl)          ; h = w
     63         ld      l,a             ; l = h
     64 
     65         jr      .set_xy_bkg_submap
     66 
     67         ;; set window tile table from bc at xy = de of size wh = hl
     68 .set_xy_win_submap::
     69         push    hl              ; store wh
     70         ldh     a,(.LCDC)
     71         and     #LCDCF_WIN9C00
     72         jr      z,.is98
     73         jr      .is9c
     74         ;; set background tile table from (bc) at xy = de of size wh = hl
     75 .set_xy_bkg_submap::
     76         push    hl              ; store wh
     77         ldh     a,(.LCDC)
     78         and     #LCDCF_BG9C00
     79         jr      nz,.is9c
     80 .is98:
     81         ld      hl,#0x9800
     82         jr      .set_xy_submap
     83 .is9c:
     84         ld      hl,#0x9c00
     85         ;; set background tile from (bc) at xy = de, size wh on stack, to vram from address (hl)
     86 .set_xy_submap::
     87         push    bc              ; store source
     88 
     89         swap    e
     90         rlc     e
     91         ld      a,e
     92         and     #0x03
     93         add     h
     94         ld      b,a
     95         ld      a,#0xe0
     96         and     e
     97         add     d
     98         ld      c,a             ; dest bc = hl + 0x20 * y + x
     99 
    100         pop     hl              ; hl = source
    101         pop     de              ; de = wh
    102         push    de              ; store wh
    103         push    bc              ; store dest
    104 3$:                             ; copy w tiles
    105         ld      a, (__submap_tile_offset)
    106         add     (hl)
    107         ld      e, a
    108         WAIT_STAT
    109         ld      a, e
    110         ld      (bc), a
    111         inc     hl
    112         
    113         ld      a, c            ; inc dest and wrap around
    114         and     #0xe0
    115         ld      e, a
    116         ld      a, c
    117         inc     a
    118         and     #0x1f
    119         or      e
    120         ld      c, a
    121 
    122         dec     d
    123         jr      nz, 3$
    124 
    125         ld      a, (.image_tile_width)
    126         ADD_A_REG16 h, l
    127 
    128         pop     bc
    129         pop     de
    130 
    131         dec     e
    132         ret     z
    133 
    134         push    de
    135 
    136         ld      a, b            ; next row and wrap around
    137         and     #0xfc
    138         ld      e, a            ; save high bits
    139 
    140         ld      a,#0x20
    141 
    142         add     c
    143         ld      c, a
    144         adc     b
    145         sub     c
    146         and     #0x03
    147         or      e               ; restore high bits
    148         ld      b, a
    149 
    150         push    bc
    151         
    152         jr      3$

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