; *************************************************************************** ; *************************************************************************** ; ; hucc-old-map.asm ; ; Based on the original HuC and MagicKit functions by David Michel and the ; other original HuC developers. ; ; Modifications copyright John Brandwood 2024. ; ; Distributed under the Boost Software License, Version 1.0. ; (See accompanying file LICENSE_1_0.txt or copy at ; http://www.boost.org/LICENSE_1_0.txt) ; ; *************************************************************************** ; *************************************************************************** ; *************************************************************************** ; *************************************************************************** ; ; void __fastcall set_tile_data( unsigned char *tile_ex<_di> ); ; ; void __fastcall __nop set_tile_data( unsigned char __far *tiles, unsigned char num_tiles, unsigned char __far *palette_table, unsigned char tile_type ); ; void __fastcall __nop sgx_set_tile_data( unsigned char __far *tiles, unsigned char num_tiles, unsigned char __far *palette_table, unsigned char tile_type ); ; ; void __fastcall __nop set_far_tile_data( unsigned char tile_bank, unsigned char *tile_addr, unsigned char num_tiles, unsigned char palette_table_bank, unsigned char *palette_table_addr, unsigned char tile_type ); ; void __fastcall __nop sgx_set_far_tile_data( unsigned char tile_bank, unsigned char *tile_addr, unsigned char num_tiles, unsigned char palette_table_bank, unsigned char *palette_table_addr, unsigned char tile_type ); .proc _set_tile_data.1 cly lda [_di], y sta.l vdc_num_tiles iny iny lda [_di], y sta vdc_tile_type iny iny lda [_di], y sta vdc_tile_bank iny iny lda [_di], y sta.l vdc_tile_addr iny lda [_di], y sta.h vdc_tile_addr iny lda [_di], y sta.l vdc_attr_addr iny lda [_di], y sta.h vdc_attr_addr lda #CONST_BANK + _bank_base sta vdc_attr_bank leave .endp huc_map_funcs .procgroup ; *************************************************************************** ; *************************************************************************** ; ; void __fastcall set_tile_address( unsigned int vram<_di> ); ; void __fastcall sgx_set_tile_address( unsigned int vram<_di> ); .if SUPPORT_SGX .proc _sgx_set_tile_address.1 ldx #SGX_VDC_OFFSET ; Offset to SGX VDC. db $F0 ; Turn "clx" into a "beq". .ref _set_tile_address.1 .endp .endif .proc _set_tile_address.1 clx ; Offset to PCE VDC. jsr set_tile_base ; Set TILE number (VRAM / 16). leave .ref _load_tile.1 .endp .alias _set_map_tile_base.1 = _set_tile_address.1 .alias _sgx_set_map_tile_base.1= _sgx_set_tile_address.1 ; *************************************************************************** ; *************************************************************************** ; ; void __fastcall load_tile( unsigned int vram<_di> ); ; void __fastcall sgx_load_tile( unsigned int vram<_di> ); .if SUPPORT_SGX .proc _sgx_load_tile.1 ldx #SGX_VDC_OFFSET ; Offset to SGX VDC. db $F0 ; Turn "clx" into a "beq". .ref _load_tile.1 .endp .endif .proc _load_tile.1 clx ; Offset to PCE VDC. jsr set_tile_base ; Set TILE number (VRAM / 16). lda.l vdc_tile_addr, x sta.l <_bp lda.h vdc_tile_addr, x sta.h <_bp lda vdc_tile_bank, x sta <_bp_bank lda.l vdc_num_tiles, x ; #tiles lo-byte (0 == 256). stz.l <_ax cmp #1 ; CC if 0, CS if NZ. !: ror a eor #$80 ; Invert top bit. ror.l <_ax lsr a ror.l <_ax ldy vdc_tile_type, x ; Set C if using 8x8. cpy #16 beq !+ lsr a ror.l <_ax lsr a ror.l <_ax !: sta.h <_ax jmp load_vram_x ; Load VRAM with the tiles. ; This is a subroutine so that set_tile_address() can use it. set_tile_base: lda.l <_di ; Get VRAM address. ldy.h <_di ; Do NOT alter _di! sty <__temp ; Set TILE base = (VRAM / 16). lsr <__temp ror a lsr <__temp ror a lsr <__temp ror a lsr <__temp ror a sta.l vdc_tile_base, x lda <__temp sta.h vdc_tile_base, x rts .endp ; *************************************************************************** ; *************************************************************************** ; ; void __fastcall load_map( unsigned char bat_x<_al>, unsigned char bat_y<_ah>, int map_x<_bx>, int map_y<_dx>, unsigned char tiles_w<_cl>, unsigned char tiles_h<_ch> ); ; void __fastcall sgx_load_map( unsigned char bat_x<_al>, unsigned char bat_y<_ah>, int map_x<_bx>, int map_y<_dx>, unsigned char tiles_w<_cl>, unsigned char tiles_h<_ch> ); ; ; Transfer a HuC tiled map into the BAT in VRAM ; ; _al = BAT x screen coordinate (in tile units) ; _ah = BAT y screen coordinate ; _bl = MAP x start coordinate in the map ; _bh = MAP y start coordinate ; _cl = #TILES width to draw ; _ch = #TILES height to draw ; ; set by load_map_init() ; _di = BAT address ; _si = palette index table ptr ; _bp = map address .if SUPPORT_SGX .proc _sgx_load_map.6 ldx #SGX_VDC_OFFSET ; Offset to SGX VDC. db $F0 ; Turn "clx" into a "beq". .ref _load_map.6 .endp .endif .proc _load_map.6 .bss ; ************** ; 16-bytes of VDC HuC map info. ; ; N.B. MUST be 16-bytes before the SGX versions to use PCE_VDC_OFFSET. ; Initialized by set_map_data() vdc_map_wrap ds 1 ; unused vdc_map_bank ds 1 vdc_map_addr ds 2 vdc_map_width ds 1 vdc_map_height ds 1 ; Initialized by set_tile_data() vdc_tile_type ds 1 ; HuC sets 8 or 16 vdc_num_tiles ds 1 ; HuC sets 0..255, 0==256. vdc_tile_addr ds 2 ; Where the TILE data is in ROM. vdc_tile_bank ds 1 vdc_attr_bank ds 1 ; Where the ATTR data is in ROM. vdc_attr_addr ds 2 ; Initialized by load_tile() or set_tile_address() vdc_tile_base ds 2 ; Where the TILE data is in VRAM / 16. .if SUPPORT_SGX ; ************** ; 16-bytes of SGX HuC map info. ; ; N.B. MUST be 16-bytes after the VDC versions to use SGX_VDC_OFFSET. ; Initialized by sgx_set_map_data() sgx_map_wrap ds 1 ; unused sgx_map_bank ds 1 sgx_map_addr ds 2 sgx_map_width ds 1 sgx_map_height ds 1 ; Initialized by sgx_set_tile_data() sgx_tile_type ds 1 ; HuC sets 8 or 16 sgx_num_tiles ds 1 ; HuC sets 0..255, 0==256. sgx_tile_addr ds 2 ; Where the TILE data is in ROM. sgx_tile_bank ds 1 sgx_attr_bank ds 1 ; Where the ATTR data is in ROM. sgx_attr_addr ds 2 ; Initialized by sgx_load_tile() or sgx_set_tile_address() sgx_tile_base ds 2 ; Where the TILE data is in VRAM / 16. .endif .code .bat_x = _al .bat_y = _ah .map_x = _bl .map_y = _bh .tiles_w = _cl .tiles_h = _ch .bat_line = _di .map_line = _bp .attr_tbl = _si clx ; Offset to PCE VDC. ; wrap 16-bit signed map x coordinate, and only use the bottom byte. .wrap_map_x: lda.l <_bx ldy vdc_map_width, x ; If map_width == 256, then beq .mapx_ok ; just take the lo-byte of X. ldy.h <_bx bmi .mapx_neg .mapx_pos: sec sbc vdc_map_width, x bcs .mapx_pos dey bpl .mapx_pos .mapx_neg: clc adc vdc_map_width, x bcc .mapx_neg iny bmi .mapx_neg .mapx_ok: sta <.map_x ; 0 <= map_x < mapwidth ; wrap 16-bit signed map y coordinate, and only use the bottom byte. .wrap_map_y: lda.l <_dx ldy vdc_map_height, x ; If map_height == 256, then beq .mapy_ok ; just take the lo-byte of Y. ldy.h <_dx bmi .mapy_neg .mapy_pos: sec sbc vdc_map_height, x bcs .mapy_pos dey bpl .mapy_pos .mapy_neg: clc adc vdc_map_height, x bcc .mapy_neg iny bmi .mapy_neg .mapy_ok: sta <.map_y ; 0 <= map_y < mapheight tma2 ; Preserve MPR2, MPR3, MPR4. pha tma3 pha tma4 pha ; ****************************** ; Calculate the map/bat pointers ; ****************************** jsr !calc_bat_tile+ ; Calculate bat tile address. jsr !calc_map_line+ ; Calculate map line address. ; Now actually draw the map! lda vdc_tile_type, x cmp #16 bcc .draw_line8 jmp .draw_line16 ; ****************************** ; Draw a bytemap of 8x8 tiles ; ****************************** .next_line8: clc ; 8x8 tiles jsr .next_map_line .draw_line8: lda <.tiles_w sta.l <__temp ; width counter in __temp.l lda vdc_bat_width, x ; calculate #tiles to edge sec sbc <.bat_x sta.h <__temp ; wrap counter in __temp.h ldy <.map_x stz , unsigned char map_y<_bh> ); ; unsigned char __fastcall sgx_map_get_tile( unsigned char map_x<_bl>, unsigned char map_y<_bh> ); .if SUPPORT_SGX .proc _sgx_map_get_tile.2 ldx #SGX_VDC_OFFSET ; Offset to SGX VDC. db $F0 ; Turn "clx" into a "beq". .ref _map_get_tile.2 .endp .endif .proc _map_get_tile.2 .map_x = _bl ; N.B. These MUST be the same .map_y = _bh ; addresses used by _load_map.6 .map_line = _bp clx ; Offset to PCE VDC. tma3 ; Preserve MPR3, MPR4. pha tma4 pha jsr !calc_map_line- ; Calculate map line address. ldy <.map_x ; Read the tile from the map. lda [.map_line], y tax ; Put return code in X. pla ; Restore MPR3, MPR4. tam4 pla tam3 cly ; Return code in Y:X, X -> A. leave ; Return and copy X -> A. .ref _load_map.6 .endp ; *************************************************************************** ; *************************************************************************** ; ; void __fastcall map_put_tile( unsigned char map_x<_bl>, unsigned char map_y<_bh>, unsigned char tile<_al> ); ; void __fastcall sgx_map_put_tile( unsigned char map_x<_bl>, unsigned char map_y<_bh>, unsigned char tile<_al> ); .if SUPPORT_SGX .proc _sgx_map_put_tile.3 ldx #SGX_VDC_OFFSET ; Offset to SGX VDC. db $F0 ; Turn "clx" into a "beq". .ref _map_put_tile.3 .endp .endif .proc _map_put_tile.3 .map_x = _bl ; N.B. These MUST be the same .map_y = _bh ; addresses used by _load_map.6 .map_line = _bp clx ; Offset to PCE VDC. tma3 ; Preserve MPR3, MPR4. pha tma4 pha jsr !calc_map_line- ; Calculate map line address. ldy <.map_x ; Write the tile to the map. lda <_al sta [.map_line], y pla ; Restore MPR3, MPR4. tam4 pla tam3 leave ; All done! .ref _load_map.6 .endp ; *************************************************************************** ; *************************************************************************** ; ; void __fastcall put_tile( unsigned char tile<_bl>, unsigned char bat_x<_al>, unsigned char bat_y<_ah> ); ; void __fastcall sgx_put_tile( unsigned char tile<_bl>, unsigned char bat_x<_al>, unsigned char bat_y<_ah> ); ; ; Transfer a single HuC map tile to the BAT in VRAM ; ; _al = BAT x screen coordinate (in tile units) ; _ah = BAT y screen coordinate ; _di = BAT address ; _bl = MAP tile number to write .if SUPPORT_SGX .proc _sgx_put_tile.3 ldx #SGX_VDC_OFFSET ; Offset to SGX VDC. db $F0 ; Turn "clx" into a "beq". .ref _put_tile.3 .endp .endif .proc _put_tile.3 .bat_x = _al ; N.B. These MUST be the same .bat_y = _ah ; addresses used by _load_map.6 .bat_tile = _bl .bat_line = _di .attr_tbl = _si clx ; Offset to PCE VDC. tma2 ; Preserve MPR2. pha jsr !calc_bat_tile- ; Calculate bat tile address. stz