Commodore 64 computer Programmer's reference guide
Below you will find brief information for computer Commodore 64. This document provides detailed information on the Commodore 64 computer system, covering topics such as machine language programming, memory management, and the KERNAL operating system. You will learn about the Commodore 64's architecture, how to write and debug machine language programs, and how to leverage the KERNAL to control various features of the computer system.
Advertisement
Advertisement
WHAT IS MACHINE LANGUAGE?
At the heart of every microcomputer, is a central microprocessor.
It's a very special microchip which is the "brain" of the computer.
The
Commodore 64 is no exception.
Every microprocessor understands its own language of instructions. These instructions are called machine language instructions. To put it more precisely, machine language is the
ONLY programming language that your Commodore 64 understands.
It is the NATIVElanguage of the machine.
If machine language is the only language that the Commodore 64 understands, then how does it understand the CBM BASIC programming language?
CBM BASIC is NOT the machine language of the Commodore
64. What, then, makes the Commodore 64 understand CBM BASIC instructions like PRINT and GOTO?
To answer this question, you must first see what happens inside your
Commodore 64. Apart from the microprocessor which is the brain of the
Commodore 64, there is a machine language program which is stored in a special type of memory so that it can't be changed.
And, more importantly, it does not disappear when the Commodore 64 is turned off, unlike a program that you may have written.
This machine language program is called the OPERATING SYSTEMof the Commodore 64. Your
Commodore 64 knows what to do when it's turned on because its
OPERATING SYSTEM(program) is automatically "RUN."
210
BASIC TO MACHINE LANGUAGE
The OPERATING SYSTEM is in charge of "organizing" all the memory in your machine for various tasks.
It also looks at what characters you type on the keyboard and puts them onto the screen, plus a whole number of other functions.
The
OPERATING
SYSTEM can be thought of as the "intelligence and personality" of the Commodore 64 (or any computer for that matter).
So when you turn on your Commodore 64, the
OPERATING SYSTEM takes control of your machine, and after it has done its housework, it then says:
.
READY.
The OPERATING SYSTEM of the Commodore 64 then allows you to type on the keyboard, and use the built-in SCREEN EDITOR on the Commodore 64. The SCREEN EDITOR allows you to move the cursor, DELete,
INSert, etc., and is, in fact, only one part of the operating system that is built in for your convenience.
All of the commands that are available in CBM BASIC are simply recognized by another huge machine language program built into your
Commodore 64.
This huge program "RUNs" the appropriate piece of machine language depending on which CBM BASIC command is being executed.
This program is called the BASIC INTERPRETER, because it interprets each command, one by one, unless it encounters a command it does not understand, and then the familiar message appears:
?SYNTAX ERROR
.
READY.
WHAT DOES MACHINE CODE LOOK LIKE?
You should be familiar with the PEEKand POKE commands in the CBM
BASIC language for changing memory locations.
You've probably used them for graphics on the screen, and for sound effects.
Each memory location has its own number which identifies it. This number is known as the "address" of a memory location.
If you imagine the memory in the
Commodore 64 as a street of buildings, then the number on each door is, of course, the address.
Now let's look at which parts of the street are used for what purposes.
BASIC TO MACHINE LANGUAGE 211
SIMPLE
MEMORY
MAP OF THE COMMODORE
64
2048 up to:
40959
40960 up to:
49151
49152 up to:
53247
53248 up to:
53294
54272 up to:
55295
55296 up to:
56296
56320 up to:
57343
57344 up to:
65535
ADDRESS
0& 1
2 up to:
1023
1024 up to:
2039
2040 up to:
2047
212
DESCRIPTION
-6510 Registers.
-Start of memory.
-Memory used by the operating system.
-Screen
-
-SPRITE
-8K memory.
pointers.
This is YOUR memory. This is where your BASIC or machine
CBM BASIC Interpreter.
-Special
-VIC-II.
-Color
-110
-8K language programs
-SID Registers.
RAM.
Registers.
BASIC TO MACHINE LANGUAGE programs,
RAM area.
(6526's)
CBM KERNALOperating or both, are stored.
System.
If you don't understand what the description of each part of memory means right now, this will become clear from other parts of this manual.
Machine language programs consist of instructions which mayor may not have operands (parameters) associated with them. Each instruction takes up one memory location, and any operand is contained in one or
.two locations following the .instruction.
In your BASIC programs, words like PRINT and GOTO do, in fact, only take up one memory location, rather than one for each character of the word. The contents of the location that represents a particular BASIC keyword is called a token. In machine language, there are different tokens for different instructions, which also take up just one byte (memory location
= byte).
Machine language instructions are very simple. Therefore, each individual instruction cannot achieve a great deal.
Machine language instructions either change the contents of a memory location, or change one of the internal registers (special storage locations) inside .the microprocessor.
The internal registers form the very basis of machine language.
THE REGISTERS INSIDE
THE 6510
MICROPROCESSOR
THE ACCUMULATOR
This is THE most important register in the microprocessor.
Various machine language instructions allow you to copy the c;:ontents of a memory location into.the accumulator, copy the contents of the accumulator into a memory location, modify the contents of the accumulator or some other register directly, without affecting any memory.
And the accumulator is the only register that has instructions for performing math.
THE X INDEX REGISTER
This
is a very important register. There are instructions for nearly all of
the transformations you can make to the accumulator.
But there are other instructions for things that only the X register can do. Various machine language instructions allow you to copy the contents of a memory location into the X register,copy the contents of the X register into a memory location, and modify the contents of the X, or some other register directly.
BASIC TO MACHINE LANGUAGE 213
THE Y INDEX REGISTER
This is a very important register.
There are instructions for nearly all of the transformations you can make to the accumulator, and the X register. But there are other instructions for things that only the Y register can do. Various machine language instructions allow you to copy the contents of a memory location into the Y register, copy the contents of the Y register into a memory location, and modify the contents of the Y, or some other register directly.
THE STATUS
REGISTER
This register consists of eight "flags" (a flag = something that indicates whether something has, or has not occurred).
THE
PROGRAM COUNTER
This contains the address of the current machine language instruction being executed.
Since the operating system is always "RUN"ning in the
Commodore 64 (or, for that matter, any computer), the program counter is always changing.
It could only be stopped by halting the microprocessor in some way.
THE STACK POINTER
This register contains the location of the first empty place on the stack.
The stack is used for temporary storage by machine language programs, and by the computer.
THE INPUT/OUTPUT
PORT
This register appears at memory locations 0 (for the DATA
DIRECTION
REGISTER)and 1 (for the actual
PORT). It is an 8-bit input/output port.
On the Commodore 64 this register is used for memory management, to allow the chip to control more than 64K of RAM and ROM memory.
The details of these registers are not given here. They are explained as the principles needed to explain them are' explained.
HOW DO YOU WRITE
GUAGE PROGRAMS?
MACHINE LAN-
Since machine language programs reside in memory, and there is no facility in your Commodore 64 for writing and editing machine language
214 BASIC TO MACHINE.
LANGUAGE
programs, you must use either a program to do this, or write for yourself a BASIC program that "allows" you to write mClchine language.
The most common methods used to write machine language programs are assembler progams.
These packages allow you to write machine language instructions in a standardized
mnemonic
format, which makes the machine language program a lot more readable than a stream of numbers!
Let's review: A program that allows you to write machine language programs in mnemonic format is called an assem-
bler.
Incidentally, a program that displays a machine language program in mnemonic format is called a disassembler. Available for your
Commodore 64 is a machine language monitor cartridge (with assembler/ disassembler, etc.) made by Commodore:
64MON
The 64MON cartridge available from your local dealer, is a program that allows you to escape from the world of CBM BASIC, into the land of machine language.
It can display the contents of the internal registers in the 6510 microprocessor, and it allows you to display portions of memory, and change them on the screen, using the screen editor. It also has a built-in assembler and disassembler, as well as many other features that allow you to write and edit machine language programs easily. You don't HAVEto use an assembler to write machine language, but the task is considerably easier with it. If you wish to write machine language programs, it is strongly suggested that you purchase an assembler of some sort. Without an assembler you will probably have to "POKE" the machine language program into memory, which is totally unadvisable.
This manual will give its examples in the format that 64MON uses, from now on. Nearly all assembler formats ar~ the same, therefore the machine language examples shown will almost certainly be compatible.
with any assembler.
But before explaining any of the other features of
64MON, the hexadecimal numbering system must be explained.
HEXADECIMAL NOTATION
Hexadecimal notation is used by most machine language programmers when they talk about a number or address in a machine language program.
Some assemblers let you refer to addresses and numbers in decimal
(base 10), binary (base 2), or even octal (base 8) as well as hexadeci-
BASICTO MACHINELANGUAGE
215
mal (base 16) (or just "hex" as most people say). These assemblers do the conversions for you.
Hexadecimal probably seems a little hard to grasp at first, but like most things, it won't take long to master with practice.
By looking at decimal (base 10) numbers, you can see that each digit falls somewhere in the range between zero and a number equal to the
base less one (e.g., 9). THIS
IS TRUEOF ALL NUMBER BASES.
Binary
(base 2) numbers have digits ranging from zero to one (which is one less than the base). Similarly, hexadecimal numbers should have digits ranging from zero to fifteen, but we do not have any single digit figures for the numbers ten to fifteen, so the first six letters of the alphabet are used instead:
DECIMAL
10
11
12
13
14
5
6
7
8
9
3
4
0
1
2
15
16
HEXADECIMAL
E
F
10
9
A
6
7
8
B
C
D
3
4
5
0
1
2
BINARY
00000000
00000001
00000010
00000011
00000100
00000101
00000110
00000111
00001000
00001001
00001010
00001011
00001100
00001101
00001110
00001111
00010000
.
I
216
BASIC TO MACHINE LANGUAGE
let's look at it another way; here's an example of how a base 10
(decimal number) is constructed:
Base raised by increasing powers:
Equals: .1000
100 10
Consider 4569 (base 10) 4 5 6 9
=(4x 1000)+(5X 100)+(6X 10)+9
Now look at an example of how a base 16 (hexadecimal number) is constructed:
Base raised by increasing powers:
Equals: .4096
256 16
Consider 11D9 (base 16) lID 9
=1 X4096+1 X256+13X 16+9
Therefore, 4569 (base 10)
=
11 D9 (base 16)
The range for addressable memory locations is 0-65535 (as was stated earlier). This range is therefore o-FFFF in hexadecimal notation.
Usually hexadecimal numbers are prefixed with a dollar sign ($). This is to distinguish them from decimal numbers.
let's look at some "hex" numbers, using 64MON, by displaying the contents of some memory by typing:
SYS 8*4096
B*
(or SYS 12*4096)
PC SR AC XR YR SP
.; 0401 32 04 5E 00 F6 (these may be different)
Then if you type in:
.M 0000 0020 (and press 8:1:1111:11.).
you will see rows of 9 hex numbers.
The first 4-digit number is the address of the first byte of memory being shown in that row, and the other eight numbers are the actual contents of the memory locations beginning at that start address.
You should really try to learn to "think" in hexadecimal.
It's not too difficult, because you don't have to think about converting it back into
BASIC TO MACHINE LANGUAGE 217
decimal.
For example, if you said that a particular value is stored at
$14ED instead of 5357, it shouldn't make any difference.
YOUR FIRST
MACHINE LANGUAGE INSTRUCTION
LDALOADTHEACCUMULATOR
In 6510 assembly language, mnemonics are always three characters.
LDA represents "load accumulator with
. . .
," and what the accumulator should be loaded with is decided by the parameter(s) associated with that instruction. The assembler knows which token is represented by each mnemonic, and when it "assembles" an instruction, it simply puts into memory (at whatever address has been specified), the token, and what parameters, are given. Some assemblers give error messages, or warnings when you try to assemble something that either the assembler, or the 6510 microprocessor, cannot do.
If you put a "#" symbol in front of the parameter associated with the instruction, this means that you want the register specified in the instruction to be loaded with the "value" after the "#." For example:
LDA #$05
~
This instruction will put $05 (decimal 5) into the accumulator register.
The assembler will put into the specified address for this instruction, $A9
(which is the token for this particular instruction, in this mode), and it will put $05 into the next location after the location containing the instruction
($A9).
If the parameter to be used by an instruction has "#" before it; i.e., the parameter is a "value," rather than the contents of a memory location, or another register, the instruction is said to be in the "immediate" mode.
To put this into perspective, let's compare this with another mode:
If you want to put the contents of memory location
$102E into the accumulator, you're using the "absolute" mode of instruction:
LDA $102E
The assembler can distinguish between the two different modes because the latter does not have a "#" before the parameter.
The 6510 microprocessor can distinguish between the immediate mode, and the absolute mode of the LDA instruction, because they have slightly different
tokens.
LDA
(immediate) has $A9 as its token, and LDA(absolute), has
$AD as its token.
218 BASICTO MACHINELANGUAGE
The mnemonic representing an instruction usually implies what it does. For instance, if we consider another instruction, LDX, what do you think this does?
If you said "load the X register with.
. . ," go to the top of the class.
If you didn't, then don't worry, learning machine language does take patience, and cannot be learned in a day.
The various internal registers can be thought of as special memory locations, because they too can hold one byte of information.
It is not necessary for us to explain the binary numbering system (base 2) since it follows the same rules as outlined for hexadecimal and decimal outlined previously, but one "bit" is one binary digit and eight bits make up one byte! This means that the maximum number that can be contained in a byte is the largest number that an eight digit binary number can be. This number is 11111111 (binary), which equals $FF (hexadecimal), which equals 255 (decimal).
You have probably wondered why only numbers from zero to 255 could be put into a memory location.
If you try POKE
7680,260 (which is a BASIC statement that "says": "Put the number two hundred and sixty, into memory location seven thousand, six hundred and eighty," the BASIC interpreter knows that only numbers 0
-
255 can be put in a memory location, and your Commodore 64 will reply with:
?ILLEGAL QUANTITY ERROR
.
READY.
If the limit of one byte is $FF (hex), how is the address parameter in the absolute instruction "LDA $1 02E" expressed in memory? It's expressed in two bytes (it won't fit into one, of course).
The lower (rightmost) two digits of the hexadecimal address form the "low byte" of the address, and the upper (leftmost) two digits form the "high byte."
The 6510 requires any address to be specified with its low byte first, and then the high byte. This means that the instruction "lDA $102E" is represented in memory by the three consecutive values:
$AD, $2E, $10
Now all you need to know is one more instruction and then you can write your first program.
That instruction is BRK. For a full explanation of this instruction, refer to M.O.S.
6502 Programming Monuol.
But right now, you can think of it as the END instruction in machine language.
BASIC TO MACHINE LANGUAGE 219
If we write a program with 64MON and put the BRKinstruction at the end, then when the program is executed, it will return to 64MON when it is finished. This might not happen if there is a mistake in your program, or the BRK instruction is never reached (just like an END statement in
BASIC may never get executed).
This means that if the Commodore 64 didn't have a STOP key, you wouldn't be able to abort your BASIC programs!
WRITING
YOUR
FIRST
PROGRAM
If you've used the POKE statement in BASIC to put characters onto the screen, you're aware that the character codes for POKEing are different from CBM ASCII character values.
For example, if you enter:
PRINT ASq"A") (and press .:~llIm~/.
) the Commodore 64 will respond with:
65
.
READY.
However, to put an "A" onto the screen by POKEing, the code is I, enter:
POKE 1024,1
:POKE 55296,14 (and .WII":~/.
) (1024 is the start of screen memory)
The "P" in the POKE statement should now be an "A."
Now let's try this in machine language.
Type the following in 64MON:
(Your cursor should be flashing alongside a "." right now.)
220
.A 1400 LDA#$01 (and press .WIII':U.
BASIC TO MACHINE LANGUAGE
The Commodore 64 will prompt you with:
Type:
.A 1400 A9 01
.A 1402
.
LDA
#$01
.A 1402 STA $0400
(The STA instruction stores the contents of the accumulator in a specified' memory location.)
The Commodore
64
will prompt you with:
.A 1405
..
Now type in:
.A 1405 LDA
#$OE
.A 1407 STA $D800
.A 140A BRK
Clear the screen', and type:
G 1400
The G should turn into an "A" if you've done everything correctly.
You have now written your first machine language program.
Its purpose is to store one character ("A") at the first location in the screen memory. Having achieved this, we must now explore some of the other instructions, and principles.
ADDRESSING MODES
ZERO PAGE
As shown earlier, absolute addresses are expressed in terms of a high and a low order byte. The high order byte is often referred to as the
page of memory. For example, the address $1637 is in page $16
(22), and $0277 is in page $02 (2). There is, however, a special mode of addressing known as zero page addressing and is, as the name implies, associated with the addressing of memory locations in page zero. These
BASIC TO MACHINE LANGUAGE 221
addresses, therefore, ALWAYShave a high order byte of zero. The zero page mode of addressing only expects one byte to describe the address, rather than two when using an absolute address.
The zero page addressing mode tells the microprocessor to assume that the high order address is zero. Therefore zero page addressing can reference memory locations whose addresses are between $0000 and $OOFF.This may not seem too important at the moment, but you'll need the principles of zero page addressing soon.
THE STACK
The 6510 microprocessor has what is known as a stack. This is used by both the programmer and the microprocessor to temporarily remember things, and to remember, for example, an order of events. The
GOSUB statement in BASIC, which allows the programmer to call a sub-
routine, must remember where it is being called from, so that when the
RETURNstatement is executed in the subroutine, the BASIC interpreter
"knows" where to go back to continue executing.
When a GOSUB statement is encountered in a program by the BASIC interpreter, the
BASIC interpreter "pushes" its current position onto the stack before
going to do the subroutine, and when
a RETURN
is executed, the in-
terpreter "pulls" off the stack the information that tells it where it was before the subroutine call was made.
The interpreter uses instructions like PHA, which pushes the contents of the accumulator onto the stack, and PLA (the reverse) which pulls a value off the stack and into the accumulator.
The status register can also be pushed and pulled with the
PHP and PLP, respectively.
The stack is 256 bytes long, and is located in page one of memory. It is therefore from $0100 to $01 FF. It is organized backwards in memory.
In other words, the first position in the stack is at $01 FF, and the last is at $0100. Another register in the 6510 microprocessor is called the stack
pointer,
and it always points to the next available location in the stack.
When something is pushed onto the stack, it is placed where the stack pointer points to, and the stack pointer is moved down to the next posi~ tion (decremented).
When something is pulled off the stack, the stack pointer is incremented, and the byte pointed to by the stack pointer is placed into the specified register.
222 BASIC TO MACHINE LANGUAGE
Up to this point, we have covered immediate, zero page, and absolute mode instructions. We have also covered, but have not really talked about, the "implied" mode. The implied mode means that information is implied by an instruction itself. In other words, what registers, flags, and memory the instruction is referring to. The examples we have seen are PHA, PLA, PHP, and PLP, which refer to stack processing and. the accumulator and status registers, respectively.
NOTE: The X register will be referred to as X from now on, and similarly A (accumulator), Y (Y index registe~), 5 (stack pointer), and P (processor status).
INDEXING
Indexing plays an extremely important part in the running of the 6510 microprocessor.
It can be defined as "creating an actual address from a base address plus the contents of either the X or Y index registers."
For example, if X contains $05, and the microprocessor executes an
LDA instruction in the "absolute X indexed mode" with base address
(e.g., $9000), then the actual location that is loaded into the A register is $9000 + $05
=
$9005. The mnemonic format of an absolute indexed instruction is the same as an absolute instruction except a ",X" or ", Y" denoting the index is added to the address.
EXAMPLE:
LDA $9000,X
There are absolute indexed, zero page indexed, indirect indexed, and indexed indirect modes of addressing available on the 6510 microprocessor.
INDIRECT INDEXED
This only allows usage
of the Y register as the index. The actual ad-
dress can only be in zero page, and the mode of instruction is called indirect because the zero page address specified in the instruction contains the low byte of the actual address, and the next byte to it contains the high order byte.
BASICTO MACHINELANGUAGE 223
EXAMPLE:
Let us suppose that location $02 contains $45, and location $03 contains $1 E. If the instruction to load the accumulator in the indirect indexed mode is executed and the specified zero page address is $02, then the actual address will be:
Low order
High order
= contents of $02
= contents of $03
Y register =$00
Thus the actual address
=
$1 E45 + Y
=
$1 E45.
The title of this mode does in fact imply an indirect principle, although this maybe difficult to grasp at first sight. Let's look at it another way:
"I am going to deliver this letter to the post office at address
$02,MEMORY ST., and the address on the letter is $05 houses past
$1600, MEMORY street." This is equivalent to the code:
LDA #$00
STA $02
LDA #$16
STA $03
LDY#$05
LDA ($02), Y
load low order actual .base address
set the low byte .of the indirect address
load high order indirect address
set the high byte of the indirect address
set the indirect index (Y)
load indirectly indexed by Y
INDEXED
INDIRECT
Indexed indirect only allows usage of the X register as the index. This is the same as indirect indexed, except it is the zero page address of the
pointer that is indexed, rather than the actual base address.
Therefore, the actual base address IS the actual address because the index has already been used for the indirect. Index indirect would also be used if
224 BASIC TO MACHINE LANGUAGE
a table of indirect pointers were located in zero page memory, and the
X register could then specify which indirect pointer to use.
EXAMPLE:
Let us suppose that location $02 contains $45, and location $03 contains $10. If the instruction to load the accumulator in the indexed indirect mode is executed and the specified zero page address is $02, then the actual address will be:
Low order
High order
X register
= contents of ($02 + X)
= contents of ($03+X)
=
$00
Thus the actual pointer is in
=
$02 + X
=
$02.
Therefore, the actual address is the indirect address contained in $02 which is again $1045.
The title of this mode does in fact imply the principle, although it may be difficult to grasp at first sight. Look at it this way:
"I am going to deliver this letter to the fourth post office at address
$01 ,MEMORY ST., and the address on the letter will then be delivered to
$1600, MEMORY street." This is equivalent to the code:
LDA #$00
STA $06
LDA #$16
STA $07
LDX#$05
LDA ($02,X)
load low order actual base address
set the low byte of the indirect address
load high order indirect address
set the high byte of the indirect address
set the indirect index (X)
-load indirectly indexed by X
NOTE: Of the two indirect methods of addressing, the first (indirect indexed) is far more widely used.
BASIC
TO MACHINE LANGUAGE 225
BRANCHES AND TESTING
Another very important principle in machine language is the ability to test, and detect certain conditions, in a smiliar fashion to the "IF.
. .
THEN, IF . . . GOTO" structure in CBM BASIC.
The various flags in the status register are affected by different instructions in different ways. For example, there is a flag that is set when an instruction has caused a zero result, and is reset when a result is not zero. The instruction:
LDA #$00 will cause the zero result flag to be set, because the instruction has resulted in the .accumulator
containing a zero.
There are a set of instructions that will, given a particular condition,
branch to another part of the program.
An example of a branch instruction is BEQ, which means Branch if result EQual to zero.
The branch instructions branch if the condition is true, and if not, the program continues onto the next instruction, as if nothing had occurred.
The branch instructions branch not by the result of the previous instruction(s), but by internally examining the status register.
As was just mentioned, there is a zero result flag in the status register.
The BEQ instruction branches if the zero result flag (known as Z) is set. Every branch instruction has an opposite branch instruction. The BEQ instruction has an opposite instruction BNE, which means Branch on result Not Equal to zero (i.e., Z not set).
The index registers have a number of associated instructions which modify their contents. For example, the INX instruction INcrements the X
index register.
If the X.register contained $FF before it was incremented
(the maximum number the X register can contain), it will "wrap around" back to zero. If you wanted a program to continue to do something until you had performed the increment of the X index that pushed it around to zero, you could use the BNE instruction to continue "looping" around, until X became zero.
The reverse of INX, is DEX, which is DEcrement the X index register. If the X index register is zero, DEX wraps around to $FF. Similarly, there are INY and DEYfor the Y index register.
226 BASIC TO MACHINE LANGUAGE
But what if a program didn't want to wait until X or Y had reached (or not reached) zero?
Well there are comparison
instructions,
CPX and
CPY, which allow the machine
language
programmer to test the index registers with specific values, or even the contents of memory locations.
If you wanted to see if the X register contained $40, you would use the instruction:
CPX #$40
BEQ
(some other part of the program)
compare X with the "value" $40.
branch to somewhere else in the program, if this condition is "true."
The compare, and branch instructions playa major part in any machine language program.
The operand specified in a branch instruction when using 64MON is the address of the part of the program that the branch goes to when the proper conditions are met.
However, the operand is only an offset, which gets you from where the program currently is to the address specified. This offset is just one byte, and therefore the range that a branch instruction can branch to is limited. It can branch from 128 bytes backward, to 127 bytes forward.
NOTE: This is a total range of 255 bytes which is, of course, the maximum range of values one byte can contain.
64MON will tell you if you "branch out of range" by refusing to "as_ semble" that particular instruction.
But don't worry about that now because it's unlikely that you will have such branches for quite a while. The branch is a "quick" instruction by machine language standards because of the "offset" principle as opposed to an absolute address.
64MON allows you to type in an absolute address, and it calculates the correct offset. This is just one of the "comforts" of using an assembler.
NOTE: It is NOT possible to cover every single branch instruction.
For further information, refer to the Bibliography section in Appendix F.
BASIC TO MACHINE LANGUAGE 227
SUBROUTINES
In machine language (in the same way as using BASIC), you can call subroutines.
The instruction to call a subroutine is JSR (Jump to Sub-
Routine), followed by the specified absolute address.
Incorporated in the operating system, there is a machine language subroutine that will PRINT a character to the screen.
The CBM ASCII code of the character should be in the accumulator before calling the subroutine.
The address of this subroutine is $FFD2.
Therefore, to print "HI" to the screen, the following program should be entered:
.A 1400 lDA #$48
.A 1402 JSR $FFD2
.A 1405 lDA #$49
.A 1407 JSR $FFD2
.A 140A lDA #$OD
.A 140C JSR $FFD2
.A 140F BRK
. G 1400
load the C8M ASCII code of "H"
print it
load the C8M ASCII code of "1"
print that too
print a carriage return as well
return to 64MON
will print "HI" and return to 64MON
The "PRINT a character" routine we have just used is part of the
KERNAl jump table. The instruction similar to GOTO in BASIC is JMP, which means JuMP to the specified
absolute address.
The KERNAl is a long list of "standardized" subroutines that control All input and output of the Commodore 64. Each entry in the KERNAlJMPs to a subroutine in the operating system. This "jump table" is found between memory locations $FF84 to $FFF5 in the operating system. A full explanation of the
KERNAl is available in the "KERNAl Reference Section" of this manual.
However, certain routines are used here to show how easy and effective the KERNAl is.
let's now use the new principles you've just learned in another program.
It will help you to put the instructions into context:
228 BASIC TO MACHINE LANGUAGE
This
program will display the alphabet using a
KERNAL
routine. The
only new instruction introduced here is TXATransfer the contents of the X
index register,
into the Accumulator.
.A 1400 LDX#$41
.A 1402 TXA
.A 1403 JSR $FFD2
.A 1406 INX
.A 1407 CPX #$5B
.A 1409 BNE $1402
.A 140B BRK
X
=
CBM ASCII of "A"
-A=X
print character
bump count
have we gone past "Z" ?
no, go back and do more
yes, return to 64MON
To see the Commodore 64 print the alphabet, type the familiar command:
.G 1400
The comments that are beside the program, explain the program flow and logic. If you are writing a program, write it on paper first, and then test it in small parts if possible.
USEFUL TIPS FOR THE BEGINNER
One of the best ways to learn machine language is to look at other peoples' machine language programs.
These are published all the time in magazines and newsletters.
Look at them even if the article is for a different computer, which also uses the 6510 (or 6502) microprocessor.
You should make sure that you thoroughly understand the code that you look at. This will require perseverence, especially when you see a new technique that you have never come across before. This can be infuriating, but if patience prevails, you will be the victor.
Having looked at other machine language programs, you MUST write your own. These may be utilities for your BASIC programs, or they may be an all machine language program.
BASIC TO MACHINE LANGUAGE 229
You should also use the utilities that are available, either IN your computer, or in a program, that aid you in writing, editing, or tracking down errors in a machine language program.
An example would be the
KERNAL, which allows you to check the keyboard, print text, control peripheral devices like disk drives, printers, modems, etc., manage memory and the screen.
It ;s extremely powerful and it is advised strongly that it is used (refer to KERNAL section, Page 268).
Advantages -of writing programs in machine languag-e:
1. Speed -Mac-hine language is thousands of times faster than
BASIC.
hundreds, and in some cases a high level language such as
2. Tightness -A machine language program can be made totally
"watertight," Le., the user can be made to do ONLY what the program allows, and no more. With a high level language, you are relying on the user not "crashing" the BASIC interpreter by entering, for example, a zero which later causes a:
?DIVISION BY ZERO ERROR IN LINE-830
.
READY.
In essence, the computer can only be maximized by the machine language programmer.
APPROACHING A LARGE TASK
When approaching a large task in machine language, a certain amount of subconscious thought has usually taken place.
You think about how certain processes are carried out in machine language.
When the task is started, it is usually a good idea to write it out on paper.
Use block diagrams of memory usage, functional modules of code required, and a program flow. Let's say that you wanted to write a roulette game in machine language.
You could outline it something like this:
230 BASIC TO MACHINELANGUAGE
.
Display title
.
Ask if player requires instructions
. YES-display them-Go to START
.
NO'-Go to START
. STARTInitialize everything
.
MAINdisplay roulette table
.
Take in bets
.
Spin wheel
.
Slow wheel to stop
.
Check bets with result
. Informplayer
.
Player any money left?
. YES-Go to MAIN
.
NO-Inform user!, and go to START
This is the main outline. As each module is approached, you can break it down further.
If you look' at a large indigestable problem as something that con be broken down into small enough pieces to be eaten, then you'll be able to approach something that seems impossible, and have it all faU into place.
This process only improves with practice, so
KEEP
TRYING.
BASIC TO MACHINE
LANGUAGE 231
MCS6510
MICROPROCESSOR
ADC
AND
ASl
Add
Memory to Accumulator with Carry
"AND" Memory with Accumulator
Shift Left One Bit (Memory or Accumulator)
BeC
BCS
BEQ
BIT
BMI
BNE
BPl
BRK
BVC
BVS
Branch on Carry Clear
Branch on Carry Set
Branch on Result Zero
Test Bits in Memory with Accumulator
Branch on Result Minus
Branch on Result not Zero
Branch on Result Plus
Force Break
Brancb on Overflow Clear
Branch on Overflow Set
ClC
ClD
Cli
ClV
CMP
CPX
CPY
DEC
DEX
DEY
Clear Carry Flag
Clear Decimal Mode
Clear Interrupt Disable Bit
Clear Overflow Flag
Compare Memory and Accumulator
Compare Memory and Index X
Compare Memory and Index Y
Decrement Memory by One
Decrement Index X by One
Decrement Index Y by One
EOR
"Exclusive-Or" Memory with Accumulator
INC
INX
INY
Increment Memoryby One
Increment Index X by One
Increment Index Y by One
JMP
Jump to New. Location
232 BASIC TO MACHINE LANGUAGE
INSTRUCTION SET-ALPHABETIC SEQUENCE
JSR
Jump to New location Saving Return Address
LDA
LDX
LDY
LSR
NOP
ORA
PHA
PHP
PLA
PLP
ROL
ROR
RTI
RTS
Load Accumulator with Memory load Index X with Memory
Load Index Y with Memory
Shift Right One Bit (Memory or Accumulator)
No Operation
"OR" Memory with Accumulator
Push Accumulator on Stack
Push Processor Status on Stack
Pull Accumulator from Stack
Pull Processor Status from Stack
Rotate One Bit Left (Memory or Accumulator)
Rotate One Bit Right (Memory or Accumulator)
Return from Interrupt
Return from Subroutine
SBC
SEe
SED
SEI
STA
STX
STY
Subtract Memory from Accumulator with Borrow
Set Carry Flag
Set Decimal Mode
Set Interrupt Disable Status
Store Accumulator in Memory
Store Index X in Memory
Store Index Y in Memory
TAX
TAY
TSX
TXA
TXS
TYA
Transfer Accumulator to Index X
Transfer Accumulator to Index Y
Transfer Stack Pointer to Index X
Transfer Index X to Accumulator
Transfer Index X to Stack Pointer
Transfer Index Y to Accumulator
BASICTO MACHINELANGUAGE
233
The following notation applies to this summary:
A
X, y
M
P
S
I
+
/\
:If
t
...
<-
V
PC
PCH
PCL
OPER
/I
Accumulator
Index Registers
Memory
ProcessorStatus Register
Stack Pointer
Change
No Change
Add
Logical AND
Subtract
Logical Exclusive Or
Transfer from Stack
Transfer to Stack
Transfer to
Transferfrom
Logical OR
Program Counter
Program Counter High
Program Counter Low
OPERAND
IMMEDIATE ADDRESSING MODE
Note: At the top of each table is located in parentheses a reference number (Ref: XX) which directs the user to that Section in the MCS6500 Microcomputer Family
Programming Manual in which the instruction is defined and discussed.
234 BASIC TO MACHINE LANGUAGE
ADC
Operation:
Addressing
Mode
Add memo,y to accumulato, with carry
A + M + C
~
A, C
Ni!-CIDV
111--1
(Ref: 2.2.1)
ADC
Assembly Language
Form
OP No.
No.
CODE
Bytes
Cycles
Immediate
Zero Page
Zero Page,
Absolute
Absolute,
Absolute,
X
Y
X
(lndirec
(Indirect), t, X)
Y
ADC
ADC
ADC
ADC
ADC
ADC
ADC
ADC
II Oper
Oper
Oper, X
Oper
Oper, X
Oper, Y
(Oper,
(Oper),
X)
Y
* Add 1 if page boundary 19 crossed.
69
65
75
6D
7D
79
61
71
3
3
2
3
2
2
2
2
2
3
4
4
4*
4*
6
5*
AND
"AND" memo,y
Logical AND to the accumulator
Operation: A
A
M
~
A
with accumulato,
(Ref: 2.2.3.0)
Addressing
Mode
Assembly Language
Form
AND
N !
C I D V
11----
OP No.
No.
CODE
Bytes Cycles
Immediate
Zero Page
Zero Page,
Absolute
X
Absolute,
Absolute,
(Indirect,
(Indirect),
X
Y
X)
Y
* Add 1 if page boundary
AND
AND
AND
AND
AND
AND
AND
AND
1/ Oper
Oper
Oper, X
Oper
Oper, X
Oper, Y
(Oper,
(Oper),
X)
Y is crossed.
29
25
35
2D
3D
39
21
31
3
3
3
2
2
2
2
2
2
3
4
4
4*
4*
6
5
BASIC TO MACHINE LANGUAGE 235
ASL
ASL
Shift Left One Bit (Memory
ASL
Operation: C
<-
~
...0
or Accumulator)
N -c C I D V
/11---
Addressing
Mode
(Ref: 10.2)
Assembly Language
Form
OP No.
No.
CODE
Bytes Cycles
Accumulator
Zero Page
Zero Page, X
Absolute
Absolute, X
ASLA
ASL Oper
ASL Oper, X
ASL Oper
ASL Oper, X
0A
06
16
0E
IE
2
3
1
2
3
2
5
6
6
7
Bee
Operation:
Branch
BCC
Branch on Carr.v Clear
on C = 0
(Ref: 4.1.1.3)
Addressing
Mode
Assembly Language
Form
Relative
BCC Oper
N i!-C I D V
------
Bee
OP No.
No.
CODE
Bytes
Cycles
90
2
2*
* Add 1 if branch
* Add 2 if branch occurs occurs to same page.
to different page.
BeS
BCS
Branchon carry set
Operation: Branch on C
=
1
(Ref: 4.1.1.4)
Addressing
Mode
Assembly Language
Form
Relative BCS
Oper
N i\ C I D V
------
Bes
OP 110.
No.
CODE
Bytes Cycles
B
2
2*
* Add 1 if branch
* Add 2 if branch occurs occurs to same page.
to next page.
236 BASIC TO MACHINE LANGUAGE
BEO
Operation: Branch
Addressing
Mode on g
BEQ Branch on result zero
1
(Ref:
4.1.1. 5)
AssemblyLanguage
Form
BEO
N g C I D V
------
OP No.
No.
CODE
Bytes Cycles
Relative
BEQ Oper F0
2 2*
* Add 1 if branch
* Add 2 if branch occurs occurs to same to next page.
page.
BIT
BIT Test:bits in memory
with accumulator
A A M, M] ~ N, M6 ~ V Operation:
Bit 6 and ] are transferred to the status
If the result of A 1\M is zero then Z register.
-
1, otherwise
Z
-
0
(Ref:
4.2.1.1)
Addressing
Mode
Assembly Language
Form
BIT
OP
CODE
No.
No.
Bytes Cycles
Zero Page
Absolute
BIT Oper
BIT
Oper
24
2C
2
3
3
4
BMI
Operation:
BMI Branch on result minus
Branch on N
=
1
(Ref:
4.1.1.1)
Addressing
Mode
Assembly Language
Form
BMI
N ~ C I D V
------
OP
No.
No.
CODE
Bytes Cycles
30
2 2*
Relative
BMI Oper
* Add 1 if branch occurs to same page.
* Add 2 if branch occurs to different page.
BASIC TO MACHINE LANGUAGE 237
BNE
BNE
Branch on result not zero
Operation: Branch on Z - 0
Addressing
Mode
(Ref: 4..1.1.6)
Assembly Language
Form
NtCIDV
------
BNE
OP No.
No.
CODE
Bytes Cycles
Relative
BNE
Oper
IX!
2 2*
* Add 1 if branch
* Add 2 if branch occurs occurs to same page.
to different page.
BPL
Operation: Branch
BPL on N
= 0
Branch on result plus
(Ref: 4.1.1.2)
Addressing
Mode
Assembly Language
Form
Relative
BPL Oper
N~CIDV
------
BPL
OP No.
No.
CODE
Bytes Cycles
10
2 2*
* Add 1 if branch
* Add 2 if branch occurs occurs to same page.
to different page.
BRK
Operation:
BRK
Forced Interrupt
Force Break
PC + 2 ~ P ~
(Ref:
9.11)
Addressing
Mode
Assembly Language
Form
Implied
BRK
1.
A BRK command cannot be masked by setting I.
238
BASIC
TO MACHINE LANGUAGE
IRK
N ~ C I D V
---1--
OP
CODE
No.
No.
Bytes Cycles
00
1 7
Bye
BVC Branch
overflow clear
Operation: Branch on V
= 0
(Ref:
4.1.1.8)
N~CIDV
------
Addressing
Mode
Relative
Assembly Language
Form
BVC Oper
Bye
OP
No.
No.
CODE
Bytes
Cycles
50
2 2*
* Add 1 if branch
* Add 2 if branch occurs occurs to same page.
to different page.
ays
Operation:
Addressing
Mode
Branch
BVS
Branch on overflow set
on V = 1
(Ref: 4.1.1.7)
Assembly Language
Form
N !! C I D V
------
BYS
OP No.
No.
CODE
Bytes
Cycles
Relative BVS
Oper
70
2 2*
* Add 1 if branch
* Add 2 if branch occurs occurs to same page.
to different page.
CLC
Operation: 0
~
C
Addressing
Mode
Implied
CLC Clear carry flag
(Ref: 3.0.2)
AssemblyLanguage
Form
CLC
eLe
N !! C I D V
--0---
OP No.
No.
CODE
BytE!s Cycles
18 1 2
BASIC TO MACHINE LANGUAGE 239
CLD
Operation:
IJ
+
D
Addressing
}fode
Implied
CLD
Clear decimal mode
(Ref: 3.3;2)
Assembly Language
Form
CLD
CLD
NtCIDV
(/1-
OP No.
No.
CODE
Bytes Cycles
D8
1 2
CLI
Operation:
IJ
+
I
CLI
Clear interrupt disable bit
(Ref: 3.2.2)
Addressing
Mode
Assembly Language
Form
Implied
CLI
CLI
N ~ C I D V
---(,!--
OP No.
No.
CODE
Bytes
Cycles.
58
1
2
CLV
Operation:
IJ
+
V
CLV Clear overflow flag
(Ref: 3.6.1)
Addressing
Mode
Assembly Language
Form
Implied
CLV
240 BASICTO MACHINE
LANGUAGE
CLV
NtCOIDV
(,!
OP No.
No.
CODE
Bytes Cycles
B8 1 2
(MP
Operation:A
-
CMP
Compare memory and accumulator
M N ~ C I D V
111---
(Ref: 4.2.1)
Addressing
Mode
Issembly Language
Form
CMP
OP No.
No.
CODE
Bytes Cycles
Immediate
Zero Page
Zero Page,
Absolute
X
Absolute,
Absolute,
(Indirect,
(Indirect),
X
Y
X)
Y
Immediate
Zero Page
Absolute
CMP #Oper
CMP Oper
CMP
Ope r, X
CMP Oper
CMP Oper, X
CMP
Oper, Y
CMP
(Oper, X)
CMP
(Oper), Y
C9
C5
D5
CD
DD
D9
Cl
Dl
2
2
2
3
2
2
3
3
4*
4*
6
5*
4
4
2
3
* Add 1 if page boundary
(PX
Operation: X M is crossed.
CPX
Compare Memory and Index
X
(Ref: 7.8)
NcCIDV
111---
Addressing
Mode
Assembly Language
Form
(PX
OP
CODE
No.
No.
Bytes Cycles
CPX #Oper
CPX Oper
CPX
Oper
E0
E4
EC
2
2
3
2
3
4
(py
Opera~ion: Y
-
M
Cpy
Compare memory and index Y
Addressing
Mode
(Ref: 7.9)
Assembly Language
Form
N i!iC
I D V
111---
Cpy
OP No.
No.
CODE
Bytes
Cycles
Immediate
Zero Page
Absolute
CPY # Oper
CPY
Oper
CPY
Oper
C0
C4
CC
2
2
3
2
3
4
BASICTO MACHINE LANGUAGE 241
DEC
Operation:
DEC
Decrement
M 1
+
M
memory by one
Addressing
Mode
(Ref: 10.7)
Assembly Language
Form
N~CIDV
//----
DEC
OP
CODE
No.
Bytes
No.
Cycles
Zero Page
Zero Page,
Absolute
X
Absolute, X
DEC
Oper
DEC
Oper, X
DEC
Oper
DEC
Oper, X
C6
D6
CE
DE
2
2
3
3
6
7
5
6
DEX
DEX
Decrement index X bv one
Operation:X
1
+
X
(Ref: 7.6)
Addressing
Mode
Assembly Language
Form
Implied
DEX
DEX
N ~ C I D V
//----
OP
No.
No.
CODE
Bytes
Cycles
CA 1 2
DEY
Operation: Y
-
1
+
DEY
Decrement index Y by one
Y
(Ref: 7.7)
Addressing
Mode
AssemblyLanguage
Form
N ~ C I D V
//----
DEY
OP No.
No.
CODE
Bytes
Cycles
Implied
DEY
88 1 2
242 BASIC TO MACHINE LANGUAGE
EOR
Operation:
EOR
"Exclusive-Or" memory with accumulator
A ¥ M
~
A
N e C I D V
11----
(Ref: 2.2.3.2)
Addressing
Mode
Assembly Language
Form
EOR
OP No.
No.
CODE
Bytes Cycles
Immediate
Zero Page
Zero Page,
Absolute
X
Absolute,
Absolute,
(Indirect,
(Indirect)
X
Y
X)
,Y
EOR # Oper
EOR
Oper
EOR
Oper, X
EOR
Oper
EOR
Oper, X
EOR
Oper, Y
EOR
(Oper, X)
EOR
(Oper) , Y
* Add 1 if page boundary is crossed.
INC
Operation:M + 1
~
INC Increment
memory by one
M
Addressing
Mode
(Ref: 10.6)
Assembly Language
Form
Zero Page
Zero Page,
Absolute
Absolute, X
X
INC Oper
INC Oper, X
INC Oper
INC Oper, X
49
45
55
4D
5D
59
41
51
2
3
2
2
3
3
2
2
INC
N e C I D V
11----
OP
No.
No.
CODE
Bytes
Cycles
E6
F6
EE
FE
2
2
3
3
5
6
6
7
4
4
2
3
4*
4*
6
5*
INX
INX Increment Index X by one
Operation: X + 1
~
X
Addressing
Mode
(Ref: 7.4)
Assembly Language
Form
Implied
INX
INX
N i5 C I D V
11----
OP No.
No.
CODE
Bytes
Cycles
E8 1
2
BASICTO
MACHINE
LANGUAGE
243
INY
INY
Increment Index
.yby one
Operation: Y + 1
~
Y
(Ref: 7.5)
Addressing
Mode
Assembly Language
Form'
INY
Impl ied
INY
N g C I D V
11----
OP No.
No.
CODE
Bytes
Cycles
C8 1
2
JMP
Op..ration: (PC + 1)
(PC + 2)
JMP
Jump to new location
PCL
PCH
(Ref:
(Ref:
4.0.2)
9.8.1)
Addressing
Mode
Assembly Language
Form
Absolute
Indirect
JMP
Oper
JMP
(Oper)
JMP
------
OP
No.
No.
CODE
Bytes Cycles
4C
6C
3
3
3
5
I
JSR
Operation:PC + 2 +, (PC + 1) ~ PCL
(PC+ 2) ~ PCH
(Ref: 8.1)
Addressing
Mode
AssemblyLanguage
Form
Absolute
JSR
Oper
JSR
N g C I D V
_ _ _ _ _ _
OP No.
CODE
Bytes
No.
Cycles
20 3 6
244 BASIC TO MACHINE LANGUAGE
LDA
'Operation:
Addressing
Mode
M ... A
LOA Load
accumulator with memory
(Ref: 2.1.1)
N;;CIDV
11----
Assembly Language
Form
LDA
OP No.
No.
CODE
Bytes
Cycles
Immediate
Zero Page
Zero Page,
Absolute
Absolute,
Absolute,
(Indirect,
X
X
Y
X)
(Indirect), Y
LDA # Oper
LDA
Oper
LDA
OpeT, X
LDA
Oper
LDA
.Oper, X
LDA
Oper, Y
LDA
(Oper, X)
LDA
(Oper), Y
A9
AS
BS
AD
BD
B9
Al
Bl
3
2
3
3
2
2
2
2
4*
6
5*
2
3
4
4
4*
* Add 1 'if page boundary is crossed.
LDX
Operation:
M'" X
OP
CODE
Bytes Cycles
A2
A6
B6
AE
BE
N i! C I D V
11----
No.
2
3
2
2
3
No.
2
3
4
4
4*
LDX
Addressing
Mode
.Immediate
.ZeroPage
.Zero
Page,
Absolute
Y
. Absolute, Y
(Ref: 7.0)
Assembly Language
Fo.rm
LDX
/I Oper
LDX
Oper
LDX Oper, Y
LDX
Oper
LDX
Oper, Y
* Add 1 when page boundary is crossed.
BASIC TO MACHINE LANGUAGE 245
LDY
Operation:
M + Y
LDY
Load index Y with memory
Addressing
Mode
(Ref: 7.1)
Assembly Language
Form
N!!CIDV
II ----
LDY
OP
CODE
No.
No.
Bytes Cycles
Immediate
Zero Page
Zero Page,
Absolute
Absolute, X
X
LDY # Oper
LDY
Oper
LDY
Oper, X
LDY
Oper
LDY
Oper, X
A0
A4
B4
AC
BC
2
3
2.
2
3
4
4
2
3
4*
* Add 1 when page boundary is crossed.
LSR
Operation:
Addressing
Mode
Assembly Language
Form
.LSR
LSR
Shift. right one bit (memory or accumulator)
0
-.+
~
-
C
N!!CIDV
011---
.(Ref: 10.1)
OP No.
No.
CODE
Bytes
Cycles
Ac=umulator
Zero Page
Zero Page,
Absolute
Absolute, X
X
LSR A
LSR
Oper
LSR
Oper, X
LSR
Oper
LSR
Oper, X
4A
46
56
4E
5E
2
3
3
1
2
2
6
7
5
6
NOP
Operation: No Operation
NOP No operation
(2 cycles)
Addressing
Mode
Implied
AssemblyLanguage
Form
NOP
NOP
N!!CIDV
------
OP No.
No.
CODE
Bytes
Cycles
EA 1 2
246 BASIC TO MACHINE LANGUAGE
ORA
operation: A V M
~
ORA
"OR" memory with accumulator
A
N;;CIDV
11----
(Ref: 2.2.3.1)
Addressing
Mode
Assembly Language
Form
ORA
OP
No.
CODE
Bytes
No.
Cycles
Iuunediate
Zero Page
Zero Page,
Absolute
Absolute,
Absolute,
(Indirect,
X
X
Y
X)
(Indirect), Y
ORA #oper
ORA
Oper
ORA
Oper, X
ORA
Oper
ORA ope'r,
ORA
Oper, Y
ORA
(Oper, X)
ORA
(Oper), Y
09
05
15
0D lD
19
01
11
3
3
2
2
2
3
2
2
2
3
4
4
6
5
4*
4*
* Add 1 on page crossing
PHA
Operation: A {
'PHA
Push-accumulator un stack
Addressing
Mode
Implied
(Ref: 8.5)
Assembly Language
Form
PHA
N ;; C I D V
------
PHA
OP No.
No.
CODE
Bytes
Cycles
48 1
3
PHP
operation: P{
Addressing
Mode
Implied
PHP
Push processor status on stack
PHP
Ni!CIDV
------
(Ref: 8.11)
AssemblyLanguage
Form
PHP
OP No.
No.
CODE
Bytes Cycles
08 1 3
BASIC TO MACHINE' LANGUAGE 247
PLA
Operation: A t
PLA Pull accumulator
from stack
Addressing
Mode
(Ref: 8.6)
Assembly Languag.!
Form
N iI C I D V
11----
PLA
OP
CODE
No.
Bytes
No.
Cycles
PLA
Implied
68 1 4
'LP
Operation: P t
PLP Pull processor status from stack
(Ref: 8.12)
Addressing
Mode
Assembly Language
Form
NilCIDV
From Stack
OP
CODE
No.
Bytes
PLP
No.
Cycles
Implied
PLP
28 1 4
ROL
Operation:
Addressing
Mode
ROL Rotate one hit left (memory
or accumulator)
Accumulator
Zero Pge
Zero Page, X
Absolute
Absolute, X
(Ref: 10.3)
Assembly Language
Form
ROL
A
ROL
Oper
ROL
Oper, X
ROL .Ope.r
ROL 'Oper,
X
ROL
N j1; C I D V
111---
OP
CODE
No.
Bytes
2A
26
36
2E
3E
1
2
2
3
3
No.
Cycles
2
5
6
6
7
.248
BASIC TO MACHINE LANGUAGE
ROR
ROR
Rotate one bit right (memory or accumulator)
ROR
Operation:
NiSCIDV
-/-/-/---
Addressing
Mode
Accumulator
Zero Page
Zero Page,X
Absolute
Absolute,X
Assembly Language
Form
RORA
ROR Oper
ROR Oper,X
ROR Oper
ROR Oper,X
OP
CODE
6A
66
76
6E
7E
No.
Bytes
2
3
3
1
2
Note: ROR instruction processors after is available
June, 1976.
on MCS650X micro-
No.
Cycles
6
7
2
5
6
RTI
Operation: pt PCt
RTf
Return from interrupt
Addressing
Morle
(Ref: 9.6)
Assembly Language
Form
Implied
RTI
RTI
N is C I D V
From Stack
OP No.
No.
CODE
Bytes Cycles
40 1
6
RTS
RTS
Return from subroutine
Operation:
PCt, PC + l~ PC
(Ref: 8.2)
Addressing
Mode
AssemblyLanguage
Form
Implied
RTS
N is C I D V
------
RTS
OP No.
No.
CODE
Bytes
Cycles
60
1
6
BASIC TO MACHINE LANGUAGE 249
S8C
Note:
SBC
Subtract memory {rom accumulator with borrow
Operation: A
-
M
-
C =
Borrow
C
~
A
(Ref: 2.2.2)
N ~ C I D V
.' .' .'
--
.'
Addressing
Mode
Assembly Language
Form
SBC
OP
No.
No.
CODE
Bytes
Cycles
Immediate
Zero Page
Zero Page,
Absolute
X
Absolute, X
Y Absolute,
(Indirect, X)
(Indirect), Y
SBC #Oper
SBC
Oper
SBC Oper, X
SBC Oper
SBC Oper, X
SBC Oper, Y
SBC
(Oper, X)
SBC
(Oper), Y
E9
E5
F5
ED
FD
F9
El
Fl
3
3
3
2
2
2
2
2
4
4
2
3
4*
4*
6
5*
* Add 1 when page boundary is crossed.
SEC
Operation: 1
~
C
Addressing
Mode
Implied
SEC Set carry flag
(Ref: 3.0.1)
Assembly Language
Form
SEC
SEC
N i5 C 1 D V
--1---
OP
CODE
No.
No.
Bytes Cycles
38 1
2
SED
Operation: 1
~
D
SED Set decimal mode
Address ing
Mode
(Ref: 3.3.1)
Assembly Language
Form
Implied SED
250
BASIC TO MACHINE LANGUAGE
SED
N i!C I D V
1-
OP No.
No.
CODE
Bytes Cycles
F8 1 2
SEI
Operation: 1
~
1
SEI Set
interrupt disable status
(Ref: 3.2.1)
Addressing
Mode
Assembly Language
FOTm
Implied
SEI
SEI
N i!iC
1 D V
---1--
OP
CODE
No.
Bytes
No_.
Cycles
78 1 2
SfA
Operation: A
~
M
STA Store accumulator in memory
(Ref: 2.l.2)
------
Addressing
Mode
Assembly Language
Form
SfA
OP
No.
No.
CODE
Bytes Cycles
Zero Page
Zero Page,
Absolute
X
Absolute,
Absolute,
(Indirect,
(Indirect),
X
Y
X)
Y
STA Oper
STA Oper, X
STA Oper
STA Oper, X
STA Oper, Y
STA
(Oper, X)
STA
(Oper), Y
85
95
8D
9D
99
81
91
3
3
2
2
2
2
3
5
5
6
6
3
4
4
STX
Operation: X
~
M
Addressing
Mode
Zero Page
Zero Page,
Absolute
Y
STX
Store index X in memory
(Ref:
7.2)
Assembly Language
Form
STX
Oper
STX Oper, Y
STX Oper
STX
N i!iC
I D V
------
OP No.
No.
CODE
Bytes Cycles
86
96
8E
2
2
3
3
4
4
BASIC TO MACHINE LANGUAGE 2S 1
STY
Operation:
Y
~
M
Addressing
Mode
Zero Page
Zero Page, X
Absolute
(Ref: 7.3)
Assembly Language
Form
STY
Oper
STY
Oper,
STY
Oper
X
STY
NaCIDV
------
OP
CODE
No.
Bytes
No.
Cycles
84
94
8C
2
2
3
3
4
4
TAX
Operation:
A
~
X
Addressing
Mode
Implied
(Ref: 7.11)
Assembly LanEuage
Form
TAX
N a C I D v
11----
TAX
OP
No.
CODE
Bytes
No.
Cycles
AA 1 2
TAY
Operation:
Addressing
Mode
A
~
Y
T A Y
Transfe." accumulator to index Y
N a C I D v
11----
(Ref:
7.13)
Assembly Language
Form
OP
CODE
No.
Bytes
TAY
No.
Cycles
TAY A8 1 2
Implied
252 BASIC TO MACHINE LANGUAGE
TSX
Operation: S
~
X
Addressing
Mode
Implied
(Ref: 8.9)
Assembly Language
Form
TSX
TSX
N ~ C I D V
11----
OP
No.
No.
CODE
Bytes Cycles
BA 1 2
TXA TXA
Operation: X
~
A
Transfer
(Ref: 7.l2)
Addressing
Mode
Assembly Language
Form
Implied
TXA
TXA
N~CIDV
11----
OP No.
No.
CODE
Bytes
Cycles
8A 1
2
TXS
Operation: X
~
S
Addressing
Mode
Implied
(Ref: 8.8)
Assembly Language
Form
TXS
TXS
N i!C I D V
-------
OP No.
No.
CODE
Bytes Cycles
9A
1 2
TYA
Operation: Y
~
ANi!
Addressing
Mode
Implied
(Ref: 7.14)
Assembly Language
Form
TYA
TVA
C I D V
11----
OP
No.
No.
CODE
Bytes Cycles
98 1
2
BASIC TO MACHINE LANGUAGE 253
INSTRUCTION ADDRESSING MODES AND x > x >
_
X
> f
...
:s c
ADC
AND
ASl
BCC
BCS
BEO
BIT
BMI
BNE
BPL
BRK
BVC
2
234
234
5 '6
3
4 4*4*
4 4* 4 *
6 7
4
2**.
2** .
2** .
6 5*
6 5*
2** .
2**.
2**.
2*'\.
2** .
BVS
ClC 2
ClV
CMP
CPX
234
2 3
2
ClD
Cli
4 4* 4 *.
.
6 5*
4
4
2
2
CPV
DEC
2 3
5 6 6 7
2
DEX
DEV 2
234 4 4* 4* .
6 5-
EOR
6 7
INC 5 6
2
INX
INY
JMP
3
2
. . . . . .
5
·
Add one cycle if indexing across page boundary
..
Add one cycle if branch is taken, Add one additional
254 BASIC
TO MACHINE LANGUAGE
RELATED EXECUTIONTIMES (in clock cycles)
-
..
a» a.
x>
-
..,..
= ._
.., .., ..,
E i a»a»&
A. A. A.
....
.. .. ..
'
= E
0 0 0 0 00=
"'''''''-'i1'''
_
N N N
"
.
GI x>-
-
E
= =
..
f
- f
0
..
ROR
RTI
RTS sse sEe
SED
SEI
STA
STX
STY
TAX
TAY
TSX
TXA
TXS
TYA
JSR
LDA
LDX
LDY
LSR
NOP
ORA
PHA
PHP
PLA
PLP
ROL
2
2
2
2 3 4
2 3
2
3
4
5 6
2 3 4
5 6
5 6
2 3
4
3 4
3
3 4
4
6
4 4* 4* .
4
4* .
4 4* .
6
7
4 4* 4* .
2
3
3
4
4
6
7
6 7
4
4* 4*
4
5 5
4
4
4
6
5* .
6 5* .
6
6
.
6
S*
.
2
2
2
6
6
2
2
2
2
2
2 if branching operation crosses page boundary
BASIC TO MACHINE LANGUAGE
255
00
-
BRK
01 - ORA-
(Indirect,X)
02
03
04
05
06
-
Future Expansion
-
Future
Expansion
-
Future Expansion
-
ORA
-
Zero Page
-
ASL
-
Zero .Page
07 - Future Expansion
08 - PHP
09 - ORA -Immediate
.0A - ASL - Accumulator
0B - Future Expansion
0c - Future Expansion
0D - ORA - Absolute
0E - ASL - Absolute
0F - Future Expansion
10 - .BPL
11 - ORA - (Indirect),Y
.12 - Future 'Expansion
13
.14
15
16
-
Future Expansion
-
Future Expansion
-
ORA
-
Zero Page,X
-
ASL
-
Zero Page;X
17
18
19
-
Future
Expansion
-
CLC
-
ORA
-
-Absolute, Y
1A
1B
1C
-
Future Expansion
-
Future Expansion
-
Future Expansion
1D -ORA - Abso1ute,X
1E - ASL - Abso1ute,X
IF
-
Future Expansion
256
BASICTO MACHINELANGUAGE
20
-
JSR
.21 -AND -
(Indirect,X)
22
23
24
25
-
Future Expansion
-
Future Expansion
-
BIT
-
Zero Page
-
AND
-
Zero Page
26 - ROL - Zero Page
27 - Future Expansion
28 - PLP
.29 - AND - Immediate
2A - ROL - Accumulator
2B - Future Expansion
2C - BIT - Absolute
2D - AND - Absolute
2E - ROL - Absolute
2F - Future Expansion
30 - 'BMl
31 - AND - (Indirect),Y
32 -
Future
Expansion
33
-
Future Expansion
34
-
Future Expansion
35
36
37
:38
-
AND
-
ROL
-
-
Zero Page,X
Zero Page,X
-
Future
Expansion
-
SEC
39
3A
3B
3C
-
AND
-
Abso1ute,Y
-
Future
Expansion
-
Future Expansion
-
Future Expansion
3D - AND - Abso1ute,X
3E - ROL - Absa1ute,X
3F
-
.Future Expansion
40
41
-
RTI
-
EOR
-
(Indirect,X)
42
-
Future Expansion
43
44
45
46
-
Future Expansion
-
Future Expansion
-
EOR
-
Zero Page
-
LSR
-
Zero
Page
47 -
Future Expansion
48
49
4A
4B
4C
4D
4E
-
PHA
-
EOR
-
LSR
-
-
Immediate
Accumulator
-
Future
Expansion
-
JHP
-
Absolute
-
EOR
-
Absolute
-
LSR
-
Absolute
4F
50
51
52
53
54
55
56
57
58
59
SA
5B
5C
5D
5E
SF
-
Future Expansion
-
BVC
-
EOR
-
(Indirect),Y
-
Future Expansion
-
Future Expansion
-
Future Expansion
-
EOR
-
Zero Page,X
-
LSR
-
Zero
Page,X
-
Future
Expansion
-
CLl
-
EOR
-
Absolute.Y
-
Future Expansion
-
Future Expansion
-
Future Expansion
-
EOR
-
Absolute,X
-
LSR
-
Absolute,X
-
Future Expansion
60
61
62
63
64
65
66
67
68
69
6A
6B
6C
6D
6E
6F
70
71
72
73
74
75
76
77
78
79
7A
7B
7C
7D
7E
7F
-
RTS
-
ADC
-
(Indirect,X)
-
Future Expansion
-
Future Expansion
-
Future Expansion
-
ADC
-
Zero
Page
-
ROR
-
Zero Page
-
Future Expansion
-
PLA
-
ADC
-
Immediate
-
ROR
-
Accumulator
-
Future Expansion
-
JHP
-
Indirect
-
ADC
-
Absolute
-
ROR
-
Absolute
-
Future Expansion
-
BVS
-
ADC
-
(Indirect),Y
-
Future Expansion
-
Future Expansion
-
Future Expansion
-
ADC
-
Zero Page,X
-
ROR
-
Zero Page. X
-
Future
Expansion
-
SEI
-
ADC
-
Abso1ute.Y
-
Future Expansion
-
Future Expansion
-
Future Expansion
-
ADC
-
Absolute,X
-
ROR
-
Absolute,X
-
Future Expansion
BASIC TO MACHINE LANGUAGE 257
80
Futur.e
Expansion
81 - STA (Indirect, X)
82 - Future Expansion
83 - Future Expansion
84 - STY - Zero Page
85 - STA - Zero Page
86 - STX - Zero Page
87 - Future Expansion
88 - DEY
89 - Future Expansion
8A - TXA
8B - Future Expansion
8C - STY - Absolute
8D - STA - Absolute
8E - STX - Absolute
8F - Future Expansion
90 - BCC
91 - STA (Indirect),Y
92 - Future Expansion
93 - Fuure Expansion
94 - STY - Zero Page, X
95 - STA - Zero Page, X
96 - STX - Zero Pge,Y
97 - Future Expansion
98 - TYA
99 - STA - Absolute,Y
9A - TXS
9B - Future Expansion
9C - Future Expansion
9D - STA - Absolute,X
9E - Future Expansion
9F - Future Expansion
258
BASICTO MACHINELANGUAGE
A0 - LDY - Immediate
A1 - LDA (Indirect,X)
A2 - LDX - Immediate
A3
- Future Expansion
A4 - LDY - Zero Page
AS - LDA - Zero Page
A6 - LDX - Zero Page
A7 - Future Expansion
A8 - TAY
A9 - LDA - Immediate
AA - TAX
AB - Future Expansion
AC - LDY - Absolute
AD - LDA - Absolute
AE - LDX - Absolute
AF - Future Expansion
B0 - BCS
B1 - LDA (Indirect),Y
B2 - Future Expansion
B3 - Future Expansion
B4 - LDY - Zero Page, X
B5 - LDA - Zero Page, X
B6 - LDX - Zero Page,Y
B7 - Future Expansion
B8 - CLV
B9 - LDA - Abso1ute,Y
BA - TSX
BB - Future Expansion
BC - LDY - Abso1ute,X
BD - LDA - Absolute,X
BE - LDX - Abso1ute,Y
BF - Future Expansion
c0 Cpy -
Iwmediate
Cl - CMP - (Indirect
,
X)
C2 - Future Expansion
C3 - Future Expansion
C4 - CPY - Zero Page
C5 - CMF - Zero Page
C6 - DEC - Zero Page e7 - Future Expansion
C8 - INY
C9 - CMP - Immediate
CA - DEX
CB - Future Expansion ec - CPY - Absolute
CD - CMP - Absolute
CE - DEC - Absolute
CF - Future Expansion
D0 - BNE
Dl - CMP (Indirect},Y
D2 - Future Expansion
D3 - Future Expansion
D4 - Future Expansion
D5 - eMP - Zero Page, X
D6 - DEC - Zero Page,X
D7 - Future Expansion
D8 - CLD
D9 - eMP - Absolute,Y
DA - Future Expansion
DB - Future Expansion
DC - Future Expansion
DD - CMF - Absolute,X
DE - DEC - Absolute,X
DF - Future Expansion
E CPX Immediate
El
-
SBe
(Indirect,X)
E2 - Future Expansion
E3 - Future Expansion
E4 - CPX - Zero Page
E5 - SBC - Zero Page
E6 - INC - Zero Page
E7 - Future Expansion
E8 - INX
Eg - SBC - Immediate
EA - NOP
EB - Future Expansion
EC - CPX - Absolute
ED - SBC - Absolute
EE - INC - Absolute
EF - Future Expansion
F0 - BEQ
Fl - SBC (Indirect),Y
F2 - Future Expansion
F3 - Future Expansion
F4 - Future Expansion
F5 - SBC - Zero Page,X
F6 - INC - Zero Page,X
F7 - Future Expansion
F8 - SED
F9 - SBC - Absolute,Y
FA - Future Expansion
FE - Future Expansion
FC - Future Expansion
FD - SBC - Absolute,X
FE - INC - Absolute,X
FF - Future Expansion
BASICTO MACHINELANGUAGE 259
MEMORY MANAGEMENT ON THE
COMMODORE 64
The Commodore 64 has 64K bytes of RAM. It also has 20K bytes of
ROM, containing BASIC, the operating system, and the standard character set. It also accesses input/output devices as a 4K chunk of memory. How is this all possible on a computer with a 16-bit address bus, that is normally only capable of addressing 64K?
The secret is in the 6510 processor chip itself. On the chip is an input/ output port. This port is used to control whether RAM or ROM or I/O will appear in certain portions of the system's memory. The port is also used to control the Datassette TM, so it is important to affect only the proper bits.
The 6510 input/output port appears at location 1. The data direction register for this port appears at location O. The port is controlled like any of the other input/output ports in the system
. . .
the data direction controls whether a given bit will be an input or an output, and the actual data transfer occurs through the port itself.
The lines in the 6510 control port are defined as follows:
NAME
LORAM
BIT DIRECTION
0 OUTPUT
HIRAM
CHAREN
1
2
3
4
5
OUTPUT
OUTPUT
OUTPUT
INPUT
OUTPUT
DESCRIPTION
Control for RAM/ROM at
$AOOO-$BFFF (BASIC)
Control for RAM/ROM at
$EOOO-$FFFF (KERNAl)
Control for I/O/ROM at
$DOOO-$DFFF
Cassette write line
Cassette switch sense
Cassette motor control
The proper value for the data direction register is as follows:
BITS 5 4 3 2 1 0
1 0 1 1 1 1
(where 1 is an output, and 0 is an input).
260 BASIC TO MACHINE LANGUAGE
This gives a value of 47 decimal.
The Commodore 64 automatically
sets the data direction
_
register to this value.
The control lines, in general, perform the function given in their descriptions. However, a combination of control lines are occasionally used to get a particular memory configuration.
LORAM
(bit 0) can generally be thought of as a control line which banks the 8K byte BASIC ROM in and out of-the microprocessor address space.
Normally, this line is HIGH for BASIC operation.
If this line is programmed LOW; the BASIC ROM will disappear from the memory map and b~ replaced by 8K bytes of RAM from $AOOO-$BfFF.
HIRAM (bit 1) can generally be thought of as a control line which banks the 8K byte KERNALROM in and out of the microprocessor address space.
Normally, this line is HIGH for BASIC operation.
If this line is programmed LOW, the KERNALROM will disappear from the memory map and be replaced- by 8K bytes of RAM from $EOOO-$FFFF.
CHAREN (bit 2) is used only to bank the 4K byte character generator
ROM in or out of the microprocessor address space.
From the processor point of view, the character ROM occupies the same address space as the I/O devices ($DOOO-$DFFF). When the CHAREN line is set to 1 (as is normal), the I/O devices appear in the microprocessor address space, and the character ROM is not accessable.
When the CHAREN bit is cleared to 0, the character ROM appears in the processor address space, and the I/O devices are not accessable.
(The microprocessor only needs to access the character ROM when downloading the character set from ROM to RAM. Special care is needed for this
. . .
see the section on PROGRAMMABLECHARACTERSin the GRAPHICS chapter).
CHAREN can be overridden configurations.
by other control lines in certain memory
CHAR EN will have no effect on any memory configuration without I/O devices.
RAM will appear from $DOOO-$DFFF instead.
NOTE: In any memory. map containing-ROM, a WRITE (a POKE) to a ROM location will stare data in the RAM "under" the ROM. Writing to a ROM location stores data in the
"hidden" RAM. For example.
this allows a hi-resolution screen to be kept underneath a ROM, and be changed without- having to bank the screen back into the processor address space.
Of course a READ of a ROM location will return the contents of the
ROM, not the "hidden"
RAM.
BASIC TO MACHINE LANGUAGE 261
COMMODORE 64 FUNDAMENTALMEMORY MAP
EOOO-FFFF
DOOO-DFFF
COOO-CFFF
AOOO-BFFF
8000~9FFF
8K KERNAL ROM
OR
RAM
4K I/O OR RAM OR
CHARACTER ROM
4K RAM
8K BASIC ROM
OR
RAM
OR
ROM PLUG.IN
8K RAM
OR
ROM PLUG.IN
4000-7FFF
16K RAM
0000-3FFF
16K RAM
I/O BREAKDOWN
262
DOOO-D3FF
D400-D7FF
D800-DBFF
DCOO-DCFF
DDOO-DDFF
DEOO-DEFF
DFOO-DFFF
VIC (Video Controller)
SID (Sound Synthesizer)
Color RAM
CIA1 (Keyboard)
CIA2 (Serial Bus, User Port/RS--232)
Open I/O slot #1 (CP/M Enable)
Open I/O slot #2 (Disk)
BASIC TO MACHINE LANGUAGE
1K Bytes
1K Bytes
1K Nybbles
256 Bytes
256 Bytes
256 Bytes
256 Bytes
The two open
I/O
slots are for general purpose user I/O, special purpose
I/O cartridges (such as IEEE), and have been tentatively.designated
for -enabling the Z-80 cartridge (CP/M option) and for interfacing to a low-cost high~speed disk. system.
.The
system provides for "auto-start" of the program in a Commodore
64 Expansion Cartridge.
The cartridge program is started if the first nine bytes of the cartridge ROM starting at location 32768 ($8000) contain specific data.
The first two bytes must hold the Cold Start vector to be used by the cartridge program.
The next two bytes at 32770 ($8002) must be the Warm Start vector used by the cartridge program.
The next three bytes must be the letters,
CBM; with bit 7 set in each letter.
The last two bytes must be the digits "80" in PET ASCII.
COMMODORE 64 MEMORY MAPS
The following tables list the various memory :configurations available on the COMMODORE 64, the states of the control lines which select each memory map, and .the
intended use of each map.
EOOO
DOOO
COOO
8KKERNAL ROM
4KI/O
4K RAM (BUFFER)
8K BASIC ROM x o
1
=
DON'T CARE
=
LOW
=
HIGH
LORAM
= 1
HIRAM
= 1
GAME = 1
EXROM = 1
AOOO
8K RAM
8000.
16K RAM
4000
16K RAM
This is Ihe default BASIC memory map whiCh provides BASIC 2.0 and
38K contiguous bytes of user RAM.
0000
BASIC TO MACHINE LANGUAGE.
263
EOOO
0000
COOO
8K RAM
4KI/O
4K RAM
16K RAM
8000
16K RAM
4000
16K RAM
0000 x = DON'T CARE o = LOW
1 = HIGH
LORAM
= 1
HIRAM
GAME
= 0
= 1
EXROM = X
OR
LORAM' = 1
HIRAM
GAME
= 0
= 0
(THE CHARACTER ROM
IS NOT ACCESSIBLE BY
THE CPU IN THIS MAP)
EX ROM.
= 0
This map provides 60K bytes of
RAM and I/O devices. The user must write his own I/O driver
routines.
EOOO
0000
COOO
8K KERNAL ROM
4KI/O
4K RAM
16K RAM
8000
16K RAM
4000
16K RAM
264
0000
BASIC TO MACHINE LANGUAGE x = DON'T CARE o = LOW
1 = HIGH
LORAM
HIRAM
GAME
EXROM o
1
1
X
This map is intended for use with soft load. languages (including
CP/M), providing 52K contiguous bytes of user
RAM, I/O devices, and I/O driver routines.
16K RAM
COOO I
16KRAM
8000
I
4000
0000
.
16KRAM
16KRAM x = DON'TCARE
O=lOW
1 = HIGH
I
I
LORAM
=
0
HIRAM = 0
GAME = 1
EXROM
= X
I
OR
LORAM = 0
HIRAM = 0
GAME
=X
EXROM = 0
.
I
This mapgives access to all 64K bytes of RAM.The 1/0devices must be bankedbackinto the processor'saddressspacefor any
I/O operation.
EOOO
0000
COoo
AOOO
8000
8K KERNAL ROM
4KI/O
4K RAM (BUFFER)
8K BASIC ROM
8K ROM CARTRIDGE
(BASIC EXP)
16K RAM
4000
16K RAM
0000 x = DON'T CARE
O=lOW
1 = HIGH
LORAM
HIRAM
= 1
= 1
GAME = 1
EXROM = 0
This is the standard configuration for a BASIC system with a BASIC expansion ROM. This map provides
32K contiguous bytes of user RAM and up to 8K bytes of BASIC
"enhancement. ..
BASIC TO MACHINE LANGUAGE 265
EOOO
0000
COOO
AOOO
8000
8K KERNAL ROM
4KII0
4K RAM (BUFFER)
8K ROM (CARTRIDGE)
8K RAM
16K RAM
4000
16K RAM
0000 x = DON'T CARE o = LOW
1 = HIGH
LORAM = 0
HIRAM = 1
GAME = 0
EXROM = 0
This map provides 40K contiguous bytes of user RAMand up to 8K bytes of plug.in ROM for special
ROM.based
applications which don't require
BASIC.
EOOO
0000
COOO
8K KERNAL ROM
4KIJO
4K RAM (BUFFER)
16K ROM (CARTRIDGE)
8000
16K RAM
4000
16K RAM
0000
266 BASIC TO MACHINE LANGUAGE x = DON'TCARE o = LOW
1 = HIGH
LORAM
HIRAM
GAME
EXROM
=
1
=
1
=
0
=
0
This map provides 32K contiguous bytes of user RAM and up to 16K bytes of plug.in ROM for special
ROM.based
applications which don't require BASIC (word processors, other languages, etc.).
EOOO
DOOO
COOO
AOOO
8000
8K CARTRIDGE ROM
4KI/O
4K OPEN
8K OPEN
8K CARTRIDGE ROM
16K OPEN
4000
1000
0000
12K OPEN
4K RAM x = DON'TCARE o = LOW
1 = HIGH
LORAM
HIRAM
= X
= X
GAME = 0
EXROM = 1
This is the ULTIMAX video game memory map. Note that the 2K byte "expansion RAM" for the
ULTIMAX, if required, is accessed out of the COMMODORE 64 and any
RAM in the cartridge is ignored.
BASIC TO MACHINE LANGUAGE 267
THE KERNAL
One of the problems facing programmers in the microcomputer field is the question of what to do when changes are made to the operating system of the computer by the company.
Machine language programs which took much time to develop might no longer work, forcing major revisions in the program.
To alleviate this problem, Commodore has developed a method of protecting software writers called the KERNAL.
Essentially, the KERNAL
JUMP TABLE
to the input,
output, and memory management routines in the operating system. The locations of each routine in ROM may change as the system is upgraded.
But the KERNALjump table will always be changed to match. If your machine language routines only use the system ROM routines through the KERNAL,it will take much less work to modify them, should that need ever arise.
The KERNALis the operating system of the Commodore 64 computer.
All input,
KERNAL.
output, and memory management is controlled by the
To simplify the machine language programs you write, and to make sure that future versions of the Commodore 64 operating system don't make your machine language programs obsolete, the KERNALcontains a jump table for you to use. By taking advantage of the 39 input/output routines and other utilities available to you from the table, not only do you save time, you also make it easier to translate your programs from one Commodore computer to another.
The jump table is located on the last page of memory, in read-only memory (ROM).
To use the KERNAl jump table, first you set up the parameters that the
KERNAl routine needs to work. Then JSR (Jump to SubRoutine) to the proper place in the KERNALjump table.
After performing its function, the KERNAl transfers control back to your machine language program.
Depending on which KERNALroutine you are using, certain registers may pass parameters back to your program.
The particular registers for each KERNAl routine may be found in the individual descriptions of the
KERNAL subroutines.
268 BASIC TO MACHINE LANGUAGE
A good question at this point is why use the jump table at all? Why not just JSR directly to the KERNAl subroutine involved? The jump table is used so that if the KERNAl or BASIC is changed, your machine language programs will still work. In future operating systems the routines may have their memory locations moved around to a different position in the memory map
. . .
but the jump table will still work correctly!
KERNAL POWER-UP ACTIVITIES
1) On power-up, the KERNAl first resets the stack pointer, and clears decimal mode.
2) The KERNAl then checks for the presence of an autostart ROM cartridge at location $8000 HEX (32768 decimal).
If this is present, normal initialization is suspended, and control is transferred to the cartridge code.
If an autostart ROM is not present, normal system initialization continues.
3) Next, the
KERNAL
initializes all INPUT/OUTPUT
is initialized.
Both 6526 -ciA chips are set to the proper values for keyboard scanning, _and the 60-Hz timer is activated.
The -SIDchip is cleared.
The BASIC memory map is selected and the cassette motor is switched off.
4) Next, the KERNAl performs -a RAM test , setting the top and bottom of memory pointers. Also, page zero is initialized, and the tape buffer is -set up.
The RAM TEST routine is a -nondestructive test starting at location
$0300 and working upward.
Once the test has found the first non-
RAM location, the top of RAM has its pointer set. The bottom of memory is always set to $0800, and the screen setup is always set at
$0400.
5) Finally, theKERNAL performs these other activities.
I/O vectors are set to default values. The indirect jump table in low memory is established. The screen is then cleared, and all screen editor variables reset. Then the indirect at $AOOOis used to start BASIC.
BASIC TO MACHINE LANGUAGE 269
HOW TO USE THE
KERNAL
When writing machine language programs it is often convenient to use the routines which are already part of the operating system for input/output, access to the system clock, memory management, and other similar operations.
It is an unnecessary duplication of effort to write these routines over and over again, so easy access to the operating system helps speed machine language programming.
As mentioned before, the KERNALis a jump table.
This is just a collection of JMP instructions to many operating system routines.
To use a KERNALroutine you musHirst make all ofthe preparations that the routine demands.
If one routine says that you must call another
KERNALroutine first, then that routine must be called.
If the routine expects you to put a number in the accumulator, then that number must be there. Otherwise your routines have little chance of working the way you expect them to work.
After all preparations are made, you must call the routine by means of the JSR instruction. All KERNALroutines you can access are structured as SUBROUTINES, and must end with an RTS instruction.
When the
KERNALroutine has finished its task, control is returned to your program at the instruction after the JSR.
Many of the KERNALroutines return error codes in the status word or the accumulator if you have problems in the routine. Good programming practice and the success of your machine language programs demand that you handle this properly.
If you ignore an error return, the rest of your program might "bomb."
That's all there is to do when you're using the KERNAL. Just these three simple steps:
1) Set up
2) Call the routine
3) Error handling
270 BASIC TO MACHINE LANGUAGE
The following conventions are used in describing the KERNAlroutines:
-FUNCTION
NAME: Name of the KERNAl routine.
-CALL ADDRESS:This is the call address of the KERNAl routine, given in hexadecimal.
-COMMUNICATIONREG1STERS: Registers listed under this heading are used to pass parameters to and from the KERNAl routines.
-PREPARATORY ROUTINES:Certain KERNAl routines require that data be set up -before they can operate.
The routines needed are listed here.
-ERROR RETURNS:A return from a KERNAl routine with the CARRYset indicates that an error was encountered in proce.ssing.
The accumulator will contain the number of the error.
-STACK REQUIREMENTS:This is the actual number of stack bytes used by the KERNAl routine.
-REGISTERS -AFFECTED:
used by the KERNAl routine are listed here.
-.-;DESCRIPTION:A short tutorial on the function ofthe KERNAl routine is given here.
The list of the KERNAl routines follows.
BASIC TO MACHINE lANGUAGE
271
NAME
ACPTR
CHKIN
CHKOUT
CHRIN
CHROUT
ClOUT
CINT
CLALL
CLOSE
CLRCHN
GETIN
10BASE
10lNIT
LISTEN
LOAD
MEMBOT
MEMTOP
OPEN
USER CALLABLE KERNAL ROUTINES
HEX
$F.FA5
ADDRESS
DECIMAL
65445
FUNCTION
$FFC6
.
$FFC9
$FFCF
65478
65481
65487
Input. byte from serial port.
Open channel for input
Open channel for output
Input character from channel
$FFD2
65490
$FFA8
$FF81
$FFE7
65448
65409
65511
Output character to channel
Output byte to serial port
Initialize screen' editor
Close all channels. and files
$FFC3
65475
Close a specified log ic'aI file
$FFCC
65484-
$FFE4
65508
Close input and. output.
channels
Get character from
$FFF3
65523 keyboard queue
(keyboard buffer)
Returns base address of
I/O devices
$FF84
$FFB1
6541 2.
65457
$FFD5
$FF9C
$FF99
$FFCO
65493
65436
65433
65472
Initialize input/output
Command devices on the serial bus to LISTEN
Load RAM from a device
Read/set the bottom of memory
Read/set the top of memory
Open a logical file
272 BASIC TO MACHINE LANGUAGE
RDTIM
READST
RESTOR
SAVE
SCNKEY
SCREEN
SECOND
SETLFS
SETMSG
SETNAM
SETTIM
SETTMO
STOP
TALK
TKSA
UDTIM
UNLSN
UNTLK
VECTOR
NAME
PLOT
RAMTAS
$FFDE
$FFB7
$FF8A
$FFD8
$FF9F
$FFED
$FF93
$FFBA
$FF90
$FFBD
$FFDB
$FFA2
$FFE1
$FFB4
$FF96
$FFEA
$FFAE
$FFAB
$FF8D
65466
65424
65469
65499
65442
65505
65460
65430
65502
65463
65418
65496
65439
65517
65427
65514
65454
65451
65421
HEX
ADDRESS
DECIMAL
$FFFO
65520
$FF87
65415
FUNCTION
Read/set X,Y cursor position
Initialize RAM, allocate tape buffer, set screen
$0400
Read real time clock
Read I/O status word
Restore default I/O vectors
Save RAM to device
Scan keyboard
Return X,Y organization of screen
Send secondary address after LISTEN
Set logical, first, and second addresses
Control KERNALmessages
Set file name
Set real time clock
Set timeout on serial bus
Scan stop key
Command serial bus device to TALK
Send secondary address after TALK
Increment real time clock
Command serial bus to
UNLISTEN
Command serial bus to
UNTALK
Read/set vectored I/O
BASIC TO MACHINE LANGUAGE
273
B-1. Function
Name:
ACPTR
Purpose: Get data from the serial bus
Call address: $FFA5 (hex) 65445 (decimal)
Communication registers: .A
Preparatory routines: TALK,TKSA
Error returns: See READST
Stack requirements: 13
Registers affected: .A, .X
Description: This is the routine to use when you want to get information from a device on the serial bus, like a disk. This routine gets a byte of data off the serial bus using full handshaking.
The data is returned in the accumulator.
To prepare for this routine the TALKroutine must be called first to command the device on the serial bus to send data through the bus. If the input device needs a secondary command, it must be sent by using the TKSA KERNAL
routine before calling this
routine.
Errors are returned in the status word. The READSTroutine is used to read the status word.
How to Use:
0) Command a device on the serial bus to prepare to send data to the Commodore 64. (Use the TALKand TKSA KERNALroutines.)
1) Call this routine (using JSR).
2) Store or otherwise use the data.
EXAMPLE:
;GET
A BYTE FROM THE BUS
JSR ACPTR
STA DATA
274 BASIC TO MACHINE LANGUAGE
B-2. Function Name:
CHKIN
Purpose: Open a channel for input
Call address:
$FFC6 (hex) 65478
(decimal)
Communication registers: .X
Preparatory
Error returns: routines: (OPEN)
Stack requirements:
Registers
None
affected: .A, .X
Description: Any logical file that has already been opened by the
KERNAL OPEN routine can be defined as an input channel by this routine. Naturally, the device on the channel must be an input device.
Otherwise an error will occur, and the routine will abort.
If you are getting data from anywhere other than the keyboard, this routine must be called before using either the CHRIN or the GETIN KER-
NAL routines for data input.
If you want to use the input from the keyboard, and no other input channels are opened, then the calls to this routine, and to the OPEN routine are not needed.
When this routine is used with a device on the serial bus, it automatically sends the talk address (and the secondary address if one was specified by the OPEN routine) over the bus.
How to Use:
0) OPEN the logical file (if necessary; see description above).
1) Load the .X register with number of the logical file to be used.
2) Call this routine (using a JSR command).
Possible errors are:
#3:
File not open
#5: Device not present
#6: File not an input file
EXAMPLE:
; PREPARE FOR INPUT FROM LOGICAL FILE 2
LDX #2
JSR CHKIN
BASIC TO MACHINE LANGUAGE 275
B-3. Function Name: CHKOUT
Purpose: Open a channel for output
Call address: $FFC9 (hex) 65481 (decimal)
Communication registers: .X
Preparatory routines: (OPEN)
Error returns: 0,3,5,7 (See READST)
Stack requirements: 4+
Registers affected: .A, .X
Description: Any logical file number that has been created by the
KERNALroutine OPEN can be defined as an output channel.
Of course, the device you intend opening a channel to must be an output device.
Otherwise an error will occur, and the routine will be aborted.
This routine must be called before any data is sent to any output device unless you want to use the Commodore 64 screen as your output device. If screen output is desired, and there are no other output channels already defined, then calls to this routine, and to the OPEN routine are not needed.
When used to open a channel to a device on the serial bus, this routine will automatically send the LISTENaddress specified by the OPEN routine (and a secondary address if there was one).
How to Use:
REMEMBER: this routine is NOT NEEDED to send data to the screen.
0) Use the KERNALOPEN routine to specify a logical file number, a
LISTENaddress, and a secondary address (if needed).
1) Load the .X register with the logical file number used in the open statement.
2) Call this routine (by using the JSR instruction).
EXAMPLE:
LDX #3
JSR CHKOUT
;DEFINE LOGICAL FILE 3 AS AN OUTPUT CHANNEL
276
Possible errors are:
#3: File not open
#5: Device not present
#7: Not an output file
BASIC TO MACHINE lANGUAGE
B-4. Function Name: CHRIN
Purpose: Get a character from the input channel
Ceill address: $FFCF (hex) 65487 (decimal)
Communication registers: .A
Preparatory routines: (OPEN, CHKIN)
Error returns: 0 (See READST)
Stack requirements: 7+
Registers affected: .A, .X
Description: This routine gets a byte of data from a channel already set up as the input channel by the KERNALroutine <;:HKIN.If the CHKIN has NOT been used to defJneanother input channel, then all your ci"ata is expected ,from the keyboard.
The data byte is returned in'the ac-
,cumulator.
The channel remains open after the call.
Input from the keyboard is handled in a special way.
First, the cursor is turned on, and blinks until a carriage return is typed on the keyboard.
All characters on .the line (up to 88 characters) are stored in the BASIC input buffer. These characters can be retrieved one at a time by calling this routine onc'e .for each character.
When the carriage return is retrieved, the entire ,line has been processed.
The next time this routine is called, the whole process begins again, Le., by flashing the cursor.
How to Use:
FROM THE KEYBOARD
1) Retrieve a byte of data by calling this routine.
2) Store the data byte.
3) Check if it 'is the last data byte (is it a CR ?).
4) If not, go to step 1.
EXAMPLE:
LDY$#00
RD JSR CHRIN
STA DATA, Y
INY
CMP #CR
BNE RD
;PREPARE THE .Y REGISTER TO STORE THE DATA
;STORE THE YTH DATA BYTE IN THE YTH
;LOCATION IN THE DATA ,AREA.
;IS IT A CARRIAGE 'RETURN?
;NO, GET ANOTHER DATA BYTE
BASIC TO MACHINE LANGUAGE 277
EXAMPLE:
-
JSR CHRIN
STA DATA
FROM
OTHER DEVICES
0) Use the KERNAl OPEN and CHKIN routines.
1) Call this routine (using a JSR instruction).
2) Store the data.
EXAMPLE:
JSR- CHRIN
STA DATA
B-5. Fundion Name:
CHROUT
Purpose: Output a character
Call address: $FFD2 (hex) 65490 (decimal)
Communication registers: .A
Preparatory routines: (CHKOUT,OPEN)
Error returns: 0 (See READST)
Stack requirements: 8+
Registers affected: .A
Description: This routine outputs a character to an already opened channel.
Use the KERNAl OPEN and CHKOUT routines to set up the output channel before calling this routine. If this call is omitted, data is sent to the default output device (number 3, the screen). The data byte to be output is loaded into the accumulator, and this routine is called.
The data is then sent to the specified output device. The channel is left open after the call.
NOTE: Care must be taken when using this routine to send data to a specific serial device since data will be sent to all open output channels -on the bus. Unless this is desired, all open output channels on the serial bus other than the intended destination channel must be closed by a call- to the
KERNAL
CLRCHN routine.
278 BASIC TO MACHINE LANGUAGE
How to Use:
0) Use the CHKOUT above).
KERNAl routine if needed
1) load the data to be output into the accumulator.
2) Call:this routine.
(see description
EXAMPLE:
;DUPLICATETHEBASIC INSTRUCTION CMD 4,IA"; lDX #4 ;lOGICAl FilE #4.
JSR CHKOUT
LDA #'A.
;OPEN CHANNEL OUT
JSR CHROUT _ ;SEND CHARACTER'
8-6. Function Name: ClOUT
Purpose: Transmit a byte over the serial bus
Call address:$FFA8 (hex) 65448 (decimal)
Communication registers: .A
Preparatory routines: LISTEN, [SECOND]
Error returns: See READST
Stack requirements: 5
Registers affected: None
Description: This routine is used- to send information to devices on the serial bus. A call to this routine will put a data byte onto the serial. bus using full serial handshaking.
Before this routine is called, the LISTEN
KERNAl routine must be used to command a device on the serial bus to get ready to receive data.
(If a device needs a secondary address, it must also be sent by using the SECOND KERNAl routine.) The accumulator is loaded with a byte to handshake as data on the serial bus.
A device must be listening or the status word will return a timeout.
This routine always buffers one character.
(The routine holds the previous character to be sent back.) So when a call to the KERNAl UNLSN routine is made to end the data transmission, the buffered character is sent with an End Or Identify (EOI) set. Then the UNlSN: command is sent to the device.
BASIC TO MACHINE LANGUAGE 279
How to Use:
.0) Use the LISTEN KERNAL
routine (and the' SECOND routine if
needed).
1) Load the accumulator with a byte of data.
2) Call this routine to send the data byte.
EXAMPLE: lDA #'X
JSR. ClOUT
;SEND AN X TO THE SERIALBUS
B-7.
Function Name: CINT
. Purpose: Initialize screen editor
& 6567 video chip
Call address: $FFS:l (hex)
65409
(decimal)
Communication registers: 'None
Preparatory routines: None
Error returns: None
Stack requirements: 4
Registers affected: .A, .X, .Y
Description: This routine sets up the 6567 video controller chip in the
Commodore 64 for normal operation.
The KERNALscreen
editor is also
initialized.
This routine should be called by a Commodore 64 program cartridge.
How to Use:
1) Call this routine.
EXAMPLE:
JSR CINT
JMP RUN
;BEGIN EXECUTION
280 BASIC TO MACHINE LANGUAGE
B-8. Function Name: CLALL
Purpose: Close all flies
Call address: $FFE7 (hex) 65511 (decimal)
Communication registers: None
Preparatory routines: None
Error returns: None
Stack requirements: 11
Registers affected: .A, .X
Description: This routine closes all open flies. When this routine is called, the pointers into the open flle table are reset, closing all flies.
Also, the CLRCHN routine is automatically called to reset the I/O channels.
How to Use:
1} Call this routine.
EXAMPLE:
JSR CLALL iCLOSE ALL FILES AND SELECT DEFAULT I/O CHANNELS
JMP RUN iBEGIN EXECUTION
8-9. Function Name: CLOSE
Purpose: Close a-logical flle
Call address: $FFC3 (hex) 65475 (decimal)
Communication registers: .A
Preparatory routines: None
Error returns: 0,240 (See READST)
Stack requirements: 2+
Registers affected: .A, .X, .Y
Description: This routine is used to close a logical flle after all I/O operations have been completed on that flle. This routine is called after the accumulator is loaded with the logical file number to be closed (the same number used when the flle was opened using the OPEN routine).
BASIC TO MACHINE LANGUAGE 281
How to Use:
1) Load the accumulator with the number of the logical file to be dosed.
2) Call this routine.
EXAMPLE:
;CLOSE 15
LDA #15
JSR CLOSE
B-10. Function Name:
CLRCHN
Purpose: Clear I/O channels
Call address: $FFCC (hex) 65484 (decimal)
Communication
Preparatory
Error returns: registers: None routines: None
Stack requirements: 9
Registers affected: .A, .X
Description: This routine is called to clear all open channels and restore the I/O channels to their original default values. It is usually called after opening other I/O channels (like a tape or disk drive) and using them for input/output operations.
The default input device is 0
(keyboard).
The default output device is 3 (the Commodore 64 screen).
If one of the channels to be closed is to the serial port, an UNTALK signal is sent first to clear the input channel or an UNLISTENis sent to clear the output channel.
By not calling this routine (and leaving listener(s) active on the serial bus) several devices can receive the same data from the Commodore 64 at the same time. One way to take ad-
vantage of this would be to command the printer to
TALK
and the disk to
LISTEN.This would allow direct printing of a disk file.
This routine is automatically called when the KERNALCLALLroutine is executed.
How to Use:
1) Call this routine using the JSR instruction.
EXAMPLE:
JSR CLRCHN
282 BASIC TO MACHINE LANGUAGE
B-II.
Function Name: GETIN
Purpose: Get a character
Call address:
$FFE4
(hex) 65508 (decimal)
Communication registers: .A
Preparatory routines: CHKIN, OPEN
Error returns: See READST
Stack requirements: 7+
Registers affected: .A (.X, .Y)
Description: If the channel is the keyboard, this subroutine removes one character from the keyboard queue and returns it as an ASCII value in the accumulator.
If the queue is empty, the value returned in the accumulator will be zero.
Characters are put into the queue automatically by an interrupt driven keyboard scan routine which calls the
SCNKEY routine. The keyboard buffer can hold up to ten characters.
After the buffer is filled, additional characters are ignored until at least one character has been removed from the queue.
If the channel is RS-
232, then only the .A register is used and a single character is returned.
See READST to check validity.
If the channel is serial, cassette; or screen, call BASIN routine.
How to Use:
1) Call this routine using a JSR instruction.
2) Check for a zero in the accumulator (empty buffer).
3) Process the data.
EXAMPLE:
;WAIT FOR A CHARACTER
WAIT JSR GETIN
CMP #0
BEQ WAIT
BASIC TO MACHINE LANGUAGE 283
B-12. Function Name: IOBASE
Purpose: Deflne I/O memory page
Call address: $FFF3 (hex) 65523 (decimal)
Communication registers: .X, .Y
Preparatory routines: None
Error returns:
Stack requirements: 2
Registers affected: .X, .Y
Description: This routine sets the X and Y registers to the address of the memory section where the memory mapped I/O devices are located.
This address can then be used with an offset to access the memory mapped I/O devices in the Commodore 64. The offset is the number of locations from the beginning of the page on which the I/O register you want is located.
The .X register contains the low order address byte, while the
.
Y register contains the high order address byte.
This routine exists to provide compatibility between the Commodore
64, VIC-20, and future models of the Commodore 64. If the I/O locations for a machine language program are set by a call to this routine, they should still remain compatible with future versions of the Commodore
64, the
KERNAL
and BASIC.
How to Use:
1) Call this routine by using the JSR instruction.
2) Store the .X and the .Y registers in consecutive locations.
3) Load the .Y register with the offset.
4) Access that I/O location.
EXAMPLE:
284
JSR 10BASE
STX POINT ;SET BASE REGISTERS
STY POINT+l
LDY #2
LDA #0 ;OFFSETFOR DDR OF THE USERPORT
STA (POINT), Y ;SET DDR TO 0
8ASIC
TO MACHINE LANGUAGE
8-13.
Function Name: lOlNIT
Purpose: Initialize r/o devices
Call Address: $FF84 (hex) 65412 (decimal)
Communication registers: None
Preparatory routines: None
Error returns:
Stack requirements: None
Registers affected: .A, ..X, .Y
Description: This routine initializes all input/output devices and routines. It is normally called as part of the initialization procedure of a
Commodore- 64 program cartridge.
EXAMPLE:
JSR 10lNIT
B-14. Function Name: LISTEN
Purpose: Command a device on the serial bus to listen
Call Address: $FFB-1 (hex) 65457 (decimai)
Communication registers: .A.
Preparatory routines: None
Error returns: See READST
Stack requirements: None
Registers affected: .A
Description: This routine will command a device on the serial bus to receive data.
The accumulator must be loaded with a device number between 0 and 31 before calling the routine. LISTENwill OR the number bit by bit to convert to a listen address, then transmits this data as a command on the serial bus. The specified device will then go' into listen mode, and be ready to accept information.
How to Use:
1)
Load the accumulator with the number of the device to command
to LISTEN.
2) Call this routine using the JSR instruction.
EXAMPLE:
;COMMAND DEVICE #8 TO LISTEN
LDA #8
JSR
LISTEN
BASIC TO MACHINE LANGUAGE 285
8-15. Fundion Name: LOAD
Purpose: load RAM from device
Call address: $FFD5 (hex) 65493 (decimal)
Communication registers: .A, .X,. Y
Preparatory routines: SETlFS, SETNAM
Error returns: 0,4,5,8,9, READST
Stack requirements: None
Registers affected: .A, .X, .Y
Description: This routine lOADs data bytes from any input device directly into the memory of the Commodore 64. It can also be used for a verify operation, comparing data from a device with the data already in memory, while leaving the data stored in RAM unchanged.
The accumulator (.A) must beset to 0 for a LOADoperation, or 1 for a verify. If the input device is OPENed with a secondary address (SA) of 0 the header information from the device is ignored.
In this case, the .X
and .Y.registers
must contain the starting address for the load.
If the device is addressed with .a secondary address of 1, then the data is loaded into memory starting at the location.specified
by the header.
This routine returns the address of the highest RAM location loaded.
Before this routine can be called, the KERNAl SETlFS, and SETNAM routines must be called.
:NOTE: You can NOT LOAD from the keyboard (0), RS-232 (2), or the screen (3).
How to Use:
.0) Call the SETlFS, and SETNAM routines.
If a relocated load is desired, use the SETlFS routine to send a secondary address of O.
1) Set the .A register to 0 for load, 1 for verify.
2) If a relocated load is desired, the .X and. Y registers must be set to the start address for the load.
3) Call the routine using the JSR instruction.
286 BASIC
TO
MACHINE LANGUAGE
EXAMPLE:
NAME
NAME 1
iLOAD
A FILE FROM TAPE
LDA
#DEVICEl
LDX #FILENO
LDY CMDl'
JSR SETLFS iSET DEVICE NUMBER iSET LOGICAL FILE, NUMBER, iSET SECONDARY ADDRESS
LDA #NAME1-NAME iLOAD .A WITH NUMBER OF iCHARACTERS IN FILE NAME
LDX
#<NAME
iLOAD..X
AND .Y WITH
iADDRESS OF iFILE NAME
LDY
JSR
LDA
LDX
LDY
JSR
STX
STY
JMP
.BYT
#>NAME
SETNAM
#0
#$FF
#$FF
LOAD
VARTAB
VA RTA B
+
1
START
'FILE NAME' iSET FLAG FOR A. LOAD iAlTERNATE START iEND OF LOAD'
B-16.
Function Name: MEMBOT
Purpose:
Set bottom of memory
Call address: $FF9C (hex) 65436 (decimal)
Communication registers: .X,.Y
Preparatory routines: None
Error returns: None
Stack requirements: None
Registers affected: .X, .Y
Description: This routine is used to set the bottom of the memory.
If the accumulator carry bit is set when this routine is called, a pointer to the lowest byte of RAM is returned in the .X and .Y registers.
On the unexpanded Commodore 64 the initial value of this pointer is $0800
(2048
in decimal).
If the accumulator carry bit is clear (=0) when this routine is called, the values of the .X and-.
Y registers are transferredto the low and high bytes, respectively, of the pointer to the beginning of
RAM.
BASIC TO MACHINE LANGUAGE 287
How to Use:
TO READ THE BOTTOM OF RAM
1) Set the carry.
-
2) Call this routine.
TO SET THE BOTTOM OF MEMORY
1) Clear the carry.
2) Call this routine.
EXAMPLE:
i MOVE BOTTOM OF MEMORY UP 1 PAGE
SECiREAD
JSR 'MEMBOT
MEMORY BOTTOM
INY
CLC
JSR ,MEMBOT iSETMEMORY BOTTOM TO NEW VALUE
8-17.
Function Name: MEMTOP
Purpose:
Set the top of RAM
Call address:
$FF99 (hex) 65433 (decimal)
Communication registers:
.X, .Y
Preparatory routines: None
Error returns: None
Stack requirements: 2
Registers affected: .X, .Y
Description: This routine is used to set the top of RAM. When this routine is called with the carry bit of the accumulator set, the pointer to the top of RAM.will be loaded into the .X and .Y registers.
When this routine is called with the accumulator carry bit clear, the contents of the
.X 'and .Y registers are loaded in the top .of :memory pointer, changing the top of memory.
EXAMPLE: iDEALLOCATE THE RS-232 BUFFER
SEC
JSR 'MEMTOP ;READ TOP OF MEMORY
DEX
CLC
JSR MEMTOP iSET NEW TOP OF'MEMORY
2BB BASIC TO MACHINE LANGUAGE
B-18.
Function Name: OPEN
Purpose: Open a logical file
Call address: $FFCO (hex) 65472 (decimal)
Communication registers: None
Preparatory routines: SETLFS,SETNAM
Error returns: 1,2,4,5,6,240, READST
Stack requirements: None
Registers affected: .A, .X, .Y
Description: This routine is used to OPEN a logical file. Once the logical file is set up, it can be used for input/output operations.
Most of the
I/O KERNALroutines call on this routine to create the logical files to operate on. No arguments need to be set up to use this routine, but both the SETLFSand SETNAM KERNALroutines must be called before using this routine.
How to Use:
0) Use the SETLFSroutine.
1) Use the SETNAM routine.
2) Call this routine.
EXAMPLE:
This is an implementation of the BASIC statement: OPEN 15,8,15,"1/0"
NAME
NAME2
LDA #NAME2-NAME
LDY#>NAME
LDX#<NAME
JSR SETNAM
LDA # 15
LDX#8
LDY#15
JSR SETLFS
JSR OPEN
.
BYT'I/O'
;LENGTH OF FILE NAME FOR SETLFS
;ADDRESS OF FILE NAME
BASIC TO MACHINELANGUAGE 2B9
8-19. Fundion Name: PLOT
Purpose: Set cursor location
Call address: $FFFO (hex) 65520 (decimal)
Communication registers: .A, .X, .Y
Preparatory routines: None
Error returns: None
Stack requirements: 2
Registers affected: .A, .X, .Y
Description: A call to this routine with the accumulator carry flag set loads the current position of the cursor on the screen (in X, Y coordinates) into the .Y and.X
registers. Y is the column number of the cursor location
(6-39), and X is the row number of the location of the cursor (0-24).
A call with the carry bit clear moves the cursor to X,Y as determined by the
.
Y and .X registers.
How to Use:
READING CURSOR LOCATION
1) Set the carry flag.
2) Call this routine.
3) Get the X and Y position from the .Y and .X registers, respectively.
SETTING CURSOR LOCATION
1) Clear carry flag.
2) Set the .Y and .X registers to the desired cursor location.
3) Call this routine.
EXAMPLE:
; MOVE THE CURSOR TO ROW 10, COLUMN 5 (5,10)
LDX#10
LDY#5
CLC
JSR PLOT
290 BASIC TO MACHINE LANGUAGE
8-20. Function Name: RAMTAS
Purpose: Perform RAM test
Call address: $FF87 (hex) 65415 (decimal)
Communication registers: .A, .X, .Y
Preparatory routines: None
Error returns: None
Stack requirements: 2
Registers affected: .A, .X, .Y
Description: This routine is used to test RAM and set the top and bottom of memory pointers accordingly.
It also clears locations $0000 to
$0101 and $0200 to $03FF. It also allocates the cassette buffer, and sets the screen base to $0400. Normally, this routine is called as part of the initialization process of a Commodore 64 program cartridge.
EXAMPLE:
JSR RAMTAS
B-21. Function Name: RDTIM
Purpose: Read system clock
Call address:
$FFDE
(hex) 65502 (decimal)
Communication registers: .A, .X, .Y
Preparatory routines: None
Error returns: None
Stack requirements: 2
Registers affected: .A, .X, .Y
Description: This routine is used to read the system clock. The clock's resolution is a 60th of a second. Three bytes are returned by the routine.
The accumulator contains the most significant byte, the X index register contains the next most significant byte, and the Y index register contains the least significant byte.
EXAMPLE:
JSR RDTIM
STYTIME
STX TIME+ 1
STA TIME+ 2
TIME *=*+3
BASIC TO MACHINE LANGUAGE 291
B-22. Function Name:
READST
Purpose: Read status word
Call address: $FFB7 (hex) 65463 (decimal)
Communication registers: .A
Preparatory routines: None
Error returns: None
Stack requirements: 2
Registers affected: .A
Description: This routine returns the current status of the I/O devices in the accumulator.
The routine is usually called after new communication to an I/O device. The routine gives you information about device status, or errors that have occurred during the I/O operation.
The bits returned in the accumulator contain the following information:
(see table below)
ST
BIT
POSITION
0
1
2
3
4
5
6
7
ST
TAPE
NUMERIC
CASSETTE SERIAURW VERIFY
VALUE READ
+ LOAD
1
2
Time out write
Time out read
4
8
16
32
64
-128
Short block long block
Unrecoverable read error
Checksum error
End of file
End of tape
Short block long block
Any mismatch
Checksum error
Earline
Device not
End of present tape
292 BASIC
TO MACHINE LANGUAGE
How to Use:
1) Call this routine.
2) Decode
the information in the .A register as it refer-s to your pro-
gram.
EXAMPLE:
;CHECK.
FOR END OF FILE DURING READ
JSR READST
AND #6,4
BNE EOF
;CHECK EOF BIT (EOF=END OF FILE)
;BRANCH ON EOF
8-23.
Function Name: RESTOR
Purpose: Restore default system and interrupt vectors
Call address: $FF8A (hex) 65418 (decimal)
Preparatory routines: None
Error returns: None
Stack requirements: 2
Registers affected: .A, .X, .Y
Description: This routine restores the default values of all system vectors used in KERNAL and BASIC routines and interrupts.
(See the Memory
Map for the default vector contents).
The KERNAL VECTOR routine is used to read and alter individualsystem vectors.
How to Use:
1) Call this routine.
EXAMPLE:
JSR RESTOR
8-24.
Function Name: SAVE
Purpose: Save memory to a device
Call address:
$FFD8 (hex) 65496
(decimal)
Communication
Preparatory registers: .A,.X,.Y
routines: SETLFS, SETNAM
Error returns: 5,8,9, READST
Stack requirements: None
Registers affected: .A, .X, .Y
BASIC TO MACHINELANGUAGE 293
Description: This routine saves a section of memory. Memory is saved from an indirect address on page 0 specified by the accumulator to the address stored in the .X and .Y registers.
It is then sent to a logical file on an input/output device.
The SETLFSand SETNAM routines must be used before calling this routine. However, a file name is not required to
SAVEto device 1 (the Datassette™ recorder).
Any attempt to save to other devices without using a file name results in an error.
"NOTE: Device 0 (the keyboard), device 2 (RS-232), and device 3 (the screen) cannot be SAVEd to. If the attempt is made, an error occurs, and the SAVE is stopped.
How to Use:
0) Use the SETLFSroutine and the SETNAMroutine (unless a SAVEwith no file name is desired on "a save to the tape recorder").
1) Load two consecutive locations on page 0 with a pointer to the start of your save (in standard 6502 low byte first, high byte next format).
2) Load the accumulator with the single byte page zero offset to the pointer.
3) Load the .X and .Y registers with the low byte and high byte respectively of the location of the end of the save.
4) Call this routine.
EXAMPLE:
LDA # 1
JSR SETLFS
LDA #0
JSR SETNAM
LDA PROG
STA TXTTAB
LDA PROG + 1
STA TXTTAB + 1
LDX VARTAB
LDY VARTAB+l
LDA #<TXTTAB
JSR SAVE
;DEVICE
;NO
=
1 :CASSETTE
FILE NAME
;LOAD START ADDRESS OF SAVE
; (LOW BYTE)
; (HIGH BYTE)
;LOAD .X WITH LOW BYTE OF END OF SAVE
;LOAD
.
Y WITH HIGH BYTE
;LOAD ACCUMULATOR WITH PAGE 0 OFFSET
294 BASIC
TO MACHINELANGUAGE
B-25.
Function Name: SCNKEY
Purpose: Scan the keyboard
Call address:$FF9F (hex) 65439 (decimal)
Communication registers: None
Preparatory routines: IOINIT
Error returns: None
Stack requirements: 5
Registers affected: .A, .X, .Y
Description: This routine scans the Commodore 64 keyboard and checks for pressed keys. It is the same routine called by the interrupt handler.
If a key is down, its ASCII value is placed in the keyboard queue. This routine is called only if the normal IRQ interrupt is bypassed.
How to
Use:
1) Call this routine.
EXAMPLE:
GET JSR SCNKEY
JSR GETIN
CMP #0
BEQ GET
JSR CHROUT iSCANKEYBOARD
iGET CHARACTER
ilS IT NULL?
iYES
. . .
SCAN AGAIN iPRINT IT
8-26.
Function Name: SCREEN
Purpose: Return screen format
Call address: $FFED
(hex) 65517
(decimal)
Communication registers: .X,. Y
Preparatory routines: None
Stack requirements: 2
Registers affected: .X, .Y
Description: This routine returns the format of the screen, e.g., 40 columns in .X and 25 lines in .Y. The routine can be used to determine what machine a program is running on. This function has been im-.
plemented on the Commodore 64 to help upward compatibility of your programs.
BASICTO MACHINELANGUAGE 295
How to Use:
1) Call this routine.
EXAMPLE:
JSR SCREEN
STX MAXCOL
STY MAXROW
8-27. Function Name: SECOND
Purpose: Send secondary address for LISTEN
Call address: $FF93 (hex) 65427 (decimal)
Communication registers: .A
Preparatory routines: LISTEN
Error returns: See READST
Stack requirements: 8
Registers affected: .A
Description: This routine is used to send a secondary address to an
I/O device after a call to the LISTEN routine is made, and the device is commanded to LISTEN. The routine canNOT be used to send a secondary address after a call to the TALK routine.
A secondary address is usually used to give setup information to a device before I/O operations begin.
When a secondary address is to be sent to a device on the serial bus, the address must first be ORed with $60.
How to Use:
1) Load. the accumulator with the secondary address to be sent.
2) Call this routine.
EXAMPLE:
;ADDRESS DEVICE #8 WITH COMMAND (SECONDARY ADDRESS) #15
LDA #8
JSR LISTEN
LDA #15
JSR SECOND
296.
BASIC
TO MACHINE LANGUAGE
8-28.
Function Name: SETLFS
Purpose: Set up a logical file
Call address:
$FFBA (hex) 65466 (decimal)
Communication registers: .A, .X, .Y
Preparatory routines: None
Error returns: None
Stack requirements: 2
Registers affected: None
Description: This routine sets the logical file number, device address, and secondary address (command number) for other KERNALroutines.
The logical file number is used by the system as a key to the file table created by the OPEN file routine. Device addresses can range from 0 to
31. The following codes are used by the Commodore 64 to stand for the
CBM devices listed below:
ADDRESS
o
1
2
3
4
8
DEVICE
Keyboard
Datassette™ #1
RS-232C device
CRTdisplay
Serial bus printer
CBM serial bus disk drive
Device numbers 4 or greater automatically refer to devices on the serial bus.
A command to the device is sent as a secondary address on the serial bus after the device number is sent during the serial attention handshaking sequence.
If no secondary address is to be sent, the. Y index register should be set to 255.
How to
Use:
1) Load the accumulator with the logical file number.
2) Load the .X index register with the device number.'
3) Load the .Y index register with the command.
BASIC TO MACHINE LANGUAGE 297
EXAMPLE:
FOR LOGICAL FILE32, DEVICE #4, AND NO COMMAND:
LDA #32
LDX #4
LDY #255
JSR SETLFS
8-29.
Function Name: SETMSG
Purpose: Control system message output
Call address:
$FF90 (hex) 65424
(decimal)
Communication registers: .A
Preparatory routines: None
Error returns: None
Stack requirements: 2
Registers affected: .A
Description: This routine controls the printing of error and control messages by the KERNAL.Either print error messages or print control messages can be selected by setting the accumulator when the routine is called.
FILE
NOT FOUND is an example of an error message.
PRESS
PLAYON CASSETTEis an example of a control message.
Bits 6 and 7 of this value determine where the message will come from. If bit 7 is 1, one of the error messages from the KERNALis printed.
If bit 6 is set, control messages are printed.
How to Use:
1) Set accumulator to desired value.
2) Call this routine.
EXAMPLE:
LDA #$40
JSR SETMSG
LDA #$80
JSR SETMSG
LDA #0
JSR SETMSG
298 BASIC TO MACHINE LANGUAGE jTURN ON CONTROL MESSAGES jTURN ON ERROR MESSAGES
;TURN OFF ALL KERNAL MESSAGES
B-30.
Function Name: SETNAM
Purpose: Set up file name
Call address: $FFBD (hex) 65469 (decimal)
Communication registers: .A, .X, .Y
Preparatory routines: None
Stack requirements: None
Registers affected: None
Description: This routine is used to set up the file name for tbe OPEN,
SAVE, or LOAD routines.
The accumulator must be loaded with the length of the file name. The .X and .Y registers must be loaded with the address of the file name, in standard 6502 low-byte/high-byte format.
The address can be any valid memory address in the system where a string of characters for the file name is stored. If no file name is desired, the accumulator must be set to 0, representing a zero file length. The .X
and .Y registers can be set to any memory address in that case.
How to Use:
1) Load the accumulator with the length of the file name.
2) Load the .X index register with the low order address of the file name.
3) Load the .Y index register with the high order address.
4) Call this routine.
EXAMPLE:
LDA #NAME2-NAME
LDX #<NAME
LDY#>NAME
JSR SETNAM
;LOAD LENGTH OF FILE NAME
;LOAD ADDRESS OF FILE NAME
B-31. Function Name: SETTIM
Purpose: Set the system clock
Call address: $FFDB (hex) 65499 (decimal)
Communication registers: .A, .X, .Y
Preparatory routines: None
Error returns: None
Stack requirements: 2
Registers affected: None
BASIC TO MACHINE LANGUAGE 299
Description: A system clock is maintained by an interrupt routine that updates the clock every 1/6Oth of a second (one "jiffy"). The clock is three bytes long, which gives it the capability to count up to 5,184,000 jiffies (24 hours). At that point the clock resets to zero. Before calling this routine to set the clock, the accumulator must contain the most significant byte, the .X index register the next most significant byte, and
the
.
Y index register the least significant byte of the initial time setting
(in jiffies).
How to Use:
1) load the accumulator with the MSB of the 3-byte number to set the clock.
2) load the .X register with the next byte.
3) load the .Y register with the lSB.
4) Call this routine.
EXAMPLE:
;SET THE CLOCK TO 10 MINUTES lDA #0
LDX #>3600
=
3600 JIFFIES
; MOST SIGNIFICANT
LDY #<3600
JSR SETTIM
; LEAST SIGNIFICANT
8-32. Function Name: SETTMO
Purpose: Set
IEEE
bus card timeout flag
Call address: $FFA2 (hex) 65442 (decimal)
Communication registers: .A
Preparatory routines: None
Error returns: None
Stack requirements: 2
Registers affected: None
NOTE: This routine is used ONLY with an IEEE add-on card!
Description: This routine sets the timeout flag for the IEEE bus. When the timeout flag is set, the Commodore 64 will wait for a device on the
IEEE
port for 64 milliseconds. If the device does not respond to the
Commodore 64's Data Address Valid (DAV)signal within that time the
Commodore 64 will recognize an error condition and leave the handshake sequence.
When this routine is called when the accumulator contains a 0 in bit 7, timeouts are enabled.
A 1 in bit 7 will disable the timeouts.
300 BASIC TO MACHINE LANGUAGE
NOTE: The Commodore 64 uses-the timeout feature to communicate that a disk:fileis not found- on an -attempt to OPEN a ,file only with an IEEE card.
How to Use:
TO SET THE TIMEOUTFLAG
I) Set :bit 7 of the accumulator to ,0.
2) Call this routine.
TO RESETTHE TIMEOUTFLAG
I) Set bit 7 of the accumulator tol.
2) Call this routine.
EXAMPLE:
;DLSABLETIMEOUT
LDA #0
JSR SETTMO
'8-33.
Function Name: STOP
Purpose: Check if
III key is pressed
Call address: $FFEI (hex) 65505 (decimal)
Communication registers: .A
Preparatory routines: None
Error returns: None
Stack requirements: None
Registers affected: .A, .X
Description: If .the
11I1I key on the keyboard was pressed during a UDTIM call, this call returns the Z flag set. In addition; the channels will be reset to default values.
All other flags remain unchanged.
If the'.
key is .not pressed then the accumulator will'contain a byte representing the.last row of the keyboard scan. The user can also check for certain other keys this way.
How to Use:
0) UDTIM should be called before this routine.
I) Call this routine.
2) Test for the z.ero flag.
BASICTO MACHINELANGUAGE 301
EXAMPLE:
JSR UDTIM ;SCAN FOR STOP
JSR STOP
BNE *+5 ;KEY
NOT DOWN
JMP READY ; =
. . .
STOP
B-34.
Function Name: TALK
Purpose: Command a device on the serial bus to TALK
Call address: $FFB4 (hex) 65460 (decimal)
Communication registers: .A
Preparatory routines: None
Error returns: See READST
Stack requirements: 8
Registers affected: .A
Description: To use this routine the accumulator must first be loaded with a device number between 0 and 31. When called, this routine then
ORs bit by bit to convert this device number to a talk address.
Then this data is transmitted as a command on the serial bus.
How to Use:
1) Load the accumulator with the. device number.
2) Call this routine.
EXAMPLE:
;COMMAND DEVICE #4 TO TALK
'LDA #4
JSR TALK
8-35.
Function Name: TKSA
Purpose: Send a secondary address to a device commanded to TALK
Call addr.ess: $FF96 (hex) 65430 (decimal)
Communication registers: .A
Preparatory routines: TALK
Error returns: See READST
Stack requirements: 8
Registers affected: .A
302 BASIC TO MACHINE LANGUAGE
Description: This routine transmits a secondary address on the serial bus for a TALKdevice. This routine must be called with a number between 0 and 31 in the accumulator.
The routine sends this number as a secondary address command over the serial bus. This routine can only be called after a call to the TALKroutine. It will not work after a LISTEN.
How to Use:
0) Use the TALK routine.
1) Load the accumulator
2) Call this routine.
'EXAMPLE: with the secondary address.
;TELLDEVICE #4 TO TALKWITH COMMAND #7
LDA #4
JSR TALK
LDA #7
JSR TALKSA
8-36. Function Name: UDTlM
Purpose: Update the system clock
Call address: $FFEA (hex) 65514 (decimal)
Communication registers: None
Preparatory routines: None
Error returns: None
Stack requirements: 2
Registers affected: .A, .X
Description: This routine updates the system clock.
Normally this routine is called by the normal KERNALinterrupt routine every 1/60th of a second.
If the user program processes its own interrupts this routine must be called to update the time. In addition, the
must be called, if the
11:I key routine
11:I key is to remain functional.
How to Use:
1) Call this routine.
EXAMPLE:
JSR UDTIM
BASIC TO MACHINE LANGUAGE
303
8-37.
Function Name: UNLSN
Purpose: Send an UNLISTENcommand
Call address: $FFAE (hex) 65454 (decimal)
Communication registers: None
Preparatory routines: None'
Error returns: See READST
Stack requirements: 8
Registers affected: .A
Description: This routine commands all devices on the' serial bus to stop receiving data from ,the Commodore 64 (Le., UNLISTEN).Calling
this routine results in an, UNLISTENcommand being transmitted on the serial bus. Only devices previously commanded to listen are affected.
This routine is ,normally used after the Commodore 64 is finished sending, data to external devices. Sending the UNLISTENcommands the listening, devices to get off the, serial bus so it can be used for other purposes.
How to Use:
1) Call this routine.
EXAMPLE:
JSR UNLSN
8-38. Function Name: UNTLK
Purpose: Send an UNTALKcommand'
Call address: $FFAB (hex) 65451 (decimal)
Communication registers: None
Preparatory routines: None
Error returns: See READST
Stack requirements: 8
Registers affected: .A
DesCI'iption: This routine transmits an UNTALKcommand on the serial bus. All devices previously set to TALKwill stop sending data when this command .is received.
How to' Use:
1) Call this routine.
EXAMPLE:
JSR UNTALK
304' BASIC TO. MACHINE LANGUAGE,
8-39.
Function Name: VECTOR
Purpose: Manage RAM vectors
Call address: $FF8D (hex) 65421 (decimal)
Communication registers: .X,.Y
Preparatory routines: None
Error returns: None
Stack requirements: 2
Registers affected: .A, .X, .Y
Description: This routine manages all system vector jump addresses stored in RAM. Calling this routine with the the accumulator carry bit set stores the current contents of the RAM vectors in a list pointed to by the
.X and .Y registers.
When this routine is called with the carry clar, the user list pointed to by the .X and. Y registers is transferred to the system
RAM vectors. The RAM vectors are listed in the memory map.
NOTE: This routine requires caution in its use. The best way to use it is to first read the entire vector contents into the user area, alter the desired vectors, and then copy the contents bock to the system vectors.
How to Use:
READ THE SYSTEM RAM VECTORS
1) Set the carry.
2) Set the .X and .y registers to the address to put the vectors.
3) Call this routine.
LOAD THE SYSTEMRAM VECTORS
1) Clear the carry bit.
2) Set the .X and .Y registers to the address of the vector list in RAM that must be loaded.
3) Call this routine.
BASIC TO MACHINE LANGUAGE 305
EXAMPLE:
;CHANGE THE INPUT ROUTINES TO NEW SYSTEM
LDX#<USER
LDY#>USER
SEC
;READ OLD VECTORS
;CHANGE INPUT
JSR VECTOR
LDA #<MYINP
STA USER+l0
LDA #>MYINP
STA USER+ 11
LDX#<USER
LDY#>USER
CLC
JSR VECTOR ;ALTERSYSTEM
USER * =* +26
ERROR CODES
The following is a list of error messages which can occur when using the KERNALroutines.
If an error occurs during a KERNALroutine, the carry bit of the accumulator is set, and the number of the error message is returned in the accumulator.
NOTE: Some KERNAll/O routines do not use these codes for error messages.
Instead, errors are identified using the KERNAl READST routine.
NUMBER
2
3
0
1
4
5
6
7
8
9
240
MEANING
Routine terminated by the"
Too many open files
File already open
File not open
File not found key
Device not present
File is not an input file
File is not an output file
File name is missing
Illegal device number
Top-of-memory change RS-232 buffer allocation/deallocation
306 BASIC TO MACHINE LANGUAGE
USING
MACHINE LANGUAGE FROM BASIC
There are several methods of using BASIC and machine language on the Commodore 64, including special statements as part of CBM BASIC as well as key locations in the machine.
There are five main ways to use machine language routines from BASIC on the Commodore 64. They are:
1) The BASIC SYS statement
2) The BASIC USR function
3) Changing one of the RAM I/O vectors
4) Changing one of the RAM interrupt vectors
5)
Changing the CHRGET routine
1) The BASIC statement SYS X causes a JUMP to a machine language subroutine located at address X. The routine must end with an RTS
(ReTurn from Subroutine) instruction.
This will transfer control back to BASIC.
Parameters are generally passed between the machine language routine and,the BASIC program using the BASIC PEEK and
POKE statements, and their machine language equivalents.
The SYS command is the most useful method of combining
BASIC with machine language.
PEEKs and POKEs make multiple parameter passing easy. There can be many SYS statements in a program, each to a different (or even the same) machine language routine.
2) The BASIC function
USR(X) transfers control to the machine language subroutine located at the address stored in locations 785 and 786.
(The address is stored in standard low-byte/high-byte fprmat.) The value X is evaluated and passed to the machine language subroutine through floating point accumulator #1, located beginning at address $61 (see memory map for more details).
A value may be returned back to the BASIC program by placing it in the floating point accumulator.
The machine language routine must end with an RTS instruction to return to BASIC.
This
statement is different from the SYS,because you have TOset up an indirect vector.
Also different is the format through which the variable is passed (floating point format).
The indirect vector must be changed if more than one machine language routine is used.
BASIC TO MACHINE LANGUAGE 307
3) Any of the input/output or BASIC internal routines accessed through the vector table located on page 3 (see ADDRESSING MODES,
ZERO PAGE) can be replaced, or amended by user code.
Each
2-byte vector consists of a low byte and a high byte address which is used by the operating system.
The KERNALVECTOR routine is the most reliable way to change any of the vectors, but a single vector can be changed by POKEs.
A new vector will point to a user prepared routine which is meant to replace or augment the standard system routine. When the appropriate BASIC command is executed, the user routine will be executed.
If after executing the user routine, it is necessary to execute the normal system routine, the user program must JMP (JuMP) to the address formerly contained in the vector. If not, the routine must end with a RTS to transfer control back to BASIC.
4) The HARDWAREINTERRUPT (IRQ) VECTOR can be changed.
Every
1/60th of a second, the operating system transfers control to the routine specified by this vector. The KERNALnormally uses this for timing, keyboard scanning, etc.
If this technique is used, you should always transfer control to the normal IRQ handling routine, unless the replacement routine is prepared to handle the CIA chip.
(REMEMBER
to end the routine with an RTI(ReTurn from Interrupt)
if the CIA is handled by the routine).
This method is useful for tasks which must happen concurrently with a BASIC program, but has the drawback of being more difficult.
NOTE: ALWAYS DISABLE INTERRUPTS BEFORE CHANGING THIS VECTORI
308
5) The
CHRGET routine is used by BASIC to get each character/token.
This makes it simple to add new BASIC commands.
Naturally, each new command must be executed by a user written machine language subroutine.
A common way to use this method is to specify a character (@ for example) which will occur before any of the new commands.
The new
CHRGET
routine will search for the
special character.
If none is present, control is passed to the normal BASIC CHRGET routine. If the special character is present, the new command is interpreted and executed by your machine language program.
This minimizes the extra execution time added by the need to search for additional commands.
This technique is often called a wedge.
BASIC TO MACHINE LANGUAGE
WHERE
TO -PUT MACHINE LANGUAGE
ROUTINES
The best place for machine langua.ge routines on the Commodore 64 is from $COOO-$CFFF, assuming the routines are smaller than 4K bytes long. This section of memory is not disturbed by BASIC.
If for some reason _it's not possible or desirable to put the machine language routine at $COOO,for instance if the routine is larger than 4K bytes, it then becomes _necessary to reserve an area at the top of memory from BASIC for the routine. The top of memory is normally$9FFF.
The top of memory can be changed through the KERNAL routine
MEMTOP, or by the following BASIC statements:
10 POKE51,L:POKE52,H:POKE55,L:POKE56,H:CLR
Where Hand L are the high and low portions, respectively, of the new top of memory. For -example, to reserve the area from $9000 to $9FFF for machine language, use the following:
10 POKE51,0:POKE52, 144:POKE55,0:POKE56, 144:CLR
HOW TO
ENTER MACHINE LANGUAGE
There are 3 common methods to add the machine language programs to a BASIC program.
They are:
1) DATA STATEMENTS:
By READing DATAstatements, and POKEing the values into memory at the start of the program, machine language routines can be added.
This is the easiest method.
No special methods are needed to save the two parts of the _program, and it is fairly easy to debug.
The drawbacks include taking up more memory space, and the wait while the program is POKEd in. Therefore, this method is better-for smaller routines.
EXAMPLE:
10 RESTORE: FORX =1 T09:READA: POKE12* 4096 + X,A: NEXT
BASIC PROGRAM
1000 DATA 161, 1,204,204,204,204,204,204,96
- BASIC TO MACHINE LANGUAGE 309
2) MACHINE LANGUAGE MONITOR (64MON):
This program allows you to enter a program in either HEX or SYM-
BOLIC codes, and save the portion of memory the program is in. Advantages of this method include easier entry of the machine language routines, debugging aids, and a much faster means of saving and loading. The drawback to this method is that it generally requires the BASIC program to load the machine language.
routine from tape or disk when it is started.
(For more details on 64MON see the machine' language section.)
EXAMPLE:
The following is an example of a BASIC program using a machine language routine. prepared by 64MON. The routine is stored on tape:
10lF FLAG=l THEN 20
15 FLAG=l:LOAD "MACHINE LANGUAGE ROUTINE NAME",l,l
20
REST OF BASIC PROGRAM
3) EDITOR/ASSEMBLER PACKAGE:
Advantages are similar to using a machine language monitor, but programs are even easier to enter. Disadvantages are also similar to the use of a machine language monitor.
COMMODORE 64 MEMORY MAP
LABEL
D6510
R6510
ADRAYl
HEX DECIMAL
ADDRESS LOCATION
0000 0
0001
0002
0003-0004
1
2
3-4
DESCRIPTION
6510 On-Chip Data-
Direction Register
6510 On-Chip 8-Bit
Input/Output Register
Unused
Jump Vector: Convert
Floating-Integer
310 BASIC TO MACHINE LANGUAGE
VALTYP
INTFlG
GARBFL
SUBFlG
INPFlG
TANSGN
LABEL
ADRAY2
CHARAC
ENDCHR
TRMPOS
VERCK
COUNT
DIMFlG lINNUM
TEMPPT
LASTPT
TEMPST
INDEX
RESHO
TXTTAB
HEX DECIMAL
ADDRESS LOCATION
0005-0006
5-6
0007
0008
0009
OOOA
OOOB
OOOC
OOOD
OOOE
OOOF
0010
0011
0012
0013
0014-0015
0016
0017 -0018
0019-0021
0022-0025
0026-002A
002B-002C
7
8
9
10
11
12
13
14
15
16
17
18
19
20- 21
22
23-24
25-33
34-37
38-42
43-44
DESCRIPTION
Jump Vector: Convert
Integer-Floating
Search Character
Flag: Scan for Quote at
End of String
Screen Column From Last
TAB
Flag: 0 ify
=
Load, 1
=
Ver-
Input Buffer Pointer / No.
of Subscripts
Flag: Default Array DI-
Mension
Data Type: $FF
= String,
$00 = Numeric
Data Type: $80
=
Integer,
$00 = Floating
Flag: DATAscan/liST quote/Garbage Coli
Flag: Subscript Ref / User
Function Call
Flag: $00
= INPUT, $40
= GET, $98 = READ
Flag: TAN sign / Comparison Result
Flag: INPUT Prompt
Temp: Integer Value
Pointer: Temporary String
Stack
Last Temp String Address
Stack for Temporary
Strings
Utility Pointer Area
Floating-Point Product of
Multiply
Pointer: Start of BASIC
Text
BASIC
TO MACHINELANGUAGE 311
CURLIN
OLDLIN
OLDTXT
DATLIN
DATPTR
INPPTR
VARNAM
VARPNT
FORPNT
LABEL
VARTAB
ARYTAB
STREND
FRETOP
FRESPC
MEMSIZ
FACEXP
FACHO
FACSGN
SGNFLG
HEX DECIMAL
ADDRESS LOCATION
002D-002E 45-46
002F-0030
0031...,.0032
0033-0034
0035-0036
0037 -0038
0039-003A
003B-003C
003D-003E
003F-0040
0041-0042
0043 -0044
0045-0046
0047 -0048
0049-004A
004B-0060
0061
0062-0065
0066
0067
47 -48
49-50
5.1-52
53-54
55-56
57-58
59-60
61-62
63-64
65-66
67-68
69-70
71-72
73-74
75-96
97
98-101
102
103
DESCRIPTION
Pointer: Start of BASIC
Variables
Pointer: Start of BASIC
Arrays
Pointer: End of BASIC Arrays
(+ 1)
Pointer: Bottom of String
.Storage
Utility String Pointer
Pointer: Highest Address
Used by BASIC
Current BASIC Line
Number
Previous BASIC Line
Number
Pointer: BASIC Statement for CO NT
Current DATA Line
Number
Pointer: Current DATA
Item Address
Vector: INPUT Routine
Current BASIC Variable
Name
Pointer: Current BASIC
Variable Data
Pointer: Index Variable for FOR/NEXT
Temp Pointer / Data Area
Floating-Point Accumulator # 1: Exponent
Floating Accum. #.1:
Mantissa
Floating Accum. #1: Sign
Pointer.: Series Evaluation
Constant
312 BASIC TO MACHINE LANGUAGE
LABEL
BITS
ARGEXP
ARGHO
C3PO
BSOUR
SYNO
LDTND
DFlTN
DFlTO
ARGSGN
ARISGN
FACOV
FBUFPT
CHRGET
CHRGOT
TXTPTR
RNDX
STATUS
STKEY
SVXT
VERCK
HEX DECIMAL
ADDRESS LOCATION
0068
104
0069
006A-006D
006E
006F
0070
0071-0072
0073-008A
0079
007A-007B
008B-008F
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
009A
105
106-109
110
111
112
113-114
115-138
121
122-123
139-143
144
145
146
147
148
149
150
151
152
153
154
DESCRIPTION
Floating Accum. # 1:
Overflow Digit
Floating-Point Accumulator #2: Exponent
Floating Accum. #2:
Mantissa
Floating Accum. #2: Sign
Sign Comparison Result:
Accum. #1 vs #2
Floating Accum. #1.
low-Order (Rounding)
Pointer: Cassette Buffer
Subroutine: Get Next Byte of BASIC Text
Entry to Get Same Byte of
Text Again
Pointer: Current Byte of
BASIC Text
Floating RND Function
Seed Value
Kernal I/O Status
Word: ST
Flag: STOP key / RVS key
Timing Constant for Tape
Flag: 0 ify
= load, 1 = Ver-
Flag: Serial Bus-Output
Char. Buffered
Buffered Character for
Serial Bus
Cassette Sync No.
Temp Data Area
No. of Open Files / Index to File Table
Default Input Device (0)
Default Output (CMD)
Device
(3)
BASICTO MACHINELANGUAGE 313
CNTDN
BUFPNT
INBIT
BITCI
RINONE
RIDATA
RIPRTY
SAL
EAL
CMPO
TAPEl
BITTS
NXTBIT
RODATA
FNLEN
LA
LABEL
PRTY
DPSW
MSGFLG
PTRl
PTR2
TIME
HEX DECIMAL
ADDRESS
LOCATION
009B
009C
009D
155
156
157
009E
009F
158
159
00AO-00A2 160-162
00A3-00A4 163-164
00A5 165
00A6
00A7
166
167
00A8
00A9
OOAA
OOAB
OOAC-OOAD
172-173
OOAE-OOAF
174-175
OOBO-OOB 1
00B2-00B3
176-177
178-179
00B4
00B5
00B6
00B7
00B8
168
169
170
171
180
181
182
183
184
DESCRIPTION
Tape Character Parity
Flag: Tape Byte-Received
Flag: $80
=
Direct Mode,
$00 = Program
Tape Pass 1 Error Log
Tape Pass 2 Error Log
Real-Time Jiffy Clock
(approx) 1/60 Sec
Temp Data Area
Cassette Sync Countdown
Pointer: Tape I/O Buffer
RS-232 Input Bits / Cassette Temp
RS-232 Input Bit Count /
Cassette Temp
RS-232 Flag: Check for
Start Bit
RS-232 Input Byte
Buffer/Cassette Temp
RS-232 Input Parity / Cassette Short Cnt
Pointer: Tape Buffer/
Screen Scrolling
Tape End Addresses/End of Program
Tape Timing Constants
Pointer: Start of Tape Buffer
RS-232 Out Bit Count /
Cassette Temp
RS-232 Next Bit to Send/
Tape EOT Flag
RS-232 Out Byte Buffer
.
Length of Current File
Name
Current Logical File
Number
314 BASIC TO MACHINE LANGUAGE
INDX
LXSP
SFDX
BLNSW
BLNCT
GDBLN
BLNON
CRSW
PNT
SA
LABEL
FA
FNADR
ROPRTY
FSBLK
MYCH
CASl
STAL
MEMUSS
LSTX
NDX
RVS
HEX
DECIMAL
ADDRESS LOCATION
00B9
185
OOBA 186
OOBB-OOBC 187 - 188
OOBD
OOBE
OOBF
OOCO
00CI-00C2
00C3-00C4
00C5
00C6
00C7
00C8
00C9-00CA
OOCB
OOCC
OOCD
OOCE
OOCF
OODO
00DI-00D2
189
190
191
192
193-194
195-196
197
198
199
200
201-202
203
204
205
206
207
208
209-210
-
DESCRIPTION
Current Secondary Address
Current Device Number
Pointer: Current File
Name
RS-232 Out Parity / Cassette Temp
Cassette Read/Write Block
Count
Serial Word Buffer
Tape Motor 'Interlock
I/O Start Address
Tape Load Temps
Current Key Pressed:
CHR${n) 0
=
No Key
No. of Chars. in
Keyboard Buffer
(Queue)
Flag: Print Reverse
Chars.-l =Yes, O=No
Used
Pointer: End of Logical
Line for INPUT
Cursor X-VPos. at Start of
INPUT
Flag: Print Shifted Chars.
Cursor Blink enable: 0
=
Flash Cursor
Timer: Countdown to
Toggle Cursor
Character Under Cursor
Flag: Last Cursor Blink
On/Off
Flag: INPUT or GET from
Keyboard
Pointer: Current Screen
Line Address
BASIC TO MACHINE LANGUAGE 315
BAD
BUF
LAT
FAT
SAT
KEYD
LABEL
PNTR
QTSW
LNMX
TBLX
INSRT lDTBl
USER
KEYTAB
RIBUF
ROBUF
FREKZP
BASZPT
316
HEX DECIMAL
ADDRESS LOCATION
00D3
211
00D4
00D5
00D6
00D7
00D8
00D9-00F2
00F3-00F4
00F5-00F6
00F7-00F8
215
216
00F9-00FA
249-250
OOFB-OOFE
OOFF
0100-0lFF
0100-010A
0100-013E
0200-0258
0259-0262
0263-026C
026D-0276
0277 -0280
212
213
214
217-242
243-244
245-246
247-248
251-254
255
256-511
256-266
256-318
512-600
601-610
611-620
621-630
631-640
DESCRIPTION
Cursor Column on Current
Line
Flag: Editor in Quote
Mode, $00 = NO
Physical Screen Line length
Current Cursor Physical
Line Number
Temp
Data Area
Flag: Insert Mode, >0 =
# INSTs
Screen Line Link Table /
Editor Temps
Pointer: Current Screen
Color RAM loc.
Vector: Keyboard Decode
Table
RS-232 Input Buffer
Pointer
RS-232 Output Buffer
Pointer
Free O-Page Space for
User Programs
BASIC Temp Data Area
Micro-Processor System
Stack Area
Floating to String Work
Area
Tape Input Error log
System INPUT Buffer
KERNAl Table: Active logical File No's.
KERNAl Table: Device No.
for Each File
KERNAl Table: Second
Address Each File
Keyboard Buffer Queue
(FIFO)
BASIC TO MACHINE LANGUAGE
LABEL
MEMSTR
MEMSIZ
TIMOUT
COLOR
GDCOl
HIBASE
XMAX
RPTFLG
KOUNT
DELAY
SHFLAG
LSTSHF
KEYLOG
MODE
AUTODN
M51CTR
M51CDR
M51AJB
BITNUM
HEX
DECIMAL
ADDRESS
LOCATION.
0281-0282
.
641-642
0283-0284
0285
0286
0287
0288
0289
028A
028B'
028C
028D
028E
028F-0290
0291
0292
0293
0294
0295-0296
0297
0298
643-644
645
646
647
648
649
650
651
652
653
654
655-656
657
658
659
660
661-662
663
664
DESCRIPTION
Pointer: Bottom of Memory for' O.S.
Pointer: Top of Memory for
O.S.
Flag: Kernal Variable for
IEEE Timeout
Current Character Color
Code
Background Color Under
Cursor
Top of Screen Memory
(Page)
Size of Keyboard Buffer
Flag: REPEATKey. Used,
$80 = Repeat
Repeat Speed CO.unter
Repeat. Delay Counter
Flag: Keyb'rd SHIFT Key/
CTRL Key/C
=
Key
Last Keyboard Shift Pattern
Vector: Keyboard Table
Setup
Flag: $OO=Disable SHIFT
Keys, $80
=
Enable
SHIFT Keys
Flag: Auto Scroll Down, 0
= ON.
RS-232: 6551' Control
Register Image
RS-232: 6551. Command
Register Image
RS-232 Non-Standard BPS
(Time/2-100) USA
RS-232: 6551 Status Register Image
RS-232 Number of Bits
Left to Send
BASICTO MACHINELANGUAGE
317
LABEL
BAUDOF
RIDBE
RIDBS
RODBS
RODBE
IRQTMP
ENABL
IERROR
IMAIN
ICRNCH
IQPLOP
IGONE
IEVAL
SAREG
SXREG
318
HEX
'DECIMAL
ADDRESS
LOCATJON
0299-029A 665-666
029B
029C
029D
029E
029F-02AO
02A1
02A2
02A3
02A4
02A5
02A6
02A7-02FF
0300-0301
0302-0303
0304-0305
0306-0307
0308-0309
030A-030B
030C
030D
667
668
669
670
671-672
673
674
675
.676
677
678
679-767
768-769
770-771
772-773
774-775
776-777
778-779
780
781
DESCRIPTION
RS-232 Baud Rate: Full Bit
Time (s)
RS-232 Index to End of
Input Buffer
RS-232 Start of Input Buffer (Page)
RS-232 Start of Output
Buffer (Page)
RS-232 Index to End of
Output Buffer
Holds IRQ Vector During
Tape I/O
RS-232 Enables
TOD Sense During Cassette I/O
Temp Storage For Cassette
Read
Temp D11RQ Indicator For
Cassette Read
Temp For Line Index
PALINTSCFlag, 0=
NTSC, 1= PAL
Unused
Vector: Print BASIC Error
Message
Vector: BASIC Warm Start
Vector: Tokenize BASIC
Text
Vector: BASIC Text LIST
Vector: BASIC Char. Dispatch
Vector: BASIC Token
Evaluation
Storage for 6502 .A Register
Storage for 6502 .X Register
I
8ASIC TO MACHINE LANGUAGE
CINV
CBINV
NMINV
IOPEN
IClOSE
ICHKIN
ICKOUT
IClRCH
IBASIN
IBSOUT
ISTOP
IGETIN
ICLAll
USRCMD
IlOAD
LABEL
SYREG
SPREG
USRPOK
USRADD
030F
0310
0313
HEX
ADDRESS
030E
0311-0312
0314-0315
0316-0317
0318-0319
031A-031 B
031 C-031 D
031 E-031 F
0320-0321
0322-0323
0324-0325
0326-0327
0328-0329
032A-032B
032C-032D
032E-032F
0330-0331
.
DECIMAL
LOCATION
782
783
784
785-786
787
788-789
790-791
792 -793
794-795
796-797
798-799
800-801
802 - 803
804-805
806-807
808-809
810-811
812-813
814-815
816-817
DESCRIPTION
Storage for 6502 .Y Register
Storage for 6502 .SP
Register
USR Function Jump Instr
(4C)
USR Address Low Byte/
High Byte
Unused
Vector: Hardware IRQ
Interrupt
Vector: BRKInstr. Interrupt
Vector: Non-Maskable
Interrupt
KERNALOPEN Routine
Vector
KERNAl CLOSE Routine
Vector
KERNAl CHKIN Routine
Vector
KERNAl CHKOUT Routine
Vector
KERNAl ClRCHN Routine
Vector
KERNAl CHRIN Routine
Vector
KERNAl CHROUT Routine
Vector
KERNAl STOP Routine
Vector
KERNAl GETIN Routine
Vector
KERNAl CLAll Routine
Vector
User-Defined Vector
KERNAl lOAD Routine
Vector
,
BASIC TO MACHINE LANGUAGE 319
LABEL
ISAVE
TBUFFR
VICSCN
HEX
DECIMAL
ADDRESS LOCATION
DESCRIPTION
.
0332-0333
0334-033B
820-827
033C-03FB
828-1019
03FC-03FF
0400-07FF
0400-07E7
07F8-07FF
0800-9FFF
8000-9FFF
AOOO- BFFF
818-819
1020- 1023
1024- 2047
1024-2023
2040-2047
2048-40959
32768:....40959
40960-49151
COOO-CFFF 49152-53247
DOOO-DFFF
EOOO-
FFFF
53248-57343
57344-65535
KERNALSAVE Routine Vector
Unused
Tape I/O Buffer
Unused
1024 Byte Screen Memory
Area
Video Matrix: 25 Lines X
40 Columns
Sprite Data Pointers
.Normal BASIC Program
Space
VSP Cartridge ROM-
8192 Bytes
BASIC ROM-8192 Bytes
(or 8K RAM)
RAM-4096 Bytes
Input/Output Devices and
Color RAM or Character Generator
ROM or RAM-4096 Bytes
KERNALROM-8192
Bytes (or 8K RAM)
COMMODORE 64 INPUT/OUTPUT ASSIGNMENTS
HEX
0000 0
DECIMAL
7-0
BITS
0001
1
0
DESCRIPTION
MOS 6510 Data Direction
Register (xx101111)
Bit= 1: Output, Bit=O:
Input, x=Don't Care
MOS 6510 Micro-Processor
On-Chip I/O Port
/LORAM Signal (O=Switch
BASIC ROM Out)
320 BASIC TO MACHINE LANGUAGE
HEX
D009
DOOA
DOOB
DOOC
DOOD
DOOE
DOOF
DOlO
DOOO
DOOI
D002
DOO3
D004
D005
D006
D007
D008
DOn
DECIMAL
53248
53249
53250
53251
53252
53253
53254
53255
53256
53257
53258
53259
53260
53261
53262
53263
53264
53265
7
6
1
2
3
4
DOOO-D02E 53248-54271
5
6-7
BITS
-
DESCRIPTION
/HIRAM Signal (O=Switch
Kernal ROM Out)
/CHAREN Signal
(O=Switch Char. ROM
In)
Cassette Data Output
Line
Cassette Switch Sense
1
=
Switch Closed
Cassette Motor Control
0= ON,
1 = OFF
Undefined
MOS 6566 VIDEO INTER-
FACE CONTROLLER
(VIC)
Sprite 0 X Pos
Sprite 0 Y Pos
Sprite 1 X Pos
Sprite 1 Y Pos
Sprite 2 X Pos
Sprite 2 Y Pos
Sprite 3 X Pos
Sprite 3 Y Pos
Sprite 4 X Pos
Sprite 4 Y Pos
Sprite 5 X Pos
Sprite 5 Y Pos
Sprite 6 X Pos
Sprite 6 Y Pos
Sprite 7 X Pos
Sprite 7 Y Pos
Sprites 0-7 X Pos (msb of
X coord.)
VIC Control Register
Raster Compare: (Bit 8)
See 53266
Extended Color Text
Mode: 1
=
Enable
BASIC TO MACHINE LANGUAGE 321
HEX
D012
D013
D014
D015
D016
D017
D018
D019
53266
53267
53268
53269
53270
DECIMAL
5
4
3
2-0
BITS
7-6
5
4
53271
53272
53273
3
2-0
7-4
3-1
7
3
322 BASICTO MACHINELANGUAGE
DESCRIPTION
Bit-Map Mode: 1 able
=
En-
Blank Screen to Border
Color: 0
=
Blank
Select 24/25 Row Text
Display: 1
=
25 Rows
Smooth Scroll to Y Dot-
Position (0-7)
Read Raster / Write Raster
Value for Compare IRQ
Light-Pen latch X Pos
Light-Pen latch Y Pos
1
Sprite Display Enable:
=
Enable
VIC Control Register
Unused
ALWAYSSET THIS BIT TO
01
Multi-Color Mode: 1
=
Enable (Text or Bit-
Map)
Select 38/40 Column Text
Display: 1
=
40 CoIs
Smooth Scroll to X Pos
Sprites 0-7 Expand 2X
Vertical (Y)
VIC Memory Control Register
Video Matrix Base Address (inside VIe)
Character Dot-Data Base
Address (inside VIe)
VIC Interrupt Flag Regis-
ter (Bit
=
1: IRQ Occurred)
Set on Any Enabled VIC
IRQ Condition
Light-Pen Triggered IRQ
Flag
HEX
D01A
D01B
D01C
D01D
DOlE
D01F
D020
D021
D022
D023
D024
D025
D026
DECIMAL
53274
53275
53276
53277
53278
53279
53280
53281
53282
53283
53284
53285
53286
D027
D028
D029
53287
53288
D02A
D02B
53289
53290
53291
53292 D02C
D02D
53293
D02E
53294
D400-D7FF
54272-55295
2
. 0
1
BITS
DESCRIPTION
Sprite to Sprite Collision
IRQ Flag
Sprite to Background
Collision IRQ Flag
Raster Compare IRQ Flag
IRQ Mask Register: 1
=
Interrupt Enabled
Sprite to Background
Display Priority: 1
Sprite
=
Sprites 0-7 Multi-Color
Mode Select: 1
=
M.C.M.
Sprites 0-7 Expand 2X
Horizontal
(X)
Sprite to Sprite Collision
Detect
Sprite to Background
Collision Detect
Border Color
Background Color 0
Background Cplor 1
Background Color 2
Background Color 3
Sprite Multi-Color Register 0
Sprite Multi-Color Register 1
Sprite 0 Color
Sprite 1 Color
Sprite 2 Color
Sprite 3 Color
Sprite 4 Color
Sprite 5 Color
Sprite 6 Color
Sprite 7 Color
MOS 6581 SOUND
INTERFACEDEVICE
(SID)
BASIC TO MACHINE LANGUAGE 323
HEX
D400
D401
D402
D403
D404
D405
D406
DECIMAL
54272
54273
54274
54275 7-4
3-0
BITS
54276
4
3
2
1
7
6
5
54277
54278
0
7-4
3-0
DESCRIPTION
Voice 1: Frequency
Control
- Low-Byte
Voice 1: Frequency
Control-High-Byte
Voice 1: Pulse Waveform
Width -low-Byte
Unused
Voice 1: Pulse Waveform
Width-High-Nybble
Voice 1: Control Register
Select Random Noise
Waveform, 1 = On
Select Pulse Waveform,
1 = On
Select Sawtooth
Waveform, 1
=
On
Select Triangle Waveform,
1 = On
Test Bit: 1
=
Disable OscilIator 1
Ring Modulate Osc. 1 with
Osc. 3 Output, 1 = On
Synchronize Osc. 1 with
Osc. 3 Frequency, 1 =
On
Gate Bit: 1 = Start Aftl
DeclSus, 0 = Start Release
Envelope Generator 1: Attack
I
Decay Cycle
Control
Select Attack Cycle Duration: 0-15
Select Decay Cycle Duration:
0- 15
Envelope Generator 1:
Sustain
Control
I
Release Cycle
324 BASIC TO MACHINE LANGUAGE
D407
D408
D409
D40A
D40B
HEX
D40C
DECIMAL
7-4
3-0
BITS
54279
54280
54281
54282 7-4
3-0
54283
3
2
1
7
6
5
4
54284
0
7-4
DESCRIPTION
Select Sustain Cycle Duration: 0-15
Select Release Cycle Duration: 0- 15
Voice 2: Frequency
ControlLow-Byte
Voice 2: Frequency
Control-High-Byte
Voice 2: Pulse Waveform
Width-low-Byte
Unused
Voice 2: Pulse Waveform
Width-High-Nybble
Voice 2: Control Register
Select Random Noise
Waveform, 1 = On
Select Pulse Waveform,
1 = On
Select Sawtooth
Waveform, 1 = On
Select Triangle
Waveform, '1 = On
Test Bit: 1 = Disable Oscillator 2
Ring Modulate Osc. 2 with
Osc. 1 Output, 1 = On
Synchronize Osc. 2 with
Osc. 1 Frequency, 1 =
On
Gate Bit: 1 = Start Attl
Dec/Sus, 0 = Start Release
Envelope Generator 2: Attack I Decay Cycle
Control
Select Attack Cycle Duration: 0- 15
BASICTO MACHINELANGUAGE 325
D40E
D40F
D410
D411
D412
HEX
D40D
DECIMAL
3-0
BITS
54285
54286
54287.
54288.
54289
54290
5
4
3
7
6
2
1
7-4
3-0
7-4
3-0
0
DESCRIPTION
Select Decay Cycle Duration: 0-15
Envelope Generator 2:
Sustain / Release Cycle
Control
Select Sustain Cycle Duration: 0- 15
Select Release Cycle Duration: 0- 15
Voice 3: Frequency
Control-low-Byte
Voice 3: Frequency
Control-High-Byte.
Voice 3: Pulse Waveform
Width
Unused
- low-Byte
Voice 3: Pulse Waveform
Width
-
High-Nybble
Voice 3: Control Register
Select Random Noise
Waveform, 1
=
On
Select Pulse Waveform, 1
= On
Select Sawtooth
Waveform, 1 = On
Select Triangle Waveform,
1 = On
Test Bit: 1 = Disable Oscillator 3
Ring ModulateOsc.
3 with
Osc. 2 Output, 1 = On
Synchronize Ose. 3 with
Osc. 2 Frequency, 1 =
On
Gate Bit: 1 = Start Att/
DeclSus, 0 = Start Release
326 BASIC TO MACHINE LANGUAGE
HEX
D413
D414
D415
D416
D417
D418
DECIMAL
54291
54292
54293
54294
54295
54296
7-4
3
2
1
0
7
6
5
7-4
3-0
7-4
3-0
BITS
DESCRIPTION
Envelope Generator 3: Attack
/ Decay Cycle
Control
Select Attack Cycle Duration: 0- 15
Select Decay Cycle Duration: 0-15
Envelope Generator 3:
Sustain / Release Cycle
Control
Select Sustain Cycle Duration: 0-15
Select Release Cycle Duration: 0-15
Filter Cutoff Frequency:
Low-Nybble (Bits 2-0)
Filter Cutoff Frequency:
High-Byte
Filter Resonance Control /
Voice Input Control
Select Filter Resonance:
0-15
Filter External Input: 1
Yes, 0 = No
Filter Voice
3
Output: 1
Yes, 0 = No
=
=
Filter Voice 2 Output: 1
Yes, 0 = No
=
=
Filter Voice 1 Output: 1
Yes, 0 = No
Select Filter Mode and
Volume
Cut-Off Voice
3
Output: 1
= Off, 0 = On
Select Filter High-Pass
Mode: 1
=
On
Select Filter Band-Pass
Mode: 1
=
On
BASIC TO MACHINE LANGUAGE 327 .
HEX
D419
DECIMAL
4
3-0
BITS
54297
D41A
D41B
D41C
54299
54230
D500-D7FF 54528-55295
D800-DBFF 55296-56319
DCOO-DCFF 56320.-56575
DCOO
DCOl
54298
56320
56321
7-0
7-6
4
3-2
3-0
DESCRIPTION
Select Filter low-Pass
Mode: 1
=
On
Select Output Volume:
0-15
Analog/Digital Converter:
Game Paddle 1 (0-
255)
Analog/Digital Converter:
Game Paddle 2 (0-
255)
Oscillator 3 Random
Number Generator
Envelope Generator 3
Output
SID IMAGES
Color RAM (Nybbles)
MOS 6526 Complex
Interface Adapter (CIA)
#1
Data Port A (Keyboard,
Joystick, Paddles, light-Pen)
Write Keyboard Column
Values for Keyboard
Scan
Read Paddles on Port A /
B (01
=
Port A, 10 =
Port B)
Joystick A fire Button: 1
Fire
=
Paddle Fire Buttons
Joystick A Direction
(0- 15)
Data Port B (Keyboard,
Joystick, Paddles):
Game Port 1
328 BASIC TO MACHINE LANGUAGE
DC02
DC03
DC04
DC05
DC06
DC07
DC08
DC09
DCOA
DCOB
DCOC
DCOD
HEX DECIMAL
BITS
7-0
56322
56323
56324
56325
56326
56327
56328
56329
56330
56331
56332
56333
7
6
4
3-2
3-0
7
4
DESCRIPTION
Read Keyboard Row
Values for Keyboard
Scan
Timer B: Toggle/Pulse
Output
Timer A: Toggle/Pulse
Output
Joystick 1 Fire Button: 1
Fire
=
Paddle Fire Buttons
Joystick 1 Direction
Data Direction
Register-Port A
(56320)
Data Direction
Register-Port B
(56321)
Timer A: Low-Byte
Timer A: High-Byte
Timer B: Low-Byte
Timer B: High-Byte
Time-of-Day Clock: 1/10
Seconds
Time-of-Day Clock: Seconds
Time-of-Day Clock: Minutes
Time-of-Day Clock: Hours
+ AM/PM Flag (Bit 7)
Synchronous Serial I/O
Data Buffer
CIA Interrupt Control
Register (Read IRQs/
Write Mask)
IRQ Flag (1
=
IRQ Occurred) / Set-Clear Flag
FLAG1 IRQ (Cassette Read
/ Serial Bus SRQ Input)
BASIC TO MACHINE LANGUAGE 329
HEX
DCOE
DCOF
DECIMAL
56334
1
0
7
3
2
BITS
56335
1
0
4
3
6
5
2
7
DESCRIPTION
Serial Port Interrupt
Time-of-Day Clock Alarm
Interrupt
Timer B Interrupt
Timer A Interrupt
CIA Control Register A
Time-of-Day Clock Frequency: 1
=
60 Hz
=
50 Hz, 0
Serial Port I/O Mode: 1
=
Output, 0 = Input
Timer A Counts: 1
=
CNT
Signals, 0
=
System 02
Clock
Force Load Timer A: 1
=
Yes
Timer A Run Mode: 1
=
One-Shot, 0 = Continuous
Timer A Output Mode to
PB6: 1
Pulse
= Toggle, 0 =
Timer A Output on PB6: 1
= Yes, 0 = No
Start/Stop Timer A: 1
=
Start, 0 = Stop
CIA Control Register B
Set Alarm/TOD-Clock: 1
=
Alarm, 0 = Clock
330 BASIC TO MACHINE LANGUAGE
HEX
DD01
DECIMAL
6-5
BITS
DDOO-DDFF 56576-56831
DDOO 56576
56577
4-0
3
2
1-0
7
6
5
4
7
DESCRIPTION
Timer B Mode Select:
00 = Count System 02
Clock Pulses
01 = Count Positive
CNT Transitions
10 = Count Timer
A
Underflow Pulses
11 = Count Timer
A
Underflows While
CNT Positive
Same as CIA Control Reg.
A-for Timer B
MOS 6526 Complex Interface Adapter (CIA) #2
Data Port A (Serial Bus,
RS-232, VIC Memory
Control)
Serial Bus Data Input
Serial Bus Clock Pulse
Input
Serial Bus Data Output
Serial Bus Clock Pulse
Output
Serial Bus ATN Signal
Output
RS-232 Data Output (User
Port)
VIC Chip System Memory
Bank Select (Default =
11)
Data Port B (User Port,
RS-232)
User
/ RS-232 Data Set
Ready
BASIC TO MACHINE LANGUAGE
331
DD02
DD03
DD04
DD05
0006
DD07
D008
DD09
DDOA
DOOB
DDOC
DDOD
HEX
56578
56579
56580
56581
56582
56583
56584
56585
56586
56587
56588
56589
DECIMAL
1
0
3
2
6
5
4
BITS DESCRIPTION
User / RS-232 Clear to
Send
User
User / RS-232 Carrier Detect
User / RS-232 Ring Indicator
User / RS-232 Data Terminal Ready
User / RS-232 Request to
Send
User / RS-232 Received
Data
Data Direction
Register-Port A
Data Direction
Register-Port
Timer A: Low-Byte
B
Timer A: High-Byte
Timer B: Low-Byte
Timer B: High-Byte
Time-of-Day Clock: 1/10
Seconds
Time-of-Day Clock: Seconds
Time-of-Day Clock: Minutes
Time-of-Day Clock: Hours
+ AM!PM Flag (Bit 7)
Synchronous Serial I/O
Data Buffer
CIA Interrupt Control
Register (Read NMls/
Write Mask)
332 BASIC TO MACHINE LANGUAGE
HEX
DDOE
DDOF
DECIMAL
56590
56591
6
5
4
3
2
7
1
0
7
4
3
1
0
7
BITS DESCRIPTION
NMI Flag (1
=
NMI Occurred) / Set-Clear Flag
FLAG1 NMI (UserlRS-232
Received Data Input)
Serial Port Interrupt
Timer B Interrupt
Timer A Interrupt
CIA Control Register A
Time-of-Day Clock Fre-
.quency: 1
=
60 Hz
=
50 Hz, 0
Serial Port I/O Mode: 1
=
Output, 0 = Input
Timer A .Counts: 1
=
CNT
Signals, 0 = System 02
Clock
Force Load Timer A: 1
=
Yes
Timer A Run Mode: 1
=
One-Shot, 0 = Continuous
Timer A Output .Mode to
PB6: 1
Pulse
= Toggle, 0 =
-Timer A Output on PB6: 1
= Yes, 0 = No
Start/Stop Timer A: 1
=
Start, 0 = Stop
CIA Control Register B
Set Alarm/TOD-Clock: 1
=
Alarm, 0 = Clock
BASIC TO MACHINE LANGUAGE 333
.
-....
HEX
..
-
DECIMAL
6-5
BITS
. DEOO-DEFF
56832-57081
DFOO-DFFF
57088-57343
4-0
. DESCRIPTION
Tiiner
B Mode Select:
00 = Count System 02
Clock Pulses
01 = Count Positive
CNT Transitions
10 = Count Ti.merA
Underflow Pulses
11 = Count Timer A
Underflows While
CNT Positive
Same as CIA Control Reg.
A-for Timer B
Reserved for Future I/O
Expansion
Reserved for Future I/O
Expansion
334 BASIC TO MACHINE LANGUAGE
Download
Advertisement
Key features
Machine language programming
Memory management
KERNAL operating system
Commodore 64 architecture
Frequently asked questions
Machine language is the only programming language that your Commodore 64 understands. It is the native language of the machine.
The most common methods used to write machine language programs are assembler programs. These packages allow you to write machine language instructions in a standardized mnemonic format, which makes the machine language program a lot more readable than a stream of numbers!
Machine language programs are hundreds, and in some cases thousands of times faster than a high level language such as BASIC. They can also be made totally "watertight," which means that the user can only do what the program allows.
When approaching a large task in machine language, it is usually a good idea to write it out on paper. Use block diagrams of memory usage, functional modules of code required, and a program flow.