-
Notifications
You must be signed in to change notification settings - Fork 12
/
modeline.cpp
917 lines (746 loc) · 31.1 KB
/
modeline.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
/**************************************************************
modeline.cpp - Modeline generation and scoring routines
---------------------------------------------------------
Switchres Modeline generation engine for emulation
License GPL-2.0+
Copyright 2010-2021 Chris Kennedy, Antonio Giner,
Alexandre Wodarczyk, Gil Delescluse
**************************************************************/
#include <stdio.h>
#include <string.h>
#include <cstddef>
#include "modeline.h"
#include "log.h"
#define max(a,b)({ __typeof__ (a) _a = (a);__typeof__ (b) _b = (b);_a > _b ? _a : _b; })
#define min(a,b)({ __typeof__ (a) _a = (a);__typeof__ (b) _b = (b);_a < _b ? _a : _b; })
//============================================================
// PROTOTYPES
//============================================================
int get_line_params(modeline *mode, monitor_range *range, int char_size);
int scale_into_range (int value, int lower_limit, int higher_limit);
int scale_into_range (double value, double lower_limit, double higher_limit);
int scale_into_aspect (int source_res, int tot_res, double original_monitor_aspect, double users_monitor_aspect, double *best_diff);
int stretch_into_range(double vfreq, monitor_range *range, double borders, bool interlace_allowed, double *interlace);
int total_lines_for_yres(int yres, double vfreq, monitor_range *range, double borders, double interlace);
double max_vfreq_for_yres (int yres, monitor_range *range, double borders, double interlace);
//============================================================
// modeline_create
//============================================================
int modeline_create(modeline *s_mode, modeline *t_mode, monitor_range *range, generator_settings *cs)
{
double vfreq_real = 0;
double interlace = 1;
double doublescan = 1;
double scan_factor = 1;
int x_scale = 0;
int y_scale = 0;
int v_scale = 0;
double x_fscale = 0;
double y_fscale = 0;
double v_fscale = 0;
double x_diff = 0;
double y_diff = 0;
double v_diff = 0;
double y_ratio = 0;
double borders = 0;
int rotation = s_mode->type & MODE_ROTATED;
double source_aspect = rotation? 1.0 / (STANDARD_CRT_ASPECT) : (STANDARD_CRT_ASPECT);
t_mode->result.weight = 0;
// ≈≈≈ Vertical refresh ≈≈≈
// try to fit vertical frequency into current range
v_scale = scale_into_range(t_mode->vfreq, range->vfreq_min, range->vfreq_max);
if (!v_scale && (t_mode->type & V_FREQ_EDITABLE))
{
t_mode->vfreq = t_mode->vfreq < range->vfreq_min? range->vfreq_min : range->vfreq_max;
v_scale = 1;
}
else if (v_scale != 1 && !(t_mode->type & V_FREQ_EDITABLE))
{
t_mode->result.weight |= R_OUT_OF_RANGE;
return -1;
}
// ≈≈≈ Vertical resolution ≈≈≈
// try to fit active lines in the progressive range first
if (range->progressive_lines_min && (!t_mode->interlace || (t_mode->type & SCAN_EDITABLE)))
y_scale = scale_into_range(t_mode->vactive, range->progressive_lines_min, range->progressive_lines_max);
// if not possible, try to fit in the interlaced range, if any
if (!y_scale && range->interlaced_lines_min && cs->interlace && (t_mode->interlace || (t_mode->type & SCAN_EDITABLE)))
{
y_scale = scale_into_range(t_mode->vactive, range->interlaced_lines_min, range->interlaced_lines_max);
interlace = 2;
}
// if we succeeded, let's see if we can apply integer scaling
if (y_scale == 1 || (y_scale > 1 && (t_mode->type & Y_RES_EDITABLE)))
{
// check if we should apply doublescan
if (cs->doublescan && y_scale % 2 == 0)
{
y_scale /= 2;
doublescan = 0.5;
}
scan_factor = interlace * doublescan;
// Calculate top border in case of multi-standard consumer TVs
if (cs->v_shift_correct)
borders = (range->progressive_lines_max - t_mode->vactive * y_scale / interlace) * (1.0 / range->hfreq_min) / 2;
// calculate expected achievable refresh for this height
vfreq_real = min(t_mode->vfreq * v_scale, max_vfreq_for_yres(t_mode->vactive * y_scale, range, borders, scan_factor));
if (vfreq_real != t_mode->vfreq * v_scale && !(t_mode->type & V_FREQ_EDITABLE))
{
t_mode->result.weight |= R_OUT_OF_RANGE;
return -1;
}
// calculate the ratio that our scaled yres represents with respect to the original height
y_ratio = double(t_mode->vactive) * y_scale / s_mode->vactive;
int y_source_scaled = s_mode->vactive * floor(y_ratio);
// if our original height doesn't fit the target height, we're forced to stretch
if (!y_source_scaled)
t_mode->result.weight |= R_RES_STRETCH;
// otherwise we try to perform integer scaling
else
{
// exclude lcd ranges from raw border computation
if (t_mode->type & V_FREQ_EDITABLE && range->progressive_lines_max - range->progressive_lines_min > 0)
{
// calculate y borders considering physical lines (instead of logical resolution)
int tot_yres = total_lines_for_yres(t_mode->vactive * y_scale, vfreq_real, range, borders, scan_factor);
int tot_source = total_lines_for_yres(y_source_scaled, t_mode->vfreq * v_scale, range, borders, scan_factor);
y_diff = tot_yres > tot_source?double(tot_yres % tot_source) / tot_yres * 100:0;
// we penalize for the logical lines we need to add in order to meet the user's lower active lines limit
int y_min = interlace == 2?range->interlaced_lines_min:range->progressive_lines_min;
int tot_rest = (y_min >= y_source_scaled / doublescan)? y_min % int(y_source_scaled / doublescan):0;
y_diff += double(tot_rest) / tot_yres * 100;
}
else
y_diff = double((t_mode->vactive * y_scale) % y_source_scaled) / (t_mode->vactive * y_scale) * 100;
// we save the integer ratio between source and target resolutions, this will be used for prescaling
y_scale = floor(y_ratio);
// now if the borders obtained are low enough (< 10%) we'll finally apply integer scaling
// otherwise we'll stretch the original resolution over the target one
if (!(y_ratio >= 1.0 && y_ratio < 16.0 && y_diff < 10.0))
t_mode->result.weight |= R_RES_STRETCH;
}
}
// otherwise, check if we're allowed to apply fractional scaling
else if (t_mode->type & Y_RES_EDITABLE)
t_mode->result.weight |= R_RES_STRETCH;
// if there's nothing we can do, we're out of range
else
{
t_mode->result.weight |= R_OUT_OF_RANGE;
return -1;
}
// ≈≈≈ Horizontal resolution ≈≈≈
// make the best possible adjustment of xres depending on what happened in the previous steps
// let's start with the SCALED case
if (!(t_mode->result.weight & R_RES_STRETCH))
{
// apply integer scaling to yres
if (t_mode->type & Y_RES_EDITABLE) t_mode->vactive *= y_scale;
// if we can, let's apply the same scaling to both directions
if (t_mode->type & X_RES_EDITABLE)
{
x_scale = cs->scale_proportional? y_scale : 1;
double aspect_corrector = max(1.0f, cs->monitor_aspect / source_aspect);
t_mode->hactive = normalize(double(t_mode->hactive) * double(x_scale) * aspect_corrector, cs->pixel_precision? 1 : 8);
}
// otherwise, try to get the best out of our current xres
else
{
x_scale = t_mode->hactive / s_mode->hactive;
// if the source width fits our xres, try applying integer scaling
if (x_scale)
{
x_scale = scale_into_aspect(s_mode->hactive, t_mode->hactive, source_aspect, cs->monitor_aspect, &x_diff);
if (x_diff > 15.0 && t_mode->hactive < cs->super_width)
t_mode->result.weight |= R_RES_STRETCH;
}
// otherwise apply fractional scaling
else
t_mode->result.weight |= R_RES_STRETCH;
}
}
// if the result was fractional scaling in any of the previous steps, deal with it
if (t_mode->result.weight & R_RES_STRETCH)
{
if (t_mode->type & Y_RES_EDITABLE)
{
// always try to use the interlaced range first if it exists, for better resolution
t_mode->vactive = stretch_into_range(t_mode->vfreq * v_scale, range, borders, cs->interlace, &interlace);
// check in case we couldn't achieve the desired refresh
vfreq_real = min(t_mode->vfreq * v_scale, max_vfreq_for_yres(t_mode->vactive, range, borders, interlace));
}
// check if we can create a normal aspect resolution
if (t_mode->type & X_RES_EDITABLE)
t_mode->hactive = max(t_mode->hactive, normalize(STANDARD_CRT_ASPECT * t_mode->vactive, cs->pixel_precision? 1 : 8));
// calculate integer scale for prescaling
x_scale = max(1, scale_into_aspect(s_mode->hactive, t_mode->hactive, source_aspect, cs->monitor_aspect, &x_diff));
y_scale = max(1, floor(double(t_mode->vactive) / s_mode->vactive));
scan_factor = interlace;
doublescan = 1;
}
x_fscale = double(t_mode->hactive) / s_mode->hactive * source_aspect / cs->monitor_aspect;
y_fscale = double(t_mode->vactive) / s_mode->vactive;
v_fscale = vfreq_real / s_mode->vfreq;
v_diff = (vfreq_real / v_scale) - s_mode->vfreq;
if (fabs(v_diff) > cs->refresh_tolerance)
t_mode->result.weight |= R_V_FREQ_OFF;
// ≈≈≈ Modeline generation ≈≈≈
// compute new modeline if we are allowed to
if (t_mode->type & V_FREQ_EDITABLE)
{
double margin = 0;
double vblank_lines = 0;
double vvt_ini = 0;
double interlace_incr = !cs->interlace_force_even && interlace == 2? 0.5 : 0;
// Get resulting refresh
t_mode->vfreq = vfreq_real;
// Get total vertical lines
vvt_ini = total_lines_for_yres(t_mode->vactive, t_mode->vfreq, range, borders, scan_factor) + interlace_incr;
// Calculate horizontal frequency
t_mode->hfreq = t_mode->vfreq * vvt_ini;
horizontal_values:
// Fill horizontal part of modeline
get_line_params(t_mode, range, cs->pixel_precision? 1 : 8);
// Calculate pixel clock
t_mode->pclock = t_mode->htotal * t_mode->hfreq;
if (t_mode->pclock <= cs->pclock_min)
{
if (t_mode->type & X_RES_EDITABLE)
{
x_scale *= 2;
x_fscale *= 2;
t_mode->hactive *= 2;
goto horizontal_values;
}
else
{
t_mode->result.weight |= R_OUT_OF_RANGE;
return -1;
}
}
// Vertical blanking
t_mode->vtotal = vvt_ini * scan_factor;
vblank_lines = round_near(t_mode->hfreq * (range->vertical_blank + borders)) + interlace_incr;
margin = (t_mode->vtotal - t_mode->vactive - vblank_lines * scan_factor) / (cs->v_shift_correct? 1 : 2);
double v_front_porch = margin + t_mode->hfreq * range->vfront_porch * scan_factor + interlace_incr;
int (*pf_round)(double) = interlace == 2? (cs->interlace_force_even? round_near_even : round_near_odd) : round_near;
t_mode->vbegin = t_mode->vactive + max(pf_round(v_front_porch), 1);
t_mode->vend = t_mode->vbegin + max(round_near(t_mode->hfreq * range->vsync_pulse * scan_factor), 1);
// Recalculate final vfreq
t_mode->vfreq = (t_mode->hfreq / t_mode->vtotal) * scan_factor;
t_mode->hsync = range->hsync_polarity;
t_mode->vsync = range->vsync_polarity;
t_mode->interlace = interlace == 2? 1 : 0;
t_mode->doublescan = doublescan == 1? 0 : 1;
}
// finally, store result
t_mode->result.scan_penalty = (s_mode->interlace != t_mode->interlace? 1 : 0) + (s_mode->doublescan != t_mode->doublescan? 1 : 0);
t_mode->result.x_scale = t_mode->result.weight & R_RES_STRETCH || t_mode->hactive >= cs->super_width ? x_fscale : (double)x_scale;
t_mode->result.y_scale = t_mode->result.weight & R_RES_STRETCH? y_fscale : (double)y_scale;
t_mode->result.v_scale = v_fscale;
t_mode->result.x_diff = x_diff;
t_mode->result.y_diff = y_diff;
t_mode->result.v_diff = v_diff;
return 0;
}
//============================================================
// get_line_params
//============================================================
int get_line_params(modeline *mode, monitor_range *range, int char_size)
{
int hhi, hhf, hht;
int hh, hs, he, ht;
double line_time, char_time, new_char_time;
double hfront_porch_min, hsync_pulse_min, hback_porch_min;
hfront_porch_min = range->hfront_porch * .90;
hsync_pulse_min = range->hsync_pulse * .90;
hback_porch_min = range->hback_porch * .90;
line_time = 1 / mode->hfreq * 1000000;
hh = round(mode->hactive / char_size);
hs = he = ht = 1;
do {
char_time = line_time / (hh + hs + he + ht);
if (hs * char_time < hfront_porch_min ||
fabs((hs + 1) * char_time - range->hfront_porch) < fabs(hs * char_time - range->hfront_porch))
hs++;
if (he * char_time < hsync_pulse_min ||
fabs((he + 1) * char_time - range->hsync_pulse) < fabs(he * char_time - range->hsync_pulse))
he++;
if (ht * char_time < hback_porch_min ||
fabs((ht + 1) * char_time - range->hback_porch) < fabs(ht * char_time - range->hback_porch))
ht++;
new_char_time = line_time / (hh + hs + he + ht);
} while (new_char_time != char_time);
hhi = (hh + hs) * char_size;
hhf = (hh + hs + he) * char_size;
hht = (hh + hs + he + ht) * char_size;
mode->hbegin = hhi;
mode->hend = hhf;
mode->htotal = hht;
return 0;
}
//============================================================
// scale_into_range
//============================================================
int scale_into_range (int value, int lower_limit, int higher_limit)
{
int scale = 1;
while (value * scale < lower_limit) scale ++;
if (value * scale <= higher_limit)
return scale;
else
return 0;
}
//============================================================
// scale_into_range
//============================================================
int scale_into_range (double value, double lower_limit, double higher_limit)
{
int scale = 1;
while (value * scale < lower_limit) scale ++;
if (value * scale <= higher_limit)
return scale;
else
return 0;
}
//============================================================
// scale_into_aspect
//============================================================
int scale_into_aspect (int source_res, int tot_res, double original_monitor_aspect, double users_monitor_aspect, double *best_diff)
{
int scale = 1, best_scale = 1;
double diff = 0;
*best_diff = 0;
while (source_res * scale <= tot_res)
{
diff = fabs(1.0 - (users_monitor_aspect / (double(tot_res) / double(source_res * scale) * original_monitor_aspect))) * 100.0;
if (diff < *best_diff || *best_diff == 0)
{
*best_diff = diff;
best_scale = scale;
}
scale ++;
}
return best_scale;
}
//============================================================
// stretch_into_range
//============================================================
int stretch_into_range(double vfreq, monitor_range *range, double borders, bool interlace_allowed, double *interlace)
{
int yres, lower_limit;
if (range->interlaced_lines_min && interlace_allowed)
{
yres = range->interlaced_lines_max;
lower_limit = range->interlaced_lines_min;
*interlace = 2;
}
else
{
yres = range->progressive_lines_max;
lower_limit = range->progressive_lines_min;
}
while (yres > lower_limit && max_vfreq_for_yres(yres, range, borders, *interlace) < vfreq)
yres -= 8;
return yres;
}
//============================================================
// total_lines_for_yres
//============================================================
int total_lines_for_yres(int yres, double vfreq, monitor_range *range, double borders, double interlace)
{
int vvt = max(yres / interlace + round_near(vfreq * yres / (interlace * (1.0 - vfreq * (range->vertical_blank + borders))) * (range->vertical_blank + borders)), 1);
while ((vfreq * vvt < range->hfreq_min) && (vfreq * (vvt + 1) < range->hfreq_max)) vvt++;
return vvt;
}
//============================================================
// max_vfreq_for_yres
//============================================================
double max_vfreq_for_yres (int yres, monitor_range *range, double borders, double interlace)
{
return range->hfreq_max / (yres / interlace + round_near(range->hfreq_max * (range->vertical_blank + borders)));
}
//============================================================
// modeline_print
//============================================================
char * modeline_print(modeline *mode, char *modeline, int flags)
{
char label[48]={'\x00'};
char params[192]={'\x00'};
if (flags & MS_LABEL)
sprintf(label, "\"%dx%d_%d%s %.6fKHz %.6fHz\"", mode->hactive, mode->vactive, mode->refresh, mode->interlace?"i":"", mode->hfreq/1000, mode->vfreq);
if (flags & MS_LABEL_SDL)
sprintf(label, "\"%dx%d_%.6f\"", mode->hactive, mode->vactive, mode->vfreq);
if (flags & MS_PARAMS)
sprintf(params, " %.6f %d %d %d %d %d %d %d %d %s %s %s %s", double(mode->pclock)/1000000.0, mode->hactive, mode->hbegin, mode->hend, mode->htotal, mode->vactive, mode->vbegin, mode->vend, mode->vtotal,
mode->interlace?"interlace":"", mode->doublescan?"doublescan":"", mode->hsync?"+hsync":"-hsync", mode->vsync?"+vsync":"-vsync");
sprintf(modeline, "%s%s", label, params);
return modeline;
}
//============================================================
// modeline_result
//============================================================
char * modeline_result(modeline *mode, char *result)
{
log_verbose(" rng(%d): ", mode->range);
if (mode->result.weight & R_OUT_OF_RANGE)
sprintf(result, " out of range");
else
sprintf(result, "%4d x%4d_%3.6f%s%s %3.6f [%s] scale(%.3f, %.3f, %.3f) diff(%.3f, %.3f, %.3f)",
mode->hactive, mode->vactive, mode->vfreq, mode->interlace?"i":"p", mode->doublescan?"d":"", mode->hfreq/1000, mode->result.weight & R_RES_STRETCH?"fract":"integ",
mode->result.x_scale, mode->result.y_scale, mode->result.v_scale, mode->result.x_diff, mode->result.y_diff, mode->result.v_diff);
return result;
}
//============================================================
// modeline_compare
//============================================================
int modeline_compare(modeline *t, modeline *best)
{
bool vector = (t->hactive == (int)t->result.x_scale);
if (t->result.weight < best->result.weight)
return 1;
else if (t->result.weight <= best->result.weight)
{
double t_v_diff = fabs(t->result.v_diff);
double b_v_diff = fabs(best->result.v_diff);
if (t->result.weight & R_RES_STRETCH || vector)
{
double t_y_score = t->result.y_scale * (t->interlace?(2.0/3.0):1.0);
double b_y_score = best->result.y_scale * (best->interlace?(2.0/3.0):1.0);
if ((t_v_diff < b_v_diff) ||
((t_v_diff == b_v_diff) && (t_y_score > b_y_score)) ||
((t_v_diff == b_v_diff) && (t_y_score == b_y_score) && (t->result.x_scale > best->result.x_scale)))
return 1;
}
else
{
int t_y_score = t->result.y_scale + t->result.scan_penalty;
int b_y_score = best->result.y_scale + best->result.scan_penalty;
double xy_diff = roundf((t->result.x_diff + t->result.y_diff) * 100) / 100;
double best_xy_diff = roundf((best->result.x_diff + best->result.y_diff) * 100) / 100;
if ((t_y_score < b_y_score) ||
((t_y_score == b_y_score) && (xy_diff < best_xy_diff)) ||
((t_y_score == b_y_score) && (xy_diff == best_xy_diff) && (t->result.x_scale < best->result.x_scale)) ||
((t_y_score == b_y_score) && (xy_diff == best_xy_diff) && (t->result.x_scale == best->result.x_scale) && (t_v_diff < b_v_diff)))
return 1;
}
}
return 0;
}
//============================================================
// modeline_vesa_gtf
// Based on the VESA GTF spreadsheet by Andy Morrish 1/5/97
//============================================================
int modeline_vesa_gtf(modeline *m)
{
int C, M;
int v_sync_lines, v_porch_lines_min, v_front_porch_lines, v_back_porch_lines, v_sync_v_back_porch_lines, v_total_lines;
int h_sync_width_percent, h_sync_width_pixels, h_blanking_pixels, h_front_porch_pixels, h_total_pixels;
double v_freq, v_freq_est, v_freq_real, v_sync_v_back_porch;
double h_freq, h_period, h_period_real, h_ideal_blanking;
double pixel_freq, interlace;
// Check if there's a value defined for vfreq. We're assuming input vfreq is the total field vfreq regardless interlace
v_freq = m->vfreq? m->vfreq:double(m->refresh);
// These values are GTF defined defaults
v_sync_lines = 3;
v_porch_lines_min = 1;
v_front_porch_lines = v_porch_lines_min;
v_sync_v_back_porch = 550;
h_sync_width_percent = 8;
M = 128.0 / 256 * 600;
C = ((40 - 20) * 128.0 / 256) + 20;
// GTF calculation
interlace = m->interlace?0.5:0;
h_period = ((1.0 / v_freq) - (v_sync_v_back_porch / 1000000)) / ((double)m->height + v_front_porch_lines + interlace) * 1000000;
v_sync_v_back_porch_lines = round_near(v_sync_v_back_porch / h_period);
v_back_porch_lines = v_sync_v_back_porch_lines - v_sync_lines;
v_total_lines = m->height + v_front_porch_lines + v_sync_lines + v_back_porch_lines;
v_freq_est = (1.0 / h_period) / v_total_lines * 1000000;
h_period_real = h_period / (v_freq / v_freq_est);
v_freq_real = (1.0 / h_period_real) / v_total_lines * 1000000;
h_ideal_blanking = double(C - (M * h_period_real / 1000));
h_blanking_pixels = round_near(m->width * h_ideal_blanking /(100 - h_ideal_blanking) / (2 * 8)) * (2 * 8);
h_total_pixels = m->width + h_blanking_pixels;
pixel_freq = h_total_pixels / h_period_real * 1000000;
h_freq = 1000000 / h_period_real;
h_sync_width_pixels = round_near(h_sync_width_percent * h_total_pixels / 100 / 8) * 8;
h_front_porch_pixels = (h_blanking_pixels / 2) - h_sync_width_pixels;
// Results
m->hactive = m->width;
m->hbegin = m->hactive + h_front_porch_pixels;
m->hend = m->hbegin + h_sync_width_pixels;
m->htotal = h_total_pixels;
m->vactive = m->height;
m->vbegin = m->vactive + v_front_porch_lines;
m->vend = m->vbegin + v_sync_lines;
m->vtotal = v_total_lines;
m->hfreq = h_freq;
m->vfreq = v_freq_real;
m->pclock = pixel_freq;
m->hsync = 0;
m->vsync = 1;
return true;
}
//============================================================
// modeline_parse
//============================================================
int modeline_parse(const char *user_modeline, modeline *mode)
{
char modeline_txt[256]={'\x00'};
if (!strcmp(user_modeline, "auto"))
return false;
// Remove quotes
char *quote_start, *quote_end;
quote_start = strstr((char*)user_modeline, "\"");
if (quote_start)
{
quote_start++;
quote_end = strstr(quote_start, "\"");
if (!quote_end || *quote_end++ == 0)
return false;
user_modeline = quote_end;
}
// Get timing flags
mode->interlace = strstr(user_modeline, "interlace")?1:0;
mode->doublescan = strstr(user_modeline, "doublescan")?1:0;
mode->hsync = strstr(user_modeline, "+hsync")?1:0;
mode->vsync = strstr(user_modeline, "+vsync")?1:0;
// Get timing values
double pclock;
int e = sscanf(user_modeline, " %lf %d %d %d %d %d %d %d %d",
&pclock,
&mode->hactive, &mode->hbegin, &mode->hend, &mode->htotal,
&mode->vactive, &mode->vbegin, &mode->vend, &mode->vtotal);
if (e != 9)
{
log_error("Switchres: missing parameter in user modeline\n %s\n", user_modeline);
memset(mode, 0, sizeof(struct modeline));
return false;
}
// Calculate timings
mode->pclock = pclock * 1000000.0;
mode->hfreq = mode->pclock / mode->htotal;
mode->vfreq = mode->hfreq / mode->vtotal * (mode->interlace?2:1);
mode->refresh = mode->vfreq;
mode->width = mode->hactive;
mode->height = mode->vactive;
log_verbose("Switchres: user modeline %s\n", modeline_print(mode, modeline_txt, MS_FULL));
return true;
}
//============================================================
// modeline_to_monitor_range
//============================================================
int modeline_to_monitor_range(monitor_range *range, modeline *mode)
{
// If Vfreq range is empty, create it around the provided vfreq
if (range->vfreq_min == 0.0f) range->vfreq_min = mode->vfreq - 0.2;
if (range->vfreq_max == 0.0f) range->vfreq_max = mode->vfreq + 0.2;
// Make sure the range includes the target vfreq
if (mode->vfreq < range->vfreq_min || mode->vfreq > range->vfreq_max)
return 0;
double line_time = 1 / mode->hfreq;
double pixel_time = line_time / mode->htotal * 1000000;
double interlace_factor = mode->interlace? 0.5 : 1.0;
range->hfront_porch = pixel_time * (mode->hbegin - mode->hactive);
range->hsync_pulse = pixel_time * (mode->hend - mode->hbegin);
range->hback_porch = pixel_time * (mode->htotal - mode->hend);
// We floor the vertical fields to remove the half line from interlaced modes, because
// the modeline generator will add it automatically. Otherwise it would be added twice.
range->vfront_porch = line_time * floor((mode->vbegin - mode->vactive) * interlace_factor);
range->vsync_pulse = line_time * floor((mode->vend - mode->vbegin) * interlace_factor);
range->vback_porch = line_time * floor((mode->vtotal - mode->vend) * interlace_factor);
range->vertical_blank = range->vfront_porch + range->vsync_pulse + range->vback_porch;
range->hsync_polarity = mode->hsync;
range->vsync_polarity = mode->vsync;
range->progressive_lines_min = mode->interlace? 0 : mode->vactive;
range->progressive_lines_max = mode->interlace? 0 : mode->vactive;
range->interlaced_lines_min = mode->interlace? mode->vactive : 0;
range->interlaced_lines_max= mode->interlace? mode->vactive : 0;
range->hfreq_min = range->vfreq_min * mode->vtotal * interlace_factor;
range->hfreq_max = range->vfreq_max * mode->vtotal * interlace_factor;
return 1;
}
//============================================================
// modeline_adjust
//============================================================
int modeline_adjust(modeline *mode, double hfreq_max, generator_settings *cs)
{
// If input values are out of range, they are fixed within range and returned in the cs struct.
// H size ajdustment, valid values 0.5-2.0
if (cs->h_size != 1.0f)
{
if (cs->h_size > 2.0f)
cs->h_size = 2.0f;
else if (cs->h_size < 0.5f)
cs->h_size = 0.5f;
monitor_range range;
memset(&range, 0, sizeof(monitor_range));
modeline_to_monitor_range(&range, mode);
range.hfront_porch /= cs->h_size;
range.hback_porch /= cs->h_size;
modeline_create(mode, mode, &range, cs);
}
// H shift adjustment, positive or negative value
if (cs->h_shift != 0)
{
if (cs->h_shift >= mode->hbegin - mode->hactive)
cs->h_shift = mode->hbegin - mode->hactive - 1;
else if (cs->h_shift <= mode->hend - mode->htotal)
cs->h_shift = mode->hend - mode->htotal + 1;
mode->hbegin -= cs->h_shift;
mode->hend -= cs->h_shift;
}
// V shift adjustment, positive or negative value
if (cs->v_shift != 0)
{
int vactive = mode->vactive;
int vbegin = mode->vbegin;
int vend = mode->vend;
int vtotal = mode->vtotal;
if (mode->interlace)
{
vactive >>= 1;
vbegin >>= 1;
vend >>= 1;
vtotal >>= 1;
}
int v_front_porch = vbegin - vactive;
int v_back_porch = vend - vtotal;
int max_vtotal = hfreq_max / mode->vfreq;
int border = max_vtotal - vtotal;
int padding = 0;
// v_shift positive
if (cs->v_shift >= v_front_porch)
{
int v_front_porch_ex = v_front_porch + border;
if (cs->v_shift >= v_front_porch_ex)
cs->v_shift = v_front_porch_ex - 1;
padding = cs->v_shift - v_front_porch + 1;
vbegin += padding;
vend += padding;
vtotal += padding;
}
// v_shift negative
else if (cs->v_shift <= v_back_porch + 1)
{
int v_back_porch_ex = v_back_porch - border;
if (cs->v_shift <= v_back_porch_ex + 1)
cs->v_shift = v_back_porch_ex + 2;
padding = -(cs->v_shift - v_back_porch - 2);
vtotal += padding;
}
vbegin -= cs->v_shift;
vend -= cs->v_shift;
if (mode->interlace)
{
vbegin = (vbegin << 1) | (mode->vbegin & 1);
vend = (vend << 1) | (mode->vend & 1);
vtotal = (vtotal << 1) | (mode->vtotal & 1);
}
mode->vbegin = vbegin;
mode->vend = vend;
mode->vtotal = vtotal;
if (padding != 0)
{
mode->hfreq = mode->vfreq * mode->vtotal / (mode->interlace? 2.0 : 1.0);
monitor_range range;
memset(&range, 0, sizeof(monitor_range));
modeline_to_monitor_range(&range, mode);
monitor_show_range(&range);
modeline_create(mode, mode, &range, cs);
}
}
return 0;
}
//============================================================
// modeline_is_different
//============================================================
int modeline_is_different(modeline *n, modeline *p)
{
// Remove on last fields in modeline comparison
return memcmp(n, p, offsetof(struct modeline, vfreq));
}
//============================================================
// modeline_copy_timings
//============================================================
void modeline_copy_timings(modeline *n, modeline *p)
{
// Only copy relevant timing fields
memcpy(n, p, offsetof(struct modeline, width));
}
//============================================================
// monitor_fill_vesa_gtf
//============================================================
int monitor_fill_vesa_gtf(monitor_range *range, const char *max_lines)
{
int lines = 0;
sscanf(max_lines, "vesa_%d", &lines);
if (!lines)
return 0;
int i = 0;
if (lines >= 480)
i += monitor_fill_vesa_range(&range[i], 384, 480);
if (lines >= 600)
i += monitor_fill_vesa_range(&range[i], 480, 600);
if (lines >= 768)
i += monitor_fill_vesa_range(&range[i], 600, 768);
if (lines >= 1024)
i += monitor_fill_vesa_range(&range[i], 768, 1024);
return i;
}
//============================================================
// monitor_fill_vesa_range
//============================================================
int monitor_fill_vesa_range(monitor_range *range, int lines_min, int lines_max)
{
modeline mode;
memset(&mode, 0, sizeof(modeline));
mode.width = real_res(STANDARD_CRT_ASPECT * lines_max);
mode.height = lines_max;
mode.refresh = 60;
range->vfreq_min = 50;
range->vfreq_max = 65;
modeline_vesa_gtf(&mode);
modeline_to_monitor_range(range, &mode);
range->progressive_lines_min = lines_min;
range->hfreq_min = mode.hfreq - 500;
range->hfreq_max = mode.hfreq + 500;
monitor_show_range(range);
return 1;
}
//============================================================
// round_near
//============================================================
int round_near(double number)
{
return number < 0.0 ? ceil(number - 0.5) : floor(number + 0.5);
}
//============================================================
// round_near_odd
//============================================================
int round_near_odd(double number)
{
return int(ceil(number)) % 2 == 0? floor(number) : ceil(number);
}
//============================================================
// round_near_even
//============================================================
int round_near_even(double number)
{
return int(ceil(number)) % 2 == 1? floor(number) : ceil(number);
}
//============================================================
// normalize
//============================================================
int normalize(int a, int b)
{
int c, d;
c = a % b;
d = a / b;
if (c) d++;
return d * b;
}
//============================================================
// real_res
//============================================================
int real_res(int x) {return (int) (x / 8) * 8;}