/
emulator.cpp
461 lines (425 loc) · 11.6 KB
/
emulator.cpp
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
/*
epple2
Copyright (C) 2008 by Chris Mosher <chris@mosher.mine.nu>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY, without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "emulator.h"
#include "configep2.h"
#include "e2const.h"
#include <SDL/SDL.h>
#include <ctime>
Emulator::Emulator():
display(screenImage),
videoStatic(display),
apple2(keypresses,paddleButtonStates,display,fhyper,buffered,screenImage),
timable(0), // No ticked object (NULL pointer)
quit(false),
repeat(false),
keysDown(0),
command(false),
pendingCommandExit(false)
{
}
Emulator::~Emulator()
{
}
void Emulator::toggleComputerPower()
{
if (this->timable==&this->videoStatic)
powerOnComputer();
else
powerOffComputer();
}
void Emulator::powerOnComputer()
{
this->apple2.powerOn();
this->screenImage.drawPower(true);
this->display.setNoise(false);
// The apple2 becomes the ticked object
this->timable = &this->apple2;
}
void Emulator::powerOffComputer()
{
// TODO Need to ask user if OK to lose any unsaved changes to disks
this->apple2.powerOff();
this->screenImage.drawPower(false);
this->display.setNoise(true);
this->videoStatic.powerOn();
// The video static becomes the ticked object
this->timable = &this->videoStatic;
}
void Emulator::config(Config& cfg)
{
cfg.parse(this->apple2.ram,this->apple2.rom,this->apple2.slts,this->apple2.revision,this->screenImage,this->apple2.cassette);
}
void Emulator::init()
{
powerOffComputer();
this->display.setType(AnalogTV::MONITOR_COLOR);
this->display.powerOn(true);
}
// How many emulation ticks between asking SDL if there is any new input
// from the user or other GUI events.
// This is also how often we shall update the estimate of the emulator's
// actual speed performance
// When the CPU is the object being ticked (each tick is a CPU cycle), then
// this is 20.04378892 Hz in emulated seconds time
#define CHECK_EVERY_CYCLE 51024
#define CHECK_CYCLES_K 51024000
#define EXPECTED_MS 50
// U.A.2 p. 7-13: REPT key repeats at 10Hz.
static const int CYCLES_PER_REPT(E2Const::AVG_CPU_HZ/10);
// The core of this Apple
int Emulator::run()
{
int skip = CHECK_EVERY_CYCLE;
Uint32 prev_ms = SDL_GetTicks();
// While the user still wants to run this emulation...
while (!this->quit)
{
// (Obligatory protection against NULL object pointer)
if (this->timable)
{
this->timable->tick();
// If the Apple ][ keyboard repeat is on (the REPT key is
// down)...
if (this->repeat)
{
// Count our way down to when the timer for the REPT key
// fires off: 10Hz in terms of how many CPU cycles have gone
// by
--this->rept;
// If it's time for the REPT key timer to fire (at long
// last)...
if (this->rept <= 0)
{
// ...reload the timer for the next firing 1/10 second from
// now ( *reset* the timer )
this->rept = CYCLES_PER_REPT;
// If any other keys are actually being held down...
if (this->keysDown > 0)
{
// ...REPEAT the most recent one that was pressed
this->keypresses.push(this->lastKeyDown);
}
}
}
}
// People who have too many press releases should be referred to as
// keyboards
--skip;
// If skip has been decremented to zero...
if (!skip)
{
// ...then it's time to drain away any piled-up user interaction
// events that SDL has stored up for us
// Reload the skip quantity
skip = CHECK_EVERY_CYCLE;
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
// If SDL is going away...
case SDL_QUIT:
this->quit = true;
break;
case SDL_KEYDOWN:
// If we're collecting a command line for changing any
// of the configurables of the emulator...
if (this->command)
cmdKey(event.key);
else
// ...else we're collecting keypresses for the keyboard
// emulation (and thus the Apple ][ emulation itself)
dispatchKeypress(event.key);
break;
case SDL_KEYUP:
// If we're collecting a command line for changing any
// of the configurables of the emulator...
if (this->command)
{
if (this->pendingCommandExit)
{
this->command = false;
this->pendingCommandExit = false;
}
}
else
{
// ...else we're collecting keypresses for the keyboard
// emulation (and thus the Apple ][ emulation itself)
dispatchKeyUp(event.key);
}
break;
}
}
// If we're trying to run as slow as a real Apple ][...
if (!this->fhyper.isHyper())
{
const int delta_ms = EXPECTED_MS-(SDL_GetTicks()-prev_ms);
if (0 < delta_ms && delta_ms <= EXPECTED_MS)
{
SDL_Delay(delta_ms);
}
}
// Display the current estimate of the emulator's actual speed
// performance
this->screenImage.displayHz(CHECK_CYCLES_K/(SDL_GetTicks()-prev_ms));
prev_ms = SDL_GetTicks();
}
}
return 0;
}
void Emulator::dispatchKeyUp(const SDL_KeyboardEvent& keyEvent)
{
unsigned char key = (unsigned char)(keyEvent.keysym.unicode & 0x7F);
SDLKey sym = keyEvent.keysym.sym;
SDLMod mod = keyEvent.keysym.mod;
unsigned char scancode = keyEvent.keysym.scancode;
// printf("key UP: %d sym: %d mod: %04X scn: %d\n",key,sym,mod,scancode);
if ((sym < 0x7F || sym == SDLK_LEFT || sym == SDLK_RIGHT) &&
!(sym == SDLK_TAB || sym == SDLK_BACKQUOTE || sym == '[' || sym == '\\' || sym == SDLK_DELETE) &&
!(sym == ']' && mod&KMOD_SHIFT))
{
--this->keysDown;
}
// ...else if this is the emulated REPT key on the Apple keyboard...
else if (sym == SDLK_F10)
{
// ...stop repeating. The key has been released
this->repeat = false;
this->rept = 0;
}
}
// Take real-world keystrokes from SDL and filter them to emulate the
// Apple ][ or Apple ][ plus keyboard
void Emulator::dispatchKeypress(const SDL_KeyboardEvent& keyEvent)
{
unsigned char key = (unsigned char)(keyEvent.keysym.unicode & 0x7F);
SDLKey sym = keyEvent.keysym.sym;
SDLMod mod = keyEvent.keysym.mod;
unsigned char scancode = keyEvent.keysym.scancode;
// printf("key DN: %d sym: %d mod: %04X scn: %d\n",key,sym,mod,scancode);
if ((sym < 0x7F || sym == SDLK_LEFT || sym == SDLK_RIGHT) &&
!(sym == SDLK_TAB || sym == SDLK_BACKQUOTE || sym == '[' || sym == '\\' || sym == SDLK_DELETE) &&
!(sym == ']' && mod&KMOD_SHIFT))
{
++this->keysDown;
}
if (sym == SDLK_LEFT)
{
key = 8;
}
else if (sym == SDLK_RIGHT)
{
key = 21;
}
else if (sym == SDLK_PAUSE)
{
this->apple2.reset();
return;
}
else if (sym == SDLK_INSERT)
{
// Feed input from the clipboard to the Apple keyboard
std::string s = this->clip.getText();
for (unsigned int i = 0; i < s.length(); ++i)
{
key = s[i];
// TODO fix pasting line-endings
if (key == '\n')
key = '\r';
if ('a' <= key && key <= 'z')
{
key -= 32;
}
this->keypresses.push(key);
}
return;
}
// ...else if this is the emulated REPT key on the Apple keyboard...
else if (sym == SDLK_F10)
{
// ...start auto-repeat
this->repeat = true;
this->rept = CYCLES_PER_REPT;
return;
}
// ...else if the user wants to run at full speed instead of emulating
// the Apple's speed...
else if (sym == SDLK_F11)
{
this->fhyper.toggleHyper();
this->screenImage.toggleHyperLabel();
return;
}
else if (sym == SDLK_F12)
{
this->buffered.toggleBuffered();
this->screenImage.toggleKdbBufferLabel();
return;
}
// ...else if the user has hit the rocker switch on the back of the Apple...
else if (sym == SDLK_F1)
{
toggleComputerPower();
return;
}
// ...else if the user wants to look at a different video display medium...
else if (sym == SDLK_F2)
{
this->display.cycleType();
this->screenImage.cycleDisplayLabel();
return;
}
// ...else if the user wants to switch to/from full screen and an
// individual application window...
else if (sym == SDLK_F3)
{
this->screenImage.toggleFullScreen();
this->screenImage.drawPower(this->timable==&this->apple2);
return;
}
// ...else if the user wants to switch between the interlaced extension
// of the display and the non-interlaced historically correct display...
else if (sym == SDLK_F4)
{
this->display.toggleBleedDown();
this->screenImage.toggleFillLinesLabel();
return;
}
// ...else initiate command line entry at the bottom of the emulator window
else if (sym == SDLK_F5)
{
this->command = true;
this->screenImage.enterCommandMode();
}
// ...else exit the entire emulation
else if (sym == SDLK_END)
{
this->quit = true;
return;
}
// ...else save a screen shot
else if (sym == SDLK_PRINT)
{
this->screenImage.saveBMP();
}
// The unmodified Apple ][ hardware keyboard only generates upper-case
if ('a' <= key && key <= 'z')
{
key -= 32;
}
if ((mod&KMOD_SHIFT) && (mod&KMOD_CTRL) && sym == '2')
{
// Ctrl-Shift-2 == Ctrl-@ == NUL == ASCII: 0
key = 0;
}
else if ((mod&KMOD_SHIFT) && (mod&KMOD_CTRL) && sym == ' ')
{
// Ctrl-Shift-Space is the same as Space
key = ' ';
}
else if ((mod&KMOD_CTRL) && !(mod&KMOD_SHIFT) && SDLK_KP0 <= sym && sym <= SDLK_KP9)
{
// Control-only numeric keypad keys are converted to regular digit keys
key = sym-SDLK_KP0+'0';
}
else if ((mod&KMOD_CTRL) && !(mod&KMOD_SHIFT) && (('0' <= sym && sym <= '9') || sym == '/' || sym == ' '))
{
// Control-only upon 0-9, / and space leaves them unchanged, the
// same as unmodified
key = sym;
}
else if (sym == ']')
{
if (mod&KMOD_SHIFT)
{
// ignore '}' (shift ']')
return;
}
if (mod&KMOD_CTRL)
{
// Ctrl-] == ASCII: $1D
key = 29;
}
}
// ...else if this is one of the keys that can't be typed on an Apple ][
// keyboard...
else if (key == 0 || sym == SDLK_TAB || sym == SDLK_BACKQUOTE || sym == '[' || sym == '\\' || sym == SDLK_DELETE)
{
return;
}
// printf(" sending to apple as ascii------------------------------>%02X (%02X)\n",key,key|0x80);
this->keypresses.push(key);
this->lastKeyDown = key;
}
// Collect and edit a command line typed at the bottom of the emulator
// window
void Emulator::cmdKey(const SDL_KeyboardEvent& keyEvent)
{
unsigned char key = (unsigned char)(keyEvent.keysym.unicode & 0x7F);
SDLKey sym = keyEvent.keysym.sym;
if (sym == SDLK_RETURN)
{
processCommand();
}
else if (sym == SDLK_ESCAPE)
{
cmdline.erase(cmdline.begin(),cmdline.end());
processCommand();
}
else if (sym == SDLK_BACKSPACE)
{
if (cmdline.length())
{
cmdline.erase(cmdline.end()-1);
this->screenImage.backspaceCommand();
}
}
else if (sym == SDLK_INSERT)
{
std::string s = this->clip.getText();
for (unsigned int i = 0; i < s.length(); ++i)
{
key = s[i];
if (key == '\n' || key == '\r')
{
processCommand();
break;
}
else
{
cmdline += key;
this->screenImage.addkeyCommand(key);
}
}
}
else if (key)
{
cmdline += key;
this->screenImage.addkeyCommand(key);
}
}
// Process a command line typed at the bottom of the emulator window
void Emulator::processCommand()
{
this->screenImage.exitCommandMode();
this->pendingCommandExit = true;
if (cmdline.empty())
{
return;
}
Config::parseLine(cmdline,this->apple2.ram,this->apple2.rom,this->apple2.slts,this->apple2.revision,this->screenImage,this->apple2.cassette);
cmdline.erase(cmdline.begin(),cmdline.end());
}