Specification of CPU for 2004  (CP8299)

8-bit cpu with 64K bytes memory space.  The processor contains one accumulator and one 16-bit stack pointer.  The instruction set contains 16 instructions with 4 addressing modes: inherit, immediate, relative and direct (absolute).

ISA

format

op:4 mode:2 unused:2   follows by 0, 1 or 2 bytes operand.

Addressing mode

00 inherit    AC
01 immediate  8 or 16-bit  (16-bit only for LDS)
10 relative for jumps, 8-bit offset relative to current PC after executing the current instruction
11 direct, refers to memory 16-bit address
 

instruction set

notation: flag 0, 1 x-not affected, + affected
mode: x - not applicable, + valid
 
code  mnenomic C Z  description  IH IM RE DI
0000  LDS    0 1  load stack pointer  x  +  x  +
0001  LDA    0 +  load AC  x  +  x  +
0010  PSH    x +    push AC to stack  +  x  x  x
0011  STA    x x  store AC to memory  x  x  x  +
0101  JMP    x x   jump unconditional  x  x  +  +
0110  JPZ    x x    jump if zero  x  x  +  +
0111  JPC    x x  jump if carry  x  x  +  +
1000  JSR    x x   jump to subroutine  x  x  +  +
1001  AND    0 +   logical and  x  +  x  +
1010  ORA    0 +  logical or  x  +  x  +
1100  POP    x x  pop stack to AC  +  x  x  x
1101  XOR    0 +  logical xor  x  +  x  +
1110  ADD    + +  add to AC  x  +  x  +
1101  RTS    x x  return from subroutine  +  x  x  x
1110  ROL    + +  rotate left ac thro carry  +  x  x  x
1111  ROR    + +   rotate right ac thro carry  +  x  x  x

LDS is a special instruction, its immediate and direct mode load 16-bit value.  Big endian is assumed, ie. in the memory which is byte-addressed, the first byte (low address) is 8-most significant bit, the second byte is the remainder 8-bit.

SP is a 16-bit register, it starts at hi memory and going downward to lo memory.

JSR stores 16-bit return address in the stack by pushing it, with 8-most significant bit being pushed first.
RTS reverses the process.  So, recursive call is possible.

Relative jumps use 8-bit offset to the current PC.  It must be understood that the current PC is the PC AFTER the current instruction (jump) is executed.  Therefore, jump relative 0 is jump to the NEXT instruction (NOT this instruction).

In the simulator, memory is set by default to 1000 bytes (MAXMEM).  SP initially is MAXMEM -1.
The simulator is set to run to the maximum 500 instructions to prevent infinite loop.  These default setting can be changed in the source code and recompile.

Some tricks

No subtract instruction is available.  To compare two integers the easy way is to convert one integer into 2's complement  (inverse bit and add 1), or its negative value, then add them.  If the result is zero then they are equal.

Example

in a while loop

while i <= 10
   do X

i <= 10    can be accomplished by   comparing i with 11

while i != 11

in assembly

.a 0
.c
    LDA I
    ADD M11
    JPZ  exit
   ...

.b
:M11 245   ;;  245 is -11
:I   0
.e

To inverse a number, xor it with 11111111.  If you need multiplication, the simplest way is to repeat add.  (but squaring add is faster).  The standard way is to shift-add, Booth's algorithm can be found in your comp. org. text book. (but it is not easy to implement correctly).   I have not tried to do it with this CPU yet, so I don't know if it is possible or not to do Booth's algorithm with CPU4.
 

last update 21 July 2004