8
8
// SS SS tt ee ll ll aa aa
9
9
// SSSS ttt eeeee llll llll aaaaa
11
// Copyright (c) 1995-2008 by Bradford W. Mott and the Stella team
11
// Copyright (c) 1995-2010 by Bradford W. Mott, Stephen Anthony
12
// and the Stella Team
13
// See the file "license" for information on usage and redistribution of
14
// See the file "License.txt" for information on usage and redistribution of
14
15
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
16
17
// $Id: CartSB.cxx,v 1.0 2007/10/11
17
18
//============================================================================
21
23
#include "System.hxx"
22
24
#include "CartSB.hxx"
24
26
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
25
27
CartridgeSB::CartridgeSB(const uInt8* image, uInt32 size)
27
myLastBank((mySize>>12)-1)
29
30
// Allocate array for the ROM image
30
31
myImage = new uInt8[mySize];
32
33
// Copy the ROM image into my buffer
33
for(uInt32 addr = 0; addr < mySize; ++addr)
34
myImage[addr] = image[addr];
34
memcpy(myImage, image, mySize);
36
// Remember startup bank
37
myStartBank = (mySize >> 12) - 1;
37
40
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
43
46
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
44
47
void CartridgeSB::reset()
46
// Upon reset we switch to the last bank
49
// Upon reset we switch to the startup bank
50
53
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
68
71
myHotSpotPageAccess[6] = mySystem->getPageAccess(0x0E00 >> shift);
69
72
myHotSpotPageAccess[7] = mySystem->getPageAccess(0x0F00 >> shift);
74
System::PageAccess access;
71
76
// Set the page accessing methods for the hot spots
72
System::PageAccess access;
77
access.directPeekBase = 0;
78
access.directPokeBase = 0;
80
access.type = System::PA_READ;
73
81
for(uInt32 i = 0x0800; i < 0x0FFF; i += (1 << shift))
75
access.directPeekBase = 0;
76
access.directPokeBase = 0;
78
82
mySystem->setPageAccess(i >> shift, access);
81
84
// Install pages for startup bank
85
88
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
86
89
uInt8 CartridgeSB::peek(uInt16 address)
88
address = address & (0x17FF+(mySize>>12));
91
address = address & (0x17FF + (mySize >> 12));
90
93
// Switch banks if necessary
91
94
if ((address & 0x1800) == 0x0800)
92
bank(address & myLastBank);
95
bank(address & myStartBank);
94
97
if(!(address & 0x1000))
105
108
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
106
void CartridgeSB::poke(uInt16 address, uInt8 value)
109
bool CartridgeSB::poke(uInt16 address, uInt8 value)
108
address = address & (0x17FF+(mySize>>12));
111
address = address & (0x17FF + (mySize >> 12));
110
113
// Switch banks if necessary
111
114
if((address & 0x1800) == 0x0800)
112
bank(address & myLastBank);
115
bank(address & myStartBank);
114
117
if(!(address & 0x1000))
118
121
int hotspot = ((address & 0x0F00) >> 8) - 8;
119
122
myHotSpotPageAccess[hotspot].device->poke(address, value);
123
127
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
124
128
void CartridgeSB::bank(uInt16 bank)
126
if(myBankLocked) return;
130
if(bankLocked()) return;
128
132
// Remember what bank we're in
129
133
myCurrentBank = bank;
130
uInt32 offset = myCurrentBank * 4096;
134
uInt32 offset = myCurrentBank << 12;
131
135
uInt16 shift = mySystem->pageShift();
133
137
// Setup the page access methods for the current bank
134
138
System::PageAccess access;
139
access.directPokeBase = 0;
135
140
access.device = this;
136
access.directPokeBase = 0;
141
access.type = System::PA_READ;
138
143
// Map ROM image into the system
139
144
for(uInt32 address = 0x1000; address < 0x2000; address += (1 << shift))
141
146
access.directPeekBase = &myImage[offset + (address & 0x0FFF)];
142
147
mySystem->setPageAccess(address >> shift, access);
149
myBankChanged = true;
146
152
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
147
int CartridgeSB::bank()
153
uInt16 CartridgeSB::bank() const
149
155
return myCurrentBank;
152
158
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
153
int CartridgeSB::bankCount()
159
uInt16 CartridgeSB::bankCount() const
158
164
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
159
165
bool CartridgeSB::patch(uInt16 address, uInt8 value)
162
myImage[myCurrentBank * 4096] = value;
163
bank(myCurrentBank); // TODO: see if this is really necessary
167
myImage[(myCurrentBank << 12) + (address & 0x0FFF)] = value;
168
return myBankChanged = true;
168
171
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
169
uInt8* CartridgeSB::getImage(int& size)
172
const uInt8* CartridgeSB::getImage(int& size) const
175
178
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
176
179
bool CartridgeSB::save(Serializer& out) const
178
string cart = name();
181
const string& cart = name();
182
185
out.putString(cart);
183
186
out.putInt(myCurrentBank);
192
cerr << "Unknown error in save state for " << cart << endl;
188
catch(const char* msg)
190
cerr << "ERROR: CartridgeSB::save" << endl << " " << msg << endl;