-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPU_6502_ROR_RW.go
129 lines (95 loc) · 3.94 KB
/
CPU_6502_ROR_RW.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
package CPU_6502
import "fmt"
// ROR Rotate One Bit Right (Memory or Accumulator)
//
// C -> [76543210] -> C N Z C I D V
// + + + - - -
//
// addressing assembler opc bytes cyles
// --------------------------------------------
// accumulator ROR A 6A 1 2
// zeropage ROR oper 66 2 5
// zeropage,X ROR oper,X 76 2 6
// absolute ROR oper 6E 3 6
// absolute,X ROR oper,X 7E 3 7
// Move each of the bits in either A or M one place to the right.
// Bit 7 is filled with the current value of the carry flag whilst the old bit 0 becomes the new carry flag value.
// ------------------------------------ Accumulator ------------------------------------ //
func opc_ROR_A(bytes uint16, opc_cycles byte) {
// Update Global Opc_cycles value
Opc_cycles = opc_cycles
// Print internal opcode cycle
debugInternalOpcCycle(opc_cycles)
// Just increment the Opcode cycle Counter
if Opc_cycle_count < opc_cycles {
Opc_cycle_count++
// After spending the cycles needed, execute the opcode
} else {
// Keep original Accumulator value for debug
original_A := A
original_carry := P[0]
// Keep the original bit 0 from Accumulator to be used as new Carry
new_Carry := A & 0x01
// Shift Right Accumulator
A = A >> 1
// Bit 7 is filled with the current value of the carry flag
A += (P[0] << 7)
// Print Opcode Debug Message
opc_ROR_A_DebugMsg(bytes, original_A, original_carry)
flags_C(new_Carry) // The old bit 0 becomes the new carry flag value
flags_N(A)
flags_Z(A)
// Increment PC
PC += bytes
// Reset Opcode Cycle counter
Opc_cycle_count = 1
}
}
func opc_ROR_A_DebugMsg(bytes uint16, original_A byte, original_carry byte) {
if Debug {
opc_string := debug_decode_opc(bytes)
dbg_show_message = fmt.Sprintf("\n\tOpcode %s [Mode: Accumulator]\tROR Rotate One Bit Right.\tA(%d) Roll Right 1 bit\t(%d) + Current Carry(%d) as new bit 7.\tA = %d\n", opc_string, original_A, original_A>>1, original_carry, A)
fmt.Println(dbg_show_message)
}
}
// --------------------------------------- Memory -------------------------------------- //
func opc_ROR(memAddr uint16, mode string, bytes uint16, opc_cycles byte) {
// Update Global Opc_cycles value
Opc_cycles = opc_cycles
// Print internal opcode cycle
debugInternalOpcCycle(opc_cycles)
// Just increment the Opcode cycle Counter
if Opc_cycle_count < opc_cycles {
Opc_cycle_count++
// After spending the cycles needed, execute the opcode
} else {
// Read data from Memory (adress in Memory Bus) into Data Bus
memData := dataBUS_Read(memAddr)
// Keep original Accumulator value for debug
original_MemValue := memData
original_carry := P[0]
// Keep the original bit 0 from Accumulator to be used as new Carry
new_Carry := memData & 0x01
// Write data to Memory (adress in Memory Bus) and update the value in Data BUS
// Shift Right Memory Value
memData = dataBUS_Write(memAddr, memData>>1)
// Bit 7 is filled with the current value of the carry flag
memData = dataBUS_Write(memAddr, memData+(P[0]<<7))
// Print Opcode Debug Message
opc_ROR_DebugMsg(bytes, mode, memAddr, original_MemValue, original_carry, memData)
flags_C(new_Carry) // The old bit 0 becomes the new carry flag value
flags_N(memData)
flags_Z(memData)
// Increment PC
PC += bytes
// Reset Internal Opcode Cycle counters
resetIntOpcCycleCounters()
}
}
func opc_ROR_DebugMsg(bytes uint16, mode string, memAddr uint16, original_MemValue byte, original_carry byte, memData byte) {
if Debug {
opc_string := debug_decode_opc(bytes)
dbg_show_message = fmt.Sprintf("\n\tOpcode %s [Mode: %s]\tROR Rotate One Bit Right.\tMemory[0x%02d](%d) Roll Right 1 bit\t(%d) + Current Carry(%d) as new bit 7.\tA = %d\n", opc_string, mode, memAddr, original_MemValue, original_MemValue>>1, original_carry, memData)
fmt.Println(dbg_show_message)
}
}