libsidplayfp 2.15.0
SystemROMBanks.h
1/*
2 * This file is part of libsidplayfp, a SID player engine.
3 *
4 * Copyright 2012-2022 Leandro Nini <drfiemost@users.sourceforge.net>
5 * Copyright 2010 Antti Lankila
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22#ifndef SYSTEMROMBANKS_H
23#define SYSTEMROMBANKS_H
24
25#include <stdint.h>
26#include <cstring>
27
28#include "Bank.h"
29#include "c64/CPU/opcodes.h"
30
31#include "sidcxx11.h"
32
33namespace libsidplayfp
34{
35
39template <int N>
40class romBank : public Bank
41{
42 static_assert((N != 0) && ((N & (N - 1)) == 0), "N must be a power of two");
43
44protected:
46 uint8_t rom[N];
47
48protected:
52 void setVal(uint_least16_t address, uint8_t val) { rom[address & (N-1)]=val; }
53
57 uint8_t getVal(uint_least16_t address) const { return rom[address & (N-1)]; }
58
62 void* getPtr(uint_least16_t address) const { return (void*)&rom[address & (N-1)]; }
63
64public:
68 void set(const uint8_t* source) { if (source != nullptr) std::memcpy(rom, source, N); }
69
73 void poke(uint_least16_t, uint8_t) override {}
74
78 uint8_t peek(uint_least16_t address) override { return rom[address & (N-1)]; }
79};
80
86class KernalRomBank final : public romBank<0x2000>
87{
88private:
89 uint8_t resetVectorLo; // 0xfffc
90 uint8_t resetVectorHi; // 0xfffd
91
92public:
93 void set(const uint8_t* kernal)
94 {
96
97 if (kernal == nullptr)
98 {
99 // IRQ entry point
100 setVal(0xffa0, PHAn); // Save regs
101 setVal(0xffa1, TXAn);
102 setVal(0xffa2, PHAn);
103 setVal(0xffa3, TYAn);
104 setVal(0xffa4, PHAn);
105 setVal(0xffa5, JMPi); // Jump to IRQ routine
106 setVal(0xffa6, 0x14);
107 setVal(0xffa7, 0x03);
108
109 // Halt
110 setVal(0xea39, 0x02);
111
112 // Hardware vectors
113 setVal(0xfffa, 0x39); // NMI vector
114 setVal(0xfffb, 0xea);
115 setVal(0xfffc, 0x39); // RESET vector
116 setVal(0xfffd, 0xea);
117 setVal(0xfffe, 0xa0); // IRQ/BRK vector
118 setVal(0xffff, 0xff);
119 }
120
121 // Backup Reset Vector
122 resetVectorLo = getVal(0xfffc);
123 resetVectorHi = getVal(0xfffd);
124 }
125
126 void reset()
127 {
128 // Restore original Reset Vector
129 setVal(0xfffc, resetVectorLo);
130 setVal(0xfffd, resetVectorHi);
131 }
132
138 void installResetHook(uint_least16_t addr)
139 {
140 setVal(0xfffc, endian_16lo8(addr));
141 setVal(0xfffd, endian_16hi8(addr));
142 }
143};
144
150class BasicRomBank final : public romBank<0x2000>
151{
152private:
153 uint8_t trap[3];
154 uint8_t subTune[11];
155
156public:
157 void set(const uint8_t* basic)
158 {
160
161 // Backup BASIC Warm Start
162 std::memcpy(trap, getPtr(0xa7ae), sizeof(trap));
163
164 std::memcpy(subTune, getPtr(0xbf53), sizeof(subTune));
165 }
166
167 void reset()
168 {
169 // Restore original BASIC Warm Start
170 std::memcpy(getPtr(0xa7ae), trap, sizeof(trap));
171
172 std::memcpy(getPtr(0xbf53), subTune, sizeof(subTune));
173 }
174
180 void installTrap(uint_least16_t addr)
181 {
182 setVal(0xa7ae, JMPw);
183 setVal(0xa7af, endian_16lo8(addr));
184 setVal(0xa7b0, endian_16hi8(addr));
185 }
186
187 void setSubtune(uint8_t tune)
188 {
189 setVal(0xbf53, LDAb);
190 setVal(0xbf54, tune);
191 setVal(0xbf55, STAa);
192 setVal(0xbf56, 0x0c);
193 setVal(0xbf57, 0x03);
194 setVal(0xbf58, JSRw);
195 setVal(0xbf59, 0x2c);
196 setVal(0xbf5a, 0xa8);
197 setVal(0xbf5b, JMPw);
198 setVal(0xbf5c, 0xb1);
199 setVal(0xbf5d, 0xa7);
200 }
201};
202
208class CharacterRomBank final : public romBank<0x1000> {};
209
210}
211
212#endif
Definition Bank.h:36
Definition SystemROMBanks.h:151
void installTrap(uint_least16_t addr)
Definition SystemROMBanks.h:180
Definition SystemROMBanks.h:208
Definition SystemROMBanks.h:87
void installResetHook(uint_least16_t addr)
Definition SystemROMBanks.h:138
Definition SystemROMBanks.h:41
void set(const uint8_t *source)
Definition SystemROMBanks.h:68
uint8_t peek(uint_least16_t address) override
Definition SystemROMBanks.h:78
uint8_t getVal(uint_least16_t address) const
Definition SystemROMBanks.h:57
void poke(uint_least16_t, uint8_t) override
Definition SystemROMBanks.h:73
uint8_t rom[N]
The ROM array.
Definition SystemROMBanks.h:46
void setVal(uint_least16_t address, uint8_t val)
Definition SystemROMBanks.h:52
void * getPtr(uint_least16_t address) const
Definition SystemROMBanks.h:62