WDC 65C816

The W65C816S (also 65C816 or 65816) is a 16-bit microprocessor (MPU) developed and sold by the Western Design Center (WDC). Introduced in 1983, the W65C816S is an enhanced version of the WDC 65C02 8-bit MPU, itself a CMOS enhancement of the venerable MOS Technology 6502 NMOS MPU. The 65C816 is the CPU for the Apple IIGS and, in modified form, the Super Nintendo Entertainment System.

The 65 in the part's designation comes from its 65C02 compatibility mode, and the 816 signifies that the MPU has selectable 8- and 16-bit register sizes. In addition to the availability of 16-bit registers, the W65C816S extends memory addressing to 24 bits, supporting up to 16 megabytes of random-access memory. It has an enhanced instruction set and a 16-bit stack pointer, as well as several new electrical signals for improved system hardware management.

At reset, the W65C816S starts in "emulation mode", meaning it substantially behaves as a 65C02. Thereafter, the W65C816S may be switched to "native mode" with a two instruction sequence, causing it to enable all enhanced features, yet still maintain a substantial degree of backward compatibility with most 65C02 software. However, unlike the PDIP40 version of the 65C02, which is a pin-compatible replacement for its NMOS ancestor, the PDIP40 W65C816S is not pin-compatible with any other 6502 family MPU.

The W65C802 or 65802 is completely software-compatible with the 65C816, but is electrically-compatible with the 6502 and 65C02. Hence the 65C802 could be used as a drop-in replacement in most systems equipped with a 6502 or 65C02. However, the 65C802 cannot emit a 24-bit address, which limits it to a 64 KB address space. The 65C802 is no longer produced.

History
In 1981, Bill Mensch, founder and CEO of WDC, began development of the 65C02 with his production partners, primarily Rockwell Semiconductor and Synertek. The primary goal of the 65C02 effort was to move from the original 6502's NMOS process to the CMOS process, which would allow it to run at much lower power levels, somewhere between $1/undefined$ and $1/undefined$ at any given clock speed. Also desired was the ability to raise the maximum supported clock speed. The 65C02 design addressed chip errata present in the NMOS 6502 (e.g., the infamous  bug) and introduced new instructions and new addressing modes for some existing instructions.

Development of the W65C816S commenced in 1982 after Mensch consulted with Apple Computer on a new version of the Apple II series of personal computers that would, among other things, have improved graphics and sound. Apple wanted an MPU that would be software compatible with the 6502 then in use in the Apple II but with the ability to address more memory, and to load and store 16 bit words. The result was the 65C816, finished in March 1984, with samples provided to both Apple and Atari in the second half of the year and full release in 1985. Mensch was aided during the design process by his sister Kathryn, who was responsible for part of the device's layout.

The same process also led to the 65C802, which was identical inside to the 65C816. Both were produced on the same fabrication lines and diverged only during the last metalization stages when the chip was being connected to the external pins. In the 65C802, those pins had the same layout as the original 6502, which allowed it to be used as a drop-in replacement while still allowing the 16-bit processing of the CPU to be used. However, as it used the original pinout it had only 16 addressing pins, and could therefore only access 64 KB of external memory. Typically, when hardware manufacturers designed a project from the ground up, they used the 65C816 rather than the 65C802, resulting in the latter being withdrawn from production.

Apple subsequently integrated the 65C816 into the Apple IIGS computer. The basic 65C816 design was second-sourced by VLSI Technology, GTE, Sanyo and others from the mid-to-late 1980s to the early 1990s.

In the 1990s, both the 65C816 and 65C02 were converted to a fully static core, which made it possible to completely stop the processor's Ø2 clock without loss of register contents. This feature, along with the use of asynchronous static RAM, made it possible to produce designs that used minimal power when in a standby state.

, the W65C816S is available from WDC in 40 pin PDIP, PLCC44, or 44-pin TQFP packaging, as an MCU through the W65C265, and as IP cores for ASIC integration (for example Winbond's W55V9x series of TV Edutainment ICs ).



Features
WDC 65c816 features:
 * Fully static CMOS design offers low power consumption (300 µA at 1 MHz) and increased noise immunity.
 * Wide operating voltage range: 1.8 V to 5.0 V ± 5%.
 * Wide operating frequency range, officially 14 MHz maximum at 5 volts (20Mhz in SuperCPU), using a single-phase clock source.
 * Emulation mode allows substantial software compatibility with the NMOS 6502 and CMOS 65C02, excepting undocumented opcodes. All 256 opcodes in the 65C816 are functional in both operating modes.
 * 24-bit memory addressing provides access to 16 MB of memory space.
 * 16-bit ALU, accumulator, stack pointer , and index registers ( and  ).
 * 16-bit direct page (aka zero page) register.
 * 8-bit data bank and program bank  registers, generating bits 16–23 of 24-bit code and data addresses. Separate program and data bank registers allow program segmentation and 16 MB linear data addressing.
 * Valid data address and valid program address  control outputs for memory qualification, dual cache and cycle steal DMA implementation.
 * Vector pull control output to indicate when an interrupt vector is being fetched.
 * Abort input and associated vector supports processor repairs of bus error conditions, such as page faults and memory access violations.
 * Direct page register and stack relative addressing provides capability for reentrant, recursive and re-locatable programming.
 * 24 addressing modes&mdash;13 original 6502 modes with 92 instructions using 256 op codes, including most new opcodes implemented in the 65C02.
 * Block-copy instructions ( and  ), allowing rapid copying of data structures from one area of RAM to another with minimal code.
 * Wait-for-Interrupt and Stop-the-Clock  instructions further reduce power consumption, decrease interrupt latency and allow synchronization with external events.
 * Co-Processor instruction with associated vector supports co-processor configurations, e.g., floating point processors.
 * Reserved "escape" instruction for future two-byte opcodes and a link to future designs (WDM are the initials of W65C816S designer William D. Mensch).

Two modes
The 65C816 has two operating modes: "emulation mode", in which the 16-bit operations are invisible—the index registers are forced to eight bits—and the chip appears to be very similar to the 6502, with the same cycle timings for the opcodes; and "native mode", which exposes all new features. The CPU automatically enters emulation mode when it is powered on or reset, which allows it to replace a 65(C)02, assuming one makes the required circuit changes to accommodate the different pin layout.

16-bit registers
The most obvious change to the 65C816 when running in native mode is the expansion of the various registers from 8-bit to 16-bit sizes. This enhancement affects the accumulator, the  and   index registers, and the stack pointer. It does not affect the program counter, which has always been 16-bit.

When running in native mode, two bits in the status register change their meaning. In the original 6502, bits 4 and 5 were not used, although bit 4 is referred to as the break flag. In native mode, bit 4 becomes the  flag and bit 5 becomes the   flag. These bits control whether or not the index registers and accumulator/memory  are 8-bit or 16-bit in size. Zeros in these bits set 16-bit sizes, ones set 8-bit sizes. These bits are locked at ones when the processor is powered on or reset, but become changeable when the processor is switched to native mode.

In native mode operation, the accumulator and index registers may be set to 16- or 8-bit sizes at the programmer’s discretion by using the  and   instructions to manipulate the   and   status register bits. This feature gives the programmer the ability to perform operations on either word- and byte-size data. As the accumulator and index register sizes are independently settable, it is possible, for example, to have the accumulator set to eight bits and the index registers set to 16 bits, giving the programmer the ability to manipulate individual bytes over a 64KB range without having to perform pointer arithmetic.

When register sizes are set to 16 bits, a memory access will fetch or store two contiguous bytes at the rate of one byte per clock cycle. Hence a read-modify-write instruction, such as, when used while the accumulator is set to 16 bits, will affect two contiguous bytes of memory, not one and will consume more clock cycles than when the accumulator is set to eight bits. Similarly, all arithmetic and logical operations will be 16-bit operations.

24-bit addressing
The other major change to the system while running in native mode is that the memory model is expanded to a 24-bit format from the original 16-bit format of the 6502. The 65c816 makes use of two 8-bit registers, the data bank register and the program bank register, to set bits 16-23 of the address, effectively generating 24-bit addresses. In both cases, 'bank' refers to a contiguous 64 KB segment of memory that is bounded by the address range, where   is the bank address, that is, bits 16-23 of the effective address. Both  and   are initialized to   at power-on or reset.

During an opcode or operand fetch cycle,  is prepended to the program counter  to form the 24-bit effective address. Should  "wrap" (return to zero),   will not be incremented. Hence a program is bounded by the limits of the bank in which it is executing. Implied by this memory model is that branch and subroutine targets must be in the same bank as the instruction making the branch or call, unless "long" jumps or subroutine calls are used to execute code in another bank. There is no programmatic means by which  can be directly changed.

During a data fetch or store cycle,  is prepended to a 16-bit data address to form the 24-bit effective address at which data will be accessed. This processor characteristic makes it possible to sanely execute 6502 or 65c02 code that uses 16-bit addresses to reference data elements. Unlike,   can be changed under program control, something that might be done to access data beyond the limits of 16-bit addressing. Also,  will temporarily increment if an address is indexed beyond the limits of the bank currently in. is ignored if a 24-bit address is specified as the operand to a data fetch/store instruction, or if the effective address is on direct (zero) page or the hardware stack. In the latter case, an implied bank  is used to generate the effective address.

A further addition to the register set is the 16-bit direct page register, which sets the base address for what was formerly called the zero page, but now referred to as direct page. Direct page addressing uses an 8-bit address, which results in faster access than when a 16- or 24-bit address is used. Also, some addressing modes that offer indirection are only possible on direct page. In the 65(c)02, the direct page is always the first 256 bytes of memory, hence “zero page”. In native mode, the 65c816 can relocate direct (zero) page anywhere in bank  (the first 64 KB of memory) by writing the 16-bit starting address into. There is a one-cycle access penalty if  is not set to an exact page boundary, that is, if the value in   is not , where   is the most-significant byte.

Switching between modes
The current mode of operation is stored in the emulation bit. Having already added the new  and   bits to the previous set of six flags in the status register, there were not enough bits left to hold the new mode bit. Instead, a unique solution was used in which the mode bit was left "invisible", unable to be directly accessed. The  (eXchange Carry with Emulation) instruction exchanges the value of the emulation bit with the carry  bit, bit 0 in. For instance, if one wants to enter native mode after the processor has started up, one would use  to clear the carry bit, and then   to write it to the emulation bit. Returning to 65c02 emulation mode uses  followed by.

Internally, the 65c816 is a fully 16-bit design. The  and   bits in   determine how the user registers (accumulator and index) appear to the rest of the system. Upon reset, the 65c816 starts in 6502 emulation mode, in which  and   are locked to. Hence the registers are locked to eight-bit size. The most significant byte (MSB) of the accumulator (the -accumulator) is not directly accessible but can be swapped with the least significant byte (LSB) of the accumulator (the  -accumulator) by using the   instruction. There is no corresponding operation for the index registers ( and  ), whose MSBs are locked at.

Upon being switched to native mode, the MSB of  and   will be zero, and the  -accumulator will be unchanged. If the  bit in   is cleared, the  -accumulator will be "ganged" to the  -accumulator to form a 16-bit register (called the  -accumulator). A load/store or arithmetic/logical operation involving the accumulator or memory will be a 16-bit operation—two bus cycles are required to fetch/store a 16-bit value.

If the  bit in   is cleared, both index registers will be set to 16 bits. If used to index an address, e.g.,, the 16-bit value in the index register will be added to the base address to form the effective address.

If the  bit in   is set, the accumulator will return to being an 8-bit register and subsequent operations on the accumulator, with a few exceptions, will be 8-bit operations. The -accumulator will retain the value it had when the accumulator was set to 16 bits. The exceptions are the instructions that transfer the direct page register and stack pointer  to/from the accumulator. These operations are always 16 bits wide in native mode, regardless of the condition of the  bit in.

If the  bit in   is set, not only will the index registers return to being 8 bits, whatever was in the MSB while they were 16 bits wide will be lost, something an assembly language programmer cannot afford to forget.

Applications
Systems based on 65c816 variants:


 * Acorn Communicator
 * Apple IIGS
 * C-One and SuperCPU enhancements for the Commodore 64
 * Super Nintendo Entertainment System: the console's Ricoh 5A22 CPU is based on the 65c816.
 * Additionally, 30+ Super NES games include the Nintendo SA1, a 65c816-based co-processor chip, in each cartridge.
 * Foenix Retro Systems C256 U/U+ model and F256K model