File: CartAR.cxx

package info (click to toggle)
stella 0.7-2
  • links: PTS
  • area: non-free
  • in suites: hamm, slink
  • size: 864 kB
  • ctags: 1,158
  • sloc: cpp: 6,615; ansic: 492; makefile: 224; asm: 31
file content (347 lines) | stat: -rw-r--r-- 9,700 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//============================================================================
//
//    SSSS    tt          lll  lll              
//   SS  SS   tt           ll   ll                
//   SS     tttttt  eeee   ll   ll   aaaa    "An Atari 2600 VCS Emulator"
//    SSSS    tt   ee  ee  ll   ll      aa      
//       SS   tt   eeeeee  ll   ll   aaaaa   Copyright (c) 1995,1996,1997
//   SS  SS   tt   ee      ll   ll  aa  aa         Bradford W. Mott
//    SSSS     ttt  eeeee llll llll  aaaaa    
//
//============================================================================

/**
  This is the cartridge class for Arcadia (aka Starpath) Supercharger
  games.  Christopher Salomon provided most of the details and code
  used for this class.

  The Supercharger has four 2K banks.  There are three banks of RAM
  and one bank of ROM.  All 6K of the RAM can be read and written.

  @author  Bradford W. Mott
  @version $Id: CartAR.cxx,v 1.1 1997/05/17 19:04:12 bwmott Exp $
*/

#include "machine.hxx"
#include "CartAR.hxx"
#include "Error.hxx"
#include "System.hxx"
#include "M6507Hi.hxx"

//============================================================================
// Constructor
//============================================================================
CartridgeAR::CartridgeAR(System& system, uByte* image, uLong size)
    : Cartridge(system)
{
  uWord i;

  // Create a load image buffer and copy the given image
  myLoadImages = new uByte[size];
  myNumberOfLoadImages = size / 8448;

  for(i = 0; i < size; ++i)
  {
    myLoadImages[i] = image[i];
  } 

  // Map all of my addresses in the system
  for(uWord addr = 0; addr < 8192; ++addr)
  {
    if(addr & 0x1000)
    {
      mySystem.mapPeek(addr, *this);
    }
  }

  // Set the current image to load 0
  setupCurrentImage(0);
}
 
//============================================================================
// Destructor
//============================================================================
CartridgeAR::~CartridgeAR()
{
  delete[] myLoadImages;
}

//============================================================================
// Answer the byte at the given address
//============================================================================
uByte CartridgeAR::peek(uWord addr)
{
  // Are they trying to access the tape rewind routine?
  if((myImageOffset[1] == 3 * 2048) && (addr == 0x180A))
  {
    uLong cycles = mySystem.m6507().cycles();

    // Yes, so let's do something...
    if((myPower && ((cycles - myPowerRomCycle) < 1000)) || !myPower)
    {
      Error err;
      err.message() << "Supercharger: The ROM has not powered up...";
      Throw(err);
    }
    else
    {
      Error err;
      err.message() << "Supercharger: Can't handle rewinding tape...";
      Throw(err);
    }
  }

  // Are they trying to access the multiload routine?
  if((myImageOffset[1] == 3 * 2048) && ((addr & 0x0FFF) == 0x0800))
  {
    uLong cycles = mySystem.m6507().cycles();

    if((myPower && ((cycles - myPowerRomCycle) < 1000)) || !myPower)
    {
      Error err;
      err.message() << "Supercharger: The ROM has not been powered up...";
      Throw(err);
    }
    else
    {
      // Get the load they're trying to access
      uByte load = mySystem.peek(0x00FA);
 
      // Setup specified load as the current image
      setupCurrentImage(load);

      return myImage[(addr & 0x07FF) + myImageOffset[1]];
    }
  } 

  uWord operandAddress = LastOperandAddress;

  // Handle bank configuration
  if(((addr & 0x0FFF) == 0x0FF8) && operandAddress)
  {
    uWord operandOffset = LastOperandOffset;
    bankConfiguration((uByte)operandAddress + operandOffset);

    PokePossible = false;
    return myImage[(addr & 0x07FF) + myImageOffset[1]];
  }

  // Handle poke if writing enabled
  if(myWriteEnabled && PokePossible)
  {
    if(((operandAddress & 0x1fff) >= 0x1000) &&
        ((operandAddress & 0x1fff) <= 0x10ff))
    {
      uLong operandCycle = LastOperandCycle;
      uLong cycles = mySystem.m6507().cycles();

      if(((cycles-operandCycle) > 3) && ((cycles-operandCycle) < 7))
      {
        uWord operandOffset = LastOperandOffset;
        uByte value = operandAddress + operandOffset;

        if((addr & 0x0fff)>=0x800)
        {
          myImage[(addr & 0x7ff) + myImageOffset[1]] = value;
        }
        else
        {
          myImage[(addr & 0x7ff) + myImageOffset[0]] = value;
        }

        PokePossible = false;
      } 
    }
  }

  if(addr < 0x1800)
    return myImage[(addr & 0x07FF) + myImageOffset[0]];
  else
    return myImage[(addr & 0x07FF) + myImageOffset[1]];
}

//============================================================================
// Reset to power on state
//============================================================================
void CartridgeAR::reset()
{
  // Set the current image to load 0
  setupCurrentImage(0);

  // Set bank configuration upon reset so ROM is selected
  myPower = true;
  myPowerRomCycle = 0;
  myWriteEnabled = false;

  myImageOffset[0] = 0 * 2048;
  myImageOffset[1] = 3 * 2048;
}

//============================================================================
// Handle a change to the bank configuration
//============================================================================
void CartridgeAR::bankConfiguration(uByte configuration)
{
  // D7-D5 of this byte: Write Pulse Delay (n/a for emulator)
  //
  // D4-D0: RAM/ROM configuration:
  //       $F000-F7FF    $F800-FFFF Address range that banks map into
  //  000wp     2            ROM
  //  001wp     0            ROM
  //  010wp     2            0      as used in Commie Mutants and many others
  //  011wp     0            2      as used in Suicide Mission
  //  100wp     2            ROM
  //  101wp     1            ROM
  //  110wp     2            1      as used in Killer Satellites
  //  111wp     1            2      as we use for 2k/4k ROM cloning
  // 
  //  w = Write Enable (1 = enabled; accesses to $F000-$F0FF cause writes
  //    to happen.  0 = disabled, and the cart acts like ROM.)
  //  p = ROM Power (0 = enabled, 1 = off.)  Only power the ROM if you're
  //    wanting to access the ROM for multiloads.  Otherwise set to 1.

  myPower = !(configuration & 0x01);

  if(myPower)
  {
    myPowerRomCycle = mySystem.m6507().cycles();
  }

  myWriteEnabled = configuration & 0x02;

  switch((configuration >> 2) & 0x07)
  {
    case 0:
    {
      myImageOffset[0] = 2 * 2048;
      myImageOffset[1] = 3 * 2048;
      break;
    }

    case 1:
    {
      myImageOffset[0] = 0 * 2048;
      myImageOffset[1] = 3 * 2048;
      break;
    }

    case 2:
    {
      myImageOffset[0] = 2 * 2048;
      myImageOffset[1] = 0 * 2048;
      break;
    }

    case 3:
    {
      myImageOffset[0] = 0 * 2048;
      myImageOffset[1] = 2 * 2048;
      break;
    }

    case 4:
    {
      myImageOffset[0] = 2 * 2048;
      myImageOffset[1] = 3 * 2048;
      break;
    }

    case 5:
    {
      myImageOffset[0] = 1 * 2048;
      myImageOffset[1] = 3 * 2048;
      break;
    }

    case 6:
    {
      myImageOffset[0] = 2 * 2048;
      myImageOffset[1] = 1 * 2048;
      break;
    }

    case 7:
    {
      myImageOffset[0] = 1 * 2048;
      myImageOffset[1] = 2 * 2048;
      break;
    }
  }
}

//============================================================================
// Sets up a "dummy" bootstrap ROM in the ROM bank of the cartridge
//============================================================================
void CartridgeAR::setupROM()
{
  static uByte dummyROMCode[] = {
0xa9, 0x0, 0xa2, 0x0, 0x95, 0x80, 0xe8, 0xe0, 
0x80, 0xd0, 0xf9, 0x4c, 0x2b, 0xfa, 0xff, 0xff, 
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 
0xa9, 0x0, 0xa2, 0x0, 0x95, 0x80, 0xe8, 0xe0, 
0x1e, 0xd0, 0xf9, 0xa2, 0x0, 0xbd, 0x44, 0xfa, 
0x95, 0xf7, 0xe8, 0xe0, 0x9, 0xd0, 0xf6, 0xa2, 
0xff, 0xa0, 0x0, 0xa9, 0x0, 0x85, 0x80, 0x85, 
0xf8, 0x4c, 0xf7, 0x0, 0xcd, 0x0, 0xf0, 0xad, 
0xf8, 0xff, 0x4c, 0x0, 0x0
  };

  int size = sizeof(dummyROMCode);

  // Copy the "dummy" ROM code into the ROM area
  for(int i = 0; i < size; ++i)
  {
    myImage[0x1A00 + i] = dummyROMCode[i];
  }

  // Put a JMP $FA20 at multiload entry point ($F800)
  myImage[0x1800] = 0x4C;
  myImage[0x1801] = 0x20;
  myImage[0x1802] = 0xFA;

  // Update ROM code to have the correct reset address and bank configuration
  myImage[0x1A00 + size - 2] = myHeader[0];
  myImage[0x1A00 + size - 1] = myHeader[1];
  myImage[0x1A00 + size - 17] = myHeader[2];

  // Finally set vectors to point to this "dummy" code at 0xFA00
  myImage[3 * 2048 + 2044] = 0x00;
  myImage[3 * 2048 + 2045] = 0xFA;
  myImage[3 * 2048 + 2046] = 0x00;
  myImage[3 * 2048 + 2047] = 0xFA;
}

//============================================================================
// Handle setting the current image from the load images
//============================================================================
void CartridgeAR::setupCurrentImage(uByte load)
{
  uByte image;
  uWord j;

  // Scan through the ROM looking for the given load
  for(image = 0; image < myNumberOfLoadImages; ++image)
  {
    // Copy the ROM image into my buffer
    for(j = 0; j < 8192; ++j)
      myImage[j] = myLoadImages[(image * 8448) + j];
 
    // Copy the Supercharger "header" from the image
    for(j = 0; j < 256; ++j)
      myHeader[j] = myLoadImages[(image * 8448) + 8192 + j];

    if(myHeader[5] == load)
    {
      setupROM();
      return;
    }
  }

  // Could find the requested load :-(
  Error err;
  err.message() << "Supercharger: Multiload image is missing in ROM image...";
  err.description() << "ROM image is corrupt or not complete." << endl;
  Throw(err);
}