-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.cpp
More file actions
1504 lines (1368 loc) · 40.6 KB
/
Copy pathmain.cpp
File metadata and controls
1504 lines (1368 loc) · 40.6 KB
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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
YAPE - Yet Another Plus/4 Emulator
The program emulates the Commodore 264 family of 8 bit microcomputers
This program is free software, you are welcome to distribute it,
and/or modify it under certain conditions. For more information,
read 'Copying'.
(c) 2000, 2001, 2004, 2005, 2007, 2015-2026 Attila Grósz
(c) 2005 VENESZ Roland
*/
#define NAME "Yape/SDL 0.81.1"
#define WINDOWX SCREENX
#define WINDOWY SCREENY
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include "keyboard.h"
#include "cpu.h"
#include "tedmem.h"
#include "tape.h"
#include "sound.h"
#include "Sid.h"
#include "archdep.h"
#include "iec.h"
#include "device.h"
#include "tcbm.h"
#include "diskfs.h"
#include "monitor.h"
#include "prg.h"
#include "interface.h"
#include "video.h"
#include "drive.h"
#include "FdcGcr.h"
#include "icon.h"
#include "vic2mem.h"
#include "vicmem.h"
#include "SaveState.h"
#include "keyoverlay.h"
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <emscripten/html5.h>
#define MAX_FRQ_INDEX 1
#else
#define MAX_FRQ_INDEX 5
#endif
// function prototypes
static void frameUpdate();
void setMainLoop(int looptype);
// used as GUI callbacks
static void toggleShowSpeed(void *none);
static void toggleFullThrottle(void *none);
static void toggleCrtEmulation(void *none);
static void toggleVsync(void *none);
static void flipMachineTypeFwd(void *name);
static const char *machineTypeLabel();
static void flipWindowScale(void *none);
static void poll_events(void);
static void toggleTrueDriveEmulation(void *none);
// SDL stuff
static SDL_Window* sdlWindow;
static SDL_Renderer *sdlRenderer;
static SDL_Texture *sdlTexture = NULL;
// class pointer for the user interface
static UI *uinterface = NULL;
// timeout variables
static unsigned int timeOutOverlayKeys = 0;
static unsigned int timeOutMousePointer = 75;
static bool mouseBtnHeld = 0;
////////////////
// Supplementary
// Core
static TED *ted8360 = NULL;
static CPU *machine = NULL;
static CTCBM *tcbm = NULL;
static CIECInterface *iec = NULL;
static CIECDrive *fsdrive = NULL;
static CTrueDrive *drive1541 = NULL;
static FakeSerialDrive *fsd1541 = NULL;
//
static char textout[128];
static char *inipath;
static char *inifile;
unsigned int g_bActive = true;
static unsigned int g_inDebug = false;
static unsigned int g_FrameRate = true;
static unsigned int g_50Hz = true;
static unsigned int g_bSaveSettings = true;
static unsigned int g_bUseOverlay = 0;
static unsigned int g_iWindowMultiplier = 2;
static unsigned int g_iEmulationLevel = 0;
static unsigned int g_bTrueDriveEmulation = 0;
static unsigned int g_bVideoVsync = 0;
static char lastSnapshotName[512] = "";
static unsigned int pixels[568 * SCR_VSIZE * 2];
extern bool openGLInit(SDL_Window* wnd, unsigned int windowWidth, unsigned int windowHeight,
unsigned int width, unsigned int height, unsigned int sync);
extern void openGLFrameUpdate(SDL_Window* wnd, void* texture, unsigned int width, unsigned int height, unsigned int sync);
static rvar_t mainSettings[] = {
{ "Show framerate", "DisplayFrameRate", toggleShowSpeed, &g_FrameRate, RVAR_TOGGLE, NULL },
{ "Display debug info", "DisplayQuickDebugInfo", NULL, &g_inDebug, RVAR_TOGGLE, NULL },
{ "Speed limit", "50HzTimerActive", toggleFullThrottle, &g_50Hz, RVAR_TOGGLE, NULL },
{ "Window scale", "WindowMultiplier", flipWindowScale, &g_iWindowMultiplier, RVAR_INT, NULL },
{ "Machine type", "EmulationLevel", flipMachineTypeFwd, &g_iEmulationLevel, RVAR_STRING_FLIPLIST, &machineTypeLabel },
{ "CRT emulation", "CRTEmulation", toggleCrtEmulation, &g_bUseOverlay, RVAR_TOGGLE, NULL },
{ "Video vertical sync", "VideoVsync", toggleVsync, &g_bVideoVsync, RVAR_TOGGLE, NULL },
{ "True drive emulation", "TrueDriveEmulation", toggleTrueDriveEmulation, &g_bTrueDriveEmulation, RVAR_TOGGLE, NULL },
{ "Save settings on exit", "SaveSettingsOnExit", NULL, &g_bSaveSettings, RVAR_TOGGLE, NULL },
{ "", "", NULL, NULL, RVAR_NULL, NULL }
};
rvar_t *settings[] = {
mainSettings,
inputSettings,
soundSettings,
SIDsound::sidSettings,
archDepSettings,
TED::tedSettings,
Vicmem::vicSettings,
videoSettings,
NULL
};
//-----------------------------------------------------------------------------
// Name: ShowFrameRate()
//-----------------------------------------------------------------------------
inline static void ShowFrameRate(unsigned int show)
{
char fpstxt[16];
static unsigned int fps = 50;
if (show) {
unsigned int speed = ad_get_fps(fps);
sprintf(fpstxt, "%u%%/%ufps", speed, fps);
unsigned int s = (unsigned int) strlen(fpstxt) << 3;
ted8360->texttoscreen((ted8360->getCyclesPerRow() >= VIC_PIXELS_PER_ROW ? 472 : 408) - s, 9, fpstxt);
}
}
//-----------------------------------------------------------------------------
// Name: DebugInfo()
//-----------------------------------------------------------------------------
inline static void DebugInfo()
{
unsigned int hpos = (ted8360->getCyclesPerRow() == 456 ? 48 : 112), vpos = 9;
sprintf(textout, "OPCODE: %02X ", machine->getcins());
ted8360->texttoscreen(hpos, vpos, textout);
ted8360->texttoscreen(hpos, vpos+8, " PC SR AC XR YR SP");
sprintf(textout, ";%04X %02X %02X %02X %02X %02X", machine->getPC(),
machine->getST(), machine->getAC(), machine->getX(), machine->getY(), machine->getSP());
ted8360->texttoscreen(hpos, vpos+16, textout);
vpos += 24;
sprintf(textout, "TAPE: %08d ", ted8360->tap->tapeSoFar);
CTrueDrive *d = CTrueDrive::Drives[0];
if (d) {
char driveText[64];
unsigned int t, s;
unsigned char ledState = d->getDriveMemHandler()->Read(0x1c00) & 8;
unsigned char motorState = d->GetFdc()->getMotorState() ? 4 : 0;
d->GetFdc()->trackSector(t, s);
sprintf(driveText, "DRIVE: T/S:%02u/%02u", t, s);
strcat(textout, driveText);
ted8360->texttoscreen(hpos, vpos, textout);
ted8360->showled(hpos+21*8, vpos, motorState);
ted8360->showled(hpos+22*8, vpos, ledState);
} else {
ted8360->texttoscreen(hpos, vpos, textout);
}
}
//-----------------------------------------------------------------------------
void machineReset(unsigned int resetlevel)
{
ted8360->Reset(resetlevel);
machine->Reset();
CTrueDrive::ResetAllDrives();
tcbm->Reset();
iec->Reset();
fsdrive->Reset();
if (fsd1541)
fsd1541->Reset();
}
void machineDoSomeFrames(unsigned int frames)
{
ted8360->getKeys()->block(true);
while (frames--) {
ted8360->ted_process(1);
if (g_inDebug)
DebugInfo();
frameUpdate();
}
ted8360->getKeys()->block(false);
}
void machineEnable1551(bool enable)
{
if (enable) {
if (drive1541) {
delete drive1541;
drive1541 = NULL;
}
if (ted8360->getEmulationLevel() < 2) {
ted8360->HookTCBM(tcbm);
} else {
fsd1541 = new FakeSerialDrive(8);
}
g_bTrueDriveEmulation = 0;
}
else {
ted8360->HookTCBM(NULL);
if (fsd1541) {
delete fsd1541;
fsd1541 = NULL;
}
if (!drive1541) {
CSerial::InitPorts();
drive1541 = new CTrueDrive(1, 8);
drive1541->Reset();
}
g_bTrueDriveEmulation = 1;
}
}
bool machineIsTrueDriveEnabled(unsigned int dn = 8)
{
return drive1541 != NULL;
}
static void toggleTrueDriveEmulation(void *none)
{
bool e = !machineIsTrueDriveEnabled();
machineEnable1551(!e);
}
static void startd64(const char *fileName, bool autostart)
{
if (!machineIsTrueDriveEnabled()) {
machineEnable1551(false);
machineDoSomeFrames(70);
}
CTrueDrive::SwapDisk(fileName);
if (autostart)
ted8360->copyToKbBuffer("L\317\042*,P\042,8,1\rRu:\r");
else
ted8360->copyToKbBuffer("L\317\042*,P\042,8,1\r\r");
}
bool openZipDisk(const char *fname, bool autostart)
{
static unsigned char *b = NULL;
unsigned int fsize = 0;
unsigned int ftype;
unzipFiles(fname, ".");
if (zipOpen(fname, &b, fsize, ftype)) {
const char* tmpName = (ftype == 1) ? "unz.d64" : "unz.tap";
FILE *tmp = fopen(tmpName, "wb");
if (b && tmp) {
fwrite(b, sizeof(char), fsize, tmp);
fclose(tmp);
if (ftype == 1)
startd64(tmpName, autostart);
else {
ted8360->tap->detachTape();
ted8360->tap->attachTape(tmpName);
if (autostart) {
ted8360->copyToKbBuffer("Lo:\rRUN\r");
ted8360->tap->pressTapeButton(ted8360->GetClockCount(), 1);
}
if (b) {
delete[] b;
b = NULL;
}
}
return true;
}
}
return false;
}
bool start_file(const char *szFile, bool autostart = true)
{
char *pFileExt = (char *) strrchr(szFile, '.');
if (pFileExt) {
char *fileext = pFileExt;
if (!strcmp(fileext,".d64") || !strcmp(fileext,".D64") ||
!strcmp(fileext, ".g64") || !strcmp(fileext, ".G64")
) {
startd64(szFile, autostart);
return true;
}
if (!strcmp(fileext,".zip") || !strcmp(fileext,".ZIP")) {
return openZipDisk(szFile, autostart);
}
if (!strcmp(fileext,".prg") || !strcmp(fileext,".PRG")
|| !strcmp(fileext,".p00") || !strcmp(fileext,".P00")) {
if (PrgLoad(szFile, 0, ted8360)) {
if (autostart)
ted8360->copyToKbBuffer("RUN:\r");
return true;
}
}
if (!strcmp(fileext, ".t64") || !strcmp(fileext,".T64")) {
if (prgLoadFromT64(szFile, 0, ted8360)) {
if (autostart)
ted8360->copyToKbBuffer("RUN:\r");
return true;
}
}
if (!strcmp(fileext,".tap") || !strcmp(fileext,".TAP") || !strcmp(fileext, ".wav") || !strcmp(fileext, ".WAV")) {
ted8360->tap->detachTape();
ted8360->tap->attachTape(szFile);
if (autostart) {
ted8360->copyToKbBuffer("Lo:\rRUN\r");
ted8360->tap->pressTapeButton(ted8360->GetClockCount(), 1);
}
return true;
}
if (!strcmp(fileext, ".yss")) {
fprintf(stderr, "Loading emulator state from %s.\n", szFile);
return SaveState::openSnapshot(szFile, false);
}
return false;
}
return false;
}
bool autostart_file(const char *szFile, bool autostart)
{
machineReset(true);
// do some frames
unsigned int frames = ted8360->getAutostartDelay();
machineDoSomeFrames(frames);
// to work around a few buggy defenders...
if (!ted8360->getEmulationLevel())
while (ted8360->getVerticalCount() != 160)
ted8360->ted_process(0);
// and then try to load the parameter as file
return start_file(szFile, autostart);
}
/* ---------- Display functions ---------- */
static char popUpMessage[256] = "";
static unsigned int popupMessageTimeOut = 0;
static void showPopUpMessage()
{
unsigned int ix;
size_t len = strlen(popUpMessage);
unsigned int offset = ted8360->getCyclesPerRow() == 456 ? 456 : 592;
char dummy[40];
ix = (unsigned int)(len);
while( ix-->0)
dummy[ix] = 32;
dummy[len] = '\0';
int tab = offset / 2 - (int(len) << 2);
int line = 130;
ted8360->texttoscreen(tab, line, dummy);
ted8360->texttoscreen(tab, line + 8, popUpMessage);
ted8360->texttoscreen(tab, line + 16, dummy);
if(popupMessageTimeOut)
popupMessageTimeOut -= 1;
}
inline void PopupMsg(const char *msg, ...)
{
va_list args;
va_start(args, msg);
vsprintf(popUpMessage, msg, args);
va_end(args);
popupMessageTimeOut = 60; // frames
}
static void showKeyboardOverlay()
{
static SDL_Texture* texture = NULL;
static SDL_Rect rc = { 0 };
if (!texture) {
SDL_RWops *rwops = SDL_RWFromMem((void*) keyoverlay, sizeof(keyoverlay));
SDL_Surface* loadedSurface = SDL_LoadBMP_RW(rwops, 1);
if (loadedSurface) {
// Note: the logical size of the window surface is 2×!
rc.w = loadedSurface->w * 2;
rc.h = loadedSurface->h * 2;
rc.x = SCREENX - rc.w / 2;
rc.y = SCREENY * 2 - rc.h;
texture = SDL_CreateTextureFromSurface(sdlRenderer, loadedSurface);
SDL_FreeSurface(loadedSurface);
if (!texture)
return;
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
}
}
SDL_SetTextureAlphaMod(texture, timeOutOverlayKeys);
SDL_RenderCopy(sdlRenderer, texture, NULL, &rc);
}
void frameUpdate(unsigned char *src, unsigned int *target)
{
const unsigned int pixelsPerRow = ted8360->getCyclesPerRow();
const unsigned int sourcePitch = (pixelsPerRow - SCREENX);
const unsigned int targetPitch = sourcePitch;
const unsigned int *texture = target;
unsigned int i, j;
//
#ifndef USE_OPENGL
if (g_bUseOverlay) {
video_convert_buffer(target, pixelsPerRow, src);
} else {
static unsigned int *palette = palette_get_rgb();
for(i = 0; i < SCREENY; i++) {
for(j = 0; j < SCREENX; j++) {
target[j] = palette[src[j]];
}
src += sourcePitch + SCREENX;
target += targetPitch + SCREENX;
}
}
// TODO: use SDL_LockTexture instead
int e = SDL_UpdateTexture(sdlTexture, NULL, texture, pixelsPerRow * sizeof (unsigned int));
// VIC20 source rectangle is smaller
bool isNtsc = ted8360->isNtscMode();
if (g_iEmulationLevel == 3 || (isNtsc && g_bActive)) {
const int ySize = isNtsc ? 240 : SCREENY;
unsigned int scaleY = g_bUseOverlay ? 1 : 0;
SDL_Rect srcrc = { 0, 1 << scaleY, SCREENX, (ySize - 4) << scaleY };
e = SDL_RenderCopy(sdlRenderer, sdlTexture, &srcrc, NULL);
} else
e = SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);
if (timeOutOverlayKeys) {
showKeyboardOverlay();
//SDL_StartTextInput();
if (!mouseBtnHeld)
timeOutOverlayKeys -= 4;
}
SDL_RenderPresent(sdlRenderer);
#else
static unsigned int* palette = palette_get_rgb();
for (i = 0; i < SCREENY; i++) {
for (j = 0; j < SCREENX; j++) {
target[j] = palette[src[j]];
}
src += sourcePitch + SCREENX;
}
openGLFrameUpdate(sdlWindow, (void*)texture, SCREENX, SCREENY, g_bVideoVsync);
#endif
}
static void frameUpdate()
{
const unsigned int cyclesPerRow = ted8360->getCyclesPerRow();
const int offsetX = cyclesPerRow == VIC_PIXELS_PER_ROW ? -72 : 8;
const int offsetY = cyclesPerRow == VIC_PIXELS_PER_ROW ? 9 : 0;
if (popupMessageTimeOut)
showPopUpMessage();
frameUpdate(ted8360->getScreenData() + (cyclesPerRow - 384 - offsetX) / 2 + offsetY * cyclesPerRow, pixels);
}
/* ---------- Management of settings ---------- */
//-----------------------------------------------------------------------------
// Name: SaveSettings
//-----------------------------------------------------------------------------
bool SaveSettings(char *inifileName)
{
unsigned int i;
char tmpStr[MAX_PATH];
FILE *ini;
unsigned int rammask;
if ((ini = fopen(inifileName, "wt"))) {
fprintf(ini, "[Yape configuration file]\n");
fprintf(ini,"DisplayFrameRate = %d\n",g_FrameRate);
fprintf(ini,"DisplayQuickDebugInfo = %d\n",g_inDebug);
fprintf(ini,"50HzTimerActive = %d\n",g_50Hz);
fprintf(ini,"ActiveJoystick = %d\n", KEYS::activejoy);
rammask = ted8360->getRamMask();
fprintf(ini,"RamMask = %x\n",rammask);
fprintf(ini,"256KBRAM = %u\n",ted8360->reuSizeKb);
fprintf(ini,"SaveSettingsOnExit = %x\n",g_bSaveSettings);
for (i = 0; i<4; i++) {
fprintf(ini,"ROMC%dLOW = %s\n",i, ted8360->romlopath[i]);
fprintf(ini,"ROMC%dHIGH = %s\n",i, ted8360->romhighpath[i]);
}
ad_get_curr_dir(tmpStr);
fprintf(ini, "CurrentDirectory = %s\n", tmpStr);
fprintf(ini, "CRTEmulation = %u\n", g_bUseOverlay);
fprintf(ini, "WindowMultiplier = %u\n", g_iWindowMultiplier);
fprintf(ini, "EmulationLevel = %u\n", g_iEmulationLevel);
fprintf(ini, "JoystickKeysIndex = %u\n", KEYS::joystickScanCodeIndex);
fprintf(ini, "Vic20RamExpSize = %u\n", Vicmem::ramExpSizeKb);
fclose(ini);
return true;
}
return false;
}
bool LoadSettings(char *inifileName)
{
FILE *ini;
unsigned int rammask;
char keyword[256], line[256], value[256];
if ((ini = fopen(inifileName, "r"))) {
fscanf(ini,"%s configuration file]\n", keyword);
if (strcmp(keyword, "[Yape"))
return false;
while(fgets(line, 255, ini)) {
strcpy(value, "");
if (sscanf(line, "%s = %[^,\n,\r]", keyword, value) ) {
int number = atoi(value);
if (!strcmp(keyword, "DisplayFrameRate")) {
g_FrameRate = !!atoi(value);
fprintf(stderr, "Display frame rate: %i\n", g_FrameRate);
}
else if (!strcmp(keyword, "DisplayQuickDebugInfo"))
g_inDebug = !!atoi(value);
else if (!strcmp(keyword, "50HzTimerActive"))
g_50Hz = !!atoi(value);
else if (!strcmp(keyword, "ActiveJoystick"))
KEYS::activejoy = atoi(value) & 3;
else if (!strcmp(keyword, "RamMask")) {
sscanf(value, "%04x", &rammask);
ted8360->setRamMask(rammask);
}
else if (!strcmp(keyword, "256KBRAM")) {
rammask = atoi(value);
if (rammask == 128 || rammask == 256 || rammask == 512) {
ted8360->enableREU(rammask);
} else
ted8360->enableREU(0);
}
else if (!strcmp(keyword, "SaveSettingsOnExit"))
g_bSaveSettings = !!atoi(value);
else if (!strcmp(keyword, "ROMC0LOW"))
strcpy(ted8360->romlopath[0], value);
else if (!strcmp(keyword, "ROMC1LOW"))
strcpy(ted8360->romlopath[1], value);
else if (!strcmp(keyword, "ROMC2LOW"))
strcpy(ted8360->romlopath[2], value);
else if (!strcmp(keyword, "ROMC3LOW"))
strcpy(ted8360->romlopath[3], value);
else if (!strcmp(keyword, "ROMC0HIGH"))
strcpy(ted8360->romhighpath[0], value);
else if (!strcmp(keyword, "ROMC1HIGH"))
strcpy(ted8360->romhighpath[1], value);
else if (!strcmp(keyword, "ROMC2HIGH"))
strcpy(ted8360->romhighpath[2], value);
else if (!strcmp(keyword, "ROMC3HIGH"))
strcpy(ted8360->romhighpath[3], value);
else if (!strcmp(keyword, "CurrentDirectory"))
ad_set_curr_dir(value);
else if (!strcmp(keyword, "CRTEmulation"))
g_bUseOverlay = !!atoi(value);
else if (!strcmp(keyword, "WindowMultiplier"))
g_iWindowMultiplier = number ? (number & 3) : 1;
else if (!strcmp(keyword, "EmulationLevel"))
g_iEmulationLevel = atoi(value);
else if (!strcmp(keyword, "JoystickKeysIndex"))
KEYS::joystickScanCodeIndex = atoi(value) % 3;
else if (!strcmp(keyword, "Vic20RamExpSize"))
Vicmem::ramExpSizeKb = atoi(value);
}
}
fclose(ini);
return true;
}
return false;
}
static bool saveScreenshotBMP(char* filepath, SDL_Window* SDLWindow, SDL_Renderer* SDLRenderer)
{
SDL_Surface* saveSurface = NULL;
int w, h;
SDL_GetRendererOutputSize(SDLRenderer, &w, &h);
saveSurface = SDL_CreateRGBSurface(0, w, h, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
if (saveSurface && 0 == SDL_RenderReadPixels(SDLRenderer, 0, SDL_PIXELFORMAT_ARGB8888, saveSurface->pixels, saveSurface->pitch)) {
SDL_SaveBMP(saveSurface, filepath);
SDL_FreeSurface(saveSurface);
return true;
}
return false;
}
static bool getSerializedFilename(const char *name, const char *extension, char *out)
{
char dummy[512];
unsigned int i = 0;
if (!name || !extension || !out)
return false;
bool found = true;
do {
sprintf(dummy, "%s%06d.%s", name, i++, extension);
FILE *fp = fopen(dummy, "rb");
if (fp) {
fclose(fp);
} else {
found = false;
strcpy(out, dummy);
}
} while (found && i < 0x1000000);
return !found;
}
#ifdef __EMSCRIPTEN__
// JavaScript function to download files from the virtual FS
EM_JS(void, download_file, (const char* filename), {
const fname = UTF8ToString(filename);
const data = FS.readFile(fname, { encoding: 'binary' });
const blob = new Blob([data], { type: "application/octet-stream" });
const a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = fname;
a.click();
URL.revokeObjectURL(a.href);
});
#endif
void snapshotSave()
{
getSerializedFilename("snapshot", "yss", lastSnapshotName);
SaveState::openSnapshot(lastSnapshotName, true);
PopupMsg(" Saving snapshot... ");
fprintf(stderr, "Saved emulator state to %s.\n", lastSnapshotName);
#ifdef __EMSCRIPTEN__
download_file(lastSnapshotName);
#endif
}
//-----------------------------------------------------------------------------
// Name: SaveBitmap()
// Desc: Saves the SDL surface to Windows bitmap file named as yapeXXXX.bmp
//-----------------------------------------------------------------------------
static int SaveBitmap()
{
char bmpname[512];
// finding the last yapeXXXX.bmp image
if (getSerializedFilename("yape", "bmp", bmpname)) {
if (saveScreenshotBMP(bmpname, sdlWindow, sdlRenderer)) {
fprintf(stderr, "Screenshot saved: %s\n", bmpname);
#ifdef __EMSCRIPTEN__
download_file(bmpname);
#endif
return true;
}
}
return false;
}
bool mainSaveMemoryAsPrg(const char *prgname, unsigned short &beginAddr, unsigned short &endAddr)
{
char newPrgname[512];
if (!prgname) {
if (!getSerializedFilename("noname", "prg", newPrgname))
return false;
} else {
strcpy(newPrgname, prgname);
}
if (beginAddr >= endAddr)
beginAddr = endAddr = 0;
return prgSaveBasicMemory(newPrgname, ted8360, beginAddr, endAddr, beginAddr == endAddr);
}
bool mainLoadPrgToMemory(const char* prgname, unsigned short& beginAddr)
{
return PrgLoad(prgname, beginAddr, ted8360);
}
static void doSwapJoy()
{
KEYS::swapjoy(NULL);
PopupMsg(" ACTIVE JOY IS : %s ", KEYS::activeJoyTxt());
}
static void doSwapKeyset()
{
KEYS::swapKeyset(NULL);
PopupMsg(" ACTIVE KEYSET IS : %s ", KEYS::activeJoyKeyset());
}
static void enterMenu()
{
timeOutOverlayKeys = 0;
sound_pause();
#ifdef __EMSCRIPTEN__
setMainLoop(0);
#else
unsigned int wasActive = g_bActive;
g_bActive = 0;
uinterface->enterMenu();
#endif
if (g_50Hz)
sound_resume();
#ifndef __EMSCRIPTEN__
g_bActive = wasActive;
#endif
if (!g_bActive) {
PopupMsg(" PAUSED ");
frameUpdate();
}
}
static void toggleFullThrottle(void *none)
{
g_50Hz = !g_50Hz;
sprintf(textout, " 50 HZ TIMER IS ");
if (g_50Hz) {
sound_resume();
strcat(textout,"ON ");
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop_timing(EM_TIMING_RAF, 0);
#endif
}
else {
sound_pause();
strcat(textout, "OFF ");
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop_timing(EM_TIMING_SETTIMEOUT, 1);
#endif
}
PopupMsg(textout);
}
static void setEmulationLevel(unsigned int level)
{
unsigned char *ram = new unsigned char[RAMSIZE];
if (!ram)
return;
unsigned int i = ted8360->getEmulationLevel();
if (level != i) {
g_bActive = 0;
sound_pause();
// Back up RAM
memcpy(ram, ted8360->Ram, RAMSIZE);
// destroy old TED object
if (ted8360)
delete ted8360;
if (fsd1541) {
delete fsd1541;
fsd1541 = NULL;
}
switch (level) {
default:
case 0:
ted8360 = new TED;
break;
case 1:
ted8360 = new TEDFAST;
break;
case 2:
ted8360 = new Vic2mem;
break;
case 3:
ted8360 = new Vicmem;
break;
}
uinterface->setNewMachine(ted8360);
ted8360->cpuptr.setMem(ted8360, ted8360->getIrqReg(), &(ted8360->Ram[0x0100]));
ted8360->HookTCBM(tcbm);
machine = &ted8360->cpuptr;
// restore RAM
memcpy(ted8360->Ram, ram, RAMSIZE);
// reload ROMs for machine type switch
ted8360->loadroms();
ted8360->cpuptr.Reset();
machineEnable1551(!g_bTrueDriveEmulation);
ted8360->Reset(0);
init_palette(ted8360);
//
sound_resume();
g_bActive = 1;
ad_vsync_init(ted8360->getFrameRate());
}
delete[] ram;
}
static void toggleShowSpeed(void *none)
{
g_FrameRate = !g_FrameRate;
}
static void toggleVsync(void *none)
{
char value[2];
g_bVideoVsync = 1 - g_bVideoVsync;
value[0] = 0x30 + g_bVideoVsync;
value[1] = 0;
SDL_SetHint(SDL_HINT_RENDER_VSYNC, value);
}
static void toggleCrtEmulation(void *none)
{
g_bUseOverlay = !g_bUseOverlay;
SDL_DestroyTexture(sdlTexture);
sdlTexture = SDL_CreateTexture(sdlRenderer,
g_bUseOverlay ? SDL_PIXELFORMAT_UYVY : SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
WINDOWX, WINDOWY * (g_bUseOverlay ? 2 : 1));
PopupMsg(" CRT emulation %s ", g_bUseOverlay ? "ON" : "OFF");
}
static const char *machineTypeLabel(unsigned int index)
{
const char *label[] = { "ACCURATE +4", "FAST +4", "COMMODORE 64", "COMMODORE VIC20"};
return label[index % (sizeof(label)/sizeof(label[0]))];
}
static const char* machineTypeLabel()
{
return machineTypeLabel(g_iEmulationLevel);
}
static void flipMachineType(char *name, int dir)
{
g_iEmulationLevel = (g_iEmulationLevel + dir) % 4;
setEmulationLevel(g_iEmulationLevel);
strcpy(name, machineTypeLabel());
}
static void flipMachineTypeFwd(void *name)
{
flipMachineType((char *) name, 1);
}
static void setWindowScale(int newScale)
{
SDL_SetWindowSize(sdlWindow, SCREENX * newScale, SCREENY * newScale);
}
static void flipWindowScale(void *none)
{
g_iWindowMultiplier = (g_iWindowMultiplier % 3) + 1;
setWindowScale(g_iWindowMultiplier);
}
static void confirmEmulationLevelChange(unsigned int shiftPressed)
{
int dir = shiftPressed ? -1 : 1;
#ifndef __EMSCRIPTEN__
int buttonid;
sprintf(textout, "Switch to %s emulation and lose data in current session?", machineTypeLabel(g_iEmulationLevel + dir));
const SDL_MessageBoxButtonData buttons[] = {
{ SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, 1, "No" },
{ SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, 0, "Yes" },
};
const SDL_MessageBoxData messageboxdata = {
SDL_MESSAGEBOX_INFORMATION,
sdlWindow,
"Confirmation required",
textout,
SDL_arraysize(buttons),
buttons,
NULL
};
if (SDL_ShowMessageBox(&messageboxdata, &buttonid) < 0) {
SDL_Log("Error opening window: %s\n", SDL_GetError());
buttonid = 0;
}
if (buttonid == 0)
#endif
{
char name[64];
flipMachineType(name, dir);
sprintf(textout, " EMULATION : %s ", name);
PopupMsg(textout);
}
}
static void pasteFromClipboard()
{
if (SDL_HasClipboardText()) {
char* lptstr = SDL_GetClipboardText();
size_t origSize = strlen(lptstr);
size_t t = origSize;
char bufferdata[16];
while (t) {
unsigned int chunkSize = (unsigned int)MIN(10, t);
strncpy(bufferdata, lptstr, chunkSize);
UI::stringToPETSCII((unsigned char*)bufferdata, chunkSize, ted8360->getCaps());
bufferdata[chunkSize] = 0;
ted8360->copyToKbBuffer(bufferdata, chunkSize);
unsigned int maxFrames = 200;
while (ted8360->Read(ted8360->getKbBufferSizePtr()) != 0 && --maxFrames) {
ted8360->ted_process(1);
frameUpdate();
}
lptstr += chunkSize;
t -= chunkSize;
}
//SDL_free((void*)lptstr);
}
}
static void copyToClipboard()
{
char* b = new char[25 * 42 + 1];
if (b) {
ted8360->getVideoMatrixText(reinterpret_cast<unsigned char*>(b));
if (0 == SDL_SetClipboardText(b)) {
PopupMsg(" Video matrix copied to clipboard ");
showPopUpMessage();
}
delete[] b;
}
}
//-----------------------------------------------------------------------------
// Name: poll_events()
// Desc: polls SDL events if there's any in the message queue
//-----------------------------------------------------------------------------
inline static void poll_events(void)
{
SDL_Event event;
if (timeOutMousePointer) {
timeOutMousePointer--;
if (!timeOutMousePointer)
SDL_ShowCursor(0);
}
if ( SDL_PollEvent(&event) ) {
switch (event.type) {
case SDL_WINDOWEVENT:
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
frameUpdate();
}
break;
case SDL_KEYDOWN:
// printf("Keysim: %x, scancode: %x - %s key was pressed!\n",
// event.key.keysym.sym, event.key.keysym.scancode, SDL_GetKeyName(event.key.keysym.sym));
if (event.key.keysym.mod & KMOD_LALT) {
sound_pause();
switch (event.key.keysym.sym) {
default:;
break;
case SDLK_KP_PLUS:
{
sound_pause();
static unsigned int currFrq = 0;
unsigned int frq[] = { 48000, 96000, 192000, 22050, 44100 };
currFrq = (currFrq + 1) % MAX_FRQ_INDEX;
unsigned int rate = frq[currFrq];
sound_change_freq(rate);
PopupMsg(" AUDIO FREQUENCY: %u ", rate);
}
break;
case SDLK_1:
case SDLK_2:
case SDLK_3:
{
int mult = (event.key.keysym.sym - SDLK_0);