libsidplayfp  1.5.3
SystemROMBanks.h
1 /*
2  * This file is part of libsidplayfp, a SID player engine.
3  *
4  * Copyright 2012-2013 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 "sidplayfp/c64/CPU/opcodes.h"
30 
35 template <int N>
36 class romBank : public Bank
37 {
38 protected:
40  uint8_t rom[N];
41 
42 protected:
46  void setVal(uint_least16_t address, uint8_t val) { rom[address & (N-1)]=val; }
47 
51  uint8_t getVal(uint_least16_t address) const { return rom[address & (N-1)]; }
52 
56  void* getPtr(uint_least16_t address) const { return (void*)&rom[address & (N-1)]; }
57 
58 public:
62  void set(const uint8_t* source) { if (source) memcpy(rom, source, N); }
63 
67  void poke(uint_least16_t address SID_UNUSED, uint8_t value SID_UNUSED) {}
68 
69  uint8_t peek(uint_least16_t address) { return rom[address & (N-1)]; }
70 };
71 
75 class KernalRomBank : public romBank<0x2000>
76 {
77 private:
78  uint8_t resetVectorLo; // 0xfffc
79  uint8_t resetVectorHi; // 0xfffd
80 
81 public:
82  void set(const uint8_t* kernal)
83  {
84  romBank<0x2000>::set(kernal);
85 
86  if (kernal)
87  {
88  // Apply Kernal hacks
89  // FIXME these are tailored to the original kernals
90  // may not work as intended on other roms
91  setVal(0xfd69, 0x9f); // Bypass memory check
92  setVal(0xe55f, 0x00); // Bypass screen clear
93  setVal(0xfdc4, 0xea); // Ignore sid volume reset to avoid DC
94  setVal(0xfdc5, 0xea); // click (potentially incompatibility)!!
95  setVal(0xfdc6, 0xea);
96  }
97  else
98  {
99  // Normal IRQ interrupt
100  setVal(0xea31, LDAa); // Ack IRQs
101  setVal(0xea32, 0x0D);
102  setVal(0xea33, 0xDC);
103  setVal(0xea34, PLAn); // Restore regs
104  setVal(0xea35, TAYn);
105  setVal(0xea36, PLAn);
106  setVal(0xea37, TAXn);
107  setVal(0xea38, PLAn);
108  setVal(0xea39, RTIn); // Return
109 
110  // Hardware setup routine
111  setVal(0xff84, LDAa); // Set CIA 1 Timer A
112  setVal(0xff85, 0xa6);
113  setVal(0xff86, 0x02);
114  setVal(0xff87, BEQr);
115  setVal(0xff88, 0x06);
116  setVal(0xff89, LDAb); // PAL
117  setVal(0xff8a, 0x25);
118  setVal(0xff8b, LDXb);
119  setVal(0xff8c, 0x40);
120  setVal(0xff8d, BNEr);
121  setVal(0xff8e, 0x04);
122  setVal(0xff8f, LDAb); // NTSC
123  setVal(0xff90, 0x95);
124  setVal(0xff91, LDXb);
125  setVal(0xff92, 0x42);
126  setVal(0xff93, STAa);
127  setVal(0xff94, 0x04);
128  setVal(0xff95, 0xdc);
129  setVal(0xff96, STXa);
130  setVal(0xff97, 0x05);
131  setVal(0xff98, 0xdc);
132  setVal(0xff99, LDAb); // Set SID to maximum volume
133  setVal(0xff9a, 0x0f);
134  setVal(0xff9b, STAa);
135  setVal(0xff9c, 0x18);
136  setVal(0xff9d, 0xd4);
137  setVal(0xff9e, RTSn);
138 
139  // IRQ entry point
140  setVal(0xffa0, PHAn); // Save regs
141  setVal(0xffa1, TXAn);
142  setVal(0xffa2, PHAn);
143  setVal(0xffa3, TYAn);
144  setVal(0xffa4, PHAn);
145  setVal(0xffa5, JMPi); // Jump to IRQ routine
146  setVal(0xffa6, 0x14);
147  setVal(0xffa7, 0x03);
148 
149  // Hardware vectors
150  setVal(0xfffa, 0x39); // NMI vector
151  setVal(0xfffb, 0xea);
152  setVal(0xfffc, 0x39); // RESET vector
153  setVal(0xfffd, 0xea);
154  setVal(0xfffe, 0xa0); // IRQ/BRK vector
155  setVal(0xffff, 0xff);
156  }
157 
158  // Backup Reset Vector
159  resetVectorLo = getVal(0xfffc);
160  resetVectorHi = getVal(0xfffd);
161  }
162 
163  void reset()
164  {
165  // Restore original Reset Vector
166  setVal(0xfffc, resetVectorLo);
167  setVal(0xfffd, resetVectorHi);
168  }
169 
175  void installResetHook(uint_least16_t addr)
176  {
177  setVal(0xfffc, endian_16lo8(addr));
178  setVal(0xfffd, endian_16hi8(addr));
179  }
180 };
181 
185 class BasicRomBank : public romBank<0x2000>
186 {
187 private:
188  uint8_t trap[3];
189  uint8_t subTune[11];
190 
191 public:
192  void set(const uint8_t* basic)
193  {
194  romBank<0x2000>::set(basic);
195 
196  // Backup BASIC Warm Start
197  memcpy(trap, getPtr(0xa7ae), 3);
198 
199  memcpy(subTune, getPtr(0xbf53), 11);
200  }
201 
202  void reset()
203  {
204  // Restore original BASIC Warm Start
205  memcpy(getPtr(0xa7ae), trap, 3);
206 
207  memcpy(getPtr(0xbf53), subTune, 11);
208  }
209 
215  void installTrap(uint_least16_t addr)
216  {
217  setVal(0xa7ae, JMPw);
218  setVal(0xa7af, endian_16lo8(addr));
219  setVal(0xa7b0, endian_16hi8(addr));
220  }
221 
222  void setSubtune(uint8_t tune)
223  {
224  setVal(0xbf53, LDAb);
225  setVal(0xbf54, tune);
226  setVal(0xbf55, STAa);
227  setVal(0xbf56, 0x0c);
228  setVal(0xbf57, 0x03);
229  setVal(0xbf58, JSRw);
230  setVal(0xbf59, 0x2c);
231  setVal(0xbf5a, 0xa8);
232  setVal(0xbf5b, JMPw);
233  setVal(0xbf5c, 0xb1);
234  setVal(0xbf5d, 0xa7);
235  }
236 };
237 
241 class CharacterRomBank : public romBank<0x1000> {};
242 
243 #endif