-
Notifications
You must be signed in to change notification settings - Fork 19
/
status.go
142 lines (125 loc) · 2.87 KB
/
status.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
// 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 (
"strings"
)
// StatusRegister is the special purpose register that stores the flags of the CPU.
type StatusRegister struct {
Sign bool
Overflow bool
Break bool
DecimalMode bool
InterruptDisable bool
Zero bool
Carry bool
}
// NewStatusRegister is the preferred method of initialisation for the status
// register.
func NewStatusRegister() StatusRegister {
sr := StatusRegister{}
sr.Load(0x00)
return sr
}
// Label returns the canonical name for the status register.
func (sr StatusRegister) Label() string {
return "SR"
}
func (sr StatusRegister) String() string {
s := strings.Builder{}
if sr.Sign {
s.WriteRune('S')
} else {
s.WriteRune('s')
}
if sr.Overflow {
s.WriteRune('V')
} else {
s.WriteRune('v')
}
s.WriteRune('-')
if sr.Break {
s.WriteRune('B')
} else {
s.WriteRune('b')
}
if sr.DecimalMode {
s.WriteRune('D')
} else {
s.WriteRune('d')
}
if sr.InterruptDisable {
s.WriteRune('I')
} else {
s.WriteRune('i')
}
if sr.Zero {
s.WriteRune('Z')
} else {
s.WriteRune('z')
}
if sr.Carry {
s.WriteRune('C')
} else {
s.WriteRune('c')
}
return s.String()
}
// Reset status flags to initial state.
func (sr *StatusRegister) Reset() {
sr.Load(0x00)
}
// Value converts the StatusRegister struct into a value suitable for pushing
// onto the stack.
func (sr StatusRegister) Value() uint8 {
var v uint8
if sr.Sign {
v |= 0x80
}
if sr.Overflow {
v |= 0x40
}
if sr.Break {
v |= 0x10
}
if sr.DecimalMode {
v |= 0x08
}
if sr.InterruptDisable {
v |= 0x04
}
if sr.Zero {
v |= 0x02
}
if sr.Carry {
v |= 0x01
}
// unused bit in the status register is always 1. this doesn't matter when
// we're in normal form but it does matter in uint8 context
v |= 0x20
return v
}
// Load sets the status register flags from an 8 bit integer (which has been
// taken from the stack, for example)
func (sr *StatusRegister) Load(v uint8) {
sr.Sign = v&0x80 == 0x80
sr.Overflow = v&0x40 == 0x40
sr.DecimalMode = v&0x08 == 0x08
sr.InterruptDisable = v&0x04 == 0x04
sr.Zero = v&0x02 == 0x02
sr.Carry = v&0x01 == 0x01
// break flags is always set on loading of value
sr.Break = true
}