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.
Emus don't matter. It just subtracts one. It'll roll from 00000000 to 11111111.
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?
Sure, or you can just use an unsigned char and then it'd be the right size you need.
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.
So then I'm assuming I could just leave it as a byte since by default in VB.NET bytes are unsigned.
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?
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.
Thank you so much for this tokumaru. It is much clearer now.
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).