-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmainsdl.cpp
1780 lines (1560 loc) · 49.1 KB
/
mainsdl.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
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
/*!
* @file mainsdl.cpp
* @author mithrendal and Dirk W. Hoffmann, www.dirkwhoffmann.de
* @copyright Dirk W. Hoffmann. All rights reserved.
*
*
* v5.0
issues:
- Use VICIIAPI::getSpriteInfo instead
-FileSystem *fs = new FileSystem(*wrapper->emu->drive8.drive->disk);
Eigentlich müsste die MediaFileAPI genug funktionalität haben (FileSystem lieber nicht benutzen)
-snapshot = wrapper->emu->c64.takeSnapshot();
Use VirtualC64API::takeSnapshot() instead (i.e.: wrapper->emu->takeSnapshot())
*/
#include <stdio.h>
#include <stdlib.h>
#include "config.h"
#include "VirtualC64.h"
#include "VirtualC64Types.h"
#include "Emulator.h"
#include <emscripten.h>
#include <SDL2/SDL.h>
#include <emscripten/html5.h>
using namespace vc64;
/* SDL2 start*/
SDL_Window * window = NULL;
SDL_Surface * window_surface = NULL;
unsigned int * pixels;
SDL_Renderer * renderer = NULL;
SDL_Texture * screen_texture = NULL;
/* SDL2 end */
void PrintEvent(const SDL_Event * event)
{
if (event->type == SDL_WINDOWEVENT) {
switch (event->window.event) {
case SDL_WINDOWEVENT_SHOWN:
printf("Window %d shown", event->window.windowID);
break;
case SDL_WINDOWEVENT_HIDDEN:
printf("Window %d hidden", event->window.windowID);
break;
case SDL_WINDOWEVENT_EXPOSED:
printf("Window %d exposed", event->window.windowID);
break;
case SDL_WINDOWEVENT_MOVED:
printf("Window %d moved to %d,%d",
event->window.windowID, event->window.data1,
event->window.data2);
break;
case SDL_WINDOWEVENT_RESIZED:
printf("Window %d resized to %dx%d",
event->window.windowID, event->window.data1,
event->window.data2);
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
printf("Window %d size changed to %dx%d",
event->window.windowID, event->window.data1,
event->window.data2);
break;
case SDL_WINDOWEVENT_MINIMIZED:
printf("Window %d minimized", event->window.windowID);
break;
case SDL_WINDOWEVENT_MAXIMIZED:
printf("Window %d maximized", event->window.windowID);
break;
case SDL_WINDOWEVENT_RESTORED:
printf("Window %d restored", event->window.windowID);
break;
case SDL_WINDOWEVENT_ENTER:
printf("Mouse entered window %d",
event->window.windowID);
break;
case SDL_WINDOWEVENT_LEAVE:
printf("Mouse left window %d", event->window.windowID);
break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
printf("Window %d gained keyboard focus",
event->window.windowID);
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
printf("Window %d lost keyboard focus",
event->window.windowID);
break;
case SDL_WINDOWEVENT_CLOSE:
printf("Window %d closed", event->window.windowID);
break;
#if SDL_VERSION_ATLEAST(2, 0, 5)
case SDL_WINDOWEVENT_TAKE_FOCUS:
printf("Window %d is offered a focus", event->window.windowID);
break;
case SDL_WINDOWEVENT_HIT_TEST:
printf("Window %d has a special hit test", event->window.windowID);
break;
#endif
default:
printf("Window %d got unknown event %d",
event->window.windowID, event->window.event);
break;
}
printf("\n");
}
}
int emu_width = Texture::width; //Texture.width; //NTSC_PIXELS; //428
int emu_height = Texture::height; //PAL_RASTERLINES; //284
int eat_border_width = 0;
int eat_border_height = 0;
int xOff = 12 + eat_border_width;
int yOff = 12 + eat_border_height;
int clipped_width = Texture::width -12 -24 -2*eat_border_width; //392
int clipped_height = Texture::height -12 -24 -2*eat_border_height; //248
int bFullscreen = false;
EM_BOOL emscripten_window_resized_callback(int eventType, const void *reserved, void *userData){
/*
double width, height;
emscripten_get_element_css_size("canvas", &width, &height);
int w = (int)width, h = (int)height;
*/
// resize SDL window
SDL_SetWindowSize(window, clipped_width, clipped_height);
/*
SDL_Rect SrcR;
SrcR.x = 0;
SrcR.y = 0;
SrcR.w = emu_width;
SrcR.h = emu_height;
SDL_RenderSetViewport(renderer, &SrcR);
*/
return true;
}
char *filename = NULL;
extern "C" void wasm_toggleFullscreen()
{
if(!bFullscreen)
{
bFullscreen=true;
EmscriptenFullscreenStrategy strategy;
strategy.scaleMode = EMSCRIPTEN_FULLSCREEN_CANVAS_SCALE_STDDEF;
strategy.filteringMode = EMSCRIPTEN_FULLSCREEN_FILTERING_DEFAULT;
strategy.canvasResizedCallback = emscripten_window_resized_callback;
emscripten_enter_soft_fullscreen("canvas", &strategy);
}
else
{
bFullscreen=false;
emscripten_exit_soft_fullscreen();
}
}
int eventFilter(void* the_emu, SDL_Event* event) {
//C64 *c64 = (C64 *)thisC64;
switch(event->type){
case SDL_WINDOWEVENT:
//PrintEvent(event);
if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED)
{//zuerst
window_surface = SDL_GetWindowSurface(window);
pixels = (unsigned int *)window_surface->pixels;
int width = window_surface->w;
int height = window_surface->h;
printf("Size changed: %d, %d\n", width, height);
}
else if(event->window.event==SDL_WINDOWEVENT_RESIZED)
{//this event comes after SDL_WINDOWEVENT_SIZE_CHANGED
//SDL_SetWindowSize(window, emu_width, emu_height);
//SDL_SetWindowPosition(window, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
//window_surface = SDL_GetWindowSurface(window);
}
break;
case SDL_KEYDOWN:
if ( event->key.keysym.sym == SDLK_RETURN &&
event->key.keysym.mod & KMOD_ALT )
{
wasm_toggleFullscreen();
}
break;
case SDL_FINGERDOWN:
case SDL_MOUSEBUTTONDOWN:
/* on some browsers (chrome, safari) we have to resume Audio on users action
https://developers.google.com/web/updates/2017/09/autoplay-policy-changes
*/
EM_ASM({
if (typeof Module === 'undefined'
|| typeof Module.SDL2 == 'undefined'
|| typeof Module.SDL2.audioContext == 'undefined')
return;
if (Module.SDL2.audioContext.state == 'suspended') {
Module.SDL2.audioContext.resume();
}
});
break;
default:
//printf("unhandeld event %d",event->type);
break;
}
return 1;
}
#define PAL_FPS 50.125
#define NTSC_FPS 59.826
bool requested_targetFrameCount_reset=false;
int sum_samples=0;
double last_time = 0.0 ;
unsigned int executed_frame_count=0;
int64_t total_executed_frame_count=0;
double start_time=emscripten_get_now();
unsigned int rendered_frame_count=0;
unsigned int frames=0, seconds=0;
double frame_rate=PAL_FPS;
double speed_boost=1.0;
bool vsync = false;
signed vsync_speed=2;
u8 vframes=0;
unsigned long current_frame=100;
unsigned host_refresh_rate=60, last_host_refresh_rate=60;
unsigned host_refresh_count=0;
signed boost_param=100;
void calibrate_boost(signed boost_param);
// The emscripten "main loop" replacement function.
void draw_one_frame_into_SDL(void *the_emu)
{
//this method is triggered by
//emscripten_set_main_loop_arg(em_arg_callback_func func, void *arg, int fps, int simulate_infinite_loop)
//which is called inside te c64.cpp
//fps Setting int <=0 (recommended) uses the browser’s requestAnimationFrame mechanism to call the function.
//The number of callbacks is usually 60 times per second, but will
//generally match the display refresh rate in most web browsers as
//per W3C recommendation. requestAnimationFrame()
double now = emscripten_get_now();
double elapsedTimeInSeconds = (now - start_time)/1000.0;
int64_t targetFrameCount = (int64_t)(elapsedTimeInSeconds * (frame_rate*speed_boost));
int max_gap = 8;
VirtualC64 *emu = (VirtualC64 *)the_emu;
emu->emu->update();
if(emu->isWarping() == true)
{
printf("warping at least 25 frames at once ...\n");
int i=25;
while(emu->isWarping() == true && i>0)
{
//c64->emu->computeFrame();
emu->emu->computeFrame();
i--;
}
start_time=now;
total_executed_frame_count=0;
targetFrameCount=1;
}
if(requested_targetFrameCount_reset)
{
start_time=now;
total_executed_frame_count=0;
targetFrameCount=1;
requested_targetFrameCount_reset=false;
}
//lost the sync
if(targetFrameCount-total_executed_frame_count > max_gap)
{
printf("lost sync target=%lld, total_executed=%lld\n", targetFrameCount, total_executed_frame_count);
//reset timer
//because we are out of sync, we do now skip max_gap-1 emulation frames
start_time=now;
total_executed_frame_count=0;
targetFrameCount=1; //we are hoplessly behind but do at least one in this round
}
host_refresh_count++;
if(now-last_time>= 1000.0)
{
double passed_time= now - last_time;
last_time = now;
seconds += 1;
frames += rendered_frame_count;
printf("time[ms]=%.0lf, audio_samples=%d, frames [executed=%u, rendered=%u] avg_fps=%u\n",
passed_time, sum_samples, executed_frame_count, rendered_frame_count, frames/seconds);
host_refresh_rate=host_refresh_count;
host_refresh_count=0;
sum_samples=0;
rendered_frame_count=0;
executed_frame_count=0;
}
EM_ASM({
// if (typeof draw_one_frame === 'undefined')
// return;
draw_one_frame(); // to gather joystick information for example
});
if(vsync)
{ //current_frame=0, vsync_speed=-2, vframes=0
//printf("current_frame=%ld, vsync_speed=%d, vframes=%d\n", current_frame, vsync_speed, vframes);
current_frame++;
if(vsync_speed<0)
{
if(current_frame % (vsync_speed*-1) !=0)
{
// printf("skip frame %ld\n", current_frame % ((unsigned long)vsync_speed*-1) );
return;
}
else{
emu->emu->computeFrame();
// printf("compute_frame \n");
executed_frame_count++;
total_executed_frame_count++;
}
}
else
{
//0 + 0 < 0 -2
while(current_frame+vframes < current_frame + vsync_speed)
{
emu->emu->computeFrame();
//printf("compute_frame \n");
executed_frame_count++;
total_executed_frame_count++;
vframes++;
}
//printf("\n");
vframes=0;
}
//check current frame rate +-1 in case user changed it on host system
if(abs((long) (host_refresh_rate-last_host_refresh_rate))>1)
{
calibrate_boost(boost_param);
last_host_refresh_rate=host_refresh_rate;
}
}
else
{
while(total_executed_frame_count < targetFrameCount) {
executed_frame_count++;
total_executed_frame_count++;
emu->emu->computeFrame();
}
}
rendered_frame_count++;
Uint8 *texture = (Uint8 *)emu->videoPort.getTexture(); //screenBuffer();
// int surface_width = window_surface->w;
// int surface_height = window_surface->h;
// SDL_RenderClear(renderer);
SDL_Rect SrcR;
SrcR.x = xOff;
SrcR.y = yOff;
SrcR.w = clipped_width;
SrcR.h = clipped_height;
SDL_UpdateTexture(screen_texture, &SrcR, texture+ (4*emu_width*SrcR.y) + SrcR.x*4, 4*emu_width);
SDL_RenderCopy(renderer, screen_texture, &SrcR, NULL);
SDL_RenderPresent(renderer);
}
void MyAudioCallback(void* the_emu,
Uint8* stream,
int len)
{
VirtualC64 *emu = (VirtualC64 *)the_emu;
int n = len / sizeof(float);
emu->audioPort.copyMono((float *)stream, n);
/* printf("copyMono[%d]: ", n);
for(int i=0; i<n; i++)
{
printf("%hhu,",stream[i]);
}
printf("\n");
*/
sum_samples += n;
}
extern "C" void wasm_create_renderer(char* name)
{
printf("try to create %s renderer\n", name);
window = SDL_CreateWindow("",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
clipped_width, clipped_height,
SDL_WINDOW_RESIZABLE);
SDL_CaptureMouse(SDL_FALSE);
SDL_EventState(SDL_MOUSEMOTION, SDL_DISABLE); // Disable mouse motion events
SDL_EventState(SDL_MOUSEBUTTONDOWN, SDL_DISABLE); // Disable mouse button down events
SDL_EventState(SDL_MOUSEBUTTONUP, SDL_DISABLE); // Disable mouse button up events
if(0==strcmp("webgl", name))
{
renderer = SDL_CreateRenderer(window,
-1,
SDL_RENDERER_PRESENTVSYNC|SDL_RENDERER_ACCELERATED);
if(renderer == NULL)
{
printf("can not get hardware accelerated renderer going with software renderer instead...\n");
}
else
{
printf("got hardware accelerated renderer ...\n");
}
}
if(renderer == NULL)
{
renderer = SDL_CreateRenderer(window,
-1,
SDL_RENDERER_SOFTWARE
);
if(renderer == NULL)
{
printf("can not get software renderer ...\n");
return;
}
else
{
printf("got software renderer ...\n");
}
}
// Since we are going to display a low resolution buffer,
// it is best to limit the window size so that it cannot
// be smaller than our internal buffer size.
SDL_SetWindowMinimumSize(window, clipped_width, clipped_height);
SDL_RenderSetLogicalSize(renderer, clipped_width, clipped_height);
SDL_RenderSetIntegerScale(renderer, SDL_TRUE);
screen_texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_ABGR8888
, SDL_TEXTUREACCESS_STREAMING,
emu_width, emu_height);
window_surface = SDL_GetWindowSurface(window);
}
void initSDL(void *the_emu)
{
if(SDL_Init(SDL_INIT_VIDEO/*|SDL_INIT_AUDIO*/)==-1)
{
printf("Could not initialize SDL:%s\n", SDL_GetError());
}
//listen to mouse, finger and keys
// SDL_SetEventFilter(eventFilter, thisC64);
// wasm_create_renderer((char*)"webgl");
}
void send_message_to_js(const char * msg)
{
EM_ASM(
{
if (typeof message_handler === 'undefined')
return;
message_handler( "MSG_"+UTF8ToString($0) );
}, msg );
}
void send_message_to_js(const char * msg, long data)
{
EM_ASM(
{
if (typeof message_handler === 'undefined')
return;
message_handler( "MSG_"+UTF8ToString($0), $1 );
}, msg, data );
}
void send_message_to_js(const char * msg, long data1, long data2)
{
EM_ASM(
{
if (typeof message_handler === 'undefined')
return;
message_handler( "MSG_"+UTF8ToString($0), $1, $2 );
}, msg, data1, data2 );
}
bool paused_the_emscripten_main_loop=false;
bool warp_mode=false;
void calculate_viewport();
void theListener(const void * c64, Message msg){
auto emu = ((VirtualC64 *)c64);
if(warp_mode && msg.type == MSG_SER_BUSY && !emu->isWarping())
{
emu->warpOn();
}
else if(msg.type == MSG_SER_IDLE && emu->isWarping())
{
emu->warpOff();
}
if(msg.type == MSG_RS232_OUT) {
int c = emu->userPort.rs232.readOutgoingPrintableByte();
send_message_to_js("RS232", c);
return;
}
else if(msg.type == MSG_DRIVE_STEP)
{
//data1=msg.drive.nr;
//data2=msg.drive.value;
send_message_to_js("DRIVE_STEP", msg.drive.nr, msg.drive.value);
return;
}
const char *message_as_string = (const char *)MsgTypeEnum::key((MsgType)msg.type);
printf("vC64 message=%s, data=%ld\n", message_as_string, msg.value);
send_message_to_js(message_as_string, msg.value);
if(msg.type == MSG_DISK_INSERT)
{
emu->drive8.drive->dump(Category::Debug);
}
if(msg.type == MSG_PAL) {
printf("switched to PAL\n");
frame_rate = PAL_FPS;
requested_targetFrameCount_reset=true;
EM_ASM({PAL_VIC=true});
calculate_viewport();
calibrate_boost(boost_param);
}
else if(msg.type == MSG_NTSC) {
printf("switched to NTSC\n");
frame_rate = NTSC_FPS;
requested_targetFrameCount_reset=true;
EM_ASM({PAL_VIC=false});
calculate_viewport();
calibrate_boost(boost_param);
}
}
class C64Wrapper {
public:
VirtualC64 *emu;
C64Wrapper()
{
printf("constructing C64 ...\n");
this->emu = new VirtualC64();
printf("connecting listener to C64 message queue...\n");
try
{
emu->launch(this->emu, &theListener);
} catch(std::exception &exception) {
printf("%s\n", exception.what());
}
printf("launch completed\n");
}
~C64Wrapper()
{
printf("closing wrapper");
}
void run()
{
/* printf("wrapper calls 4x c64->loadRom(...) method\n");
c64->loadRom(ROM_KERNAL ,"roms/kernal.901227-03.bin");
c64->loadRom(ROM_BASIC, "roms/basic.901226-01.bin");
c64->loadRom(ROM_CHAR, "roms/characters.901225-01.bin");
c64->loadRom(ROM_VC1541, "roms/1541-II.251968-03.bin");
*/
printf("v5 run start\n");
try { emu->isReady(); } catch(...) {
EM_ASM({
setTimeout(function() {message_handler( 'MSG_ROM_MISSING' );}, 0);
});
}
/*
EM_ASM({
setTimeout(function() {message_handler( $0 );}, 0);
}, msg_code[MSG_ROM_MISSING].c_str() );
*/
//emu->setTakeAutoSnapshots(false);
//emu->setWarpLoad(true);
// emu->set(OPT_VICII_GRAY_DOT_BUG, false);
// emu->set(OPT_VICII_REVISION, VICII_PAL_6569_R1);
emu->set(OPT_SID_ENGINE, SIDENGINE_RESID);
// c64->configure(OPT_SID_SAMPLING, SID_SAMPLE_INTERPOLATE);
emu->set(OPT_MOUSE_MODEL, MOUSE_C1351);
emu->set(OPT_MOUSE_VELOCITY, 255);
emu->set(OPT_MOUSE_SHAKE_DETECT, false);
// master Volumne
emu->set(OPT_AUD_VOL_L, 100);
emu->set(OPT_AUD_VOL_R, 100);
emu->set(OPT_DRV_AUTO_CONFIG,DRIVE8,1);
//SID1 Volumne
/* c64->configure(OPT_AUDVOL, 1, 100);
c64->configure(OPT_AUDPAN, 1, 50);
c64->configure(OPT_SID_ENABLE, 1, true);
c64->configure(OPT_SID_ADDRESS, 1, 0xd420);
*/
//c64->configure(OPT_HIDE_SPRITES, true);
//c64->dump();
//printf("is running = %u\n",c64->isRunning());
// c64->dump();
// c64->drive1.dump();
// emu->setDebugLevel(2);
//emuid.setDebugLevel(4);
// c64->drive1.setDebugLevel(3);
// emuid.dump();
/*
c64->configure(OPT_DRV_POWER_SAVE, 8, true);
c64->configure(OPT_SID_POWER_SAVE, true);
c64->configure(OPT_VIC_POWER_SAVE, true);
*/
printf("waiting on emulator ready in javascript ...\n");
}
};
C64Wrapper *wrapper = NULL;
extern "C" int main(int argc, char** argv) {
wrapper= new C64Wrapper();
initSDL(wrapper->emu);
wrapper->run();
return 0;
}
/* emulation of macos mach_absolute_time() function. */
uint64_t mach_absolute_time()
{
uint64_t nano_now = (uint64_t)(emscripten_get_now()*1000000.0);
//printf("emsdk_now: %lld\n", nano_now);
return nano_now;
}
extern "C" void wasm_keyboard_reset()
{
printf("wasm_keyboard_reset\n");
// wrapper->emu->keyboard.keyboard->reset(true);
wrapper->emu->keyboard.releaseAll();
}
extern "C" void wasm_auto_type(char* text)
{
wrapper->emu->keyboard.autoType(text);
}
extern "C" void wasm_key(int code1, int code2, int pressed)
{
printf("wasm_key ( %d, %d, %d ) \n", code1, code2, pressed);
if(code1 == 9 && code2 == 9)
{
if(pressed == 1)
{
wrapper->emu->keyboard.keyboard->press(C64Key::restore);
}
else
{
wrapper->emu->keyboard.keyboard->release(C64Key::restore);
}
}
else if(pressed==1)
{
wrapper->emu->keyboard.keyboard->press(C64Key(code1,code2));
}
else
{
wrapper->emu->keyboard.keyboard->release(C64Key(code1,code2));
//wrapper->emu->keyboard.releaseRowCol(code1, code2);
}
}
extern "C" void wasm_schedule_key(int code1, int code2, int pressed, int frame_delay)
{
if(code1 == 9 && code2 == 9)
{
if(pressed == 1)
{
printf("scheduleKeyPress ( 31, %d ) \n", frame_delay);
// wrapper->emu->keyboard.keyboard->scheduleKeyPress(31, frame_delay); //pressRestore();
wrapper->emu->put(CMD_KEY_PRESS, KeyCmd(31,frame_delay / frame_rate));
}
else
{
printf("scheduleKeyRelease ( 31, %d ) \n", frame_delay);
// wrapper->emu->keyboard.scheduleKeyRelease(31, frame_delay); //releaseRestore();
wrapper->emu->put(CMD_KEY_RELEASE, KeyCmd(31,frame_delay / frame_rate));
}
}
else if(pressed==1)
{
printf("scheduleKeyPress ( %d, %d, %f ) \n", code1, code2, frame_delay / frame_rate);
// wrapper->emu->keyboard.scheduleKeyPress(C64Key(code1,code2), frame_delay);
auto xxx =C64Key(code1, code2);
wrapper->emu->put(CMD_KEY_PRESS, KeyCmd(xxx.nr,frame_delay/ frame_rate ));
// wrapper->emu->keyboard.keyboard->press(C64Key(code1,code2));
}
else
{
printf("scheduleKeyRelease ( %d, %d, %f ) \n", code1, code2, frame_delay / frame_rate);
//wrapper->emu->keyboard.scheduleKeyRelease(C64Key(code1,code2), frame_delay);
auto xxx =C64Key(code1, code2);
wrapper->emu->put(CMD_KEY_RELEASE, KeyCmd(xxx.nr,frame_delay / frame_rate));
// wrapper->emu->keyboard.keyboard->release(C64Key(code1,code2));
}
wrapper->emu->emu->update();
}
char wasm_pull_user_snapshot_file_json_result[255];
D64File *export_disk=NULL;
extern "C" void wasm_delete_disk()
{
if(export_disk!=NULL)
{
delete export_disk;
export_disk=NULL;
printf("disk memory deleted\n");
}
}
extern "C" char* wasm_export_disk()
{
wasm_delete_disk();
// if(!wrapper->emu->drive8.drive->hasDisk())
if(!wrapper->emu->drive8.getInfo().hasDisk)
{
printf("no disk in drive8\n");
sprintf(wasm_pull_user_snapshot_file_json_result, "{\"size\": 0 }");
return wasm_pull_user_snapshot_file_json_result;
}
// FSDevice *fs = FSDevice::makeWithDisk(wrapper->emu->drive8.disk);
// D64File *d64 = D64File::makeWithFileSystem(*fs);
FileSystem *fs = new FileSystem(*wrapper->emu->drive8.drive->disk);
export_disk = new D64File(*fs);
delete fs;
/* size_t size = d64->size;
uint8_t *buffer = new uint8_t[size];
d64->writeToBuffer(buffer);
for(int i=0; i < 30; i++)
{
printf("%d",buffer[i]);
}
printf("\n");
*/
sprintf(wasm_pull_user_snapshot_file_json_result, "{\"address\":%lu, \"size\": %lu }",
export_disk->data.ptr,
export_disk->data.size
);
printf("return => %s\n",wasm_pull_user_snapshot_file_json_result);
return wasm_pull_user_snapshot_file_json_result;
}
MediaFile *snapshot=NULL;
extern "C" void wasm_delete_user_snapshot()
{
// printf("request to free user_snapshot memory\n");
if(snapshot!=NULL)
{
delete snapshot;
snapshot=NULL;
printf("freed user_snapshot memory\n");
}
}
extern "C" char* wasm_take_user_snapshot()
{
printf("wasm_take_user_snapshot\n");
wasm_delete_user_snapshot();
snapshot = wrapper->emu->c64.takeSnapshot();
sprintf(wasm_pull_user_snapshot_file_json_result, "{\"address\":%lu, \"size\": %lu, \"width\": %lu, \"height\":%lu }",
(unsigned long)snapshot->getData(),
snapshot->getSize(),
snapshot->previewImageSize().first,
snapshot->previewImageSize().second
);
printf("return => %s\n",wasm_pull_user_snapshot_file_json_result);
return wasm_pull_user_snapshot_file_json_result;
}
float sound_buffer[12*1024*2];
extern "C" float* wasm_get_sound_buffer_address()
{
return sound_buffer;
}
extern "C" unsigned wasm_copy_into_sound_buffer()
{
// auto count=wrapper->emu->audioPort.stream.count();
auto count=wrapper->emu->audioPort.audioPort->count();
auto copied_samples=0;
for(;copied_samples+1024<=count;copied_samples+=1024)
{
wrapper->emu->audioPort.copyMono((float *)sound_buffer+copied_samples, 1024);
}
sum_samples += copied_samples;
return copied_samples;
}
extern "C" unsigned wasm_copy_into_sound_buffer_stereo()
{
auto count=wrapper->emu->audioPort.audioPort->count();
auto copied_samples=0;
for(unsigned ipos=1024;ipos<=count;ipos+=1024)
{
wrapper->emu->audioPort.copyStereo(
sound_buffer+copied_samples,
sound_buffer+copied_samples+1024,
1024);
copied_samples+=1024*2;
}
sum_samples += copied_samples;
return copied_samples/2;
}
extern "C" bool wasm_is_warping()
{
return wrapper->emu->isWarping();
}
extern "C" void wasm_set_warp(unsigned on)
{
warp_mode = (on == 1);
wrapper->emu->set(OPT_C64_WARP_MODE, warp_mode? WARP_AUTO : WARP_NEVER);
/* if(wrapper->emu->serialPort.serialPort->isTransferring() &&
(
(wrapper->emu->isWarping() && warp_mode == false)
||
(wrapper->emu->isWarping() == false && warp_mode)
)
)
{
if(warp_mode)
wrapper->emu->warpOn();
else
wrapper->emu->warpOff();
}
*/
}
bool borderless=false;
void calculate_viewport()
{
auto pal = wrapper->emu->vicii.vicii->pal();// frame_rate < 60;//*/wrapper->emu->vicii.vicii->pal();
if(pal)
{
eat_border_width = 31 * borderless;
xOff = 12 + eat_border_width + 92;
clipped_width = Texture::width -112 -24 -2*eat_border_width; //392
//428-12-24-2*33 =326
eat_border_height = 34 * borderless;
yOff = 16 + eat_border_height;
clipped_height = Texture::height -42 -2*eat_border_height; //248
//284-11-24-2*22=205
}
else //NTSC
{
eat_border_width = borderless? 31:0;
eat_border_height = borderless ? 9 :0;
auto ntsc_height=220;
auto ntsc_width = 370;
auto ntsc_xoffset = 12 + 6/*NTSC*/ + eat_border_width + 92;
clipped_height = ntsc_height -2*eat_border_height;
if(borderless)
{
eat_border_width = 31; //redundant
xOff = 12 + eat_border_width + 92;
clipped_width = Texture::width -112 -24 -2*eat_border_width; //392
if( wrapper->emu->get(OPT_VICII_REVISION) ==VICII_NTSC_6567_R56A)
{
eat_border_height++;
}
}
else
{
clipped_width = ntsc_width;
// printf("xOff=%u + ntsc_off=%i\n", xOff,ntsc_xoffset);
xOff = ntsc_xoffset;
}
yOff = 16 + eat_border_height;
}
SDL_SetWindowMinimumSize(window, clipped_width, clipped_height);
SDL_RenderSetLogicalSize(renderer, clipped_width, clipped_height);
SDL_SetWindowSize(window, clipped_width, clipped_height);
}
extern "C" void wasm_set_borderless(unsigned on)
{
borderless= on==1;
calculate_viewport();
}
string
extractSuffix(const string &s)
{
auto idx = s.rfind('.');
auto pos = idx != string::npos ? idx + 1 : 0;
auto len = string::npos;
return s.substr(pos, len);
}
extern "C" const char* wasm_loadFile(char* name, Uint8 *blob, long len)
{
printf("load file=%s len=%ld, header bytes= %x, %x, %x\n", name, len, blob[0],blob[1],blob[2]);
filename=name;
// auto file_suffix= util::extractSuffix(name);
if(wrapper == NULL)
{
return "";
}
bool file_still_unprocessed=true;
if (D64File::isCompatible(filename)) {
try{
printf("try to build D64File\n");
/* D64File d64 = D64File(blob, len);
auto disk = std::make_unique<Disk>(d64);
printf("isD64\n");
wrapper->emu->drive8.insertDisk(std::move(disk));*/
auto file = MediaFile::make(blob, len, FILETYPE_D64);
wrapper->emu->drive8.insertMedia(*file, false /* wp*/);
file_still_unprocessed=false;
} catch(Error &exception) {
//ErrorCode ec=exception.data;
printf("%s\n", exception.description.c_str());
//printf("%s\n", ErrorCodeEnum::key(ec));
}
}
if (file_still_unprocessed && G64File::isCompatible(filename)) {
try{
printf("try to build G64File\n");
auto file = MediaFile::make(blob, len, FILETYPE_G64);
printf("isG64 ...\n");
wrapper->emu->drive8.insertMedia(*file, false /* wp*/);
file_still_unprocessed=false;
} catch(Error &exception) {
printf("%s\n", exception.what());
}
}
if (file_still_unprocessed && PRGFile::isCompatible(filename)) {
try
{
printf("try to build PRGFile\n");