-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
1964 lines (1886 loc) · 60 KB
/
main.c
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
#if 0
gcc -s -O2 -Wno-unused-result -fwrapv main.c `sdl-config --cflags --libs`
exit
#endif
// This file is public domain.
#include "SDL.h"
#include <dirent.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
// Subprocess support
#include <signal.h>
#include <sys/wait.h>
#ifdef __linux
#include <pty.h>
#include <sys/prctl.h>
#endif
#ifdef __NetBSD__
#include <sys/ioctl.h>
#include <util.h>
#endif
static const Uint8 fontdata[]={ // font for built-in menus
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x78,0x78,0x30,0x30,0x00,0x30,0x00,
0x6C,0x6C,0x6C,0x00,0x00,0x00,0x00,0x00,0x6C,0x6C,0xFE,0x6C,0xFE,0x6C,0x6C,0x00,
0x30,0x7C,0xC0,0x78,0x0C,0xF8,0x30,0x00,0x00,0xC6,0xCC,0x18,0x30,0x66,0xC6,0x00,
0x38,0x6C,0x38,0x76,0xDC,0xCC,0x76,0x00,0x60,0x60,0xC0,0x00,0x00,0x00,0x00,0x00,
0x18,0x30,0x60,0x60,0x60,0x30,0x18,0x00,0x60,0x30,0x18,0x18,0x18,0x30,0x60,0x00,
0x00,0x66,0x3C,0xFF,0x3C,0x66,0x00,0x00,0x00,0x30,0x30,0xFC,0x30,0x30,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x60,0x00,0x00,0x00,0xFC,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x06,0x0C,0x18,0x30,0x60,0xC0,0x80,0x00,
0x7C,0xC6,0xCE,0xDE,0xF6,0xE6,0x7C,0x00,0x30,0x70,0x30,0x30,0x30,0x30,0xFC,0x00,
0x78,0xCC,0x0C,0x38,0x60,0xCC,0xFC,0x00,0x78,0xCC,0x0C,0x38,0x0C,0xCC,0x78,0x00,
0x1C,0x3C,0x6C,0xCC,0xFE,0x0C,0x1E,0x00,0xFC,0xC0,0xF8,0x0C,0x0C,0xCC,0x78,0x00,
0x38,0x60,0xC0,0xF8,0xCC,0xCC,0x78,0x00,0xFC,0xCC,0x0C,0x18,0x30,0x30,0x30,0x00,
0x78,0xCC,0xCC,0x78,0xCC,0xCC,0x78,0x00,0x78,0xCC,0xCC,0x7C,0x0C,0x18,0x70,0x00,
0x00,0x30,0x30,0x00,0x00,0x30,0x30,0x00,0x00,0x30,0x30,0x00,0x00,0x30,0x30,0x60,
0x18,0x30,0x60,0xC0,0x60,0x30,0x18,0x00,0x00,0x00,0xFC,0x00,0x00,0xFC,0x00,0x00,
0x60,0x30,0x18,0x0C,0x18,0x30,0x60,0x00,0x78,0xCC,0x0C,0x18,0x30,0x00,0x30,0x00,
0x7C,0xC6,0xDE,0xDE,0xDE,0xC0,0x78,0x00,0x30,0x78,0xCC,0xCC,0xFC,0xCC,0xCC,0x00,
0xFC,0x66,0x66,0x7C,0x66,0x66,0xFC,0x00,0x3C,0x66,0xC0,0xC0,0xC0,0x66,0x3C,0x00,
0xF8,0x6C,0x66,0x66,0x66,0x6C,0xF8,0x00,0xFE,0x62,0x68,0x78,0x68,0x62,0xFE,0x00,
0xFE,0x62,0x68,0x78,0x68,0x60,0xF0,0x00,0x3C,0x66,0xC0,0xC0,0xCE,0x66,0x3E,0x00,
0xCC,0xCC,0xCC,0xFC,0xCC,0xCC,0xCC,0x00,0x78,0x30,0x30,0x30,0x30,0x30,0x78,0x00,
0x1E,0x0C,0x0C,0x0C,0xCC,0xCC,0x78,0x00,0xE6,0x66,0x6C,0x78,0x6C,0x66,0xE6,0x00,
0xF0,0x60,0x60,0x60,0x62,0x66,0xFE,0x00,0xC6,0xEE,0xFE,0xFE,0xD6,0xC6,0xC6,0x00,
0xC6,0xE6,0xF6,0xDE,0xCE,0xC6,0xC6,0x00,0x38,0x6C,0xC6,0xC6,0xC6,0x6C,0x38,0x00,
0xFC,0x66,0x66,0x7C,0x60,0x60,0xF0,0x00,0x78,0xCC,0xCC,0xCC,0xDC,0x78,0x1C,0x00,
0xFC,0x66,0x66,0x7C,0x6C,0x66,0xE6,0x00,0x78,0xCC,0xE0,0x70,0x1C,0xCC,0x78,0x00,
0xFC,0xB4,0x30,0x30,0x30,0x30,0x78,0x00,0xCC,0xCC,0xCC,0xCC,0xCC,0xCC,0xFC,0x00,
0xCC,0xCC,0xCC,0xCC,0xCC,0x78,0x30,0x00,0xC6,0xC6,0xC6,0xD6,0xFE,0xEE,0xC6,0x00,
0xC6,0xC6,0x6C,0x38,0x38,0x6C,0xC6,0x00,0xCC,0xCC,0xCC,0x78,0x30,0x30,0x78,0x00,
0xFE,0xC6,0x8C,0x18,0x32,0x66,0xFE,0x00,0x78,0x60,0x60,0x60,0x60,0x60,0x78,0x00,
0xC0,0x60,0x30,0x18,0x0C,0x06,0x02,0x00,0x78,0x18,0x18,0x18,0x18,0x18,0x78,0x00,
0x10,0x38,0x6C,0xC6,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,
0x30,0x30,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x78,0x0C,0x7C,0xCC,0x76,0x00,
0xE0,0x60,0x60,0x7C,0x66,0x66,0xDC,0x00,0x00,0x00,0x78,0xCC,0xC0,0xCC,0x78,0x00,
0x1C,0x0C,0x0C,0x7C,0xCC,0xCC,0x76,0x00,0x00,0x00,0x78,0xCC,0xFC,0xC0,0x78,0x00,
0x38,0x6C,0x60,0xF0,0x60,0x60,0xF0,0x00,0x00,0x00,0x76,0xCC,0xCC,0x7C,0x0C,0xF8,
0xE0,0x60,0x6C,0x76,0x66,0x66,0xE6,0x00,0x30,0x00,0x70,0x30,0x30,0x30,0x78,0x00,
0x0C,0x00,0x0C,0x0C,0x0C,0xCC,0xCC,0x78,0xE0,0x60,0x66,0x6C,0x78,0x6C,0xE6,0x00,
0x70,0x30,0x30,0x30,0x30,0x30,0x78,0x00,0x00,0x00,0xCC,0xFE,0xFE,0xD6,0xC6,0x00,
0x00,0x00,0xF8,0xCC,0xCC,0xCC,0xCC,0x00,0x00,0x00,0x78,0xCC,0xCC,0xCC,0x78,0x00,
0x00,0x00,0xDC,0x66,0x66,0x7C,0x60,0xF0,0x00,0x00,0x76,0xCC,0xCC,0x7C,0x0C,0x1E,
0x00,0x00,0xDC,0x76,0x66,0x60,0xF0,0x00,0x00,0x00,0x7C,0xC0,0x78,0x0C,0xF8,0x00,
0x10,0x30,0x7C,0x30,0x30,0x34,0x18,0x00,0x00,0x00,0xCC,0xCC,0xCC,0xCC,0x76,0x00,
0x00,0x00,0xCC,0xCC,0xCC,0x78,0x30,0x00,0x00,0x00,0xC6,0xD6,0xFE,0xFE,0x6C,0x00,
0x00,0x00,0xC6,0x6C,0x38,0x6C,0xC6,0x00,0x00,0x00,0xCC,0xCC,0xCC,0x7C,0x0C,0xF8,
0x00,0x00,0xFC,0x98,0x30,0x64,0xFC,0x00,0x1C,0x30,0x30,0xE0,0x30,0x30,0x1C,0x00,
0x18,0x18,0x18,0x00,0x18,0x18,0x18,0x00,0xE0,0x30,0x30,0x1C,0x30,0x30,0xE0,0x00,
0x76,0xDC,0x00,0x00,0x00,0x00,0x00,0x00,0x55,0xAA,0x55,0xAA,0x55,0xAA,0x55,0xAA,
};
#define EXPANDED_MEMORY 8
#define MEMSIZE ((EXPANDED_MEMORY+1)*0x10000)
#define GET16(p) (((p)[0]<<8)|(p)[1])
#define PUT16(p,q) ((p)[0]=(q)>>8,(p)[1]=(q))
typedef struct {
Uint8 p;
Uint8 d[256];
} Stack;
typedef struct Device {
Uint8 d[16];
Uint8(*in)(struct Device*,Uint8);
void(*out)(struct Device*,Uint8);
void(*exp)(struct Device*,Uint16);
void*aux;
} Device;
typedef struct {
FILE*file;
DIR*dir;
char name[4096];
struct dirent*de;
Uint8 mode;
} UxnFile;
typedef struct {
Uint32 envtime,phase,freq;
Uint32 length,end,pos;
Uint32 env[4];
Uint8 vol[2];
} UxnAudio;
typedef struct {
Uint8*data;
Uint32 addr;
Uint16 len;
Uint8 kind,stage;
} Script;
static SDL_Color colors[64]={
// 0
{0xFF,0xFF,0xFF,0},
{0x00,0x00,0x00,0},
{0x00,0xFF,0xFF,0},
{0xFF,0x00,0x00,0},
{0xAA,0xAA,0xAA,0},
{0x43,0x43,0x33,0},
{0x43,0xAA,0xAA,0},
{0xAA,0x43,0x33,0},
// 1
{0x00,0x00,0x00,0},
{0xFF,0xFF,0xFF,0},
{0x00,0xFF,0xFF,0},
{0xFF,0x00,0x00,0},
{0x43,0x43,0x33,0},
{0xAA,0xAA,0xAA,0},
{0x43,0xAA,0xAA,0},
{0xAA,0x43,0x33,0},
// 2
{0x10,0x30,0x10,0},
{0x60,0xE0,0x60,0},
{0x60,0x60,0xE0,0},
{0xE0,0x60,0x60,0},
{0x10,0x80,0x10,0},
{0x38,0xFF,0xF8,0},
{0xF8,0x38,0xFF,0},
{0xFF,0xF8,0x38,0},
// 3
{0x55,0x55,0x55,0},
{0xFF,0x55,0x00,0},
{0x00,0x00,0x00,0},
{0xFF,0xFF,0xFF,0},
{0x55,0x55,0x55,0},
{0xFF,0x55,0x00,0},
{0x00,0x00,0x00,0},
{0xFF,0xFF,0xFF,0},
// 4
{0x55,0x55,0x55,0},
{0xFF,0x55,0x00,0},
{0xFF,0xFF,0xFF,0},
{0x00,0x00,0x00,0},
{0x55,0x55,0x55,0},
{0xFF,0x55,0x00,0},
{0xFF,0xFF,0xFF,0},
{0x00,0x00,0x00,0},
// 5
{0xFF,0xFF,0xFF,0},
{0xAA,0xAA,0xAA,0},
{0x55,0x55,0x55,0},
{0x00,0x00,0x00,0},
{0xFF,0x80,0x00,0},
{0xC6,0x60,0x00,0},
{0x94,0x40,0x00,0},
{0x72,0x20,0x00,0},
// 6
{0xAA,0xAA,0xAA,0},
{0x00,0x00,0x00,0},
{0xAA,0xAA,0xAA,0},
{0x00,0x00,0x00,0},
{0x91,0xAA,0x9A,0},
{0x0D,0x0E,0x0E,0},
{0x91,0xAA,0x9A,0},
{0x19,0x15,0x00,0},
// 7
{0xFF,0xFF,0xFF,0},
{0x00,0x00,0x00,0},
{0x77,0xCC,0xAA,0},
{0x99,0x22,0x44,0},
{0xFF,0xFF,0xFF,0},
{0x00,0x00,0x00,0},
{0x77,0xCC,0xAA,0},
{0x99,0x22,0x44,0},
};
static Uint8 blend[4][16]={
{4,0,0,0,1,4,1,1,2,2,4,2,3,3,3,4},
{0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3},
{1,2,3,1,1,2,3,1,1,2,3,1,1,2,3,1},
{2,3,1,2,2,3,1,2,2,3,1,2,2,3,1,2},
};
static const Uint32 notes[12]={
0x20000000,0x21e71f26,0x23eb3588,0x260dfc14,0x285145f3,0x2ab70212,
0x2d413ccd,0x2ff221af,0x32cbfd4a,0x35d13f33,0x39047c0f,0x3c686fce,
};
static Uint8 mem[MEMSIZE];
static Stack ds,rs;
static Device device[16];
static Uint16 scr_w,scr_h;
static Uint8*bglayer;
static Uint8*fglayer;
static UxnFile uxnfile0;
static UxnFile uxnfile1;
static FILE*inf;
static FILE*outf;
static const char*rom_name;
static Uint8 use_console=1;
static Uint8 use_screen=1;
static Uint8 use_mouse=1;
static Uint8 use_debug=0;
static Uint8 use_utc=0;
static Uint8 use_extension=0;
static Uint8 use_thread=0;
static Uint8 hide_cursor=0;
static Uint8 allow_write=0;
static Uint8 joypad_repeat=0;
static Uint16 scroll_size=1;
static Uint8 zoom=1;
static Uint8 disallow_size_change=0;
static Sint16 default_width=512;
static Sint16 default_height=320;
static Uint32 scrflags=SDL_SWSURFACE;
static Uint16 timer_rate=16;
static Uint8 paused=0;
static Uint8 picture_changed=0;
static Uint8 size_changed=0;
static volatile Uint8 timer_expired=0;
static SDL_Surface*screen;
static Uint8 layers;
static Uint8 palet=7;
static Uint8 touch_mode=0;
static SDL_cond*stdin_cond;
static Uint8 screencompat=0x03;
static Uint16 scriptc;
static Script*scriptv;
static FILE*scriptf;
static Sint32*audio_option;
static SDL_AudioSpec audiospec;
static Uint8 joy_mask;
static Uint8 joy_player[4];
static Uint8 joy_hat,joy_hat_mode;
static Uint8 joy_axis_x,joy_axis_y,joy_axis_mode;
static Uint8 joy_button_map[128];
static Uint16 joy_sensitivity=8192;
static Uint8 debugger=0;
static Uint16 breakpoint=0;
static Uint16 break_pc=0x0100;
static Uint8 debug_op=0;
static void debug(int i) {
int j;
if(i) fprintf(stderr,"Debug(%02X)\n",i);
fprintf(stderr,"ds:");
for(i=0;i<ds.p;i++) fprintf(stderr," %02X",ds.d[i]);
fputc('\n',stderr);
fprintf(stderr,"rs:");
for(i=0;i<rs.p;i++) fprintf(stderr," %02X",rs.d[i]);
fputc('\n',stderr);
fprintf(stderr," 0. 1. 2. 3. 4. 5. 6. 7. 8. 9. A. B. C. D. E. F.\n");
for(i=0;i<16;i++) {
fprintf(stderr,"%1X:",i);
for(j=0;j<16;j++) fprintf(stderr," %02X",device[i].d[j]);
if(device[i].aux) fprintf(stderr," *");
fputc('\n',stderr);
}
}
static void uxnerr(int n,int pc) {
if(use_debug) debug(-1);
fflush(outf);
fflush(stderr);
errx(2,"Uxn error %d at 0x%04X",n,pc);
}
#define Push8(z) ({ s->d[s->p++]=(z); })
#define Push16(z) ({ s->d[s->p++]=(z)>>8; s->d[s->p++]=(z); })
#define Push(z) ({ if(v&0x20) s->d[s->p++]=(z)>>8; s->d[s->p++]=(z); })
#define Pop8(z) ({ z=s->d[0xFF&--k]; if(v<0x80) s->p=k; })
#define Pop16(z) ({ z=s->d[0xFF&--k]; z|=s->d[0xFF&--k]<<8; if(v<0x80) s->p=k; })
#define Pop(z) ({ if(v&0x20) Pop16(z); else Pop8(z); })
static void run(Uint16 pc) {
Stack*s;
Uint8 k,v;
Uint16 x,y,z;
if(!pc) return;
start:
while(v=mem[pc++]) {
if(pc==breakpoint && use_screen) goto stop;
s=(v&0x40?&rs:&ds);
k=s->p;
switch(v&0x1F?:v) {
case 0x01: Pop(x); Push(x+1); break;
case 0x02: Pop(x); break;
case 0x03: Pop(x); Pop(y); Push(x); break;
case 0x04: Pop(x); Pop(y); Push(x); Push(y); break;
case 0x05: Pop(z); Pop(y); Pop(x); Push(y); Push(z); Push(x); break;
case 0x06: Pop(x); Push(x); Push(x); break;
case 0x07: Pop(x); Pop(y); Push(y); Push(x); Push(y); break;
case 0x08: Pop(x); Pop(y); Push8(y==x?1:0); break;
case 0x09: Pop(x); Pop(y); Push8(y!=x?1:0); break;
case 0x0A: Pop(x); Pop(y); Push8(y>x?1:0); break;
case 0x0B: Pop(x); Pop(y); Push8(y<x?1:0); break;
case 0x0C: Pop(x); jump: if(v&0x20) pc=x; else pc+=(Sint8)x; break;
case 0x0D: Pop(x); Pop8(y); if(y) goto jump; break;
case 0x0E: Pop(x); y=pc; goto stash;
case 0x0F: Pop(x); goto stash;
case 0x10: Pop8(x); peek: if(v&0x20) x=GET16(mem+x); else x=mem[x]; Push(x); break;
case 0x11: Pop8(x); Pop(y); poke: if(v&0x20) PUT16(mem+x,y); else mem[x]=y; break;
case 0x12: Pop8(x); x=pc+(Sint8)x; goto peek; break;
case 0x13: Pop8(x); Pop(y); x=pc+(Sint8)x; goto poke; break;
case 0x14: Pop16(x); goto peek; break;
case 0x15: Pop16(x); Pop(y); goto poke; break;
case 0x16:
Pop8(x);
z=(x>>4)&15; x&=15;
y=device[z].in(device+z,x);
if(v&0x20) y=(y<<8)|device[z].in(device+z,x+1);
Push(y);
break;
case 0x17:
Pop8(x); Pop(y);
z=(x>>4)&15; x&=15;
if(v&0x20) {
device[z].d[x]=y>>8;
device[z].d[(x+1)&15]=y;
device[z].out(device+z,x);
device[z].out(device+z,x+1);
} else {
device[z].d[x]=y;
device[z].out(device+z,x);
}
break;
case 0x18: Pop(x); Pop(y); Push(y+x); break;
case 0x19: Pop(x); Pop(y); Push(y-x); break;
case 0x1A: Pop(x); Pop(y); Push(y*x); break;
case 0x1B: Pop(x); Pop(y); Push(x?y/x:0); break;
case 0x1C: Pop(x); Pop(y); Push(y&x); break;
case 0x1D: Pop(x); Pop(y); Push(y|x); break;
case 0x1E: Pop(x); Pop(y); Push(y^x); break;
case 0x1F: Pop8(x); Pop(y); z=x>>4; x&=15; Push((y>>x)<<z); break;
case 0x20: Pop8(x); pc+=x?GET16(mem+pc)+2:2; break;
case 0x40: pc+=GET16(mem+pc)+2; break;
case 0x60: Push16(pc+2); pc+=GET16(mem+pc)+2; break;
case 0x80: case 0xC0: Push8(mem[pc++]); break;
case 0xA0: case 0xE0: Push8(mem[pc++]); Push8(mem[pc++]); break;
}
continue;
stash:
s=(v&0x40?&ds:&rs);
if(v&1) {
Push(x);
} else {
Push16(y);
if(v&0x20) pc=x; else pc+=(Sint8)x;
}
}
if(pc==breakpoint && use_screen) {
stop:
paused=debugger=picture_changed=1;
break_pc=pc-1;
}
fflush(outf);
}
static void load_rom(void) {
FILE*fp=fopen(rom_name,"r");
if(!fp) err(1,"Cannot load program file");
memset(mem,0,MEMSIZE);
fread(mem+0x0100,1,MEMSIZE-0x0100,fp);
if(ferror(fp)) err(1,"Cannot load program file");
fclose(fp);
}
static Uint8 default_in(Device*misc1,Uint8 misc2) { return misc1->d[misc2&15]; }
static void default_out(Device*misc1,Uint8 misc2) {}
static void default_exp(Device*misc1,Uint16 misc2) {}
static Uint8 system_in(Device*dev,Uint8 id) {
switch(id) {
case 4: return ds.p;
case 5: return rs.p;
default: return dev->d[id];
}
}
static void system_out(Device*dev,Uint8 id) {
Uint16 x;
switch(id) {
case 3:
x=GET16(dev->d+2);
device[mem[x]>>4].exp(device+(mem[x]>>4),x);
break;
case 4:
ds.p=dev->d[4];
break;
case 5:
rs.p=dev->d[5];
break;
case 8 ... 13:
colors[070].r=colors[074].r=(dev->d[8]>>4)*0x11;
colors[071].r=colors[075].r=(dev->d[8]&15)*0x11;
colors[072].r=colors[076].r=(dev->d[9]>>4)*0x11;
colors[073].r=colors[077].r=(dev->d[9]&15)*0x11;
colors[070].g=colors[074].g=(dev->d[10]>>4)*0x11;
colors[071].g=colors[075].g=(dev->d[10]&15)*0x11;
colors[072].g=colors[076].g=(dev->d[11]>>4)*0x11;
colors[073].g=colors[077].g=(dev->d[11]&15)*0x11;
colors[070].b=colors[074].b=(dev->d[12]>>4)*0x11;
colors[071].b=colors[075].b=(dev->d[12]&15)*0x11;
colors[072].b=colors[076].b=(dev->d[13]>>4)*0x11;
colors[073].b=colors[077].b=(dev->d[13]&15)*0x11;
if(screen && palet==7) {
SDL_SetColors(screen,colors+palet*8,0,8);
picture_changed=1;
}
break;
case 14: if(dev->d[14] && use_debug) debug(dev->d[14]); break;
case 15: if(dev->d[15]) exit(dev->d[15]&0x7F); break;
}
}
// Subprocess support
static char enable_subprocess=0;
static char *fork_args[4] = {"/bin/sh", "-c", "", NULL};
static int child_mode;
static int to_child_fd[2];
static int from_child_fd[2];
static int saved_in;
static int saved_out;
static pid_t child_pid;
/* call after we're sure the process has exited */
static void console_clean_after_child(void) {
child_pid = 0;
if(child_mode & 0x01) {
close(to_child_fd[1]);
dup2(saved_out, 1);
}
if(child_mode & (0x04 | 0x02)) {
close(from_child_fd[0]);
dup2(saved_in, 0);
}
child_mode = 0;
saved_in = -1;
saved_out = -1;
}
static void console_start_fork_pipe(Device*dev) {
pid_t pid;
pid_t parent_pid = getpid();
int addr = GET16(dev->d+12);
fflush(stdout);
if(child_mode & 0x08) {
dev->d[6] = dev->d[5] = 0;
return;
}
if(child_mode & 0x01) {
/* parent writes to child's stdin */
if(pipe(to_child_fd) == -1) {
dev->d[6] = dev->d[5] = 255;
fprintf(stderr, "pipe error: to child\n");
return;
}
}
if(child_mode & (0x04 | 0x02)) {
/* parent reads from child's stdout and/or stderr */
if(pipe(from_child_fd) == -1) {
dev->d[6] = dev->d[5] = 255;
fprintf(stderr, "pipe error: from child\n");
return;
}
}
fork_args[2] = (char *)(mem + GET16(dev->d+12));
pid = fork();
if(pid < 0) { /* failure */
dev->d[6] = dev->d[5] = 255;
fprintf(stderr, "fork failure\n");
} else if(pid == 0) { /* child */
#ifdef __linux__
int r = prctl(PR_SET_PDEATHSIG, SIGTERM);
if(r == -1) {
perror(0);
exit(6);
}
if(getppid() != parent_pid) exit(13);
#endif
if(child_mode & 0x01) {
dup2(to_child_fd[0], 0);
close(to_child_fd[1]);
}
if(child_mode & (0x04 | 0x02)) {
if(child_mode & 0x02) dup2(from_child_fd[1], 1);
if(child_mode & 0x04) dup2(from_child_fd[1], 2);
close(from_child_fd[0]);
}
fflush(stdout);
execvp(fork_args[0], fork_args);
exit(1);
} else { /*parent*/
child_pid = pid;
dev->d[5] = 1;
dev->d[6] = 0;
if(child_mode & 0x01) {
saved_out = dup(1);
dup2(to_child_fd[1], 1);
close(to_child_fd[0]);
}
if(child_mode & (0x04 | 0x02)) {
saved_in = dup(0);
dup2(from_child_fd[0], 0);
close(from_child_fd[1]);
}
}
}
static void console_check_child(Device*dev) {
int wstatus;
if(child_pid) {
if(waitpid(child_pid, &wstatus, WNOHANG)) {
dev->d[5] = 255;
dev->d[6] = WEXITSTATUS(wstatus);
console_clean_after_child();
} else {
dev->d[5] = 1;
dev->d[6] = 0;
}
}
}
static void console_kill_child(Device*dev) {
int wstatus;
if(child_pid) {
kill(child_pid, 9);
if(waitpid(child_pid, &wstatus, WNOHANG)) {
dev->d[5] = 255;
dev->d[6] = WEXITSTATUS(wstatus);
console_clean_after_child();
}
}
}
static void console_start_fork(Device*dev) {
fflush(stderr);
console_kill_child(dev);
child_mode = dev->d[14];
console_start_fork_pipe(dev);
}
static void close_console(Device*dev) {
console_kill_child(dev);
}
static Uint8 subprocess_console_in(Device*dev,Uint8 id) {
switch(id) {
case 5: case 6: console_check_child(dev); break;
}
return dev->d[id];
}
// End of subprocess support
static void console_out(Device*dev,Uint8 id) {
switch(id) {
case 8: fputc(dev->d[8],outf); break;
case 9: fputc(dev->d[9],stderr); break;
// Subprocess
case 15: if(enable_subprocess) console_start_fork(dev); break;
}
}
static Uint16 files_stat(int fd,char*name,Uint16 len,Uint8*out) {
struct stat st;
if(strlen(name)+7>len || fstatat(fd,name,&st,0)) return 0;
if(S_ISDIR(st.st_mode)) return snprintf(out,len,"---- %s\n",name);
else if(st.st_size<0x10000) return snprintf(out,len,"%04x %s\n",(unsigned int)st.st_size,name);
else return snprintf(out,len,"???? %s\n",name);
return 0;
}
static Uint32 files_seek(UxnFile*uf,Uint8 mode,Uint32 len) {
switch(uf->mode) {
case 1: case 2: case 4:
switch(mode) {
case 0: fseek(uf->file,len,SEEK_SET); break;
case 1: fseek(uf->file,(Sint32)len,SEEK_CUR); break;
case 2: fseek(uf->file,(Sint32)len,SEEK_END); break;
case 3: return ftell(uf->file);
}
break;
case 3:
if(!mode && !len) rewinddir(uf->dir);
uf->de=0;
break;
}
return 0;
}
static int files_set(Device*dev,int rw) {
// rw: 0(close) 1(read) 2(write) 3(both)
// return: 1 if file opened, 0 if not opened
Uint8 m=dev->d[7];
UxnFile*uf=dev->aux;
if(uf->file) fclose(uf->file);
if(uf->dir) closedir(uf->dir);
uf->file=0;
uf->dir=0;
uf->de=0;
uf->mode=0;
dev->d[2]=dev->d[3]=0;
if(!uf->name[0]) return 0;
if(rw==1) {
if(uf->dir=opendir(uf->name)) {
uf->mode=3;
return 1;
} else if(uf->file=fopen(uf->name,"r")) {
uf->mode=1;
return 1;
} else {
warn("Cannot open file '%s' for reading",uf->name);
}
} else if(rw==2 && allow_write) {
if(uf->file=fopen(uf->name,m?"a":"w")) {
uf->mode=2;
return 1;
} else {
warn("Cannot open file '%s' for writing",uf->name);
}
} else if(rw==3) {
if(allow_write && (uf->file=fopen(uf->name,"r+"))) {
uf->mode=4;
return 1;
} else if(uf->file=fopen(uf->name,"r")) {
uf->mode=1;
return 1;
} else {
warn("Cannot open file '%s' for reading/writing",uf->name);
}
}
return 0;
}
static void files_out(Device*dev,Uint8 id) {
UxnFile*uf=dev->aux;
Uint16 len=GET16(dev->d+10);
Uint16 addr,x,y;
int i;
switch(id) {
case 5:
addr=GET16(dev->d+4);
dev->d[2]=dev->d[3]=0;
if(!uf->name[0]) break;
y=files_stat(AT_FDCWD,uf->name,len,mem+addr);
if(y<=len) PUT16(dev->d+2,y);
break;
case 7:
if(use_extension) files_set(dev,0);
break;
case 9:
addr=GET16(dev->d+8);
files_set(dev,0);
x=GET16(dev->d+8);
for(i=0;i<4095;i++) {
if(!(uf->name[i]=mem[x+i])) break;
// if(mem[x+i]=='/') uf->name[i]=0;
}
if(use_debug) fprintf(stderr,"! File: %s\n",uf->name);
break;
case 13:
addr=GET16(dev->d+12);
dev->d[2]=dev->d[3]=0;
read:
if(uf->mode==1 || uf->mode==4) {
if(!len) break;
x=fread(mem+addr,1,len,uf->file);
PUT16(dev->d+2,x);
if(ferror(uf->file)) {
warn("Error reading file '%s'",uf->name);
clearerr(uf->file);
}
} else if(uf->mode==3) {
do {
if(uf->de) {
if(x=files_stat(dirfd(uf->dir),uf->de->d_name,len,mem+addr)) {
addr+=x;
len-=x;
} else {
break;
}
}
} while(uf->de=readdir(uf->dir));
PUT16(dev->d+2,addr-GET16(dev->d+12));
} else {
if(files_set(dev,1)) goto read;
}
break;
case 15:
addr=GET16(dev->d+14);
dev->d[2]=dev->d[3]=0;
if(!allow_write) {
disallow:
warnx("Not allowed to write to file '%s'.",uf->name);
break;
}
write:
if(uf->mode==2 || uf->mode==4) {
if(!len) break;
x=fwrite(mem+addr,1,len,uf->file);
PUT16(dev->d+2,x);
if(ferror(uf->file)) {
warn("Error writing file '%s'",uf->name);
clearerr(uf->file);
}
} else {
if(files_set(dev,2)) goto write;
}
break;
}
}
static Uint8 files_in(Device*dev,Uint8 id) {
UxnFile*uf=dev->aux;
int c;
switch(id) {
case 12: case 13:
dev->d[2]=dev->d[3]=0;
read:
if(uf->mode==1 || uf->mode==4) {
c=fgetc(uf->file);
if(c==EOF) {
if(ferror(uf->file)) {
warn("Error reading file '%s'",uf->name);
clearerr(uf->file);
}
c=0;
} else {
dev->d[3]=1;
}
return c;
} else if(uf->mode==3) {
return 0;
} else {
if(files_set(dev,1)) goto read;
}
return 0;
default: return dev->d[id];
}
}
static void files_exp(Device*dev,Uint16 addr) {
UxnFile*uf=dev->aux;
Uint32 x;
if(use_extension) switch(mem[addr]&15) {
case 0 ... 2:
x=GET16(mem+addr+1)<<16;
x|=GET16(mem+addr+3);
if(!uf->mode) files_set(dev,3);
files_seek(uf,mem[addr]&15,x);
break;
case 3:
x=files_seek(uf,3,0);
PUT16(mem+addr+1,x>>16);
PUT16(mem+addr+3,x);
break;
}
}
static Uint8 datetime_in(Device*dev,Uint8 id) {
time_t t=time(0);
struct tm r;
if(use_utc) gmtime_r(&t,&r); else localtime_r(&t,&r);
switch(id) {
case 0: return (r.tm_year+1900)>>8;
case 1: return r.tm_year+1900;
case 2: return r.tm_mon;
case 3: return r.tm_mday;
case 4: return r.tm_hour;
case 5: return r.tm_min;
case 6: return r.tm_sec;
case 7: return r.tm_wday;
case 8: return r.tm_yday>>8;
case 9: return r.tm_yday;
case 10: return r.tm_isdst;
default: return dev->d[id];
}
}
static void set_cursor(void) {
if(hide_cursor) SDL_ShowCursor(paused || !use_mouse);
}
static Uint32 timer_callback(Uint32 interval) {
if(!timer_expired) {
SDL_Event e;
timer_expired=1;
e.type=SDL_USEREVENT;
SDL_PushEvent(&e);
timer_expired=1;
}
return interval;
}
static void set_screen_mode(int w,int h) {
picture_changed=size_changed=0;
free(bglayer);
if(screen) SDL_FreeSurface(screen);
if(w<1) w=1;
if(h<1) h=1;
scr_w=w;
scr_h=h;
screen=SDL_SetVideoMode(w*zoom,h*zoom,8,scrflags);
if(!screen) errx(1,"Cannot set screen mode: %s",SDL_GetError());
if(!bglayer) {
if(timer_rate) SDL_SetTimer(timer_rate,timer_callback);
if(audio_option) {
if(SDL_OpenAudio(&audiospec,0)) errx(1,"Cannot initialize SDL audio: %s",SDL_GetError());
SDL_PauseAudio(0);
}
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);
SDL_WM_SetCaption(rom_name,rom_name);
set_cursor();
SDL_EnableUNICODE(1);
}
SDL_SetColors(screen,colors+palet*8,0,8);
bglayer=calloc(w*h,2);
if(!bglayer) err(1,"Memory allocation error");
fglayer=bglayer+w*h;
}
static void screen_fill(Uint8*p,Uint8 v,Uint16 x0,Uint16 y0,Uint16 x1,Uint16 y1) {
if(y1>scr_h) y1=scr_h;
if(x1>scr_w) x1=scr_w;
if(y0>=y1 || x0>=x1) return;
p+=y0*scr_w+x0;
x1-=x0;
while(y0++<y1) memset(p,v,x1),p+=scr_w;
}
static void draw_sprite(Uint16 x,Uint16 y,Uint16 addr,Uint8 v) {
Uint8 b=v&0x80;
Uint8 u;
Uint16 c;
Uint8*p=v&0x40?fglayer:bglayer;
Uint8*m;
if(x+8>scr_w || y+8>scr_h || addr>0x10000-(b?16:8)) return;
p+=y*scr_w+x;
m=mem+addr;
for(y=0;y<8;y++) {
c=m[y]|(b?m[y+8]<<8:0);
for(x=0;x<8;x++) {
u=(c&1)|((c>>7)&2);
c>>=1;
if((u=blend[u][v&0x0F])!=4) p[(y^(v&0x20?7:0))*scr_w+(x^(v&0x10?0:7))]=u;
}
}
}
static void screen_out(Device*dev,Uint8 id) {
int i,fx,fy;
Uint16 w;
Uint16 x=GET16(dev->d+8);
Uint16 y=GET16(dev->d+10);
Uint8 z=dev->d[id];
if(id<6 && disallow_size_change) {
PUT16(dev->d+2,scr_w);
PUT16(dev->d+4,scr_h);
return;
}
switch(id) {
case 2: case 3:
x=GET16(dev->d+2);
if(x!=scr_w) scr_w=x,size_changed=1;
break;
case 4: case 5:
x=GET16(dev->d+4);
if(x!=scr_h) scr_h=x,size_changed=1;
break;
case 14:
if(size_changed) set_screen_mode(scr_w,scr_h);
picture_changed=1;
if(z<0x80) {
if(x<scr_w && y<scr_h) (z&0x40?fglayer:bglayer)[x+y*scr_w]=z&3;
if(dev->d[6]&0x01) PUT16(dev->d+8,x+1);
if(dev->d[6]&0x02) PUT16(dev->d+10,y+1);
} else {
screen_fill(z&0x40?fglayer:bglayer,z&3,dev->d[14]&0x10?0:x,dev->d[14]&0x20?0:y,dev->d[14]&0x10?x:scr_w,dev->d[14]&0x20?y:scr_h);
}
break;
case 15:
if(size_changed) set_screen_mode(scr_w,scr_h);
picture_changed=1;
w=GET16(dev->d+12);
fx=((z&0x10)&&(screencompat&0x02)?-8:8);
fy=((z&0x20)&&(screencompat&0x02)?-8:8);
for(i=0;i<=(dev->d[6]>>4);i++) {
draw_sprite(x+i*(dev->d[6]&0x02?fx:0),y+i*(dev->d[6]&0x01?fy:0),w,z);
if(dev->d[6]&0x04) w+=z&0x80?16:8;
}
if(dev->d[6]&0x01) PUT16(dev->d+8,x+fx);
if(dev->d[6]&0x02) PUT16(dev->d+10,y+fy);
if(dev->d[6]&0x04) PUT16(dev->d+12,w);
break;
}
}
static void audio_out(Device*dev,Uint8 id) {
UxnAudio*a;
Uint32 s,n;
if(id!=15) return;
if(use_screen) SDL_LockAudio();
a=dev->aux;
a->env[0]=(dev->d[8]>>4)*audio_option['e'-'a'];
a->env[1]=(dev->d[8]&15)*audio_option['e'-'a'];
a->env[2]=(dev->d[9]>>4)*audio_option['e'-'a'];
a->env[3]=(dev->d[9]&15)*audio_option['e'-'a'];
a->length=n=GET16(dev->d+10);
a->pos=s=GET16(dev->d+12);
a->end=s+n;
if(dev->d[15]&0x80) a->length=0;
a->envtime=0;
a->phase=0;
s=(dev->d[15]&0x7F)+audio_option['n'-'a'];
s=notes[s%12]>>(10-s/12);
if(n<256) s=(n*(unsigned long long)s)>>8;
if(use_extension && (dev->d[6] || dev->d[7])) s=(((unsigned long long)s)*(0x10000+GET16(dev->d+6)))>>16;
a->freq=s;
if(audiospec.channels==1) {
a->vol[0]=a->vol[1]=((dev->d[14]&15)+(dev->d[14]>>4))/2;
} else {
a->vol[0]=dev->d[14]>>4;
a->vol[1]=dev->d[14]&15;
}
if(use_screen) SDL_UnlockAudio();
}
static Uint32 envelope(const UxnAudio*a) {
// Returns a number from 0 to 64.
Uint32 t=a->envtime;
if(!(a->env[0]|a->env[1]|a->env[2]|a->env[3])) return 64;
if(t<a->env[0]) return ((64*t)/a->env[0])?:1;
t-=a->env[0];
if(t<a->env[1]) return 64-((32*t)/a->env[1]);
t-=a->env[1];
if(t<a->env[2]) return 32;
t-=a->env[2];
if(t<a->env[3]) return 32-((32*t)/a->env[3]);
return 0;
}
static void audio_callback(void*userdata,Uint8*stream,int len) {
Uint8 c=audiospec.channels;
Uint32 vol=audio_option['m'-'a']?0:audio_option['v'-'a'];
Sint16*buf=(void*)stream;
UxnAudio*a;
Uint32 o0,o1,d,n,e;
len>>=1;
for(n=0;n<len;n+=c) {
o0=o1=0;
for(d=3;d<7;d++) {
a=device[d].aux;
if(a->pos>=a->end && !a->length) continue;
e=envelope(a);
if(!e) {
a->pos=a->length=a->end=0;
continue;
}
a->envtime++;
e*=(Sint8)(mem[a->pos]^0x80);
a->phase+=a->freq;
if(a->phase>=0x1000000) {
a->pos+=a->phase>>24;
a->phase&=0xFFFFFF;
while(a->pos>=a->end && a->length) a->pos-=a->length;
}
o0+=e*a->vol[0];