From 160a9afafb6b7abe96d946cde3ce898790faf10d Mon Sep 17 00:00:00 2001 From: Lucas Scharenbroich Date: Sat, 17 Jul 2021 21:00:46 -0500 Subject: [PATCH] Add proper handling of wraparound in vertical positioning --- src/Actions.s | 7 +++--- src/App.Main.s | 7 ++++++ src/Render.s | 9 ++++++-- src/blitter/BG1.s | 46 ++++++++++++++++++++++++++++++++++------ src/blitter/DirectPage.s | 16 ++++++++++---- src/blitter/Horz.s | 3 ++- src/blitter/Vert.s | 7 ++++-- 7 files changed, 76 insertions(+), 19 deletions(-) diff --git a/src/Actions.s b/src/Actions.s index 9008b60..f15fc3e 100644 --- a/src/Actions.s +++ b/src/Actions.s @@ -27,9 +27,9 @@ MoveRight pla rts -MoveDown +MoveUp clc - adc StartY ; Increment the virtual X-position + adc StartY ; Increment the virtual Y-position jsr SetBG0YPos lda StartY @@ -39,7 +39,7 @@ MoveDown jsr DoFrame rts -MoveUp +MoveDown pha lda StartY sec @@ -104,3 +104,4 @@ FPSStr str 'FPS' + diff --git a/src/App.Main.s b/src/App.Main.s index 5e5f071..a2b19cb 100644 --- a/src/App.Main.s +++ b/src/App.Main.s @@ -643,10 +643,16 @@ BlitInit stz ScreenTileHeight stz ScreenTileWidth stz StartX + stz StartXMod164 stz StartY + stz StartYMod208 stz EngineMode stz DirtyBits stz LastPatchOffset + stz BG1StartX + stz BG1StartXMod164 + stz BG1StartY + stz BG1StartYMod208 ]step equ 0 lup 13 @@ -976,5 +982,6 @@ qtRec adrl $0000 + diff --git a/src/Render.s b/src/Render.s index 10d4e0c..6d37211 100644 --- a/src/Render.s +++ b/src/Render.s @@ -64,10 +64,13 @@ Render jsr ShadowOff jsr ShadowOn - jsr _ApplyBG0XPos ; Patch the PEA instructions with exit BRA opcode +; It's important to do _ApplyBG0YPos first because it calculates the value of StartY % 208 which is +; used in all of the other loops + jsr _ApplyBG0YPos ; Set stack addresses for the virtual lines to the physical screen + jsr _ApplyBG0XPos ; Patch the PEA instructions with exit BRA opcode + jsr _ApplyBG1YPos ; Adjust the index values into the BG1 bank buffer jsr _ApplyBG1XPos ; Adjust the direct page pointers to the BG1 bank - jsr _ApplyBG1YPos ldx #0 ; Blit the full virtual buffer to the screen ldy ScreenHeight @@ -90,3 +93,5 @@ Render + + diff --git a/src/blitter/BG1.s b/src/blitter/BG1.s index 0b2df98..732d006 100644 --- a/src/blitter/BG1.s +++ b/src/blitter/BG1.s @@ -1,16 +1,38 @@ ; Initialization and setup routines for the second background _InitBG1 - jsr _ApplyBG1XPos jsr _ApplyBG1YPos + jsr _ApplyBG1XPos rts SetBG1XPos - sta BG1StartX - rts + cmp BG1StartX + beq :out ; Easy, if nothing changed, then nothing changes + + ldx BG1StartX ; Load the old value (but don't save it yet) + sta BG1StartX ; Save the new position + + lda #DIRTY_BIT_BG1_X + tsb DirtyBits ; Check if the value is already dirty, if so exit + bne :out ; without overwriting the original value + + stx OldBG1StartX ; First change, so preserve the value +:out rts + SetBG1YPos - sta BG1StartY - rts + cmp BG1StartY + beq :out ; Easy, if nothing changed, then nothing changes + + ldx BG1StartY ; Load the old value (but don't save it yet) + sta BG1StartY ; Save the new position + + lda #DIRTY_BIT_BG1_Y + tsb DirtyBits ; Check if the value is already dirty, if so exit + bne :out ; without overwriting the original value + + stx OldBG1StartY ; First change, so preserve the value +:out rts + ; Everytime either BG1 or BG0 X-position changes, we have to update the direct page values. We ; *could* do this by adjusting the since address offset, but we have to change up to 200 values @@ -86,9 +108,12 @@ _ApplyBG1YPos :draw_count equ tmp2 :ytbl_idx equ tmp3 - stz :ytbl_idx ; Start copying from the first entry in the table + lda BG1StartY + jsr Mod208 + sta BG1StartYMod208 + sta :ytbl_idx ; Start copying from the first entry in the table - lda StartY ; This is the base line of the virtual screen + lda StartYMod208 ; This is the base line of the virtual screen sta :virt_line ; Keep track of it lda ScreenHeight @@ -246,6 +271,13 @@ CopyBG1YTableToBG1Addr + + + + + + + diff --git a/src/blitter/DirectPage.s b/src/blitter/DirectPage.s index df16ed3..a2dec4c 100644 --- a/src/blitter/DirectPage.s +++ b/src/blitter/DirectPage.s @@ -24,12 +24,16 @@ OldStartY equ 32 LastPatchOffset equ 34 ; Offset into code field that was patched with BRA instructions StartXMod164 equ 36 +StartYMod208 equ 38 -BG1StartX equ 38 ; Logical offset of the second background -BG1StartXMod164 equ 40 +BG1StartX equ 40 ; Logical offset of the second background +BG1StartXMod164 equ 42 -BG1StartY equ 42 -BG1StartYMod208 equ 44 +BG1StartY equ 44 +BG1StartYMod208 equ 46 + +OldBG1StartX equ 48 +OldBG1StartY equ 50 bstk equ 208 ; 16-byte stack to push bank addresses @@ -66,6 +70,10 @@ DIRTY_BIT_BG1_Y equ $0008 + + + + diff --git a/src/blitter/Horz.s b/src/blitter/Horz.s index 781629d..90401d2 100644 --- a/src/blitter/Horz.s +++ b/src/blitter/Horz.s @@ -146,7 +146,7 @@ _ApplyBG0XPos ; This code is fairly succinct. See the corresponding code in Vert.s for more detailed comments. :ok - lda StartY ; This is the base line of the virtual screen + lda StartYMod208 ; This is the base line of the virtual screen sta :virt_line ; Keep track of it lda ScreenHeight @@ -745,3 +745,4 @@ SetCodeEntryOpcode + diff --git a/src/blitter/Vert.s b/src/blitter/Vert.s index 9db476a..7e1d144 100644 --- a/src/blitter/Vert.s +++ b/src/blitter/Vert.s @@ -5,8 +5,7 @@ ; SetBG0YPos ; -; Set the virtual position of the primary background layer. In addition to -; updating the direct page state locations, this routine needs to +; Set the virtual position of the primary background layer. SetBG0YPos cmp StartY beq :out ; Easy, if nothing changed, then nothing changes @@ -38,6 +37,9 @@ _ApplyBG0YPos stz :rtbl_idx ; Start copying from the first entry in the table lda StartY ; This is the base line of the virtual screen + jsr Mod208 + sta StartYMod208 + sta :virt_line ; Keep track of it ; copy a range of address from the table into the destination bank. If we restrict ourselves to @@ -188,3 +190,4 @@ CopyRTableToStkAddr sta: STK_ADDR+$0000,y :none rts +