libsidplayfp  1.0.1
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 <string.h>
26 #include <stdint.h>
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:
44  void setVal(uint_least16_t address, uint8_t val) { rom[address & (N-1)]=val; }
45 
47  uint8_t getVal(uint_least16_t address) const { return rom[address & (N-1)]; }
48 
50  void* getPtr(uint_least16_t address) const { return (void*)&rom[address & (N-1)]; }
51 
52 public:
54  void set(const uint8_t* source) { if (source) memcpy(rom, source, N); }
55 
57  void poke(uint_least16_t address SID_UNUSED, uint8_t value SID_UNUSED) {}
58 
59  uint8_t peek(uint_least16_t address) { return rom[address & (N-1)]; }
60 };
61 
65 class KernalRomBank : public romBank<0x2000>
66 {
67 private:
68  uint8_t resetVectorLo; // 0xfffc
69  uint8_t resetVectorHi; // 0xfffd
70 
71 public:
72  void set(const uint8_t* kernal)
73  {
74  romBank<0x2000>::set(kernal);
75 
76  if (kernal)
77  {
78  // Apply Kernal hacks
79  setVal(0xfd69, 0x9f); // Bypass memory check
80  setVal(0xe55f, 0x00); // Bypass screen clear
81  setVal(0xfdc4, 0xea); // Ignore sid volume reset to avoid DC
82  setVal(0xfdc5, 0xea); // click (potentially incompatibility)!!
83  setVal(0xfdc6, 0xea);
84  }
85  else
86  {
87  // Normal IRQ interrupt
88  setVal(0xea31, LDAa); // Ack IRQs
89  setVal(0xea32, 0x0D);
90  setVal(0xea33, 0xDC);
91  setVal(0xea34, PLAn); // Restore regs
92  setVal(0xea35, TAYn);
93  setVal(0xea36, PLAn);
94  setVal(0xea37, TAXn);
95  setVal(0xea38, PLAn);
96  setVal(0xea39, RTIn); // Return
97 
98  // Hardware setup routine
99  setVal(0xff84, LDAa); // Set CIA 1 Timer A
100  setVal(0xff85, 0xa6);
101  setVal(0xff86, 0x02);
102  setVal(0xff87, BEQr);
103  setVal(0xff88, 0x06);
104  setVal(0xff89, LDAb); // PAL
105  setVal(0xff8a, 0x25);
106  setVal(0xff8b, LDXb);
107  setVal(0xff8c, 0x40);
108  setVal(0xff8d, BNEr);
109  setVal(0xff8e, 0x04);
110  setVal(0xff8f, LDAb); // NTSC
111  setVal(0xff90, 0x95);
112  setVal(0xff91, LDXb);
113  setVal(0xff92, 0x42);
114  setVal(0xff93, STAa);
115  setVal(0xff94, 0x04);
116  setVal(0xff95, 0xdc);
117  setVal(0xff96, STXa);
118  setVal(0xff97, 0x05);
119  setVal(0xff98, 0xdc);
120  setVal(0xff99, LDAb); // Set SID to maximum volume
121  setVal(0xff9a, 0x0f);
122  setVal(0xff9b, STAa);
123  setVal(0xff9c, 0x18);
124  setVal(0xff9d, 0xd4);
125  setVal(0xff9e, RTSn);
126 
127  // IRQ entry point
128  setVal(0xffa0, PHAn); // Save regs
129  setVal(0xffa1, TXAn);
130  setVal(0xffa2, PHAn);
131  setVal(0xffa3, TYAn);
132  setVal(0xffa4, PHAn);
133  setVal(0xffa5, JMPi); // Jump to IRQ routine
134  setVal(0xffa6, 0x14);
135  setVal(0xffa7, 0x03);
136 
137  // Hardware vectors
138  setVal(0xfffa, 0x39); // NMI vector
139  setVal(0xfffb, 0xea);
140  setVal(0xfffc, 0x39); // RESET vector
141  setVal(0xfffd, 0xea);
142  setVal(0xfffe, 0xa0); // IRQ/BRK vector
143  setVal(0xffff, 0xff);
144  }
145 
146  // Backup Reset Vector
147  resetVectorLo = getVal(0xfffc);
148  resetVectorHi = getVal(0xfffd);
149  }
150 
151  void reset()
152  {
153  // Restore original Reset Vector
154  setVal(0xfffc, resetVectorLo);
155  setVal(0xfffd, resetVectorHi);
156  }
157 
163  void installResetHook(uint_least16_t addr)
164  {
165  setVal(0xfffc, endian_16lo8(addr));
166  setVal(0xfffd, endian_16hi8(addr));
167  }
168 };
169 
173 class BasicRomBank : public romBank<0x2000>
174 {
175 private:
176  uint8_t trap[3];
177  uint8_t subTune[11];
178 
179 public:
180  void set(const uint8_t* basic)
181  {
182  romBank<0x2000>::set(basic);
183 
184  // Backup BASIC Warm Start
185  memcpy(trap, getPtr(0xa7ae), 3);
186 
187  memcpy(subTune, getPtr(0xbf53), 11);
188  }
189 
190  void reset()
191  {
192  // Restore original BASIC Warm Start
193  memcpy(getPtr(0xa7ae), trap, 3);
194 
195  memcpy(getPtr(0xbf53), subTune, 11);
196  }
197 
203  void installTrap(uint_least16_t addr)
204  {
205  setVal(0xa7ae, JMPw);
206  setVal(0xa7af, endian_16lo8(addr));
207  setVal(0xa7b0, endian_16hi8(addr));
208  }
209 
210  void setSubtune(uint8_t tune)
211  {
212  setVal(0xbf53, LDAb);
213  setVal(0xbf54, tune);
214  setVal(0xbf55, STAa);
215  setVal(0xbf56, 0x0c);
216  setVal(0xbf57, 0x03);
217  setVal(0xbf58, JSRw);
218  setVal(0xbf59, 0x2c);
219  setVal(0xbf5a, 0xa8);
220  setVal(0xbf5b, JMPw);
221  setVal(0xbf5c, 0xb1);
222  setVal(0xbf5d, 0xa7);
223  }
224 };
225 
229 class CharacterRomBank : public romBank<0x1000> {};
230 
231 #endif