forked from MiSTer-devel/Main_MiSTer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
video.cpp
1529 lines (1328 loc) · 33.1 KB
/
video.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <linux/fb.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/vt.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "hardware.h"
#include "user_io.h"
#include "spi.h"
#include "cfg.h"
#include "file_io.h"
#include "menu.h"
#include "video.h"
#include "input.h"
#include "shmem.h"
#include "support.h"
#include "lib/imlib2/Imlib2.h"
#define FB_SIZE (1920*1080)
#define FB_ADDR (0x20000000 + (32*1024*1024)) // 512mb + 32mb(Core's fb)
/*
-- [2:0] : 011=8bpp(palette) 100=16bpp 101=24bpp 110=32bpp
-- [3] : 0=16bits 565 1=16bits 1555
-- [4] : 0=RGB 1=BGR (for 16/24/32 modes)
-- [5] : TBD
*/
#define FB_FMT_565 0b00100
#define FB_FMT_1555 0b01100
#define FB_FMT_888 0b00101
#define FB_FMT_8888 0b00110
#define FB_FMT_PAL8 0b00011
#define FB_FMT_RxB 0b10000
#define FB_EN 0x8000
#define FB_DV_LBRD 3
#define FB_DV_RBRD 6
#define FB_DV_UBRD 2
#define FB_DV_BBRD 2
static volatile uint32_t *fb_base = 0;
static int fb_enabled = 0;
static int fb_width = 0;
static int fb_height = 0;
static int fb_num = 0;
static int brd_x = 0;
static int brd_y = 0;
static int menu_bg = 0;
static int menu_bgn = 0;
struct vmode_t
{
uint32_t vpar[8];
double Fpix;
};
vmode_t vmodes[] =
{
{ { 1280, 110, 40, 220, 720, 5, 5, 20 }, 74.25 }, //0
{ { 1024, 24, 136, 160, 768, 3, 6, 29 }, 65 }, //1
{ { 720, 16, 62, 60, 480, 9, 6, 30 }, 27 }, //2
{ { 720, 12, 64, 68, 576, 5, 5, 39 }, 27 }, //3
{ { 1280, 48, 112, 248, 1024, 1, 3, 38 }, 108 }, //4
{ { 800, 40, 128, 88, 600, 1, 4, 23 }, 40 }, //5
{ { 640, 16, 96, 48, 480, 10, 2, 33 }, 25.175 }, //6
{ { 1280, 440, 40, 220, 720, 5, 5, 20 }, 74.25 }, //7
{ { 1920, 88, 44, 148, 1080, 4, 5, 36 }, 148.5 }, //8
{ { 1920, 528, 44, 148, 1080, 4, 5, 36 }, 148.5 }, //9
{ { 1366, 70, 143, 213, 768, 3, 3, 24 }, 85.5 }, //10
{ { 1024, 40, 104, 144, 600, 1, 3, 18 }, 48.96 }, //11
{ { 1920, 48, 32, 80, 1440, 2, 4, 38 }, 185.203 }, //12
{ { 2048, 48, 32, 80, 1536, 2, 4, 38 }, 209.318 }, //13
};
#define VMODES_NUM (sizeof(vmodes) / sizeof(vmodes[0]))
vmode_t tvmodes[] =
{
{{ 640, 30, 60, 70, 240, 4, 4, 14 }, 12.587 }, //NTSC 15K
{{ 640, 16, 96, 48, 480, 8, 4, 33 }, 25.175 }, //NTSC 31K
{{ 640, 30, 60, 70, 288, 6, 4, 14 }, 12.587 }, //PAL 15K
{{ 640, 16, 96, 48, 576, 2, 4, 42 }, 25.175 }, //PAL 31K
};
struct vmode_custom_t
{
uint32_t item[32];
double Fpix;
};
static vmode_custom_t v_cur = {}, v_def = {}, v_pal = {}, v_ntsc = {};
static int vmode_def = 0, vmode_pal = 0, vmode_ntsc = 0;
static uint32_t getPLLdiv(uint32_t div)
{
if (div & 1) return 0x20000 | (((div / 2) + 1) << 8) | (div / 2);
return ((div / 2) << 8) | (div / 2);
}
static int findPLLpar(double Fout, uint32_t *pc, uint32_t *pm, double *pko)
{
uint32_t c = 1;
while ((Fout*c) < 400) c++;
while (1)
{
double fvco = Fout*c;
uint32_t m = (uint32_t)(fvco / 50);
double ko = ((fvco / 50) - m);
fvco = ko + m;
fvco *= 50.f;
if (ko && (ko <= 0.05f || ko >= 0.95f))
{
printf("Fvco=%f, C=%d, M=%d, K=%f ", fvco, c, m, ko);
if (fvco > 1500.f)
{
printf("-> No exact parameters found\n");
return 0;
}
printf("-> K is outside allowed range\n");
c++;
}
else
{
*pc = c;
*pm = m;
*pko = ko;
return 1;
}
}
//will never reach here
return 0;
}
static void setPLL(double Fout, vmode_custom_t *v)
{
double Fpix;
double fvco, ko;
uint32_t m, c;
printf("Calculate PLL for %.4f MHz:\n", Fout);
if (!findPLLpar(Fout, &c, &m, &ko))
{
c = 1;
while ((Fout*c) < 400) c++;
fvco = Fout*c;
m = (uint32_t)(fvco / 50);
ko = ((fvco / 50) - m);
//Make sure K is in allowed range.
if (ko <= 0.05f)
{
ko = 0;
}
else if (ko >= 0.95f)
{
m++;
ko = 0;
}
}
uint32_t k = ko ? (uint32_t)(ko * 4294967296) : 1;
fvco = ko + m;
fvco *= 50.f;
Fpix = fvco / c;
printf("Fvco=%f, C=%d, M=%d, K=%f(%u) -> Fpix=%f\n", fvco, c, m, ko, k, Fpix);
v->item[9] = 4;
v->item[10] = getPLLdiv(m);
v->item[11] = 3;
v->item[12] = 0x10000;
v->item[13] = 5;
v->item[14] = getPLLdiv(c);
v->item[15] = 9;
v->item[16] = 2;
v->item[17] = 8;
v->item[18] = 7;
v->item[19] = 7;
v->item[20] = k;
v->Fpix = Fpix;
}
static char scaler_flt_cfg[1024] = { 0 };
static char new_scaler = 0;
static void setScaler()
{
fileTYPE f = {};
static char filename[1024];
uint32_t arc[4] = {};
for (int i = 0; i < 2; i++)
{
if (cfg.custom_aspect_ratio[i][0])
{
if (sscanf(cfg.custom_aspect_ratio[i], "%u:%u", &arc[i * 2], &arc[(i * 2) + 1]) != 2 || arc[i * 2] < 1 || arc[i * 2] > 4095 || arc[(i * 2) + 1] < 1 || arc[(i * 2) + 1] > 4095)
{
arc[(i * 2) + 0] = 0;
arc[(i * 2) + 1] = 0;
}
}
}
spi_uio_cmd_cont(UIO_SET_AR_CUST);
for (int i = 0; i < 4; i++) spi_w(arc[i]);
DisableIO();
if (!spi_uio_cmd_cont(UIO_SET_FLTNUM))
{
DisableIO();
return;
}
new_scaler = 1;
spi8(scaler_flt_cfg[0]);
DisableIO();
sprintf(filename, COEFF_DIR"/%s", scaler_flt_cfg + 1);
if (FileOpen(&f, filename))
{
//printf("Read scaler coefficients\n");
char *buf = (char*)malloc(f.size+1);
if (buf)
{
memset(buf, 0, f.size + 1);
int size;
if ((size = FileReadAdv(&f, buf, f.size)))
{
spi_uio_cmd_cont(UIO_SET_FLTCOEF);
char *end = buf + size;
char *pos = buf;
int phase = 0;
while (pos < end)
{
char *st = pos;
while ((pos < end) && *pos && (*pos != 10)) pos++;
*pos = 0;
while (*st == ' ' || *st == '\t' || *st == 13) st++;
if (*st == '#' || *st == ';' || !*st) pos++;
else
{
int c0, c1, c2, c3;
int n = sscanf(st, "%d,%d,%d,%d", &c0, &c1, &c2, &c3);
if (n == 4)
{
//printf(" phase %c-%02d: %4d,%4d,%4d,%4d\n", (phase >= 16) ? 'V' : 'H', phase % 16, c0, c1, c2, c3);
//printf("%03X: %03X %03X %03X %03X;\n",phase*4, c0 & 0x1FF, c1 & 0x1FF, c2 & 0x1FF, c3 & 0x1FF);
spi_w((c0 & 0x1FF) | (((phase * 4) + 0) << 9));
spi_w((c1 & 0x1FF) | (((phase * 4) + 1) << 9));
spi_w((c2 & 0x1FF) | (((phase * 4) + 2) << 9));
spi_w((c3 & 0x1FF) | (((phase * 4) + 3) << 9));
phase++;
if (phase >= 32) break;
}
}
}
DisableIO();
}
free(buf);
}
}
}
int video_get_scaler_flt()
{
return new_scaler ? scaler_flt_cfg[0] : -1;
}
char* video_get_scaler_coeff()
{
return scaler_flt_cfg + 1;
}
static char scaler_cfg[128] = { 0 };
void video_set_scaler_flt(int n)
{
scaler_flt_cfg[0] = (char)n;
FileSaveConfig(scaler_cfg, &scaler_flt_cfg, sizeof(scaler_flt_cfg));
spi_uio_cmd8(UIO_SET_FLTNUM, scaler_flt_cfg[0]);
spi_uio_cmd(UIO_SET_FLTCOEF);
}
void video_set_scaler_coeff(char *name)
{
strcpy(scaler_flt_cfg + 1, name);
FileSaveConfig(scaler_cfg, &scaler_flt_cfg, sizeof(scaler_flt_cfg));
setScaler();
user_io_send_buttons(1);
}
static void loadScalerCfg()
{
sprintf(scaler_cfg, "%s_scaler.cfg", user_io_get_core_name());
if (!FileLoadConfig(scaler_cfg, &scaler_flt_cfg, sizeof(scaler_flt_cfg) - 1) || scaler_flt_cfg[0]>4)
{
memset(scaler_flt_cfg, 0, sizeof(scaler_flt_cfg));
if (cfg.vfilter_default[0])
{
strcpy(scaler_flt_cfg+1, cfg.vfilter_default);
scaler_flt_cfg[0] = 1;
}
}
}
static char gamma_cfg[1024] = { 0 };
static char has_gamma = 0;
static void setGamma()
{
fileTYPE f = {};
static char filename[1024];
if (!spi_uio_cmd_cont(UIO_SET_GAMMA))
{
DisableIO();
return;
}
has_gamma = 1;
spi8(0);
DisableIO();
sprintf(filename, GAMMA_DIR"/%s", gamma_cfg + 1);
if (FileOpen(&f, filename))
{
char *buf = (char*)malloc(f.size+1);
if (buf)
{
memset(buf, 0, f.size + 1);
int size;
if ((size = FileReadAdv(&f, buf, f.size)))
{
spi_uio_cmd_cont(UIO_SET_GAMCURV);
char *end = buf + size;
char *pos = buf;
int index = 0;
while (pos < end)
{
char *st = pos;
while ((pos < end) && *pos && (*pos != 10)) pos++;
*pos = 0;
while (*st == ' ' || *st == '\t' || *st == 13) st++;
if (*st == '#' || *st == ';' || !*st) pos++;
else
{
int c0, c1, c2;
int n = sscanf(st, "%d,%d,%d", &c0, &c1, &c2);
if (n == 1)
{
c1 = c0;
c2 = c0;
n = 3;
}
if (n == 3)
{
spi_w((index << 8) | (c0 & 0xFF));
spi_w((index << 8) | (c1 & 0xFF));
spi_w((index << 8) | (c2 & 0xFF));
index++;
if (index >= 256) break;
}
}
}
DisableIO();
spi_uio_cmd8(UIO_SET_GAMMA, gamma_cfg[0]);
}
free(buf);
}
}
}
int video_get_gamma_en()
{
return has_gamma ? gamma_cfg[0] : -1;
}
char* video_get_gamma_curve()
{
return gamma_cfg + 1;
}
static char gamma_cfg_path[1024] = { 0 };
void video_set_gamma_en(int n)
{
gamma_cfg[0] = (char)n;
FileSaveConfig(gamma_cfg_path, &gamma_cfg, sizeof(gamma_cfg));
setGamma();
}
void video_set_gamma_curve(char *name)
{
strcpy(gamma_cfg + 1, name);
FileSaveConfig(gamma_cfg_path, &gamma_cfg, sizeof(gamma_cfg));
setGamma();
user_io_send_buttons(1);
}
static void loadGammaCfg()
{
sprintf(gamma_cfg_path, "%s_gamma.cfg", user_io_get_core_name());
if (!FileLoadConfig(gamma_cfg_path, &gamma_cfg, sizeof(gamma_cfg) - 1) || gamma_cfg[0]>1)
{
memset(gamma_cfg, 0, sizeof(gamma_cfg));
}
}
static char fb_reset_cmd[128] = {};
static void set_video(vmode_custom_t *v, double Fpix)
{
loadGammaCfg();
setGamma();
loadScalerCfg();
setScaler();
v_cur = *v;
vmode_custom_t v_fix = v_cur;
if (cfg.direct_video)
{
v_fix.item[2] = FB_DV_RBRD;
v_fix.item[4] = FB_DV_LBRD;
v_fix.item[1] += v_cur.item[2] - v_fix.item[2];
v_fix.item[1] += v_cur.item[4] - v_fix.item[4];
v_fix.item[6] = FB_DV_BBRD;
v_fix.item[8] = FB_DV_UBRD;;
v_fix.item[5] += v_cur.item[6] - v_fix.item[6];
v_fix.item[5] += v_cur.item[8] - v_fix.item[8];
}
printf("Send HDMI parameters:\n");
spi_uio_cmd_cont(UIO_SET_VIDEO);
printf("video: ");
for (int i = 1; i <= 8; i++)
{
spi_w(v_fix.item[i]);
printf("%d(%d), ", v_cur.item[i], v_fix.item[i]);
}
if(Fpix) setPLL(Fpix, &v_cur);
printf("\nPLL: ");
for (int i = 9; i < 21; i++)
{
printf("0x%X, ", v_cur.item[i]);
if (i & 1) spi_w(v_cur.item[i] | ((i == 9 && Fpix && cfg.vsync_adjust == 2 && !is_menu()) ? 0x8000 : 0));
else
{
spi_w(v_cur.item[i]);
spi_w(v_cur.item[i] >> 16);
}
}
printf("Fpix=%f\n", v_cur.Fpix);
DisableIO();
if (cfg.fb_size <= 1) cfg.fb_size = ((v_cur.item[1] * v_cur.item[5]) <= FB_SIZE) ? 1 : 2;
else if (cfg.fb_size == 3) cfg.fb_size = 2;
else if (cfg.fb_size > 4) cfg.fb_size = 4;
fb_width = v_cur.item[1] / cfg.fb_size;
fb_height = v_cur.item[5] / cfg.fb_size;
brd_x = cfg.vscale_border / cfg.fb_size;;
brd_y = cfg.vscale_border / cfg.fb_size;;
if (fb_enabled) video_fb_enable(1, fb_num);
sprintf(fb_reset_cmd, "echo %d %d %d %d %d >/sys/module/MiSTer_fb/parameters/mode", 8888, 1, fb_width, fb_height, fb_width * 4);
system(fb_reset_cmd);
}
static int parse_custom_video_mode(char* vcfg, vmode_custom_t *v)
{
int khz = 0;
int cnt = 0;
char *orig = vcfg;
while (*vcfg)
{
char *next;
if (cnt == 9 && v->item[0] == 1)
{
double Fpix = khz ? strtoul(vcfg, &next, 0)/1000.f : strtod(vcfg, &next);
if (vcfg == next || (Fpix < 2.f || Fpix > 300.f))
{
printf("Error parsing video_mode parameter: ""%s""\n", orig);
return -1;
}
setPLL(Fpix, v);
break;
}
uint32_t val = strtoul(vcfg, &next, 0);
if (vcfg == next || (*next != ',' && *next))
{
printf("Error parsing video_mode parameter: ""%s""\n", orig);
return -1;
}
if (!cnt && val >= 100)
{
v->item[cnt++] = 1;
khz = 1;
}
if (cnt < 32) v->item[cnt] = val;
if (*next == ',') next++;
vcfg = next;
cnt++;
}
if (cnt == 1)
{
printf("Set predefined video_mode to %d\n", v->item[0]);
return v->item[0];
}
if ((v->item[0] == 0 && cnt < 21) || (v->item[0] == 1 && cnt < 9))
{
printf("Incorrect amount of items in video_mode parameter: %d\n", cnt);
return -1;
}
if (v->item[0] > 1)
{
printf("Incorrect video_mode parameter\n");
return -1;
}
return -2;
}
static int store_custom_video_mode(char* vcfg, vmode_custom_t *v)
{
int ret = parse_custom_video_mode(vcfg, v);
if (ret == -2) return 1;
uint mode = (ret < 0) ? 0 : ret;
if (mode >= VMODES_NUM) mode = 0;
for (int i = 0; i < 8; i++) v->item[i + 1] = vmodes[mode].vpar[i];
setPLL(vmodes[mode].Fpix, v);
return ret >= 0;
}
static void fb_init()
{
if (!fb_base)
{
fb_base = (volatile uint32_t*)shmem_map(FB_ADDR, FB_SIZE * 4 * 3);
if (!fb_base)
{
printf("Unable to mmap FB!\n");
}
}
spi_uio_cmd16(UIO_SET_FBUF, 0);
}
void video_mode_load()
{
fb_init();
if (cfg.direct_video && cfg.vsync_adjust)
{
printf("Disabling vsync_adjust because of enabled direct video.\n");
cfg.vsync_adjust = 0;
}
if (cfg.direct_video)
{
int mode = cfg.menu_pal ? 2 : 0;
if (cfg.forced_scandoubler) mode++;
v_def.item[0] = mode;
for (int i = 0; i < 8; i++) v_def.item[i + 1] = tvmodes[mode].vpar[i];
setPLL(tvmodes[mode].Fpix, &v_def);
vmode_def = 1;
vmode_pal = 0;
vmode_ntsc = 0;
}
else
{
vmode_def = store_custom_video_mode(cfg.video_conf, &v_def);
vmode_pal = store_custom_video_mode(cfg.video_conf_pal, &v_pal);
vmode_ntsc = store_custom_video_mode(cfg.video_conf_ntsc, &v_ntsc);
}
set_video(&v_def, 0);
}
static int api1_5 = 0;
int hasAPI1_5()
{
return api1_5 || is_menu();
}
static uint32_t show_video_info(int force)
{
uint32_t ret = 0;
static uint16_t nres = 0;
spi_uio_cmd_cont(UIO_GET_VRES);
uint16_t res = spi_w(0);
if ((nres != res) || force)
{
if (nres != res) force = 0;
nres = res;
uint32_t width = spi_w(0) | (spi_w(0) << 16);
uint32_t height = spi_w(0) | (spi_w(0) << 16);
uint32_t htime = spi_w(0) | (spi_w(0) << 16);
uint32_t vtime = spi_w(0) | (spi_w(0) << 16);
uint32_t ptime = spi_w(0) | (spi_w(0) << 16);
uint32_t vtimeh = spi_w(0) | (spi_w(0) << 16);
DisableIO();
float vrate = 100000000;
if (vtime) vrate /= vtime; else vrate = 0;
float hrate = 100000;
if (htime) hrate /= htime; else hrate = 0;
float prate = width * 100;
prate /= ptime;
printf("\033[1;33mINFO: Video resolution: %u x %u%s, fHorz = %.1fKHz, fVert = %.1fHz, fPix = %.2fMHz\033[0m\n", width, height, (res & 0x100) ? "i" : "", hrate, vrate, prate);
printf("\033[1;33mINFO: Frame time (100MHz counter): VGA = %d, HDMI = %d\033[0m\n", vtime, vtimeh);
if (vtimeh) api1_5 = 1;
if (hasAPI1_5() && cfg.video_info)
{
static char str[128], res1[16], res2[16];
float vrateh = 100000000;
if (vtimeh) vrateh /= vtimeh; else vrateh = 0;
sprintf(res1, "%dx%d%s", width, height, (res & 0x100) ? "i" : "");
sprintf(res2, "%dx%d", v_cur.item[1], v_cur.item[5]);
sprintf(str, "%9s %6.2fKHz %4.1fHz\n" \
"\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\x81\n" \
"%9s %6.2fMHz %4.1fHz",
res1, hrate, vrate, res2, v_cur.Fpix, vrateh);
Info(str, cfg.video_info * 1000);
}
uint32_t scrh = v_cur.item[5];
if (scrh)
{
if (cfg.vscale_mode && height)
{
uint32_t div = 1 << (cfg.vscale_mode - 1);
uint32_t mag = (scrh*div) / height;
scrh = (height * mag) / div;
printf("Set vertical scaling to : %d\n", scrh);
spi_uio_cmd16(UIO_SETHEIGHT, scrh);
}
else if(cfg.vscale_border)
{
uint32_t border = cfg.vscale_border * 2;
if ((border + 100) > scrh) border = scrh - 100;
scrh -= border;
printf("Set max vertical resolution to : %d\n", scrh);
spi_uio_cmd16(UIO_SETHEIGHT, scrh);
}
else
{
spi_uio_cmd16(UIO_SETHEIGHT, 0);
}
}
uint32_t scrw = v_cur.item[1];
if (scrw)
{
if (cfg.vscale_border && !(cfg.vscale_mode && height))
{
uint32_t border = cfg.vscale_border * 2;
if ((border + 100) > scrw) border = scrw - 100;
scrw -= border;
printf("Set max horizontal resolution to : %d\n", scrw);
spi_uio_cmd16(UIO_SETWIDTH, scrw);
}
else
{
spi_uio_cmd16(UIO_SETWIDTH, 0);
}
}
if (vtime && vtimeh) ret = vtime;
minimig_set_adjust(2);
}
else
{
DisableIO();
}
return force ? 0 : ret;
}
void video_mode_adjust()
{
static int force = 0;
uint32_t vtime = show_video_info(force);
force = 0;
if (vtime && cfg.vsync_adjust && !is_menu())
{
printf("\033[1;33madjust_video_mode(%u): vsync_adjust=%d", vtime, cfg.vsync_adjust);
int adjust = 1;
vmode_custom_t *v = &v_def;
if (vmode_pal || vmode_ntsc)
{
if (vtime > 1800000)
{
if (vmode_pal)
{
printf(", using PAL mode");
v = &v_pal;
}
else
{
printf(", PAL mode cannot be used. Using predefined NTSC mode");
v = &v_ntsc;
adjust = 0;
}
}
else
{
if (vmode_ntsc)
{
printf(", using NTSC mode");
v = &v_ntsc;
}
else
{
printf(", NTSC mode cannot be used. Using predefined PAL mode");
v = &v_pal;
adjust = 0;
}
}
}
printf(".\033[0m\n");
double Fpix = 0;
if (adjust)
{
Fpix = 100 * (v->item[1] + v->item[2] + v->item[3] + v->item[4]) * (v->item[5] + v->item[6] + v->item[7] + v->item[8]);
Fpix /= vtime;
if (Fpix < 2.f || Fpix > 300.f)
{
printf("Estimated Fpix(%.4f MHz) is outside supported range. Canceling auto-adjust.\n", Fpix);
Fpix = 0;
}
uint32_t hz = 100000000 / vtime;
if (cfg.refresh_min && hz < cfg.refresh_min)
{
printf("Estimated frame rate (%d Hz) is less than MONITOR_HZ_MIN(%d Hz). Canceling auto-adjust.\n", hz, cfg.refresh_min);
Fpix = 0;
}
if (cfg.refresh_max && hz > cfg.refresh_max)
{
printf("Estimated frame rate (%d Hz) is more than MONITOR_HZ_MAX(%d Hz). Canceling auto-adjust.\n", hz, cfg.refresh_max);
Fpix = 0;
}
}
set_video(v, Fpix);
user_io_send_buttons(1);
force = 1;
}
}
void video_fb_enable(int enable, int n)
{
if (fb_base)
{
int res = spi_uio_cmd_cont(UIO_SET_FBUF);
if (res)
{
if (is_menu() && !enable && menu_bg)
{
enable = 1;
n = menu_bgn;
}
if (enable)
{
uint32_t fb_addr = FB_ADDR + (FB_SIZE * 4 * n) + (n ? 0 : 4096);
fb_num = n;
int xoff = 0, yoff = 0;
if (cfg.direct_video)
{
xoff = v_cur.item[4] - FB_DV_LBRD;
yoff = v_cur.item[8] - FB_DV_UBRD;
}
printf("Switch to HPS frame buffer\n");
spi_w((uint16_t)(FB_EN | FB_FMT_RxB | FB_FMT_8888)); // format, enable flag
spi_w((uint16_t)fb_addr); // base address low word
spi_w(fb_addr >> 16); // base address high word
spi_w(fb_width); // frame width
spi_w(fb_height); // frame height
spi_w(xoff); // scaled left
spi_w(xoff + v_cur.item[1] - 1); // scaled right
spi_w(yoff); // scaled top
spi_w(yoff + v_cur.item[5] - 1); // scaled bottom
spi_w(fb_width * 4); // stride
printf("HPS frame buffer: %dx%d, stride = %d bytes\n", fb_width, fb_height, fb_width * 4);
if (!fb_num)
{
system(fb_reset_cmd);
input_switch(0);
}
else
{
input_switch(1);
}
}
else
{
printf("Switch to core frame buffer\n");
spi_w(0); // enable flag
input_switch(1);
}
fb_enabled = enable;
}
else
{
printf("Core doesn't support HPS frame buffer\n");
input_switch(1);
}
DisableIO();
if (cfg.direct_video) set_vga_fb(enable);
if (is_menu()) user_io_8bit_set_status((fb_enabled && !fb_num) ? 0x160 : 0, 0x1E0);
}
}
int video_fb_state()
{
if (is_menu())
{
return fb_enabled && !fb_num;
}
return fb_enabled;
}
static void draw_checkers()
{
volatile uint32_t* buf = fb_base + (FB_SIZE*menu_bgn);
uint32_t col1 = 0x888888;
uint32_t col2 = 0x666666;
int sz = fb_width / 128;
for (int y = brd_y; y < fb_height - brd_y; y++)
{
int c1 = (y / sz) & 1;
int pos = y * fb_width;
for (int x = brd_x; x < fb_width - brd_x; x++)
{
int c2 = c1 ^ ((x / sz) & 1);
buf[pos + x] = c2 ? col2 : col1;
}
}
}
static void draw_hbars1()
{
volatile uint32_t* buf = fb_base + (FB_SIZE*menu_bgn);
int height = fb_height - 2 * brd_y;
int old_base = 0;
int gray = 255;
int sz = height / 7;
int stp = 0;
for (int y = brd_y; y < fb_height - brd_y; y++)
{
int pos = y * fb_width;
int base_color = ((7 * (y-brd_y)) / height) + 1;
if (old_base != base_color)
{
stp = sz;
old_base = base_color;
}
gray = 255 * stp / sz;
for (int x = brd_x; x < fb_width - brd_x; x++)
{
uint32_t color = 0;
if (base_color & 4) color |= gray;
if (base_color & 2) color |= gray << 8;
if (base_color & 1) color |= gray << 16;
buf[pos + x] = color;
}
stp--;
if (stp < 0) stp = 0;
}
}
static void draw_hbars2()
{
volatile uint32_t* buf = fb_base + (FB_SIZE*menu_bgn);
int height = fb_height - 2 * brd_y;
int width = fb_width - 2 * brd_x;
for (int y = brd_y; y < fb_height - brd_y; y++)
{
int pos = y * fb_width;
int base_color = ((14 * (y - brd_y)) / height);
int inv = base_color & 1;
base_color >>= 1;
base_color = (inv ? base_color : 6 - base_color) + 1;
for (int x = brd_x; x < fb_width - brd_x; x++)
{
int gray = (256 * (x - brd_x)) / width;
if (inv) gray = 255 - gray;
uint32_t color = 0;
if (base_color & 4) color |= gray;
if (base_color & 2) color |= gray << 8;
if (base_color & 1) color |= gray << 16;
buf[pos + x] = color;
}
}
}
static void draw_vbars1()
{
volatile uint32_t* buf = fb_base + (FB_SIZE*menu_bgn);
int width = fb_width - 2 * brd_x;
int sz = width / 7;
int stp = 0;
for (int y = brd_y; y < fb_height - brd_y; y++)
{
int pos = y * fb_width;
int old_base = 0;
int gray = 255;
for (int x = brd_x; x < fb_width - brd_x; x++)
{
int base_color = ((7 * (x - brd_x)) / width) + 1;
if (old_base != base_color)
{
stp = sz;
old_base = base_color;
}
gray = 255 * stp / sz;
uint32_t color = 0;
if (base_color & 4) color |= gray;
if (base_color & 2) color |= gray << 8;
if (base_color & 1) color |= gray << 16;
buf[pos + x] = color;
stp--;
if (stp < 0) stp = 0;
}
}
}
static void draw_vbars2()
{
volatile uint32_t* buf = fb_base + (FB_SIZE*menu_bgn);
int height = fb_height - 2 * brd_y;