summaryrefslogtreecommitdiff
path: root/src/cpu/addressing.c
blob: 8c6c61909201b1825c1fcc30192b7d0aa4947785 (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
//	addressing.h

#include "addressing.h"


address fAddressGetAddress(Addressing mode, address x)
{
	switch(mode)
		{
		case eImplied:
		case eRelative:
		case eImmediate:
		case eAccumulator:
			return 0x0000;
		case eAbsolute:
			return x;
		case eIndirectAbsolute:
			return (((address)GetMemory(x+1)) << 8) + (address)GetMemory(x);
		case eAbsoluteIndexedX:
			return x + X;
		case eAbsoluteIndexedY:
			return x + Y;
		case eZeroPage:
			return x & 0x00FF;
		case eZeroPageIndexedX:
			return ((x + X) & 0x00FF);
		case eZeroPageIndexedY:
			return ((x + Y) & 0x00FF);
		case eIndexedIndirect:
			return (((address)(GetMemory(x+X+1))) << 8) + (address)(GetMemory(x+X));
		case eIndirectIndexed:
		    return ((address)(GetMemory(x+1)) << 8) + ((address)(GetMemory(x))) + ((address)Y);
		}
}


int fAddressGetLength(Addressing mode)
{
	switch(mode)
		{
		case eAbsolute: case eAbsoluteIndexedX: case eAbsoluteIndexedY: case eIndirectAbsolute:
			return 3;
		case eAccumulator: case eImplied:
			return 1;
		default:
			return 2;
		}
}


byte fAddressGetValue(Addressing mode, address x, address addr)
{
	switch(mode)
		{
		case eImplied:
			return 0;
		case eRelative:
		case eImmediate:
			return x;
		case eAccumulator:
			return acc;
		default:
			return GetMemory(addr);
		}
}


struct State fAddress(Addressing mode, address x)
{
	struct State ret;
	ret.address = fAddressGetAddress (mode, x);
	ret.value   = fAddressGetValue   (mode, x, ret.address);
	ret.length  = fAddressGetLength  (mode);
	return ret;
}