File: main.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 (223 lines) | stat: -rw-r--r-- 5,882 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
//============================================================================
//
//    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
//
//============================================================================

#include <time.h>
#include <iostream.h>

#ifdef MSDOS_OS
  #include <strstrea.h>
#else
  #include <strstream.h>
#endif

#include "Term.hxx"
#include "System.hxx"
#include "M6507.hxx"
#include "M6532.hxx"
#include "TIA.hxx"
#include "Snd.hxx"
#include "Cart.hxx"
#include "Joystick.hxx"
#include "Props.hxx"
#include "Error.hxx"
#include "VCS.hxx"
#include "VCSScrpt.hxx"

#if defined DJGPP
  #include <crt0.h>

  // For now we'll just lock all pages so our interrupt routines don't fail
  // TODO: A better solution is needed for this
  int _crt0_startup_flags = _CRT0_FLAG_LOCK_MEMORY;
#endif

#if defined EXCEPTION_FIX
//============================================================================
// Called instead of throwing an exception if EXCEPTION_FIX is defined
//============================================================================
void ExceptionFix(Error& msg)
{
  cerr << "===========" << endl;
  cerr << "FATAL ERROR" << endl;
  cerr << "===========" << endl << endl;
  cerr << msg << endl << endl;
  exit(1);
}

// Dummy error msg
Error msg;
#endif

//============================================================================
// Display usage message
//============================================================================
void usage()
{
  cerr << "Usage: stella filename" << endl;
  exit(1);
}

//============================================================================
// Evaluates the builtin VCS-Script program then evaluates the
// VCS-Script program in 'stella.vcs' if it exists in the 
// current working directory.
//============================================================================
void handleVCSScript(Properties& properties)
{
  VCSScript script;

  // First we evaluate the "built-in" vcs file
  istrstream builtin(defaultVCSScript());
  script.evaluate(builtin, properties);

  // Now, see if there's a file to evaluate
  const char* filename = "stella.vcs";
  ifstream in;

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

  if(!in.fail())
  {
    script.evaluate(in, properties);
  }
}

//============================================================================
// Main loop
//============================================================================
main(int argc, char* argv[])
{
  // Make sure we have the correct number of arguments
  if(argc != 2)
  {
    usage();
  }

  Properties* pProperties = 0;
  Cartridge* pCartridge = 0;
  Terminal* pTerminal = 0;
  System* pSystem = 0;

  try
  {
    // Load 2600 cartridge properties from the given file
    pProperties = new Properties(argv[1]);

    // Use properties in 'stella.vcs' file or the default one
    handleVCSScript(*pProperties);

    // Make sure the cartridge ROM image exists before we create the 
    // terminal object to avoid screen flickers
    Cartridge::exists(*pProperties);

    // Create the terminal object
    pTerminal = new Terminal(*pProperties);

    // Create 2600 system
    pSystem = new System(*pTerminal, *pProperties);

    #ifdef SHOW_TIMING
      time_t startingClockValue = clock();
    #endif

    bool paused = false;
    bool examinePause = false;

    for(;;)
    {
      // Handle the "pause" key
      bool pause = pTerminal->eventState(BasicTerminal::Pause);

      if(!paused && examinePause && pause)
      {
        paused = true;
        examinePause = false;

        // Turn off the TIA sound by setting volume to zero
        Sound& sound = pSystem->tia().sound();
        sound.set(Sound::AUDV0, 0);
        sound.set(Sound::AUDV1, 0);
      }
      else if(paused && examinePause && pause)
      {
        paused = false;
        examinePause = false;

        // We have to reset the throttle or things will be strange
        pSystem->throttleReset();
      }
      else if(!pause)
      {
        examinePause = true;
      }

      // Exit if the Q key has been pressed
      if(pTerminal->eventState(BasicTerminal::Quit))
        break;

      if(paused)
        pTerminal->update(pSystem->tia());
      else
        pSystem->update();

      // Throttle the frame rate so games don't run to fast
      pSystem->throttle();
    }

    #ifdef SHOW_TIMING
      time_t executionTime = clock() - startingClockValue;
    #endif

    delete pTerminal;

    #ifdef SHOW_TIMING
      #ifdef CLOCKS_PER_SEC
        double clocks = CLOCKS_PER_SEC;
      #else
        double clocks = 1000000.0;
      #endif

      double seconds = ((double)executionTime / clocks);
      double cyclesPerSecond = pSystem->m6507().cycles() / seconds;
      double efficiency = (cyclesPerSecond / 1190000.0) * 100; 
      double framesPerSecond = pSystem->numberOfFrames() / seconds;

      cout << endl;
      cout << pSystem->numberOfFrames() << " total frames drawn" << endl;
      cout << cyclesPerSecond << " Cycles/Second\n";
      cout << efficiency << "% real speed\n";
      cout << framesPerSecond << " Frames/Second\n";
      cout << endl;
    #endif

    delete pSystem;
    delete pCartridge;
    delete pProperties;
  }
  catch(Error& msg)
  {
    delete pTerminal;

    cerr << "===========" << endl;
    cerr << "FATAL ERROR" << endl;
    cerr << "===========" << endl << endl;
    cerr << msg << endl << endl;
    exit(1);
  }
}