Skip to content

Commit

Permalink
Removed a couple of uses of multiplication registers + fixed a bug wi…
Browse files Browse the repository at this point in the history
…th screen clearin gin the SA-1 build
  • Loading branch information
RPGHacker committed Jan 13, 2024
1 parent b8baa24 commit 8b95108
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 43 deletions.
10 changes: 9 additions & 1 deletion patches/shared/shared.asm
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,12 @@ function dma_source_indirect_word(address, bank) = ((bank&$FF)<<24)|(address&$FF
function dma_size_indirect(address) = $01000000|(address&$FFFFFF)


; Same as above, but also indicates that the passed in address lies in the direct page.
; This lets the %dma_transfer() macro know to use an LDA.b instead of another LDA.

function dma_size_indirect_dp(address) = $02000000|(address&$FFFFFF)


; A simple macro for preparing access to VRAM.

macro configure_vram_access(access_mode, increment_mode, increment_size, address_remap, vram_address)
Expand Down Expand Up @@ -254,12 +260,14 @@ macro dma_transfer(channel, dma_settings, destination, source, num_bytes)
!temp_num_bytes_resolved #= <num_bytes>
!temp_dma_num_bytes_mode #= ((!temp_num_bytes_resolved>>24)&$FF)
assert !temp_dma_num_bytes_mode <= 1, "Invalid argument passed to num_bytes of %dma_transfer()."
assert !temp_dma_num_bytes_mode <= 2, "Invalid argument passed to num_bytes of %dma_transfer()."
if !temp_dma_num_bytes_mode == 0
!temp_dma_num_bytes = "lda.w #<num_bytes>"
elseif !temp_dma_num_bytes_mode == 1
!temp_dma_num_bytes = "lda <num_bytes>"
elseif !temp_dma_num_bytes_mode == 2
!temp_dma_num_bytes = "lda.b <num_bytes>"
endif

lda.b #<dma_settings>
Expand Down
63 changes: 48 additions & 15 deletions patches/vwf_dialogues/vwf_dialogues.asm
Original file line number Diff line number Diff line change
Expand Up @@ -266,22 +266,55 @@ macro vwf_mvn_transfer(bytes, source, destination, current_cpu, ...)

phb
if sizeof(...) > 0
lda.b #<bytes> ; Calculate starting index
sta.w select(!temp_currently_on_sa1_cpu,$2251,$211B)
lda.b #<bytes>>>8
sta.w select(!temp_currently_on_sa1_cpu,$2252,$211B)
stz.w select(!temp_currently_on_sa1_cpu,$2250,$211C)
lda <...[0]>
sta.w select(!temp_currently_on_sa1_cpu,$2253,$211C)
if !temp_currently_on_sa1_cpu
stz $2254
nop
!temp_start_index := <...[0]>
!temp_log2 #= floor(log2(<bytes>))

if 2**!temp_log2 == <bytes>
; RPG Hacker: The multiplier is logarithmic - we can use a number of ASL to do the multiplication.
; If the exponent gets too large, this might be less performant than an actual multiplication,
; but I don't think we'll ever get to those severe dimensions, so I won't optimize for them.
lda.b #$00
xba
lda !temp_start_index
rep #$31
asl #!temp_log2
adc.w #<source>
tax
elseif !temp_currently_on_sa1_cpu == !vwf_cpu_snes && <bytes> < $100
; If the number of bytes is representable as a single byte (we assume the start index to always be
; representable as a single byte), and we're currently on the SNES CPU, do the multiplication via the
; regular multiplication registers instead of the mode 7 registers (this can prevent certain quirks).
lda.b #<bytes> ; Calculate starting index
sta $4202
lda !temp_start_index
sta $4203
rep #$31
lda.w #<source>
nop #2
adc $4216
tax
else
; If none of the above is possible, fall back to a generic solution.
lda.b #<bytes> ; Calculate starting index
sta.w select(!temp_currently_on_sa1_cpu,$2251,$211B)
lda.b #<bytes>>>8
sta.w select(!temp_currently_on_sa1_cpu,$2252,$211B)
stz.w select(!temp_currently_on_sa1_cpu,$2250,$211C)
lda !temp_start_index
sta.w select(!temp_currently_on_sa1_cpu,$2253,$211C)
if !temp_currently_on_sa1_cpu
stz $2254
nop
endif

rep #$31
lda.w select(!temp_currently_on_sa1_cpu,$2306,$2134) ; Add source address
adc.w #<source> ; to get new source address
tax
endif

rep #$31
lda.w select(!temp_currently_on_sa1_cpu,$2306,$2134) ; Add source address
adc.w #<source> ; to get new source address
tax
undef "temp_log2"
undef "temp_start_index"
else
rep #$30
ldx.w #<source>
Expand Down Expand Up @@ -5710,7 +5743,7 @@ endif
sep #$20

%configure_vram_access(VRamAccessMode.Write, VRamIncrementMode.OnHighByte, VRamIncrementSize.1Byte, VRamAddressRemap.None, $02)
%dma_transfer(!vwf_dma_channel_nmi, DmaMode.WriteOnce, $2118, dma_source_indirect_word($00, bank(!vwf_buffer_text_box_tilemap)), dma_size_indirect($04))
%dma_transfer(!vwf_dma_channel_nmi, DmaMode.WriteOnce, $2118, dma_source_indirect_word($00, bank(!vwf_buffer_text_box_tilemap)), dma_size_indirect_dp($04))

rep #$20
lda !vwf_clear_box_remaining_bytes
Expand Down
50 changes: 23 additions & 27 deletions patches/vwf_dialogues/vwfroutines.asm
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ endmacro
; During processing: Pointer (24-bit) to font, offset by two 8x8 tile (to access right half of 16x16 tiles)
; $0E: Current pixel offset into destination memory buffer
; $0F: #$01 if this is writing first tile in the current buffer, otherwise #$00
; Used as temporary memory during processing
%vwf_register_shared_routine(VWF_GenerateVWF)
!vwf_var_ram_and_gfx_ram_in_same_bank #= equal(bank(!vwf_var_ram), bank(!vwf_gfx_ram))

Expand Down Expand Up @@ -195,28 +196,30 @@ endif
dex
bpl.b ..Check2
.Skip:
if !use_sa1_mapping
lda.b #$01
sta.l $2250
endif
if !vwf_var_ram_and_gfx_ram_in_same_bank != false
ldy.b #$00
else
ldx.b #$00
txy
endif
lda.b #$00
pha

lda.w !vwf_current_pixel ; Adjust destination address
clc
adc.w !vwf_char_width
sta.l select(!use_sa1_mapping,$2251,$4204)
lda.b #$00
sta.l select(!use_sa1_mapping,$2252,$4205)
lda.b #$08
sta.l select(!use_sa1_mapping,$2253,$4206)
if !use_sa1_mapping
sta $0F

lsr #3 ; Compute quotient of division by 8
pha

lda.b #$00
sta.l $2254
endif
pha

lda $0F ; Compute remainder of division by 8
and.b #%00000111
pha

lda.w !vwf_first_tile ; Skip one step if first tile in line
beq .Combine
stz.w !vwf_first_tile
Expand Down Expand Up @@ -255,7 +258,7 @@ endif
iny
cpy.b #$60
bne.b .Copy
lda.l select(!use_sa1_mapping,$2306,$4214)
lda $03,s
asl
pha
clc
Expand All @@ -266,25 +269,18 @@ endif
sep #$20
adc.w !vwf_buffer_index
sta.w !vwf_buffer_index
if !use_sa1_mapping
lda.b #$00
sta.l $2250
endif
lda.l select(!use_sa1_mapping,$2308,$4216)
lda $01,s
sta.w !vwf_current_pixel
lda.b #$20
sta.l select(!use_sa1_mapping,$2251,$4202)
lda.l select(!use_sa1_mapping,$2306,$4214)
sta.l select(!use_sa1_mapping,$2253,$4203)
lda $03,s
clc
adc.w !vwf_current_x
sta.w !vwf_current_x
if !use_sa1_mapping
lda.b #$00
sta.l $2254
endif
rep #$21
lda.l select(!use_sa1_mapping,$2308,$4216)
pla
pla
; The XBA here is necessary, because bytes are stored on the stack in reverse order
; due to the fact we use stack-relative indexing to access them.
xba
adc.b $09
sta.b $09
dec.w !vwf_num_bytes ; Adjust number of bytes
Expand Down

0 comments on commit 8b95108

Please sign in to comment.