Skip to content

SAP1Emu Instruction Set

Bob Baker edited this page Oct 5, 2020 · 51 revisions

A lot of this is outdated. Documentation is slowly moving over to sap1emu.net/Docs/Docs

Currently, the SAP1Emu Project supports 3 different instruction sets.

  1. SAP1Emu Extended Set (Default)
  2. Malvino Restricted Set
  3. Ben Eater's Set

The desired set can be chosen by the user in the CLI version with the Instruction Set Flag (see Emulator CLI) and in the GUI via a dropdown menu.

Malvino's SAP-1 Instruction Set

The SAP-1 Computer outlined by Malvino supported only five instructions:

LDA addr   // A Register = RAM[addr] 
ADD addr   // A Register = A Register + RAM[addr]
SUB addr   // A Register = A Register - RAM[addr]
OUT 0x0    // Output Register = A Register
HLT 0x0    // Halt Execution

SAP1Emu's Extended Instruction Set

The SAP1Emu Project expands on this by adding more instructions. It is still fully compatible with Malvino's instruction set without any side effects. All Malvino coded will run as SAP1Emu code, but SAP1Emu code is not backwards compatible with Malvino code.

LDA addr   // A Register = RAM[addr] 
ADD addr   // A Register = A Register + RAM[addr]
SUB addr   // A Register = A Register - RAM[addr]
STA addr   // RAM[addr] = A Register
JMP addr   // Program Counter = addr
JEQ addr   // If A==0, Program Counter = addr     
JNQ addr   // If A!=0, Program Counter = addr
JLT addr   // If A<0, Program Counter = adrr (treats A as two's-complement) 
JGT addr   // If A>0, Program Counter = adrr (treats A as two's-complement) 
JIC addr   // If Carry_Flag == 1 (ALU Overflow), Program Counter = adrr
OUT 0x0    // Output Register = A Register
HLT 0x0    // Halt Execution
NOP 0x0    // No Operation (must come after HLT)

Note: At this time, once set, the Carry Flag will stay set until the end of the program.

With this expanded instruction set, the SAP1Emu emulator can run much more sophisticated programs.

Ben Eater's Instruction Set

In his videos, Ben Eater uses a slightly different instruction set than Malvino. This is probably to assist his Sequencer optimizations.
In this set, the binary values are slightly different, but that shouldn't matter unless you are programming in binary. The GUI program does have an Assembler built in where you can see your program assembled into different binaries depending on the set used.

NOP 0x0    // No Operation (must come after HLT) 
LDA addr   // A Register = RAM[addr] 
ADD addr   // A Register = A Register + RAM[addr]
SUB addr   // A Register = A Register - RAM[addr]
STA addr   // RAM[addr] = A Register
LDI value  // Loads a 4-bit value into the A Register // Not yet implemented
JMP addr   // Program Counter = addr
JIC addr   // If Carry_Flag == 1 (ALU Overflow), Program Counter = adrr
JEQ addr   // If A==0, Program Counter = addr     
OUT 0x0    // Output Register = A Register
HLT 0x0    // Halt Execution

Semantics and Definitions

Memory Referenced Instructions

A memory referenced instruction is an instruction that takes a memory address as its operand. All memory address in SAP1Emu Assembly must be a 4-bit hex value in the form 0xH.
LDA, ADD, SUB, and STA are all memory referenced instructions.

Non-Memory/Null Referenced Instructions

A non-memory/null referenced instruction still needs an operand, but it does not do anything. By convention, it is suggested to use 0x0 after a non-memory/null referenced instruction.
OUT, HLT and NOP are all non-memory referenced instructions.

Value Referenced Instructions

The Jump-Category of instructions are not technically Memory Referenced Instructions nor are they Non-Memory/Null Referenced Instructions. Their operands have meaning but do not reference the RAM directly.
While this is trivial nomenclature, this project will refer to the jump instructions as Value Referenced. This includes JMP, JEQ, JNQ, JLT, JGT, and JIC.

Note: If you use a jump command, for example, JMP 0x0, it will jump to the first line in RAM.
JMP 0x1 will not jump to the first line of code, but the second line of code because the RAM indexing starts at 0.

Macros

The SAP1Emu Instruction Set also has one macro:

...        // Fill with NOP's until the data section

It dramatically increases the readability of your code and is recommended for use in all programs where it is valid.
Its usage is covered in the Assembly File page.

Clone this wiki locally