VirtualC64 v5.0 beta
Commodore 64 Emulator
Loading...
Searching...
No Matches
C64Memory.h
1// -----------------------------------------------------------------------------
2// This file is part of VirtualC64
3//
4// Copyright (C) Dirk W. Hoffmann. www.dirkwhoffmann.de
5// This FILE is dual-licensed. You are free to choose between:
6//
7// - The GNU General Public License v3 (or any later version)
8// - The Mozilla Public License v2
9//
10// SPDX-License-Identifier: GPL-3.0-or-later OR MPL-2.0
11// -----------------------------------------------------------------------------
12
13#pragma once
14
15#include "MemoryTypes.h"
16#include "SubComponent.h"
17#include "Heatmap.h"
18
19namespace vc64 {
20
21class C64Memory final : public SubComponent, public Inspectable<MemInfo, MemStats> {
22
23 friend class Heatmap;
24
25 Descriptions descriptions = {{
26
27 .name = "Memory",
28 .description = "C64 Memory"
29 }};
30
31 ConfigOptions options = {
32
36 };
37
38 // Current configuration
39 MemConfig config = { };
40
41public:
42
43 /* C64 bank mapping
44 *
45 * BankMap[index][range] where
46 *
47 * index = (EXROM, GAME, CHAREN, HIRAM, LORAM)
48 * range = upper four bits of address
49 */
50 MemoryType bankMap[32][16];
51
52 // Random Access Memory
53 u8 ram[65536];
54
55 /* Color RAM
56 * The color RAM is located in the I/O space, starting at $D800 and ending
57 * at $DBFF. Only the lower four bits are accessible, the upper four bits
58 * are open and can show any value.
59 */
60 u8 colorRam[1024];
61
62 // Read Only Memory
63 /* Only specific memory cells are valid ROM locations. In total, the C64
64 * has three ROMs that are located at different addresses. Note, that the
65 * ROMs do not span over the whole 64KB range. Therefore, only some
66 * addresses are valid ROM addresses.
67 */
68 u8 rom[65536];
69
70 // Peek source lookup table
71 MemoryType peekSrc[16];
72
73 // Poke target lookup table
74 MemoryType pokeTarget[16];
75
76 // Indicates if watchpoints should be checked
77 bool checkWatchpoints = false;
78
79 // Debugging
80 Heatmap heatmap;
81
82
83 //
84 // Methods
85 //
86
87public:
88
89 C64Memory(C64 &ref);
90
91 C64Memory& operator= (const C64Memory& other) {
92
93 CLONE_ARRAY(ram)
94 CLONE_ARRAY(rom)
95 CLONE_ARRAY(colorRam)
96
97 CLONE_ARRAY(peekSrc)
98 CLONE_ARRAY(pokeTarget)
99
100 CLONE(config)
101
102 return *this;
103 }
104
105
106 //
107 // Methods from Serializable
108 //
109
110public:
111
112 template <class T>
113 void serialize(T& worker)
114 {
115 if (isSoftResetter(worker)) return;
116
117 worker
118
119 << ram
120 << colorRam;
121
122 if (isResetter(worker)) return;
123
124 worker
125
126 << peekSrc
127 << pokeTarget
128
129 << config.saveRoms
130 << config.ramPattern;
131
132 }
133
134 void operator << (SerResetter &worker) override { serialize(worker); }
135 void operator << (SerChecker &worker) override { serialize(worker); }
136 void operator << (SerCounter &worker) override;
137 void operator << (SerReader &worker) override;
138 void operator << (SerWriter &worker) override;
139
140
141 //
142 // Methods from CoreComponent
143 //
144
145public:
146
147 const Descriptions &getDescriptions() const override { return descriptions; }
148
149private:
150
151 void _dump(Category category, std::ostream& os) const override;
152 void _reset(bool hard) override;
153
154
155 //
156 // Configuring
157 //
158
159public:
160
161 const MemConfig &getConfig() const { return config; }
162 const ConfigOptions &getOptions() const override { return options; }
163 i64 getOption(Option opt) const override;
164 void checkOption(Option opt, i64 value) override;
165 void setOption(Option opt, i64 value) override;
166
167
168 //
169 // Inspecting
170 //
171
172public:
173
174 void cacheInfo(MemInfo &result) const override;
175
176
177 //
178 // Accessing
179 //
180
181public:
182
183 // Erases the RAM with the provided init pattern
184 void eraseWithPattern(RamPattern pattern);
185
186 /* Updates the peek and poke lookup tables. The lookup values depend on
187 * three processor port bits and the cartridge exrom and game lines.
188 */
189 void updatePeekPokeLookupTables();
190
191 // Returns the current peek source of the specified memory address
192 MemoryType getPeekSource(u16 addr) { return peekSrc[addr >> 12]; }
193
194 // Returns the current poke target of the specified memory address
195 MemoryType getPokeTarget(u16 addr) { return pokeTarget[addr >> 12]; }
196
197 // Reads a value from memory
198 u8 peek(u16 addr, MemoryType source);
199 u8 peek(u16 addr, bool gameLine, bool exromLine);
200 u8 peek(u16 addr) { return peek(addr, peekSrc[addr >> 12]); }
201 u8 peekZP(u8 addr);
202 u8 peekStack(u8 sp);
203 u8 peekIO(u16 addr);
204
205 // Reads a value from memory and discards the result (idle access)
206 void peekIdle(u16 addr) { (void)peek(addr); }
207 void peekZPIdle(u8 addr) { (void)peekZP(addr); }
208 void peekStackIdle(u8 sp) { (void)peekStack(sp); }
209 void peekIOIdle(u16 addr) { (void)peekIO(addr); }
210
211 // Reads a value from memory without side effects
212 u8 spypeek(u16 addr, MemoryType source) const;
213 u8 spypeek(u16 addr) const { return spypeek(addr, peekSrc[addr >> 12]); }
214 u8 spypeekIO(u16 addr) const;
215 u8 spypeekColor(u16 addr) const;
216
217 // Writing a value into memory
218 void poke(u16 addr, u8 value, MemoryType target);
219 void poke(u16 addr, u8 value, bool gameLine, bool exromLine);
220 void poke(u16 addr, u8 value) { poke(addr, value, pokeTarget[addr >> 12]); }
221 void pokeZP(u8 addr, u8 value);
222 void pokeStack(u8 sp, u8 value);
223 void pokeIO(u16 addr, u8 value);
224
225 // Reads a vector address from memory
226 u16 nmiVector() const;
227 u16 irqVector() const;
228 u16 resetVector();
229
230 // Returns a string representations for a portion of memory
231 string memdump(u16 addr, isize num, bool hex, isize pads, MemoryType src) const;
232 string hexdump(u16 addr, isize num, isize pads, MemoryType src) const;
233 string decdump(u16 addr, isize num, isize pads, MemoryType src) const;
234 string txtdump(u16 addr, isize num, MemoryType src) const;
235
236 // Convenience wrappers
237 string memdump(u16 addr, isize num, bool hex) { return memdump(addr, num, hex, 1, peekSrc[addr >> 12]); }
238 string hexdump(u16 addr, isize num) { return hexdump(addr, num, 1, peekSrc[addr >> 12]); }
239 string decdump(u16 addr, isize num) { return decdump(addr, num, 1, peekSrc[addr >> 12]); }
240 string txtdump(u16 addr, isize num) { return txtdump(addr, num, peekSrc[addr >> 12]); }
241
242 // Dumps a portion of memory to a stream
243 void memDump(std::ostream& os, u16 addr, isize numLines = 16, bool hex = true);
244
245
246 //
247 // Executing
248 //
249
250 void endFrame();
251};
252
253}
VirtualC64 project namespace.
Definition CmdQueue.cpp:16
@ OPT_MEM_INIT_PATTERN
Ram initialization pattern.
Definition OptionTypes.h:129
@ OPT_MEM_HEATMAP
Memory heatmap.
Definition OptionTypes.h:130
@ OPT_MEM_SAVE_ROMS
Save Roms in snapshots.
Definition OptionTypes.h:131