Tmach
256bit vm
Install / Use
/learn @xtaci/TmachREADME
tmach Virtual Machine Instruction Set Architecture Specification
1. Registers
1.1 General-Purpose Registers
- Width: 256 bits.
- Function: Store integer or floating-point data.
- Classification:
- Integer Registers:
R0toR7for integer operations. - Floating-Point Registers:
F0toF7for floating-point operations.
- Integer Registers:
- Encoding:
R0toR7:0000to0111.F0toF7:1000to1111.
1.2 Address Registers
- Width: 32 bits.
- Function: Used for memory address calculations.
- Registers:
A0toA7. - Encoding: Each address register is uniquely identified by a 4-bit code from
0000to0111.
1.3 Special-Purpose Registers
-
Status Register (SR):
- Width: 8 bits.
- Function: Records the results of arithmetic and logical operations.
- Flags:
- Bit 0: Zero Flag (ZF) – Set to 1 if the result of the previous operation is zero.
- Bit 1: Overflow Flag (OF) – Set to 1 if an arithmetic overflow occurs.
- Bit 2: Divide-by-Zero Flag (DF) – Set to 1 if division by zero is attempted.
- Bits 3–5: Comparison Result (CR) – Encodes the outcome of a
CMPinstruction (e.g., "less than," "equal," or "greater than"). - Bits 6–7: Reserved for future use.
-
Program Counter (PC):
- Function: Stores the address of the next instruction to execute.
- Property: Read-only.
-
Jump Return Register (J):
- Function: Automatically stores the return address (the address of the instruction immediately following a jump) when a jump instruction is executed.
- Property: Read-only; used to facilitate function calls and returns.
2. Instruction Set
2.1 Memory Access Instructions
- Function: Load data from memory into registers or store register data into memory.
- Address Calculation: Memory addresses are fully determined by address registers (no offset).
- Instruction Format:
- LOAD:
LOAD Rd, [Ax]- Loads data from the memory address stored in
Axinto registerRd.
- Loads data from the memory address stored in
- STORE:
STORE Rs, [Ax]- Stores the contents of register
Rsinto the memory address stored inAx.
- Stores the contents of register
- LOAD:
- Machine Code Format (16 bits):
- Opcode: 8 bits.
- Target/Source Register: 4 bits.
- Address Register: 4 bits.
Examples:
-
LOAD R2, [A1]:- Opcode:
0x08 - Target Register:
0010(R2) - Address Register:
0001(A1) - Machine Code:
0x0821
- Opcode:
-
STORE R3, [A2]:- Opcode:
0x09 - Source Register:
0011(R3) - Address Register:
0010(A2) - Machine Code:
0x0932
- Opcode:
2.2 Arithmetic Instructions
- Function: Perform arithmetic operations (addition, subtraction, multiplication, division).
- Operand Type: Determined by the register name (
Rfor integer,Ffor floating-point). - Instruction Format:
- ADD:
ADD Rd, Rs, Rt- Adds the values in
RsandRt, storing the result inRd.
- Adds the values in
- SUB:
SUB Rd, Rs, Rt- Subtracts
RtfromRs, storing the result inRd.
- Subtracts
- MUL:
MUL Rd, Rs, Rt- Multiplies
RsandRt, storing the result inRd.
- Multiplies
- DIV:
DIV Rd, Rs, Rt- Divides
RsbyRt, storing the result inRd. Sets theDFflag if division by zero occurs.
- Divides
- ADD:
- Machine Code Format (24 bits):
- Opcode: 8 bits.
- Source Register 1: 4 bits.
- Source Register 2: 4 bits.
- Destination Register: 4 bits.
- Reserved: 4 bits (unused).
Example:
ADD R0, R1, R2:- Opcode:
0x01 - Source Register 1:
0001(R1) - Source Register 2:
0010(R2) - Destination Register:
0000(R0) - Machine Code:
0x010012
- Opcode:
2.3 Comparison Instruction
- Function: Compare two register values and update the Status Register (SR).
- Instruction Format:
- CMP:
CMP Rs, Rt- Compares
RsandRt, updating theZF,LT, andGTflags.
- Compares
- CMP:
- Machine Code Format (16 bits):
- Opcode: 8 bits.
- Source Register 1: 4 bits.
- Source Register 2: 4 bits.
Example:
CMP R0, R1:- Opcode:
0x05 - Source Register 1:
0000(R0) - Source Register 2:
0001(R1) - Machine Code:
0x0501
- Opcode:
2.4 Type Conversion Instructions
- Function: Convert between integer and floating-point representations.
- Instruction Format:
- ITOF:
ITOF Fd, Rs- Converts the integer value in
Rsto a floating-point value, stored inFd.
- Converts the integer value in
- FTOI:
FTOI Rd, Fs- Converts the floating-point value in
Fsto an integer value, stored inRd.
- Converts the floating-point value in
- ITOF:
2.5 Logical and Bitwise Instructions
- Function: Perform bitwise operations.
- Instruction Format:
- AND:
AND Rd, Rs, Rt- Bitwise AND of
RsandRt, stored inRd.
- Bitwise AND of
- OR:
OR Rd, Rs, Rt- Bitwise OR of
RsandRt, stored inRd.
- Bitwise OR of
- XOR:
XOR Rd, Rs, Rt- Bitwise XOR of
RsandRt, stored inRd.
- Bitwise XOR of
- NOT:
NOT Rd, Rs- Bitwise NOT of
Rs, stored inRd.
- Bitwise NOT of
- LSH:
LSH Rd, N- Left-shifts the value in
RdbyNbits.
- Left-shifts the value in
- RSH:
RSH Rd, N- Right-shifts the value in
RdbyNbits.
- Right-shifts the value in
- CSH:
CSH Rd, N- Cyclically shifts the value in
RdbyNbits.
- Cyclically shifts the value in
- AND:
2.6 Control Flow Instructions
- Function: Modify program flow based on the Status Register (SR) flags.
- Instruction Format:
- JMP:
JMP Addr- Unconditionally jumps to the address
Addrand saves the return address inJ.
- Unconditionally jumps to the address
- Conditional Jumps:
- JZ: Jump if
ZF == 1. - JNZ: Jump if
ZF == 0. - JGT: Jump if
GT == 1. - JLT: Jump if
LT == 1. - JEQ: Jump if
ZF == 1.
- JZ: Jump if
- JMP:
2.7 Miscellaneous Instruction
- NOP: No operation (used for timing or alignment).
3. Machine Code Format Overview
3.1 Memory Access Instructions (16 bits)
| Field | Length | Description |
|------------------|--------|---------------------------------|
| Opcode | 8 bits | e.g., 0x08 for LOAD |
| Target/Source Reg| 4 bits | General-purpose register (R/F) |
| Address Register | 4 bits | Address register (A0–A7) |
3.2 Arithmetic/Logical Instructions (24 bits)
| Field | Length | Description |
|------------------|--------|---------------------------------|
| Opcode | 8 bits | e.g., 0x01 for ADD |
| Source Register 1| 4 bits | e.g., Rs |
| Source Register 2| 4 bits | e.g., Rt |
| Destination Reg | 4 bits | e.g., Rd |
| Reserved | 4 bits | Unused |
3.3 Jump Instructions (32 bits)
| Field | Length | Description |
|------------------|--------|---------------------------------|
| Opcode | 8 bits | e.g., 0x10 for JMP |
| Address |32 bits | Full memory address to jump to |
4. Offset Support at the Assembly Language Level
- Implementation:
- Address Calculation Macros: For example,
LOAD R0, [A1 + 30]is translated by the assembler into two instructions:- Use
ADDto computeA1 + 30and store the result in a temporary address register (e.g.,A2). - Execute
LOAD R0, [A2].
- Use
- Pseudo-Instructions: Provide syntax like
LOAD R0, [A1 + 30], which the assembler automatically converts into low-level instructions.
- Address Calculation Macros: For example,
5. Summary
The tmach Virtual Machine Instruction Set is designed for simplicity and flexibility, with memory addressing fully controlled by 32-bit address registers. By removing offsets from hardware instructions, the design achieves greater compactness, while offset support is reintroduced at the assembly language level through macros or pseudo-instructions. This architecture is well-suited for high-precision computations, embedded systems, and scenarios requiring efficient control flow. Future enhancements could include stack support for nested function calls and expanded status flag definitions.
