| 1 | # 8-Bit CPU Emulator |
| 2 | |
| 3 | This project is an educational 8-bit CPU emulator written in Rust, it's build to understand core computer architecture concepts such as instruction sets, flags, |
| 4 | control flow, stacks and system calls. |
| 5 | |
| 6 | This project is **not** a production CPU and **not** accurate. It is designed purely for learning and experimentation. |
| 7 | |
| 8 | ----- |
| 9 | |
| 10 | ## Features |
| 11 | |
| 12 | - Custom 8-bit ISA (control-flow and arithmetic operations) |
| 13 | - 4 general-purpose registers |
| 14 | - 64 KB (0x0000-0xFFFF) byte-addressable memory |
| 15 | - Two-pass assembler with label support |
| 16 | - Stack-based function calls |
| 17 | - Stack is software-managed and grows downward |
| 18 | |
| 19 | --- |
| 20 | |
| 21 | |
| 22 | |
| 23 | ## Registers |
| 24 | | Register | Size | Description | |
| 25 | | -------- | ------ | ------------------------------ | |
| 26 | | A | 8-bit | General-purpose register | |
| 27 | | B | 8-bit | General-purpose register | |
| 28 | | C | 8-bit | General-purpose register | |
| 29 | | D | 8-bit | General-purpose register | |
| 30 | | PC | 16-bit | Program Counter | |
| 31 | | SP | 16-bit | Stack pointer | |
| 32 | |
| 33 | |
| 34 | ## Flags Register |
| 35 | |
| 36 | The CPU maintains a small flags register. |
| 37 | |
| 38 | | Flag | Description | |
| 39 | | ----- | ------------ | |
| 40 | | Z | Zero flag - set if last result was `0` | |
| 41 | | C | Carry/Borrow flag | |
| 42 | |
| 43 | - Control-flow instructions do **not** modify flags. |
| 44 | |
| 45 | ## Supported Instructions |
| 46 | |
| 47 | | Instruction | Syntax | |
| 48 | | ----------- | ---------------- | |
| 49 | | MOV | mov dest, src OR mov reg, imm | |
| 50 | | ADD | add r1, r2 OR add reg, imm | |
| 51 | | SUB | sub r1, r2 OR sub reg, imm | |
| 52 | | JMP | jmp addr OR jmp addr | |
| 53 | | JZ | jz addr OR jz addr | |
| 54 | | JNZ | jnz addr OR jnz addr | |
| 55 | | CMP | cmp r1, r2 OR cmp reg, imp | |
| 56 | | MUL | mul r1, r2 | |
| 57 | | DIV | div r1, r2 | |
| 58 | | CALL | call \<label\> | |
| 59 | | SYS | sys \<syscall_no\> | |
| 60 | | HLT (Halt) | hlt | |
| 61 | |
| 62 | ## Syscalls |
| 63 | |
| 64 | | Imm | Meaning | |
| 65 | | --- | ------- | |
| 66 | | 0 | Exit Program | |
| 67 | | 1 | Print register A as integer | |
| 68 | | 2 | Print register A as ASCII char | |
| 69 | |
| 70 | - Syscalls also do not modify flags |
| 71 | |
| 72 | ## Example assembly program |
| 73 | |
| 74 | ```assembly |
| 75 | mov b, 3 |
| 76 | mov a, 1 |
| 77 | |
| 78 | loop: |
| 79 | sub b, a |
| 80 | jnz loop |
| 81 | |
| 82 | sys 1 ; print register A |
| 83 | sys 0 ; exit |
| 84 | ``` |
| 85 | |
| 86 | ## Usage |
| 87 | |
| 88 | ```bash |
| 89 | cargo run -- --f <examples/filename.asc> |
| 90 | ``` |
| 91 | |
| 92 | ## Non-goals |
| 93 | |
| 94 | 1. No pipelining |
| 95 | 2. No interrupts |
| 96 | 3. No virtual memory |
| 97 | 4. No privilege levels |
| 98 | 5. No hardware I/O |
| 99 | |
| 100 | ## Goals |
| 101 | |
| 102 | - [x] Assembler |
| 103 | - [x] Lexer/Tokenizer |
| 104 | - [x] Add label support (supporting JMP/JZ/JNZ) |
| 105 | - [x] Add instructions |
| 106 | - [x] CMP |
| 107 | - [x] MUL |
| 108 | - [x] DIV |
| 109 | - [x] CALL |
| 110 | - [x] RET |
| 111 | - [x] SYS |
| 112 | - [ ] Better error-handling |
| 113 | - [ ] TUI debugger |