File: Props.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 (563 lines) | stat: -rw-r--r-- 16,472 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
//============================================================================
//
//    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    
//
//============================================================================

/**
  Maintains a collection of property names and values.  Upon
  construction the property object contains some reasonable default
  values for most properties as well as "Cartridge.Image" set to
  the name of the ROM image and "Cartridge.Id" set to the
  cartridge's computed identification.

  @author  Bradford W. Mott
  @version $Id: Props.cxx,v 1.2 1997/05/17 19:00:06 bwmott Exp $
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fstream.h>

#include "machine.hxx"
#include "Error.hxx"
#include "Props.hxx"

//============================================================================
// Constructor
//============================================================================
Properties::Properties(const char* filename)
{
  mySize = 64;
  myProperties = new Property[mySize];
  myNumberOfProperties = 0;

  // Set the default property values
  set("Cartridge.Directory", "");
  set("Cartridge.Name", "Unknown");
  set("Cartridge.Image", "Unknown");
  set("Cartridge.Id", "Unknown");
  set("Cartridge.Type", "4K");

  set("Display.FrameRate", "60");
  set("Display.XStart", "0");
  set("Display.Width", "160");
  set("Display.YStart", "38");
  set("Display.Height", "210");

  set("Console.LeftDifficulty", "B");
  set("Console.RightDifficulty", "B");
  set("Console.TelevisionType", "Color");

  set("Controller.Paddle0", "None");
  set("Controller.Paddle1", "None");
  set("Controller.Paddle2", "None");
  set("Controller.Paddle3", "None");

  set("Controller.Joystick0", "0");
  set("Controller.Joystick1", "1");

  set("Timer.Adjustment", "0");
  set("TIA.PlayerDelay", "1");
  set("M6507.Compatibility", "Low");

  try
  {
    // Autodetect some of the properties based on the ROM image
    autodetect(filename);

    // Make sure all the values are reasonable
    validate();
  }
  catch(...)
  {
    // Free each of the properties
    for(uLong i = 0; i < myNumberOfProperties; ++i)
    {
      delete[] myProperties[i].keyword;
      delete[] myProperties[i].value;
    }

    // Free the properties array
    delete[] myProperties;
    
    // Propagate the exception
    throw;
  }
}

//============================================================================
// Destructor
//============================================================================
Properties::~Properties()
{
  // Free each of the properties
  for(uLong i = 0; i < myNumberOfProperties; ++i)
  {
    delete[] myProperties[i].keyword;
    delete[] myProperties[i].value;
  }

  // Free the properties array
  delete[] myProperties;
}

//============================================================================
// Answer the value of the property or the empty string if it doesn't exist
//============================================================================
const char* Properties::find(const char* property) const
{
  // Find the named property and answer its value
  for(uLong i = 0; i < myNumberOfProperties; ++i)
  {
    if(strcmp(property, myProperties[i].keyword) == 0)
      return myProperties[i].value;
  }

  return 0;
}

//============================================================================
// Answer the integer value of the property
//============================================================================
int Properties::integer(const char* property) const
{
  return atoi(find(property));
}

//============================================================================
// Answer the value assigned to property
//============================================================================
const char* Properties::get(const char* property) const
{
  // Find the named property and answer its value
  for(uLong i = 0; i < myNumberOfProperties; ++i)
  {
    if(strcmp(property, myProperties[i].keyword) == 0)
      return myProperties[i].value;
  }

  return "";
}

//============================================================================
// Set the value of property to the specified value
//============================================================================
void Properties::set(const char* keyword, const char* value)
{
  // See if the property already exists
  for(uLong i = 0; i < myNumberOfProperties; ++i)
  {
    if(strcmp(keyword, myProperties[i].keyword) == 0)
    {
      delete[] myProperties[i].value;
      char* v = new char[strlen(value) + 1];
      strcpy(v, value);
      myProperties[i].value = v;
      return;
    }
  }

  // See if my array needs to be resized
  if(myNumberOfProperties == mySize)
  {
    Property* newProperties = new Property[mySize * 2];

    for(uLong i = 0; i < mySize; ++i)
      newProperties[i] = myProperties[i];

    delete[] myProperties;
    myProperties = newProperties;
    mySize *= 2;
  } 

  char* k = new char[strlen(keyword) + 1];
  char* v = new char[strlen(value) + 1];
  strcpy(k, keyword);
  strcpy(v, value);
  myProperties[myNumberOfProperties].keyword = k;
  myProperties[myNumberOfProperties].value = v;

  ++myNumberOfProperties;
}

//============================================================================
// Make sure the values assigned to properties are reasonable
//============================================================================
void Properties::validate()
{
  // TODO: Check other values to make sure they're ok

  // Make sure the starting x and width values are ok
  if(integer("Display.XStart") % 4 != 0)
  {
    Error err;
    err.message() << "There is a problem with the 'stella.vcs' file!";
    err.description() << "Display.XStart must be a multiple of four!";
    Throw(err);
  }

  if(integer("Display.Width") % 4 != 0)
  {
    Error err;
    err.message() << "There is a problem with the 'stella.vcs' file!";
    err.description() << "Display.Width must be a multiple of four!";
    Throw(err);
  }

  // Let's check the controller properties
  const char* joystick0 = find("Controller.Joystick0");
  const char* joystick1 = find("Controller.Joystick1");
  const char* paddle0 = find("Controller.Paddle0");
  const char* paddle1 = find("Controller.Paddle1");
  const char* paddle2 = find("Controller.Paddle2");
  const char* paddle3 = find("Controller.Paddle3");

  // Check the Controller.Joystick0 property
  if((strcmp(joystick0, "None") != 0) &&
     (strcmp(joystick0, "0") != 0) &&
     (strcmp(joystick0, "1") != 0))
  {
    Error err;
    err.message() << "There is a problem with the 'stella.vcs' file!";
    err.description()
        << "Invalid Controller.Joystick0 value!  Please use the value " 
        << "0, 1 or None for the Controller.Joystick0 property.";
    Throw(err);
  }

  // Check the Controller.Joystick1 property
  if((strcmp(joystick1, "None") != 0) &&
     (strcmp(joystick1, "0") != 0) &&
     (strcmp(joystick1, "1") != 0))
  {
    Error err;
    err.message() << "There is a problem with the 'stella.vcs' file!";
    err.description()
        << "Invalid Controller.Joystick1 value!  Please use the value " 
        << "0, 1 or None for the Controller.Joystick1 property.";
    Throw(err);
  }

  // Check the Controller.Paddle0 property
  if((strcmp(paddle0, "None") != 0) &&
     (strcmp(paddle0, "0") != 0) &&
     (strcmp(paddle0, "1") != 0) &&
     (strcmp(paddle0, "2") != 0) &&
     (strcmp(paddle0, "3") != 0))
  {
    Error err;
    err.message() << "There is a problem with the 'stella.vcs' file!";
    err.description()
        << "Invalid Controller.Paddle0 value!  Please use the value " 
        << "0, 1, 2, 3 or None for the Controller.Paddle0 property.";
    Throw(err);
  }

  // Check the Controller.Paddle1 property
  if((strcmp(paddle1, "None") != 0) &&
     (strcmp(paddle1, "0") != 0) &&
     (strcmp(paddle1, "1") != 0) &&
     (strcmp(paddle1, "2") != 0) &&
     (strcmp(paddle1, "3") != 0))
  {
    Error err;
    err.message() << "There is a problem with the 'stella.vcs' file!";
    err.description()
        << "Invalid Controller.Paddle1 value!  Please use the value " 
        << "0, 1, 2, 3 or None for the Controller.Paddle1 property.";
    Throw(err);
  }

  // Check the Controller.Paddle2 property
  if((strcmp(paddle2, "None") != 0) &&
     (strcmp(paddle2, "0") != 0) &&
     (strcmp(paddle2, "1") != 0) &&
     (strcmp(paddle2, "2") != 0) &&
     (strcmp(paddle2, "3") != 0))
  {
    Error err;
    err.message() << "There is a problem with the 'stella.vcs' file!";
    err.description()
        << "Invalid Controller.Paddle2 value!  Please use the value " 
        << "0, 1, 2, 3 or None for the Controller.Paddle2 property.";
    Throw(err);
  }

  // Check the Controller.Paddle3 property
  if((strcmp(paddle3, "None") != 0) &&
     (strcmp(paddle3, "0") != 0) &&
     (strcmp(paddle3, "1") != 0) &&
     (strcmp(paddle3, "2") != 0) &&
     (strcmp(paddle3, "3") != 0))
  {
    Error err;
    err.message() << "There is a problem with the 'stella.vcs' file!";
    err.description()
        << "Invalid Controller.Paddle3 value!  Please use the value " 
        << "0, 1, 2, 3 or None for the Controller.Paddle3 property.";
    Throw(err);
  }

  // Make sure the setup makes a little sense
  if((strcmp(joystick0, "None") != 0) &&
      ((strcmp(paddle0, "None") != 0) || (strcmp(paddle1, "None") != 0)))
  {
    Error err;
    err.message() << "There is a problem with the 'stella.vcs' file!";
    err.description()
        << "The 'stella.vcs' has joystick 0 and paddle 0 or 1 active" << endl
        << "which is not possible.  Please modify the file and turn" << endl
        << "off either the joystick or the paddles.";
    Throw(err);
  }

  if((strcmp(joystick1, "None") != 0) &&
      ((strcmp(paddle2, "None") != 0) || (strcmp(paddle3, "None") != 0)))
  {
    Error err;
    err.message() << "There is a problem with the 'stella.vcs' file!";
    err.description()
        << "The 'stella.vcs' has joystick10 and paddle 2 or 3 active" << endl
        << "which is not possible.  Please modify the file and turn" << endl
        << "off either the joystick or the paddles.";
    Throw(err);
  }
}

//============================================================================
// Autodetect properties from the given ROM file
//============================================================================
void Properties::autodetect(const char* file)
{
  char* dirname = new char[strlen(file) + 1];
  char* filename = new char[strlen(file) + 1];

  // Break file up into dirname and filename
  const char* separator = strrchr(file, PATH_SEPARATOR);
  if(separator == 0)
  {
    strcpy(filename, file);
    strcpy(dirname, "");
  }
  else
  {
    strcpy(filename, separator + 1);
    strncpy(dirname, file, separator - file);
    dirname[separator - file] = 0;
  }

  // Calculate the cart's id
  char id[128];
  calculateId(file, id);

  // Attempt to get the cart's type based on ROM image and Cartridge Id
  const char* type = cartridgeType(file, id);

  // Supercharger games require the CPU to be in high compability mode
  if(strcmp(type, "AR") == 0)
  {
    set("M6507.Compatibility", "High");
  }
 
  set("Cartridge.Name", filename);
  set("Cartridge.Directory", dirname);
  set("Cartridge.Image", filename);
  set("Cartridge.Type", type);
  set("Cartridge.Id", id);

  delete[] dirname;
  delete[] filename;
}

//============================================================================
// Calculate the Cartridge Id associated with the ROM Image
//============================================================================
void Properties::calculateId(const char* file, char* id)
{
  // Get the filename
  const char* separator = strrchr(file, PATH_SEPARATOR);
  const char* filename = (separator == 0) ? file : separator + 1;

  ifstream in;

  #if defined UNIX_OS
    in.open(file, ios::in | ios::nocreate);
  #elif defined MAC_OS
    in.open(file, ios::in | ios::binary);
  #else
    in.open(file, ios::in | ios::nocreate | ios::binary);
  #endif

  // Make sure we were able to open the file
  if(in.fail())
  {
    // Can't open file so throw exception
    Error err;
    err.message() << "The file \"" << filename << "\" couldn't be opened...";
    Throw(err);
  }
  else
  {
    // Read ROM from the input stream
    uByte rom[32768];
    in.read(rom, 32768);

    // Calculate Cartridge ID
    uLong cartId = 0;
    for(uWord i = 1024; i < 2048; i++)
    {
      cartId += (((uLong)rom[i]) << ((i & 0x0003) * 8));
    }

    sprintf(id, "%08x", cartId); 
  }
}

//============================================================================
// Determine the cartridge's type
//============================================================================
const char* Properties::cartridgeType(const char* file, const char* id)
{
  struct IdToType {
    const char* id;
    const char* type;
  };

  // Maps a cartridge id to its type (for those hard to guess carts :-)
  static IdToType table[] = {
    {"e9422fb1", "E0"},    // Death Star
    {"b82650c9", "E0"},    // Gyruss
    {"98553b40", "E0"},    // Super Cobra
    {"45dfd9a3", "E0"},    // Tutankamn
    {"48a54df0", "E0"},    // Popeye
    {"2ca40acd", "E0"},    // Star Wars, Arcade
    {"84b60c35", "E0"},    // Frogger2
    {"80a2714d", "E0"},    // Montezuma's Revenge
    {"15dd4774", "F6SC"},  // Dig Dug
    {"9f99ada9", "F8SC"},  // Defender ][
    {"37f268a9", "FASC"},  // Tunnel runner
    {"ce3d8002", "FASC"},  // Mountain King
    {"b0cb2c2e", "FASC"},  // Omega Race
    {"d439a04a", "E7"},    // Burger Timer
    {"ffffff00", "E7"},    // Bump-N-Jump
    {"c3af1519", "E7"},    // He-man
    {"",         ""}
  };

  // Get the filename
  const char* separator = strrchr(file, PATH_SEPARATOR);
  const char* filename = (separator == 0) ? file : separator + 1;
 
  // Open ROM image so we can look at it
  ifstream in;

  #if defined UNIX_OS
    in.open(file, ios::in | ios::nocreate);
  #elif defined MAC_OS
    in.open(file, ios::in | ios::binary);
  #else
    in.open(file, ios::in | ios::nocreate | ios::binary);
  #endif

  // Make sure we were able to open the file
  if(in.fail())
  {
    // Can't open file so throw exception
    Error err;
    err.message() << "The file \"" << filename << "\" couldn't be opened...";
    Throw(err);
  }
  else
  {
    // Read ROM from the input stream and see how big it is
    uByte rom[32768];
    in.read(rom, 32768); 
    uLong size = in.gcount();

    // Make sure the size is reasonable
    if(((size % 2048) != 0) && (size != 0) && ((size % 8448) != 0))
    {
      Error err;
      err.message() << "The file \"" << filename << "\" doesn't seem to be "
          << "a ROM image file...";
      Throw(err);
    }

    // See if this cartridge is listed in the table
    for(IdToType* entry = table; *entry->id != 0; ++entry)
    {
      if(strcmp(entry->id, id) == 0)
        return entry->type;
    }

    // Calculate the type of the cartridge
    if((size % 8448) == 0)
    {
      // Supercharger ROMs are always a multiple of 8448
      return "AR";
    }
    if(size == 2048)
    {
      return "2K";
    }
    else
    {
      if(memcmp(rom, rom + 2048, 2048) == 0)
      {
        return "2K";
      }
      else if(size == 4096)
      {
        return "4K";
      }
      else
      {
        if(memcmp(rom, rom + 4096, 4096) == 0)
        {
          return "4K";
        }
        else if(size == 8192)
        {
          return "F8";
        }
        else
        {
          if(memcmp(rom, rom + 8192, 8192) == 0)
          {
            return "F8";
          }
          else if(size == 12288)
          {
            return "FASC";
          }
          else if(size == 32768)
          {
            return "F4SC";
          }
          else
          {
            // See if this cart has 128 bytes of RAM
            uByte first = rom[0];
            for(uWord i = 0; i < 256; ++i)
            {
              if(rom[i] != first)
                return "F6";
            }
            return "F6SC";
          }
        }
      }
    } 
  }

  return "4K";
}