libsidplayfp 2.16.1
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 <algorithm>
27#include <iterator>
28#include <cstring>
29
30#include "Bank.h"
31#include "c64/CPU/opcodes.h"
32
33#include "sidcxx11.h"
34
35namespace libsidplayfp
36{
37
41template <int N>
42class romBank : public Bank
43{
44 static_assert((N != 0) && ((N & (N - 1)) == 0), "N must be a power of two");
45
46protected:
48 uint8_t rom[N];
49
50protected:
54 void setVal(uint_least16_t address, uint8_t val) { rom[address & (N-1)]=val; }
55
59 uint8_t getVal(uint_least16_t address) const { return rom[address & (N-1)]; }
60
64 void* getPtr(uint_least16_t address) const { return (void*)&rom[address & (N-1)]; }
65
66public:
70 void set(const uint8_t* source) { if (source != nullptr) std::memcpy(rom, source, N); }
71
75 void poke(uint_least16_t, uint8_t) override {}
76
80 uint8_t peek(uint_least16_t address) override { return rom[address & (N-1)]; }
81};
82
88class KernalRomBank final : public romBank<0x2000>
89{
90private:
91 uint8_t resetVectorLo; // 0xfffc
92 uint8_t resetVectorHi; // 0xfffd
93
94public:
95 void set(const uint8_t* kernal)
96 {
98
99 if (kernal == nullptr)
100 {
101 std::fill(std::begin(rom), std::end(rom), RTSn);
102
103 // IRQ routine
104 setVal(0xea31, JMPw);
105 setVal(0xea32, 0x7e);
106 setVal(0xea33, 0xea);
107
108 setVal(0xea7e, NOPa); // Clear IRQ
109 setVal(0xea7f, 0x0d);
110 setVal(0xea80, 0xdc);
111 setVal(0xea81, PLAn); // Restore registers
112 setVal(0xea82, TAYn);
113 setVal(0xea83, PLAn);
114 setVal(0xea84, TAXn);
115 setVal(0xea85, PLAn);
116 setVal(0xea86, RTIn); // Return from interrupt
117
118 // Reset
119 setVal(0xfce2, 0x02); // Halt
120
121 // NMI entry point
122 setVal(0xfe43, SEIn);
123 setVal(0xfe44, JMPi); // Jump to NMI routine (Default: $FE47)
124 setVal(0xfe45, 0x18);
125 setVal(0xfe46, 0x03);
126
127 // NMI routine
128 setVal(0xfe47, RTIn);
129
130 // IRQ entry point
131 setVal(0xff48, PHAn); // Save regs
132 setVal(0xff49, TXAn);
133 setVal(0xff4a, PHAn);
134 setVal(0xff4b, TYAn);
135 setVal(0xff4c, PHAn);
136 setVal(0xff4d, JMPi); // Jump to IRQ routine (Default: $EA31)
137 setVal(0xff4e, 0x14);
138 setVal(0xff4f, 0x03);
139
140 // Hardware vectors
141 setVal(0xfffa, 0x43); // NMI vector $FE43
142 setVal(0xfffb, 0xfe);
143 setVal(0xfffc, 0xe2); // RESET vector $FCE2
144 setVal(0xfffd, 0xfc);
145 setVal(0xfffe, 0x48); // IRQ/BRK vector $FF48
146 setVal(0xffff, 0xff);
147 }
148
149 // Backup Reset Vector
150 resetVectorLo = getVal(0xfffc);
151 resetVectorHi = getVal(0xfffd);
152 }
153
154 void reset()
155 {
156 // Restore original Reset Vector
157 setVal(0xfffc, resetVectorLo);
158 setVal(0xfffd, resetVectorHi);
159 }
160
166 void installResetHook(uint_least16_t addr)
167 {
168 setVal(0xfffc, endian_16lo8(addr));
169 setVal(0xfffd, endian_16hi8(addr));
170 }
171};
172
178class BasicRomBank final : public romBank<0x2000>
179{
180private:
181 uint8_t trap[3];
182 uint8_t subTune[11];
183
184public:
185 void set(const uint8_t* basic)
186 {
188
189 // Backup BASIC Warm Start
190 std::memcpy(trap, getPtr(0xa7ae), sizeof(trap));
191
192 std::memcpy(subTune, getPtr(0xbf53), sizeof(subTune));
193 }
194
195 void reset()
196 {
197 // Restore original BASIC Warm Start
198 std::memcpy(getPtr(0xa7ae), trap, sizeof(trap));
199
200 std::memcpy(getPtr(0xbf53), subTune, sizeof(subTune));
201 }
202
208 void installTrap(uint_least16_t addr)
209 {
210 setVal(0xa7ae, JMPw);
211 setVal(0xa7af, endian_16lo8(addr));
212 setVal(0xa7b0, endian_16hi8(addr));
213 }
214
215 void setSubtune(uint8_t tune)
216 {
217 setVal(0xbf53, LDAb);
218 setVal(0xbf54, tune);
219 setVal(0xbf55, STAa);
220 setVal(0xbf56, 0x0c);
221 setVal(0xbf57, 0x03);
222 setVal(0xbf58, JSRw);
223 setVal(0xbf59, 0x2c);
224 setVal(0xbf5a, 0xa8);
225 setVal(0xbf5b, JMPw);
226 setVal(0xbf5c, 0xb1);
227 setVal(0xbf5d, 0xa7);
228 }
229};
230
236class CharacterRomBank final : public romBank<0x1000> {};
237
238}
239
240#endif
Definition Bank.h:36
Definition SystemROMBanks.h:179
void installTrap(uint_least16_t addr)
Definition SystemROMBanks.h:208
Definition SystemROMBanks.h:236
Definition SystemROMBanks.h:89
void installResetHook(uint_least16_t addr)
Definition SystemROMBanks.h:166
Definition SystemROMBanks.h:43
void set(const uint8_t *source)
Definition SystemROMBanks.h:70
uint8_t peek(uint_least16_t address) override
Definition SystemROMBanks.h:80
uint8_t getVal(uint_least16_t address) const
Definition SystemROMBanks.h:59
void poke(uint_least16_t, uint8_t) override
Definition SystemROMBanks.h:75
uint8_t rom[N]
The ROM array.
Definition SystemROMBanks.h:48
void setVal(uint_least16_t address, uint8_t val)
Definition SystemROMBanks.h:54
void * getPtr(uint_least16_t address) const
Definition SystemROMBanks.h:64