-
Notifications
You must be signed in to change notification settings - Fork 21
/
memory.c
120 lines (95 loc) · 3.54 KB
/
memory.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
#include <aiv_gb.h>
u8_t aiv_gb_memory_read8(aiv_gameboy *gb, u16_t address)
{
return gb->memory_read[address >> 12](gb, address);
}
void aiv_gb_memory_write8(aiv_gameboy *gb, u16_t address, u8_t value)
{
gb->memory_write[address >> 12](gb, address, value);
}
u16_t aiv_gb_memory_read16(aiv_gameboy *gb, u16_t address)
{
u16_t low = (u16_t)aiv_gb_memory_read8(gb, address);
u16_t high = (u16_t)aiv_gb_memory_read8(gb, address + 1);
u16_t value = high << 8 | low;
return value;
}
static u8_t aiv_gb_memory_read_cartridge(aiv_gameboy *gb, u16_t address)
{
return gb->cartridge[address];
}
static void aiv_gb_memory_write_cartridge(aiv_gameboy *gb, u16_t address, u8_t value)
{
gb->cartridge[address] = value;
}
static u8_t aiv_gb_memory_read_vram(aiv_gameboy *gb, u16_t address)
{
return gb->vram[address - 0x8000];
}
static void aiv_gb_memory_write_vram(aiv_gameboy *gb, u16_t address, u8_t value)
{
gb->vram[address - 0x8000] = value;
}
static u8_t aiv_gb_memory_read_ram(aiv_gameboy *gb, u16_t address)
{
return gb->ram[address - 0xc000];
}
static void aiv_gb_memory_write_ram(aiv_gameboy *gb, u16_t address, u8_t value)
{
gb->ram[address - 0xc000] = value;
}
static u8_t aiv_gb_memory_read_external_ram(aiv_gameboy *gb, u16_t address)
{
return gb->ram[address - 0xa000];
}
static void aiv_gb_memory_write_external_ram(aiv_gameboy *gb, u16_t address, u8_t value)
{
gb->ram[address - 0xa000] = value;
}
static u8_t aiv_gb_memory_read_f000(aiv_gameboy *gb, u16_t address)
{
return 0;
}
static void aiv_gb_memory_write_f000(aiv_gameboy *gb, u16_t address, u8_t value)
{
}
void aiv_gb_memory_init(aiv_gameboy *gb)
{
gb->memory_read[0x0] = aiv_gb_memory_read_cartridge;
gb->memory_read[0x1] = aiv_gb_memory_read_cartridge;
gb->memory_read[0x2] = aiv_gb_memory_read_cartridge;
gb->memory_read[0x3] = aiv_gb_memory_read_cartridge;
gb->memory_read[0x4] = aiv_gb_memory_read_cartridge;
gb->memory_read[0x5] = aiv_gb_memory_read_cartridge;
gb->memory_read[0x6] = aiv_gb_memory_read_cartridge;
gb->memory_read[0x7] = aiv_gb_memory_read_cartridge;
gb->memory_read[0x8] = aiv_gb_memory_read_vram;
gb->memory_read[0x9] = aiv_gb_memory_read_vram;
gb->memory_read[0xa] = aiv_gb_memory_read_external_ram;
gb->memory_read[0xb] = aiv_gb_memory_read_external_ram;
gb->memory_read[0xc] = aiv_gb_memory_read_ram;
gb->memory_read[0xd] = aiv_gb_memory_read_ram;
gb->memory_read[0xf] = aiv_gb_memory_read_f000;
gb->memory_write[0x0] = aiv_gb_memory_write_cartridge;
gb->memory_write[0x1] = aiv_gb_memory_write_cartridge;
gb->memory_write[0x2] = aiv_gb_memory_write_cartridge;
gb->memory_write[0x3] = aiv_gb_memory_write_cartridge;
gb->memory_write[0x4] = aiv_gb_memory_write_cartridge;
gb->memory_write[0x5] = aiv_gb_memory_write_cartridge;
gb->memory_write[0x6] = aiv_gb_memory_write_cartridge;
gb->memory_write[0x7] = aiv_gb_memory_write_cartridge;
gb->memory_write[0x8] = aiv_gb_memory_write_vram;
gb->memory_write[0x9] = aiv_gb_memory_write_vram;
gb->memory_write[0xa] = aiv_gb_memory_write_external_ram;
gb->memory_write[0xb] = aiv_gb_memory_write_external_ram;
gb->memory_write[0xc] = aiv_gb_memory_write_ram;
gb->memory_write[0xd] = aiv_gb_memory_write_ram;
gb->memory_write[0xf] = aiv_gb_memory_write_f000;
}
void aiv_gb_memory_write16(aiv_gameboy *gb, u16_t address, u16_t value)
{
u16_t low = value & 0x00ff;
u16_t high = value >> 8;
aiv_gb_memory_write8(gb, address, low);
aiv_gb_memory_write8(gb, address + 1, high);
}