File: System.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 (404 lines) | stat: -rw-r--r-- 11,829 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
//============================================================================
//
//    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    
//
//============================================================================

/**
  The 2600 System class.

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

#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#include "BasTerm.hxx"
#include "Error.hxx"
#include "Joystick.hxx"
#include "M6507Hi.hxx"
#include "M6507Lo.hxx"
#include "Paddle.hxx"
#include "Random.hxx"
#include "System.hxx"

#if defined (WIN32)
#include <mmsystem.h>
#endif

//============================================================================
// Constructor
//============================================================================
System::System(BasicTerminal& terminal, const Properties& props)
    : myProperties(&props),
      myTerminal(terminal),
      myConsole(*this),
      myNumberOfFrames(0)
{
  Random random;

  // Setup the memory map
  myMap = new Map[8192];

  myUnmappedDummyData = (uWord)random;
  myUnmappedDummyDataOffset = 0;
 
  for(int i = 0; i < 8192; ++i)
  {
    myMap[i].peekBase = (uByte*)&myUnmappedDummyData;
    myMap[i].peekOffset = &myUnmappedDummyDataOffset;
    myMap[i].pokeBase = (uByte*)&myUnmappedDummyData;
    myMap[i].device = 0;
  }

  myCartridge = Cartridge::create(*this, *myProperties);

  // At this point I don't have any controllers
  myController[0] = myController[1] = 0;

  // Configure controllers by setting my properties
  properties(props);

  // Figure out the minimum frame rate
  switch(myMinimumFrameRate = properties().integer("Display.FrameRate"))
  {
    case 60:
      myFrameRateMultiple = 1; break;
    case 30:
      myFrameRateMultiple = 2; break;
    case 20:
      myFrameRateMultiple = 3; break;
    case 15:
      myFrameRateMultiple = 4; break;
    case 10:
      myFrameRateMultiple = 6; break;
    default:
      myFrameRateMultiple = 1; break;
  }

  // Create each of my sub-systems
  if(strcmp(properties().get("M6507.Compatibility"), "High") == 0)
  {
    myM6507 = new M6507High(*this);
  }
  else
  {
    myM6507 = new M6507Low(*this);
  }

  myM6532 = new M6532(*this);
  myTIA = new TIA(*this);

  // Now that all of the parts are mapped into the system they need resetting
  myCartridge->reset();
  myM6532->reset();
  myM6507->reset();
  myTIA->reset();

  throttleReset(); 
}

//============================================================================
// Destructor
//============================================================================
System::~System()
{
  // Free each of my sub-systems
  delete myCartridge;
  delete myM6532;
  delete myM6507;
  delete myTIA;

  // Free the controllers
  delete myController[0];
  delete myController[1];

  // Free my memory map
  delete myMap;
}

//============================================================================
// Reset myself and all my components
//============================================================================
void System::reset()
{
  myNumberOfFrames = 0;

  myM6532->reset();
  myM6507->reset();
  myTIA->reset();
}

//============================================================================
// Change my properties and configure controllers
//============================================================================
void System::properties(const Properties& props)
{
  // Change my property pointer
  myProperties = &props;

  // Delete old controllers
  delete myController[0];
  delete myController[1];

  // Create the controllers
  const char* joystick0 = properties().find("Controller.Joystick0");
  const char* joystick1 = properties().find("Controller.Joystick1");
  const char* paddle0 = properties().find("Controller.Paddle0");
  const char* paddle1 = properties().find("Controller.Paddle1");
  const char* paddle2 = properties().find("Controller.Paddle2");
  const char* paddle3 = properties().find("Controller.Paddle3");

  // Create the left controller object
  if(strcmp(joystick0, "None") != 0)
  {
    Joystick::PhysicalPort port = Joystick::Zero;

    if(strcmp(joystick0, "0") == 0)
      port = Joystick::Zero;
    else if(strcmp(joystick0, "1") == 0)
      port = Joystick::One;

    myController[0] = new Joystick(*this, port);
  }
  else if((strcmp(paddle0, "None") != 0) || (strcmp(paddle1, "None") != 0))
  {
    Paddle::PhysicalPort paddle0Port = Paddle::None;
    Paddle::PhysicalPort paddle1Port = Paddle::None;

    if(strcmp(paddle0,"None") == 0)
      paddle0Port = Paddle::None;
    else if(strcmp(paddle0, "0") == 0)
      paddle0Port = Paddle::Zero;
    else if(strcmp(paddle0, "1") == 0)
      paddle0Port = Paddle::One;
    else if(strcmp(paddle0, "2") == 0)
      paddle0Port = Paddle::Two;
    else if(strcmp(paddle0, "3") == 0)
      paddle0Port = Paddle::Three;

    if(strcmp(paddle1,"None") == 0)
      paddle1Port = Paddle::None;
    else if(strcmp(paddle1, "0") == 0)
      paddle1Port = Paddle::Zero;
    else if(strcmp(paddle1, "1") == 0)
      paddle1Port = Paddle::One;
    else if(strcmp(paddle1, "2") == 0)
      paddle1Port = Paddle::Two;
    else if(strcmp(paddle1, "3") == 0)
      paddle1Port = Paddle::Three;

    myController[0] = new Paddle(*this, paddle0Port, paddle1Port);
  }
  else
  { 
    // No real device active so just attach the standard controller
    myController[0] = new Controller(*this);
  }

  // Create the right controller object
  if(strcmp(joystick1, "None") != 0)
  {
    Joystick::PhysicalPort port = Joystick::Zero;

    if(strcmp(joystick1, "0") == 0)
      port = Joystick::Zero;
    else if(strcmp(joystick1, "1") == 0)
      port = Joystick::One;

    myController[1] = new Joystick(*this, port);
  }
  else if((strcmp(paddle2, "None") != 0) || (strcmp(paddle3, "None") != 0))
  {
    Paddle::PhysicalPort paddle2Port = Paddle::None;
    Paddle::PhysicalPort paddle3Port = Paddle::None;

    if(strcmp(paddle2,"None") == 0)
      paddle2Port = Paddle::None;
    else if(strcmp(paddle2, "0") == 0)
      paddle2Port = Paddle::Zero;
    else if(strcmp(paddle2, "1") == 0)
      paddle2Port = Paddle::One;
    else if(strcmp(paddle2, "2") == 0)
      paddle2Port = Paddle::Two;
    else if(strcmp(paddle2, "3") == 0)
      paddle2Port = Paddle::Three;

    if(strcmp(paddle3,"None") == 0)
      paddle3Port = Paddle::None;
    else if(strcmp(paddle3, "0") == 0)
      paddle3Port = Paddle::Zero;
    else if(strcmp(paddle3, "1") == 0)
      paddle3Port = Paddle::One;
    else if(strcmp(paddle3, "2") == 0)
      paddle3Port = Paddle::Two;
    else if(strcmp(paddle3, "3") == 0)
      paddle3Port = Paddle::Three;

    myController[1] = new Paddle(*this, paddle2Port, paddle3Port);
  }
  else
  { 
    // No real device active so just attach the standard controller
    myController[1] = new Controller(*this);
  }
}
 
//============================================================================
// This routine should be called on a regular basis to update the system
//============================================================================
void System::update()
{
  // Update my components
  myConsole.update();
  myController[0]->update();
  myController[1]->update();

  if((myNumberOfFrames % myFrameRateMultiple) == 0)
  {
    tia().frame(true);
    terminal().update(tia());
    ++myNumberOfFrames;
  }
  else
  {
    tia().frame(false);
    ++myNumberOfFrames;
  }
}

//============================================================================
// Throttle the frame rate so games don't run to fast
//============================================================================
void System::throttle()
{
#ifdef THROTTLE
  if(myM6507->cycles() >= myCycleLimit)
  {
#if defined (WIN32)
    while((double)timeGetTime() < myClockLimit)
#else
    while((double)clock() < myClockLimit)
#endif
    {
      if(myTerminal.eventState(BasicTerminal::Quit)) 
        break;
    }

    myCycleLimit += myCyclesPerThrottle;
    myClockLimit += myClocksPerThrottle;
  }
#endif
}

//============================================================================
// Reset the throttle variables based on current system state
//============================================================================
void System::throttleReset()
{
#ifdef THROTTLE
  // Some systems don't define CLOCKS_PER_SEC so we'll assume microseconds
  #ifdef CLOCKS_PER_SEC
    double clocks = CLOCKS_PER_SEC;
  #else
    double clocks = 1000000.0;
  #endif

  myClocksPerThrottle = clocks / (double)myMinimumFrameRate;
  myCyclesPerThrottle = 1193333.0 / (double)myMinimumFrameRate;
  myCycleLimit = (double)m6507().cycles();

  #if defined(WIN32)
    myClockLimit = (double)clock();
  #else
    myClockLimit = (double)clock();
  #endif
#endif
}

//============================================================================
// Answer a reference to the controller
//============================================================================
Controller& System::controller(uByte port) const
{
  if(port == 0)
    return *myController[0];
  else
    return *myController[1];
}

//============================================================================
// Map all peeks at address to the given device using base and offset
//============================================================================
void System::mapPeek(uWord address, Device& device, uByte* base, uWord* offset)
{
  address = address & 0x1fff;

  // An address cannot be mapped to two different devices
  assert((myMap[address].device == 0) || (myMap[address].device == &device));

  myMap[address].device = &device;
  myMap[address].peekBase = base;
  myMap[address].peekOffset = offset;
}

//============================================================================
// Map all peeks at address to the given device
//============================================================================
void System::mapPeek(uWord address, Device& device)
{
  address = address & 0x1fff;

  myMap[address].device = &device;
  myMap[address].peekBase = (uByte*)&myUnmappedDummyData;
  myMap[address].peekOffset = &myUnmappedDummyDataOffset;
}

//============================================================================
// Map all pokes at address to the given device using base and offset
//============================================================================
void System::mapPoke(uWord address, Device& device, uByte* base)
{
  address = address & 0x1fff;

  myMap[address].device = &device;
  myMap[address].pokeBase = base;
}

//============================================================================
// Map all pokes at address to the given device
//============================================================================
void System::mapPoke(uWord address, Device& device)
{
  address = address & 0x1fff;

  myMap[address].device = &device;
  myMap[address].pokeBase = (uByte*)&myUnmappedDummyData;
}

//============================================================================
// Remove any mappings associated with the given device
//============================================================================
void System::unmap(Device& device)
{
  for(int i = 0; i < 8192; ++i)
  {
    if(myMap[i].device == &device)
    {
      myMap[i].device = 0;
      myMap[i].peekBase = (uByte*)&myUnmappedDummyData;
      myMap[i].peekOffset = &myUnmappedDummyDataOffset;
      myMap[i].pokeBase = (uByte*)&myUnmappedDummyData;
    }
  }
}