-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset_arithmetic.go
More file actions
238 lines (185 loc) · 5.9 KB
/
Copy pathset_arithmetic.go
File metadata and controls
238 lines (185 loc) · 5.9 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package cpu
// The implementation of the add with carry. Decimal mode will be handled automatically
// depending on the decimal mode flag being set and the Core's feature flags for decimal
// mode being set.
//
// This will change flags in the Core it's run in.
func (c *Core) adc_impl(middle byte) {
var u1 = uint16(c.A)
var u2 = uint16(middle)
var result = u1 + u2 + uint16(c.Flags&FLAG_CARRY)
if c.Features.DecimalModeImplemented && c.Flags&FLAG_DECIMAL > 0 {
var lo = (u1 & 0x0F) + (u2 & 0x0F) + uint16(c.Flags&FLAG_CARRY)
var loCarry = 0
if lo > 0x09 {
lo += 6
loCarry = 1
lo = lo & 0x0F
}
var hi = ((u1 & 0xF0) >> 4) + ((u2 & 0xF0) >> 4) + uint16(loCarry)
var hiCarry = 0
if hi > 0x09 {
hi += 6
hiCarry = 1
hi = hi & 0x0F
}
var bcdResult = (hi << 4) | lo
c.A = byte(bcdResult & 0xFF)
if hiCarry > 0 {
c.Flags = c.Flags | FLAG_CARRY
} else {
c.Flags = c.Flags & ^FLAG_CARRY
}
if !c.Features.NMOSDecimalModeFlagBug {
result = bcdResult
}
} else {
c.A = byte(result & 0xFF)
if result&0xFF != result {
c.Flags = c.Flags | FLAG_CARRY
} else {
c.Flags = c.Flags & ^FLAG_CARRY
}
}
if result&0b10000000 > 0 {
c.Flags = c.Flags | FLAG_NEGATIVE
} else {
c.Flags = c.Flags & ^FLAG_NEGATIVE
}
if result == 0 {
c.Flags = c.Flags | FLAG_ZERO
} else {
c.Flags = c.Flags & ^FLAG_ZERO
}
// the fucking overflow flag
if (u1^result)&(u2^result)&0x80 != 0 {
c.Flags = c.Flags | FLAG_OVERFLOW
} else {
c.Flags = c.Flags & ^FLAG_OVERFLOW
}
}
// The implementation of the subtract with borrow.
//
// This will change flags in the Core it's run in.
func (c *Core) sbc_impl(middle byte) {
if c.Features.DecimalModeImplemented && c.Flags&FLAG_DECIMAL > 0 {
c.sbc_impl_decimal(middle)
return
}
var u1 = uint16(c.A)
var u2 = uint16(middle)
var result = (u1 - u2) + uint16(c.Flags&FLAG_CARRY)
opl := c.A
c.A = byte(result & 0xFF)
if result&0xFF != result {
c.Flags = c.Flags & ^FLAG_CARRY
} else {
c.Flags = c.Flags | FLAG_CARRY
}
if result&0b10000000 > 0 {
c.Flags = c.Flags | FLAG_NEGATIVE
} else {
c.Flags = c.Flags & ^FLAG_NEGATIVE
}
if result == 0 {
c.Flags = c.Flags | FLAG_ZERO
} else {
c.Flags = c.Flags & ^FLAG_ZERO
}
// the fucking overflow flag
if (opl^middle)&(opl^c.A)&0x80 != 0 {
c.Flags = c.Flags | FLAG_OVERFLOW
} else {
c.Flags = c.Flags & ^FLAG_OVERFLOW
}
}
// The implementation of the subtract with borrow using BCD.
//
// This will change flags in the Core it's run in.
func (c *Core) sbc_impl_decimal(middle byte) {
var u1 = uint16(c.A)
var u2 = uint16(middle)
var normResult = (u1 - u2) + uint16(1-c.Flags&FLAG_CARRY)
opl := c.A
var lo = (u1 & 0x0F) - (u2 & 0x0F) - uint16(1-c.Flags&FLAG_CARRY)
var loBorrow = lo & 0b10000000 >> 7
lo = lo & 0x0F
if loBorrow > 0 {
lo += 0xA
lo = lo & 0x0F
}
var hi = ((u1 & 0xF0) >> 4) - ((u2 & 0xF0) >> 4) - loBorrow
var hiBorrow = hi & 0b10000000 >> 7
hi = hi & 0x0F
if hiBorrow > 0 {
hi += 0xA
hi = hi & 0x0F
}
var result = (hi << 4) | lo
c.A = byte(result & 0xFF)
if !c.Features.NMOSDecimalModeFlagBug {
normResult = result
}
if hiBorrow > 0 {
c.Flags = c.Flags & ^FLAG_CARRY
} else {
c.Flags = c.Flags | FLAG_CARRY
}
if normResult&0b10000000 > 0 {
c.Flags = c.Flags | FLAG_NEGATIVE
} else {
c.Flags = c.Flags & ^FLAG_NEGATIVE
}
if normResult == 0 {
c.Flags = c.Flags | FLAG_ZERO
} else {
c.Flags = c.Flags & ^FLAG_ZERO
}
// the fucking overflow flag
if (opl^middle)&(opl^byte(normResult&0xFF))&0x80 != 0 {
c.Flags = c.Flags | FLAG_OVERFLOW
} else {
c.Flags = c.Flags & ^FLAG_OVERFLOW
}
}
// Add with Carry - Absolute
func (c *Core) ADC____a(addr uint16) { c.PC += 3; c.adc_impl(c.Memory[addr]) }
// Add with Carry - Absolute indexed with X
func (c *Core) ADC___ax(addr uint16) { c.PC += 3; c.adc_impl(c.Memory[addr+uint16(c.X)]) }
// Add with Carry - Absolute indexed with Y
func (c *Core) ADC___ay(addr uint16) { c.PC += 3; c.adc_impl(c.Memory[addr+uint16(c.Y)]) }
// Add with Carry - Immediate
func (c *Core) ADC__Imm(literal byte) { c.PC += 2; c.adc_impl(literal) }
// Add with Carry - Zero Page
func (c *Core) ADC__ZPg(zp byte) { c.PC += 2; c.adc_impl(c.Memory[zp]) }
// Add with Carry - Zero Page Indexed Indirect
func (c *Core) ADC_IZPx(zp byte) { c.PC += 2; c.adc_impl(c.Memory[c.indirectZpX(zp)]) }
// Add with Carry - Zero Page indexed with X
func (c *Core) ADC__ZPx(zp byte) { c.PC += 2; c.adc_impl(c.Memory[(zp+c.X)&0xFF]) }
// Add with Carry - Zero Page Indirect Indexed with Y
func (c *Core) ADC_IZPy(zp byte) { c.PC += 2; c.adc_impl(c.Memory[c.indirectZpY(zp)]) }
// Subtract with Borrow - Absolute
func (c *Core) SBC____a(addr uint16) { c.PC += 3; c.sbc_impl(c.Memory[addr]) }
// Subtract with Borrow - Absolute indexed with X
func (c *Core) SBC___ax(addr uint16) { c.PC += 3; c.sbc_impl(c.Memory[addr+uint16(c.X)]) }
// Subtract with Borrow - Absolute indexed with Y
func (c *Core) SBC___ay(addr uint16) { c.PC += 3; c.sbc_impl(c.Memory[addr+uint16(c.Y)]) }
// Subtract with Borrow - Immediate
func (c *Core) SBC__Imm(literal byte) { c.PC += 2; c.sbc_impl(literal) }
// Subtract with Borrow - Zero Page
func (c *Core) SBC__Zpg(zp byte) { c.PC += 2; c.sbc_impl(c.Memory[zp]) }
// Subtract with Borrow - Zero Page Indexed Indirect
func (c *Core) SBC_IZPx(zp byte) { c.PC += 2; c.sbc_impl(c.Memory[c.indirectZpX(zp)]) }
// Subtract with Borrow - Zero Page indexed with X
func (c *Core) SBC__ZPx(zp byte) { c.PC += 2; c.sbc_impl(c.Memory[(zp+c.X)&0xFF]) }
// Subtract with Borrow - Zero Page Indirect Indexed with Y
func (c *Core) SBC_IZPy(zp byte) { c.PC += 2; c.sbc_impl(c.Memory[c.indirectZpY(zp)]) }
// 65c02 Instructions/Implementations below this line
// Add with Carry - Zero Page Indirect
//
// CMOS 65c02
func (c *Core) ADC__IZP(zp uint8) { c.PC += 2; c.adc_impl(byte(c.indirectZp(zp))) }
// Subtract with Borrow - Zero Page Indirect
//
// CMOS 65c02
func (c *Core) SBC__IZP(zp uint8) { c.PC += 2; c.sbc_impl(byte(c.indirectZp(zp))) }