It is currently Mon Aug 19, 2019 2:51 am

All times are UTC - 7 hours





Post new topic Reply to topic  [ 37 posts ]  Go to page 1, 2, 3  Next
Author Message
PostPosted: Tue Jul 23, 2019 10:53 am 
Offline

Joined: Tue Jul 23, 2019 10:41 am
Posts: 31
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.


Top
 Profile  
 
PostPosted: Tue Jul 23, 2019 11:10 am 
Offline
User avatar

Joined: Tue Feb 27, 2018 10:41 am
Posts: 53
Location: Brazil
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.

_________________
itch.io | github


Top
 Profile  
 
PostPosted: Tue Jul 23, 2019 11:13 am 
Offline

Joined: Tue Jul 23, 2019 10:41 am
Posts: 31
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?


Top
 Profile  
 
PostPosted: Tue Jul 23, 2019 11:28 am 
Offline

Joined: Tue Mar 30, 2010 7:59 pm
Posts: 15
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).


Top
 Profile  
 
PostPosted: Tue Jul 23, 2019 11:57 am 
Offline
User avatar

Joined: Sun Sep 19, 2004 9:28 pm
Posts: 4208
Location: A world gone mad
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


Top
 Profile  
 
PostPosted: Tue Jul 23, 2019 12:02 pm 
Offline

Joined: Tue Jul 23, 2019 10:41 am
Posts: 31
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!


Top
 Profile  
 
PostPosted: Tue Jul 23, 2019 12:04 pm 
Offline

Joined: Tue Jul 23, 2019 10:41 am
Posts: 31
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.


Top
 Profile  
 
PostPosted: Tue Jul 23, 2019 12:17 pm 
Offline

Joined: Tue Jul 23, 2019 10:41 am
Posts: 31
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!


Top
 Profile  
 
PostPosted: Tue Jul 23, 2019 12:25 pm 
Offline

Joined: Sun Apr 13, 2008 11:12 am
Posts: 8532
Location: Seattle
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).


Top
 Profile  
 
PostPosted: Tue Jul 23, 2019 12:40 pm 
Offline

Joined: Tue Jul 23, 2019 10:41 am
Posts: 31
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?


Top
 Profile  
 
PostPosted: Tue Jul 23, 2019 1:34 pm 
Offline
User avatar

Joined: Tue Feb 27, 2018 10:41 am
Posts: 53
Location: Brazil
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...

_________________
itch.io | github


Top
 Profile  
 
PostPosted: Tue Jul 23, 2019 1:40 pm 
Offline

Joined: Tue Jul 23, 2019 10:41 am
Posts: 31
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.


Top
 Profile  
 
PostPosted: Tue Jul 23, 2019 1:52 pm 
Offline

Joined: Tue Jul 23, 2019 10:41 am
Posts: 31
I tried it out, and I'm confused,

I wrote some c++ code:
Code:
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?


Top
 Profile  
 
PostPosted: Tue Jul 23, 2019 1:53 pm 
Offline
User avatar

Joined: Sun Sep 19, 2004 9:28 pm
Posts: 4208
Location: A world gone mad
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.

Top
 Profile  
 
PostPosted: Tue Jul 23, 2019 1:54 pm 
Offline

Joined: Sun Apr 13, 2008 11:12 am
Posts: 8532
Location: Seattle
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 37 posts ]  Go to page 1, 2, 3  Next

All times are UTC - 7 hours


Who is online

Users browsing this forum: Drilian and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group