Saturday, June 27, 2015

NES Hardware Explained

Hardware :

The basic Famicom/NES consists of a 40-pin Central Processing Unit (CPU), a 40-pin Picture Processing Unit (PPU), two 2KB Static RAMs, and six standard logic chips.  The front loader NES also contains a lockout chip and an extra standard logic chip, but they are not integral to the console's function except as a security measure.  Every NES has 2x7pin controller ports, a power button and a reset button and an RF modulator.  Front loaders also have composite video and (mono) audio outputs.  A very few rare Top Loaders have a Nintendo Multi-out port instead of an RF modulator.  The last top loaders with improved picture quality (RF or Nintendo Multi-out) use a newer motherboard that consolidates three of the standard logic chips into a Nintendo custom chip.

There were also rare devices that Nintendo released that used NES cartridges.  These include the Sharp Nintendo Television, which was a 13 or 19" TV on top of a front loading NES.  There also was the M82 Demonstration Unit that could hold 12 NES cartridges and allow for timed play in stores.

The NES power adapter is a step down converter that converts 120VAC or 240VAC into 9VAC and 1.3A.  AC to DC conversion is done inside the console.  A DC power adapter with a suitable plug can also be used so long as the step down specs are met.  Polarity of the AC adapter's connector is irrelevant.

The RF switch is an autoswitching type, meaning that there is no TV/Game switch on it as there were with older consoles.  When the NES turns on, it sends a signal of sufficient strength to automatically switch from the TV video signal to the NES video signal.  You can use a TV/Game switch if that is all you have available.

Specifications :

CPU - 2A03 running at 1.79MHz (NTSC) or 2C0 7  running at 1.66MHz (PAL).  The 2A03 contains a full 6502 CPU core with all unofficial opcodes but with a disabled decimal mode.

CPU Internal Memory - 19 Audio Processing Unit (APU) Registers, 2 I/O Registers, 1 Sprite DMA register

CPU External Memory - 2KB CPU RAM in the console + 32KB ROM and 8KB (optional) RAM in cartridge*

PPU - 2C02 running at 5.37MHz (NTSC) or 2C07 running at 5.32MHz (PAL).  The PPU can access 2 sets of 256 tiles for background and sprite graphics.  It can assign up to three distinct colors for a background or sprite tile.  Each set of 16x16 background tiles displayed on the screen must share the same palette settings.  The PPU is capable of scrolling horizontally and vertically by one pixel for smooth scrolling.

PPU Internal Memory - 8 Control Registers + 256 bytes + 32 bytes + 25 bytes

PPU External Memory - 2KB PPU RAM in the console + 8KB ROM and/or RAM in cartridge*

* - Limits without bankswitching hardware

PPU Resolution and Frame Rate : 256x240@60.0988fps (NTSC)/50fps (PAL), 224 lines typically viewable

Number of Colors in Palette : 54 distinct entries (out of 64) x 8 combinations of color emphasis bits = 432 colors

Number of Colors on-screen : 25 (1 Universal Background Color + 12 Background Palette Colors + 12 Sprite Colors)

Number of Sprites : 64 at 8x8 resolution or up to 32 at 8x16 resolution

Number of Sprites per Line : 8

Number of Audio Channels : 5 (2 x Pulse, Triangle, Noise, PCM)

APU Features : Variable duty-cycle rectangular waves (12.5%, 25/75%, 50%) for the two pulse channels, independent timers for all channels and length counters (automatic shut off) for first four channels, frame counter, volume envelope for pulse and noise channels, sweep unit for pulse channels, short and long noise mode (short mode only available on 2A03E or higher CPU revisions), 7-bit PCM samples or 1-bit Delta Modulation, internal mixer for the two pulse channels on one output pin and the triangle, noise and PCM on another output pin.  

Standard Controller : 4-way directional pad and 4 buttons, digital latched serial bitstream interface

Cartridge Features :

Number of Games with extra Work RAM : 89

Number of Games with battery backed Work RAM : 54 (out of 89)

Number of Games with extra CHR-RAM - 3

Smallest NES Cartridge Size : 24KB

Typical Cartridge Sizes : 24KB, 32KB, 40KB, 48K, 64KB, 96KB, 128KB, 160KB, 192KB, 256KB, 320KB, 384KB, 512KB

Median Cartridge Size : 256KB

Largest NES Cartridge Size : 768KB Licensed, 2MB Unlicensed

Most Common Memory Controller hardware : MMC1

Number of games using hardwired Horizontal Mirroring - 86

Number of games using hardwired Vertical Mirroring - 209

Number of games using hardwired One Screen Mirroring - 32

Licensed Game PCB Manufacturers : Nintendo, Konami (14 games), Acclaim (15 games), Virgin Games (1 games), Sunsoft (1 games).

Graphics Basics :

The NES uses a complex scheme to draw graphics.  The 2KB of internal PPU RAM holds two name tables and the attribute tables for the background graphics.  Each byte entry in a 32x30 name table points to an 8x8 graphics tile in CHR ROM or CHR RAM in the cartridge and tells the PPU to display that tile at a certain place on the screen.  Each byte entry in the attribute table selects the background palette entries that each set of 16x16 tiles will use.  There are four sets of palette entries consisting of three colors chosen from the 54 colors the NES usually displays and the universal background color.  This creates the background graphics.

The PPU RAM has space for two name tables and attribute tables.  The typical use for two sets of background graphics is to aid in scrolling.  One of the more impressive capabilities of the NES compared to earlier consoles is that it can scroll smoothly one column or line of pixels at a time, even though its graphics are tile based.  Many NES cartridges can be wired so that the graphics pages can be mirrored horizontally or mirrored vertically.  When the pages are mirrored vertically, scrolling the game horizontally becomes easier. Likewise, when the pages are mirrored horizontally, scrolling the game vertically becomes easier.  Games that scroll both horizontally and vertically like Metroid or scroll diagonally like Bionic Commando typically have hardware to allow for fast mirroring changes in software.  Two games add an extra 2KB of PPU RAM on the cartridge to allow for four pages of background graphics.

Sprites are controlled by a special 256 byte memory area internal to the PPU called Object Attribute Memory (OAM).  Each 8x8 sprite uses 4 bytes of the memory area, and the bytes select the tile to be displayed from the cartridge's CHR ROM or CHR RAM to represent the sprite, the X and Y coordinates on the screen where the sprite is to be displayed, which of the 4 palettes of 3 color the sprite will choose for color and whether the sprite is to be flipped horizontally or vertically.  Each sprite palette has a transparency color by which you can see the background graphics through clear pixels.  Typically, the CPU writes the values for all 64 sprites at a time.  There is also a special buffer that is not accessible by the CPU of 32 bytes which represent the data for the 8 sprites allowed on a line at a time.  When there are more than 8 sprites on a line, the PPU cannot display them all, resulting in flickering.

How the CPU Controls the System :

The NES is almost unique among consoles up to that point because it uses a dual bus design.  The CPU has its own address and data bus to its internal RAM and the cartridge PRG ROM and (if any) PRG RAM.  The PPU has a separate address and data bus to access its internal RAM and the cartridge CHR ROM or CHR RAM.  The CPU reads and writes data to memory locations in its memory map assigned to the APU and PPU.  These locations correspond to the APU and PPU registers and the input from the controllers.

Even though the PPU has its own address and data bus, it is told what to do by the CPU, it does not have its own specialized instruction set like a true coprocessor.  The CPU can read and write to memory anywhere in the PPU's memory addressing space.  When a cartridge uses CHR RAM instead of CHR ROM, it is up to the CPU to write the graphics data to fill the CHR RAM with graphics patterns.  This adds some flexibility at the cost of some speed and about 200 games use CHR RAM instead of CHR ROM (a couple use both).

Bankswitching Basics :

The NES's CPU can only handle 32KB of ROM at a time.  Similarly, the NES's PPU can only handle 8KB of ROM or RAM at a time.  Soon after the NES was introduced, ROM sizes on cartridges expanded beyond the combined 40KB that the NES was capable of accessing.  The NES had to use extra hardware inside the cartridge to switch in and out areas of ROM outside the 32KB and 8KB addressing limits.  With simple logic chips, it was possible to bankswitch additional PRG ROM, CHR ROM or both.  Typically, for PRG ROM, there would be a fixed 16KB bank and a switchable 16KB bank, with the game's kernel being held in the fixed bank.  Thus, for a game with a 128KB PRG ROM, there were 7 switchable banks.  This allowed for larger, more complex games with more music.  With CHR ROM, the simple schemes would switch the whole 8KB at a time.  A game with 32KB of CHR ROM could have 8 tilesets instead of the usual 2.  Games did not typically bankswitch CHR RAM or PRG RAM, but exceptions exist.

Adding extra logic chips made cartridges more difficult to manufacture and could have overwhelmed Nintendo's manufacturing capacities.  Nintendo created VLSI memory mapping controller chips to consolidate the functions of many simple logic chips into one chip.  The first of these was called the MMC1, and it allowed for bankswitching PRG ROM and CHR ROM, allowing for PRG ROM and PRG RAM to coexist and allowing the mirroring to be changed by the MMC1 at will.  By adding a battery to the PRG RAM, games could be saved after the console had been powered off, this is how The Legend of Zelda works.  Eventually more complex MMCs would be released, many of which only saw use in a handful of games.  MMC1 is the most common VLSI controller and can typically handle 256KB of PRG ROM and 128KB of CHR ROM and 8KB of PRG RAM.  There are NES MMC1 games that sacrifice CHR ROM bankswitching to bankswitch 512KB of PRG ROM or 16KB of PRG RAM.

The success of the NES increased the demand for more complex MMCs.  Nintendo released the MMC3 chip, which was found in games like Super Mario Bros. 3 and almost as many games as the MMC1.  The MMC3 could do all the MMC1 and more, it could bankswitch PRG ROM and CHR ROM in smaller memory slices, it could be accessed faster than MMC1, address more ROM and it had a scanline IRQ counter for easily splitting the screen between a main action area and a status bar.  It also provides for much better memory protection so you do not have to hold in reset as you turn the power off as you would on MMC1 games.  The maximum amount of memory the MMC3 can handle is 512KB of PRG ROM and 256KB of CHR ROM and 8KB of PRG RAM.

There is an extremely advanced MMC Nintendo released late in the NES's life called the MMC5.  In addition to even more flexible bankswitching and more sophisticated scanline IRQ counter compared to the MMC3, it can access 1MB of PRG ROM, 64KB of PRG RAM and 1MB of CHR ROM.  That was huge, the largest MMC5 NES cartridge only used 640KB.  It also added two pulse and a PCM sound channel, which was never used on the NES because there was no wiring to mix the cartridge audio with the internal audio.  It also could implement a vertical split screen scrolling mode, also probably never used in a NES cart.   It also had an 8-bit hardware multiplier. Finally, it had 1KB embedded into its chip that could be used for a third name table and attribute table, or be used to allow each background tile to independently select its palette entries.

When NES emulation became popular, various bankswitching methods was given "mapper" numbers.  As more and more games became emulated, the number kept expanding.  Here is the current mapper list and number of games using each mapper for all NTSC Licensed and Unlicensed games released during the NES's lifetime in the US :

Mapper 0 - 57

Mapper 1 MMC1 - 220

Mapper 2 - 90

Mapper 3 - 63

Mapper 4 MMC3 - 187

Mapper 4 Acclaim MMC3 - 12

Mapper 4 MMC6 - 2

Mapper 5 MMC5 - 8

Mapper 7 - 32

Mapper 9 MMC2 - 2

Mapper 13 - 1

Mapper 64 RAMBO-1 - 5

Mapper 68 Sunsoft-4/Tengen 337007 - 1

Mapper 69 Sunsoft FME-7 - 1

Mapper 206 Namco 109 - 4

Mapper 206 Namco 109/MIMIC-1 - 10

Mapper 11 - 30

Mapper 34 Nintendo - 1

Mapper 34 SEI/AVE - 1

Mapper 41 - 1

Mapper 37 MMC3 - 1

Mapper 47 MMC3 - 1

Mapper 71 - 15

Mapper 79 - 15

Mapper 118 MMC3 - 3

Mapper 119 MMC3 - 2

Mapper 144 - 1

Mapper 158 RAMBO-1 - 1

Mapper 168 - 1

Mapper 228 - 2

Mapper 232 - 5

Mapper 234 - 1

The numbers may change for Europe, but no new major memory controller hardware schemes were introduced there during the NES's lifetime.

CPU/PPU Revisions :

2A03E & 2C02E-0 : Used on early NES front loaders with PCBs labeled NES-CPU-04 and maybe NES-CPU-05.

2A03G & 2C02G-0 : Used on all other NES front loaders and many top loaders, Sharp Nintendo Televisions and NES Top Loaders with the original PCB design.  By far the most common combination in a NES.

2A03H & 2C02H-0 : Used on NES Top Loaders.

There are no known compatibility issues among these chips.  (The same cannot be said for Famicoms with CPUs and PPUs earlier than the 2A03E/2C02E-0 or the Famicom Titler or Sharp C1 TVs using the 2C03 or 2C05 PPUs.)

Console Generic Logic Chips :

74HC368 x 2*
2KBx8 SRAM 100ns x 2
74HCU04 (Front Loader only)
3913A CIC (Nintendo security chip, Front Loader only)

* - These three chips are consolidated into a Nintendo chip labeled BU3266S or BU3270S in the Top Loaders with the redesigned PCB.

Video and Audio Capabilities :

The audio output comes from two CPU pins.  On one pins, there are the two square wave channels, on the other channel are the triangle, noise and PCM channels.  These two signals are mixed together via resistors and then sent to the audio line output.  The NES can be modded for stereo by bypassing the external mixers, but the result will usually sound unbalanced since most of the music will come from one speaker and most of the sound effects from the other speaker.

The NES's PPU generates composite video internally and outputs it on a single pin.  This pin is amplified and sent to an RF modulator or RCA jacks for composite video output.  S-Video, Component Video and RGB Video are not possible with a 2C02 PPU without a substantial modification.

There are two available mods to improve the NES video.  First is the 2C03 PPU mod.  This mod requires taking a Nintendo 2C03 PPU chip from a Playchoice-10 arcade board and replacing the 2C02 from a NES with it.  This mod has been around for a very long time and for many years it was the only mod to obtain RGB from the NES.  You do not have to keep the 2C02, so it is a bit easier than the later mod, which requires you to desolder the PPU without destroying it or the PCB.  However, 2C03 chips are very, very expensive.  You will need to build an RGB amplifier for the three signals.  It also has a tendency to show jailbars in the video output.  Finally, it is incompatible with at least seven NES games and will show unintended graphical anomalies in many more.  Compare the NTSC palette with the RGB palette :

NTSC 2C02 PPU Palette

RGB 2C03 PPU Palette

The newer mod is the NESRGB board, and it is much cheaper and does not have any major compatibility issues.  It also allows for S-Video output and can also allow the original composite signal from the 2C02.  Component video can be added with a small board.

How the NESRGB Works :

The NES has a 16 color palette for all background tiles and another 16 color palette for all sprite tiles.  In simple terms, each of the 16 indexes in either palette corresponds to one of the 64 colors available on the NES.  Each active picture pixel the NES outputs will have its color determined by an entry in one of those two 16 color palettes.  There is also a universal background color.

There are four pins on a 2C02 PPU that are not used in a stock NES.  They are called EXT0-EXT3.  They can be set to input or output and are grounded on a NES or a Famicom, which is set to the input mode by default.  In output mode, these pins will output the palette entry for every pixel the NES displays.  The output is 4-bit and is digital, giving the nucleus of the necessary information to accurately capture the screen output digitally and losslessly.

This is where the NESRGB comes in.  It sits on the PPU bus and does several things.  First, it tells the PPU to put the EXT pins into output mode and prevents games from telling the PPU to do otherwise.  Second, it sits on the PPU's address and data bus to discover the values the CPU is writing into the palette entries.  This allows it to assign colors to the palette entries in its own output.  Third, it reads the EXT pins to determine the color of the pixels by the palette indexes being output by the pins.  However, because the EXT pins only output a 4-bit value, the NESRGB cannot tell whether the palette entry is from the background palettes or the sprite palettes.

In order to derive the 5th bit, which distinguishes the background from the sprite palettes, the NESRGB board will analyze the composite video output.  If this did not occur, the backgrounds or the sprites would have the wrong colors because only one palette would be used.

From the composite output signal, the blanking and sync information are also derived, just like a Framemeister.  NESRGB combines this with its digitized palette entries and then converts the signal into analog RGB and S-Video.  NESRGB offers three palette choices, composite, RGB and improved.  If no palette is selected, then the NESRGB operates merely as a passthrough for the original composite video signal.  If there is a palette selected, then the original composite video is not available.  This is because the NESRGB sends special palette color entries to the real PPU, all background palettes are black and all sprite palettes are white.  This allows it to easily compare background and sprite pixels by analyzing the PPU's video output signal.  Remember, the EXT pins are outputting palette indexes, not palette color data.

Lockout Chips :

The NES was officially released in many, many countries.  One of the lockout chip's functions was to keep people from importing cartridges from another region.  There were four lockout chips produced :

3193A/6113A/6113B1 - Used in US, Canada and Brazil
3195A - Used in PAL-B territories (France, Spain, Federal Republic of Germany, Norway, Sweden, Finland, Denmark, The Netherlands, Belgium, Luxembourg, Austria, Greece) and South Korea
3196A - Used in Hong Kong, Asian territories
3197A - Used in PAL-A territories (United Kingdom, Italy, Australia)
3198A - Used by the Famicom Box (a pay-to-play system Nintendo released for Japanese hotels).

The lockout chip can be disabled by removing pin 4 and connecting it to ground.

Sound Overview :

The NES uses the same basic approach to sound generation as other Programmable Sound Generator devices like the AY-3-891x/Yamaha YM-2149, the TI SN76496, the Atari POKEY and the Phillips SAA-1099.  At the heart of these chips is a square wave, an electrical signal that rises and falls at equal intervals (the period) a certain number of times per second.  These chips can control the frequency of the period, which is given in terms of hertz.  Hertz can be equated into notes on a musical scale, so a square wave running at a frequency of 256Hz will sound like a middle C.  These chips can also modify the volume of the waveform either directly and some can through an ASDR envelope.

The NES can output two square waves, but unlike any of the above sound chips, can change the period so that the time spent at the high and low ends is not equal.  Thus it becomes a rectangular wave.  Unlike the Commodore 64, the NES has no difficulty producing strong, distinct rectangular waves.  Both of these rectangular wave channels have the same features.  A rectangular wave has a high pitched but somewhat hollow sound and typically is used as the main musical instrument in the NES.  All these features make it far more suitable for sound effects than the earlier chips.

The third sound channel is a triangle wave, and instead of rising nearly-instantly to the high point, it gets there in steps.  The period cannot be changed.  The triangle wave is usually a softer sound and is often found acting as the bass line in NES music.

The fourth sound channel is the noise channel.  Noise is generated by outputting a long semi-random series of 0s and 1s at a particular frequency.  The resulting waveform looks like random spikes.  Noise is typically used for sound effects in the older chips, but the NES put it to good use for percussion when there was not sufficient space for samples.

The fifth and last sound channel is for playing samples.  It was seldom used in the early years of NES cartridges because sound samples take up a lot of space.  This channel plays sound in two different ways.  First, it can accept a 7-bit value sent directly to the channel.  The second is through delta modulation, whereby a single bit is sent to the channel output over a period of time.  A 1 bit indicates an increase in volume, and without further 1 bits, the signal will decay and fall silent.  DPCM samples take up less space but the quality is lower than the 7-bit PCM.

Unlike the Famicom, the NES does not send its internal audio to the cartridge connector for mixing with any cartridge audio hardware.  Instead, audio could be mixed via the expansion port, but no product was ever released that did that.  Therefore, expansion audio just does not exist on the NES unless Famicom cartridges are being used.  


Wizard Wizard said...

With the MMC1 chip, do you have 2 screens for sprites, and 2 screens for background? or do you only have two screen all together. One for sprites, one for background?

Great Hierophant said...

NES graphics don't work like that. The two screen mirroring mainly deals with background tiles, you don't waste sprites on the inactive screen. You get two tables worth of patterns with 8KB of CHR-ROM or CHR-RAM. The bankswitching allows you access to more, but the sprites and the background must takes their patterns from the banks currently within the 8KB.