all repos — mgba @ d22a12091829c7f55cec8a16e3c0986a2af883cd

mGBA Game Boy Advance Emulator

src/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 "util/common.h"
 10
 11#include "core/log.h"
 12#include "gb/interface.h"
 13#include "lr35902/lr35902.h"
 14
 15#include <time.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_VRAM = 0x8000,
 26	GB_BASE_EXTERNAL_RAM = 0xA000,
 27	GB_BASE_WORKING_RAM_BANK0 = 0xC000,
 28	GB_BASE_WORKING_RAM_BANK1 = 0xD000,
 29	GB_BASE_OAM = 0xFE00,
 30	GB_BASE_UNUSABLE = 0xFEA0,
 31	GB_BASE_IO = 0xFF00,
 32	GB_BASE_HRAM = 0xFF80,
 33	GB_BASE_IE = 0xFFFF
 34};
 35
 36enum {
 37	GB_REGION_CART_BANK0 = 0x0,
 38	GB_REGION_CART_BANK1 = 0x4,
 39	GB_REGION_VRAM = 0x8,
 40	GB_REGION_EXTERNAL_RAM = 0xA,
 41	GB_REGION_WORKING_RAM_BANK0 = 0xC,
 42	GB_REGION_WORKING_RAM_BANK1 = 0xD,
 43	GB_REGION_WORKING_RAM_BANK1_MIRROR = 0xE,
 44	GB_REGION_OTHER = 0xF,
 45};
 46
 47enum {
 48	GB_SIZE_CART_BANK0 = 0x4000,
 49	GB_SIZE_CART_MAX = 0x800000,
 50	GB_SIZE_VRAM = 0x4000,
 51	GB_SIZE_VRAM_BANK0 = 0x2000,
 52	GB_SIZE_EXTERNAL_RAM = 0x2000,
 53	GB_SIZE_WORKING_RAM = 0x8000,
 54	GB_SIZE_WORKING_RAM_BANK0 = 0x1000,
 55	GB_SIZE_OAM = 0xA0,
 56	GB_SIZE_IO = 0x80,
 57	GB_SIZE_HRAM = 0x7F,
 58};
 59
 60enum {
 61	GB_SRAM_DIRT_NEW = 1,
 62	GB_SRAM_DIRT_SEEN = 2
 63};
 64
 65struct GBMemory;
 66typedef void (*GBMemoryBankController)(struct GB*, uint16_t address, uint8_t value);
 67
 68DECL_BITFIELD(GBMBC7Field, uint8_t);
 69DECL_BIT(GBMBC7Field, SK, 6);
 70DECL_BIT(GBMBC7Field, CS, 7);
 71DECL_BIT(GBMBC7Field, IO, 1);
 72
 73enum GBMBC7MachineState {
 74	GBMBC7_STATE_NULL = -1,
 75	GBMBC7_STATE_IDLE = 0,
 76	GBMBC7_STATE_READ_COMMAND = 1,
 77	GBMBC7_STATE_READ_ADDRESS = 2,
 78	GBMBC7_STATE_COMMAND_0 = 3,
 79	GBMBC7_STATE_COMMAND_SR_WRITE = 4,
 80	GBMBC7_STATE_COMMAND_SR_READ = 5,
 81	GBMBC7_STATE_COMMAND_SR_FILL = 6,
 82	GBMBC7_STATE_READ = 7,
 83	GBMBC7_STATE_WRITE = 8,
 84};
 85
 86struct GBMBC1State {
 87	int mode;
 88};
 89
 90struct GBMBC7State {
 91	enum GBMBC7MachineState state;
 92	uint32_t sr;
 93	uint8_t address;
 94	bool writable;
 95	int srBits;
 96	int command;
 97	GBMBC7Field field;
 98};
 99
100union GBMBCState {
101	struct GBMBC1State mbc1;
102	struct GBMBC7State mbc7;
103};
104
105struct mRotationSource;
106struct GBMemory {
107	uint8_t* rom;
108	uint8_t* romBase;
109	uint8_t* romBank;
110	enum GBMemoryBankControllerType mbcType;
111	GBMemoryBankController mbc;
112	union GBMBCState mbcState;
113	int currentBank;
114
115	uint8_t* wram;
116	uint8_t* wramBank;
117	int wramCurrentBank;
118
119	bool sramAccess;
120	uint8_t* sram;
121	uint8_t* sramBank;
122	int sramCurrentBank;
123
124	uint8_t io[GB_SIZE_IO];
125	bool ime;
126	uint8_t ie;
127
128	uint8_t hram[GB_SIZE_HRAM];
129
130	int32_t dmaNext;
131	uint16_t dmaSource;
132	uint16_t dmaDest;
133	int dmaRemaining;
134
135	int32_t hdmaNext;
136	uint16_t hdmaSource;
137	uint16_t hdmaDest;
138	int hdmaRemaining;
139	bool isHdma;
140
141	size_t romSize;
142
143	bool rtcAccess;
144	int activeRtcReg;
145	bool rtcLatched;
146	uint8_t rtcRegs[5];
147	time_t rtcLastLatch;
148	struct mRTCSource* rtc;
149	struct mRotationSource* rotation;
150	struct mRumble* rumble;
151};
152
153void GBMemoryInit(struct GB* gb);
154void GBMemoryDeinit(struct GB* gb);
155
156void GBMemoryReset(struct GB* gb);
157void GBMemorySwitchWramBank(struct GBMemory* memory, int bank);
158
159uint8_t GBLoad8(struct LR35902Core* cpu, uint16_t address);
160void GBStore8(struct LR35902Core* cpu, uint16_t address, int8_t value);
161
162uint8_t GBView8(struct LR35902Core* cpu, uint16_t address, int segment);
163
164int32_t GBMemoryProcessEvents(struct GB* gb, int32_t cycles);
165void GBMemoryDMA(struct GB* gb, uint16_t base);
166void GBMemoryWriteHDMA5(struct GB* gb, uint8_t value);
167
168uint8_t GBDMALoad8(struct LR35902Core* cpu, uint16_t address);
169void GBDMAStore8(struct LR35902Core* cpu, uint16_t address, int8_t value);
170
171void GBPatch8(struct LR35902Core* cpu, uint16_t address, int8_t value, int8_t* old, int segment);
172
173struct GBSerializedState;
174void GBMemorySerialize(const struct GB* gb, struct GBSerializedState* state);
175void GBMemoryDeserialize(struct GB* gb, const struct GBSerializedState* state);
176
177#endif