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

      1     .include    "global.s"
      2 
      3     .area   GBDKOVR (PAG, OVR)
      4     _set_bkg_submap_PARM_3::
      5     _set_bkg_based_submap_PARM_3::
      6     _set_win_submap_PARM_3::
      7     _set_win_based_submap_PARM_3::  .ds 1
      8     _set_bkg_submap_PARM_4::
      9     _set_bkg_based_submap_PARM_4::
     10     _set_win_submap_PARM_4::
     11     _set_win_based_submap_PARM_4::  .ds 1
     12     _set_bkg_submap_PARM_5::
     13     _set_bkg_based_submap_PARM_5::  
     14     _set_win_submap_PARM_5::
     15     _set_win_based_submap_PARM_5::  .ds 2
     16     _set_bkg_submap_PARM_6::
     17     _set_bkg_based_submap_PARM_6::
     18     _set_win_submap_PARM_6::
     19     _set_win_based_submap_PARM_6::  .ds 1
     20     _set_bkg_based_submap_PARM_7::
     21     _set_win_based_submap_PARM_7::  .ds 1
     22     .xpos:                          .ds 1
     23     .ypos:                          .ds 1
     24     .num_rows:                      .ds 1
     25     .src_tiles:                     .ds 2
     26     .remainder:                     .ds 1
     27     .ppuhi:                         .ds 1
     28     .stripe_loop_counter:           .ds 1
     29 
     30     .define .width          "_set_bkg_submap_PARM_3"
     31     .define .height         "_set_bkg_submap_PARM_4"
     32     .define .tiles          "_set_bkg_submap_PARM_5"
     33     .define .map_width      "_set_bkg_submap_PARM_6"
     34     .define .tile_offset    "_set_bkg_based_submap_PARM_7"
     35 
     36     .area   _HOME
     37 
     38 .ifdef NES_TILEMAP_S
     39 .define PPUHI_MASK "#>PPU_NT0"
     40 .else
     41 .define PPUHI_MASK "*.ppuhi"
     42 .endif
     43 
     44 .ifdef NES_WINDOW_LAYER
     45 _set_win_submap::
     46     ldy #0
     47     sty *.tile_offset
     48 _set_win_based_submap::
     49     lda *__current_vram_cfg_write
     50     pha
     51     ora #PPUHI_WIN
     52     sta *__current_vram_cfg_write
     53     jsr _set_bkg_based_submap
     54     pla
     55     sta *__current_vram_cfg_write
     56     rts
     57 .endif
     58 
     59 _set_bkg_submap::
     60     ldy #0
     61     sty *.tile_offset
     62 _set_bkg_based_submap::
     63     sta *.xpos
     64     stx *.ypos
     65     lda .tiles
     66     clc
     67     adc *.xpos
     68     sta *.src_tiles
     69     lda .tiles+1
     70     adc #0
     71     sta *.src_tiles+1
     72     ; += ypos * map_width
     73     lda *.ypos
     74     ldx *.map_width
     75     jsr __muluchar
     76     clc
     77     adc *.src_tiles
     78     sta *.src_tiles
     79     txa
     80     adc *.src_tiles+1
     81     sta *.src_tiles+1
     82 .set_bkg_common::
     83     ;
     84     lda *.height
     85     sta *.num_rows
     86 .ifne NT_2W
     87     ; .ppuhi = (xpos >> 3) & 0b00000100
     88     lda *.xpos
     89     lsr
     90     lsr
     91     lsr
     92     and #0b00000100
     93     ora #>PPU_NT0
     94     sta *.ppuhi
     95 .else
     96 .ifne NT_2H
     97     lda #>PPU_NT0
     98     sta *.ppuhi
     99 .endif
    100 .endif
    101     ; xpos %= NT_WIDTH
    102     lda *.xpos
    103     and #NT_WIDTH-1
    104     sta *.xpos
    105     ldx *.ypos
    106     ; ypos %= NT_HEIGHT
    107     lda *.ypos
    108     jsr .div_mod_height
    109     sta *.ypos
    110 .ifne NT_2H
    111     ; .ppuhi |= (ypos % DEVICE_SCREEN_BUFFER_HEIGHT) & 0b00001000
    112     txa
    113     asl
    114     asl
    115     asl
    116     and #0b00001000
    117     ora *.ppuhi
    118     sta *.ppuhi
    119 .endif
    120     ; Prefer vertical stripes if height > width
    121     lda *.height
    122     cmp *.width
    123     bcc _set_bkg_submap_horizontalStripes
    124     beq _set_bkg_submap_horizontalStripes
    125     jmp _set_bkg_submap_verticalStripes
    126 _set_bkg_submap_horizontalStripes:
    127     ; Adjust width and calculate remainder if row wraps from right to left
    128     lda *.xpos
    129     clc
    130     adc *.width
    131     sec
    132     sbc #NT_WIDTH
    133     sta *.remainder
    134     bmi 1$
    135     lda *.width
    136     sec
    137     sbc *.remainder
    138     sta *.width
    139 1$:
    140 _set_bkg_submap_horizontalStripes_rowLoop:
    141     ldx *.width
    142     stx *.stripe_loop_counter
    143     jsr .setup_stripe_address
    144     ldy #0
    145     ldx *.tmp+1
    146     lda *.tmp
    147     jsr .write_horizontal_stripe
    148     ; if wrapped around, write remainder
    149     lda *.remainder
    150     bmi _set_bkg_submap_horizontalStripes_rowLoopEnd
    151     bne _set_bkg_submap_horizontalStripes_remainder
    152 _set_bkg_submap_horizontalStripes_rowLoopEnd:
    153     ; .src_tiles += .map_width
    154     lda *.map_width
    155     clc
    156     adc *.src_tiles
    157     sta *.src_tiles
    158     bcc 2$
    159     inc *.src_tiles+1
    160 2$:
    161     ; ypos += 1, with wrap back to 0 if gone past bottom of nametable
    162     lda *.ypos
    163     clc
    164     adc #1
    165     cmp #NT_HEIGHT
    166     bcc 1$
    167     adc #0xE1 ; 0x02-0x20-C   ; (carry assumed set)
    168 .ifne NT_2H
    169     ; Flip nametable Y bit after storing wrapped ypos
    170     sta *.ypos
    171     lda *.ppuhi
    172     eor #0b00001000
    173     sta *.ppuhi
    174     bcs 3$   ; Carry still set, use BCS in place of JMP
    175 .endif
    176 1$:
    177     sta *.ypos
    178 3$:
    179     dec *.num_rows
    180     bne _set_bkg_submap_horizontalStripes_rowLoop
    181     rts
    182 
    183 _set_bkg_submap_horizontalStripes_remainder:
    184     ldx *.remainder
    185     stx *.stripe_loop_counter
    186 .ifne NT_2W + NT_2H
    187     lda *.tmp+1
    188     ora *.ppuhi
    189     eor #0b00000100
    190     tax
    191 .else
    192     ldx *.tmp+1
    193 .endif
    194     lda *.tmp
    195     and #0xE0   ; Always start remainder at X=0
    196     jsr .write_horizontal_stripe
    197     jmp _set_bkg_submap_horizontalStripes_rowLoopEnd
    198 
    199 
    200 .define .num_cols  ".num_rows"
    201 
    202 _set_bkg_submap_verticalStripes:
    203     ; Adjust height and calculate remainder if row wraps from bottom to top
    204     lda *.ypos
    205     clc
    206     adc *.height
    207     sec
    208     sbc #NT_HEIGHT
    209     sta *.remainder
    210     bmi 1$
    211     lda *.height
    212     sec
    213     sbc *.remainder
    214     sta *.height
    215 1$:
    216     lda *.src_tiles
    217     sta *.tiles
    218     lda *.src_tiles+1
    219     sta *.tiles+1
    220     ;
    221     lda *.width
    222     sta *.num_cols
    223     ldy #0
    224 _set_bkg_submap_verticalStripes_columnLoop:
    225     ldx *.height
    226     stx *.stripe_loop_counter
    227     lda *.tiles
    228     sta *.src_tiles
    229     lda *.tiles+1
    230     sta *.src_tiles+1
    231     ;
    232     jsr .setup_stripe_address
    233     ldx *.tmp+1
    234     lda *.tmp
    235     jsr .write_vertical_stripe
    236     ; if wrapped around, write remainder
    237     lda *.remainder
    238     bmi _set_bkg_submap_verticalStripes_columnLoopEnd
    239     bne _set_bkg_submap_verticalStripes_remainder
    240 _set_bkg_submap_verticalStripes_columnLoopEnd:
    241     iny
    242     inc *.xpos
    243     dec *.num_cols
    244     bne _set_bkg_submap_verticalStripes_columnLoop
    245     rts
    246 
    247 _set_bkg_submap_verticalStripes_remainder:
    248     ldx *.remainder
    249     stx *.stripe_loop_counter
    250 .ifne NT_2H
    251     lda *.ppuhi
    252     eor #0b00001000
    253     tax
    254 .else
    255     ldx PPUHI_MASK
    256 .endif
    257     lda *.tmp
    258     and #NT_WIDTH-1   ; Always start remainder at Y=0
    259     jsr .write_vertical_stripe
    260     jmp _set_bkg_submap_verticalStripes_columnLoopEnd
    261 
    262 .div_mod_height:
    263     ldx #0
    264     sec
    265 1$:
    266     sbc #NT_HEIGHT
    267     bcc 2$
    268     inx
    269     jmp 1$
    270 2$:
    271     adc #NT_HEIGHT
    272     rts
    273 
    274 .setup_stripe_address:
    275     ; tmp = ppuhi | (ypos << 5) | xpos
    276     lda #(PPU_NT0 >> 13)
    277     sta *.tmp+1
    278     lda *.ypos
    279     asl
    280     rol *.tmp+1
    281     asl
    282     rol *.tmp+1
    283     asl
    284     rol *.tmp+1
    285     asl
    286     rol *.tmp+1
    287     asl
    288     rol *.tmp+1
    289     ora *.xpos
    290     sta *.tmp
    291     ;
    292     lda *.tmp+1
    293 .ifne NT_2W + NT_2H
    294     ora PPUHI_MASK
    295 .endif
    296     sta *.tmp+1
    297     rts
    298 
    299 .write_horizontal_stripe:
    300     jsr .ppu_stripe_begin_horizontal
    301 1$:
    302     lda [*.src_tiles],y
    303     iny
    304     clc
    305     adc *.tile_offset
    306     jsr .ppu_stripe_write_byte
    307     dec *.stripe_loop_counter
    308     bne 1$
    309     jmp .ppu_stripe_end
    310 
    311 .write_vertical_stripe:
    312     jsr .ppu_stripe_begin_vertical
    313 1$:
    314     lda [*.src_tiles],y
    315     clc
    316     adc *.tile_offset
    317     jsr .ppu_stripe_write_byte
    318     ; .src_tiles += width
    319     lda *.map_width
    320     clc
    321     adc *.src_tiles
    322     sta *.src_tiles
    323     bcc 2$
    324     inc *.src_tiles+1
    325 2$:
    326     dec *.stripe_loop_counter
    327     bne 1$
    328     jmp .ppu_stripe_end

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