Score Keeping on the NES

Sometimes I feel like I should put a content warning here when the technical level of a post is higher than usual. This one would probably be a five out of five for geekery. It’s a video from NESHacker on counting score on the Nintendo Entertainment System. But I don’t want to discourage you from watching it! It’s nine minutes long, and it contains a definition of the term double dabble.

Human-readable numbers are tracked by computers in a number of different ways. Nowadays we basically just do a printf or some version of it, but on a 1 megahertz platform, optimization really matters. It’s easy to think of computers as being impossibly fast, but in truth speed only ever counts relative to the efficiency of the algorithm you use. Computers are fast, but they aren’t all that fast.

One of the big tradeoffs in processor design is, fewer complex instructions that do a lot but take a lot of cycles, and processor complexity, to execute, or many simple instructions, each doing little and being relatively simple, and not needing a complex processor design to implement.

The 6502 microprocessor generally follows the latter design philosophy. It made some important tradeoffs to keep costs down. For example, it doesn’t have hardware that can multiply arbitrary numbers together. It relies on the programmer, or else a library author, to use the instructions given to code their own multiplication algorithm, if they need one. The result is going to be slower, probably, that if the chip had the circuits to do this automatically in silicon, but it reduced the cost of the chip, basically allowing more to be made, or else increasing the profits for the manufacturer.

Personally I’m a fan of just storing the score as a series of digits that match up to their positions in the character set. Gain 1,000 points? Just bump the 1000s-place up by one, and if it goes past 9, subtract 10 and bump the 10,000s place. That’s a tried-and-true system that many games use, and works well if all you ever have to do is add numbers. Comparing values, like for detecting extra life award levels, make things slightly more complex, but not by much. There’s sometimes other factors involved though, and that may explain why Super Mario Bros. uses different systems for its counters, as explained by NESHacker.

NESHacker’s Guide to the NES Hardware

More and more I find I should do a blog search to make sure that I haven’t posted something before, and my search for this video didn’t find it. It did find our link to the Copetti Site’s discussion of various console architectures, and a separate link specifically to their explication of the SNES’ construction, but not this particular video from NESHacker, so it’s fair game. Post! (zoop)

It’s only about nine minutes long so you can guess that it doesn’t go into deep detail. Essentially the NES is split into two parts, the CPU and its memory, and the PPU graphics chip and its own memory. A lot of classic consoles and microcomputers had to take special measures to support their display, which often ended up being the most complex part of the unit. Think about it: you have what amounts to a deluxe broadcast character generator right there in a box on your desk, shelf or floor, with lots of extra bells and whistles besides. (In fact, home computers were often used to generate current events channels for local cable companies, and an Amiga was essentially the basis for the old Prevue Guide channel.) It’s like a tiny special-purpose, single-receiver TV station just for your own use.

Graphics hardware is extremely timing sensitive. It has to generate the signal for your TV to display according to standardized picture generation requirements, so special requirements are often necessary. In the Commodore 64, for instance, the VIC-II graphics chip has the power to actually put the 6510 CPU to sleep, so it can have unrestricted access to the computer’s memory, without fear of bus conflicts, when it’s needed. This reduces the overall speed of the processor by a bit, and it’s why C64s turn off the screen when loading programs from cassette tape, in order to keep the CPU timing consistent relative to the data being streamed in off the tape.

The NES gets around this by giving the PPU RAM and address bus for its own exclusive use, and to put stuff in it the CPU has to use the PPU as an intermediary. And what’s more the NES exposes both the CPU and PPU’s address busses through the cartridge connector (which is why it’s got so many pins), allowing carts to supply dedicated ROM and RAM to both chips.

Even though it’s just a high-level overview, I found it a worthwhile use of those nine minutes, and you may very well enjoy it too.

NES Hardware Explained (from NESHacker, on Youtube, 9 minutes)