-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstructions.go
65 lines (56 loc) · 1.35 KB
/
instructions.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
package main
// the most significant nibble is all that's
// used to identify an instruction
const (
NOP uint8 = 0x00
LDA uint8 = 0x10
ADD uint8 = 0x20
JMP uint8 = 0x30
OUT uint8 = 0xE0
HALT uint8 = 0xF0
)
var instructionNames = map[uint8]string{
NOP: "NOP",
LDA: "LDA",
ADD: "ADD",
JMP: "ADD",
OUT: "OUT",
HALT: "HALT",
}
// instructionMap is a lookup table that defines
// how the control flags should be set for each
// cycle. Instructions can take a variable number
// of cycles.
var instructionMap = map[uint8][]int{
// load from an address RAM into the A register
LDA: {
IO | MI, // instruction register to memory address register
RO | AI, // RAM to A register
},
// add the number from an address in RAM to the A register,
// via the B register
ADD: {
IO | MI, // instruction register to memory address register
RO | BI, // RAM to B register
ZO | AI, // sum to A register
},
// set the program counter to the least significant
// nibble of the instruction register
JMP: {
IO | J,
},
// load from the A register to the output register
OUT: {
AO | OI, // A register to output register
},
// halt the CPU
HALT: {
HLT, // Halt :)
},
}
// op is a convenience function for combining an
// instruction and an argument. E.g.
// op(LDA, 15) -> 0x1E
func op(instr uint8, arg uint8) uint8 {
return instr | (arg & 0x0F)
}