src/gba/memory.h (view raw)
1/* Copyright (c) 2013-2015 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 GBA_MEMORY_H
7#define GBA_MEMORY_H
8
9#include "util/common.h"
10
11#include "arm/arm.h"
12
13#include "gba/hardware.h"
14#include "gba/savedata.h"
15
16enum GBAMemoryRegion {
17 REGION_BIOS = 0x0,
18 REGION_WORKING_RAM = 0x2,
19 REGION_WORKING_IRAM = 0x3,
20 REGION_IO = 0x4,
21 REGION_PALETTE_RAM = 0x5,
22 REGION_VRAM = 0x6,
23 REGION_OAM = 0x7,
24 REGION_CART0 = 0x8,
25 REGION_CART0_EX = 0x9,
26 REGION_CART1 = 0xA,
27 REGION_CART1_EX = 0xB,
28 REGION_CART2 = 0xC,
29 REGION_CART2_EX = 0xD,
30 REGION_CART_SRAM = 0xE,
31 REGION_CART_SRAM_MIRROR = 0xF
32};
33
34enum GBAMemoryBase {
35 BASE_BIOS = 0x00000000,
36 BASE_WORKING_RAM = 0x02000000,
37 BASE_WORKING_IRAM = 0x03000000,
38 BASE_IO = 0x04000000,
39 BASE_PALETTE_RAM = 0x05000000,
40 BASE_VRAM = 0x06000000,
41 BASE_OAM = 0x07000000,
42 BASE_CART0 = 0x08000000,
43 BASE_CART0_EX = 0x09000000,
44 BASE_CART1 = 0x0A000000,
45 BASE_CART1_EX = 0x0B000000,
46 BASE_CART2 = 0x0C000000,
47 BASE_CART2_EX = 0x0D000000,
48 BASE_CART_SRAM = 0x0E000000,
49 BASE_CART_SRAM_MIRROR = 0x0F000000
50};
51
52enum {
53 SIZE_BIOS = 0x00004000,
54 SIZE_WORKING_RAM = 0x00040000,
55 SIZE_WORKING_IRAM = 0x00008000,
56 SIZE_IO = 0x00000400,
57 SIZE_PALETTE_RAM = 0x00000400,
58 SIZE_VRAM = 0x00018000,
59 SIZE_OAM = 0x00000400,
60 SIZE_CART0 = 0x02000000,
61 SIZE_CART1 = 0x02000000,
62 SIZE_CART2 = 0x02000000,
63 SIZE_CART_SRAM = 0x00010000,
64 SIZE_CART_FLASH512 = 0x00010000,
65 SIZE_CART_FLASH1M = 0x00020000,
66 SIZE_CART_EEPROM = 0x00002000
67};
68
69enum {
70 OFFSET_MASK = 0x00FFFFFF,
71 BASE_OFFSET = 24
72};
73
74enum DMAControl {
75 DMA_INCREMENT = 0,
76 DMA_DECREMENT = 1,
77 DMA_FIXED = 2,
78 DMA_INCREMENT_RELOAD = 3
79};
80
81enum DMATiming {
82 DMA_TIMING_NOW = 0,
83 DMA_TIMING_VBLANK = 1,
84 DMA_TIMING_HBLANK = 2,
85 DMA_TIMING_CUSTOM = 3
86};
87
88mLOG_DECLARE_CATEGORY(GBA_MEM);
89
90DECL_BITFIELD(GBADMARegister, uint16_t);
91DECL_BITS(GBADMARegister, DestControl, 5, 2);
92DECL_BITS(GBADMARegister, SrcControl, 7, 2);
93DECL_BIT(GBADMARegister, Repeat, 9);
94DECL_BIT(GBADMARegister, Width, 10);
95DECL_BIT(GBADMARegister, DRQ, 11);
96DECL_BITS(GBADMARegister, Timing, 12, 2);
97DECL_BIT(GBADMARegister, DoIRQ, 14);
98DECL_BIT(GBADMARegister, Enable, 15);
99
100struct GBADMA {
101 GBADMARegister reg;
102
103 uint32_t source;
104 uint32_t dest;
105 int32_t count;
106 uint32_t nextSource;
107 uint32_t nextDest;
108 int32_t nextCount;
109 int32_t nextEvent;
110};
111
112struct GBAMemory {
113 uint32_t* bios;
114 uint32_t* wram;
115 uint32_t* iwram;
116 uint32_t* rom;
117 uint16_t io[SIZE_IO >> 1];
118
119 struct GBACartridgeHardware hw;
120 struct GBASavedata savedata;
121 size_t romSize;
122 uint32_t romMask;
123 uint16_t romID;
124 int fullBios;
125
126 char waitstatesSeq32[256];
127 char waitstatesSeq16[256];
128 char waitstatesNonseq32[256];
129 char waitstatesNonseq16[256];
130 char waitstatesPrefetchSeq32[16];
131 char waitstatesPrefetchSeq16[16];
132 char waitstatesPrefetchNonseq32[16];
133 char waitstatesPrefetchNonseq16[16];
134 int activeRegion;
135 bool prefetch;
136 uint32_t lastPrefetchedPc;
137 uint32_t lastPrefetchedLoads;
138 uint32_t biosPrefetch;
139
140 struct GBADMA dma[4];
141 int activeDMA;
142 int32_t nextDMA;
143 int32_t eventDiff;
144
145 bool mirroring;
146};
147
148void GBAMemoryInit(struct GBA* gba);
149void GBAMemoryDeinit(struct GBA* gba);
150
151void GBAMemoryReset(struct GBA* gba);
152
153uint32_t GBALoad32(struct ARMCore* cpu, uint32_t address, int* cycleCounter);
154uint32_t GBALoad16(struct ARMCore* cpu, uint32_t address, int* cycleCounter);
155uint32_t GBALoad8(struct ARMCore* cpu, uint32_t address, int* cycleCounter);
156
157uint32_t GBALoadBad(struct ARMCore* cpu);
158
159void GBAStore32(struct ARMCore* cpu, uint32_t address, int32_t value, int* cycleCounter);
160void GBAStore16(struct ARMCore* cpu, uint32_t address, int16_t value, int* cycleCounter);
161void GBAStore8(struct ARMCore* cpu, uint32_t address, int8_t value, int* cycleCounter);
162
163uint32_t GBAView32(struct ARMCore* cpu, uint32_t address);
164uint16_t GBAView16(struct ARMCore* cpu, uint32_t address);
165uint8_t GBAView8(struct ARMCore* cpu, uint32_t address);
166
167void GBAPatch32(struct ARMCore* cpu, uint32_t address, int32_t value, int32_t* old);
168void GBAPatch16(struct ARMCore* cpu, uint32_t address, int16_t value, int16_t* old);
169void GBAPatch8(struct ARMCore* cpu, uint32_t address, int8_t value, int8_t* old);
170
171uint32_t GBALoadMultiple(struct ARMCore*, uint32_t baseAddress, int mask, enum LSMDirection direction,
172 int* cycleCounter);
173uint32_t GBAStoreMultiple(struct ARMCore*, uint32_t baseAddress, int mask, enum LSMDirection direction,
174 int* cycleCounter);
175
176void GBAAdjustWaitstates(struct GBA* gba, uint16_t parameters);
177
178uint32_t GBAMemoryWriteDMASAD(struct GBA* gba, int dma, uint32_t address);
179uint32_t GBAMemoryWriteDMADAD(struct GBA* gba, int dma, uint32_t address);
180void GBAMemoryWriteDMACNT_LO(struct GBA* gba, int dma, uint16_t count);
181uint16_t GBAMemoryWriteDMACNT_HI(struct GBA* gba, int dma, uint16_t control);
182
183void GBAMemoryScheduleDMA(struct GBA* gba, int number, struct GBADMA* info);
184void GBAMemoryRunHblankDMAs(struct GBA* gba, int32_t cycles);
185void GBAMemoryRunVblankDMAs(struct GBA* gba, int32_t cycles);
186void GBAMemoryUpdateDMAs(struct GBA* gba, int32_t cycles);
187int32_t GBAMemoryRunDMAs(struct GBA* gba, int32_t cycles);
188
189struct GBASerializedState;
190void GBAMemorySerialize(const struct GBAMemory* memory, struct GBASerializedState* state);
191void GBAMemoryDeserialize(struct GBAMemory* memory, const struct GBASerializedState* state);
192
193#endif