NES Emulator
Introduction
The NES Emulator (has no name) is Java-based Nintendo Entertainment System (NES) emulator. It supports the UNROM, CNROM, MMC1 and MMC3 mappers as well as Game Genie codes. It also has gamepad support. Currently it doesn't play sound and has tons of glitches.
Development
The NES emulator uses the same core as my C64 Emulator? as they use essentially the same CPU (both are based on the MOS 6502). In the case of the NES it is a Ricoh 2A03 which adds sound support and lacks Binary Coded Decimal (BCD) arithmetic, though the flag can still be used.
Architecture

Overall Operation
(refer to architecture diagram)
CPU
Obviously the core of the whole thing is the CPU, in this case the MOS6502Emulator? which uses the NESInstructionSet?. So that the CPU has knows what instructions to execute, it needs Program ROM. Because of the quirks of how the NES operates, the CPU doesn't actually see it as ROM? but rather as a MemoryHandler? which is capable of receiving writes. Those writes do not actually go the program data, but rather are handled by the Mapper? which sits on top of it. The mapper enables more than 32K of Program ROM to be used and the writes are what decide what "bank" of code to use (among other things, depending on the complexity of the mapper).
The implementation of the CPU itself is essentially "textbook" in it's nature:
- Fetch - Reads a byte from the address corresponding to the current Program Counter
- Decode - Determine what Instruction corresponds to the opcode by retrieving it from the InstructionSet?. Then read the operands depending on the size of the instruction (which may use 0, 1, or 2 operands)
- Execute - Run the instruction (by calling it's execute() method) with the operands. The instruction may read and write from memory and read and set registers.
PPU
The Picture Processing Unit (PPU?) is the graphics card for the NES. Technically it is the 2C02. It is always running in the real NES, but that would be quite hard to do in our case so instead it registers itself as a CycleObserver? with the CPU which enables it to "know" when it should redraw the screen and send interrupts to the CPU (such as for vertical refresh). For cartridges that don't have their own Character ROM, the PPU has 8K of VRAM? which can be written from the running program. To enable this, the PPU is passed in (as a MemoryHandler?) to the NESMemory. This is also used to enable Sprite DMA writes (used to write sprite data) to the PPU's internal sprite memory. For the cartridges that have their own Character ROM, the mapper is also responsible for providing a view of this. As far as the PPU is concerned, it has 8K of character data, but with the mapper it can be quite a bit more. Note that currently the Mappers are used even when the cartridge itself doesn't have a mapper.
NESController
The last (most important?) part of the NES is the controller which is the player's method of using the system. The default NESController? implementation just uses the keyboard but there is also a GamepadController? implementation that uses the JInput libraries, provided through the Lightweight Java Game Library (LWJGL). These hook into Windows' DirectX gamepad controllers.
The controller is quite simple in it's operation. When the program writes to the CPU memory address of 0x4016 (which is the memory-mapped address for the controller), this is passed to the controller. First a 1 is written and then 0, which resets it. After that subsequent reads will step through each button in sequence. For example if the 'A' button is pressed, then the second read will return a 1 corresponding to this press.
Reference Documents/Sites
It's amazing the work that has gone into documenting the ins and outs of the NES so that people like me can write emulators for it. Documents
- NESDoc.pdf - Yoshi's consolidation of a huge amount of documentation that's out there. Has a good bibliography if you need more details
- 2C02 Technical Reference.txt - Brad Taylor's incredibly detailed analysis of the Nintendo PPU (2C02)
- 2A03 Technical Reference - Also by Brad Taylor. In depth analysis of the CPU
Sites
http://nesdev.icequake.net/
http://www.zophar.net/tech/nes.html
http://nesdev.parodius.net - Practically the default location for NES documents.
