ZPU instruction set
Since ZPU is a stack-based processor, all operands are in stack except for load instructions. Similarly, all target for operations is the stack, except for store instructions.
Mnemonic | Opcode | Description | Clocks |
BREAKPOINT | 00000000 | Stop processing or jump to debugger | 4 |
IM x | 1xxxxxxx | Push or append 7-bit immediate | 4 |
STORESP x | 010xxxxx | Pop and store to stack with offset | 5 |
LOADSP x | 011xxxxx | Push value present on stack with offset | 4 |
ADDSP x | 0001xxxx | Add value present on stack with offset to TOS | 6 |
EMULATE x | 001xxxxx | Emulate instruction. See emulation table. | 4 |
POPPC | 00000100 | Pop current value and set PC | 5 |
LOAD | 00001000 | Pop current value and push memory value | 4 (b) |
STORE | 00001100 | Store NOS into memory pointed by TOS, Pop both | 6 (b) |
PUSHSP | 00000010 | Push current SP | - |
POPSP | 00001101 | Pop and store into SP | 5 |
ADD | 00000101 | Integer addition | 5 |
AND | 00000110 | Bitwise AND | 5 |
OR | 00000111 | Bitwise OR | 5 |
NOT | 00001001 | Bitwise NOT | 4 |
FLIP | 00001010 | Flip bits | 4 |
NOP | 00001011 | NO-Operation | 4 |
Optional instructions (emulated)
Emulation opcodes start at 33, and may be implemented in hardware or use emulation code
Mnemonic | Opcode | Description | Clocks |
N/A | 33 | This instruction is not available | |
LOADH | 34 | Load short | (a) |
STOREH | 35 | Store short | (a) |
LESSTHAN | 36 | Signed less-than comparison | (a) |
LESSTHANOREQUAL | 37 | Signed less-than-or-equal comparison | (a) |
ULESSTHAN | 38 | Unsigned less-than comparison | (a) |
ULESSTHANOREQUAL | 39 | Unsigned less-than-or-equal comparison | (a) |
SWAP | 40 | Instruction not implemented | |
MULT | 41 | Signed 32-bit multiplication | (a) |
LSHIFTRIGHT | 42 | Logical shift right | (a) |
ASHIFTLEFT | 43 | Arithmetic shift left | (a) |
ASHIFTRIGHT | 44 | Arithmetic shift right | (a) |
CALL | 45 | Call function | (a) |
EQ | 46 | Comparison equal | (a) |
NEQ | 47 | Comparison not equal | (a) |
NEG | 48 | Negative | (a) |
SUB | 49 | Subtract | (a) |
XOR | 50 | Exclusive-OR | (a) |
LOADB | 51 | Load byte | (a) |
STOREB | 52 | Store byte | (a) |
DIV | 53 | Signed 32-bit division | (a) |
MOD | 54 | Signed 32-bit modulus | (a) |
EQBRANCH | 55 | Branch if equal | (a) |
NEQBRANCH | 56 | Branch if not equal | (a) |
POPPCREL | 57 | Pop PC relative | (a) |
CONFIG | 58 | Internal configuration | (a) |
PUSHPC (a) | 59 | Push current PC | (a) |
SYSCALL (a) | 60 | System Call (currently not implemented) | (a) |
PUSHSPADD | 61 | Push SP + offset | 48 (a) |
HALFMULT | 62 | Instruction not implemented | |
CALLPCREL | 63 | Call relative function | (a) |
Instruction details
BREAKPOINT
- Purpose
- Hardware breakpoint
IM
- Purpose
- Push or append 7-bit immediate
- Description
- Push or append 7-bit immediate, specified by the lowermost 7 bits of opcode. If previous instruction was not IM, then immediate value is pushed into stack. If previous instruction was an IM, then current TOS is shifted 7 bits to the left, and immediate value is added to it. Interrupts are disabled while processing IM instructions.
- C Algorithm
- unsigned offset = opcode & 0x7F; if (idim_flag) { unsigned X = Pop(); Push( ( X << 7 ) | offset ); } else { Push ( offset ); idim_flag = 1; }
- Examples
LOADSP
- Purpose
- Push SP-relative value into stack
- Description
- Push value of memory location SP+xxxxx*4, where xxxxx is a positive integer, onto stack. "xxxxx" are the lowermost 5 bits of opcode, but with most significant bit inverted.
- C Algorithm
- unsigned offset = ( opcode & 0x1f ) ^ 0x10; Push( MEMORY[SP + offset * 4] );
STORESP
- Purpose
- Store value in top of stack to SP-relative position
- Description
- Pop value off stack and store it in the SP+xxxxx*4 memory location, where xxxxx is a positive integer. "xxxxx" are the lowermost 5 bits of opcode, but with most significant bit inverted.
- C Algorithm
-
unsigned X = Pop(); unsigned offset = ( opcode & 0x1f ) ^ 0x10; MEMORY[SP + offset * 4] = X;
ADDSP
- Purpose
- Add SP-relative value to top of stack
- Description
- Add value of memory location SP+xxxx*4 to value on top of stack. "xxxx" are the lowermost 4 bits of opcode.
- C Algorithm
-
unsigned offset = ( opcode & 0x0f ); Push( Pop() + MEMORY[SP + offset * 4] );
EMULATE
- Emulate instruction
- Description
- Instruction emulation opcode. See emulation table for a list and description of emulated opcodes. Push PC to stack and set PC to 0x0+xxxxx*32. This mnemonic cannot be used directly.
- C Algorithm
-
unsigned offset = ( opcode & 0x1f ); Push( PC ); PC = offset * 32;
Purpose
POPPC
- Pop value from stack, and set PC (Program Counter)
- Description
- This is a jump instruction. Desired location is popped from stack and written to Program Counter.
- C Algorithm
-
PC = Pop();
Purpose
LOAD
- Load 32-bit value from memory.
- Description
- Pops address stored on stack and loads the value of that address onto stack. Bit 0 and 1 of address are always treated as 0(i.e. ignored) by the HDL implementations and C code is guaranteed by the programming model never to use 32 bit LOAD on non-32 bit aligned addresses(i.e. if a program does this, then it has a bug).
- C Algorithm
-
unsigned address = Pop(); Push( MEMORY[ address & ~3 ] );
Purpose
STORE
- Store 32-bit value to memory.
- Description
- Pops address, then value from stack and stores the value into the memory location of the address. Bit 0 and 1 of address are always treated as 0.
- C Algorithm
-
unsigned address = Pop(); unsigned value = Pop(); MEMORY[ address & ~3 ] = value;
Purpose
PUSHSP
- Push current stack pointer.
- Description
- Push current SP (stack pointer) value.
- C Algorithm
-
Push( SP );
Purpose
POPSP
- Pop and set stack pointer.
- Description
- Pops value off top of stack and sets SP to that value. Used to allocate/deallocate space on stack for variables or when changing threads.
- C Algorithm
-
SP = Pop();
Purpose
ADD
- Add two values.
- Description
- Pop two values from stack, add them and push the result.
- C Algorithm
-
unsigned A = Pop(); unsigned B = Pop(); Push( A + B );
Purpose
AND
- Bitwise AND two values.
- Description
- Pop two values from stack, do a bitwise AND and push the result.
- C Algorithm
-
unsigned A = Pop(); unsigned B = Pop(); Push( A & B );
Purpose
OR
- Bitwise OR two values.
- Description
- Pop two values from stack, do a bitwise OR and push the result.
- C Algorithm
-
unsigned A = Pop(); unsigned B = Pop(); Push( A | B );
Purpose
NOT
- Bitwise NOT
- Description
- Pop value from stack, invert all bits and push back
- C Algorithm
-
unsigned A = Pop(); Push( ~A );
Purpose
FLIP
- Flip all bits
- Description
- Reverses the bit order of the value on the stack, i.e. abc->cba, 100->001, 110->011, etc. The raison d'etre for this instruction is mainly to emulate other instructions.
- C Algorithm
-
unsigned A = Pop(); unsigned B = 0; for (i=0; i<32; i++) { if (A & (1<<i)) { B |= (1<<(31-i)); } } Push( B );
Purpose
NOP
- No Operation
- Description
- No Operation. This will still cause idim_flag to be set to 0.