/
startup_support.c
2812 lines (2366 loc) · 144 KB
/
startup_support.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
// ---- startup_support.c - Dreamcast Startup Support Module ----
//
// Version 1.1.2
//
// This file is part of the DreamHAL project, a hardware abstraction library
// primarily intended for use on the SH7091 found in hardware such as the SEGA
// Dreamcast game console.
//
// This support module is hereby released into the public domain in the hope that it
// may prove useful.
//
// --Moopthehedgehog, January 2020
//
// This file contains some C code that is required by startup.S for setting FPSCR
// in addition to video setup support functions
//
#include "startup_support.h"
//==============================================================================
// System Support
//==============================================================================
// Enable or disable 8kB onchip RAM
const uint32_t STARTUP_use_ocram = ENABLE_OCRAM;
// Set by startup.S depending on the state of dcload (0 = none, 1 = present with console, 2 = present without console)
uint32_t STARTUP_dcload_present = 0;
// Set by STARTUP_Init_Video().
// This is a global cable type value for user reference (0 = VGA, 2 = RGB, 3 = Composite/S-Video)
uint32_t STARTUP_cable_type = 0;
// Set by STARTUP_Init_Video().
// This is a global console region type for user reference (0 = JP, 1 = NA, 2 = PAL)
uint32_t STARTUP_console_region = 0;
//==============================================================================
// FPSCR Support
//==============================================================================
// startup.S needs this to set FPSCR since GCC deprecated __set_fpscr and
// __get_fpscr and replaced them with builtins.
//
// void __builtin_sh_set_fpscr(uint32_t val) doesn't affect SZ, PR, and FR,
// unlike the old void __set_fpscr(uint32_t val) macro.
//
// uint32_t __builtin_sh_get_fpscr(void), on the other hand, behaves the same as
// the old uint32_t __get_fpscr(void) macro.
//
// Also: the old macros were in libgcc, and the new ones are not (yay!).
//
// GCC <= 4 needs to link with -lgcc
#if __GNUC__ <= 4
extern void __set_fpscr(uint32_t value);
extern uint32_t __get_fpscr(void);
void __call_builtin_sh_set_fpscr(uint32_t value)
{
__set_fpscr(value);
}
uint32_t __call_builtin_sh_get_fpscr(void)
{
return __get_fpscr();
}
#else
void __call_builtin_sh_set_fpscr(uint32_t value)
{
__builtin_sh_set_fpscr(value);
}
uint32_t __call_builtin_sh_get_fpscr(void)
{
return __builtin_sh_get_fpscr();
}
#endif
//==============================================================================
// Video Support
//==============================================================================
//
// After days of examining a disassembly I made of my own Dreamcast BootROM and
// testing on actual Dreamcast hardware, this should be everything there is to
// set up correct video output modes.
//
// The framebuffer address will always be 0xa5000000 after any of these run.
//
// Auxiliary Reading Material:
//
// These links are provided as a courtesy in the event one wants to learn about
// what these registers actually do, and because they're all cool projects and
// documentation that you should totally check out anyways:
//
// - A register mapping effort done here:
// http://dev.dcemulation.org/tutorials/registermap.htm
// - Lars Olsson's descriptions of the Dreamcast video subsystem:
// https://www.ludd.ltu.se/~jlo/dc/powervr-reg.txt
// - Lars Olsson's attempt to reconstruct a Dreamcast BootROM:
// https://www.ludd.ltu.se/~jlo/dc/bootROM.c
// - Marcus Comstedt's descriptions of the Dreamcast video subsystem:
// http://mc.pp.se/dc/pvr.html
// - The Renesas SH7750 series hardware manual (in the documents section, it's
// called "SH7750, SH7750S, SH7750R Group User's Manual: Hardware"), which
// describes the GPIO port settings:
// https://www.renesas.com/us/en/products/microcontrollers-microprocessors/superh/sh7750/sh7750r.html
// - KallistiOS 2.1.0 video.c file:
// https://github.com/KallistiOS/KallistiOS/
// - 240p Test Suite v1.25 vmode.c file:
// https://sourceforge.net/p/testsuite240p/
//
// Notes:
// - The color mode in 0xa05f8048 should (maybe?) match the framebuffer mode for
// the graphics chip to render directly to the screen
// - For SH4 to draw directly to the framebuffer,0xa05f8048 doesn't matter
// - Bit 23 of 0xa05f8044 selects between 13.5MHz (NTSC/PAL) and 27MHz (VGA)
// as required by the BU142x series of video encoders, which are apparently
// used by the Dreamcast: https://www.digchip.com/datasheets/parts/datasheet/406/BU1425AK.php
// (the Dreamcast has the exact same video DAC model as the Naomi 2, which uses
// this series of chips per https://segaretro.org/Sega_NAOMI_2. Notice that
// 27MHz (VGA) is 54MHz/2 and 13.5MHz (NTSC/PAL) is 54MHz/4).
// - Cable types: 0 is VGA, 1 is undefined, 2 is SCART, 3 is composite
// - Framebuffer color modes: 16bit = 2Bpp (RGB0555, RGB565), 24bit = 3Bpp
// (RGB888), 32bit = 4Bpp (RGB0888)
// - Apparently the AICA needs to know the cable type, and it turns out it does
// have a pin--it's actually the AICA's JTAG TDO--that it uses to communicate
// to the video DAC. This AICA register also seems to have a reset bit, so it
// appears to be the AICA's JTAG control register being written to here. Dear
// Sega engineers: nice one, using an output pin that's always an output
// instead of a GPIO pin whose state gets reset to pulled-up input every boot.
// Probably solved a graphical glitch or monitor compatibility issue that way. :)
//
VIDEO_PARAMS_STRUCT STARTUP_video_params = {1.0f, 1.0f, 640, 480, 60, 0, 640, 480, 2};
// These get set by STARTUP_Init_Video() for use only by STARTUP_Set_Video() and
// extra video mode setup functions.
static uint32_t cable_mode = 0;
static uint32_t video_region = 0;
// Video mode is automatically determined based on cable type and console region
// This sets up everything related to Dreamcast video modes.
void STARTUP_Init_Video(uint8_t fbuffer_color_mode, uint8_t use_320x240)
{
// Set cable type to hardware pin setting
// Need to read port 8 and 9 data (bits 8 & 9 in PDTRA), so set them as input
// direction via PCTRA (necessary per SH7750 hardware manual):
*(volatile uint32_t*)0xff80002c = ( (*(volatile uint32_t*)0xff80002c) & 0xfff0ffff ) | 0x000a0000;
// According to the BootROM, cable data is on PORT8/9 GPIO pins.
// Read them and then write them to somewhere in AICA memory (refer to notes
// section for an explanation and a theory as to why this might be necessary):
cable_mode = (uint32_t)( (*(volatile uint16_t*)0xff800030) & 0x300 );
*(volatile uint32_t*)0xa0702c00 = ( (*(volatile uint32_t*)0xa0702c00) & 0xfffffcff ) | cable_mode;
// Per the SH7750 manual, there are 16 data regs, hence a 16-bit read should
// be used on PDTRA.
// Store global cable type (0 = VGA, 2 = RGB, 3 = Composite/S-Video)
STARTUP_cable_type = cable_mode >> 8;
// Store global console region type (0 = JP, 1 = NA, 2 = PAL)
STARTUP_console_region = (*(uint8_t*)0x8c000072) - 0x30;
// Store video output region (0 = NTSC, 1 = PAL)
video_region = (*(uint8_t*)0x8c000074) - 0x30;
// Reset graphics subsystem (PVR2), but keep the graphics memory bus on ..or
// else things will hang when writing to graphics memory since it got disabled!
*(volatile uint32_t*)0xa05f8008 = 0x00000003;
// Re-enable PVR, TA
*(volatile uint32_t*)0xa05f8008 = 0x00000000;
STARTUP_Set_Video(fbuffer_color_mode, use_320x240);
}
// Set a standard Dreamcast video mode
// These are the plain 640x480 modes (VGA and composite/S-Video), and are based
// on how the BootROM does things. 320x240 is a linedoubled + pixeldoubled mode,
// so the framebuffer is 320x240xColorBpp and the output frame is standard 640x480.
void STARTUP_Set_Video(uint8_t fbuffer_color_mode, uint8_t use_320x240)
{
// Set global scale factors
STARTUP_video_params.video_scale = 1.0f;
STARTUP_video_params.video_scale_multiplier = 1.0f;
// Set global video output mode parameters
STARTUP_video_params.video_width = 640;
STARTUP_video_params.video_height = 480;
STARTUP_video_params.video_color_type = fbuffer_color_mode;
STARTUP_video_params.video_refresh_rate = 60;
uint32_t horiz_active_area = 640;
uint32_t vert_active_area = 480;
// {RGB0555, RGB565} = 2Bpp, {RGB888} = 3Bpp, {RGB0888} = 4Bpp
uint32_t bpp_mode_size = fbuffer_color_mode + 1 + (0x1 ^ ((fbuffer_color_mode & 0x1) | (fbuffer_color_mode >> 1))); // Add another 1 only if 0b00
// NOTE:
// Unlike the extra video modes that set framebuffer parameters here, this
// function sets the framebuffer parameters at the end to account for the
// scaled 320x240 modes.
if((!cable_mode) && (!use_320x240)) // VGA 640x480 @ 60Hz
{
// Set registers the same way that the BootROM does
*(volatile uint32_t*)0xa05f80e8 = 0x00160008;
*(volatile uint32_t*)0xa05f8044 = 0x00800000 | (fbuffer_color_mode << 2);
// *(volatile uint32_t*)0xa05f8048 = ( (*(volatile uint32_t*)0xa05f8048) & 0xfffffff8 ) | 0 -> 0,3 / 1 -> 1 / 2 -> 4 / 3 -> 5,6; ( x -> 2 is ARGB4444, doesn't match any display mode)
*(volatile uint32_t*)0xa05f804c = (horiz_active_area * bpp_mode_size) / 8; // for PVR to know active area width
*(volatile uint32_t*)0xa05f8040 = 0x00000000; // Border color in RGB0888 format
*(volatile uint32_t*)0xa05f805c = (1 << 20) | ((vert_active_area - 1) << 10) | (((horiz_active_area * bpp_mode_size) / 4) - 1); // progressive scan has a 1 since no lines are skipped
*(volatile uint32_t*)0xa05f80ec = 0x000000a8;
*(volatile uint32_t*)0xa05f80f0 = 0x00280028;
*(volatile uint32_t*)0xa05f80c8 = 0x03450000;
*(volatile uint32_t*)0xa05f80cc = 0x00150208;
*(volatile uint32_t*)0xa05f80d0 = 0x00000100;
*(volatile uint32_t*)0xa05f80d4 = 0x007e0345;
*(volatile uint32_t*)0xa05f80d8 = 0x020c0359;
*(volatile uint32_t*)0xa05f80dc = 0x00280208;
*(volatile uint32_t*)0xa05f80e0 = 0x03f1933f;
uint32_t scan_area_size = horiz_active_area * vert_active_area;
uint32_t scan_area_size_bytes = scan_area_size * bpp_mode_size; // This will always be divisible by 4
// Reset framebuffer address
*(volatile uint32_t*)0xa05f8050 = 0x00000000; // BootROM sets this to 0x00200000 (framebuffer base is 0xa5000000 + this)
*(volatile uint32_t*)0xa05f8054 = 0x00000000; // Same for progressive, resetting the offset gets us 2MB VRAM back after BootROM is done with it
// zero out framebuffer area
for(uint32_t pixel_or_two = 0; pixel_or_two < scan_area_size_bytes; pixel_or_two += 4)
{
*(uint32_t*)(0xa5000000 + pixel_or_two) = 0;
}
// re-enable video
*(volatile uint32_t*)0xa05f80e8 &= ~8;
*(volatile uint32_t*)0xa05f8044 |= 1;
}
else if((!cable_mode) && (use_320x240)) // VGA 320x240 @ 60Hz (linedoubled + pixeldoubled to 640x480)
{
// Modify what BootROM does to get 320x240 in VGA, formatted just like 640x480
horiz_active_area /= 2;
vert_active_area /= 2;
*(volatile uint32_t*)0xa05f80e8 = 0x00160008 | 0x100; // 0x100 for pixeldouble (each horiz pixel gets drawn twice; 2x horiz scale)
*(volatile uint32_t*)0xa05f8044 = 0x00800000 | (fbuffer_color_mode << 2) | 0x2; // 0x2 for linedouble (each line gets drawn twice; 2x vert scale)
*(volatile uint32_t*)0xa05f804c = (horiz_active_area * bpp_mode_size) / 8; // for PVR to know active area width
*(volatile uint32_t*)0xa05f8040 = 0x00000000; // Border color in RGB0888 format
*(volatile uint32_t*)0xa05f805c = (1 << 20) | ((vert_active_area - 1) << 10) | (((horiz_active_area * bpp_mode_size) / 4) - 1); // progressive scan has a 1 since no lines are skipped
*(volatile uint32_t*)0xa05f80ec = 0x000000a8;
*(volatile uint32_t*)0xa05f80f0 = 0x00280028;
*(volatile uint32_t*)0xa05f80c8 = 0x03450000;
*(volatile uint32_t*)0xa05f80cc = 0x00150208;
*(volatile uint32_t*)0xa05f80d0 = 0x00000100;
*(volatile uint32_t*)0xa05f80d4 = 0x007e0345;
*(volatile uint32_t*)0xa05f80d8 = 0x020c0359;
*(volatile uint32_t*)0xa05f80dc = 0x00280208;
*(volatile uint32_t*)0xa05f80e0 = 0x03f1933f;
uint32_t scan_area_size = horiz_active_area * vert_active_area;
uint32_t scan_area_size_bytes = scan_area_size * bpp_mode_size; // This will always be divisible by 4
// Reset framebuffer address
*(volatile uint32_t*)0xa05f8050 = 0x00000000; // BootROM sets this to 0x00200000 (framebuffer base is 0xa5000000 + this)
*(volatile uint32_t*)0xa05f8054 = 0x00000000; // Same for progressive, resetting the offset gets us 2MB VRAM back after BootROM is done with it
// zero out framebuffer area
for(uint32_t pixel_or_two = 0; pixel_or_two < scan_area_size_bytes; pixel_or_two += 4)
{
*(uint32_t*)(0xa5000000 + pixel_or_two) = 0;
}
// re-enable video
*(volatile uint32_t*)0xa05f80e8 &= ~8;
*(volatile uint32_t*)0xa05f8044 |= 1;
}
else if((!video_region) && (!use_320x240)) // NTSC (480i)
{
// Set registers the same way that the BootROM does
*(volatile uint32_t*)0xa05f80e8 = 0x00160008;
*(volatile uint32_t*)0xa05f8044 = 0x00000000 | (fbuffer_color_mode << 2);
*(volatile uint32_t*)0xa05f804c = (horiz_active_area * bpp_mode_size) / 8; // for PVR to know active area width
*(volatile uint32_t*)0xa05f8040 = 0x00000000; // Border color in RGB0888 format
*(volatile uint32_t*)0xa05f805c = ((((horiz_active_area * bpp_mode_size) / 4) + 1) << 20) | (((vert_active_area / 2) - 1) << 10) | (((horiz_active_area * bpp_mode_size) / 4) - 1);
*(volatile uint32_t*)0xa05f80ec = 0x000000a4;
*(volatile uint32_t*)0xa05f80f0 = 0x00120012;
*(volatile uint32_t*)0xa05f80c8 = 0x03450000;
*(volatile uint32_t*)0xa05f80cc = 0x00150104;
*(volatile uint32_t*)0xa05f80d0 = 0x00000150;
*(volatile uint32_t*)0xa05f80d4 = 0x007e0345;
*(volatile uint32_t*)0xa05f80d8 = 0x020c0359;
*(volatile uint32_t*)0xa05f80dc = 0x00240204;
*(volatile uint32_t*)0xa05f80e0 = 0x07d6c63f;
uint32_t scan_area_size = horiz_active_area * vert_active_area;
uint32_t scan_area_size_bytes = scan_area_size * bpp_mode_size; // This will always be divisible by 4
// Reset framebuffer address
*(volatile uint32_t*)0xa05f8050 = 0x00000000; // BootROM sets this to 0x00200000 (framebuffer base is 0xa5000000 + this)
*(volatile uint32_t*)0xa05f8054 = horiz_active_area * bpp_mode_size; // This is for interlaced, resetting the offset gets us 2MB VRAM back after BootROM is done with it
// zero out framebuffer area
for(uint32_t pixel_or_two = 0; pixel_or_two < scan_area_size_bytes; pixel_or_two += 4)
{
*(uint32_t*)(0xa5000000 + pixel_or_two) = 0;
}
// re-enable video
*(volatile uint32_t*)0xa05f80e8 &= ~8;
*(volatile uint32_t*)0xa05f8044 |= 1;
}
else if((!video_region) && (use_320x240)) // NTSC (240p, but we need to wrap it in 480i for most TVs to care)
{
horiz_active_area /= 2;
vert_active_area /= 2;
*(volatile uint32_t*)0xa05f80e8 = 0x00160008 | 0x100; // 0x100 for pixeldouble (each horiz pixel gets drawn twice; 2x horiz scale)
*(volatile uint32_t*)0xa05f8044 = 0x00000000 | (fbuffer_color_mode << 2) | 0x2; // 0x2 for linedouble (each line gets drawn twice; 2x vert scale)
*(volatile uint32_t*)0xa05f804c = (horiz_active_area * bpp_mode_size) / 8; // for PVR to know active area width
*(volatile uint32_t*)0xa05f8040 = 0x00000000; // Border color in RGB0888 format
*(volatile uint32_t*)0xa05f805c = ((((horiz_active_area * bpp_mode_size) / 4) + 1) << 20) | ((vert_active_area - 1) << 10) | (((horiz_active_area * bpp_mode_size) / 4) - 1);
*(volatile uint32_t*)0xa05f80ec = 0x000000a4;
*(volatile uint32_t*)0xa05f80f0 = 0x00120012;
*(volatile uint32_t*)0xa05f80c8 = 0x03450000;
*(volatile uint32_t*)0xa05f80cc = 0x00150104;
*(volatile uint32_t*)0xa05f80d0 = 0x00000140 | 0x10; // Interlaced
*(volatile uint32_t*)0xa05f80d4 = 0x007e0345;
*(volatile uint32_t*)0xa05f80d8 = 0x020c0359;
*(volatile uint32_t*)0xa05f80dc = 0x00240204;
*(volatile uint32_t*)0xa05f80e0 = 0x07d6c63f; // Really we need 240p-in-480i
uint32_t scan_area_size = horiz_active_area * vert_active_area;
uint32_t scan_area_size_bytes = scan_area_size * bpp_mode_size; // This will always be divisible by 4
// Reset framebuffer address
*(volatile uint32_t*)0xa05f8050 = 0x00000000; // BootROM sets this to 0x00200000 (framebuffer base is 0xa5000000 + this)
*(volatile uint32_t*)0xa05f8054 = horiz_active_area * bpp_mode_size; // Resetting the framebuffer offset gets us 2MB VRAM back after BootROM is done with it
// zero out framebuffer area
for(uint32_t pixel_or_two = 0; pixel_or_two < scan_area_size_bytes; pixel_or_two += 4)
{
*(uint32_t*)(0xa5000000 + pixel_or_two) = 0;
}
// re-enable video
*(volatile uint32_t*)0xa05f80e8 &= ~8;
*(volatile uint32_t*)0xa05f8044 |= 1;
}
else if(use_320x240) // PAL (would be 288p, actually 264p according to 240p Test Suite, but we need to wrap it in 576i for modern TVs to care)
{
horiz_active_area /= 2;
vert_active_area /= 2;
#ifdef PAL_EXTRA_LINES
// Low-res PAL can actually have 264 active vertical lines on the Dreamcast (24 more than NTSC!)
// In theory a developer could hide a secret message for PAL players in this area
vert_active_area += 24;
#endif
*(volatile uint32_t*)0xa05f80e8 = 0x00160008 | 0x100; // 0x100 for pixeldouble (each horiz pixel gets drawn twice; 2x horiz scale)
*(volatile uint32_t*)0xa05f8044 = 0x00000000 | (fbuffer_color_mode << 2) | 0x2; // 0x2 for linedouble (each line gets drawn twice; 2x vert scale)
*(volatile uint32_t*)0xa05f804c = (horiz_active_area * bpp_mode_size) / 8; // for PVR to know active area width
*(volatile uint32_t*)0xa05f8040 = 0x00000000; // Border color in RGB0888 format
*(volatile uint32_t*)0xa05f805c = ((((horiz_active_area * bpp_mode_size) / 4) + 1) << 20) | ((vert_active_area - 1) << 10) | (((horiz_active_area * bpp_mode_size) / 4) - 1);
*(volatile uint32_t*)0xa05f80ec = 0x000000ae;
*(volatile uint32_t*)0xa05f80f0 = 0x002e002d;
*(volatile uint32_t*)0xa05f80c8 = 0x034b0000;
*(volatile uint32_t*)0xa05f80cc = 0x00150136;
*(volatile uint32_t*)0xa05f80d0 = 0x00000180 | 0x10; // Interlaced
*(volatile uint32_t*)0xa05f80d4 = 0x008d034b;
*(volatile uint32_t*)0xa05f80d8 = 0x0270035f;
*(volatile uint32_t*)0xa05f80dc = 0x002c026c;
*(volatile uint32_t*)0xa05f80e0 = 0x07d6a53f; // Really we need 288p-in-576i
uint32_t scan_area_size = horiz_active_area * vert_active_area;
uint32_t scan_area_size_bytes = scan_area_size * bpp_mode_size; // This will always be divisible by 4
// Reset framebuffer address
*(volatile uint32_t*)0xa05f8050 = 0x00000000; // BootROM sets this to 0x00200000 (framebuffer base is 0xa5000000 + this)
*(volatile uint32_t*)0xa05f8054 = horiz_active_area * bpp_mode_size; // Resetting the framebuffer offset gets us 2MB VRAM back after BootROM is done with it
// zero out framebuffer area
for(uint32_t pixel_or_two = 0; pixel_or_two < scan_area_size_bytes; pixel_or_two += 4)
{
*(uint32_t*)(0xa5000000 + pixel_or_two) = 0;
}
// re-enable video
*(volatile uint32_t*)0xa05f80e8 &= ~8;
*(volatile uint32_t*)0xa05f8044 |= 1;
}
else // PAL (576i)
{
#ifdef PAL_EXTRA_LINES
// Interlaced PAL can actually have 528 active vertical lines on the Dreamcast (48 more than NTSC!)
// In theory a developer could hide a secret message for PAL players in this area
vert_active_area += 48;
#endif
// Set registers the same way that the BootROM does
*(volatile uint32_t*)0xa05f80e8 = 0x00160008;
*(volatile uint32_t*)0xa05f8044 = 0x00000000 | (fbuffer_color_mode << 2);
*(volatile uint32_t*)0xa05f804c = (horiz_active_area * bpp_mode_size) / 8; // for PVR to know active area width
*(volatile uint32_t*)0xa05f8040 = 0x00000000; // Border color in RGB0888 format
*(volatile uint32_t*)0xa05f805c = ((((horiz_active_area * bpp_mode_size) / 4) + 1) << 20) | (((vert_active_area / 2) - 1) << 10) | (((horiz_active_area * bpp_mode_size) / 4) - 1);
*(volatile uint32_t*)0xa05f80ec = 0x000000ae;
*(volatile uint32_t*)0xa05f80f0 = 0x002e002d;
*(volatile uint32_t*)0xa05f80c8 = 0x034b0000;
*(volatile uint32_t*)0xa05f80cc = 0x00150136;
*(volatile uint32_t*)0xa05f80d0 = 0x00000190;
*(volatile uint32_t*)0xa05f80d4 = 0x008d034b;
*(volatile uint32_t*)0xa05f80d8 = 0x0270035f;
*(volatile uint32_t*)0xa05f80dc = 0x002c026c;
*(volatile uint32_t*)0xa05f80e0 = 0x07d6a53f;
uint32_t scan_area_size = horiz_active_area * vert_active_area;
uint32_t scan_area_size_bytes = scan_area_size * bpp_mode_size; // This will always be divisible by 4
// Reset framebuffer address
*(volatile uint32_t*)0xa05f8050 = 0x00000000; // BootROM sets this to 0x00200000 (framebuffer base is 0xa5000000 + this)
*(volatile uint32_t*)0xa05f8054 = horiz_active_area * bpp_mode_size; // This is for interlaced, resetting the offset gets us 2MB VRAM back after BootROM is done with it
// zero out framebuffer area
for(uint32_t pixel_or_two = 0; pixel_or_two < scan_area_size_bytes; pixel_or_two += 4)
{
*(uint32_t*)(0xa5000000 + pixel_or_two) = 0;
}
// re-enable video
*(volatile uint32_t*)0xa05f80e8 &= ~8;
*(volatile uint32_t*)0xa05f8044 |= 1;
}
// Set global framebuffer parameters
STARTUP_video_params.fb_width = horiz_active_area;
STARTUP_video_params.fb_height = vert_active_area;
STARTUP_video_params.fb_color_bytes = bpp_mode_size;
}
//==============================================================================
// Extra Video Modes
//==============================================================================
//
// These can be used to set up extra video modes after running STARTUP_Init_Video().
//
// The framebuffer address will always be set to 0xa5000000 after any of these run.
//
// Modes marked "PVR 32x32" have a framebuffer that's an integer multiple of 32x32.
//
//------------------------------------------------------------------------------
// 60Hz Modes
//------------------------------------------------------------------------------
//
// Scaled 60Hz video modes
//
// 848x480 @ 60Hz (16:9, DMT, but using a slightly-too-short hsync)
// Framebuffer: 678x480
// Horizontal scale: 0.799528302x
// DMT specifies a 3.32usec hsync width, which is 90 Dreamcast pixels when scaled
// for 848x480. The Dreamcast maxes at an hsync width of 64 pixels, which is
// 2.37usec. This may not really cause a problem (no issues with any of the 5
// different LCDs I tried), but it is worth pointing out.
void STARTUP_848x480_VGA(uint8_t fbuffer_color_mode)
{
// Set global scale factors
STARTUP_video_params.video_scale = 848.0f / 678.0f;
STARTUP_video_params.video_scale_multiplier = 678.0f / 848.0f;
// Set global video output mode parameters
STARTUP_video_params.video_width = 848;
STARTUP_video_params.video_height = 480;
STARTUP_video_params.video_color_type = fbuffer_color_mode;
STARTUP_video_params.video_refresh_rate = 60;
// 1088 wide scaled to 870 wide (field)
// 848 wide scaled to 678 wide (visible frame)
uint32_t horiz_active_area = 678;
uint32_t vert_active_area = 480;
// {RGB0555, RGB565} = 2Bpp, {RGB888} = 3Bpp, {RGB0888} = 4Bpp
uint32_t bpp_mode_size = fbuffer_color_mode + 1 + (0x1 ^ ((fbuffer_color_mode & 0x1) | (fbuffer_color_mode >> 1))); // Add another 1 only if 0b00
// Set global framebuffer parameters
STARTUP_video_params.fb_width = horiz_active_area;
STARTUP_video_params.fb_height = vert_active_area;
STARTUP_video_params.fb_color_bytes = bpp_mode_size;
if(!cable_mode) // VGA 60Hz
{
*(volatile uint32_t*)0xa05f80e8 = 0x00160008;
*(volatile uint32_t*)0xa05f8044 = 0x00800000 | (fbuffer_color_mode << 2);
*(volatile uint32_t*)0xa05f804c = (horiz_active_area * bpp_mode_size) / 8; // for PVR to know active area width
*(volatile uint32_t*)0xa05f8040 = 0x00000000; // Border color in RGB0888 format (this mode has no border)
*(volatile uint32_t*)0xa05f805c = (1 << 20) | ((vert_active_area - 1) << 10) | (((horiz_active_area * bpp_mode_size) / 4) - 1); // progressive scan has a 1 since no lines are skipped
*(volatile uint32_t*)0xa05f80ec = 0x000000b3; // ok: 179 ? 168, default: 0x000000a8 (horiz)
*(volatile uint32_t*)0xa05f80f0 = 0x001f001f; // ok: 31 & 31 ? 40 & 40, default: 0x00280028 (vert)
*(volatile uint32_t*)0xa05f80c8 = 0x03590000; // ok: 857 ? 837, default: 0x03450000 (horiz)
*(volatile uint32_t*)0xa05f80cc = 0x001f01ff; // ok: 31, 511 ? 21, 520, default: 0x00150208 (vert)
*(volatile uint32_t*)0xa05f80d0 = 0x00000100;
*(volatile uint32_t*)0xa05f80d4 = 0x00b30359; // ok: 179, 857 ? 126, 837, default: 0x007e0345 (horiz)
*(volatile uint32_t*)0xa05f80d8 = 0x02040365; // ok: 869x516 (870x517) ? 857x524 (858x525), default: 0x020c0359
*(volatile uint32_t*)0xa05f80dc = 0x001f01ff; // ok: 31, 511 ? 40, 520, default: 0x00280208 (vert)
*(volatile uint32_t*)0xa05f80e0 = 0x03f2583f; // ok: 15 (16,- 1), 805 (870 - 64,- 1), 8 (per DMT), 63 (64,- 1) ? 15, 793, 3, 63, default: 0x03f1933f
// scaled hsync should be 112/1.25 = 90, but max is 64... May/may not work with everything as a result.
// the 16 should be 22 for 89.6/4, but, since it's 64, using 16 instead.
uint32_t scan_area_size = horiz_active_area * vert_active_area;
uint32_t scan_area_size_bytes = scan_area_size * bpp_mode_size; // This will always be divisible by 4
// Reset framebuffer address
*(volatile uint32_t*)0xa05f8050 = 0x00000000; // BootROM sets this to 0x00200000 (framebuffer base is 0xa5000000 + this)
*(volatile uint32_t*)0xa05f8054 = 0x00000000; // Same for progressive, resetting the offset gets us 2MB VRAM back after BootROM is done with it
// zero out framebuffer area
for(uint32_t pixel_or_two = 0; pixel_or_two < scan_area_size_bytes; pixel_or_two += 4)
{
*(uint32_t*)(0xa5000000 + pixel_or_two) = 0;
}
// re-enable video
*(volatile uint32_t*)0xa05f80e8 &= ~8;
*(volatile uint32_t*)0xa05f8044 |= 1;
}
}
// 848x480 @ 60Hz (16:9, DMT, but using a slightly-too-short hsync) - PVR 32x32
// Framebuffer: 672x480
// Horizontal scale: 0.79245283x
// Same caveat as 848x480 VGA, but this mode has been shrunken by 6 columns for
// 32x32 framebuffer compatibility. As a result, there may be 6 total columns of
// blank pixels on the horizontal sides.
void STARTUP_848x480_VGA_PVR(uint8_t fbuffer_color_mode)
{
// Set global scale factors
STARTUP_video_params.video_scale = 848.0f / 672.0f;
STARTUP_video_params.video_scale_multiplier = 672.0f / 848.0f;
// Set global video output mode parameters
STARTUP_video_params.video_width = 848;
STARTUP_video_params.video_height = 480;
STARTUP_video_params.video_color_type = fbuffer_color_mode;
STARTUP_video_params.video_refresh_rate = 60;
// 1088 wide scaled to 870 wide (field)
// 848 wide scaled to 678 wide (visible frame)
uint32_t horiz_active_area = 672;
uint32_t vert_active_area = 480;
// {RGB0555, RGB565} = 2Bpp, {RGB888} = 3Bpp, {RGB0888} = 4Bpp
uint32_t bpp_mode_size = fbuffer_color_mode + 1 + (0x1 ^ ((fbuffer_color_mode & 0x1) | (fbuffer_color_mode >> 1))); // Add another 1 only if 0b00
// Set global framebuffer parameters
STARTUP_video_params.fb_width = horiz_active_area;
STARTUP_video_params.fb_height = vert_active_area;
STARTUP_video_params.fb_color_bytes = bpp_mode_size;
if(!cable_mode) // VGA 60Hz
{
*(volatile uint32_t*)0xa05f80e8 = 0x00160008;
*(volatile uint32_t*)0xa05f8044 = 0x00800000 | (fbuffer_color_mode << 2);
*(volatile uint32_t*)0xa05f804c = (horiz_active_area * bpp_mode_size) / 8; // for PVR to know active area width
*(volatile uint32_t*)0xa05f8040 = 0x00000000; // Border color in RGB0888 format
*(volatile uint32_t*)0xa05f805c = (1 << 20) | ((vert_active_area - 1) << 10) | (((horiz_active_area * bpp_mode_size) / 4) - 1); // progressive scan has a 1 since no lines are skipped
*(volatile uint32_t*)0xa05f80ec = 0x000000b6; // ok: 182 ? 168, default: 0x000000a8 (horiz)
*(volatile uint32_t*)0xa05f80f0 = 0x001f001f; // ok: 31 & 31 ? 40 & 40, default: 0x00280028 (vert)
*(volatile uint32_t*)0xa05f80c8 = 0x03590000; // ok: 857 ? 837, default: 0x03450000 (horiz)
*(volatile uint32_t*)0xa05f80cc = 0x001f01ff; // ok: 31, 511 ? 21, 520, default: 0x00150208 (vert)
*(volatile uint32_t*)0xa05f80d0 = 0x00000100;
*(volatile uint32_t*)0xa05f80d4 = 0x00b30359; // ok: 179, 857 ? 126, 837, default: 0x007e0345 (horiz)
*(volatile uint32_t*)0xa05f80d8 = 0x02040365; // ok: 869x516 (870x517) ? 857x524 (858x525), default: 0x020c0359
*(volatile uint32_t*)0xa05f80dc = 0x001f01ff; // ok: 31, 511 ? 40, 520, default: 0x00280208 (vert)
*(volatile uint32_t*)0xa05f80e0 = 0x03f2583f; // ok: 15 (16,- 1), 805 (870 - 64,- 1), 8 (per DMT), 63 (64,- 1) ? 15, 793, 3, 63, default: 0x03f1933f
// scaled hsync should be 112/1.25 = 90, but max is 64... May/may not work with everything as a result.
// the 16 should be 22 for 89.6/4, but, since it's 64, using 16 instead.
uint32_t scan_area_size = horiz_active_area * vert_active_area;
uint32_t scan_area_size_bytes = scan_area_size * bpp_mode_size; // This will always be divisible by 4
// Reset framebuffer address
*(volatile uint32_t*)0xa05f8050 = 0x00000000; // BootROM sets this to 0x00200000 (framebuffer base is 0xa5000000 + this)
*(volatile uint32_t*)0xa05f8054 = 0x00000000; // Same for progressive, resetting the offset gets us 2MB VRAM back after BootROM is done with it
// zero out framebuffer area
for(uint32_t pixel_or_two = 0; pixel_or_two < scan_area_size_bytes; pixel_or_two += 4)
{
*(uint32_t*)(0xa5000000 + pixel_or_two) = 0;
}
// re-enable video
*(volatile uint32_t*)0xa05f80e8 &= ~8;
*(volatile uint32_t*)0xa05f8044 |= 1;
}
}
// 800x600 @ 60Hz (4:3, DMT, but using a slightly-too-short hsync)
// Framebuffer: 540x600
// Horizontal scale: 0.675x (exact)
// DMT specifies a 3.20usec hsync width, which is 86 Dreamcast pixels when scaled
// for 800x600. The Dreamcast maxes at an hsync width of 64 pixels, which is
// 2.37usec. This may not really cause a problem (no issues with any of the 5
// different LCDs I tried), but it is worth pointing out.
void STARTUP_800x600_VGA(uint8_t fbuffer_color_mode)
{
// Set global scale factors
STARTUP_video_params.video_scale = 800.0f / 540.0f;
STARTUP_video_params.video_scale_multiplier = 540.0f / 800.0f;
// Set global video output mode parameters
STARTUP_video_params.video_width = 800;
STARTUP_video_params.video_height = 600;
STARTUP_video_params.video_color_type = fbuffer_color_mode;
STARTUP_video_params.video_refresh_rate = 60;
// 1056 wide scaled to 713 wide (field)
// 800 wide scaled to 540 wide (visible frame)
uint32_t horiz_active_area = 540;
uint32_t vert_active_area = 600;
// {RGB0555, RGB565} = 2Bpp, {RGB888} = 3Bpp, {RGB0888} = 4Bpp
uint32_t bpp_mode_size = fbuffer_color_mode + 1 + (0x1 ^ ((fbuffer_color_mode & 0x1) | (fbuffer_color_mode >> 1))); // Add another 1 only if 0b00
// Set global framebuffer parameters
STARTUP_video_params.fb_width = horiz_active_area;
STARTUP_video_params.fb_height = vert_active_area;
STARTUP_video_params.fb_color_bytes = bpp_mode_size;
if(!cable_mode) // VGA 60Hz
{
*(volatile uint32_t*)0xa05f80e8 = 0x00160008;
*(volatile uint32_t*)0xa05f8044 = 0x00800000 | (fbuffer_color_mode << 2);
*(volatile uint32_t*)0xa05f804c = (horiz_active_area * bpp_mode_size) / 8; // for PVR to know active area width
*(volatile uint32_t*)0xa05f8040 = 0x00000000; // Border color in RGB0888 format (this mode has no border)
*(volatile uint32_t*)0xa05f805c = (1 << 20) | ((vert_active_area - 1) << 10) | (((horiz_active_area * bpp_mode_size) / 4) - 1); // progressive scan has a 1 since no lines are skipped
*(volatile uint32_t*)0xa05f80ec = 0x00000092; // ok: 146 ? 168, default: 0x000000a8 (horiz)
*(volatile uint32_t*)0xa05f80f0 = 0x001b001b; // ok: 27 & 27 ? 40 & 40, default: 0x00280028 (vert)
*(volatile uint32_t*)0xa05f80c8 = 0x02ae0000; // ok: 686 ? 837, default: 0x03450000 (horiz)
*(volatile uint32_t*)0xa05f80cc = 0x001b0273; // ok: 27, 627 ? 21, 520, default: 0x00150208 (vert)
*(volatile uint32_t*)0xa05f80d0 = 0x00000100;
*(volatile uint32_t*)0xa05f80d4 = 0x009202ae; // ok: 146, 686 ? 126, 837, default: 0x007e0345 (horiz)
*(volatile uint32_t*)0xa05f80d8 = 0x027302c8; // ok: 712x627 (713x628) ? 857x524 (858x525), default: 0x020c0359
*(volatile uint32_t*)0xa05f80dc = 0x001b0273; // ok: 27, 627 ? 40, 520, default: 0x00280208 (vert)
*(volatile uint32_t*)0xa05f80e0 = 0x03e8843f; // ok: 15 (16,- 1), 648 (713 - 64,- 1), 4 (per DMT), 63 (64,- 1) ? 15, 793, 3, 63, default: 0x03f1933f
// scaled hsync should be 128/1.48 = 86, but max is 64... May/may not work with everything as a result.
// the 16 should be 22 for 86.4/4, but, since it's 64, using 16 instead.
uint32_t scan_area_size = horiz_active_area * vert_active_area;
uint32_t scan_area_size_bytes = scan_area_size * bpp_mode_size; // This will always be divisible by 4
// Reset framebuffer address
*(volatile uint32_t*)0xa05f8050 = 0x00000000; // BootROM sets this to 0x00200000 (framebuffer base is 0xa5000000 + this)
*(volatile uint32_t*)0xa05f8054 = 0x00000000; // Same for progressive, resetting the offset gets us 2MB VRAM back after BootROM is done with it
// zero out framebuffer area
for(uint32_t pixel_or_two = 0; pixel_or_two < scan_area_size_bytes; pixel_or_two += 4)
{
*(uint32_t*)(0xa5000000 + pixel_or_two) = 0;
}
// re-enable video
*(volatile uint32_t*)0xa05f80e8 &= ~8;
*(volatile uint32_t*)0xa05f8044 |= 1;
}
}
// 800x600 @ 60Hz (4:3, DMT, but using a slightly-too-short hsync) - PVR 32x32
// Framebuffer: 544x608
// Horizontal scale: 0.68x (exact)
// Same caveat as standard 800x600 VGA regarding slightly-too-short hsync width.
// Here, the framebuffer is slightly expanded to 544x608, which is an exact
// multiple of 32x32 for Dreamcast Tile Accelerator compatibility. Note that 8
// lines in the vertical direction may simply be cut off by a monitor as the
// monitor will think this is 800x600. Likewise, 4 columns of pixels may be cut
// off. No idea which side; horizontally it may even be 2 left and 2 right
// depending on the monitor.
void STARTUP_800x600_VGA_PVR(uint8_t fbuffer_color_mode)
{
// Set global scale factors
STARTUP_video_params.video_scale = 800.0f / 544.0f;
STARTUP_video_params.video_scale_multiplier = 544.0f / 800.0f;
// Set global video output mode parameters
STARTUP_video_params.video_width = 800;
STARTUP_video_params.video_height = 600;
STARTUP_video_params.video_color_type = fbuffer_color_mode;
STARTUP_video_params.video_refresh_rate = 60;
// 1056 wide scaled to 713 wide (field)
// 800 wide scaled to 540 wide (visible frame)
uint32_t horiz_active_area = 544;
uint32_t vert_active_area = 608;
// {RGB0555, RGB565} = 2Bpp, {RGB888} = 3Bpp, {RGB0888} = 4Bpp
uint32_t bpp_mode_size = fbuffer_color_mode + 1 + (0x1 ^ ((fbuffer_color_mode & 0x1) | (fbuffer_color_mode >> 1))); // Add another 1 only if 0b00
// Set global framebuffer parameters
STARTUP_video_params.fb_width = horiz_active_area;
STARTUP_video_params.fb_height = vert_active_area;
STARTUP_video_params.fb_color_bytes = bpp_mode_size;
if(!cable_mode) // VGA 60Hz
{
*(volatile uint32_t*)0xa05f80e8 = 0x00160008;
*(volatile uint32_t*)0xa05f8044 = 0x00800000 | (fbuffer_color_mode << 2);
*(volatile uint32_t*)0xa05f804c = (horiz_active_area * bpp_mode_size) / 8; // for PVR to know active area width
*(volatile uint32_t*)0xa05f8040 = 0x00000000; // Border color in RGB0888 format (this mode has no border)
*(volatile uint32_t*)0xa05f805c = (1 << 20) | ((vert_active_area - 1) << 10) | (((horiz_active_area * bpp_mode_size) / 4) - 1); // progressive scan has a 1 since no lines are skipped
*(volatile uint32_t*)0xa05f80ec = 0x00000090; // ok: 144 ? 168, default: 0x000000a8 (horiz)
*(volatile uint32_t*)0xa05f80f0 = 0x00130013; // ok: 19 & 19 ? 40 & 40, default: 0x00280028 (vert)
*(volatile uint32_t*)0xa05f80c8 = 0x02b00000; // ok: 688 ? 837, default: 0x03450000 (horiz)
*(volatile uint32_t*)0xa05f80cc = 0x00130273; // ok: 19, 627 ? 21, 520, default: 0x00150208 (vert)
*(volatile uint32_t*)0xa05f80d0 = 0x00000100;
*(volatile uint32_t*)0xa05f80d4 = 0x009002b0; // ok: 144, 688 ? 126, 837, default: 0x007e0345 (horiz)
*(volatile uint32_t*)0xa05f80d8 = 0x027302c8; // ok: 712x627 (713x628) ? 857x524 (858x525), default: 0x020c0359
*(volatile uint32_t*)0xa05f80dc = 0x00130273; // ok: 19, 627 ? 40, 520, default: 0x00280208 (vert)
*(volatile uint32_t*)0xa05f80e0 = 0x03e8843f; // ok: 15 (16,- 1), 648 (713 - 64,- 1), 4 (per DMT), 63 (64,- 1) ? 15, 793, 3, 63, default: 0x03f1933f
// scaled hsync should be 128/1.48 = 86, but max is 64... May/may not work with everything as a result.
// the 16 should be 22 for 86.4/4, but, since it's 64, using 16 instead.
uint32_t scan_area_size = horiz_active_area * vert_active_area;
uint32_t scan_area_size_bytes = scan_area_size * bpp_mode_size; // This will always be divisible by 4
// Reset framebuffer address
*(volatile uint32_t*)0xa05f8050 = 0x00000000; // BootROM sets this to 0x00200000 (framebuffer base is 0xa5000000 + this)
*(volatile uint32_t*)0xa05f8054 = 0x00000000; // Same for progressive, resetting the offset gets us 2MB VRAM back after BootROM is done with it
// zero out framebuffer area
for(uint32_t pixel_or_two = 0; pixel_or_two < scan_area_size_bytes; pixel_or_two += 4)
{
*(uint32_t*)(0xa5000000 + pixel_or_two) = 0;
}
// re-enable video
*(volatile uint32_t*)0xa05f80e8 &= ~8;
*(volatile uint32_t*)0xa05f8044 |= 1;
}
}
// 800x600 @ 60Hz (4:3, CVT)
// Framebuffer: 565x600
// Horizontal scale: 0.70625x (exact)
// This won't work with all monitors due to Dreamcast sync polarity: Dreamcast
// sync polarity has to be -h -v or the video DAC won't work, while CVT asks for
// -h +v polarity. Some monitors don't care, some do; those that do may think
// this signal is 1440x576. This is why most of these modes are following DMT
// standards, since DMT modes can be either -h -v or +h +v and it doesn't really
// matter which.
void STARTUP_800x600_VGA_CVT(uint8_t fbuffer_color_mode)
{
// Set global scale factors
STARTUP_video_params.video_scale = 800.0f / 565.0f;
STARTUP_video_params.video_scale_multiplier = 565.0f / 800.0f;
// Set global video output mode parameters
STARTUP_video_params.video_width = 800;
STARTUP_video_params.video_height = 600;
STARTUP_video_params.video_color_type = fbuffer_color_mode;
STARTUP_video_params.video_refresh_rate = 60;
// 1024 wide scaled to 723 wide (field)
// 800 wide scaled to 565 wide (visible frame)
uint32_t horiz_active_area = 565;
uint32_t vert_active_area = 600;
// {RGB0555, RGB565} = 2Bpp, {RGB888} = 3Bpp, {RGB0888} = 4Bpp
uint32_t bpp_mode_size = fbuffer_color_mode + 1 + (0x1 ^ ((fbuffer_color_mode & 0x1) | (fbuffer_color_mode >> 1))); // Add another 1 only if 0b00
// Set global framebuffer parameters
STARTUP_video_params.fb_width = horiz_active_area;
STARTUP_video_params.fb_height = vert_active_area;
STARTUP_video_params.fb_color_bytes = bpp_mode_size;
if(!cable_mode) // VGA 60Hz
{
*(volatile uint32_t*)0xa05f80e8 = 0x00160008;
*(volatile uint32_t*)0xa05f8044 = 0x00800000 | (fbuffer_color_mode << 2);
*(volatile uint32_t*)0xa05f804c = (horiz_active_area * bpp_mode_size) / 8; // for PVR to know active area width
*(volatile uint32_t*)0xa05f8040 = 0x00000000; // Border color in RGB0888 format (this mode has no border)
*(volatile uint32_t*)0xa05f805c = (1 << 20) | ((vert_active_area - 1) << 10) | (((horiz_active_area * bpp_mode_size) / 4) - 1); // progressive scan has a 1 since no lines are skipped
*(volatile uint32_t*)0xa05f80ec = 0x00000087; // ok: 135 ? 168, default: 0x000000a8 (horiz)
*(volatile uint32_t*)0xa05f80f0 = 0x00150015; // ok: 21 & 21 ? 40 & 40, default: 0x00280028 (vert)
*(volatile uint32_t*)0xa05f80c8 = 0x02bc0000; // ok: 700 ? 837, default: 0x03450000 (horiz)
*(volatile uint32_t*)0xa05f80cc = 0x0015026d; // ok: 21, 621 ? 21, 520, default: 0x00150208 (vert)
*(volatile uint32_t*)0xa05f80d0 = 0x00000100;
*(volatile uint32_t*)0xa05f80d4 = 0x008702bc; // ok: 135, 700 ? 126, 837, default: 0x007e0345 (horiz)
*(volatile uint32_t*)0xa05f80d8 = 0x026f02d2; // ok: 722x623 (723x624) ? 857x524 (858x525), default: 0x020c0359
*(volatile uint32_t*)0xa05f80dc = 0x0015026d; // ok: 21, 621 ? 40, 520, default: 0x00280208 (vert)
*(volatile uint32_t*)0xa05f80e0 = 0x0369a437; // ok: 13 (14,- 1), 666 (723 - 56,- 1), 4 (4:3), 55 (56,- 1) ? 15, 793, 3, 63, default: 0x03f1933f
uint32_t scan_area_size = horiz_active_area * vert_active_area;
uint32_t scan_area_size_bytes = scan_area_size * bpp_mode_size; // This will always be divisible by 4
// Reset framebuffer address
*(volatile uint32_t*)0xa05f8050 = 0x00000000; // BootROM sets this to 0x00200000 (framebuffer base is 0xa5000000 + this)
*(volatile uint32_t*)0xa05f8054 = 0x00000000; // Same for progressive, resetting the offset gets us 2MB VRAM back after BootROM is done with it
// zero out framebuffer area
for(uint32_t pixel_or_two = 0; pixel_or_two < scan_area_size_bytes; pixel_or_two += 4)
{
*(uint32_t*)(0xa5000000 + pixel_or_two) = 0;
}
// re-enable video
*(volatile uint32_t*)0xa05f80e8 &= ~8;
*(volatile uint32_t*)0xa05f8044 |= 1;
}
}
// 800x600 @ 60Hz (4:3, CVT) - PVR 32x32
// Framebuffer: 544x608
// Horizontal scale: 0.68x (exact)
// Same caveat as 800x600 VGA CVT.
// This mode has been shrunken by 11 columns and expanded by 8 rows for 32x32
// framebuffer compatibility. As a result, there may be 11 total columns of blank
// pixels on the horizontal sides, and 8 total rows of pixels cut off on the
// vertical sides.
void STARTUP_800x600_VGA_CVT_PVR(uint8_t fbuffer_color_mode)
{
// Set global scale factors
STARTUP_video_params.video_scale = 800.0f / 544.0f;
STARTUP_video_params.video_scale_multiplier = 544.0f / 800.0f;
// Set global video output mode parameters
STARTUP_video_params.video_width = 800;
STARTUP_video_params.video_height = 600;
STARTUP_video_params.video_color_type = fbuffer_color_mode;
STARTUP_video_params.video_refresh_rate = 60;
// 1024 wide scaled to 723 wide (field)
// 800 wide scaled to 565 wide (visible frame)
uint32_t horiz_active_area = 544;
uint32_t vert_active_area = 608;
// {RGB0555, RGB565} = 2Bpp, {RGB888} = 3Bpp, {RGB0888} = 4Bpp
uint32_t bpp_mode_size = fbuffer_color_mode + 1 + (0x1 ^ ((fbuffer_color_mode & 0x1) | (fbuffer_color_mode >> 1))); // Add another 1 only if 0b00
// Set global framebuffer parameters
STARTUP_video_params.fb_width = horiz_active_area;
STARTUP_video_params.fb_height = vert_active_area;
STARTUP_video_params.fb_color_bytes = bpp_mode_size;
if(!cable_mode) // VGA 60Hz
{
*(volatile uint32_t*)0xa05f80e8 = 0x00160008;
*(volatile uint32_t*)0xa05f8044 = 0x00800000 | (fbuffer_color_mode << 2);
*(volatile uint32_t*)0xa05f804c = (horiz_active_area * bpp_mode_size) / 8; // for PVR to know active area width
*(volatile uint32_t*)0xa05f8040 = 0x00000000; // Border color in RGB0888 format
*(volatile uint32_t*)0xa05f805c = (1 << 20) | ((vert_active_area - 1) << 10) | (((horiz_active_area * bpp_mode_size) / 4) - 1); // progressive scan has a 1 since no lines are skipped
// Push right by 11 (back border), so front porch side left by 0.. Which centers the image.
// Well, alright then.
*(volatile uint32_t*)0xa05f80ec = 0x00000092; // ok: 146 ? 168, default: 0x000000a8 (horiz)
*(volatile uint32_t*)0xa05f80f0 = 0x000d000d; // ok: 13 & 13 ? 40 & 40, default: 0x00280028 (vert)
*(volatile uint32_t*)0xa05f80c8 = 0x02bc0000; // ok: 700 ? 837, default: 0x03450000 (horiz)
*(volatile uint32_t*)0xa05f80cc = 0x000d026d; // ok: 13, 621 ? 21, 520, default: 0x00150208 (vert)
*(volatile uint32_t*)0xa05f80d0 = 0x00000100;
*(volatile uint32_t*)0xa05f80d4 = 0x008702bc; // ok: 135, 700 ? 126, 837, default: 0x007e0345 (horiz)
*(volatile uint32_t*)0xa05f80d8 = 0x026f02d2; // ok: 722x623 (723x624) ? 857x524 (858x525), default: 0x020c0359
*(volatile uint32_t*)0xa05f80dc = 0x000d026d; // ok: 13, 621 ? 40, 520, default: 0x00280208 (vert)
*(volatile uint32_t*)0xa05f80e0 = 0x0369a437; // ok: 13 (14,- 1), 666 (723 - 56,- 1), 4 (4:3), 55 (56,- 1) ? 15, 793, 3, 63, default: 0x03f1933f
uint32_t scan_area_size = horiz_active_area * vert_active_area;
uint32_t scan_area_size_bytes = scan_area_size * bpp_mode_size; // This will always be divisible by 4
// Reset framebuffer address
*(volatile uint32_t*)0xa05f8050 = 0x00000000; // BootROM sets this to 0x00200000 (framebuffer base is 0xa5000000 + this)
*(volatile uint32_t*)0xa05f8054 = 0x00000000; // Same for progressive, resetting the offset gets us 2MB VRAM back after BootROM is done with it
// zero out framebuffer area
for(uint32_t pixel_or_two = 0; pixel_or_two < scan_area_size_bytes; pixel_or_two += 4)
{
*(uint32_t*)(0xa5000000 + pixel_or_two) = 0;
}
// re-enable video
*(volatile uint32_t*)0xa05f80e8 &= ~8;
*(volatile uint32_t*)0xa05f8044 |= 1;
}
}
// 1024x768 @ 60Hz (4:3, DMT)
// Framebuffer: 425x768
// Horizontal scale: 0.415039063x
// This one actually uses negative/negative polarity, too!
void STARTUP_1024x768_VGA(uint8_t fbuffer_color_mode)
{
// Set global scale factors
STARTUP_video_params.video_scale = 1024.0f / 425.0f;
STARTUP_video_params.video_scale_multiplier = 425.0f / 1024.0f;
// Set global video output mode parameters
STARTUP_video_params.video_width = 1024;
STARTUP_video_params.video_height = 768;
STARTUP_video_params.video_color_type = fbuffer_color_mode;
STARTUP_video_params.video_refresh_rate = 60;
// 1344 wide scaled to 558 wide (field)
// 1024 wide scaled to 425 wide (visible frame)
uint32_t horiz_active_area = 425;
uint32_t vert_active_area = 768;
// {RGB0555, RGB565} = 2Bpp, {RGB888} = 3Bpp, {RGB0888} = 4Bpp
uint32_t bpp_mode_size = fbuffer_color_mode + 1 + (0x1 ^ ((fbuffer_color_mode & 0x1) | (fbuffer_color_mode >> 1))); // Add another 1 only if 0b00
// Set global framebuffer parameters
STARTUP_video_params.fb_width = horiz_active_area;
STARTUP_video_params.fb_height = vert_active_area;
STARTUP_video_params.fb_color_bytes = bpp_mode_size;
if(!cable_mode) // VGA 60Hz
{
*(volatile uint32_t*)0xa05f80e8 = 0x00160008;
*(volatile uint32_t*)0xa05f8044 = 0x00800000 | (fbuffer_color_mode << 2);
*(volatile uint32_t*)0xa05f804c = (horiz_active_area * bpp_mode_size) / 8; // for PVR to know active area width
*(volatile uint32_t*)0xa05f8040 = 0x00000000; // Border color in RGB0888 format (this mode has no border)
*(volatile uint32_t*)0xa05f805c = (1 << 20) | ((vert_active_area - 1) << 10) | (((horiz_active_area * bpp_mode_size) / 4) - 1); // progressive scan has a 1 since no lines are skipped
*(volatile uint32_t*)0xa05f80ec = 0x0000007b; // ok: 123 ? 168, default: 0x000000a8 (horiz)
*(volatile uint32_t*)0xa05f80f0 = 0x00230023; // ok: 35 & 35 ? 40 & 40, default: 0x00280028 (vert)
*(volatile uint32_t*)0xa05f80c8 = 0x02240000; // ok: 548 ? 837, default: 0x03450000 (horiz)
*(volatile uint32_t*)0xa05f80cc = 0x00230323; // ok: 35, 803 ? 21, 520, default: 0x00150208 (vert)
*(volatile uint32_t*)0xa05f80d0 = 0x00000100;
*(volatile uint32_t*)0xa05f80d4 = 0x007b0224; // ok: 123, 548 ? 126, 837, default: 0x007e0345 (horiz)
*(volatile uint32_t*)0xa05f80d8 = 0x0325022D; // ok: 557x805 (558x806) ? 857x524 (858x525), default: 0x020c0359
*(volatile uint32_t*)0xa05f80dc = 0x00230323; // ok: 35, 803 ? 40, 520, default: 0x00280208 (vert)
*(volatile uint32_t*)0xa05f80e0 = 0x035f5637; // ok: 13 (14,- 1), 501 (558 - 56,- 1), 6 (per DMT), 55 (56,- 1) ? 15, 793, 3, 63, default: 0x03f1933f
uint32_t scan_area_size = horiz_active_area * vert_active_area;
uint32_t scan_area_size_bytes = scan_area_size * bpp_mode_size; // This will always be divisible by 4
// Reset framebuffer address
*(volatile uint32_t*)0xa05f8050 = 0x00000000; // BootROM sets this to 0x00200000 (framebuffer base is 0xa5000000 + this)
*(volatile uint32_t*)0xa05f8054 = 0x00000000; // Same for progressive, resetting the offset gets us 2MB VRAM back after BootROM is done with it
// zero out framebuffer area
for(uint32_t pixel_or_two = 0; pixel_or_two < scan_area_size_bytes; pixel_or_two += 4)
{
*(uint32_t*)(0xa5000000 + pixel_or_two) = 0;
}
// re-enable video
*(volatile uint32_t*)0xa05f80e8 &= ~8;
*(volatile uint32_t*)0xa05f8044 |= 1;
}
}
// 1024x768 @ 60Hz (4:3, DMT) - PVR 32x32
// Framebuffer: 416x768
// Horizontal scale: 0.40625x (exact)
// This mode has been shrunken by 9 columns for 32x32 framebuffer compatibility.
// As a result, there may be 9 total columns of blank pixels on the horizontal
// sides.
void STARTUP_1024x768_VGA_PVR(uint8_t fbuffer_color_mode)
{
// Set global scale factors
STARTUP_video_params.video_scale = 1024.0f / 416.0f;
STARTUP_video_params.video_scale_multiplier = 416.0f / 1024.0f;
// Set global video output mode parameters
STARTUP_video_params.video_width = 1024;
STARTUP_video_params.video_height = 768;
STARTUP_video_params.video_color_type = fbuffer_color_mode;
STARTUP_video_params.video_refresh_rate = 60;
// 1344 wide scaled to 558 wide (field)
// 1024 wide scaled to 425 wide (visible frame)
uint32_t horiz_active_area = 416;
uint32_t vert_active_area = 768;
// {RGB0555, RGB565} = 2Bpp, {RGB888} = 3Bpp, {RGB0888} = 4Bpp
uint32_t bpp_mode_size = fbuffer_color_mode + 1 + (0x1 ^ ((fbuffer_color_mode & 0x1) | (fbuffer_color_mode >> 1))); // Add another 1 only if 0b00
// Set global framebuffer parameters
STARTUP_video_params.fb_width = horiz_active_area;
STARTUP_video_params.fb_height = vert_active_area;
STARTUP_video_params.fb_color_bytes = bpp_mode_size;
if(!cable_mode) // VGA 60Hz
{
*(volatile uint32_t*)0xa05f80e8 = 0x00160008;
*(volatile uint32_t*)0xa05f8044 = 0x00800000 | (fbuffer_color_mode << 2);
*(volatile uint32_t*)0xa05f804c = (horiz_active_area * bpp_mode_size) / 8; // for PVR to know active area width
*(volatile uint32_t*)0xa05f8040 = 0x00000000; // Border color in RGB0888 format
*(volatile uint32_t*)0xa05f805c = (1 << 20) | ((vert_active_area - 1) << 10) | (((horiz_active_area * bpp_mode_size) / 4) - 1); // progressive scan has a 1 since no lines are skipped