-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.go
113 lines (95 loc) · 2.35 KB
/
helpers.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
package i8080
// 16-bit registers helpers
// 16-bit register aliases
const (
BC = B
DE = D
HL = H
R4 = M
)
// GetR16 gets a 16-bit register pair by index
func (c *CPU) GetR16(reg uint8) uint16 {
switch reg {
case BC:
return c.BC()
case DE:
return c.DE()
case HL:
return c.HL()
default:
panic("invalid 16-bit register")
}
}
// SetR16 sets a 16-bit register pair by index
func (c *CPU) SetR16(reg uint8, val uint16) {
switch reg {
case BC:
c.SetBC(val)
case DE:
c.SetDE(val)
case HL:
c.SetHL(val)
default:
panic("invalid 16-bit register")
}
}
// BC returns the value of the BC register pair
func (c *CPU) BC() uint16 {
return uint16(c.Registers[B])<<8 | uint16(c.Registers[C])
}
// SetBC sets the value of the BC register pair
func (c *CPU) SetBC(v uint16) {
c.Registers[B] = uint8(v >> 8)
c.Registers[C] = uint8(v)
}
// DE returns the value of the DE register pair
func (c *CPU) DE() uint16 {
return uint16(c.Registers[D])<<8 | uint16(c.Registers[E])
}
// SetDE sets the value of the DE register pair
func (c *CPU) SetDE(v uint16) {
c.Registers[D] = uint8(v >> 8)
c.Registers[E] = uint8(v)
}
// HL returns the value of the HL register pair
func (c *CPU) HL() uint16 {
return uint16(c.Registers[H])<<8 | uint16(c.Registers[L])
}
// SetHL sets the value of the HL register pair
func (c *CPU) SetHL(v uint16) {
c.Registers[H] = uint8(v >> 8)
c.Registers[L] = uint8(v)
}
// PSW gets the value of the PSW register pair
func (c *CPU) PSW() uint16 {
return uint16(c.Registers[A])<<8 | uint16(c.Flags)
}
// SetPSW sets the value of the PSW register pair
// The bits of the flag register that are always 1 or always 0 will be forced to those values
func (c *CPU) SetPSW(v uint16) {
c.Registers[A] = uint8(v >> 8)
c.Flags = flags(v)
c.Flags |= FlagBit1
c.Flags &= ^(FlagBit3 | FlagBit5)
}
// PUSH/POP and 16-bit memory helpers
// Push pushes a value to the stack
func (c *CPU) Push(v uint16) {
c.SP -= 2
c.Write16(c.SP, v)
}
// Pop pops a value from the stack
func (c *CPU) Pop() uint16 {
v := c.Read16(c.SP)
c.SP += 2
return v
}
// Write16 writes a 16-bit value `v` at address `addr`
func (c *CPU) Write16(addr uint16, v uint16) {
c.Memory[addr+1] = uint8(v >> 8)
c.Memory[addr] = uint8(v)
}
// Read16 reads a 16-bit value from address `addr`
func (c *CPU) Read16(addr uint16) uint16 {
return uint16(c.Memory[addr+1])<<8 | uint16(c.Memory[addr])
}