I need more info about the 6502 CPU instruction set.
Moderator: Moderators
I need more info about the 6502 CPU instruction set.
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.
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.
Re: I need more info about the 6502 CPU instruction set.
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.
Warning about the 6502.txt: it has useful descriptions but has some bugs.
Re: I need more info about the 6502 CPU instruction set.
Thanks! Those are useful.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.
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?
Re: I need more info about the 6502 CPU instruction set.
For that example it means: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?
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).
Re: I need more info about the 6502 CPU instruction set.
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
Find them (public library, college library, online, local bookstores, etc.) and use them: https://wiki.nesdev.com/w/index.php/Pro ... _materials
Re: I need more info about the 6502 CPU instruction set.
Thanks!NickMass wrote:For that example it means: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?
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).
Re: I need more info about the 6502 CPU instruction set.
Thanks. I will take a look, but I think I have enough for now.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
Re: I need more info about the 6502 CPU instruction set.
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!
If I e.g. do ADC, does it handle the data as signed or unsigned?
Thanks!
Re: I need more info about the 6502 CPU instruction set.
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).
The results of the arithmetic must be same regardless of whether you operate as (signed char + signed char) or (unsigned char + unsigned char).
Re: I need more info about the 6502 CPU instruction set.
So in binary, it does the exact same thing?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).
Re: I need more info about the 6502 CPU instruction set.
It uses 2's complement, so yes, it's the same thing...timl132 wrote:So in binary, it does the exact same thing?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).
Re: I need more info about the 6502 CPU instruction set.
Okay, thanks.NOOPr wrote:It uses 2's complement, so yes, it's the same thing...timl132 wrote:So in binary, it does the exact same thing?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).
Re: I need more info about the 6502 CPU instruction set.
I tried it out, and I'm confused,
I wrote some c++ code:
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?
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;
}
So they aren't treated the same or am I missing something?
Re: I need more info about the 6502 CPU instruction set.
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.timl132 wrote:Thanks. I will take a look, but I think I have enough for now.
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.
Re: I need more info about the 6502 CPU instruction set.
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.