summaryrefslogtreecommitdiff
path: root/addressing.h
blob: 300dac5c9b468628e2f8e13f9ecdb1f1bf5b5b59 (plain)
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
enum Addressing {
	eImmediate,
	eAccumulator,
	eAbsolute,
	eAbsoluteIndexedX,
	eAbsoluteIndexedY,
	eZeroPage,
	eZeroPageIndexedX,
	eZeroPageIndexedY,
	eIndexedIndirect,
	eIndirectIndexed,
	eImplied,
	eIndirectAbsolute,
	eRelative
};

typedef int Addressing;

//Holds address of current instruction.
void (*current_instruction)(Addressing, address);

struct AddData{
	int cycles;
	int length;
	byte value;
};

AddData fAddress(Addressing addr, short x) {
	AddData ret;

	// VALUE

	switch(addr){
		case eImmediate:		ret.value = x;		break;
		case eAccumulator:		ret.value = acc;	break;

		case eAbsolute:			ret.value = Memory[x]; break;
		case eAbsoluteIndexedX:	ret.value = Memory[(x + X)]; break;
		case eAbsoluteIndexedY:	ret.value = Memory[(x + Y)]; break;

		case eZeroPage:			ret.value = Memory[(x & 0x00FF)]; break;
		case eZeroPageIndexedX:	ret.value = Memory[((x + X) & 0x00FF)]; break;
		case eZeroPageIndexedY:	ret.value = Memory[((x + Y) & 0x00FF)]; break;

		case eIndexedIndirect:	ret.value = Memory[	(((address)Memory[x+X+1])<<8) + (Memory[x+X])	];	break;
		case eIndirectIndexed:	ret.value = Memory[	(((address)Memory[x+1])<<8) + (Memory[x]) + Y	];	break;
	}

	// LENGTH

	switch(addr){
		case eAbsolute: case eAbsoluteIndexedX: case eAbsoluteIndexedY:
			ret.length = 3;	break;
		case eAccumulator:
			ret.length = 1;	break;
		default:
			ret.length = 2;	break;
	}

	// CYCLES

	switch(current_function){	// Initial value
		case &fADC: case &fAND: case &fBIT: case &fCMP: case &fCPX: case &fCPY:	case &fEOR:	case &fLDA:
		case &fLDX: case &fLDY: case &fORA: case &fSBC: case &fSTX: case &fSTY:
			switch(addr){
				case eImmediate:
					ret.cycles = 2; break;
				case eZeroPage:
					ret.cycles = 3; break;
				case eZeroPageIndexedX: case eAbsolute: case eAbsoluteIndexedX: case eAbsoluteIndexedY:
					ret.cycles = 4; break;
				case eIndexedIndirect:
					ret.cycles = 6; break;
				case eIndirectIndexed:
					ret.cycles = 5; break;
			}
			break;

		case &fASL: case &fDEC: case &fINC: case &fLSR: case &fROL: case &fROR:
			switch(addr){
				case eAccumulator:
					ret.cycles = 2; break;
				case eZeroPage:
					ret.cycles = 5; break;
				case eZeroPageIndexedX: case eAbsolute:
					ret.cycles = 6; break;
				case eAbsoluteIndexedX:
					ret.cycles = 7; break;
			}
			break;

		case &fSTA:
			switch(addr){
				case eZeroPage:
					ret.cycles = 3; break;
				case eZeroPageIndexedX: case eAbsolute:
					ret.cycles = 4; break;
				case eAbsoluteIndexedX: case eAbsoluteIndexedY:
					ret.cycles = 5; break;
				case eIndexedIndirect: case eIndirectIndexed:
					ret.cycles = 6; break;
			}
			break;

		case &fBRK:
			ret.cycles = 7;
			break;

		case &RTI: case &RTS: case &JSR:
			ret.cycles = 6;
			break;

		case &fJMP:
			ret.cycles = 5;
			break;

		case &fPLA: case &fPLP:
			ret.cycles = 4;
			break;

		case &fPHA: case &fPHP:
			ret.cycles = 3;
			break;

		default:	//Any instruction which doesn't fit any of these conditions is probably an implied/relative mode instruction which costs 2 cycles
			ret.cycles = 2;
	}

	switch(current_function){	// Page Boundary
		case &fADC: case &fSBC: case &fLDA: case &fLDX: case &fLDY: case &fEOR: case &fAND: case &fORA: case &fCMP:
			switch(addr){
				case eAbsoluteIndexedX: 
					if ((x & 0xFFFC) != ((x + X) & 0xFFFC)) ret.cycles++; break;
				case eAbsoluteIndexedY: 
					if ((x & 0xFFFC) != ((x + Y) & 0xFFFC)) ret.cycles++; break;
				case eIndirectIndexed:	//I am not 100% sure if this is the correct handling a page boundary cross with indirect indexed addressing. also its kinda ugly
					if(	(((((address)Memory[x+1])<<8) + (Memory[x]) + Y) & 0xFFFC) !=
					(((((address)Memory[x+1])<<8) + (Memory[x])) & 0xFFFC))	ret.cycles++; break;
			}
	}
}