-
Notifications
You must be signed in to change notification settings - Fork 0
/
Addressing.c
162 lines (131 loc) · 5.11 KB
/
Addressing.c
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
#include "Cpu6502.h"
/*
All addressing modes are static to mimic private methods that are
not needed in the public interface of Cpu6502.
This file is to be included in CpuStep.c
Instructions as seen in http://www.obelisk.demon.co.uk/6502/
*_adr functions return an address for writing operations like STA
other funcionts return the value in the specified memory address for reading operations like LDA
*/
#define get_operand2_high() ( cpu->read_memory[cpu->pc + 2]( cpu->sys, cpu->pc + 2 ) <<8 )
// -------------------------------------------------------------------------------
static word Absolute_adr( Cpu6502 *cpu, byte address_lowbyte ) // STA $HHHH
{
word address = address_lowbyte;
address |= get_operand2_high();
cpu->pc += 3;
#ifdef _Cpu6502_Disassembler
cpu->disasm.address = address;
cpu->disasm.value = cpu->read_memory_disasm( cpu->sys, address );
#endif
return address;
}
// -------------------------------------------------------------------------------
static byte Absolute( Cpu6502 *cpu, byte address_lowbyte ) // LDA $HHHH
{
word address = address_lowbyte;
address |= get_operand2_high();
cpu->pc += 3;
#ifdef _Cpu6502_Disassembler
cpu->disasm.address = address;
cpu->disasm.value = cpu->read_memory_disasm( cpu->sys, address );
#endif
return cpu->read_memory[address]( cpu->sys, address );
}
// -------------------------------------------------------------------------------
static word Absolute_Indexed_adr( Cpu6502 *cpu, byte address_lowbyte, byte index ) // STA $HHHH,X
{
word address = address_lowbyte + index;
if( address > 0xFF ) {
cpu->addressing_page_cross = 1;
}
address += get_operand2_high();
cpu->pc += 3;
#ifdef _Cpu6502_Disassembler
cpu->disasm.address = address;
cpu->disasm.value = cpu->read_memory_disasm( cpu->sys, address );
#endif
return address;
}
// -------------------------------------------------------------------------------
static byte Absolute_Indexed( Cpu6502 *cpu, byte address_lowbyte, byte index ) // LDA $HHHH,Y
{
word address = address_lowbyte + index;
if( address > 0xFF ) {
cpu->addressing_page_cross = 1;
}
address += get_operand2_high();
cpu->pc += 3;
#ifdef _Cpu6502_Disassembler
cpu->disasm.address = address;
cpu->disasm.value = cpu->read_memory_disasm( cpu->sys, address );
#endif
return cpu->read_memory[address]( cpu->sys, address );
}
// -------------------------------------------------------------------------------
static word Indexed_Indirect_X_adr( Cpu6502 *cpu, byte base ) // STA ($HH,X)
{
cpu->pc += 2;
byte pointer = ((byte)(base + cpu->x ));
word address = cpu->read_memory[pointer]( cpu->sys, pointer );
pointer++;
address |= cpu->read_memory[pointer]( cpu->sys, pointer ) <<8;
#ifdef _Cpu6502_Disassembler
cpu->disasm.temp_address = (byte)(pointer-1);
cpu->disasm.address = address;
cpu->disasm.value = cpu->read_memory_disasm( cpu->sys, address );
#endif
return address;
}
// -------------------------------------------------------------------------------
static byte Indexed_Indirect_X( Cpu6502 *cpu, byte base ) // LDA ($HH,X)
{
cpu->pc += 2;
byte pointer = ((byte)(base + cpu->x ));
word address = cpu->read_memory[pointer]( cpu->sys, pointer );
pointer++;
address |= cpu->read_memory[pointer]( cpu->sys, pointer ) <<8;
#ifdef _Cpu6502_Disassembler
cpu->disasm.temp_address = (byte)(pointer-1);
cpu->disasm.address = address;
cpu->disasm.value = cpu->read_memory_disasm( cpu->sys, address );
#endif
return cpu->read_memory[address]( cpu->sys, address );
}
// -------------------------------------------------------------------------------
static word Indirect_Indexed_Y_adr( Cpu6502 *cpu, byte base ) // STA ($HH),Y
{
cpu->pc += 2;
word address = cpu->read_memory[base]( cpu->sys, base ) + cpu->y;
if( address > 0xFF ) {
cpu->addressing_page_cross = 1;
}
base++;
address += ( cpu->read_memory[base]( cpu->sys, base ) <<8 );
#ifdef _Cpu6502_Disassembler
cpu->disasm.temp_address = cpu->read_memory_disasm( cpu->sys, (byte)(base-1) );
cpu->disasm.temp_address |= cpu->read_memory_disasm( cpu->sys, base ) <<8;
cpu->disasm.address = address;
cpu->disasm.value = cpu->read_memory_disasm( cpu->sys, address );
#endif
return address;
}
// -------------------------------------------------------------------------------
static byte Indirect_Indexed_Y( Cpu6502 *cpu, byte base ) // LDA ($HH),Y
{
cpu->pc += 2;
word address = cpu->read_memory[base]( cpu->sys, base ) + cpu->y;
if( address > 0xFF ) {
cpu->addressing_page_cross = 1;
}
base++;
address += ( cpu->read_memory[base]( cpu->sys, base ) <<8 );
#ifdef _Cpu6502_Disassembler
cpu->disasm.temp_address = cpu->read_memory_disasm( cpu->sys, (byte)(base-1) );
cpu->disasm.temp_address |= cpu->read_memory_disasm( cpu->sys, base ) <<8;
cpu->disasm.address = address;
cpu->disasm.value = cpu->read_memory_disasm( cpu->sys, address );
#endif
return cpu->read_memory[address]( cpu->sys, address );
}
// -------------------------------------------------------------------------------