Question about DEX/DEY/INY/INX opcodes

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

Post Reply
urbanspr1nter
Posts: 39
Joined: Thu Aug 16, 2012 7:55 pm

Question about DEX/DEY/INY/INX opcodes

Post by urbanspr1nter » Thu Aug 16, 2012 8:00 pm

Hi all, I'm currently attempting to write an NES emulator through .NET and I have a question about the particular opcodes that do decrementing and incrementing...

Since X, and Y registers are 8 bits, is it an unsigned or signed byte? That is, is the value range of the X and Y registers from -128 to 127 or 0-255?

I am confused by this because if the X and Y registers are initialized as 0, what happens when a DEX is performed? Or is it up to the programmer to actually worry about that?

Thanks in advance for the help everyone.
Last edited by urbanspr1nter on Thu Aug 16, 2012 8:04 pm, edited 1 time in total.

3gengames
Formerly 65024U
Posts: 2281
Joined: Sat Mar 27, 2010 12:57 pm

Re: Question about DEX/DEY/INY/INX opcodes

Post by 3gengames » Thu Aug 16, 2012 8:03 pm

Emus don't matter. It just subtracts one. It'll roll from 00000000 to 11111111.

urbanspr1nter
Posts: 39
Joined: Thu Aug 16, 2012 7:55 pm

Re: Question about DEX/DEY/INY/INX opcodes

Post by urbanspr1nter » Thu Aug 16, 2012 8:08 pm

So In terms of implementation from what you're saying is it is fine to make it an unsigned byte? 0-255?

So if i were to do a DEX when X = 0, I can basically check first to see if X is already 0, then just reset it to 255?

3gengames
Formerly 65024U
Posts: 2281
Joined: Sat Mar 27, 2010 12:57 pm

Re: Question about DEX/DEY/INY/INX opcodes

Post by 3gengames » Thu Aug 16, 2012 8:12 pm

Sure, or you can just use an unsigned char and then it'd be the right size you need.

urbanspr1nter
Posts: 39
Joined: Thu Aug 16, 2012 7:55 pm

Re: Question about DEX/DEY/INY/INX opcodes

Post by urbanspr1nter » Thu Aug 16, 2012 8:15 pm

Thanks!

User avatar
Dwedit
Posts: 4408
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: Question about DEX/DEY/INY/INX opcodes

Post by Dwedit » Thu Aug 16, 2012 8:22 pm

Not an "unsigned char" in .NET, char is a 16-bit unicode character. "unsigned char" applies to regular C or C++, where bytes are called "char"s.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!

urbanspr1nter
Posts: 39
Joined: Thu Aug 16, 2012 7:55 pm

Re: Question about DEX/DEY/INY/INX opcodes

Post by urbanspr1nter » Thu Aug 16, 2012 8:24 pm

So then I'm assuming I could just leave it as a byte since by default in VB.NET bytes are unsigned.

urbanspr1nter
Posts: 39
Joined: Thu Aug 16, 2012 7:55 pm

Re: Question about DEX/DEY/INY/INX opcodes

Post by urbanspr1nter » Thu Aug 16, 2012 8:27 pm

Also, could someone also verify my logic of thinking?

I was thinking about what 3gen said about value ranges not mattering in terms of implementation and I realized this is due to the fact that the we can read numbers as negative if the SIGN bit of the status register gets set?

User avatar
tokumaru
Posts: 11991
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Question about DEX/DEY/INY/INX opcodes

Post by tokumaru » Fri Aug 17, 2012 6:36 am

When represented in binary, signed and unsigned numbers are the same: 11111111 is both 255 and -1, it depends on what kind of math the program does with them and how the results are interpreted. The N flag (is this what you call the SIGN bit?) is just a copy of bit 7 of the last manipulated value, and can be used to tell whether it's negative because all negative numbers have this bit set. If the program isn't treating the number as signed, it will just ignore this flag (or use it for purposes other then identifying the sign). The overflow flag is also meant for signed numbers, while the carry can be used to test overflows and underflows of unsigned numbers.

urbanspr1nter
Posts: 39
Joined: Thu Aug 16, 2012 7:55 pm

Re: Question about DEX/DEY/INY/INX opcodes

Post by urbanspr1nter » Sat Aug 18, 2012 9:59 am

Thank you so much for this tokumaru. It is much clearer now.

WedNESday
Posts: 1236
Joined: Thu Sep 15, 2005 9:23 am
Location: Berlin, Germany
Contact:

Re: Question about DEX/DEY/INY/INX opcodes

Post by WedNESday » Mon Aug 27, 2012 8:17 am

Just think of all of the Registers (A, P, S, X, Y) as 8-bit unsigned char. For instance X will never be a minus number and when you emulate it's usage you just add it nothing else (so long as its defined as being unsigned).

Post Reply