Debian Codegraph @

gba/cartridge/cartridge.cpp

raw
1 #include <gba/gba.hpp>
2
3 namespace GameBoyAdvance {
4
5 #include "eeprom.cpp"
6 #include "flashrom.cpp"
7 #include "serialization.cpp"
8 Cartridge cartridge;
9
10 string Cartridge::title() {
11 return information.title;
12 }
13
14 void Cartridge::load() {
15 interface->loadRequest(ID::Manifest, "manifest.bml");
16
17 auto document = Markup::Document(information.markup);
18 information.title = document["information/title"].text();
19
20 unsigned rom_size = 0;
21 if(document["cartridge/rom"].exists()) {
22 auto info = document["cartridge/rom"];
23 interface->loadRequest(ID::ROM, info["name"].data);
24 rom_size = numeral(info["size"].data);
25 for(unsigned addr = rom_size; addr < rom.size; addr++) {
26 rom.data[addr] = rom.data[Bus::mirror(addr, rom_size)];
27 }
28 }
29
30 has_sram = false;
31 has_eeprom = false;
32 has_flashrom = false;
33
34 if(document["cartridge/ram"].exists()) {
35 auto info = document["cartridge/ram"];
36
37 if(info["type"].data == "SRAM" || info["type"].data == "FRAM") {
38 has_sram = true;
39 ram.size = numeral(info["size"].data);
40 ram.mask = ram.size - 1;
41 for(unsigned n = 0; n < ram.size; n++) ram.data[n] = 0xff;
42
43 interface->loadRequest(ID::RAM, info["name"].data);
44 memory.append({ID::RAM, info["name"].data});
45 }
46
47 if(info["type"].data == "EEPROM") {
48 has_eeprom = true;
49 eeprom.size = numeral(info["size"].data);
50 eeprom.bits = eeprom.size <= 512 ? 6 : 14;
51 if(eeprom.size == 0) eeprom.size = 8192, eeprom.bits = 0; //auto-detect size
52 eeprom.mask = rom_size > 16 * 1024 * 1024 ? 0x0fffff00 : 0x0f000000;
53 eeprom.test = rom_size > 16 * 1024 * 1024 ? 0x0dffff00 : 0x0d000000;
54 for(unsigned n = 0; n < eeprom.size; n++) eeprom.data[n] = 0xff;
55
56 interface->loadRequest(ID::EEPROM, info["name"].data);
57 memory.append({ID::EEPROM, info["name"].data});
58 }
59
60 if(info["type"].data == "FlashROM") {
61 has_flashrom = true;
62 flashrom.id = numeral(info["id"].data);
63 flashrom.size = numeral(info["size"].data);
64 for(unsigned n = 0; n < flashrom.size; n++) flashrom.data[n] = 0xff;
65
66 interface->loadRequest(ID::FlashROM, info["name"].data);
67 memory.append({ID::FlashROM, info["name"].data});
68 }
69 }
70
71 sha256 = nall::sha256(rom.data, rom_size);
72
73 system.load();
74 loaded = true;
75 }
76
77 void Cartridge::unload() {
78 if(loaded == false) return;
79 loaded = false;
80 memory.reset();
81 }
82
83 void Cartridge::power() {
84 eeprom.power();
85 flashrom.power();
86 }
87
88 uint8* Cartridge::ram_data() {
89 if(has_sram) return ram.data;
90 if(has_eeprom) return eeprom.data;
91 if(has_flashrom) return flashrom.data;
92 return nullptr;
93 }
94
95 unsigned Cartridge::ram_size() {
96 if(has_sram) return ram.size;
97 if(has_eeprom) return eeprom.size;
98 if(has_flashrom) return flashrom.size;
99 return 0u;
100 }
101
102 uint32 Cartridge::read(uint8 *data, uint32 addr, uint32 size) {
103 if(size == Word) addr &= ~3;
104 if(size == Half) addr &= ~1;
105 data += addr;
106 if(size == Word) return data[0] << 0 | data[1] << 8 | data[2] << 16 | data[3] << 24;
107 if(size == Half) return data[0] << 0 | data[1] << 8;
108 return data[0];
109 }
110
111 void Cartridge::write(uint8 *data, uint32 addr, uint32 size, uint32 word) {
112 if(size == Word) addr &= ~3;
113 if(size == Half) addr &= ~1;
114 data += addr;
115 switch(size) {
116 case Word: data[3] = word >> 24;
117 data[2] = word >> 16;
118 case Half: data[1] = word >> 8;
119 case Byte: data[0] = word >> 0;
120 }
121 }
122
123 #define RAM_ANALYZE
124
125 uint32 Cartridge::read(uint32 addr, uint32 size) {
126 #ifdef RAM_ANALYZE
127 if((addr & 0x0e000000) == 0x0e000000) {
128 static bool once = true;
129 if(once) once = false, print("* SRAM/FlashROM read detected\n");
130 }
131 #endif
132
133 if(has_sram && (addr & 0x0e000000 ) == 0x0e000000 ) return read(ram.data, addr & ram.mask, size);
134 if(has_eeprom && (addr & eeprom.mask) == eeprom.test) return eeprom.read();
135 if(has_flashrom && (addr & 0x0e000000 ) == 0x0e000000 ) return flashrom.read(addr);
136 if(addr < 0x0e000000) return read(rom.data, addr & 0x01ffffff, size);
137 return cpu.pipeline.fetch.instruction;
138 }
139
140 void Cartridge::write(uint32 addr, uint32 size, uint32 word) {
141 #ifdef RAM_ANALYZE
142 if((addr & 0x0e000000) == 0x0e000000) {
143 static bool once = true;
144 if(once) once = false, print("* SRAM/FlashROM write detected\n");
145 }
146 if((addr & 0x0f000000) == 0x0d000000) {
147 static bool once = true;
148 if(once) once = false, print("* EEPROM write detected\n");
149 }
150 if((addr & 0x0e00ffff) == 0x0e005555 && (word & 0xff) == 0xaa) {
151 static bool once = true;
152 if(once) once = false, print("* FlashROM write detected\n");
153 }
154 #endif
155
156 if(has_sram && (addr & 0x0e000000 ) == 0x0e000000 ) return write(ram.data, addr & ram.mask, size, word);
157 if(has_eeprom && (addr & eeprom.mask) == eeprom.test) return eeprom.write(word & 1);
158 if(has_flashrom && (addr & 0x0e000000 ) == 0x0e000000 ) return flashrom.write(addr, word);
159 }
160
161 Cartridge::Cartridge() {
162 loaded = false;
163 rom.data = new uint8[rom.size = 32 * 1024 * 1024];
164 ram.data = new uint8[ram.size = 32 * 1024];
165 eeprom.data = new uint8[eeprom.size = 8 * 1024];
166 flashrom.data = new uint8[flashrom.size = 128 * 1024];
167 }
168
169 Cartridge::~Cartridge() {
170 delete[] rom.data;
171 delete[] ram.data;
172 delete[] eeprom.data;
173 delete[] flashrom.data;
174 }
175
176 }