This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

Very simple 6502 asm question

Very simple 6502 asm question
by on (#25140)
lda #$01
sta $4016
lda #$00
sta $4016

Would the above code simply write $0001 to address $4016, and then instantly replace it with $0000? What purpose could that serve?

Or am I missing something?
Re: Very simple 6502 asm question
by on (#25142)
Bainemo wrote:
lda #$01
sta $4016
lda #$00
sta $4016

Would the above code simply write $0001 to address $4016, and then instantly replace it with $0000? What purpose could that serve?

The game controllers are connected to a serial port with 3 lines: start, next bit, and data. Bit 0 of $4016 sets the state of the start line; writing a 1 then a 0 prompts the controllers to start sending the first bit on the data line. Then each read of $4016 or $4017 returns the state of one button and asserts a signal on the next bit line, which prompts the controller to put the next bit on the data line.

by on (#25144)
I see. Well, that does make sense then. I was unaware the NES had special "watch out for these writes and then start doing things" instructions beyond the usually init stuff.

The code I got from above came from some asm code this guy wrote, and while much of it makes sense, it uses "double-writes" for a LOT of addresses. These include:

$2006 (stored from A)
$2006 (stored from X)
$2007

If you're interested, the code is http://www.21cmagazine.com/issue2/cory_clouds.html <-- There. I understand that this code is pretty simple, but a lot of it uses code that, on the surface, seems totally superfluous. Can you reccomend an article the introduces the basics of 6502 ASM for the NES?

Note: I can read the asm code for SMB 1 just fine (save for all the branches and jumps they do every other line); it's only this I'm having much trouble with.

EDIT: Phew - major edit issues

by on (#25147)
Bainemo wrote:
I see. Well, that does make sense then. I was unaware the NES had special "watch out for these writes and then start doing things" instructions beyond the usually init stuff.

Boy, you're going to freak when you start hitting PPU and mapper stuff.

Quote:
The code I got from above came from some asm code this guy wrote, and while much of it makes sense, it uses "double-writes" for a LOT of addresses. These include:

$2006 (stored from A)
$2006 (stored from X)
$2007

Addresses in $20xx are the PPU.

by on (#25150)
Yeah, the NES does everything like that, so you better get used to it. This is how the CPU, where the program is running, "talks" to everything else (controllers, sound hardware, video hardware).

The CPU (6502) is just a part of the NES. The program (game) running there must tell all the other parts what to do, and receive information from those other parts too, and this is all done by reading/writing from/to special memory locations.

As tepples said, you'll freak out when you hit the PPU and the mapper stuff, but specially the mappers, because those "special addresses" do different things in different games!

Anyway, there are many many documents indicating what each address does on reads and writes, just look at the main NESDEV page.

EDIT:

BTW, this:
Code:
$2006 (stored from A)
$2006 (stored from X)
$2007

Is how you write data (such as background graphics or color palettes) to the PPU. The PPU memory is on a different address bus than the CPU memory, so the process for sending stuff from the CPU memory to the PPU memory involves writing the address where you want to send the data to (the address is 16 bits long, hence the 2 writes to $2006) followed by the data itself (writes to $2007). The address is auto incremented after every write to $2007, as this was meant for sequential data. Every time you want to write something that's not sequential, you have to set the address (by writing it to $2006) again.

by on (#25159)
It's called "Memory Mapped IO". Those aren't RAM addresses, they are IO ports. Look at a NES memory map or something to see what address controls what.