forked from vAmigaWeb/vAmigaWeb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
2166 lines (1878 loc) · 58.4 KB
/
main.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 main.cpp
* @author mithrendal and Dirk W. Hoffmann, www.dirkwhoffmann.de
* @copyright Dirk W. Hoffmann. All rights reserved.
*/
#include <stdio.h>
#include "config.h"
#include "Amiga.h"
#include "AmigaTypes.h"
#include "RomFile.h"
#include "ExtendedRomFile.h"
#include "ADFFile.h"
#include "DMSFile.h"
#include "EXEFile.h"
#include "HDFFile.h"
#include "Snapshot.h"
#include "EADFFile.h"
#include "OtherFile.h"
#include "MemUtils.h"
#include <emscripten.h>
#ifdef wasm_worker
#include <emscripten/wasm_worker.h>
#include <emscripten/threading.h>
#endif
using namespace vamiga;
#define RENDER_SOFTWARE 0
#define RENDER_GPU 1
#define RENDER_SHADER 2
u8 render_method = RENDER_SOFTWARE;
#define DISPLAY_NARROW 0
#define DISPLAY_STANDARD 1
#define DISPLAY_WIDER 2
#define DISPLAY_OVERSCAN 3
#define DISPLAY_ADAPTIVE 4
#define DISPLAY_BORDERLESS 5
u8 geometry = DISPLAY_ADAPTIVE;
const char* display_names[] = {"narrow","standard","wider","overscan",
"viewport tracking","borderless"};
bool log_on=false;
//HRM: In NTSC, the fields are 262, then 263 lines and in PAL, 312, then 313 lines.
//312-50=262
#define PAL_EXTRA_VPIXEL 50
bool ntsc=false;
u8 target_fps = 50;
extern "C" void wasm_set_display(const char *name);
#ifdef wasm_worker
EM_JS(void, console_log, (const char* str), {
console.log(UTF8ToString(str));
});
void log_on_main_thread(const char *msg)
{
console_log(msg);
//printf("%s",msg);
//assert(!emscripten_current_thread_is_wasm_worker());
}
void main_log(const char *msg)
{
//call function on ui thread
emscripten_wasm_worker_post_function_sig(EMSCRIPTEN_WASM_WORKER_ID_PARENT,(void *) log_on_main_thread, "id", msg);
}
#endif
int clip_width = 724 * TPP;
int clip_height = 568;
int clip_offset = 0;//HBLANK_MIN*4;//HBLANK_MAX*4;
int buffer_size = 4096;
bool prevLOF = false;
bool currLOF = false;
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 = HPIXELS*TPP -12 -24 -2*eat_border_width; //392
int clipped_height = VPOS_MAX -12 -24 -2*eat_border_height; //248
int bFullscreen = false;
char *filename = NULL;
//unsigned int warp_to_frame=0;
int sum_samples=0;
double last_time = 0.0 ;
double last_time_calibrated = 0.0 ;
unsigned int executed_frame_count=0;
int64_t total_executed_frame_count=0;
double start_time=0;//emscripten_get_now();
unsigned int rendered_frame_count=0;
unsigned int frames=0, seconds=0;
// The emscripten "main loop" replacement function.
u16 vstart_min=26;
u16 vstop_max=256;
u16 hstart_min=200;
u16 hstop_max=HPIXELS;
u16 vstart_min_tracking=26;
u16 vstop_max_tracking=256;
u16 hstart_min_tracking=200;
u16 hstop_max_tracking=HPIXELS;
bool reset_calibration=true;
bool request_to_reset_calibration=false;
void set_viewport_dimensions()
{
if(log_on) printf("calib: set_viewport_dimensions hmin=%d, hmax=%d, vmin=%d, vmax=%d\n",hstart_min,hstop_max,vstart_min, vstop_max);
// hstart_min=0; hstop_max=HPIXELS;
if(render_method==RENDER_SHADER)
{
if(geometry == DISPLAY_ADAPTIVE || geometry == DISPLAY_BORDERLESS)
{
clipped_width = hstop_max-hstart_min;
clipped_height = vstop_max-vstart_min;
}
}
else
{
if(geometry== DISPLAY_ADAPTIVE || geometry == DISPLAY_BORDERLESS)
{
xOff = hstart_min;
yOff = vstart_min;
clipped_width = hstop_max-hstart_min;
clipped_height = vstop_max-vstart_min;
}
}
EM_ASM({js_set_display($0,$1,$2,$3); scaleVMCanvas();},xOff, yOff, clipped_width*TPP,clipped_height );
}
u16 vstart_min_calib=0;
u16 vstop_max_calib=0;
u16 hstart_min_calib=0;
u16 hstop_max_calib=0;
bool calculate_viewport_dimensions(u32 *texture)
{
if(reset_calibration)
{
//printf("reset_calibration %d %d\n",vstop_max_tracking, vstart_min_tracking);
//first call after new viewport size
//set start values to the opposite max. borderpos (scan area)
vstart_min_calib = vstop_max_tracking;
vstop_max_calib = vstart_min_tracking;
hstart_min_calib = hstop_max_tracking;
hstop_max_calib = hstart_min_tracking;
reset_calibration=false;
}
bool pixels_found=false;
//top border: get vstart_min from texture
u32 ref_pixel= texture[(HPIXELS*vstart_min_tracking + hstart_min_tracking)*TPP];
// printf("refpixel:%u\n",ref_pixel);
for(int y=vstart_min_tracking;y<vstart_min_calib && !pixels_found;y++)
{
// printf("\nvstart_line:%u\n",y);
for(int x=hstart_min_tracking;x<hstop_max_tracking;x++){
u32 pixel= texture[(HPIXELS*y + x)*TPP];
// printf("%u:%u ",x,pixel);
if(ref_pixel != pixel){
pixels_found=true;
// printf("\nfirst_pos=%d, vstart_min=%d, vstart_min_track=%d\n",y, vstart_min, vstart_min_tracking);
vstart_min_calib= y<vstart_min_calib?y:vstart_min_calib;
break;
}
}
}
//bottom border: get vstop_max from texture
pixels_found=false;
ref_pixel= texture[ (HPIXELS*vstop_max_tracking + hstart_min_tracking)*TPP];
// printf("refpixel:%u\n",ref_pixel);
// printf("hstart:%u,hstop:%u\n",hstart_min,hstop_max);
for(int y=vstop_max_tracking;y>vstop_max_calib && !pixels_found;y--)
{
// printf("\nline:%u\n",y);
for(int x=hstart_min_tracking;x<hstop_max_tracking;x++){
u32 pixel= texture[(HPIXELS*y + x)*TPP];
// printf("%u:%u ",x,pixel);
if(ref_pixel != pixel){
pixels_found=true;
y++; //this line has pixels, so put vstop_max to the next line
// printf("\ncalib: last_pos=%d, vstop_max=%d, vstop_max_tracking%d\n",y, vstop_max, vstop_max_tracking);
vstop_max_calib= y>vstop_max_calib?y:vstop_max_calib;
break;
}
}
}
//left border: get hstart_min from texture
pixels_found=false;
ref_pixel= texture[ (HPIXELS*vstart_min_tracking + hstart_min_tracking-1)*TPP];
for(int x=hstart_min_tracking-1;x<hstart_min_calib;x++)
{
// printf("\nrow:%u\n",x);
for(int y=vstart_min_calib;y<vstop_max_calib && !pixels_found;y++)
{
u32 pixel= texture[(HPIXELS*y + x)*TPP];
// printf("%u:%u ",x,pixel);
if(ref_pixel != pixel){
pixels_found=true;
// printf("\nlast_xpos=%d, hstop_max=%d\n",x, hstop_max);
hstart_min_calib=x<hstart_min_calib?x:hstart_min_calib;
break;
}
}
}
//right border: get hstop_max from texture
pixels_found=false;
ref_pixel= texture[ (HPIXELS*vstart_min_tracking + hstop_max_tracking)*TPP];
// printf("hstop_max_tracking %d\n",hstop_max_tracking);
// printf("hstop_max_calib %d\n",hstop_max_calib);
// printf("ref_pixel %d\n",ref_pixel);
for(int x=hstop_max_tracking;x>hstop_max_calib;x--)
{
// printf("\ncol:%u\n",x);
for(int y=vstart_min_calib;y<vstop_max_calib && !pixels_found;y++)
{
u32 pixel= texture[(HPIXELS*y + x)*TPP];
// printf("%u:%u ",x,pixel);
if(ref_pixel != pixel){
pixels_found=true;
x++; //this line has pixels, so put hstop_max to the next line
// printf("\nlast_xpos=%d, hstop_max=%d\n",x, hstop_max);
hstop_max_calib= x>hstop_max_calib?x:hstop_max_calib;
break;
}
}
}
// printf("->hstop_max_calib %d\n",hstop_max_calib);
bool dimensions_changed=false;
//handdisk start pixel 416, stop pixel 676
#define HANDSTART_PIXEL 416
#define HANDSTOP_PIXEL 676
if(hstart_min_calib > HANDSTART_PIXEL-32)
hstart_min_calib= HANDSTART_PIXEL-32;
if(hstop_max_calib < HANDSTOP_PIXEL+32)
hstop_max_calib= HANDSTOP_PIXEL+32;
if(hstart_min_calib < hstop_max_calib )
{
if(hstart_min!=hstart_min_calib)
{
hstart_min=hstart_min_calib;
dimensions_changed=true;
}
if(hstop_max != hstop_max_calib)
{
hstop_max = hstop_max_calib;
dimensions_changed=true;
}
}
if(vstart_min_calib > VPOS_MAX/4)
vstart_min_calib= VPOS_MAX/4;
if(vstop_max_calib < 3*VPOS_MAX/4)
vstop_max_calib= 3*VPOS_MAX/4;
if(vstart_min_calib< vstop_max_calib)
{
if(vstart_min != vstart_min_calib)
{
vstart_min=vstart_min_calib;
dimensions_changed=true;
}
if(vstop_max != vstop_max_calib)
{
vstop_max=vstop_max_calib;
dimensions_changed=true;
}
}
// printf("\nCALIBRATED: (%d,%d) (%d,%d) \n",hstart_min, vstart_min, hstop_max, vstop_max);
//printf("calib dimensions changed=%d\n",dimensions_changed);
return dimensions_changed;
}
#define MAX_GAP 5
Amiga *thisAmiga=NULL;
u8 executed_since_last_host_frame=0;
extern "C" void wasm_execute()
{
thisAmiga->execute();
executed_since_last_host_frame++;
executed_frame_count++;
total_executed_frame_count++;
}
#ifdef wasm_worker
char wasm_log_buffer[256];
#endif
extern "C" int wasm_draw_one_frame(double now)
//int draw_one_frame_into_SDL(void *thisAmiga, float now)
{
//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 * target_fps);
Amiga *amiga = /*(Amiga *)*/thisAmiga;
bool show_stat=true;
if(amiga->isWarping() == true)
{
if(log_on) printf("warping at least 25 frames at once ...\n");
int i=25;
while(amiga->isWarping() == true && i>0)
{
amiga->execute();
i--;
/* if(warp_to_frame>0 && amiga->agnus.pos.frame > warp_to_frame)
{
if(log_on) printf("reached warp_to_frame count\n");
amiga->warpOff();
warp_to_frame=0;
}
*/
}
start_time=now;
total_executed_frame_count=0;
targetFrameCount=1;
show_stat=false;
}
if(show_stat)
{
//lost the sync
if(targetFrameCount-total_executed_frame_count > MAX_GAP)
{
if(log_on) {
#ifdef wasm_worker
snprintf(wasm_log_buffer,sizeof(wasm_log_buffer),"lost sync target=%lld, total_executed=%lld\n", targetFrameCount, total_executed_frame_count);
main_log(wasm_log_buffer);
#else
printf("lost sync target=%lld, total_executed=%lld\n", targetFrameCount, total_executed_frame_count);
#endif
}
//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
}
if(now-last_time>= 1000.0)
{
double passed_time= now - last_time;
last_time = now;
seconds += 1;
frames += rendered_frame_count;
if(log_on) {
#ifdef wasm_worker
snprintf(wasm_log_buffer,sizeof(wasm_log_buffer),"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);
main_log(wasm_log_buffer);
#else
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);
#endif
}
sum_samples=0;
rendered_frame_count=0;
executed_frame_count=0;
}
}
int behind=targetFrameCount-total_executed_frame_count;
if(behind<=0 && executed_since_last_host_frame==0)
return -1; //don't render if ahead of time and everything is already drawn
#ifndef wasm_worker
if(behind>0)
{
amiga->execute();
executed_frame_count++;
total_executed_frame_count++;
behind--;
}
#endif
executed_since_last_host_frame=0;
rendered_frame_count++;
if(geometry == DISPLAY_BORDERLESS)
{
// printf("calibration count=%f\n",now-last_time_calibrated);
if(now-last_time_calibrated >= 700.0)
{
last_time_calibrated=now;
auto stable_ptr = amiga->denise.pixelEngine.stablePtr();
bool dimensions_changed=calculate_viewport_dimensions((u32 *)stable_ptr - HBLANK_MIN*4*TPP);
if(dimensions_changed)
{
#ifdef wasm_worker
emscripten_wasm_worker_post_function_v(EMSCRIPTEN_WASM_WORKER_ID_PARENT,set_viewport_dimensions);
#else
set_viewport_dimensions();
#endif
}
if(request_to_reset_calibration)
{
reset_calibration=true;
request_to_reset_calibration=false;
}
}
}
return behind;
}
int sample_size=0;
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_main_thread(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 );
}
void send_message_to_js_with_param(const char * message_as_string, long data1, long data2)
{
if(log_on)
{
#ifdef wasm_worker
if(emscripten_current_thread_is_wasm_worker())
{
snprintf(wasm_log_buffer,sizeof(wasm_log_buffer),"worker: vAmiga message=%s, data1=%ld, data2=%ld\n", message_as_string, data1, data2);
main_log(wasm_log_buffer);
}
else
{
printf("main: vAmiga message=%s\n", message_as_string);
}
#else
printf("vAmiga message=%s, data1=%ld, data2=%ld\n", message_as_string, data1, data2);
#endif
}
#ifdef wasm_worker
if(emscripten_current_thread_is_wasm_worker())
{
emscripten_wasm_worker_post_function_sig(EMSCRIPTEN_WASM_WORKER_ID_PARENT,(void*)send_message_to_js_main_thread, "iii", message_as_string, data1, data2);
}
else
send_message_to_js_main_thread(message_as_string, data1, data2);
#else
send_message_to_js_main_thread(message_as_string, data1, data2);
#endif
}
/*
void send_message_to_js_w(const char * msg, long data1, long data2)
{
send_message_to_js_main_thread(msg,data1,data2);
}
*/
//bool paused_the_emscripten_main_loop=false;
bool already_run_the_emscripten_main_loop=false;
bool warp_mode=false;
//void theListener(const void * amiga, long type, int data1, int data2, int data3, int data4){
void theListener(const void * amiga, Message msg){
int data1=msg.value;
int data2=0;
if(msg.type == MSG_VIEWPORT)
{
if(msg.viewport.hstrt==0 && msg.viewport.vstrt==0 && msg.viewport.hstop == 0 && msg.viewport.vstop == 0)
return;
hstart_min= msg.viewport.hstrt;
vstart_min= msg.viewport.vstrt;
hstop_max= msg.viewport.hstop;
vstop_max= msg.viewport.vstop;
if(log_on) printf("tracking MSG_VIEWPORT=%d, %d, %d, %d\n",hstart_min, vstart_min, hstop_max, vstop_max);
hstart_min *=2;
hstop_max *=2;
hstart_min=hstart_min<208 ? 208:hstart_min;
hstop_max=hstop_max>(HPIXELS+HBLANK_MAX)? (HPIXELS+HBLANK_MAX):hstop_max;
if(vstart_min < 26)
vstart_min = 26;
if(vstop_max > VPOS_MAX)
vstop_max = VPOS_MAX; //312
if(ntsc && vstop_max > VPOS_MAX-PAL_EXTRA_VPIXEL )
vstop_max = VPOS_MAX-PAL_EXTRA_VPIXEL;
if(log_on)
{
#ifdef wasm_worker
snprintf(wasm_log_buffer,sizeof(wasm_log_buffer),"tracking MSG_VIEWPORT=%u %u %u %u\n",hstart_min, vstart_min, hstop_max, vstop_max);
main_log(wasm_log_buffer);
#else
printf("tracking MSG_VIEWPORT=%u %u %u %u\n",hstart_min, vstart_min, hstop_max, vstop_max);
#endif
}
vstart_min_tracking = vstart_min;
vstop_max_tracking = vstop_max;
hstart_min_tracking = hstart_min;
hstop_max_tracking = hstop_max;
#ifdef wasm_worker
emscripten_wasm_worker_post_function_v(EMSCRIPTEN_WASM_WORKER_ID_PARENT,set_viewport_dimensions);
#else
set_viewport_dimensions();
#endif
reset_calibration=true;
}
else if(msg.type == MSG_VIDEO_FORMAT)
{
if(log_on) printf("video format=%s data1=%d\n",VideoFormatEnum::key(msg.value),data1);
wasm_set_display(msg.value == NTSC? "ntsc":"pal");
request_to_reset_calibration=true;
}
else if(msg.type == MSG_DRIVE_STEP || msg.type == MSG_DRIVE_POLL
||msg.type == MSG_HDR_STEP)
{
data1=msg.drive.nr;
data2=msg.drive.value;
}
if(msg.type == MSG_DRIVE_SELECT)
{
}
else
{
const char *message_as_string = (const char *)MsgTypeEnum::key((MsgType)msg.type);
if(msg.type == MSG_SER_OUT)
{
int byte = ((Amiga *)amiga)->serialPort.readOutgoingByte();
while(byte>=0)
{
send_message_to_js_with_param(message_as_string, byte, data2);
byte = ((Amiga *)amiga)->serialPort.readOutgoingByte();
}
}
else
{
send_message_to_js_with_param(message_as_string, data1, data2);
}
}
}
class vAmigaWrapper {
public:
Amiga *amiga;
vAmigaWrapper()
{
printf("constructing vAmiga ...\n");
this->amiga = new Amiga();
}
~vAmigaWrapper()
{
printf("closing wrapper");
}
void run()
{
try { amiga->isReady(); } catch(...) {
printf("***** put missing rom message\n");
// amiga->msgQueue.put(ROM_MISSING);
EM_ASM({
setTimeout(function() {message_handler( 'MSG_ROM_MISSING' );}, 0);
});
}
printf("wrapper calls run on vAmiga->run() method\n");
printf("waiting on emulator ready in javascript ...\n");
}
};
#ifdef wasm_worker
emscripten_wasm_worker_t worker;
void run_in_worker()
{
// EM_ASM({console.log("Hello log")});
// emscripten_wasm_worker_post_function_sig(EMSCRIPTEN_WASM_WORKER_ID_PARENT,(void *) test_success, "id");
auto behind = wasm_draw_one_frame(emscripten_performance_now());
while(behind>0)
{
thisAmiga->execute();
executed_since_last_host_frame++;
executed_frame_count++;
total_executed_frame_count++;
behind--;
}
}
#endif
extern "C" bool wasm_is_worker_built()
{
#ifdef wasm_worker
return true;
#else
return false;
#endif
}
extern "C" void wasm_worker_run()
{
#ifdef wasm_worker
emscripten_wasm_worker_post_function_v(worker, run_in_worker);
#endif
}
vAmigaWrapper *wrapper = NULL;
extern "C" int main(int argc, char** argv) {
#ifdef wasm_worker
worker = emscripten_malloc_wasm_worker(/*stack size: */2048);
printf("running on wasm webworker...\n");
#else
printf("running on main thread...\n");
#endif
wrapper= new vAmigaWrapper();
printf("adding a listener to vAmiga message queue...\n");
// wrapper->amiga->msgQueue.setListener(wrapper->amiga, &theListener);
wrapper->amiga->launch(wrapper->amiga, &theListener);
wrapper->amiga->defaults.setFallback(OPT_HDC_CONNECT, 0, false);
// wrapper->amiga->defaults.setFallback(OPT_FILTER_TYPE, FILTER_NONE);
// wrapper->amiga->configure(OPT_FILTER_TYPE, FILTER_NONE);
// master Volumne
wrapper->amiga->configure(OPT_AUDVOLL, 100);
wrapper->amiga->configure(OPT_AUDVOLR, 100);
//Volumne
wrapper->amiga->configure(OPT_AUDVOL, 0, 100);
wrapper->amiga->configure(OPT_AUDPAN, 0, 0);
wrapper->amiga->configure(OPT_CHIP_RAM, 512);
wrapper->amiga->configure(OPT_SLOW_RAM, 512);
wrapper->amiga->configure(OPT_AGNUS_REVISION, AGNUS_OCS);
//turn automatic hd mounting off because kick1.2 makes trouble
wrapper->amiga->configure(OPT_HDC_CONNECT,/*hd drive*/ 0, /*enable*/false);
wrapper->amiga->configure(OPT_DRIVE_CONNECT,/*df1*/ 1, /*enable*/false);
wrapper->run();
return 0;
}
extern "C" Texel * wasm_pixel_buffer()
{
auto stable_ptr = thisAmiga->denise.pixelEngine.stablePtr();
return stable_ptr;
}
extern "C" u32 wasm_frame_info()
{
auto &stableBuffer = thisAmiga->denise.pixelEngine.getStableBuffer();
u32 info = (u32)stableBuffer.nr;
info = info<<1;
if(stableBuffer.prevlof)
info |= 0x0001;
info = info<<1;
if(stableBuffer.lof)
info |= 0x0001;
return info;
}
extern "C" int wasm_get_renderer()
{
return render_method;
}
extern "C" int wasm_get_render_width()
{
return clipped_width;
}
extern "C" int wasm_get_render_height()
{
return clipped_height;
}
extern "C" void wasm_set_target_fps(int _target_fps)
{
target_fps=_target_fps;
}
/* 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_key(int code, int pressed)
{
// printf("wasm_key ( %d, %d ) \n", code, pressed);
if(pressed==1)
{
wrapper->amiga->keyboard.pressKey(code);
}
else
{
wrapper->amiga->keyboard.releaseKey(code);
}
}
extern "C" void wasm_auto_type(int code, int duration, int delay)
{
if(log_on) printf("auto_type ( %d, %d, %d ) \n", code, duration, delay);
wrapper->amiga->keyboard.autoType(code, MSEC(duration), MSEC(delay));
}
extern "C" void wasm_schedule_key(int code1, int code2, int pressed, int frame_delay)
{
if(pressed==1)
{
// printf("scheduleKeyPress ( %d, %d, %d ) \n", code1, code2, frame_delay);
wrapper->amiga->keyboard.pressKey(code1);
// wrapper->amiga->keyboard.scheduleKeyPress(*new AmigaKey(code1,code2), frame_delay);
}
else
{
// printf("scheduleKeyRelease ( %d, %d, %d ) \n", code1, code2, frame_delay);
wrapper->amiga->keyboard.releaseKey(code1);
// wrapper->amiga->keyboard.scheduleKeyRelease(*new C64Key(code1,code2), frame_delay);
}
}
char wasm_pull_user_snapshot_file_json_result[255];
extern "C" bool wasm_has_disk(const char *drive_name)
{
if(strcmp(drive_name,"df0") == 0)
{
return wrapper->amiga->df0.hasDisk();
}
else if(strcmp(drive_name,"df1") == 0)
{
return wrapper->amiga->df1.hasDisk();
}
else if(strcmp(drive_name,"df2") == 0)
{
return wrapper->amiga->df2.hasDisk();
}
else if(strcmp(drive_name,"df3") == 0)
{
return wrapper->amiga->df3.hasDisk();
}
else if (strcmp(drive_name,"dh0") == 0)
{
return wrapper->amiga->hd0.hasDisk();
}
else if (strcmp(drive_name,"dh1") == 0)
{
return wrapper->amiga->hd1.hasDisk();
}
else if (strcmp(drive_name,"dh2") == 0)
{
return wrapper->amiga->hd2.hasDisk();
}
else if (strcmp(drive_name,"dh3") == 0)
{
return wrapper->amiga->hd3.hasDisk();
}
return false;
}
extern "C" void wasm_eject_disk(const char *drive_name)
{
if(strcmp(drive_name,"df0") == 0)
{
if(wrapper->amiga->df0.hasDisk())
wrapper->amiga->df0.ejectDisk();
}
else if(strcmp(drive_name,"df1") == 0)
{
if(wrapper->amiga->df1.hasDisk())
wrapper->amiga->df1.ejectDisk();
}
else if(strcmp(drive_name,"df2") == 0)
{
if(wrapper->amiga->df2.hasDisk())
wrapper->amiga->df2.ejectDisk();
}
else if(strcmp(drive_name,"df3") == 0)
{
if(wrapper->amiga->df3.hasDisk())
wrapper->amiga->df3.ejectDisk();
}
else if (strcmp(drive_name,"dh0") == 0)
{
if(wrapper->amiga->hd0.hasDisk())
{
wrapper->amiga->powerOff();
wrapper->amiga->configure(OPT_HDC_CONNECT,/*hd drive*/ 0, /*enable*/false);
wrapper->amiga->powerOn();
}
}
else if (strcmp(drive_name,"dh1") == 0)
{
if(wrapper->amiga->hd1.hasDisk())
{
wrapper->amiga->powerOff();
wrapper->amiga->configure(OPT_HDC_CONNECT,/*hd drive*/ 1, /*enable*/false);
wrapper->amiga->powerOn();
}
}
else if (strcmp(drive_name,"dh2") == 0)
{
if(wrapper->amiga->hd2.hasDisk())
{
wrapper->amiga->powerOff();
wrapper->amiga->configure(OPT_HDC_CONNECT,/*hd drive*/ 2, /*enable*/false);
wrapper->amiga->powerOn();
}
}
else if (strcmp(drive_name,"dh3") == 0)
{
if(wrapper->amiga->hd3.hasDisk())
{
wrapper->amiga->powerOff();
wrapper->amiga->configure(OPT_HDC_CONNECT,/*hd drive*/ 3, /*enable*/false);
wrapper->amiga->powerOn();
}
}
}
extern "C" char* wasm_export_disk(const char *drive_name)
{
Buffer<u8> *data=NULL;
sprintf(wasm_pull_user_snapshot_file_json_result, "{\"size\": 0 }");
if(strcmp(drive_name,"df0") == 0)
{
if(!wrapper->amiga->df0.hasDisk())
{
return wasm_pull_user_snapshot_file_json_result;
}
ADFFile *adf = new ADFFile(wrapper->amiga->df0);
data=&(adf->data);
}
else if(strcmp(drive_name,"df1") == 0)
{
if(!wrapper->amiga->df1.hasDisk())
{
return wasm_pull_user_snapshot_file_json_result;
}
ADFFile *adf = new ADFFile(wrapper->amiga->df1);
data=&(adf->data);
}
else if(strcmp(drive_name,"df2") == 0)
{
if(!wrapper->amiga->df2.hasDisk())
{
return wasm_pull_user_snapshot_file_json_result;
}
ADFFile *adf = new ADFFile(wrapper->amiga->df2);
data=&(adf->data);
}
else if(strcmp(drive_name,"df3") == 0)
{
if(!wrapper->amiga->df3.hasDisk())
{
return wasm_pull_user_snapshot_file_json_result;
}
ADFFile *adf = new ADFFile(wrapper->amiga->df3);
data=&(adf->data);
}
else if (strcmp(drive_name,"dh0") == 0)
{
if(!wrapper->amiga->hd0.hasDisk())
{
return wasm_pull_user_snapshot_file_json_result;
}
HDFFile *hdf = new HDFFile(wrapper->amiga->hd0);
data=&(hdf->data);
}
else if (strcmp(drive_name,"dh1") == 0)
{
if(!wrapper->amiga->hd1.hasDisk())
{
return wasm_pull_user_snapshot_file_json_result;
}
HDFFile *hdf = new HDFFile(wrapper->amiga->hd1);
data=&(hdf->data);
}
else if (strcmp(drive_name,"dh2") == 0)
{
if(!wrapper->amiga->hd2.hasDisk())
{
return wasm_pull_user_snapshot_file_json_result;
}
HDFFile *hdf = new HDFFile(wrapper->amiga->hd2);
data=&(hdf->data);
}
else if (strcmp(drive_name,"dh3") == 0)
{
if(!wrapper->amiga->hd3.hasDisk())
{
return wasm_pull_user_snapshot_file_json_result;
}
HDFFile *hdf = new HDFFile(wrapper->amiga->hd3);
data=&(hdf->data);
}