It is currently Wed Jul 18, 2018 11:42 am

 All times are UTC - 7 hours

 Page 1 of 1 [ 3 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Negative numbers in prg-rom?!Posted: Mon Aug 01, 2005 11:53 pm
Hey all,

I've just started to to write an NES emulator. The first thing I've done is make a simple hex viewer to see the code, but I find that there are negative codes! For example, I might a "0xffffff80" randomly and if I format my output to decimal instead of hex I find actual negative numbers (ie -15). Why oh why are there negative numbers?!

Thanks for the help

(Btw, I'm using C and working off of MarioBros.nes. I am simply opening the nes file, reading in a byte, outputting that byte to another file formattted as hexidecimal with a label and then repeating)

Top

 Post subject: Posted: Tue Aug 02, 2005 1:06 am

Joined: Mon Sep 27, 2004 8:33 am
Posts: 3715
Location: Central Texas, USA
A ROM simply contains a bunch of bits, usually organized in 8-bit units (bytes). The interpretation of the bits is up to the hardware connected to it. The NES CPU interprets bytes as opcodes, addresses , and values.

Unsigned (modulo) addition and subtraction can be used for signed addition and subtraction using two's complement representation. In this representation the high bit happens to be set for negative numbers and clear for positive numbers and zero. It can be understood by noting that adding 11 hours on an old-style clock face is the same as subtracting one hour.

Thus a byte in ROM can have a negative value if interpreted as a signed value. \$ff = -1. \$02 + \$ff = \$101. \$fe = -2. \$80 = -128. \$7f = 127.

The 6502 doesn't have any arithmetic registers larger than 8 bits. On machines that do, there is usually an instruction to convert a signed 8-bit value to a signed 16-bit value (and larger). This is called sign extension, and simply involves copying the sign bit to all higher bits in the destination. \$fe sign-extended to 16 bits is \$fffe.

You're probably loading the 8-bit value from ROM into a signed 8-bit value in your program, then converting it to a 32-bit value. If you want to treat it as unsigned, mask it with 0xff.

In short, a ROM is just a collection of bits and what they mean is up to the machine that interprets it. It can contain (representations of) colors, pictures, sounds, etc.

Top

 Post subject: Posted: Wed Aug 03, 2005 11:10 am

Joined: Fri Nov 19, 2004 7:35 pm
Posts: 4056
Use unsigned char instead of char.

_________________
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 3 posts ]

 All times are UTC - 7 hours

#### Who is online

Users browsing this forum: No registered users and 7 guests

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

Search for:
 Jump to:  Select a forum ------------------ NES / Famicom    NESdev    NESemdev    NES Graphics    NES Music    Homebrew Projects       2018 NESdev Competition       2017 NESdev Competition       2016 NESdev Competition       2014 NESdev Competition       2011 NESdev Competition    Newbie Help Center    NES Hardware and Flash Equipment       Reproduction    NESdev International       FCdev       NESdev China       NESdev Middle East Other    General Stuff    Membler Industries    Other Retro Dev       SNESdev       GBDev    Test Forum Site Issues    phpBB Issues    Web Issues    nesdevWiki