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