-
Notifications
You must be signed in to change notification settings - Fork 19
/
register.go
181 lines (152 loc) · 4.54 KB
/
register.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
// This file is part of Gopher2600.
//
// Gopher2600 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Gopher2600 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Gopher2600. If not, see <https://www.gnu.org/licenses/>.
package registers
import (
"fmt"
)
// Register is an array of of type bit, used for register representation.
type Register struct {
label string
value uint8
onLoad func(val uint8)
}
// NewRegister creates a new register of a givel size and name, and initialises
// the value.
func NewRegister(val uint8, label string) Register {
return Register{
value: val,
label: label,
}
}
// SetOnLoad sets the callback function for a load register event.
func (r *Register) SetOnLoad(onLoad func(uint8)) {
r.onLoad = onLoad
}
// NewAnonRegister initialises a new register without a name.
func NewAnonRegister(val uint8) Register {
return NewRegister(val, "")
}
// Label returns the registers label (or ID).
func (r Register) Label() string {
return r.label
}
// returns value as a string in hexadecimal notation.
func (r Register) String() string {
return fmt.Sprintf("%02x", r.value)
}
// Value returns the current value of the register.
func (r Register) Value() uint8 {
return r.value
}
// BitWidth returns the number of bits used to store the register value.
func (r Register) BitWidth() int {
return 8
}
// Address returns the current value of the register /as a uint16/. this is
// useful when you want to use the register value in an address context.
//
// for example, the stack pointer stores page zero addresses - which can be
// stored in just 8bits but which are always interpreted as 16bit value.
func (r Register) Address() uint16 {
return uint16(r.value)
}
// IsNegative checks the sign bit of the register.
func (r Register) IsNegative() bool {
return r.value&0x80 == 0x80
}
// IsZero checks if register is zero.
func (r Register) IsZero() bool {
return r.value == 0
}
// IsBitV returns the state of the second MSB.
func (r Register) IsBitV() bool {
return r.value&0x40 == 0x40
}
// Load value into register.
func (r *Register) Load(val uint8) {
r.value = val
if r.onLoad != nil {
r.onLoad(val)
}
}
// Add value to register. Returns carry and overflow states.
func (r *Register) Add(val uint8, carry bool) (rcarry bool, overflow bool) {
// note value of register before we change it
v := r.value
r.value += val
if carry {
r.value++
}
// overflow detection from Ken Shirriff's blog: "The 6502 overflow flag
// explained mathematically"
overflow = ((v ^ r.value) & (val ^ r.value) & 0x80) != 0
// carry detection
if v == r.value {
rcarry = carry
} else {
rcarry = r.value < v
}
return rcarry, overflow
}
// Subtract value from register. Returns carry and overflow states.
func (r *Register) Subtract(val uint8, carry bool) (rcarry bool, overflow bool) {
return r.Add(^val, carry)
}
// AND value with register.
func (r *Register) AND(val uint8) {
r.value &= val
}
// ASL (arithmetic shift left) shifts register one bit to the left. Returns
// the most significant bit as it was before the shift. If we think of the
// ASL operation as a multiply by two then the return value is the carry bit.
func (r *Register) ASL() bool {
carry := r.IsNegative()
r.value <<= 1
return carry
}
// EOR (exclusive or) value with register.
func (r *Register) EOR(val uint8) {
r.value ^= val
}
// LSR (logical shift right) shifts register one bit to the right.
// the least significant bit as it was before the shift. If we think of
// the ASL operation as a division by two then the return value is the carry bit.
func (r *Register) LSR() bool {
carry := r.value&1 == 1
r.value >>= 1
return carry
}
// ORA (non-exclusive or) value with register.
func (r *Register) ORA(val uint8) {
r.value |= val
}
// ROL rotates register 1 bit to the left. Returns new carry status.
func (r *Register) ROL(carry bool) bool {
rcarry := r.IsNegative()
r.value <<= 1
if carry {
r.value |= 1
}
return rcarry
}
// ROR rotates register 1 bit to the right. Returns new carry status.
func (r *Register) ROR(carry bool) bool {
rcarry := r.value&1 == 1
r.value >>= 1
if carry {
r.value |= 0x80
}
return rcarry
}