How to organize code? ("good code")

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

User avatar
Ben Boldt
Posts: 1148
Joined: Tue Mar 22, 2016 8:27 pm
Location: Minnesota, USA

Re: How to organize code? ("good code")

Post by Ben Boldt »

puppydrum64 wrote: Wed May 19, 2021 9:00 pm I think the better reason for using INX to wrap around to 0 is to waste time to wait for the PPU to warm up, since INX takes 5 CPU cycles compared to just 2 for LDX
Oziphantom wrote: Wed May 19, 2021 10:39 pm inx takes 2 clocks, it saves a byte over LDX #00
Where do you think INX is 5 cycles? I always use this instruction set, it says 2 cycles for both ways:

https://www.masswerk.at/6502/6502_instruction_set.html

I think the only difference is that INX saves 1 byte ROM.
lidnariq
Posts: 11429
Joined: Sun Apr 13, 2008 11:12 am

Re: How to organize code? ("good code")

Post by lidnariq »

Probably confused with INC zp.
puppydrum64
Posts: 160
Joined: Sat Apr 24, 2021 7:25 am

Re: How to organize code? ("good code")

Post by puppydrum64 »

Yeah that's what I meant
User avatar
Controllerhead
Posts: 314
Joined: Tue Nov 13, 2018 4:58 am
Location: $4016
Contact:

Re: How to organize code? ("good code")

Post by Controllerhead »

puppydrum64 wrote: Wed May 19, 2021 9:00 pm I think the better reason for using INX to wrap around to 0 is to waste time to wait for the PPU to warm up, since INX takes 5 CPU cycles compared to just 2 for LDX
I honestly don't even bother with zeroing memory before PPU warmup. I wait for the PPU and zero out both the CPU and PPU memory in one swoop. Even with this delay, my first screen is drawn and main loop is going in 6 frames, which is a tenth of a second mind you. Nerdy Nights doesn't zero PPU memory, but it's (usually) a good idea to do so. My init code looks something like this:

Code: Select all

; Set stack pointer and turn off interrupts etc

LDA #$FF
BIT PPU_STATUS	; Initial NMI read. State unknown on RESET.
- ; Loop
	BIT PPU_STATUS
BPL - 

- ; Loop
	BIT PPU_STATUS
BPL -
	
; Load high bytes
LDX #$24
STX PPU_ADDR
LDX #$07
STX $01

; Load low bytes
LDY #$00
STY PPU_ADDR
STY $00
TYA

- ; Loop
	-- ; Loop
		STA ($00), y	; $00-$FF	
		STA PPU_DATA
		INY; $00-$FF / $00
	BNE -
	DEC $01; $07-$00 / $FF
BPL --
	
; Store $FF in sprite page
LDA #$FF
- ; Loop
	STA $200, y
	INY
	STA $200, y
	INY
	INY
	STA $200, y
	INY
BNE -
Image
Post Reply