forked from daitangio/x16-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo.c
1136 lines (1010 loc) · 29.2 KB
/
video.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
// Commander X16 Emulator
// Copyright (c) 2019 Michael Steil
// All rights reserved. License: 2-clause BSD
#include "video.h"
#include "memory.h"
#include "ps2.h"
#include "glue.h"
#include "debugger.h"
#include "gif.h"
#include "vera_spi.h"
#ifdef __EMSCRIPTEN__
#include "emscripten.h"
#endif
#define ADDR_VRAM_START 0x00000
#define ADDR_VRAM_END 0x20000
#define ADDR_COMPOSER_START 0xF0000
#define ADDR_COMPOSER_END 0xF1000
#define ADDR_PALETTE_START 0xF1000
#define ADDR_PALETTE_END 0xF2000
#define ADDR_LAYER1_START 0xF2000
#define ADDR_LAYER1_END 0xF3000
#define ADDR_LAYER2_START 0xF3000
#define ADDR_LAYER2_END 0xF4000
#define ADDR_SPRITES_START 0xF4000
#define ADDR_SPRITES_END 0xF5000
#define ADDR_SPRDATA_START 0xF5000
#define ADDR_SPRDATA_END 0xF6000
#define ADDR_SPI_START 0xF7000
#define ADDR_SPI_END 0xF8000
#define ESC_IS_BREAK /* if enabled, Esc sends Break/Pause key instead of Esc */
#define NUM_SPRITES 128
// both VGA and NTSC
#define SCAN_WIDTH 800
#define SCAN_HEIGHT 525
// VGA
#define VGA_FRONT_PORCH_X 16
#define VGA_FRONT_PORCH_Y 10
#define VGA_PIXEL_FREQ 25.175
// NTSC: 262.5 lines per frame, lower field first
#define NTSC_FRONT_PORCH_X 80
#define NTSC_FRONT_PORCH_Y 22
#define NTSC_PIXEL_FREQ (15.750 * 800 / 1000)
#define TITLE_SAFE_X 0.067
#define TITLE_SAFE_Y 0.05
// visible area we're darwing
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define SCREEN_RAM_OFFSET 0x00000
#ifdef __APPLE__
#define LSHORTCUT_KEY SDL_SCANCODE_LGUI
#define RSHORTCUT_KEY SDL_SCANCODE_RGUI
#else
#define LSHORTCUT_KEY SDL_SCANCODE_LCTRL
#define RSHORTCUT_KEY SDL_SCANCODE_RCTRL
#endif
static SDL_Window *window;
static SDL_Renderer *renderer;
static SDL_Texture *sdlTexture;
static bool is_fullscreen = false;
static uint8_t video_ram[0x20000];
static uint8_t palette[256 * 2];
static uint8_t sprite_data[256][8];
// I/O registers
static uint32_t io_addr[2];
static uint8_t io_inc[2];
bool io_addrsel;
static uint8_t ien = 0;
static uint8_t isr = 0;
static uint8_t reg_layer[2][16];
static uint8_t reg_sprites[16];
static uint8_t reg_composer[32];
static uint8_t layer_line[2][SCREEN_WIDTH];
static uint8_t sprite_line_col[SCREEN_WIDTH];
static uint8_t sprite_line_z[SCREEN_WIDTH];
static bool layer_line_empty[2];
static bool sprite_line_empty;
float scan_pos_x;
uint16_t scan_pos_y;
static uint8_t framebuffer[SCREEN_WIDTH * SCREEN_HEIGHT * 4];
static GifWriter gif_writer;
static const uint16_t default_palette[] = {
0x000,0xfff,0x800,0xafe,0xc4c,0x0c5,0x00a,0xee7,0xd85,0x640,0xf77,0x333,0x777,0xaf6,0x08f,0xbbb,0x000,0x111,0x222,0x333,0x444,0x555,0x666,0x777,0x888,0x999,0xaaa,0xbbb,0xccc,0xddd,0xeee,0xfff,0x211,0x433,0x644,0x866,0xa88,0xc99,0xfbb,0x211,0x422,0x633,0x844,0xa55,0xc66,0xf77,0x200,0x411,0x611,0x822,0xa22,0xc33,0xf33,0x200,0x400,0x600,0x800,0xa00,0xc00,0xf00,0x221,0x443,0x664,0x886,0xaa8,0xcc9,0xfeb,0x211,0x432,0x653,0x874,0xa95,0xcb6,0xfd7,0x210,0x431,0x651,0x862,0xa82,0xca3,0xfc3,0x210,0x430,0x640,0x860,0xa80,0xc90,0xfb0,0x121,0x343,0x564,0x786,0x9a8,0xbc9,0xdfb,0x121,0x342,0x463,0x684,0x8a5,0x9c6,0xbf7,0x120,0x241,0x461,0x582,0x6a2,0x8c3,0x9f3,0x120,0x240,0x360,0x480,0x5a0,0x6c0,0x7f0,0x121,0x343,0x465,0x686,0x8a8,0x9ca,0xbfc,0x121,0x242,0x364,0x485,0x5a6,0x6c8,0x7f9,0x020,0x141,0x162,0x283,0x2a4,0x3c5,0x3f6,0x020,0x041,0x061,0x082,0x0a2,0x0c3,0x0f3,0x122,0x344,0x466,0x688,0x8aa,0x9cc,0xbff,0x122,0x244,0x366,0x488,0x5aa,0x6cc,0x7ff,0x022,0x144,0x166,0x288,0x2aa,0x3cc,0x3ff,0x022,0x044,0x066,0x088,0x0aa,0x0cc,0x0ff,0x112,0x334,0x456,0x668,0x88a,0x9ac,0xbcf,0x112,0x224,0x346,0x458,0x56a,0x68c,0x79f,0x002,0x114,0x126,0x238,0x24a,0x35c,0x36f,0x002,0x014,0x016,0x028,0x02a,0x03c,0x03f,0x112,0x334,0x546,0x768,0x98a,0xb9c,0xdbf,0x112,0x324,0x436,0x648,0x85a,0x96c,0xb7f,0x102,0x214,0x416,0x528,0x62a,0x83c,0x93f,0x102,0x204,0x306,0x408,0x50a,0x60c,0x70f,0x212,0x434,0x646,0x868,0xa8a,0xc9c,0xfbe,0x211,0x423,0x635,0x847,0xa59,0xc6b,0xf7d,0x201,0x413,0x615,0x826,0xa28,0xc3a,0xf3c,0x201,0x403,0x604,0x806,0xa08,0xc09,0xf0b
};
static uint8_t video_space_read(uint32_t address);
void
video_reset()
{
// init I/O registers
memset(io_addr, 0, sizeof(io_addr));
memset(io_inc, 0, sizeof(io_inc));
io_addrsel = 0;
// init Layer registers
memset(reg_layer, 0, sizeof(reg_layer));
// init sprite registers
memset(reg_sprites, 0, sizeof(reg_sprites));
// init composer registers
memset(reg_composer, 0, sizeof(reg_composer));
reg_composer[1] = 128; // hscale = 1.0
reg_composer[2] = 128; // vscale = 1.0
// init sprite data
memset(sprite_data, 0, sizeof(sprite_data));
// copy palette
memcpy(palette, default_palette, sizeof(palette));
for (int i = 0; i < 256; i++) {
palette[i * 2 + 0] = default_palette[i] & 0xff;
palette[i * 2 + 1] = default_palette[i] >> 8;
}
scan_pos_x = 0;
scan_pos_y = 0;
}
bool
video_init(int window_scale, char *quality)
{
video_reset();
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, quality);
SDL_CreateWindowAndRenderer(SCREEN_WIDTH * window_scale, SCREEN_HEIGHT * window_scale, 0, &window, &renderer);
#ifndef __MORPHOS__
SDL_SetWindowResizable(window, true);
#endif
SDL_RenderSetLogicalSize(renderer, SCREEN_WIDTH, SCREEN_HEIGHT);
sdlTexture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_RGB888,
SDL_TEXTUREACCESS_STREAMING,
SCREEN_WIDTH, SCREEN_HEIGHT);
SDL_SetWindowTitle(window, "Commander X16");
if (record_gif) {
record_gif = GifBegin(&gif_writer, gif_path, SCREEN_WIDTH, SCREEN_HEIGHT, 1, 8, false);
}
return true;
}
#define EXTENDED_FLAG 0x100
int
ps2_scancode_from_SDLKey(SDL_Scancode k)
{
switch (k) {
case SDL_SCANCODE_GRAVE:
return 0x0e;
case SDL_SCANCODE_BACKSPACE:
return 0x66;
case SDL_SCANCODE_TAB:
return 0xd;
case SDL_SCANCODE_CLEAR:
return 0;
case SDL_SCANCODE_RETURN:
return 0x5a;
case SDL_SCANCODE_PAUSE:
return 0;
case SDL_SCANCODE_ESCAPE:
#ifdef ESC_IS_BREAK
return 0xff;
#else
return 0x76;
#endif
case SDL_SCANCODE_SPACE:
return 0x29;
case SDL_SCANCODE_APOSTROPHE:
return 0x52;
case SDL_SCANCODE_COMMA:
return 0x41;
case SDL_SCANCODE_MINUS:
return 0x4e;
case SDL_SCANCODE_PERIOD:
return 0x49;
case SDL_SCANCODE_SLASH:
return 0x4a;
case SDL_SCANCODE_0:
return 0x45;
case SDL_SCANCODE_1:
return 0x16;
case SDL_SCANCODE_2:
return 0x1e;
case SDL_SCANCODE_3:
return 0x26;
case SDL_SCANCODE_4:
return 0x25;
case SDL_SCANCODE_5:
return 0x2e;
case SDL_SCANCODE_6:
return 0x36;
case SDL_SCANCODE_7:
return 0x3d;
case SDL_SCANCODE_8:
return 0x3e;
case SDL_SCANCODE_9:
return 0x46;
case SDL_SCANCODE_SEMICOLON:
return 0x4c;
case SDL_SCANCODE_EQUALS:
return 0x55;
case SDL_SCANCODE_LEFTBRACKET:
return 0x54;
case SDL_SCANCODE_BACKSLASH:
return 0x5d;
case SDL_SCANCODE_RIGHTBRACKET:
return 0x5b;
case SDL_SCANCODE_A:
return 0x1c;
case SDL_SCANCODE_B:
return 0x32;
case SDL_SCANCODE_C:
return 0x21;
case SDL_SCANCODE_D:
return 0x23;
case SDL_SCANCODE_E:
return 0x24;
case SDL_SCANCODE_F:
return 0x2b;
case SDL_SCANCODE_G:
return 0x34;
case SDL_SCANCODE_H:
return 0x33;
case SDL_SCANCODE_I:
return 0x43;
case SDL_SCANCODE_J:
return 0x3B;
case SDL_SCANCODE_K:
return 0x42;
case SDL_SCANCODE_L:
return 0x4B;
case SDL_SCANCODE_M:
return 0x3A;
case SDL_SCANCODE_N:
return 0x31;
case SDL_SCANCODE_O:
return 0x44;
case SDL_SCANCODE_P:
return 0x4D;
case SDL_SCANCODE_Q:
return 0x15;
case SDL_SCANCODE_R:
return 0x2D;
case SDL_SCANCODE_S:
return 0x1B;
case SDL_SCANCODE_T:
return 0x2C;
case SDL_SCANCODE_U:
return 0x3C;
case SDL_SCANCODE_V:
return 0x2A;
case SDL_SCANCODE_W:
return 0x1D;
case SDL_SCANCODE_X:
return 0x22;
case SDL_SCANCODE_Y:
return 0x35;
case SDL_SCANCODE_Z:
return 0x1A;
case SDL_SCANCODE_DELETE:
return 0;
case SDL_SCANCODE_UP:
return 0x75 | EXTENDED_FLAG;
case SDL_SCANCODE_DOWN:
return 0x72 | EXTENDED_FLAG;
case SDL_SCANCODE_RIGHT:
return 0x74 | EXTENDED_FLAG;
case SDL_SCANCODE_LEFT:
return 0x6b | EXTENDED_FLAG;
case SDL_SCANCODE_INSERT:
return 0;
case SDL_SCANCODE_HOME:
return 0x6c | EXTENDED_FLAG;
case SDL_SCANCODE_END:
return 0;
case SDL_SCANCODE_PAGEUP:
return 0;
case SDL_SCANCODE_PAGEDOWN:
return 0;
case SDL_SCANCODE_F1:
return 0x05;
case SDL_SCANCODE_F2:
return 0x06;
case SDL_SCANCODE_F3:
return 0x04;
case SDL_SCANCODE_F4:
return 0x0c;
case SDL_SCANCODE_F5:
return 0x03;
case SDL_SCANCODE_F6:
return 0x0b;
case SDL_SCANCODE_F7:
return 0x83;
case SDL_SCANCODE_F8:
return 0x0a;
case SDL_SCANCODE_F9:
return 0x01;
case SDL_SCANCODE_F10:
return 0x09;
case SDL_SCANCODE_F11:
return 0x78;
case SDL_SCANCODE_F12:
return 0x07;
case SDL_SCANCODE_RSHIFT:
return 0x59;
case SDL_SCANCODE_LSHIFT:
return 0x12;
case SDL_SCANCODE_LCTRL:
return 0x14;
case SDL_SCANCODE_RCTRL:
return 0x14 | EXTENDED_FLAG;
case SDL_SCANCODE_LALT:
return 0x11;
case SDL_SCANCODE_RALT:
return 0x11 | EXTENDED_FLAG;
// case SDL_SCANCODE_LGUI: // Windows/Command
// return 0x5b | EXTENDED_FLAG;
case SDL_SCANCODE_NONUSBACKSLASH:
return 0x61;
case SDL_SCANCODE_KP_ENTER:
return 0x5a | EXTENDED_FLAG;
case SDL_SCANCODE_KP_0:
return 0x70;
case SDL_SCANCODE_KP_1:
return 0x69;
case SDL_SCANCODE_KP_2:
return 0x72;
case SDL_SCANCODE_KP_3:
return 0x7a;
case SDL_SCANCODE_KP_4:
return 0x6b;
case SDL_SCANCODE_KP_5:
return 0x73;
case SDL_SCANCODE_KP_6:
return 0x74;
case SDL_SCANCODE_KP_7:
return 0x6c;
case SDL_SCANCODE_KP_8:
return 0x75;
case SDL_SCANCODE_KP_9:
return 0x7d;
case SDL_SCANCODE_KP_PERIOD:
return 0x71;
case SDL_SCANCODE_KP_PLUS:
return 0x79;
case SDL_SCANCODE_KP_MINUS:
return 0x7b;
case SDL_SCANCODE_KP_MULTIPLY:
return 0x7c;
case SDL_SCANCODE_KP_DIVIDE:
return 0x4a | EXTENDED_FLAG;
default:
return 0;
}
}
struct video_layer_properties
{
bool enabled;
uint8_t mode;
uint32_t map_base;
uint32_t tile_base;
bool text_mode;
bool tile_mode;
bool bitmap_mode;
uint16_t hscroll;
uint16_t vscroll;
uint16_t mapw;
uint16_t maph;
uint16_t tilew;
uint16_t tileh;
uint16_t mapw_max;
uint16_t maph_max;
uint16_t tilew_max;
uint16_t tileh_max;
uint16_t layerw;
uint16_t layerh;
uint16_t layerw_max;
uint16_t layerh_max;
uint8_t bits_per_pixel;
uint32_t tile_size;
};
struct video_layer_properties layer_properties[2];
void refresh_layer_properties(uint8_t layer)
{
struct video_layer_properties* props = &layer_properties[layer];
props->enabled = reg_layer[layer][0] & 1;
props->mode = reg_layer[layer][0] >> 5;
props->map_base = reg_layer[layer][2] << 2 | reg_layer[layer][3] << 10;
props->tile_base = reg_layer[layer][4] << 2 | reg_layer[layer][5] << 10;
props->text_mode = (props->mode == 0) || (props->mode == 1);
props->tile_mode = (props->mode == 2) || (props->mode == 3) || (props->mode == 4);
props->bitmap_mode = (props->mode == 5) || (props->mode == 6) || (props->mode == 7);
if (!props->bitmap_mode) {
props->hscroll = reg_layer[layer][6] | (reg_layer[layer][7] & 0xf) << 8;
props->vscroll = reg_layer[layer][8] | (reg_layer[layer][9] & 0xf) << 8;
}
props->mapw = 0;
props->maph = 0;
props->tilew = 0;
props->tileh = 0;
if (props->tile_mode || props->text_mode) {
props->mapw = 1 << ((reg_layer[layer][1] & 3) + 5);
props->maph = 1 << (((reg_layer[layer][1] >> 2) & 3) + 5);
if (props->tile_mode) {
props->tilew = 1 << (((reg_layer[layer][1] >> 4) & 1) + 3);
props->tileh = 1 << (((reg_layer[layer][1] >> 5) & 1) + 3);
} else {
props->tilew = 8;
props->tileh = 8;
}
} else if (props->bitmap_mode) {
// bitmap mode is basically tiled mode with a single huge tile
props->tilew = ((reg_layer[layer][1] >> 4) & 1) ? 640 : 320;
props->tileh = SCREEN_HEIGHT;
}
// We know mapw, maph, tilew, and tileh are powers of two, and any products of that set will be powers of two,
// so there's no need to modulo against them if we have bitmasks we can bitwise-and against.
props->mapw_max = props->mapw - 1;
props->maph_max = props->maph - 1;
props->tilew_max = props->tilew - 1;
props->tileh_max = props->tileh - 1;
props->layerw = props->mapw * props->tilew;
props->layerh = props->maph * props->tileh;
props->layerw_max = props->layerw - 1;
props->layerh_max = props->layerh - 1;
props->bits_per_pixel = 0;
if ((props->mode == 0) || (props->mode == 1)) {
props->bits_per_pixel = 1;
} else if ((props->mode == 2) || (props->mode == 5)) {
props->bits_per_pixel = 2;
} else if ((props->mode == 3) || (props->mode == 6)) {
props->bits_per_pixel = 4;
} else if ((props->mode == 4) || (props->mode == 7)) {
props->bits_per_pixel = 8;
}
props->tile_size = (props->tilew * props->bits_per_pixel * props->tileh) >> 3;
}
struct video_sprite_properties
{
int8_t sprite_zdepth;
int16_t sprite_x;
int16_t sprite_y;
uint8_t sprite_width;
uint8_t sprite_height;
bool hflip;
bool vflip;
bool mode;
uint32_t sprite_address;
uint16_t palette_offset;
};
struct video_sprite_properties sprite_properties[256];
void refresh_sprite_properties(uint16_t sprite)
{
struct video_sprite_properties* props = &sprite_properties[sprite];
props->sprite_zdepth = (sprite_data[sprite][6] >> 2) & 3;
props->sprite_x = sprite_data[sprite][2] | (sprite_data[sprite][3] & 3) << 8;
props->sprite_y = sprite_data[sprite][4] | (sprite_data[sprite][5] & 3) << 8;
props->sprite_width = 1 << (((sprite_data[sprite][7] >> 4) & 3) + 3);
props->sprite_height = 1 << ((sprite_data[sprite][7] >> 6) + 3);
// fix up negative coordinates
if (props->sprite_x >= 0x400 - props->sprite_width) {
props->sprite_x |= 0xff00 - 0x200;
}
if (props->sprite_y >= 0x200 - props->sprite_height) {
props->sprite_y |= 0xff00 - 0x100;
}
props->hflip = sprite_data[sprite][6] & 1;
props->vflip = (sprite_data[sprite][6] >> 1) & 1;
props->mode = (sprite_data[sprite][1] >> 7) & 1;
props->sprite_address = sprite_data[sprite][0] << 5 | (sprite_data[sprite][1] & 0xf) << 13;
props->palette_offset = (sprite_data[sprite][7] & 0x0f) << 4;
}
static void
render_sprite_line(uint16_t y)
{
if (!(reg_sprites[0] & 1)) {
// sprites disabled
sprite_line_empty = true;
return;
}
sprite_line_empty = false;
for (int i = 0; i < SCREEN_WIDTH; i++) {
sprite_line_col[i] = 0;
sprite_line_z[i] = 0;
}
uint16_t sprite_budget = 800 + 1;
for (int i = 0; i < NUM_SPRITES; i++) {
// one clock per lookup
sprite_budget--; if (sprite_budget == 0) break;
struct video_sprite_properties *props = &sprite_properties[i];
if (props->sprite_zdepth == 0) {
continue;
}
// check whether this line falls within the sprite
if (y < props->sprite_y || y >= props->sprite_y + props->sprite_height) {
continue;
}
for (uint16_t sx = 0; sx < props->sprite_width; sx++) {
uint16_t line_x = props->sprite_x + sx;
if (line_x >= SCREEN_WIDTH) {
continue;
}
uint16_t eff_sx = sx;
uint16_t eff_sy = y - props->sprite_y;
// flip
if (props->hflip) {
eff_sx = (props->sprite_width - 1) - eff_sx;
}
if (props->vflip) {
eff_sy = (props->sprite_height - 1) - eff_sy;
}
uint8_t col_index = 0;
uint32_t vaddr;
if (props->mode == 0) {
// 4 bpp
vaddr = props->sprite_address + (eff_sy * props->sprite_width >> 1) + (eff_sx >> 1);
uint8_t byte = video_ram[vaddr];
if (eff_sx & 1) {
col_index = byte & 0xf;
} else {
col_index = byte >> 4;
}
} else {
// 8 bpp
vaddr = props->sprite_address + eff_sy * props->sprite_width + eff_sx;
col_index = video_ram[vaddr];
}
// one clock per fetched 32 bits
if (!(vaddr & 3)) {
sprite_budget--; if (sprite_budget == 0) break;
}
// one clock per rendered pixel
sprite_budget--; if (sprite_budget == 0) break;
// palette offset
if (col_index > 0) {
col_index += props->palette_offset;
if (props->sprite_zdepth > sprite_line_z[line_x]) {
sprite_line_col[line_x] = col_index;
sprite_line_z[line_x] = props->sprite_zdepth;
}
}
}
}
}
static void
render_layer_line(uint8_t layer, uint16_t y)
{
struct video_layer_properties *props = &layer_properties[layer];
if (!props->enabled) {
layer_line_empty[layer] = true;
} else {
layer_line_empty[layer] = false;
for (int x = 0; x < SCREEN_WIDTH; x++) {
uint8_t col_index = 0;
int xx, yy;
int eff_x = x;
int eff_y = y;
// Scrolling
if (!props->bitmap_mode) {
eff_x = (x + props->hscroll) & (props->layerw_max);
eff_y = (y + props->vscroll) & (props->layerh_max);
}
if (props->bitmap_mode) {
xx = eff_x % props->tilew;
yy = eff_y % props->tileh;
} else {
xx = eff_x & props->tilew_max;
yy = eff_y & props->tileh_max;
}
uint16_t tile_index = 0;
uint8_t fg_color = 0;
uint8_t bg_color = 0;
uint8_t palette_offset = 0;
// extract all information from the map
if (props->bitmap_mode) {
tile_index = 0;
palette_offset = reg_layer[layer][7] & 0xf;
} else {
uint32_t map_addr = props->map_base + (eff_y / props->tileh * props->mapw + eff_x / props->tilew) * 2;
uint8_t byte0 = video_space_read(map_addr);
uint8_t byte1 = video_space_read(map_addr + 1);
if (props->text_mode) {
tile_index = byte0;
if (props->mode == 0) {
fg_color = byte1 & 15;
bg_color = byte1 >> 4;
} else {
fg_color = byte1;
bg_color = 0;
}
} else if (props->tile_mode) {
tile_index = byte0 | ((byte1 & 3) << 8);
// Tile Flipping
bool vflip = (byte1 >> 3) & 1;
bool hflip = (byte1 >> 2) & 1;
if (vflip) {
yy = yy ^ (props->tileh - 1);
}
if (hflip) {
xx = xx ^ (props->tilew - 1);
}
palette_offset = byte1 >> 4;
}
}
// offset within tilemap of the current tile
uint32_t tile_start = tile_index * props->tile_size;
// additional bytes to reach the correct line of the tile
uint32_t y_add = (yy * props->tilew * props->bits_per_pixel) >> 3;
// additional bytes to reach the correct column of the tile
uint16_t x_add = (xx * props->bits_per_pixel) >> 3;
uint32_t tile_offset = tile_start + y_add + x_add;
uint8_t s = video_space_read(props->tile_base + tile_offset);
// convert tile byte to indexed color
if (props->bits_per_pixel == 1) {
bool bit = (s >> (7 - xx)) & 1;
col_index = bit ? fg_color : bg_color;
} else if (props->bits_per_pixel == 2) {
col_index = (s >> (6 - ((xx & 3) << 1))) & 3;
} else if (props->bits_per_pixel == 4) {
col_index = (s >> (4 - ((xx & 1) << 2))) & 0xf;
} else if (props->bits_per_pixel == 8) {
col_index = s;
}
// Apply Palette Offset
if (palette_offset && col_index > 0 && col_index < 16) {
col_index += palette_offset << 4;
}
layer_line[layer][x] = col_index;
}
}
}
static void
render_line(uint16_t y)
{
uint8_t out_mode = reg_composer[0] & 3;
bool chroma_disable = (reg_composer[0] >> 2) & 1;
float hscale = 128.0 / reg_composer[1];
float vscale = 128.0 / reg_composer[2];
uint8_t border_color = reg_composer[3];
uint16_t hstart = reg_composer[4] | (reg_composer[8] & 3) << 8;
uint16_t hstop = reg_composer[5] | ((reg_composer[8] >> 2) & 3) << 8;
uint16_t vstart = reg_composer[6] | ((reg_composer[8] >> 4) & 1) << 8;
uint16_t vstop = reg_composer[7] | ((reg_composer[8] >> 5) & 1) << 8;
int eff_y = 1.0 / vscale * (y - vstart);
render_sprite_line(eff_y);
render_layer_line(0, eff_y);
render_layer_line(1, eff_y);
for (uint16_t x = 0; x < SCREEN_WIDTH; x++) {
uint8_t r;
uint8_t g;
uint8_t b;
if (out_mode == 0) {
// video generation off
// -> show blue screen
r = 0;
g = 0;
b = 255;
} else {
uint8_t col_index = 0;
if (x < hstart || x > hstop || y < vstart || y > vstop) {
col_index = border_color;
} else {
int eff_x = 1.0 / hscale * (x - hstart);
uint8_t spr_col_index = sprite_line_empty ? 0 : sprite_line_col[eff_x];
uint8_t spr_zindex = sprite_line_z[eff_x];
uint8_t l1_col_index = layer_line_empty[0] ? 0 : layer_line[0][eff_x];
uint8_t l2_col_index = layer_line_empty[1] ? 0 : layer_line[1][eff_x];
switch (spr_zindex) {
case 3:
col_index = spr_col_index ?: l2_col_index ?: l1_col_index;
break;
case 2:
col_index = l2_col_index ?: spr_col_index ?: l1_col_index;
break;
case 1:
col_index = l2_col_index ?: l1_col_index ?: spr_col_index;
break;
case 0:
col_index = l2_col_index ?: l1_col_index;
break;
}
}
// palette lookup
uint16_t entry = palette[col_index * 2] | palette[col_index * 2 + 1] << 8;
r = ((entry >> 8) & 0xf) << 4 | ((entry >> 8) & 0xf);
g = ((entry >> 4) & 0xf) << 4 | ((entry >> 4) & 0xf);
b = (entry & 0xf) << 4 | (entry & 0xf);
if (chroma_disable) {
r = g = b = (r + b + g) / 3;
}
}
// NTSC overscan
if (out_mode == 2) {
if (x < SCREEN_WIDTH * TITLE_SAFE_X ||
x > SCREEN_WIDTH * (1 - TITLE_SAFE_X) ||
y < SCREEN_HEIGHT * TITLE_SAFE_Y ||
y > SCREEN_HEIGHT * (1 - TITLE_SAFE_Y)) {
#if 1
r /= 3;
g /= 3;
b /= 3;
#else
r ^= 128;
g ^= 128;
b ^= 128;
#endif
}
}
int fbi = (y * SCREEN_WIDTH + x) * 4;
framebuffer[fbi + 0] = b;
framebuffer[fbi + 1] = g;
framebuffer[fbi + 2] = r;
}
}
bool
video_step(float mhz)
{
uint8_t out_mode = reg_composer[0] & 3;
bool new_frame = false;
float advance = ((out_mode & 2) ? NTSC_PIXEL_FREQ : VGA_PIXEL_FREQ) / mhz;
scan_pos_x += advance;
if (scan_pos_x > SCAN_WIDTH) {
scan_pos_x -= SCAN_WIDTH;
uint16_t front_porch = (out_mode & 2) ? NTSC_FRONT_PORCH_Y : VGA_FRONT_PORCH_Y;
uint16_t y = scan_pos_y - front_porch;
if (y < SCREEN_HEIGHT) {
render_line(y);
}
scan_pos_y++;
if (scan_pos_y == SCAN_HEIGHT) {
scan_pos_y = 0;
new_frame = true;
if (ien & 1) { // VSYNC IRQ
isr |= 1;
}
}
if (ien & 2) { // LINE IRQ
y = scan_pos_y - front_porch;
uint16_t irq_line = reg_composer[9] | (reg_composer[10] & 1) << 8;
if (y < SCREEN_HEIGHT && y == irq_line) {
isr |= 2;
}
}
}
return new_frame;
}
bool
video_get_irq_out()
{
return isr > 0;
}
//
// saves the video memory and register content into a file
//
void
video_save(FILE *f)
{
fwrite(&video_ram[0], sizeof(uint8_t), sizeof(video_ram), f);
fwrite(®_composer[0], sizeof(uint8_t), sizeof(reg_composer), f);
fwrite(&palette[0], sizeof(uint8_t), sizeof(palette), f);
fwrite(®_layer[0][0], sizeof(uint8_t), sizeof(reg_layer), f);
fwrite(®_sprites[0], sizeof(uint8_t), sizeof(reg_sprites), f);
fwrite(&sprite_data[0], sizeof(uint8_t), sizeof(sprite_data), f);
}
bool
video_update()
{
SDL_UpdateTexture(sdlTexture, NULL, framebuffer, SCREEN_WIDTH * 4);
if (record_gif) {
record_gif = GifWriteFrame(&gif_writer, framebuffer, SCREEN_WIDTH, SCREEN_HEIGHT, 2, 8, false);
if (!record_gif) {
GifEnd(&gif_writer);
}
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, sdlTexture, NULL, NULL);
if (debuger_enabled && showDebugOnRender != 0) {
DEBUGRenderDisplay(SCREEN_WIDTH, SCREEN_HEIGHT, renderer);
SDL_RenderPresent(renderer);
return true;
}
SDL_RenderPresent(renderer);
static bool cmd_down = false;
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
return false;
}
if (event.type == SDL_KEYDOWN) {
bool consumed = false;
if (cmd_down) {
if (event.key.keysym.sym == SDLK_s) {
machine_dump();
consumed = true;
} else if (event.key.keysym.sym == SDLK_r) {
machine_reset();
consumed = true;
} else if (event.key.keysym.sym == SDLK_v) {
machine_paste(SDL_GetClipboardText());
consumed = true;
} else if (event.key.keysym.sym == SDLK_f || event.key.keysym.sym == SDLK_RETURN) {
is_fullscreen = !is_fullscreen;
SDL_SetWindowFullscreen(window, is_fullscreen ? SDL_WINDOW_FULLSCREEN : 0);
consumed = true;
}
}
if (!consumed) {
if (log_keyboard) {
printf("DOWN 0x%02X\n", event.key.keysym.scancode);
fflush(stdout);
}
if (event.key.keysym.scancode == LSHORTCUT_KEY || event.key.keysym.scancode == RSHORTCUT_KEY) {
cmd_down = true;
}
int scancode = ps2_scancode_from_SDLKey(event.key.keysym.scancode);
if (scancode == 0xff) {
// "Pause/Break" sequence
kbd_buffer_add(0xe1);
kbd_buffer_add(0x14);
kbd_buffer_add(0x77);
kbd_buffer_add(0xe1);
kbd_buffer_add(0xf0);
kbd_buffer_add(0x14);
kbd_buffer_add(0xf0);
kbd_buffer_add(0x77);
} else {
if (scancode & EXTENDED_FLAG) {
kbd_buffer_add(0xe0);
}
kbd_buffer_add(scancode & 0xff);
}
}
return true;
}
if (event.type == SDL_KEYUP) {
if (log_keyboard) {
printf("UP 0x%02X\n", event.key.keysym.scancode);
fflush(stdout);
}
if (event.key.keysym.scancode == LSHORTCUT_KEY || event.key.keysym.scancode == RSHORTCUT_KEY) {
cmd_down = false;
}
int scancode = ps2_scancode_from_SDLKey(event.key.keysym.scancode);
if (scancode & EXTENDED_FLAG) {
kbd_buffer_add(0xe0);
}
kbd_buffer_add(0xf0); // BREAK
kbd_buffer_add(scancode & 0xff);
return true;
}
if (event.type == SDL_MOUSEBUTTONDOWN) {
switch (event.button.button) {
case SDL_BUTTON_LEFT:
mouse_button_down(0);
break;
case SDL_BUTTON_RIGHT:
mouse_button_down(1);
break;
}
}
if (event.type == SDL_MOUSEBUTTONUP) {
switch (event.button.button) {
case SDL_BUTTON_LEFT:
mouse_button_up(0);
break;
case SDL_BUTTON_RIGHT:
mouse_button_up(1);
break;
}
}
if (event.type == SDL_MOUSEMOTION) {
mouse_move(event.motion.x, event.motion.y);
}
}
return true;
}
void
video_end()
{
if (record_gif) {
GifEnd(&gif_writer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
}
uint32_t
get_and_inc_address(uint8_t sel)
{
uint32_t address = io_addr[sel];
if (io_inc[sel]) {
io_addr[sel] += 1 << (io_inc[sel] - 1);
}
// printf("address = %06X, new = %06X\n", address, io_addr[sel]);