-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.go
107 lines (88 loc) · 1.52 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
package main
import "fmt"
type Cpu struct {
// Registers
pc, sp int // Program counter, Stack pointer
a int // Accumulator, Flag
b, c, d, e, h, l int // Auxiliary registers
p FlagReg
// Memory
m Memory
// Next instruction to execute
nextInstr Instruction
}
type FlagReg struct {
z, n, h, c int
}
const (
A = iota
B
C
D
E
H
L
BC
HL
DE
SP
)
func (f *FlagReg) toInt() int {
return f.z<<7 | f.n<<6 | f.h<<5 | f.c<<4
}
func fromInt(n int) FlagReg {
f := FlagReg{
z: (n & BIT_7) >> 7,
n: (n & BIT_6) >> 6,
h: (n & BIT_5) >> 5,
c: (n & BIT_4) >> 4,
}
return f
}
type Mem interface {
Read(addr int) int
Write(val, addr int)
}
const (
BIT_0 = 1 << iota
BIT_1
BIT_2
BIT_3
BIT_4
BIT_5
BIT_6
BIT_7
)
func (cpu *Cpu) tick() {
// Read current opcode from memory
// Decode instruction
// Build Instruction object
// Place it as current instruction inside CPU
// Call corresponding method
}
// Fetches the next instruction
func (cpu *Cpu) fetch() int {
opcode := cpu.m.Read(cpu.pc)
cpu.pc++
return opcode
}
// Creates a new instruction from the given opcode,
// containing the operands (if any)
func (cpu *Cpu) decode(opcode int) {
instr := instructionSet[opcode]
for i := 0; i < instr.size-1; i++ {
instr.operands[i] = cpu.m.Read(cpu.pc)
cpu.pc++
}
cpu.nextInstr = instr
}
func (cpu *Cpu) nop() {
fmt.Println("NOP")
}
//func (cpu *Cpu) ld_n_nn(reg *int, val int) {
// *reg = val
//}
//
//func (cpu *Cpu) ld_r1_r2(reg1, reg2 *int) {
// *reg1 = *reg2
//}