Added new VM opcodes.

This commit is contained in:
Bobbi Webber-Manners 2018-05-21 14:35:59 -04:00 committed by GitHub
parent 6dbf45c39c
commit f94f6117bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 76 additions and 61 deletions

137
README.md
View File

@ -923,67 +923,82 @@ The call stack is used for all memory allocation within the virtual machine, as
### VM Instructions
| Instruction | Description |
|-------------|---------------------------------------------------------------------|
| END | Terminate execution |
| LDI | Pushes the following 16 bit word to the evaluation stack |
| LDAW | Replaces X with 16 bit value pointed to by X. |
| LDAB | Replaces X with 8 bit value pointed to by X. |
| STAW | Stores 16 bit value Y in addr pointed to by X. Drops X and Y. |
| STAB | Stores 8 bit value Y in addr pointed to by X. Drops X and Y. |
| LDRW | Replaces X with 16 bit value pointed to by `X+FP+1`. |
| LDRB | Replaces X with 8 bit value pointed to by `X+FP+1`. |
| STRW | Stores 16 bit value Y in addr pointed to by `X+FP+1`. Drops X and Y.|
| STRB | Stores 8 bit value Y in addr pointed to by `X+FP+1`. Drops X and Y. |
| SWP | Swaps X and Y |
| DUP | Duplicates X -> X, Y |
| DUP2 | Duplicates X -> X,Z; Y -> Y,T |
| DROP | Drops X |
| OVER | Duplicates Y -> X,Z |
| PICK | Duplicates stack level specified in `X+1` -> X |
| POPW | Pop 16 bit value from call stack, push onto eval stack [X] |
| POPB | Pop 8 bit value from call stack, push onto eval stack [X] |
| PSHW | Push 16 bit value in X onto call stack. Drop X. |
| PSHB | Push 8 bit value in X onto call stack. Drop X. |
| DISC | Discard X bytes from call stack. Drop X. |
| SPTOFP | Copy stack pointer to frame pointer. (Enter function scope) |
| FPTOSP | Copy frame pointer to stack pointer. (Release local vars) |
| ATOR | Convert absolute address in X to FP-relative address |
| RTOA | Convert FP-relative address in X to absolute address |
| INC | `X = X+1`. |
| DEC | `X = X-1`. |
| ADD | `X = Y+X`. Y is dropped. |
| SUB | `X = Y-X`. Y is dropped. |
| MUL | `X = Y*X`. Y is dropped. |
| DIV | `X = Y/X`. Y is dropped. |
| MOD | `X = Y%X`. Y is dropped . |
| NEG | `X = -X` |
| GT | `X = Y>X`. Y is dropped. |
| GTE | `X = Y>=X`. Y is dropped. |
| LT | `X = Y<X`. Y is dropped. |
| LTE | `X = Y<=X`. Y is dropped. |
| EQL | `X = Y==X`. Y is dropped. |
| NEQL | `X = Y!=X`. Y is dropped. |
| AND | `X = Y&&X`. Y is dropped. |
| OR | `X = Y||X`. Y is dropped. |
| NOT | `X = !X` |
| BAND | `X = Y&X`. Y is dropped. |
| BITOR | `X = Y|X`. Y is dropped. |
| BITXOR | `X = Y^X`. Y is dropped. |
| BITNOT | `X = ~X`. |
| LSH | `X = Y<<X`. Y is dropped. |
| RSH | `X = Y>>X`. Y is dropped. |
| JMP | Jump to address X. Drop X. |
| BRC | If `Y!= 0`, jump to address X. Drop X, Y. |
| JSR | Push PC to call stack. Jump to address X. Drop X. |
| RTS | Pop call stack, jump to the address popped. |
| PRDEC | Print 16 bit decimal in X. Drop X |
| PRHEX | Print 16 bit hex in X. Drop X |
| PRCH | Print character in X. Drop X |
| PRSTR | Print null terminated string pointed to by X. Drop X |
| PRMSG | Print literal string at PC (null terminated) |
| KBDCH | Push character from keyboard onto eval stack |
| KBDLN | Obtain line from keyboard and write to memory pointed to by Y. X contains the max number of bytes in buf. Drop X, Y. | |
Note that all the instructions with names ending in 'I' are so-called 'immediate mode' instructions. This means that the operand is the 16 bit word following the opcode, rather than the topmost element of the evaluation stack. The 'immediate mode' operand may be a data value or an address.
Relative mode instructions allow addressing relative to the frame pointer. This is helpful for easy access to local variables.
| Instruction | Description | Imm? | Rel? |
|-------------|------------------------------------------------------------------------------------------|------|------|
| END | Terminate execution | | |
| LDI | Pushes the following 16 bit word to the evaluation stack | * | |
| LDAW | Replaces X with 16 bit value pointed to by X. | | |
| LDAWI | Pushes the 16 bit value pointed to by following 16 bit word to evaluation stack. | * | |
| LDAB | Replaces X with 8 bit value pointed to by X. | | |
| LDABI | Pushes the 8 bit value pointed to by following 16 bit word to evaluation stack. | * | |
| STAW | Stores 16 bit value Y in addr pointed to by X. Drops X and Y. | | |
| STAWI | Stores 16 bit value X in addr pointed to by following 16 bit word. Drops X. | * | |
| STAB | Stores 8 bit value Y in addr pointed to by X. Drops X and Y. | | |
| STABI | Stores 8 bit value X in addr pointed to by following 16 bit word. Drops X. | * | |
| LDRW | Replaces X with 16 bit value pointed to by `X+FP+1`. | | * |
| LDRWI | Pushes the 16 bit value pointed to by following 16 bit word `+FP+1` to evaluation stack. | * | * |
| LDRB | Replaces X with 8 bit value pointed to by `X+FP+1`. | | * |
| LDRBI | Pushes the 8 bit value pointed to by following 16 bit word `+FP+1` to evaluation stack. | * | * |
| STRW | Stores 16 bit value Y in addr pointed to by `X+FP+1`. Drops X and Y. | | * |
| STRWI | Stores 16 bit value X in addr pointed to by following 16 bit word `+FP+1`. Drops X. | * | * |
| STRB | Stores 8 bit value Y in addr pointed to by `X+FP+1`. Drops X and Y. | | * |
| STRBI | Stores 8 bit value X in addr pointed to by following 16 bit word `+FP+1`. Drops X. | * | * |
| SWP | Swaps X and Y | | |
| DUP | Duplicates X -> X, Y | | |
| DUP2 | Duplicates X -> X,Z; Y -> Y,T | | |
| DROP | Drops X | | |
| OVER | Duplicates Y -> X,Z | | |
| PICK | Duplicates stack level specified in `X+1` -> X | | |
| POPW | Pop 16 bit value from call stack, push onto eval stack [X] | | |
| POPB | Pop 8 bit value from call stack, push onto eval stack [X] | | |
| PSHW | Push 16 bit value in X onto call stack. Drop X. | | |
| PSHB | Push 8 bit value in X onto call stack. Drop X . | | |
| DISC | Discard X bytes from call stack. Drop X. | | |
| SPTOFP | Copy stack pointer to frame pointer. (Enter function scope) | | |
| FPTOSP | Copy frame pointer to stack pointer. (Release local vars) | | |
| ATOR | Convert absolute address in X to FP-relative address | | |
| RTOA | Convert FP-relative address in X to absolute address | | |
| INC | `X = X+1`. | | |
| DEC | `X = X-1`. | | |
| ADD | `X = Y+X`. Y is dropped. | | |
| SUB | `X = Y-X`. Y is dropped. | | |
| MUL | `X = Y*X`. Y is dropped. | | |
| DIV | `X = Y/X`. Y is dropped. | | |
| MOD | `X = Y%X`. Y is dropped . | | |
| NEG | `X = -X` | | |
| GT | `X = Y>X`. Y is dropped. | | |
| GTE | `X = Y>=X`. Y is dropped. | | |
| LT | `X = Y<X`. Y is dropped. | | |
| LTE | `X = Y<=X`. Y is dropped. | | |
| EQL | `X = Y==X`. Y is dropped. | | |
| NEQL | `X = Y!=X`. Y is dropped. | | |
| AND | `X = Y&&X`. Y is dropped. | | |
| OR | `X = Y\|\|X`. Y is dropped. | | |
| NOT | `X = !X ` | | |
| BAND | `X = Y&X`. Y is dropped. | | |
| BITOR | `X = Y\|X`. Y is dropped. | | |
| BITXOR | `X = Y^X`. Y is dropped. | | |
| BITNOT | `X = ~X`. | | |
| LSH | `X = Y<<X`. Y is dropped. | | |
| RSH | `X = Y>>X`. Y is dropped. | | |
| JMP | Jump to address X. Drop X. | | |
| JMPI | Jump to 16 bit word following opcode. | * | |
| BRC | If `Y!= 0`, jump to address X. Drop X, Y. | | |
| BRCI | If `X!= 0`, jump to 16 bit word following opcode. Drop X. | * | |
| JSR | Push PC to call stack. Jump to address X. Drop X. | | |
| JSRI | Push PC to call stack. Jump to 16 bit word following opcode. Drop X. | * | |
| RTS | Pop call stack, jump to the address popped. | | |
| PRDEC | Print 16 bit value in X in decimal. Drop X. | | |
| PRHEX | Print 16 bit value in X in hexadecimal. Drop X. | | |
| PRCH | Print character in X. Drop X. | | |
| PRSTR | Print null terminated string pointed to by X. Drop X. | | |
| PRMSG | Print literal string at PC (null terminated) | | |
| KBDCH | Push character from keyboard onto eval stack | | |
| KBDLN | Obtain line from keyboard and write to memory pointed to by Y. X contains the max number of bytes in buf. Drop X, Y. | | |
### VM Memory Organization