-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincDecOpcodes.go
177 lines (152 loc) · 4.52 KB
/
incDecOpcodes.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
package nmos6502
/*
Decrements & Increments
DEC decrement (memory)
DEX decrement X
DEY decrement Y
INC increment (memory)
INX increment X
INY increment Y
*/
/*
DEC -Decrement Memory by One
M - 1 -> M
N Z C I D V
+ + - - - -
addressing assembler opc bytes cycles
zeropage DEC oper C6 2 5
zeropage,X DEC oper,X D6 2 6
absolute DEC oper CE 3 6
absolute,X DEC oper,X DE 3 7
*/
func decrementMemory(cpu *CPU, addr uint16) {
cpu.Memory[addr]--
}
func incrementMemory(cpu *CPU, addr uint16) {
cpu.Memory[addr]++
}
func (cpu *CPU) opcode0xC6() (byte, bool) { // DEC Zero Page
address := uint16(cpu.fetch()) // Fetch the zero page address
cpu.Memory[address]--
cpu.updateZeroAndNegativeFlags(cpu.Memory[address])
return 5, false // DEC zero page takes 5 cycles
}
func (cpu *CPU) opcode0xD6() (byte, bool) { // DEC Zero Page,X
baseAddress := cpu.fetch() // Fetch the zero page base address
address := uint16((baseAddress + cpu.X) & 0xFF) // Calculate the effective address with zero page wrap-around
cpu.Memory[address]--
cpu.updateZeroAndNegativeFlags(cpu.Memory[address])
return 6, false // DEC zero page,X takes 6 cycles
}
func (cpu *CPU) opcode0xCE() (byte, bool) { // DEC Absolute
low := cpu.fetch() // Fetch the low byte of the address
high := cpu.fetch() // Fetch the high byte of the address
address := uint16(high)<<8 | uint16(low) // Combine to form the full address
cpu.Memory[address]--
cpu.updateZeroAndNegativeFlags(cpu.Memory[address])
return 6, false // DEC absolute takes 6 cycles
}
func (cpu *CPU) opcode0xDE() (byte, bool) { // DEC Absolute,X
low := cpu.fetch() // Fetch the low byte of the address
high := cpu.fetch() // Fetch the high byte of the address
address := uint16(high)<<8 | uint16(low) // Combine to form the full address
effectiveAddress := address + uint16(cpu.X) // Add the X register to the address
cpu.Memory[address]--
cpu.updateZeroAndNegativeFlags(cpu.Memory[effectiveAddress])
return 7, false // DEC absolute,X takes 7 cycles
}
/*
DEX - Decrement Index X by One
X - 1 -> X
N Z C I D V
+ + - - - -
addressing assembler opc bytes cycles
implied DEX CA 1 2
*/
func (cpu *CPU) opcode0xCA() (byte, bool) { // DEX Implied
cpu.X--
cpu.updateZeroAndNegativeFlags(cpu.X)
return 2, false // DEX implied takes 2 cycles
}
/*
DEY
Decrement Index Y by One
Y - 1 -> Y
N Z C I D V
+ + - - - -
addressing assembler opc bytes cycles
implied DEY 88 1 2
*/
func (cpu *CPU) opcode0x88() (byte, bool) { // DEY Implied
cpu.Y--
cpu.updateZeroAndNegativeFlags(cpu.Y)
return 2, false // DEY implied takes 2 cycles
}
/*
INC - Increment Memory by One
M + 1 -> M
N Z C I D V
+ + - - - -
addressing assembler opc bytes cycles
zeropage INC oper E6 2 5
zeropage,X INC oper,X F6 2 6
absolute INC oper EE 3 6
absolute,X INC oper,X FE 3 7
*/
func (cpu *CPU) opcode0xE6() (byte, bool) { // INC Zero Page
address := uint16(cpu.fetch())
incrementMemory(cpu, address)
cpu.updateZeroAndNegativeFlags(cpu.Memory[address])
return 5, false // INC zeropage takes 5 cycles
}
func (cpu *CPU) opcode0xF6() (byte, bool) { // INC Zero Page,X
baseAddress := cpu.fetch()
address := uint16((baseAddress + cpu.X) & 0xFF)
incrementMemory(cpu, address)
cpu.updateZeroAndNegativeFlags(cpu.Memory[address])
return 6, false // INC zeropage,X takes 6 cycles
}
func (cpu *CPU) opcode0xEE() (byte, bool) { // INC Absolute
low := cpu.fetch()
high := cpu.fetch()
address := uint16(high)<<8 | uint16(low)
incrementMemory(cpu, address)
cpu.updateZeroAndNegativeFlags(cpu.Memory[address])
return 6, false // INC absolute takes 6 cycles
}
func (cpu *CPU) opcode0xFE() (byte, bool) { // INC Absolute,X
low := cpu.fetch()
high := cpu.fetch()
address := uint16(high)<<8 | uint16(low)
effectiveAddress := address + uint16(cpu.X)
incrementMemory(cpu, effectiveAddress)
cpu.updateZeroAndNegativeFlags(cpu.Memory[effectiveAddress])
return 7, false // INC absolute,X takes 7 cycles
}
/*
INX - Increment Index X by One
X + 1 -> X
N Z C I D V
+ + - - - -
addressing assembler opc bytes cycles
implied INX E8 1 2
*/
func (cpu *CPU) opcode0xE8() (byte, bool) { // INX Implied
cpu.X++
cpu.updateZeroAndNegativeFlags(cpu.X)
return 2, false // INX implied takes 2 cycles
}
/*
INY
Increment Index Y by One
Y + 1 -> Y
N Z C I D V
+ + - - - -
addressing assembler opc bytes cycles
implied INY C8 1 2
*/
func (cpu *CPU) opcode0xC8() (byte, bool) { // INY Implied
cpu.Y++
cpu.updateZeroAndNegativeFlags(cpu.Y)
return 2, false // INY implied takes 2 cycles
}