Assistance Needed with Sprite and Playfield Programming Issue

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Elijah

Hello everyone,

I'm reaching out for some assistance regarding a programming challenge I've encountered while working on my project.

For the past couple of days, I've been tackling the task of integrating a sprite (currently without movement) into a playfield that I've developed. However, I've hit a roadblock where the playfield fails to render correctly, and worse, the character sprite isn't appearing at all. Instead, there's just a rapid flashing of the playfield. This playfield was perfectly fine before trying to draw the player sprite.

After some investigation, I suspect that I may have exceeded the machine cycle limit, causing the first issue. However, the reason behind the sprite not drawing remains unclear to me. As someone relatively new to this field, I'm hopeful that the solution might be simpler than it appears.

I would greatly appreciate any insights, suggestions, or guidance that you could offer to help resolve this issue. Thank you all in advance for your time and assistance.

Attached below this post, you'll find the relevant sections of the code for your reference.

Best regards,
Elijah
           processor 6502

           include "vcs.h"

           include "macro.h"



           SEG
PFCOL      equ #$A2
Pfheight   equ #192
os_h       equ #34
Playerheight equ #10
BACKGROUND equ #$00
COL0       equ #$1E
            seg.u vars ; uninitialized segment
            org $80
p0_y   ds 1
tp0_y  ds 1
scanline ds 1
           seg main
           ORG $F000




reset: ldx #0 ; Clear RAM and all TIA registers
lda #0
 
clear:        sta 0,x ; $0 to $7F (0-127) reserved OS page zero, $80 to $FF (128-255) user zero page ram.
inx
bne clear

lda #%00000001 ; Set D0 to reflect the playfield
sta CTRLPF ; Apply to the CTRLPF register

lda #PFCOL
sta COLUPF ; Set the PF color

               
                lda #COL0
                sta COLUP0

                sta     WSYNC

                SLEEP   19

                sta     RESP0


StartOfFrame:
                lda   #%00000010
                sta  VSYNC

                sta WSYNC
                sta WSYNC
                sta WSYNC

                ; turn off vsync
                lda #0
                sta VSYNC

                ldx #0
lvblank:    sta WSYNC
                inx
                cpx #37
                bne lvblank

                ldx #0
Drawfield:      lda       #%00000000 ;2
                sta PF0 ;3
                sta PF1 ;3
                sta PF2 ;3

                stx COLUBK  ;note, havent loaded background yet, 3
                sta WSYNC  ;3

                inx ;2
                    cpx #162 ;2?
                    bne Drawfield ;2
                ldx #0 ;2
               
Drawfield2:               lda #%11111100 ;2
                sta PF0 ;3
                sta PF1 ;3
                sta PF2 ;3

                lda #BACKGROUND ;load background color,3
                sta COLUBK ;3
                inx ;2
                    cpx #30 ;2
                    bne Drawfield2 ;2
                ldx #0 ;2

player1       
                lda #Playerheight
                sec
                sbc tp0_y ; Replace "isb tp0_y" with "sbc tp0_y"
                bcs draw_player
                lda #0
draw_player
                tay
                lda mainplayer,y

                sta WSYNC
                sta GRP0
                dec scanline
                bne player1

                sta WSYNC

                lda #%00000010
                sta VBLANK
                ldx #os_h
mainplayer
;just a square
.byte %00000111 ;
.byte %00000111 ;
.byte %00000111 ;
.byte %00000000 ;
.byte %00000000 ;
.byte %00000000 ;
.byte %00000000 ;
.byte %00000000 ;
.byte %00000000 ;
.byte %00000000 ;
.byte %00000000 ;
.byte %00000000 ;
.byte %00000000 ;
.byte %00000000 ;
.byte %00000000 ;
.byte %00000000 ;

overscan:    sta WSYNC
                  inx
                  cpx #30
                  bne overscan

                  jmp StartOfFrame

                  org $FFFA
 

irqvectors:
.word reset          ; NMI
.word reset          ; RESET
.word reset          ; IRQ
               

JetSetIlly

Hi Elijah,

First of all, well done on getting this far. Looking at your code the first problem I see is that you've placed the "mainplayer" data in the middle of the executable code. This means that the 2600 will try execute the data!

So, in this case, after executing the "ldx #os_h" instruction the program counter will be pointing to the memory location holding the %00000111 byte. I'm not sure what instruction this is without looking it up, but I know it can't be anything good.

A few cycles later the program counter will point to the %00000000 instruction, which I know is a BRK instruction. This causes your program to start all over again from the very beginning.

So, first thing you should do is move the mainplayer data block so that it is "outside" the executable data. A good place will be after the "ORG $F000" directive and before the "reset:" label

Looking at the player code I'm sure there are other problems but move the data block as instructed first and see how you get on from there.



https://github.com/JetSetIlly/Gopher2600
@JetSetIlly@mastodon.gamedev.place
@jetsetilly.bsky.social

alex_79

You also need to intialize the decimal flag and stack pointer because the internal state of the CPU is undetermined when you power on the console.
e.g. add this at the beginning of your "reset" code:
   cld         ;clear the decimal flag
   ldx #$ff
   txs         ;set the stack pointer to "$ff"
If you don't, the result of arithmetic operations (ADC, SBC) might not be the expected one, and using subroutines might overwrite your variables or TIA registers.


Elijah

Quote from: JetSetIlly on 31 Jan 2024, 07:54 PMHi Elijah,

First of all, well done on getting this far. Looking at your code the first problem I see is that you've placed the "mainplayer" data in the middle of the executable code. This means that the 2600 will try execute the data!

So, in this case, after executing the "ldx #os_h" instruction the program counter will be pointing to the memory location holding the %00000111 byte. I'm not sure what instruction this is without looking it up, but I know it can't be anything good.

A few cycles later the program counter will point to the %00000000 instruction, which I know is a BRK instruction. This causes your program to start all over again from the very beginning.

So, first thing you should do is move the mainplayer data block so that it is "outside" the executable data. A good place will be after the "ORG $F000" directive and before the "reset:" label

Looking at the player code I'm sure there are other problems but move the data block as instructed first and see how you get on from there.




Hello there,
  First thank you for the kind words!  Moving this section of code has made a difference. While the playfield no longer flashes, it's now appearing for about two frames before disappearing entirely, which I consider to be progress. Still no character, but progress is progress! I'll continue to delve into this until I either crack the solution or someone else discovers and shares it on this thread. The journey to resolve this issue continues! Thank you for your help!

Quote from: alex_79 on 31 Jan 2024, 09:16 PMYou also need to intialize the decimal flag and stack pointer because the internal state of the CPU is undetermined when you power on the console.
e.g. add this at the beginning of your "reset" code:
  cld        ;clear the decimal flag
  ldx #$ff
  txs        ;set the stack pointer to "$ff"
If you don't, the result of arithmetic operations (ADC, SBC) might not be the expected one, and using subroutines might overwrite your variables or TIA registers.



Thank you!

As of now, I haven't made any changes to other parts of the code except for the recommended adjustments mentioned in this thread. This means that anyone who wishes to contribute can easily do so, and should I make substantial progress independently, I'll be sure to share it here in this thread for everyone's benefit. Thanks again everyone!

Andrew Davie



           SEG
PFCOL      equ #$A2
Pfheight   equ #192
os_h       equ #34
Playerheight equ #10
BACKGROUND equ #$00
COL0       equ #$1E


Some comments about the above.
The first "SEG" is superfluous. The equates that follow have nothing to do with a segment - they are just defining name <--> value equivalences for DASM's use.  For example wherever there is COL0 in the code it is replaced by $1E.
Your use of the # in these equates is odd/unusual. Maybe not *incorrect* but definitely confusing. Essentially, the "#" is superfluous here. It's only required in defining immediate values (numbers) in loads to registers (e.g., "lda #10" means load the acccumulator with the value 10, not the contents of location 10).  In equates, you just write the value, no "#"


PFCOL      equ $A2
Pfheight   equ 192
os_h       equ 34
Playerheight equ 10
BACKGROUND equ $00
COL0       equ $1E

Also you have mixed radix usage. You're using decimal and hex in various places. This is perfectly OK, but can lead to mistakes. As a pertinent for-example, you have defined Playerheight as 10 (decimal) but your player data is 16 bytes (decimal) = $10 hex.  So just be aware of that.
You are also mixing case. In the above equates, which appear to all be constants, my personal style is to use all UPPERCASE for such values.  "THIS" is a constant. "this" is variable. But as I said, this is a style thing so up to you.
Another style issue is your mixed usage of defining labels with and without a trailing ":".  Me, I HATE HATE HATE the colons so never use it. Many seem to like it. Up to you, but even worse that ":" is inconsistent use of it. Choose one or the other :). I have been known to refuse to help people who insist on using colons on labels ;)

If I'm reading correctly, there is a big problem where you go "ldx #os_h" and then enter the overscan loop. That should probably be "ldx #0" as you are looping for 30 lines, right?  Since os_h is 34 that would actually loop for around 252 lines instead.


Good luck!

Andrew Davie

Hit "Ctrl+L" on Stella, or "F7" on Gopher to overlay the screen with info. This lets you see the number of scanlines being generated. You should aim for about 262 on NTSC, 312 on PAL.