libsidplayfp  2.4.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 
33 namespace libsidplayfp
34 {
35 
39 template <int N>
40 class romBank : public Bank
41 {
42 #ifdef HAVE_CXX11
43  static_assert((N != 0) && ((N & (N - 1)) == 0), "N must be a power of two");
44 #endif
45 
46 protected:
48  uint8_t rom[N];
49 
50 protected:
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 
66 public:
70  void set(const uint8_t* source) { if (source != nullptr) 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 
88 class KernalRomBank final : public romBank<0x2000>
89 {
90 private:
91  uint8_t resetVectorLo; // 0xfffc
92  uint8_t resetVectorHi; // 0xfffd
93 
94 public:
95  void set(const uint8_t* kernal)
96  {
97  romBank<0x2000>::set(kernal);
98 
99  if (kernal == nullptr)
100  {
101  // IRQ entry point
102  setVal(0xffa0, PHAn); // Save regs
103  setVal(0xffa1, TXAn);
104  setVal(0xffa2, PHAn);
105  setVal(0xffa3, TYAn);
106  setVal(0xffa4, PHAn);
107  setVal(0xffa5, JMPi); // Jump to IRQ routine
108  setVal(0xffa6, 0x14);
109  setVal(0xffa7, 0x03);
110 
111  // Halt
112  setVal(0xea39, 0x02);
113 
114  // Hardware vectors
115  setVal(0xfffa, 0x39); // NMI vector
116  setVal(0xfffb, 0xea);
117  setVal(0xfffc, 0x39); // RESET vector
118  setVal(0xfffd, 0xea);
119  setVal(0xfffe, 0xa0); // IRQ/BRK vector
120  setVal(0xffff, 0xff);
121  }
122 
123  // Backup Reset Vector
124  resetVectorLo = getVal(0xfffc);
125  resetVectorHi = getVal(0xfffd);
126  }
127 
128  void reset()
129  {
130  // Restore original Reset Vector
131  setVal(0xfffc, resetVectorLo);
132  setVal(0xfffd, resetVectorHi);
133  }
134 
140  void installResetHook(uint_least16_t addr)
141  {
142  setVal(0xfffc, endian_16lo8(addr));
143  setVal(0xfffd, endian_16hi8(addr));
144  }
145 };
146 
152 class BasicRomBank final : public romBank<0x2000>
153 {
154 private:
155  uint8_t trap[3];
156  uint8_t subTune[11];
157 
158 public:
159  void set(const uint8_t* basic)
160  {
161  romBank<0x2000>::set(basic);
162 
163  // Backup BASIC Warm Start
164  memcpy(trap, getPtr(0xa7ae), sizeof(trap));
165 
166  memcpy(subTune, getPtr(0xbf53), sizeof(subTune));
167  }
168 
169  void reset()
170  {
171  // Restore original BASIC Warm Start
172  memcpy(getPtr(0xa7ae), trap, sizeof(trap));
173 
174  memcpy(getPtr(0xbf53), subTune, sizeof(subTune));
175  }
176 
182  void installTrap(uint_least16_t addr)
183  {
184  setVal(0xa7ae, JMPw);
185  setVal(0xa7af, endian_16lo8(addr));
186  setVal(0xa7b0, endian_16hi8(addr));
187  }
188 
189  void setSubtune(uint8_t tune)
190  {
191  setVal(0xbf53, LDAb);
192  setVal(0xbf54, tune);
193  setVal(0xbf55, STAa);
194  setVal(0xbf56, 0x0c);
195  setVal(0xbf57, 0x03);
196  setVal(0xbf58, JSRw);
197  setVal(0xbf59, 0x2c);
198  setVal(0xbf5a, 0xa8);
199  setVal(0xbf5b, JMPw);
200  setVal(0xbf5c, 0xb1);
201  setVal(0xbf5d, 0xa7);
202  }
203 };
204 
210 class CharacterRomBank final : public romBank<0x1000> {};
211 
212 }
213 
214 #endif
Definition: Bank.h:36
Definition: SystemROMBanks.h:153
void installTrap(uint_least16_t addr)
Definition: SystemROMBanks.h:182
Definition: SystemROMBanks.h:210
Definition: SystemROMBanks.h:89
void installResetHook(uint_least16_t addr)
Definition: SystemROMBanks.h:140
Definition: SystemROMBanks.h:41
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