
Naviagtion
Home
|
6502 Cross Overlay Overview
IntroductionThis is where it all began with for me. I started assembly programming on an Apple ][ computer powered by a 6502 processor. My first SB-Assembler was originally written on and for a 6502 processor and that's why it is actually tailored for this processor.
The 6502 was designed by MOS Technologies in NMOS technology.
Unfortunately that company doesn't exist anymore, otherwise we might even be running on 6502 descendants today, rather than on Pentiums ;-)
The original 6502 had some bugs in it, but none serious enough to be bothered about.
All 3 processor types are so alike that I can describe them here in one page.
I think it is obvious that the 6502.sba and cr6502.py overlays are meant for the 6502 itself.
To make things even more complicated not all CMOS versions have the WAI and STP instructions.
None of the Version 2 cross overlays can handle these instructions.
The CMOS cross overlay cr65c02.py for Version 3 can assemble these instructions however.
I think the greatest power of the 6502 is its simplicity and its flexible addressing modes. Especially the zero page addressing was a wonderful invention (or was it invented by Motorola?). Programming ModelThe programming model in the picture below shows the most important registers of the 6502 processor. I only include a little summary about the features of the 6502's programming model here. It is not my intention to make the original documentation obsolete, so please refer to the original documentation for further details. More information about the 6502 can be found on www.6502.org, or better still at Andrew Jacobs' site where you can find plenty of information about the 6502 and the 65C02.
Don't be alarmed by the relatively small number of registers compared to other processor types. The 6502 has a very powerful addressing mode called zero page addressing. This way all 256 bytes of page 0 in memory can be addressed with only 8 bits. These 256 addresses can be considered the "registers" of the 6502! The Accumulator The Accumulator is the most important register for 8 bit arithmetic operations. It can not be used as an index register The Index registers X and Y The 6502 has 2 index registers, called X and Y. They are both 8 bits wide and can be used as temporary registers, or as index pointers in a variety of addressing modes. The index registers can not be used for arithmetic operations. The Program Status Register The P register holds all the system flags. Some flags are set or reset under program control. Others reflect the status of the machine after mathematical instructions. The P contains 8 system flags:
There is a small difference in behaviour between the 6502 and the 65C02 when it comes to the Decimal flag.
The Decimal flag of the 6502 is not initialized during reset, leaving it in an unpredictable state.
Therefore you'll always see a CLD instruction directly after reset on all 6502 programs.
The Stack Pointer The stack pointer of the 6502 is only 8 bits long and therefore the stack is limited to 256 bytes. The stack itself is always located on page $01 in RAM memory. The NMOS 6502 could only push or pull the accumulator (PHA, PLA) and the P register (PHP, PLP) on the stack. Therefore context switching speed during interrupts was quite low. The CMOS 65C02 processors can also push and pull both index registers to the stack (PHX, PLX, PHY and PLY).
During subroutine calls (JSR) both program counter bytes are pushed on the stack.
The value that is pushed is the address of the next instruction to be executed after return (RTS) minus 1 !
This is a bit odd, but no problem as long as you know it.
First the high byte of PC is saved and the stack pointer S is decremented by 1.
Then the low byte of PC is saved and the stack pointer S is decremented by 1 once more.
The Program Counter The program counter PC is normally incremented after fetching each instruction or operand byte during program execution. The only way you can change this behaviour is with the jump, subroutine and return instructions. Also interrupts can change the program counter's value. Timing
SB-Assmebler Version 3 can show you the cycle times of each instruction when the TON list flag is switched on.
The numbers presented are the number clock cycles the processor needs to execute the instruction.
Reserved WordsThe SB-Assembler 6502 cross overlay family has only one reserved word, which is the letter A which stands for Accumulator. However, I advise you not to use any of the register names as labels. By the way: Labels should have meaningful names for better readability of your sources and I can't think of any meaningful name with only one letter. In Version 3 of the 6502 cross overlays the label SWEET16 has a special meaning. See the description of the Sweet-16 interpreter further on. Special FeaturesCompound instructions
A compound instruction is an instruction that is translated to more than 1 real instruction.
I've added 2 compound instructions to the 6502 family of cross overlays, namely: ADD and SUB.
While programming Version 3 of the SB-Assembler I've noticed a little oops here.
The ADD and SUB mnemonics are also used in the Sweet16 interpreter.
Therefore you can't use the Sweet16 instructions because they will be translated to the wrong opcodes.
Forced zero page and absolute addressing modes One of the strongest features of the 6502 family is its zero page addressing mode. With zero page addressing mode you specify a memory location that can be addressed with only one byte (instead of 2 for all other memory locations). This way the 6502 can be seen as a microprocessor with 256 registers.
The SB-Assembler automatically selects zero page addressing mode when that mode is available and the high byte of the address is $00 (being the zero page).
We only know for sure that the high byte of the address is $00 if there was no unresolved label in the expression identifying the address.
If a forward referenced label is used in an address expression we automatically assume the worst case situation and opt for absolute addressing mode (2 bytes address field).
Examples: 0010- LABEL .EQ $10 A zero page address 8000-A5 10 LDA LABEL Appears to be zero page 8002-A5 11 LDA <FORWARD Clearly a forward referenced label 8004-85 12 STA $12 Is a zero page address 8006-8D 11 00 STA >$11 Force absolute addressing mode 0011- FORWARD .EQ $11 A zero page address Implied Accumulator Instructions
There are some differences in the syntax for some implied accumulator instructions.
On the original 6502 these instructions are ASL, LSR, ROL and ROR.
The 65C02 derivatives add the instructions DEC and INC to this list.
Examples:
ASL Implied Accumulator instruction
ASL A The same instruction with different syntax
ASL $12 This instruction operates on the zero page
JMP (IND) bug
The NMOS 6502 version had some bugs in it.
One of those bugs actually affects the assembly language programming in a very rare occasion.
The bug I'm talking about is called the JMP (IND) bug, and will only be a problem when using this instruction/addressing mode combination.
The SB-Assembler for the NMOS 6502 checks the indirect address to see if it is endangered by the hardware bug.
If it is a "*** 6502 JMP (IND) bug error" is reported to warn you.
It is up to you then to move the indirect jump table out of the danger zone.
Adding one extra byte in front of the table will also do the trick.
Please note that this only applies to the NMOS version of the 6502, so the 65C02 processors are not affected. Therefore those assemblers won't generate the above mentioned error message. Sweet-16 interpreter Early Apple ][ people might know what the Sweet-16 interpreter did, although it was one of the least understood features of the first apple models. It is an interpreter, pretending to be a 16-bit machine on an 8-bit processor. The Sweet-16 interpreter was written by the famous Apple pioneer Steve Wozniak. Only the first Apple ][ models, the ones with Integer basic, are standard equipped with the Sweet-16 interpreter. Later came the models with Applesoft basic and the Autostart monitor ROM and they didn't have room for the interpreter in ROM anymore.
I've added the pseudo Sweet-16 instructions to the 6502 cross overlay family, for those of you who would like to play around with this interpreter.
The trick to use the interpreter is simple:
I have never actually used the Sweet-16 interpreter, I wonder even if someone ever has.
But if you want to use it, the SB-Assembler won't stop you.
I'm not going to include the Sweet-16 interpreter code on my home page or in the assembler package.
I doubt that the original copyrights are still valid, but I'd better be safe than sorry.
You can find lots of information about the Sweet-16 interpreter on the internet using one of the numerous search engines out there.
Differences between SB-Assembler Version 2 and Version 3.
With Version 2 the Sweet-16 and the real 6502 instructions are translated from the same big list.
This means that the assembler will translate Sweet-16 code in between 6502 code, and the other way around, without complaining.
In real life this will result in a program which won't work.
Remember that the Sweet-16 interpreter only runs when it is called with JSR SWEET16.
Differences in syntax from the real Sweet-16 assembler (if there ever was one).
The original Sweet-16 syntax was SET R15 VALUE.
The SB-Assembler uses its own syntax here, which is SET 15,VALUE.
Thus R15 is now just 15 and the space between the two operand is replaced by a comma.
Overlay InitializationTwo things are set while initializing the 6502 overlay family every time the overlay is loaded by the .CR directive.
Differences With Other AssemblersThere are some differences between the SB-Assembler and other assemblers for the 6502 family processor. These differences require you to adapt existing source files before they can be assembled by the SB-Assembler. This is not too difficult though, and is the (small) price you have to pay for having a very universal cross assembler.
|
|||||||||||||||||||||||||||||||||||||||||||