libsidplayfp  2.0.2
SystemROMBanks.h
1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2012-2015 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 
40 template <int N>
41 class romBank : public Bank
42 {
43 protected:
45  uint8_t rom[N];
46 
47 protected:
51  void setVal(uint_least16_t address, uint8_t val) { rom[address & (N-1)]=val; }
52 
56  uint8_t getVal(uint_least16_t address) const { return rom[address & (N-1)]; }
57 
61  void* getPtr(uint_least16_t address) const { return (void*)&rom[address & (N-1)]; }
62 
63 public:
67  void set(const uint8_t* source) { if (source != nullptr) memcpy(rom, source, N); }
68 
72  void poke(uint_least16_t, uint8_t) override {}
73 
77  uint8_t peek(uint_least16_t address) override { return rom[address & (N-1)]; }
78 };
79 
85 class KernalRomBank final : public romBank<0x2000>
86 {
87 private:
88  uint8_t resetVectorLo; // 0xfffc
89  uint8_t resetVectorHi; // 0xfffd
90 
91 public:
92  void set(const uint8_t* kernal)
93  {
94  romBank<0x2000>::set(kernal);
95 
96  if (kernal == nullptr)
97  {
98  // IRQ entry point
99  setVal(0xffa0, PHAn); // Save regs
100  setVal(0xffa1, TXAn);
101  setVal(0xffa2, PHAn);
102  setVal(0xffa3, TYAn);
103  setVal(0xffa4, PHAn);
104  setVal(0xffa5, JMPi); // Jump to IRQ routine
105  setVal(0xffa6, 0x14);
106  setVal(0xffa7, 0x03);
107 
108  // Halt
109  setVal(0xea39, 0x02);
110 
111  // Hardware vectors
112  setVal(0xfffa, 0x39); // NMI vector
113  setVal(0xfffb, 0xea);
114  setVal(0xfffc, 0x39); // RESET vector
115  setVal(0xfffd, 0xea);
116  setVal(0xfffe, 0xa0); // IRQ/BRK vector
117  setVal(0xffff, 0xff);
118  }
119 
120  // Backup Reset Vector
121  resetVectorLo = getVal(0xfffc);
122  resetVectorHi = getVal(0xfffd);
123  }
124 
125  void reset()
126  {
127  // Restore original Reset Vector
128  setVal(0xfffc, resetVectorLo);
129  setVal(0xfffd, resetVectorHi);
130  }
131 
137  void installResetHook(uint_least16_t addr)
138  {
139  setVal(0xfffc, endian_16lo8(addr));
140  setVal(0xfffd, endian_16hi8(addr));
141  }
142 };
143 
149 class BasicRomBank final : public romBank<0x2000>
150 {
151 private:
152  uint8_t trap[3];
153  uint8_t subTune[11];
154 
155 public:
156  void set(const uint8_t* basic)
157  {
158  romBank<0x2000>::set(basic);
159 
160  // Backup BASIC Warm Start
161  memcpy(trap, getPtr(0xa7ae), sizeof(trap));
162 
163  memcpy(subTune, getPtr(0xbf53), sizeof(subTune));
164  }
165 
166  void reset()
167  {
168  // Restore original BASIC Warm Start
169  memcpy(getPtr(0xa7ae), trap, sizeof(trap));
170 
171  memcpy(getPtr(0xbf53), subTune, sizeof(subTune));
172  }
173 
179  void installTrap(uint_least16_t addr)
180  {
181  setVal(0xa7ae, JMPw);
182  setVal(0xa7af, endian_16lo8(addr));
183  setVal(0xa7b0, endian_16hi8(addr));
184  }
185 
186  void setSubtune(uint8_t tune)
187  {
188  setVal(0xbf53, LDAb);
189  setVal(0xbf54, tune);
190  setVal(0xbf55, STAa);
191  setVal(0xbf56, 0x0c);
192  setVal(0xbf57, 0x03);
193  setVal(0xbf58, JSRw);
194  setVal(0xbf59, 0x2c);
195  setVal(0xbf5a, 0xa8);
196  setVal(0xbf5b, JMPw);
197  setVal(0xbf5c, 0xb1);
198  setVal(0xbf5d, 0xa7);
199  }
200 };
201 
207 class CharacterRomBank final : public romBank<0x1000> {};
208 
209 }
210 
211 #endif
libsidplayfp::KernalRomBank::installResetHook
void installResetHook(uint_least16_t addr)
Definition: SystemROMBanks.h:137
libsidplayfp::Bank
Definition: Bank.h:36
libsidplayfp::romBank::set
void set(const uint8_t *source)
Definition: SystemROMBanks.h:67
libsidplayfp::romBank::getPtr
void * getPtr(uint_least16_t address) const
Definition: SystemROMBanks.h:61
libsidplayfp::romBank::setVal
void setVal(uint_least16_t address, uint8_t val)
Definition: SystemROMBanks.h:51
libsidplayfp::BasicRomBank::installTrap
void installTrap(uint_least16_t addr)
Definition: SystemROMBanks.h:179
libsidplayfp::BasicRomBank
Definition: SystemROMBanks.h:150
libsidplayfp::KernalRomBank
Definition: SystemROMBanks.h:86
libsidplayfp::romBank::poke
void poke(uint_least16_t, uint8_t) override
Definition: SystemROMBanks.h:72
libsidplayfp::romBank::getVal
uint8_t getVal(uint_least16_t address) const
Definition: SystemROMBanks.h:56
libsidplayfp::CharacterRomBank
Definition: SystemROMBanks.h:207
libsidplayfp::romBank::rom
uint8_t rom[N]
The ROM array.
Definition: SystemROMBanks.h:45
libsidplayfp::romBank::peek
uint8_t peek(uint_least16_t address) override
Definition: SystemROMBanks.h:77
libsidplayfp::romBank
Definition: SystemROMBanks.h:42