Skip to content

basiccpu ISA :: Adding a new instruction

jslick edited this page May 8, 2012 · 1 revision

To add a new instruction to BasicCpu:

1. Design the instruction

Including:

  • The opcode string
  • The operands, and which operand types the instruction should take
2. Write guest code that uses the instruction

So that you can test your implementation

3. Add the token to assembler.ypp

4. Scan the token in assembler.l

5. Add the token to the assembler grammar (most likely in the `opcode` rule)

6. Add the opcode to opcodes.h

Feel free to re-arrange the existing opcodes; but be aware that if you change the opcode values, you will need to rebuild the interpreter and your guest programs.

7. Add the assembler implementation of the opcode

  • In Isa::loadOpcodeTable, use MAP_OPCODE() to add the opcode to the framework's tables
  • Specify the instruction size in Isa::loadInstructionSizeTable
    • If the instruction size is fixed, specify the fixed-length size (in bytes)
    • If the instruction size varies according to its operands, specify 0 as the size
  • If the instruction size varies, implement the instruction size in Isa::calcInstructionSize
  • Encode the instruction in Isa::generateInstructions
Try to be consistent with other instructions

8. Add the instruction to the interpreter (basiccpu.cpp)

Be aware that the format of the instruction is the same format that you defined in the assembler (in Isa::generateInstructions).

Clone this wiki locally