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 "lr35902/lr35902.h"
12
13struct GB;
14
15enum {
16 GB_BASE_CART_BANK0 = 0x0000,
17 GB_BASE_CART_BANK1 = 0x4000,
18 GB_BASE_VRAM = 0x8000,
19 GB_BASE_EXTERNAL_RAM = 0xA000,
20 GB_BASE_WORKING_RAM_BANK0 = 0xC000,
21 GB_BASE_WORKING_RAM_BANK1 = 0xD000,
22 GB_BASE_OAM = 0xFE00,
23 GB_BASE_IO = 0xFF00,
24 GB_BASE_HRAM = 0xFF80,
25 GB_BASE_IE = 0xFFFF
26};
27
28enum {
29 GB_REGION_CART_BANK0 = 0x0,
30 GB_REGION_CART_BANK1 = 0x4,
31 GB_REGION_VRAM = 0x8,
32 GB_REGION_EXTERNAL_RAM = 0xA,
33 GB_REGION_WORKING_RAM_BANK0 = 0xC,
34 GB_REGION_WORKING_RAM_BANK1 = 0xD,
35 GB_REGION_WORKING_RAM_BANK1_MIRROR = 0xE,
36 GB_REGION_OTHER = 0xF,
37};
38
39enum {
40 GB_SIZE_CART_BANK0 = 0x4000,
41 GB_SIZE_VRAM = 0x2000,
42 GB_SIZE_EXTERNAL_RAM = 0x2000,
43 GB_SIZE_WORKING_RAM = 0x8000,
44 GB_SIZE_WORKING_RAM_BANK0 = 0x1000,
45 GB_SIZE_OAM = 0xA0,
46 GB_SIZE_IO = 0x80,
47 GB_SIZE_HRAM = 0x7F,
48};
49
50enum GBMemoryBankControllerType {
51 GB_MBC_NONE = 0,
52 GB_MBC1 = 1,
53 GB_MBC2 = 2,
54 GB_MBC3 = 3,
55 GB_MBC4 = 4,
56 GB_MBC5 = 5,
57 GB_MMM01 = 0x10,
58 GB_HuC1 = 0x11
59};
60
61struct GBMemory;
62typedef void (*GBMemoryBankController)(struct GBMemory*, uint16_t address, uint8_t value);
63
64struct GBMemory {
65 uint8_t* rom;
66 uint8_t* romBank;
67 enum GBMemoryBankControllerType mbcType;
68 GBMemoryBankController mbc;
69 int currentBank;
70
71 uint8_t* wram;
72 uint8_t* wramBank;
73
74 uint8_t io[GB_SIZE_IO];
75 bool ime;
76 uint8_t ie;
77
78 uint8_t hram[GB_SIZE_HRAM];
79
80 size_t romSize;
81};
82
83void GBMemoryInit(struct GB* gb);
84void GBMemoryDeinit(struct GB* gb);
85
86void GBMemoryReset(struct GB* gb);
87
88uint16_t GBLoad16(struct LR35902Core* cpu, uint16_t address);
89uint8_t GBLoad8(struct LR35902Core* cpu, uint16_t address);
90
91void GBStore16(struct LR35902Core* cpu, uint16_t address, int16_t value);
92void GBStore8(struct LR35902Core* cpu, uint16_t address, int8_t value);
93
94uint16_t GBView16(struct LR35902Core* cpu, uint16_t address);
95uint8_t GBView8(struct LR35902Core* cpu, uint16_t address);
96
97void GBPatch16(struct LR35902Core* cpu, uint16_t address, int16_t value, int16_t* old);
98void GBPatch8(struct LR35902Core* cpu, uint16_t address, int8_t value, int8_t* old);
99
100#endif