File: TermSVGA.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 (301 lines) | stat: -rw-r--r-- 9,658 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
//============================================================================
//
//    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 class provides a standard API for the screen.  This file contains
  the Linux SVGALIB implementation of the API.

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

#include <assert.h>
#include <stdio.h>
#include <iostream.h>

#include "colors.h"
#include "Term.hxx"
#include "TIA.hxx"

// Indicates if an instance is alive
bool Terminal::ourInstanceCreated = false;

// Contains the state of each of the events based on the keyboard
static volatile uLong theKeyboardEventState[TERMINAL_NUMBER_OF_EVENTS];

// Contains the state of each of the events based on the joystick
static volatile uLong theJoystickEventState[TERMINAL_NUMBER_OF_EVENTS];

static uWord thePixelDataTable[256];

// Indicates the width and height based on the property information
static uLong theWidth;
static uLong theHeight;
static uLong theXStart;

#ifdef LINUX_JOYSTICK
  #include <unistd.h>
  #include <fcntl.h>
  #include <linux/joystick.h>

  // File descriptors for the joystick devices
  int theLeftJoystickFd;
  int theRightJoystickFd;
#endif

#include <termios.h>
#include <vga.h>
#include <vgamouse.h>
#include <vgakeyboard.h>

// Define scancodes for 1,2,3 and 4 if they aren't defined
#ifndef SCANCODE_1
  #define SCANCODE_1                      2
  #define SCANCODE_2                      3
  #define SCANCODE_3                      4
  #define SCANCODE_4                      5
#endif

// Buffer for the terminal settings
struct termios theOldTerminalSettings;

//============================================================================
// Called whenever a key is pressed or released
//============================================================================
static void HandleKeyEvent(int scanCode, int press)
{
  struct Switches
  {
    int scanCode;
    Terminal::Event eventCode;
  };

  static Switches list[] = {
    { SCANCODE_CURSORBLOCKDOWN,   Terminal::LeftJoystickDown },
    { SCANCODE_CURSORBLOCKUP,     Terminal::LeftJoystickUp },
    { SCANCODE_CURSORBLOCKLEFT,   Terminal::LeftJoystickLeft },
    { SCANCODE_CURSORBLOCKRIGHT,  Terminal::LeftJoystickRight },
    { SCANCODE_SPACE,             Terminal::LeftJoystickFire }, 
    { SCANCODE_ENTER,             Terminal::LeftJoystickFire }, 
    { SCANCODE_H,                 Terminal::RightJoystickDown },
    { SCANCODE_Y,                 Terminal::RightJoystickUp },
    { SCANCODE_G,                 Terminal::RightJoystickLeft },
    { SCANCODE_J,                 Terminal::RightJoystickRight },
    { SCANCODE_Z,                 Terminal::RightJoystickFire }, 
    { SCANCODE_C,                 Terminal::Color },
    { SCANCODE_B,                 Terminal::BlackAndWhite },
    { SCANCODE_1,                 Terminal::LeftDifficultyA },
    { SCANCODE_2,                 Terminal::LeftDifficultyB },
    { SCANCODE_3,                 Terminal::RightDifficultyA },
    { SCANCODE_4,                 Terminal::RightDifficultyB },
    { SCANCODE_S,                 Terminal::Select },
    { SCANCODE_R,                 Terminal::Reset },
    { SCANCODE_P,                 Terminal::Pause },
    { SCANCODE_Q,                 Terminal::Quit }
  };

  for(unsigned int i = 0; i < sizeof(list) / sizeof(Switches); ++i)
  {
    if(list[i].scanCode == scanCode)
    {
      theKeyboardEventState[(int)list[i].eventCode] = 
          (press == KEY_EVENTPRESS) ? 1 : 0;
    }
  }
}

//============================================================================
// Constructor
//============================================================================
Terminal::Terminal(const Properties& properties)
    : myProperties(&properties)
{
  assert(!ourInstanceCreated);
  ourInstanceCreated = true;

  // Clear the terminal event state
  for(uLong i = 0; i < TERMINAL_NUMBER_OF_EVENTS; i++) 
  {
    theKeyboardEventState[i] = 0;
    theJoystickEventState[i] = 0;
  }

  // Setup the pixel data table
  for(uLong j = 0; j < 256; ++j)
  {
    thePixelDataTable[j] = j | (j << 8);
  }

  // Enable automatic mouse setup
  vga_setmousesupport(1);

  // Init the SVGALIB library
  vga_init();
  vga_setmode(G320x200x256);
  vga_waitretrace();

  for(int index = 0; index < 256; ++index)
  {
    vga_setpalette(index, (theColorTable[index] & 0x00ff0000) >> 18,
                          (theColorTable[index] & 0x0000ff00) >> 10,
                          (theColorTable[index] & 0x000000ff) >> 2);
  }

  // Set the mouse range from 0 to 255
  mouse_setxrange(0, 512);

  tcgetattr(0, &theOldTerminalSettings);
  keyboard_init();
  keyboard_seteventhandler(HandleKeyEvent);

#ifdef LINUX_JOYSTICK
  // Open the joystick devices 
  theLeftJoystickFd = open("/dev/js0", O_RDONLY);
  theRightJoystickFd = open("/dev/js1", O_RDONLY);
#endif

  // Get the desired width and height of the display
  theWidth = myProperties->integer("Display.Width");
  theXStart = myProperties->integer("Display.XStart");
  theHeight = myProperties->integer("Display.Height");
}

//============================================================================
// Destructor
//============================================================================
Terminal::~Terminal()
{
  keyboard_close();

  // Restore text mode
  tcsetattr(0, 0, &theOldTerminalSettings);
  vga_setmode(TEXT);

#ifdef LINUX_JOYSTICK
  if(theLeftJoystickFd >= 0)
    close(theLeftJoystickFd);

  if(theRightJoystickFd >= 0)
    close(theRightJoystickFd);
#endif

  ourInstanceCreated = false;
}

//============================================================================
// Update the screen with the TIA frame
//============================================================================
void Terminal::update(TIA& tia)
{
  uByte* currentFrame = tia.currentFrameBuffer();
  uByte* previousFrame = tia.previousFrameBuffer();

  uLong width = theWidth / 4;
  uLong height = (theHeight > 200) ? 200 : theHeight;

  int offset = ((200 - height) / 2) * 320;
  uWord* data = (uWord*)(vga_getgraphmem() + offset) + ((160 - theWidth) / 2);

  for(uLong y = 0; y < height; ++y)
  {
    uLong* current = (uLong*)(currentFrame + theXStart);
    uLong* previous = (uLong*)(previousFrame + theXStart);
    uWord* screen = data;
    for(uLong x = 0; x < width; ++x, ++current, ++previous)
    {
      if(*current != *previous)
      {
        uByte* frame = (uByte*)current;
        *(screen++) = thePixelDataTable[*frame++];
        *(screen++) = thePixelDataTable[*frame++];
        *(screen++) = thePixelDataTable[*frame++];
        *(screen++) = thePixelDataTable[*frame];
      }
      else
      {
        screen += 4;
      }
    }
    currentFrame += 160;
    previousFrame += 160;
    data += 160;
  }

  // Handle any events that have occured
  handleEvents();
}

//============================================================================
// Answer the state of the given event
//============================================================================
uLong Terminal::eventState(Event event)
{
  // If the value is not set by joystick then return the keyboard's value
  if(theJoystickEventState[(int)event] == 0)
    return theKeyboardEventState[(int)event];
  else
    return theJoystickEventState[(int)event];
}

//============================================================================
// Process any events related to the terminal
//============================================================================
void Terminal::handleEvents()
{
  // Update events based on any keys that have been pressed or released
  keyboard_update();

  // Handle any mouse events
  mouse_update();

  theJoystickEventState[(int)Terminal::PaddleZeroPosition] = 512 - mouse_getx();
  theJoystickEventState[(int)Terminal::PaddleZeroFire] = 
      mouse_getbutton() & MOUSE_LEFTBUTTON;
  
#ifdef LINUX_JOYSTICK
  // Read joystick values and modify event states
  if(theLeftJoystickFd >= 0)
  {
    struct JS_DATA_TYPE joystick;
    read(theLeftJoystickFd, &joystick, JS_RETURN);

    theJoystickEventState[(int)Terminal::LeftJoystickFire] = 
        (joystick.buttons & 0x01) ? 1 : 0;
    theJoystickEventState[(int)Terminal::LeftJoystickLeft] = 
        (joystick.x < 250) ? 1 : 0;
    theJoystickEventState[(int)Terminal::LeftJoystickRight] = 
        (joystick.x > 750) ? 1 : 0;
    theJoystickEventState[(int)Terminal::LeftJoystickUp] = 
        (joystick.y < 250) ? 1 : 0;
    theJoystickEventState[(int)Terminal::LeftJoystickDown] = 
        (joystick.y > 750) ? 1 : 0;
  }

  if(theRightJoystickFd >= 0)
  {
    struct JS_DATA_TYPE joystick;
    read(theRightJoystickFd, &joystick, JS_RETURN);

    theJoystickEventState[(int)Terminal::RightJoystickFire] = 
        (joystick.buttons & 0x01) ? 1 : 0;
    theJoystickEventState[(int)Terminal::RightJoystickLeft] = 
        (joystick.x < 250) ? 1 : 0;
    theJoystickEventState[(int)Terminal::RightJoystickRight] = 
        (joystick.x > 750) ? 1 : 0;
    theJoystickEventState[(int)Terminal::RightJoystickUp] = 
        (joystick.y < 250) ? 1 : 0;
    theJoystickEventState[(int)Terminal::RightJoystickDown] = 
        (joystick.y > 750) ? 1 : 0;
  }
#endif
}