include/mgba/internal/gb/memory.h (view raw)
1/* Copyright (c) 2013-2016 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#ifndef GB_MEMORY_H
7#define GB_MEMORY_H
8
9#include <mgba-util/common.h>
10
11CXX_GUARD_START
12
13#include <mgba/core/log.h>
14#include <mgba/core/timing.h>
15#include <mgba/gb/interface.h>
16
17mLOG_DECLARE_CATEGORY(GB_MBC);
18mLOG_DECLARE_CATEGORY(GB_MEM);
19
20struct GB;
21
22enum {
23 GB_BASE_CART_BANK0 = 0x0000,
24 GB_BASE_CART_BANK1 = 0x4000,
25 GB_BASE_CART_HALFBANK1 = 0x4000,
26 GB_BASE_CART_HALFBANK2 = 0x6000,
27 GB_BASE_VRAM = 0x8000,
28 GB_BASE_EXTERNAL_RAM = 0xA000,
29 GB_BASE_EXTERNAL_RAM_HALFBANK0 = 0xA000,
30 GB_BASE_EXTERNAL_RAM_HALFBANK1 = 0xB000,
31 GB_BASE_WORKING_RAM_BANK0 = 0xC000,
32 GB_BASE_WORKING_RAM_BANK1 = 0xD000,
33 GB_BASE_OAM = 0xFE00,
34 GB_BASE_UNUSABLE = 0xFEA0,
35 GB_BASE_IO = 0xFF00,
36 GB_BASE_HRAM = 0xFF80,
37 GB_BASE_IE = 0xFFFF
38};
39
40enum {
41 GB_REGION_CART_BANK0 = 0x0,
42 GB_REGION_CART_BANK1 = 0x4,
43 GB_REGION_VRAM = 0x8,
44 GB_REGION_EXTERNAL_RAM = 0xA,
45 GB_REGION_WORKING_RAM_BANK0 = 0xC,
46 GB_REGION_WORKING_RAM_BANK1 = 0xD,
47 GB_REGION_WORKING_RAM_BANK1_MIRROR = 0xE,
48 GB_REGION_OTHER = 0xF,
49};
50
51enum {
52 GB_SIZE_CART_BANK0 = 0x4000,
53 GB_SIZE_CART_HALFBANK = 0x2000,
54 GB_SIZE_CART_MAX = 0x800000,
55 GB_SIZE_VRAM = 0x4000,
56 GB_SIZE_VRAM_BANK0 = 0x2000,
57 GB_SIZE_EXTERNAL_RAM = 0x2000,
58 GB_SIZE_EXTERNAL_RAM_HALFBANK = 0x1000,
59 GB_SIZE_WORKING_RAM = 0x8000,
60 GB_SIZE_WORKING_RAM_BANK0 = 0x1000,
61 GB_SIZE_OAM = 0xA0,
62 GB_SIZE_IO = 0x80,
63 GB_SIZE_HRAM = 0x7F,
64};
65
66enum {
67 GB_SRAM_DIRT_NEW = 1,
68 GB_SRAM_DIRT_SEEN = 2
69};
70
71struct GBMemory;
72typedef void (*GBMemoryBankControllerWrite)(struct GB*, uint16_t address, uint8_t value);
73typedef uint8_t (*GBMemoryBankControllerRead)(struct GBMemory*, uint16_t address);
74
75DECL_BITFIELD(GBMBC7Field, uint8_t);
76DECL_BIT(GBMBC7Field, CS, 7);
77DECL_BIT(GBMBC7Field, CLK, 6);
78DECL_BIT(GBMBC7Field, DI, 1);
79DECL_BIT(GBMBC7Field, DO, 0);
80
81enum GBMBC7MachineState {
82 GBMBC7_STATE_IDLE = 0,
83 GBMBC7_STATE_READ_COMMAND = 1,
84 GBMBC7_STATE_DO = 2,
85
86 GBMBC7_STATE_EEPROM_EWDS = 0x10,
87 GBMBC7_STATE_EEPROM_WRAL = 0x11,
88 GBMBC7_STATE_EEPROM_ERAL = 0x12,
89 GBMBC7_STATE_EEPROM_EWEN = 0x13,
90 GBMBC7_STATE_EEPROM_WRITE = 0x14,
91 GBMBC7_STATE_EEPROM_READ = 0x18,
92 GBMBC7_STATE_EEPROM_ERASE = 0x1C,
93};
94
95enum GBTAMA5Register {
96 GBTAMA5_BANK_LO = 0x0,
97 GBTAMA5_BANK_HI = 0x1,
98 GBTAMA5_WRITE_LO = 0x4,
99 GBTAMA5_WRITE_HI = 0x5,
100 GBTAMA5_CS = 0x6,
101 GBTAMA5_ADDR_LO = 0x7,
102 GBTAMA5_MAX = 0x8,
103 GBTAMA5_ACTIVE = 0xA,
104 GBTAMA5_READ_LO = 0xC,
105 GBTAMA5_READ_HI = 0xD,
106};
107
108struct GBMBC1State {
109 int mode;
110 int multicartStride;
111};
112
113struct GBMBC6State {
114 int currentBank1;
115 uint8_t* romBank1;
116 bool sramAccess;
117 int currentSramBank1;
118 uint8_t* sramBank1;
119};
120
121struct GBMBC7State {
122 enum GBMBC7MachineState state;
123 uint16_t sr;
124 uint8_t address;
125 bool writable;
126 int srBits;
127 uint8_t access;
128 uint8_t latch;
129 GBMBC7Field eeprom;
130};
131
132struct GBPocketCamState {
133 bool registersActive;
134 uint8_t registers[0x36];
135};
136
137struct GBTAMA5State {
138 uint8_t reg;
139 uint8_t registers[GBTAMA5_MAX];
140};
141
142union GBMBCState {
143 struct GBMBC1State mbc1;
144 struct GBMBC6State mbc6;
145 struct GBMBC7State mbc7;
146 struct GBPocketCamState pocketCam;
147 struct GBTAMA5State tama5;
148};
149
150struct mRotationSource;
151struct GBMemory {
152 uint8_t* rom;
153 uint8_t* romBase;
154 uint8_t* romBank;
155 enum GBMemoryBankControllerType mbcType;
156 GBMemoryBankControllerWrite mbcWrite;
157 GBMemoryBankControllerRead mbcRead;
158 union GBMBCState mbcState;
159 int currentBank;
160
161 uint8_t* wram;
162 uint8_t* wramBank;
163 int wramCurrentBank;
164
165 bool sramAccess;
166 uint8_t* sram;
167 uint8_t* sramBank;
168 int sramCurrentBank;
169
170 uint8_t io[GB_SIZE_IO];
171 bool ime;
172 uint8_t ie;
173
174 uint8_t hram[GB_SIZE_HRAM];
175
176 uint16_t dmaSource;
177 uint16_t dmaDest;
178 int dmaRemaining;
179
180 uint16_t hdmaSource;
181 uint16_t hdmaDest;
182 int hdmaRemaining;
183 bool isHdma;
184
185 struct mTimingEvent dmaEvent;
186 struct mTimingEvent hdmaEvent;
187
188 size_t romSize;
189
190 bool rtcAccess;
191 int activeRtcReg;
192 bool rtcLatched;
193 uint8_t rtcRegs[5];
194 time_t rtcLastLatch;
195 struct mRTCSource* rtc;
196 struct mRotationSource* rotation;
197 struct mRumble* rumble;
198 struct mImageSource* cam;
199};
200
201struct LR35902Core;
202void GBMemoryInit(struct GB* gb);
203void GBMemoryDeinit(struct GB* gb);
204
205void GBMemoryReset(struct GB* gb);
206void GBMemorySwitchWramBank(struct GBMemory* memory, int bank);
207
208uint8_t GBLoad8(struct LR35902Core* cpu, uint16_t address);
209void GBStore8(struct LR35902Core* cpu, uint16_t address, int8_t value);
210
211int GBCurrentSegment(struct LR35902Core* cpu, uint16_t address);
212
213uint8_t GBView8(struct LR35902Core* cpu, uint16_t address, int segment);
214
215void GBMemoryDMA(struct GB* gb, uint16_t base);
216uint8_t GBMemoryWriteHDMA5(struct GB* gb, uint8_t value);
217
218void GBPatch8(struct LR35902Core* cpu, uint16_t address, int8_t value, int8_t* old, int segment);
219
220struct GBSerializedState;
221void GBMemorySerialize(const struct GB* gb, struct GBSerializedState* state);
222void GBMemoryDeserialize(struct GB* gb, const struct GBSerializedState* state);
223
224CXX_GUARD_END
225
226#endif