Programming the 65816 Including the 6502, 65C02, and 65802 (1986)
For years, the 6502 stood alone as the original and sole member of the 65x series—or 6500 series, as the family was originally to be called. First shipped in 1975, the 6502 was, at its height, the most popular eight-bit microprocessor on the market, with tens of millions sold. It is found in such personal computers as those made by Acorn, Apple, Atari, Commodore, and Ohio Scientific—to name some of the leading manufacturers of past and present—as well as in video games and dedicated control applications. Currently the 6502 is manufactured by its original developer, MOS Technology, and also by Rockwell International.
The 65C02, first introduced in 1983, was intended as a replacement for the 6502. Using the CMOS fabrication process which became popular for microprocessor manufacturing in the early eighties, it strove for (and for most practical purposes achieved) complete compatibility with the 6502, and sought to differentiate itself in the market primarily by virtue of its CMOS fabrication. Nonetheless, it included several significant enhancements to the 6502 instruction set and fixed some of the known problems in the 6502 design. These minor extensions, it turned out, were intimations of the 65802 and 65816 to come.
The 65C02 was the design effort of William D. Mensch, Jr., who had been, at MOS Technology, the lead designer on the original 6502 development project. Mensch left MOS Technology to found his own company, The Western Design Center, where he designed the 65C02. In addition to being available from the Western Design Center, the 65C02 is also manufactured by GTE Microcircuits, NCR, Rockwell International, and Hyundai. The first notable adoption of the 65C02 was by Apple Computer for their portable Apple //c computer, in which the low power consumption and low heat generation that results from the CMOS process provides significant advantage over the 6502.
Almost immediately after completing the 65C02, Mensch and The Western Design Center began work on the 65816 and 65802 processors, sixteen-bit versions of the original 6502 design. In addition to the strengths they inherit from the 6502 and the set of powerful new extensions they implement, the 65802 and 65816 are unique among modern microprocessors in that they faithfully execute the object code of their eight-bit predecessors, the 6502 and 65C02.
Although they are two distinct products, the 65802 and 65816 are really just two versions of the same design, which is fully realized in the 65816, with its sixteen-megabyte address space. The 65802, on the other hand, provides compatibility with the 6502 not only on a software level but, incredibly, on a hardware level, too: it can replace a 6502 or 65C02 in an existing system and emulate the processor it replaces faithfully, even as it provides a broad range of new features like sixteen-bit registers; but all that compatibility leaves it confined to the earlier processors' 64K address space.
The hardware compatibility of the 65802 makes the 65816 architecture readily accessible to the thousands of users of existing personal computers. It will undoubtedly provide many users with their first exposure to the 65816.
How to Use this Book
The uniqueness of the 65802's and 65816's compatibility with the 6502 and 65C02 cried out for a unique approach to an assembly language book about them: an introduction not just to one of these microprocessors, but to the entire family of them.
How you approach this book will depend most of all on who you are. If you have little experience with assembly language, you should probably begin with Chapter One, Basic Programming Concepts, and read sequentially. You will find that it introduces you to the concepts essential to understanding everything that follows. It should also provide a useful and convenient review for more experienced readers.
If you understand assembly language, but have little or no experience with 65x family processors, you should begin with Part Two, Architecture. Each of the three chapters introduces the architecture of one of the three generations of 65x processors. Because the 65802 executes the same instruction set as the 65816 (as limited by the 65802's memory space restrictions), these two share a single chapter. Each chapter builds on the last, so you should read them in order: Since the 65816 is a superset of all of the other processors, each chapter describes a larger subset of the complete 65816 design. Furthermore, they illustrate the register set and other basics on which the tutorial section which follows is based.
If you know and have worked with the 6502 before, you may want to skip or lightly skim the 6502 architecture chapter and go right on to the 65C02 chapter. If you know the 65C02, you can go right on to the 65816/65802 chapter.
Part Three, Tutorial, is a teaching section, with code examples sprinkled throughout. It is devoted to a step-by-step survey of all 256 different instructions, grouped into six categories (moving data, flow of control, arithmetic, logic and bit manipulation, subroutines, and system control and interrupts), and all 25 different addressing modes, divided into two classes (simple and complex).
Those of you who either have no experience with assembly language or have no experience with the 65x family will find it especially helpful. Even if you're familiar with the 65x family, however, you may want to selectively read from this section.
Having built up to a concept of the 65816 by examining its predecessor designs, the tutorial section views the entire series from this vantage—that of the full 65816 architecture. Of course, the 65816 is a superset of all the other members of the 65x family, so a complete discussion of the 65816 is by definition a discussion of all the other processors as well.
Almost all of the examples in this section and the next are intended to be executed on a system with either a 65802 or 65816 processor, and more likely than not include 65816 instructions, although there are some examples which are intentionally restricted to either the 6502 or 65C02 instruction set for purposes of comparison.
As the 65816 is explored, however, care is taken to distinguish features, such as instructions or addressing modes, by the processors that they are common to. In this way, this book provides the only reference needed for the programmer faced with developing software for more than one of the different processors in the series.
The highlighting and contrasting of the differences between the processors in the series should also be helpful for the programmer already familiar with one processor who wants to learn another—both the 65816 programmer who needs to restrict his knowledge when programming for the 6502, as well as the 6502 programmer who wishes to learn the 65816.
If your interest is in writing applications for the 65x processors, you will find Part Four, Applications, of particular interest and use. From the selected code examples in Chapter 14 to the debugging tool in Chapter 15 to the debugging checklist in Chapter 16, this section should provide helpful, down-to-earth examples and how-to.
But even if your interest in the 65x family is strictly academic, you should study the examples in Chapter 14: The code for the sieve of Eratosthenes, for example, provides you the means of comparing the 65816/65802 with other processors, in design, size, and speed; multiply and divide routines for all three generations of 65x processors demonstrate what can be involved in conversion between them; there's a comparison between machine code created by a hypothetical compiler and assembly code written by a hypothetical programmer; and there are routines which deal with the likelihood that many readers will write 65802 programs to be run under 6502-based and 65C02-based operating systems.
Finally, Part Five, Reference, is designed so you can turn to it over and over for information and detail on how the various instructions and addressing modes work, their syntax, and their opcodes. You'll find fully illustrated addressing modes arranged alphabetically in Chapter 17; the instructions arranged alphabetically, with descriptions and tables of opcodes and syntax, in Chapter 18; and the instructions listed four ways—alphabetically, functionally, numerically, and mapped in a matrix—in Chapter 19.
If you're a whiz at assembly language or already know one of the 65x processors intimately, this section may be all you need to learn and use the entire 65x family (although we recommend looking over the architecture and applications sections for ideas and review; you may also want to use the Debugl6 program in Chapter 15 as an aid for developing code).
If you need specialized information—hardware descriptions, data sheets, compatible I/O parts, cycle descriptions, instruction group breakdowns, deviant family members, and an ASCII chart (with high-bit both set and reset)—you'll find it in the appendix.