yaze 0.2.0
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
memory.cc
Go to the documentation of this file.
2
3#include <cstdint>
4#include <iostream>
5#include <string>
6#include <vector>
7
8#include "imgui/imgui.h"
9
10namespace yaze {
11namespace app {
12namespace emu {
13namespace memory {
14
15void MemoryImpl::Initialize(const std::vector<uint8_t>& rom_data,
16 bool verbose) {
17 verbose_ = verbose;
18 type_ = 1;
19
20 auto location = 0x7FC0; // GetHeaderOffset();
21 rom_size_ = 0x400 << rom_data[location + 0x17];
22 sram_size_ = 0x400 << rom_data[location + 0x18];
23 rom_.resize(rom_size_);
24
25 // Copy memory into rom_
26 for (size_t i = 0; i < rom_size_; i++) {
27 rom_[i] = rom_data[i];
28 }
29 ram_.resize(sram_size_);
30 for (size_t i = 0; i < sram_size_; i++) {
31 ram_[i] = 0;
32 }
33
34 // Clear memory
35 memory_.resize(0x1000000); // 16 MB
36 std::fill(memory_.begin(), memory_.end(), 0);
37
38 // Load ROM data into memory based on LoROM mapping
39 size_t rom_data_size = rom_data.size();
40 size_t rom_address = 0;
41 const size_t ROM_CHUNK_SIZE = 0x8000; // 32 KB
42 for (size_t bank = 0x00; bank <= 0x3F; ++bank) {
43 for (size_t offset = 0x8000; offset <= 0xFFFF; offset += ROM_CHUNK_SIZE) {
44 if (rom_address < rom_data_size) {
45 std::copy(rom_data.begin() + rom_address,
46 rom_data.begin() + rom_address + ROM_CHUNK_SIZE,
47 memory_.begin() + (bank << 16) + offset);
48 rom_address += ROM_CHUNK_SIZE;
49 }
50 }
51 }
52}
53
54uint8_t MemoryImpl::cart_read(uint8_t bank, uint16_t adr) {
55 switch (type_) {
56 case 0:
57 return open_bus_;
58 case 1:
59 return cart_readLorom(bank, adr);
60 case 2:
61 return cart_readHirom(bank, adr);
62 case 3:
63 return cart_readExHirom(bank, adr);
64 }
65 return open_bus_;
66}
67
68void MemoryImpl::cart_write(uint8_t bank, uint16_t adr, uint8_t val) {
69 switch (type_) {
70 case 0:
71 break;
72 case 1:
73 cart_writeLorom(bank, adr, val);
74 break;
75 case 2:
76 cart_writeHirom(bank, adr, val);
77 break;
78 case 3:
79 cart_writeHirom(bank, adr, val);
80 break;
81 }
82}
83
84uint8_t MemoryImpl::cart_readLorom(uint8_t bank, uint16_t adr) {
85 if (((bank >= 0x70 && bank < 0x7e) || bank >= 0xf0) && adr < 0x8000 &&
86 sram_size_ > 0) {
87 // banks 70-7e and f0-ff, adr 0000-7fff
88 return ram_[(((bank & 0xf) << 15) | adr) & (sram_size_ - 1)];
89 }
90 bank &= 0x7f;
91 if (adr >= 0x8000 || bank >= 0x40) {
92 // adr 8000-ffff in all banks or all addresses in banks 40-7f and c0-ff
93 return rom_[((bank << 15) | (adr & 0x7fff)) & (rom_size_ - 1)];
94 }
95 return open_bus_;
96}
97
98void MemoryImpl::cart_writeLorom(uint8_t bank, uint16_t adr, uint8_t val) {
99 if (((bank >= 0x70 && bank < 0x7e) || bank > 0xf0) && adr < 0x8000 &&
100 sram_size_ > 0) {
101 // banks 70-7e and f0-ff, adr 0000-7fff
102 ram_[(((bank & 0xf) << 15) | adr) & (sram_size_ - 1)] = val;
103 }
104}
105
106uint8_t MemoryImpl::cart_readHirom(uint8_t bank, uint16_t adr) {
107 bank &= 0x7f;
108 if (bank < 0x40 && adr >= 0x6000 && adr < 0x8000 && sram_size_ > 0) {
109 // banks 00-3f and 80-bf, adr 6000-7fff
110 return ram_[(((bank & 0x3f) << 13) | (adr & 0x1fff)) & (sram_size_ - 1)];
111 }
112 if (adr >= 0x8000 || bank >= 0x40) {
113 // adr 8000-ffff in all banks or all addresses in banks 40-7f and c0-ff
114 return rom_[(((bank & 0x3f) << 16) | adr) & (rom_size_ - 1)];
115 }
116 return open_bus_;
117}
118
119uint8_t MemoryImpl::cart_readExHirom(uint8_t bank, uint16_t adr) {
120 if ((bank & 0x7f) < 0x40 && adr >= 0x6000 && adr < 0x8000 && sram_size_ > 0) {
121 // banks 00-3f and 80-bf, adr 6000-7fff
122 return ram_[(((bank & 0x3f) << 13) | (adr & 0x1fff)) & (sram_size_ - 1)];
123 }
124 bool secondHalf = bank < 0x80;
125 bank &= 0x7f;
126 if (adr >= 0x8000 || bank >= 0x40) {
127 // adr 8000-ffff in all banks or all addresses in banks 40-7f and c0-ff
128 return rom_[(((bank & 0x3f) << 16) | (secondHalf ? 0x400000 : 0) | adr) &
129 (rom_size_ - 1)];
130 }
131 return open_bus_;
132}
133
134void MemoryImpl::cart_writeHirom(uint8_t bank, uint16_t adr, uint8_t val) {
135 bank &= 0x7f;
136 if (bank < 0x40 && adr >= 0x6000 && adr < 0x8000 && sram_size_ > 0) {
137 // banks 00-3f and 80-bf, adr 6000-7fff
138 ram_[(((bank & 0x3f) << 13) | (adr & 0x1fff)) & (sram_size_ - 1)] = val;
139 }
140}
141
142uint32_t MemoryImpl::GetMappedAddress(uint32_t address) const {
143 uint8_t bank = address >> 16;
144 uint32_t offset = address & 0xFFFF;
145
146 if (bank <= 0x3F) {
147 if (address <= 0x1FFF) {
148 return (0x7E << 16) + offset; // Shadow RAM
149 } else if (address <= 0x5FFF) {
150 return (bank << 16) + (offset - 0x2000) + 0x2000; // Hardware Registers
151 } else if (address <= 0x7FFF) {
152 return offset - 0x6000 + 0x6000; // Expansion RAM
153 } else {
154 // Return lorom mapping
155 return (bank << 16) + (offset - 0x8000) + 0x8000; // ROM
156 }
157 } else if (bank == 0x7D) {
158 return offset + 0x7D0000; // SRAM
159 } else if (bank == 0x7E || bank == 0x7F) {
160 return offset + 0x7E0000; // System RAM
161 } else if (bank >= 0x80) {
162 // Handle HiROM and mirrored areas
163 }
164
165 return address; // Return the original address if no mapping is defined
166}
167
168} // namespace memory
169} // namespace emu
170} // namespace app
171} // namespace yaze
std::vector< uint8_t > memory_
Definition memory.h:307
uint8_t cart_readExHirom(uint8_t bank, uint16_t adr)
Definition memory.cc:119
void cart_writeLorom(uint8_t bank, uint16_t adr, uint8_t val)
Definition memory.cc:98
uint8_t cart_readLorom(uint8_t bank, uint16_t adr)
Definition memory.cc:84
uint32_t GetMappedAddress(uint32_t address) const
Definition memory.cc:142
std::vector< uint8_t > rom_
Definition memory.h:269
uint8_t cart_read(uint8_t bank, uint16_t adr)
Definition memory.cc:54
std::vector< uint8_t > ram_
Definition memory.h:270
void cart_writeHirom(uint8_t bank, uint16_t adr, uint8_t val)
Definition memory.cc:134
void Initialize(const std::vector< uint8_t > &romData, bool verbose=false)
Definition memory.cc:15
void cart_write(uint8_t bank, uint16_t adr, uint8_t val)
Definition memory.cc:68
uint8_t cart_readHirom(uint8_t bank, uint16_t adr)
Definition memory.cc:106
Definition common.cc:22