include/mgba/internal/ds/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 DS_MEMORY_H
7#define DS_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/internal/arm/arm.h>
16#include <mgba/internal/ds/dma.h>
17#include <mgba/internal/ds/io.h>
18#include <mgba/internal/ds/slot1.h>
19
20enum DSMemoryRegion {
21 DS7_REGION_BIOS = 0x0,
22 DS9_REGION_ITCM = 0x0,
23 DS9_REGION_ITCM_MIRROR = 0x1,
24 DS_REGION_RAM = 0x2,
25 DS_REGION_WORKING_RAM = 0x3,
26 DS_REGION_IO = 0x4,
27 DS9_REGION_PALETTE_RAM = 0x5,
28 DS_REGION_VRAM = 0x6,
29 DS9_REGION_OAM = 0x7,
30 DS_REGION_SLOT2 = 0x8,
31 DS_REGION_SLOT2_EX = 0x9,
32 DS_REGION_SLOT2_SRAM = 0xA,
33 DS9_REGION_BIOS = 0xFF,
34};
35
36enum DSMemoryBase {
37 DS7_BASE_BIOS = 0x00000000,
38 DS9_BASE_ITCM = 0x00000000,
39 DS_BASE_RAM = 0x02000000,
40 DS9_BASE_DTCM = 0x027C0000,
41 DS_BASE_WORKING_RAM = 0x03000000,
42 DS7_BASE_WORKING_RAM = 0x03800000,
43 DS_BASE_IO = 0x04000000,
44 DS9_BASE_PALETTE_RAM = 0x05000000,
45 DS_BASE_VRAM = 0x06000000,
46 DS9_BASE_OAM = 0x07000000,
47 DS_BASE_SLOT2 = 0x08000000,
48 DS_BASE_SLOT2_EX = 0x09000000,
49 DS9_BASE_BIOS = 0xFFFF0000,
50};
51
52enum {
53 DS9_SIZE_ITCM = 0x00008000,
54 DS9_SIZE_DTCM = 0x00004000,
55 DS7_SIZE_BIOS = 0x00004000,
56 DS9_SIZE_BIOS = 0x00008000,
57 DS_SIZE_RAM = 0x00400000,
58 DS_SIZE_VRAM = 0x000A4000,
59 DS_SIZE_WORKING_RAM = 0x00008000,
60 DS7_SIZE_WORKING_RAM = 0x00010000,
61 DS9_SIZE_PALETTE_RAM = 0x00000800,
62 DS9_SIZE_OAM = 0x00000800,
63 DS_SIZE_SLOT2 = 0x02000000,
64 DS_SIZE_SLOT2_SRAM = 0x00010000,
65};
66
67enum {
68 DS_OFFSET_MASK = 0x00FFFFFF,
69 DS_BASE_OFFSET = 24,
70 DS_VRAM_OFFSET = 14
71};
72
73mLOG_DECLARE_CATEGORY(DS_MEM);
74
75struct DSMemory {
76 uint32_t* bios7;
77 uint32_t* bios9;
78 uint32_t* itcm;
79 uint32_t* dtcm;
80 uint32_t* ram;
81 uint32_t* wram;
82 uint32_t* wramBase7;
83 uint32_t* wramBase9;
84 uint32_t* wram7;
85 uint32_t* rom;
86 uint16_t io7[DS7_REG_MAX >> 1];
87 uint16_t io9[DS9_REG_MAX >> 1];
88 struct DSSlot1 slot1;
89
90 uint16_t vramMirror[9][0x40];
91 uint16_t vramMode[9][8];
92 uint16_t* vramBank[9];
93
94 size_t romSize;
95 size_t wramSize7;
96 size_t wramSize9;
97
98 uint32_t dtcmBase;
99 uint32_t dtcmSize;
100 uint32_t itcmSize;
101
102 bool slot1Owner;
103 bool slot2Owner;
104};
105
106struct DSCoreMemory {
107 uint16_t* io;
108 int activeRegion;
109
110 char waitstatesSeq32[256];
111 char waitstatesSeq16[256];
112 char waitstatesNonseq32[256];
113 char waitstatesNonseq16[256];
114 char waitstatesPrefetchSeq32[16];
115 char waitstatesPrefetchSeq16[16];
116 char waitstatesPrefetchNonseq32[16];
117 char waitstatesPrefetchNonseq16[16];
118
119 struct GBADMA dma[4];
120 struct mTimingEvent dmaEvent;
121 int activeDMA;
122 bool slot1Access;
123 bool slot2Access;
124};
125
126struct DS;
127void DSMemoryInit(struct DS* ds);
128void DSMemoryDeinit(struct DS* ds);
129
130void DSMemoryReset(struct DS* ds);
131
132uint32_t DS7Load32(struct ARMCore* cpu, uint32_t address, int* cycleCounter);
133uint32_t DS7Load16(struct ARMCore* cpu, uint32_t address, int* cycleCounter);
134uint32_t DS7Load8(struct ARMCore* cpu, uint32_t address, int* cycleCounter);
135
136void DS7Store32(struct ARMCore* cpu, uint32_t address, int32_t value, int* cycleCounter);
137void DS7Store16(struct ARMCore* cpu, uint32_t address, int16_t value, int* cycleCounter);
138void DS7Store8(struct ARMCore* cpu, uint32_t address, int8_t value, int* cycleCounter);
139
140uint32_t DS7LoadMultiple(struct ARMCore*, uint32_t baseAddress, int mask, enum LSMDirection direction,
141 int* cycleCounter);
142uint32_t DS7StoreMultiple(struct ARMCore*, uint32_t baseAddress, int mask, enum LSMDirection direction,
143 int* cycleCounter);
144
145uint32_t DS9Load32(struct ARMCore* cpu, uint32_t address, int* cycleCounter);
146uint32_t DS9Load16(struct ARMCore* cpu, uint32_t address, int* cycleCounter);
147uint32_t DS9Load8(struct ARMCore* cpu, uint32_t address, int* cycleCounter);
148
149void DS9Store32(struct ARMCore* cpu, uint32_t address, int32_t value, int* cycleCounter);
150void DS9Store16(struct ARMCore* cpu, uint32_t address, int16_t value, int* cycleCounter);
151void DS9Store8(struct ARMCore* cpu, uint32_t address, int8_t value, int* cycleCounter);
152
153uint32_t DS9LoadMultiple(struct ARMCore*, uint32_t baseAddress, int mask, enum LSMDirection direction,
154 int* cycleCounter);
155uint32_t DS9StoreMultiple(struct ARMCore*, uint32_t baseAddress, int mask, enum LSMDirection direction,
156 int* cycleCounter);
157
158void DSConfigureWRAM(struct DSMemory*, uint8_t config);
159void DSConfigureExternalMemory(struct DS*, uint16_t config);
160
161#endif