-
Notifications
You must be signed in to change notification settings - Fork 4
/
cpu.go
414 lines (366 loc) · 10.1 KB
/
cpu.go
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package main
import (
"fmt"
"log"
"time"
)
// RunState specifies the current running state of the Processor.
type RunState uint32
const (
// RunStateStopped indicates that the Processor is no longer incrementing the
// program counter and executing instructions.
RunStateStopped RunState = iota
// RunStateRunning indicates that the Processor is currently executing
// instructions until interrupted.
RunStateRunning
)
// CPU is a Processor to emulate the LC-3 CPU.
type CPU struct {
Reg [8]uint16 // registers
PC uint16 // Program Counter
Memory [65536]uint16 // CPU Memory
CondRegister *CondRegister // Condition Flags Register
keyBuffer []rune // Key Buffer
TimerStarted bool
TimerStart time.Time
DebugMode bool
OP uint16 // current opcode
runState RunState // current state
}
// CondRegister stores the state of the CPU condition flags register.
type CondRegister struct {
P bool // Sign (S), set if the result is negative.
Z bool // Zero (Z), set if the result is zero.
N bool // Parity (P), set if the number of 1 bits in the result is even.
}
// Memory Mapped Registers
const (
// Keyboard status
MemRegKBSR uint16 = 0xFE00
// Keyboard data
MemRegKBDR uint16 = 0xFE02
)
// List of OpCodes
const (
OpBR uint16 = iota // 0: branch
OpADD // 1: add
OpLD // 2: load
OpST // 3: store
OpJSR // 4: jump register
OpAND // 5: bitwise and
OpLDR // 6: load register
OpSTR // 7: store register
OpRTI // 8: unused
OpNOT // 9: bitwise not
OpLDI // 10: load indirect
OpSTI // 11: store indrect
OpJMP // 12: jump
OpRES // 13: reserved (unused)
OpLEA // 14: load effective address
OpTRAP // 15: execute trap
)
// List of Trap codes
const (
TrapGETC uint16 = 0x20 // get character from keyboard
TrapOUT uint16 = 0x21 // output a character
TrapPUTS uint16 = 0x22 // output a word string
TrapIN uint16 = 0x23 // input a string
TrapPUTSP uint16 = 0x24 // output a byte string
TrapHALT uint16 = 0x25 // halt the program
)
// NewCPU creates a new instance of the CPU
func NewCPU() *CPU {
cpu := CPU{}
return &cpu
}
// Run executes any program loaded into memory, starting from the program
// counter value, running until completion.
func (c *CPU) Run() (err error) {
if len(c.Memory) == 0 {
return errNoProgram
}
for {
err = c.Step()
if err != nil || c.runState == RunStateStopped {
//break
return
}
}
}
// Reset the CPU
func (c *CPU) Reset() {
// set the PC to the starting position
// 0x3000 is the default
c.PC = 0x3000
// Reset the condition register flags
c.CondRegister = &CondRegister{}
}
// Step executes the program loaded into memory
func (c *CPU) Step() (err error) {
c.runState = RunStateRunning
// Process any key presses since last time
c.ProcessInput()
// Process the current instruction
c.EmulateInstruction()
// Increment MCC
c.Memory[0xFFFF]++
return
}
// Stop instructs the processor to stop processing instructions.
func (c *CPU) Stop() (err error) {
c.runState = RunStateStopped
return
}
// ProcessInput handles keyboard input
func (c *CPU) ProcessInput() (err error) {
kbsrVal := c.ReadMemory(MemRegKBSR)
kbsrReady := ((kbsrVal & 0x8000) == 0)
if kbsrReady && len(c.keyBuffer) > 0 {
c.WriteMemory(MemRegKBSR, kbsrVal|0x8000)
c.WriteMemory(MemRegKBDR, uint16(c.keyBuffer[0]))
}
return
}
// ReadMemory reads an address from memory
func (c *CPU) ReadMemory(address uint16) uint16 {
//log.Printf("Reading memory address: 0x%04X", address)
if address == MemRegKBDR {
c.WriteMemory(MemRegKBSR, c.ReadMemory(MemRegKBSR)&0x7FFF)
}
switch {
case address <= 65535:
//log.Printf("Value is: %d", c.Memory[address])
return uint16(c.Memory[address])
default:
log.Fatalf("unhandled cpu memory read at address: 0x%04X", address)
}
return 0
}
// WriteMemory writes to an address in memory
func (c *CPU) WriteMemory(address uint16, value uint16) {
switch {
case address <= 65535:
c.Memory[address] = value
default:
log.Fatalf("unhandled cpu memory write at address: 0x%04X", address)
}
}
// EmulateInstruction emulates the LC-3 instruction
func (c *CPU) EmulateInstruction() (err error) {
var pc uint16 = c.PC + 1
instr := c.ReadMemory(c.PC)
op := instr >> 12
if c.DebugMode {
log.Println("Op code: ", op)
}
// process the current opcode
switch op {
case OpBR:
n := extract1C(instr, 11, 11) == 1
z := extract1C(instr, 10, 10) == 1
p := extract1C(instr, 9, 9) == 1
PCoffset9 := extract2C(instr, 8, 0)
if c.DebugMode {
brString := fmt.Sprintf("0x%04x: BR", c.PC)
if n {
brString += fmt.Sprintf("n")
}
if z {
brString += fmt.Sprintf("z")
}
if p {
brString += fmt.Sprintf("p")
}
brString += fmt.Sprintf(" #%d\n", int16(PCoffset9))
log.Println(brString)
}
if (n && c.CondRegister.N) || (z && c.CondRegister.Z) || (p && c.CondRegister.P) {
pc += PCoffset9
}
case OpJMP:
baseR := extract1C(instr, 8, 6)
pc = c.Reg[baseR]
case OpADD:
dr := extract1C(instr, 11, 9)
sr1 := extract1C(instr, 8, 6)
bit5 := extract1C(instr, 5, 5)
if bit5 == 1 {
imm5 := extract2C(instr, 4, 0)
//log.Println(fmt.Sprintf("0x%04x: ADD R%d,R%d,#%d\n", c.PC, dr, sr1, int16(imm5)))
c.Reg[dr] = c.Reg[sr1] + imm5
} else {
sr2 := extract1C(instr, 2, 0)
//log.Println(fmt.Sprintf("0x%04x: ADD R%d,R%d,R%d\n", c.PC, dr, sr1, sr2))
c.Reg[dr] = c.Reg[sr1] + c.Reg[sr2]
}
c.SetCC(c.Reg[dr])
case OpAND:
dr := extract1C(instr, 11, 9)
sr1 := extract1C(instr, 8, 6)
bit5 := extract1C(instr, 5, 5)
if bit5 == 1 {
imm5 := extract2C(instr, 4, 0)
c.Reg[dr] = c.Reg[sr1] & imm5
} else {
sr2 := extract1C(instr, 2, 0)
c.Reg[dr] = c.Reg[sr1] & c.Reg[sr2]
}
c.SetCC(c.Reg[dr])
case OpNOT:
dr := extract1C(instr, 11, 9)
sr := extract1C(instr, 8, 6)
c.Reg[dr] = ^c.Reg[sr]
c.SetCC(c.Reg[dr])
case OpLD:
dr := extract1C(instr, 11, 9)
PCoffset9 := extract2C(instr, 8, 0)
c.Reg[dr] = c.ReadMemory(pc + PCoffset9)
c.SetCC(c.Reg[dr])
//log.Println(fmt.Sprintf("0x%04x: LD R%d,%d", c.PC, dr, PCoffset9))
case OpLDI:
dr := extract1C(instr, 11, 9)
PCoffset9 := extract2C(instr, 8, 0)
addr := c.ReadMemory(pc + PCoffset9)
c.Reg[dr] = c.ReadMemory(addr)
c.SetCC(c.Reg[dr])
//log.Println(fmt.Sprintf("0x%04x: LDI R%d,0x%04x", c.PC, dr, addr))
case OpJSR:
bit11 := extract1C(instr, 11, 11)
c.Reg[7] = pc
if bit11 == 1 {
PCoffset11 := extract2C(instr, 10, 0)
pc += PCoffset11
//log.Println(fmt.Sprintf("0x%04x: JSR BIT1 0x%04x,0x%04x", c.PC, c.Reg[7], pc))
} else {
baseR := extract1C(instr, 8, 6)
pc = c.Reg[baseR]
//log.Println(fmt.Sprintf("0x%04x: JSR BASER 0x%04x,0x%04x", c.PC, c.Reg[7], baseR))
}
case OpLDR:
dr := extract1C(instr, 11, 9)
baseR := extract1C(instr, 8, 6)
offset6 := extract2C(instr, 5, 0)
c.Reg[dr] = c.ReadMemory(c.Reg[baseR] + offset6)
c.SetCC(c.Reg[dr])
//log.Println(fmt.Sprintf("0x%04x: LDR R%d,R%d 0x%04x", c.PC, dr, baseR, offset6))
case OpLEA:
dr := extract1C(instr, 11, 9)
PCoffset9 := extract2C(instr, 8, 0)
c.Reg[dr] = pc + PCoffset9
c.SetCC(c.Reg[dr])
//log.Println(fmt.Sprintf("0x%04x: LEA R%d,%d", c.PC, dr, PCoffset9))
case OpST:
sr := extract1C(instr, 11, 9)
PCoffset9 := extract2C(instr, 8, 0)
c.WriteMemory(pc+PCoffset9, c.Reg[sr])
//log.Println(fmt.Sprintf("0x%04x: ST R%d,%d", c.PC, sr, PCoffset9))
case OpSTI:
sr := extract1C(instr, 11, 9)
PCoffset9 := extract2C(instr, 8, 0)
c.WriteMemory(c.ReadMemory(pc+PCoffset9), c.Reg[sr])
case OpSTR:
sr := extract1C(instr, 11, 9)
baseR := extract1C(instr, 8, 6)
offset6 := extract2C(instr, 5, 0)
c.WriteMemory(c.Reg[baseR]+offset6, c.Reg[sr])
//log.Println(fmt.Sprintf("0x%04x: STR R%d 0x%04x,0x%04x", c.PC, sr, c.Reg[baseR]+offset6, c.Reg[sr]))
case OpTRAP:
trapCode := instr & 0xFF
switch trapCode {
case TrapGETC:
// block until a key is pressed
for {
if len(c.keyBuffer) > 0 {
break
}
}
// pop one key from the queue (x, a = a[0], a[1:]) into register 0
c.Reg[0], c.keyBuffer = uint16(c.keyBuffer[0]), c.keyBuffer[1:]
case TrapOUT:
chr := rune(c.Reg[0])
fmt.Printf("%c", chr)
case TrapPUTS:
address := c.Reg[0]
var chr uint16
var i uint16
for ok := true; ok; ok = (chr != 0x0) {
chr = c.Memory[address+i] & 0xFFFF
fmt.Printf("%c", rune(chr))
i++
}
case TrapHALT:
if c.DebugMode {
log.Println("HALT")
}
c.Stop()
default:
log.Fatalf("Trap code not implemented: 0x%04X", instr)
}
case OpRES:
case OpRTI:
default:
log.Fatalf("Bad Op Code received: %d", op)
}
// increment the program counter
//log.Println(fmt.Sprintf("Setting PC to 0x%04x", pc))
c.PC = pc
return
}
func printBytes(s string) {
fmt.Println("printBytes:")
sbytes := []byte(s)
for i, b := range sbytes {
// Print the position of the byte in the string
// and the integer value of the byte in hexadecimal.
fmt.Printf("\t%2d: %2X\n", i, b)
}
}
func (c *CPU) SetCC(data uint16) {
c.CondRegister.N = isNegative(data)
c.CondRegister.Z = isZero(data)
c.CondRegister.P = isPositive(data)
}
func isPositive(data uint16) bool {
return int16(data) > 0
}
func isZero(data uint16) bool {
return data == 0
}
func isNegative(data uint16) bool {
return int16(data) < 0
}
func extract1C(inst uint16, hi, lo int) uint16 {
if hi >= 16 || hi < 0 || lo >= 16 || lo < 0 {
log.Println("Argument out of bounds")
}
//Build mask
mask := uint16(0)
for i := 0; i <= hi-lo; i++ {
mask = mask << 1
mask |= 0x0001
}
for i := 0; i < lo; i++ {
mask = mask << 1
}
//fmt.Printf("Mask %04x ", mask)
//Apply mask
field := inst & mask
//Shift field down
field = field >> uint(lo)
//fmt.Printf("Field %04x\n", field)
return field
}
func extract2C(inst uint16, hi, lo int) uint16 {
field := extract1C(inst, hi, lo)
if extract1C(field, hi, hi) == 1 {
//Build sign extension
mask := uint16(0)
for i := 0; i <= 15-hi; i++ {
mask = mask << 1
mask |= 0x0001
}
mask = mask << uint(hi)
field = inst | mask
}
return field
}