MOS Technology 6502
![](http://upload.wikimedia.org/wikipedia/commons/thumb/4/49/MOS_6502AD_4585_top.jpg/220px-MOS_6502AD_4585_top.jpg)
The MOS Technology 6502 is an 8-bit microprocessor that was designed by Chuck Peddle and Bill Mensch for MOS Technology in 1975. When it was introduced, it was the least expensive full-featured microprocessor on the market by a considerable margin, costing less than one-sixth the price of competing designs from larger companies such as Motorola and Intel. It was nevertheless fully comparable with them, and, along with the Zilog Z80, sparked a series of computer projects that would eventually result in the home computer revolution of the 1980s. The 6502 design, with about 4,000 transistors, was originally second-sourced by Rockwell and Synertek and later licensed to a number of companies. It is still made for embedded systems.
History and use
Origins at Motorola
The 6502 was designed primarily by the same engineering team that had designed the Motorola 6800. One of the designers, Chuck Peddle, traveled around with the 6800 to introduce it to potential industrial customers. At the time it was targeted at the embedded market, and although their presentations sparked interest, the price, at about $300, was simply too high to be widely used. When he asked what sort of price would make the product useful, it settled on $25. Peddle returned to Motorola and proposed producing a low-cost product to attack this market, but found that management was uninterested; the 6800 was generating nice profits, there seemed to be no reason to change their sales efforts.[1][2]
Undaunted, Peddle started working on such a design on his own. The new design was similar to the 6800 in that it also used a small number of processor registers with a single accumulator. The 6800 supported stacks directly, but the new one introduced a "zero page" that could be used to simulate a single stack, but for far lower cost in terms of circuitry. Additionally the new design fixed one of the 6800's greatest problems, and allowed the accumulator and index registers to be used together as inputs and offsets. Other additions included instructions for binary coded decimal and other practical issues.
As the effort continued, the design team grew increasingly unhappy with management's lack of support. At the same time, management grew increasingly upset with the team spending time on a project they didn't support. Eventually Peddle was sent an official memo from management telling him to stop working on the low-cost design. In response, much of the original 6800 design team resigned en masse and went looking for a company that would be interested in building their new design.[1]
Moving to MOS
The team was soon in negotiations with MOS Technology, at that time a small fab who produced chips for 3rd parties with limited internal design capability. The company had originally been set up by Allen-Bradley as a second-source for Texas Instruments calculator chips, but had since branched out to produce runs for a number of different companies. One of their recent products at the time was a single-chip implementation of the popular Pong video game. The existing MOS engineers were not pleased to find an entire new team parachuted into their company. They were even more upset when they learned that the team would be cut in on part of any profits the new design generated.[1]
At MOS, they quickly designed the 6501, a completely new processor that was pin-compatible with the 6800. This allowed it to be plugged into motherboards designed for the Motorola processor, although its instruction set was different. Motorola sued immediately, and MOS agreed to stop producing the 6501 and went back to the drawing board. The result was the "lawsuit-compatible" 6502, which was by design unusable in a 6800 motherboard but otherwise identical to the 6501. Motorola had no objection.
However, this left MOS with the problem of getting developers to try their processor, so engineer Peddle designed the MDT-650 (for "microcomputer development terminal") single-board computer. Another group inside the company designed the KIM-1. Much to their surprise, the KIM-1 sold well to hobbyists and tinkerers as well as to the engineers it was intended for. The related Rockwell AIM 65 control/training/development system also did well. The software in the AIM 65 was based on that in the MDT. Another roughly similar product was the Synertek SYM-1.
Introducing the 6502
The 6502 was introduced at $25 at the Wescon show in September 1975. The company had an off-floor suite with a big jar full of the chips. However with this early run they only had a handful of working ones. To give the appearance of larger quantities, the bottom of the jar was stuffed with defective chips, and only the ones at the top of the jar worked.[3] At the same show the 6800 and Intel 8080 were selling for $179.[4] At first many people thought the new chip's price was a hoax or a mistake, but while the show was still ongoing both Motorola and Intel had dropped their chips to $79.[5] These price reductions legitimized the 6502, which started selling by the hundreds.[3]
One of the first "public" uses for the design was the Apple I computer, introduced in 1976. The 6502 was next used in the Commodore PET and the Apple II. It was later used in the Atari home computers, the BBC Micro family, the Commodore VIC-20 and a large number of other designs both for home computers and business, such as Ohio Scientific and Oric. The 6510, a direct successor of the 6502 with a digital I/O port and a tri-state address bus, was the CPU utilized in the Commodore 64 home computer. (Commodore's disk drive, the 1541, had a processor of its own—it too was a 6502.)
Another important use of the 6500 family was in video games. The first to make use was the Atari 2600 video game console. The 2600 used an offshoot of the 6502 called the 6507, which had fewer pins and, as a result, could address only 8 KB of memory. Millions of the Atari consoles would be sold, each with a MOS processor. Another significant use was by the Nintendo Famicom, a Japanese video game console. Its international equivalent, the Nintendo Entertainment System, also used the processor. The 6502 used in the NES was a second source version by Ricoh, a partial system-on-a-chip, that lacked the binary-coded decimal mode but added 22 memory-mapped registers (and on-die hardware) for sound generation, joypad reading, and sprite list DMA. Called 2A03 in NTSC consoles and 2A07 in PAL consoles (the difference being the memory divider ratio and a lookup table for audio sample rates), this processor was produced exclusively for Nintendo.
Technical description
The 6502 is an 8-bit processor with a 16-bit address bus. The internal logic runs at the same speed as the external clock rate, but despite the slow clock speeds (typically in the neighborhood of 1 to 2 MHz), the 6502's performance was actually competitive with other CPUs using significantly faster clocks. This is partly due to a simplistic state machine implemented by combinatorial (clockless) logic to a greater extent than in many other designs; the two phase clock (supplying two synchronizations per cycle) can thereby control the whole machine-cycle directly. Like most simple CPUs of the era, the dynamic NMOS 6502 chip was not sequenced by a microcode ROM but used a PLA (which occupied about 15% of the chip area) for instruction decoding and sequencing. Like most eight-bit microprocessors, the chip does some limited overlapping of fetching and execution.
The low clock frequency moderated the speed requirement of memory and peripherals attached to the CPU, as only about 50% of the clock cycle was available for memory access (due to the asynchronous design, this percentage varied strongly among chip versions). This was critical at a time when affordable memory had access times in the range 450-250ns. The original NMOS 6502 was minimalistically engineered and efficiently manufactured and therefore cheap—an important factor in getting design wins in the very price-sensitive game console and home computer markets.
![](http://upload.wikimedia.org/wikipedia/commons/thumb/4/44/MOS6502.svg/215px-MOS6502.svg.png)
Like its precursor, the Motorola 6800 (but unlike Intel 8080 and similar microprocessors) the 6502 has very few registers. At the time the processor was designed, the number of transistors that could be economically put on a chip was very constrained (around a few thousand), so it made sense to rely on RAM instead of wasting expensive NMOS chip area on CPU registers.
The 6502's registers included one 8-bit accumulator register (A), two 8-bit index registers (X and Y), an 8-bit processor status register (P), an 8-bit stack pointer (S), and a 16-bit program counter (PC). The subroutine call/scratchpad stack's address space was hardwired to memory page $01, i.e. the address range $0100–$01FF (256–511). Software access to the stack was done via four implied addressing mode instructions whose functions were to push or pop (pull) the accumulator or the processor status register. The same stack was also used for subroutine calls via the JSR (Jump to Subroutine) and RTS (Return from Subroutine) instructions, and for interrupt handling.
The chip used the index and stack registers effectively with several addressing modes, including a fast "direct page" or "zero page" mode, similar to that found on the PDP-8, that accessed memory locations from address 0 to 255 with a single 8-bit address (saving the cycle normally required to fetch the high-order byte of the address)—code for the 6502 used the zero page much as code for other processors would have used registers. On some 6502-based microcomputers with an operating system, the OS would use most of zero page, leaving only a handful of locations for the user.
Addressing modes also included implied (1 byte instructions); absolute (3 bytes); indexed absolute (3 bytes); indexed zero-page (2 bytes); relative (2 bytes); accumulator (1); indirect,x and indirect,y (2); and immediate (2). Absolute mode was a general-purpose mode. Branch instructions used a signed 8-bit offset relative to the instruction after the branch; the numerical range -128..127 therefore translates to 128 bytes backward and 127 bytes forward from the instruction following the branch (which is 126 bytes backward and 129 bytes forward from the start of the branch instruction). Accumulator mode used the accumulator as an effective address, and did not need any operand data. Immediate mode used an 8-bit literal operand.
The indirect modes were useful for array processing and other looping. With the 5/6 cycle "(indirect),y" mode, the 8-bit Y register was added to a 16-bit base address in zero page which was located by a single byte following the opcode. The Y register was therefore an index-register in the sense that it was used to hold an actual index (as opposed to the X register in the 6800 where a base address was directly stored and to which an immediate offset could be added). Incrementing the index register to walk the array byte-wise took only two additional cycles. With the less frequently used "(indirect,x)" mode the effective address for the operation was found at the zero page address formed by adding the second byte of the instruction to the contents of the X register. Using the indexed modes, the zero page effectively acted as a set of 128 additional (though very slow) address registers.
The 6502 also included a set of binary coded decimal (BCD) instructions, a feature normally implemented in software. Placing the CPU into BCD allowed numbers to be manipulated in base-10, with a set of conversion instructions to convert between base-10 and binary (base-2). For instance, with the "D" flag set, 99+1 would result in 00 and the carry flag being set. These instructions made implementing a BASIC programming language easier, removing the need to convert numbers for display in the BASIC interpreter itself. However, this feature meant other useful instructions could not be implemented due to a lack of CPU real estate, and was sometimes removed to make room for custom instructions.
A Byte magazine article[citation needed] once referred to the 6502 as "the original RISC processor," due to its efficient, simplistic, and nearly orthogonal instruction set (most instructions work with most addressing modes), as well as its 256 zero-page "registers". The 6502 is technically not a RISC design however, as arithmetic operations can read any memory cell (not only zero-page), and some instructions (inc, rol etc.) even modify memory contrary to the basic load/store philosophy of RISC. Furthermore, orthogonality is equally often associated with "CISC". However the 6502 performed reasonably well compared to other contemporary processors such as the Z80, which used a much faster clock rate, and the 6502 has been credited as being inspirational to RISC processors such as the ARM.[6] However the inspiration from the 6502 was related to the simple implementation, rather than the architecture, which is very different to that of the ARM.
See the Hello world! article for a simple but characteristic example of 6502 assembly language.
Detailed behavior
The processor's non-maskable interrupt (NMI) input is edge sensitive, which means that if the source of an NMI holds the line low, NMIs are effectively disabled.
The simultaneous assertion of the NMI (non-maskable) and IRQ (maskable) hardware interrupt lines causes IRQ to be ignored. However, if the IRQ line remains asserted after the servicing of the NMI, the processor will immediately respond to IRQ, as IRQ is level-sensitive. Thus a sort of built-in interrupt priority was established in the 6502 design.
The "Break" flag of the processor is very different from the other flag bits. It has no flag setting, resetting, and testing instructions of its own, and is not handled by the PHP and PLP instructions either. It exists only on the stack, where BRK and PHP write a 1, while IRQ and NMI write a 0.
Bugs and quirks
![]() | This section possibly contains original research. (May 2008) |
The original 6502 and its NMOS derivatives are noted for having a variety of undocumented instructions, which vary from one chip manufacturer to the next. The 6502's instruction decoding is implemented in a hardwired logic array (similar to a programmable logic array) which is only defined for 151 of the 256 available opcodes. The remaining 105 trigger strange and hard-to-predict actions (e.g., immediately crashing the processor, performing several valid instructions at once, or simply doing nothing at all). Eastern House Software developed the "Trap65", a device that plugged between the processor and its socket to convert (trap) unimplemented opcodes into BRK (software interrupt) instructions. Some programmers utilized this feature to extend the 6502's instruction set by providing functionality for the unimplemented opcodes with specially written software intercepted at the BRK instruction's 0xFFFE vector. All of the undefined opcodes have been replaced by NOP instructions in the 65C02 CMOS version (although with varying byte sizes and execution times).
The 6502's memory indirect jump instruction, JMP (<address low-word>), is partially broken. If <address low-word> was hex xxFF (i.e. any word ending in FF), the processor would not jump to the address stored in xxFF and xxFF+1, but rather the one in xxFF and xx00, an effect referred to as "address wrapping." This defect continued through the entire NMOS line, but was fixed in the CMOS derivatives.
The 6502's Indirect-Indexed-Y ((Ind),Y) addressing mode is also partially broken. If the zero-page address was hex FF (i.e. last address of zero-page FF), the processor would not fetch data from the address pointed to by 00FF and 0100 + Y, but rather the one in 00FF and 0000 +Y. This defect continued through the entire NMOS line, but was fixed in some of the CMOS derivatives.
The 6502's Indirect-Indexed-X ((Ind,X)) addressing mode is also partially broken if the zero-page address was hex FF (i.e. last address of zero-page FF), again a case of address wrap.
The NMOS 6502's indexed addressing across page boundaries will do an extra read of invalid address. This can cause issues by accessing hardware that acts on a read (e.g., clearing timer or IRQ flags, sending an I/O handshake, etc.). This defect continued through the entire NMOS line, but was fixed in the CMOS derivatives, in which the processor does an extra read of the last instruction byte.
The 6502's Read/Modify/Write instruction at effective address does one read and two write cycles. This can cause issues by twice accessing hardware that acts on a write. This defect continued through the entire NMOS line, but was fixed in the CMOS derivatives, in which the processor will do two reads and one write cycle.
The N (result negative), V (sign bit overflow) and Z (result zero) status flags are not valid when performing arithmetic operations while the processor is in BCD mode, as these flags reflect the binary, not BCD, result. This limitation was removed in the CMOS derivatives. Therefore, this feature may be used to cleanly distinguish CMOS from NMOS CPU versions without using any illegal opcodes.[7]
If the processor happens to be in BCD mode when a hardware interrupt occurs it will not revert to binary mode. This quirk could result in hard-to-solve bugs in the interrupt service routine if it failed to clear BCD mode before performing any arithmetic operations. For example, the Commodore 64's kernel did not correctly handle this processor characteristic, requiring that IRQs be disabled or revectored during BCD math operations. This issue was addressed in the CMOS derivatives as well.
The SO pin (Set Overflow) was intended for use in high-speed device drivers. Asserting it would immediately set the processor's Overflow (V) status register bit. Successful use of this feature could eliminate a load instruction from a high-speed device driver, reducing the number of instructions in a data transfer loop by 25 percent, but obviously great care was required in the system design in order not to corrupt general computation. In practice, the SO feature was seldom used.
The 6502 instruction set includes BRK (opcode $00, deliberately chosen to catch the processor should a fault cause it to attempt to execute data), which is technically a software interrupt (similar in spirit to the SWI mnemonic of the 6800). BRK is most often used to interrupt program execution and start a machine code monitor for testing and debugging during software development. It could also be used to route program execution using a simple jump table (analogous to the manner in which the 8088 and derivatives handle software interrupts by number). Unfortunately, if a hardware interrupt occurs at the same time the processor is fetching a BRK instruction, the NMOS version of the processor will fail to execute BRK and instead proceed as if only a hardware interrupt had occurred. This fault was corrected in the CMOS implementation of the processor.
The JSR (call subroutine) instruction pushes the address of the last byte of the call instruction on to the stack (the program counter would have been increased after execution has been completed). The RTS (return) instruction pulls the return address off the stack and increments it before placing it into the program counter, resulting in automatic compensation for this design quirk.[8] This characteristic would go unnoticed unless you pulled the return address to pick up parameters in the code stream (a common 6502 programming idiom). It remains a characteristic of 6502 derivatives to this day.
16-bit derivatives
The Western Design Center designed and produced the 65C816 processor, a 16-bit successor to the 65C02, as well as a hybrid offshoot called the 65C802 which was a 65C816 core with a 64 KB address space in a 65(C)02 pin-compatible package—it could be plugged into a 6502 board and would function as a 65C02, or it could be configured via software to expose its 16 bit accumulator and index registers. Few 65C802 parts were sold, and the chip is no longer produced.
The 65C816 was the core of the widely popular Super Nintendo Entertainment System, and the Apple IIGS. The 65C816 is still widely used, both as a discrete processor and as the core of a microcontroller.
Mitsubishi (now Renesas Technology) made a line of 16-bit microcontrollers with an architecture very similar to the 65816, though it was not 100% compatible.
Synertek published a data sheet and application notes on the SY6516, a 16-bit derivative of the 6502, but the part was never introduced.
32-bit derivatives
The Western Design Center at one time claimed to be developing a W65T32 Terbium, a compatible 32-bit follow-up to the W65C816. The Terbium was named after the 65th element as 65 is the prefix to all of WDC's microprocessor number identifiers. The chip would have had a 32-bit address bus, a 16-bit data bus, and a variable length instruction set.
Acceleration
Many users of 1 MHz 6502-based systems soon wished their computers could go faster. As the 6502 is externally-clocked, upgrading the speed involved more than dropping a faster chip into the processor socket. To meet user demand, a number of companies sold hardware to speed up those systems. These "accelerators" included a modicum of high-speed RAM and glue circuitry used to synchronize the faster processor with the computer's original RAM and its peripherals. For example, the Apple II floppy disk relied on software accessing the controller's I/O registers with critical timing; Apple II accelerators were therefore designed to fall back to 1 MHz during disk access. The first accelerators were circuit boards; some later accelerators (such as the Zip Chip) miniaturized the processor and support circuits to fit into a DIP package that was plug-compatible with the original processor.
6502 in popular culture
In the science fiction movie The Terminator (1984), starring Arnold Schwarzenegger, the audience at one point is treated to a view through the T-800 Model-101 robot character's eye/camera display with some 6502 assembly/machine code program fragments scrolling down the screen. The program was listing the Apple DOS 3.3 disassembled program listing.[citation needed] Also shown is the output from a run of an Apple checksum program called KEY PERFECT 4.0, published in Nibble magazine.[citation needed]
Bender, a fictional android "industrial robot" manufactured in 2998 CE, and a main character in the animated TV series Futurama created by David X. Cohen, was revealed to have a 6502 as his "brain" in the episode "Fry and the Slurm Factory". Cohen has confirmed that he and friends David Borden and David Schiminovich wrote an assembly language compiler (for a custom language they created named "FLEET") for the Apple II Plus, that uses the 6502, while at high school, which led to the reference in the show.[9]
See also
![](http://upload.wikimedia.org/wikipedia/commons/thumb/d/df/Wikibooks-logo-en-noslogan.svg/40px-Wikibooks-logo-en-noslogan.svg.png)
- Lisa assembler
- MOS Technology 6502-based home computers
- Interrupts in 65xx processors
- Vasm, a free assembler
References
- ^ a b c Ian Matthews, "The Rise of MOS Technology & The 6502", 26 June 2006
- ^ Robert Sugarman, "Does the Country Need A Good $20 Microprocessor?", Electronic Engineering Times, 25 August 1975, pg. 25
- ^ a b Brian Bagnall, "On the Edge: The Spectacular Rise and Fall of Commodore", Variant Press, 2005, p. 24
- ^ James (September 1975). "James Advertisement". Popular Electronics. 8 (3): 107. James is now Jameco Electronics. The 8080 CPU was $149.95, the 8008 CPU was 29.95. A 2102 1K bit static RAM was $4.95. Their price for a 8080A CPU was 37.95 in the June 1976 issue.
- ^ Digi-Key (December 1975). "Digi-Key Advertisement". Popular Electronics. 8 (6): 124. The 8080A CPU was $69.50. A 2102 1K bit static RAM was $3.50. Digi-Key's price for a 8080A was $34.95 in the June 1976 issue.
- ^ Risc Vs Cisc
- ^ http://web.archive.org/web/20080102014138/http://www.s-direktnet.de/homepages/k_nadj/cputest.html
- ^ Atari Roots - Chapter 6
- ^ The Truth About Bender's Brain: David X. Cohen, of "Futurama," reveals how MOS Technology's 6502 processor ended up in the robot's head, IEEE spectrum magazine, May 2009. Retrieved 2009-05-02
- Leventhal, Lance A. (1986). 6502 Assembly Language Programming 2nd Edition. Osborne/McGraw-Hill. ISBN 0-07-881216-X.
- Leventhal, Lance A. (1982). 6502 Assembly Language Subroutines. Osborne/McGraw-Hill. ISBN 0-931988-59-4.
- Mansfield, Richard (1983). Machine Language For Beginners. Personal Computer Machine Language Programming For The Atari, VIC, Apple, Commodore 64, And PET/CBM Computers (or, Machine Language Programming For BASIC Language Programmers). Greensboro, North Carolina: Compute! Publications, Inc. Copyright © 1983, Small System Services, Inc. ISBN 0-942386-11-6.
- Zaks, Rodnay (1983). Programming the 6502 (Fourth Edition). Sybex, Inc. ISBN 0-89588-135-7.
- Zaks, Rodnay (1982). Advanced 6502 Programming. Sybex, Inc. ISBN 0-89588-089-X.
- Fylstra, Daniel (November 1975). "Son of Motorola (or the $20 CPU Chip)". BYTE. 1 (3). Peterborough NH: Green Publishing: 56–62. A comparison of the Motorola M6800 and the MOS Technology MCS6501/MCS6502 microprocessors. It was written in August 1975, about a month before the 6500 family was introduced at the WESCON trade show in San Francisco CA., (September 16-19, 1975)
- Sugarman, Robert (August 25, 1975), "Does the Country Need a Good $20 Microprocessor?" (PDF), Electrical Engineering Times, p. 25, retrieved 2009-05-03
- "3rd Generation Microprocessor" (PDF). Microcomputer Digest. 2 (2). Cupertino CA: Microcomputer Associates: pp. 1–3. August 1975.
{{cite journal}}
:|pages=
has extra text (help)
External links
General information:
- 6502.org - the 6502 microprocessor resource – Repository and portal (Mike Naberezny)
- 650x information – Concise description, photos of MOS and second source chips; at cpu-collection.de (Dirk Oppelt)
- a2DMAMagic – The secret, transparent, high-throughput 6502 DMA channel (by Jorge Chamorro Bieling)
- mdfs.net – 6502 instruction set
Instruction set features:
- 6502 - the first RISC µP – With link to concise 6502 programming chart in PDF (Eric Clever)
- Harrod, Dennette A. (1980). "6502 Gets Microprogrammable Instructions". BYTE. 5 (10). McGraw Hill: pp 282–285. ISSN 00360-5280.
{{cite journal}}
:|pages=
has extra text (help); Unknown parameter|month=
ignored (help)
Emulators and simulators:
- List of 6502 software emulators – At Zophar's Domain (Sam Michaels)
- 6502 simulator for Windows – At Dan Boris' homepage hosted by The Atari Gaming Headquarters
- Web-based 6502 compatible compiler and emulator written in JavaScript – by Stian Søreng
Early 6502 computers:
- Apple I Owners Club – Hosted by the Applefritter site (Tom Owad)
- KIM-1 schematics – At The Obsolete Technology Website (anonymous collector)
- Rockwell AIM 65 – The Old Computer Hut
Hardware accelerators:
- 6502 accelerator information – At the Apple II History website (Steven Weyhrich)
- "Underneath the Hood of the SuperCPU" – By Jim Brain, in C=Hacking, Issue #12, Version 1.3, March 1996 (search for "@(#)cmdcpu")
Hardware:
- The Western Design Center, Inc. (WDC) – home of the 65xx brand CMOS microprocessors.
This article is based on material taken from the Free On-line Dictionary of Computing prior to 1 November 2008 and incorporated under the "relicensing" terms of the GFDL, version 1.3 or later.