-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu.go
More file actions
247 lines (214 loc) · 6.63 KB
/
cpu.go
File metadata and controls
247 lines (214 loc) · 6.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package main
import (
"fmt"
)
// OpCode represents an instruction operation code
type OpCode byte
const (
// Arithmetic operations
OpADD OpCode = 0x01 // ADD r1, r2, r3: r1 = r2 + r3
OpSUB OpCode = 0x02 // SUB r1, r2, r3: r1 = r2 - r3
OpMUL OpCode = 0x03 // MUL r1, r2, r3: r1 = r2 * r3
OpDIV OpCode = 0x04 // DIV r1, r2, r3: r1 = r2 / r3
// Memory operations
OpLOAD OpCode = 0x10 // LOAD r1, addr: r1 = memory[addr]
OpSTORE OpCode = 0x11 // STORE r1, addr: memory[addr] = r1
OpLOADI OpCode = 0x12 // LOADI r1, imm: r1 = imm (load immediate)
// Control flow
OpJMP OpCode = 0x20 // JMP addr: PC = addr
OpJZ OpCode = 0x21 // JZ r1, addr: if r1 == 0 then PC = addr
OpHALT OpCode = 0xFF // HALT: stop execution
)
// Instruction represents a single instruction
type Instruction struct {
Op OpCode // Operation code
Arg1 byte // First argument (usually destination register)
Arg2 byte // Second argument (source register or address high byte)
Arg3 byte // Third argument (source register or address low byte)
}
// CPU represents the emulated processor
type CPU struct {
Registers [16]int32 // 16 general-purpose registers
PC uint16 // Program counter
Memory [65536]byte // 64KB of memory
Halted bool // CPU halt flag
Debug bool // Debug mode flag
}
// NewCPU creates a new CPU instance
func NewCPU() *CPU {
return &CPU{
Registers: [16]int32{},
PC: 0,
Memory: [65536]byte{},
Halted: false,
Debug: false,
}
}
// LoadProgram loads a program into memory starting at address 0
func (c *CPU) LoadProgram(program []byte) {
copy(c.Memory[:], program)
}
// Fetch fetches the next instruction from memory
func (c *CPU) Fetch() Instruction {
if c.PC >= 65536-3 {
c.Halted = true
return Instruction{Op: OpHALT}
}
instr := Instruction{
Op: OpCode(c.Memory[c.PC]),
Arg1: c.Memory[c.PC+1],
Arg2: c.Memory[c.PC+2],
Arg3: c.Memory[c.PC+3],
}
c.PC += 4 // Each instruction is 4 bytes
return instr
}
// Decode decodes an instruction and returns a human-readable string
func (c *CPU) Decode(instr Instruction) string {
switch instr.Op {
case OpADD:
return fmt.Sprintf("ADD R%d, R%d, R%d", instr.Arg1, instr.Arg2, instr.Arg3)
case OpSUB:
return fmt.Sprintf("SUB R%d, R%d, R%d", instr.Arg1, instr.Arg2, instr.Arg3)
case OpMUL:
return fmt.Sprintf("MUL R%d, R%d, R%d", instr.Arg1, instr.Arg2, instr.Arg3)
case OpDIV:
return fmt.Sprintf("DIV R%d, R%d, R%d", instr.Arg1, instr.Arg2, instr.Arg3)
case OpLOAD:
addr := uint16(instr.Arg2)<<8 | uint16(instr.Arg3)
return fmt.Sprintf("LOAD R%d, 0x%04X", instr.Arg1, addr)
case OpSTORE:
addr := uint16(instr.Arg2)<<8 | uint16(instr.Arg3)
return fmt.Sprintf("STORE R%d, 0x%04X", instr.Arg1, addr)
case OpLOADI:
imm := int32(int16(uint16(instr.Arg2)<<8 | uint16(instr.Arg3)))
return fmt.Sprintf("LOADI R%d, %d", instr.Arg1, imm)
case OpJMP:
addr := uint16(instr.Arg1)<<8 | uint16(instr.Arg2)
return fmt.Sprintf("JMP 0x%04X", addr)
case OpJZ:
addr := uint16(instr.Arg2)<<8 | uint16(instr.Arg3)
return fmt.Sprintf("JZ R%d, 0x%04X", instr.Arg1, addr)
case OpHALT:
return "HALT"
default:
return fmt.Sprintf("UNKNOWN (0x%02X)", instr.Op)
}
}
// Execute executes a single instruction
func (c *CPU) Execute(instr Instruction) error {
switch instr.Op {
case OpADD:
if instr.Arg1 >= 16 || instr.Arg2 >= 16 || instr.Arg3 >= 16 {
return fmt.Errorf("invalid register index")
}
c.Registers[instr.Arg1] = c.Registers[instr.Arg2] + c.Registers[instr.Arg3]
case OpSUB:
if instr.Arg1 >= 16 || instr.Arg2 >= 16 || instr.Arg3 >= 16 {
return fmt.Errorf("invalid register index")
}
c.Registers[instr.Arg1] = c.Registers[instr.Arg2] - c.Registers[instr.Arg3]
case OpMUL:
if instr.Arg1 >= 16 || instr.Arg2 >= 16 || instr.Arg3 >= 16 {
return fmt.Errorf("invalid register index")
}
c.Registers[instr.Arg1] = c.Registers[instr.Arg2] * c.Registers[instr.Arg3]
case OpDIV:
if instr.Arg1 >= 16 || instr.Arg2 >= 16 || instr.Arg3 >= 16 {
return fmt.Errorf("invalid register index")
}
if c.Registers[instr.Arg3] == 0 {
return fmt.Errorf("division by zero")
}
c.Registers[instr.Arg1] = c.Registers[instr.Arg2] / c.Registers[instr.Arg3]
case OpLOAD:
if instr.Arg1 >= 16 {
return fmt.Errorf("invalid register index")
}
addr := uint16(instr.Arg2)<<8 | uint16(instr.Arg3)
// Load 4 bytes as a 32-bit integer (little-endian)
c.Registers[instr.Arg1] = int32(c.Memory[addr]) |
int32(c.Memory[addr+1])<<8 |
int32(c.Memory[addr+2])<<16 |
int32(c.Memory[addr+3])<<24
case OpSTORE:
if instr.Arg1 >= 16 {
return fmt.Errorf("invalid register index")
}
addr := uint16(instr.Arg2)<<8 | uint16(instr.Arg3)
// Store 32-bit integer as 4 bytes (little-endian)
val := c.Registers[instr.Arg1]
c.Memory[addr] = byte(val)
c.Memory[addr+1] = byte(val >> 8)
c.Memory[addr+2] = byte(val >> 16)
c.Memory[addr+3] = byte(val >> 24)
case OpLOADI:
if instr.Arg1 >= 16 {
return fmt.Errorf("invalid register index")
}
// Load immediate value (sign-extended from 16-bit)
imm := int32(int16(uint16(instr.Arg2)<<8 | uint16(instr.Arg3)))
c.Registers[instr.Arg1] = imm
case OpJMP:
addr := uint16(instr.Arg1)<<8 | uint16(instr.Arg2)
c.PC = addr
case OpJZ:
if instr.Arg1 >= 16 {
return fmt.Errorf("invalid register index")
}
if c.Registers[instr.Arg1] == 0 {
addr := uint16(instr.Arg2)<<8 | uint16(instr.Arg3)
c.PC = addr
}
case OpHALT:
c.Halted = true
default:
return fmt.Errorf("unknown opcode: 0x%02X", instr.Op)
}
return nil
}
// Step executes a single instruction cycle
func (c *CPU) Step() error {
if c.Halted {
return fmt.Errorf("CPU is halted")
}
instr := c.Fetch()
if c.Debug {
fmt.Printf("PC: 0x%04X | %s\n", c.PC-4, c.Decode(instr))
}
return c.Execute(instr)
}
// Run executes instructions until HALT or error
func (c *CPU) Run() error {
for !c.Halted {
if err := c.Step(); err != nil {
return err
}
}
return nil
}
// Reset resets the CPU to initial state
func (c *CPU) Reset() {
c.Registers = [16]int32{}
c.PC = 0
c.Halted = false
}
// ReadMemory32 reads a 32-bit integer from memory at the given address (little-endian)
func (c *CPU) ReadMemory32(addr uint16) int32 {
return int32(c.Memory[addr]) |
int32(c.Memory[addr+1])<<8 |
int32(c.Memory[addr+2])<<16 |
int32(c.Memory[addr+3])<<24
}
// DumpRegisters prints the current state of all registers
func (c *CPU) DumpRegisters() {
fmt.Println("Register Dump:")
for i := 0; i < 16; i += 4 {
fmt.Printf("R%02d: %10d R%02d: %10d R%02d: %10d R%02d: %10d\n",
i, c.Registers[i],
i+1, c.Registers[i+1],
i+2, c.Registers[i+2],
i+3, c.Registers[i+3])
}
fmt.Printf("PC: 0x%04X\n", c.PC)
}