I need more info about the 6502 CPU instruction set.

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

timl132
Posts: 35
Joined: Tue Jul 23, 2019 10:41 am

I need more info about the 6502 CPU instruction set.

Post by timl132 » Tue Jul 23, 2019 10:53 am

Hello,

I am in the process of writing a NES emulator, it has been going well so far, my INES loader works and I wrote a little disasembler that displays the assembly names of an opcode.(Also created ram and mapped rom to it) But this doesn't do anything of course. My next step is to start implementing instructions, however, I am having trouble finding clear descriptions of instructions. I found these 3 sites:
http://www.6502.org/tutorials/6502opcodes.html
https://www.masswerk.at/6502/6502_instruction_set.html
These sites are nice, but they don't provide enough information about what the instructions do. There are the obvious ones like JMP and NOP, but then instructions like PHP or RTI I don't exactly know what they do. Of course I have a vague idea, e.g. RTI probably goes back to where it was before the interrupt happens. But I don't exactly know where it needs to return to, where I store where it needs to go and such. This is probably partly because I am not super familiar with assembly.

So I am hoping someone has like a sheet that tells me exactly what each instruction does, if it includes what micro-instructions it does it would be even better. Does anyone have/know something like that?

Also, bit off topic, a description of the cpu flags would be nice, because I don't know some of them.

Thanks!

Edit:
I need descriptions of the opcodes seperately, not the assembly opcodes. Because something like JMP can evaluate to different opcodes.

User avatar
NOOPr
Posts: 68
Joined: Tue Feb 27, 2018 10:41 am
Location: Brazil
Contact:

Re: I need more info about the 6502 CPU instruction set.

Post by NOOPr » Tue Jul 23, 2019 11:10 am

Try http://www.obelisk.me.uk/6502/reference.html and http://nesdev.com/6502.txt .
Warning about the 6502.txt: it has useful descriptions but has some bugs.

timl132
Posts: 35
Joined: Tue Jul 23, 2019 10:41 am

Re: I need more info about the 6502 CPU instruction set.

Post by timl132 » Tue Jul 23, 2019 11:13 am

NOOPr wrote:Try http://www.obelisk.me.uk/6502/reference.html and http://nesdev.com/6502.txt .
Warning about the 6502.txt: it has useful descriptions but has some bugs.
Thanks! Those are useful.
I took a look at the obelisk site and it has stuff like this in it: A,Z,C,N = A+M+C
What does it mean?

NickMass
Posts: 15
Joined: Tue Mar 30, 2010 7:59 pm

Re: I need more info about the 6502 CPU instruction set.

Post by NickMass » Tue Jul 23, 2019 11:28 am

timl132 wrote: I took a look at the obelisk site and it has stuff like this in it: A,Z,C,N = A+M+C
What does it mean?
For that example it means:

Register A
Flag Z (Zero)
Flag C (Carry)
Flag N (Negative)

Get their updated values based on the result of adding these together:

Register A
Memory value of the instruction
Flag C (Carry - 1 or 0).

User avatar
koitsu
Posts: 4218
Joined: Sun Sep 19, 2004 9:28 pm
Location: A world gone mad

Re: I need more info about the 6502 CPU instruction set.

Post by koitsu » Tue Jul 23, 2019 11:57 am

There are books and other on this subject (re: understanding the CPU in detail, including each opcode, as well as flags and how they are affected by each opcode) that are documented on the wiki.
Find them (public library, college library, online, local bookstores, etc.) and use them: https://wiki.nesdev.com/w/index.php/Pro ... _materials

timl132
Posts: 35
Joined: Tue Jul 23, 2019 10:41 am

Re: I need more info about the 6502 CPU instruction set.

Post by timl132 » Tue Jul 23, 2019 12:02 pm

NickMass wrote:
timl132 wrote: I took a look at the obelisk site and it has stuff like this in it: A,Z,C,N = A+M+C
What does it mean?
For that example it means:

Register A
Flag Z (Zero)
Flag C (Carry)
Flag N (Negative)

Get their updated values based on the result of adding these together:

Register A
Memory value of the instruction
Flag C (Carry - 1 or 0).
Thanks!

timl132
Posts: 35
Joined: Tue Jul 23, 2019 10:41 am

Re: I need more info about the 6502 CPU instruction set.

Post by timl132 » Tue Jul 23, 2019 12:04 pm

koitsu wrote:There are books and other on this subject (re: understanding the CPU in detail, including each opcode, as well as flags and how they are affected by each opcode) that are documented on the wiki.
Find them (public library, college library, online, local bookstores, etc.) and use them: https://wiki.nesdev.com/w/index.php/Pro ... _materials
Thanks. I will take a look, but I think I have enough for now.

timl132
Posts: 35
Joined: Tue Jul 23, 2019 10:41 am

Re: I need more info about the 6502 CPU instruction set.

Post by timl132 » Tue Jul 23, 2019 12:17 pm

I actually have one more question, specific to arithmetic operations.
If I e.g. do ADC, does it handle the data as signed or unsigned?

Thanks!

lidnariq
Posts: 9510
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: I need more info about the 6502 CPU instruction set.

Post by lidnariq » Tue Jul 23, 2019 12:25 pm

Both. The only difference is context.

The results of the arithmetic must be same regardless of whether you operate as (signed char + signed char) or (unsigned char + unsigned char).

timl132
Posts: 35
Joined: Tue Jul 23, 2019 10:41 am

Re: I need more info about the 6502 CPU instruction set.

Post by timl132 » Tue Jul 23, 2019 12:40 pm

lidnariq wrote:Both. The only difference is context.

The results of the arithmetic must be same regardless of whether you operate as (signed char + signed char) or (unsigned char + unsigned char).
So in binary, it does the exact same thing?

User avatar
NOOPr
Posts: 68
Joined: Tue Feb 27, 2018 10:41 am
Location: Brazil
Contact:

Re: I need more info about the 6502 CPU instruction set.

Post by NOOPr » Tue Jul 23, 2019 1:34 pm

timl132 wrote:
lidnariq wrote:Both. The only difference is context.

The results of the arithmetic must be same regardless of whether you operate as (signed char + signed char) or (unsigned char + unsigned char).
So in binary, it does the exact same thing?
It uses 2's complement, so yes, it's the same thing...

timl132
Posts: 35
Joined: Tue Jul 23, 2019 10:41 am

Re: I need more info about the 6502 CPU instruction set.

Post by timl132 » Tue Jul 23, 2019 1:40 pm

NOOPr wrote:
timl132 wrote:
lidnariq wrote:Both. The only difference is context.

The results of the arithmetic must be same regardless of whether you operate as (signed char + signed char) or (unsigned char + unsigned char).
So in binary, it does the exact same thing?
It uses 2's complement, so yes, it's the same thing...
Okay, thanks.

timl132
Posts: 35
Joined: Tue Jul 23, 2019 10:41 am

Re: I need more info about the 6502 CPU instruction set.

Post by timl132 » Tue Jul 23, 2019 1:52 pm

I tried it out, and I'm confused,

I wrote some c++ code:

Code: Select all

uint8_t a = 100;
        uint8_t b = -1;
        if (a > 255-b)
        {
            std::cout << "overflow" << std::endl;
        }
It checks if a+b would overflow. a and b as uint8_t returns "overflow" in the terminal. But if I make a and b an int8_t, it doesn't write "overflow".
So they aren't treated the same or am I missing something?

User avatar
koitsu
Posts: 4218
Joined: Sun Sep 19, 2004 9:28 pm
Location: A world gone mad

Re: I need more info about the 6502 CPU instruction set.

Post by koitsu » Tue Jul 23, 2019 1:53 pm

timl132 wrote:Thanks. I will take a look, but I think I have enough for now.
Strongly urge you to read "Programming the 65816 (including the 6502, 65C02, and 65802)". It is 65816-focused (e.g. 16-bit CPU used in SNES and Apple IIGS), but it goes over 8-bit 6502 (NES etc.) and 65C02 (Apple IIE etc.) as well. Your questions like about ADC/SBC and signed numbers are covered in very great detail (several pages, with examples), as well as general nuances. There is a reason a CPU manufacturer boughts right to this book. You just have to learn how to "ignore" the 65816/16-bit-specific stuff. It's still a good manual for 6502 despite that.

Also, with regards to ADC/SBC, because you're writing an emulator: these two opcodes are still the top struggle point for emulator authors. Please see here for almost two decades of details. Edit: you're already posting about emulation problems with these opcodes and how overflow fits into the picture. Please see what I linked here. Take the 30+ minutes to read the threads/links, you will find code as well.
Last edited by koitsu on Tue Jul 23, 2019 1:54 pm, edited 1 time in total.

lidnariq
Posts: 9510
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: I need more info about the 6502 CPU instruction set.

Post by lidnariq » Tue Jul 23, 2019 1:54 pm

C/C++'s type promotions don't allow arithmetic on 8-bit types (everything must be promoted to signed ints first, before then being cast to the type it's stored in). You'll have to think through how 2's complement works.

Post Reply