forked from hchunhui/tiny386
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvga.c
More file actions
2319 lines (2168 loc) · 85.6 KB
/
vga.c
File metadata and controls
2319 lines (2168 loc) · 85.6 KB
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
/*
* Dummy VGA device
*
* Copyright (c) 2003-2017 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <inttypes.h>
#include <assert.h>
#include "vga.h"
#include "pci.h"
#ifdef BUILD_ESP32
#include "esp_attr.h"
#else
#define IRAM_ATTR
#endif
#ifdef BUILD_ESP32
void *pcmalloc(long size);
#else
#define pcmalloc malloc
#endif
//#define DEBUG_VBE
//#define DEBUG_VGA_REG
#define MSR_COLOR_EMULATION 0x01
#define MSR_PAGE_SELECT 0x20
#define ST01_V_RETRACE 0x08
#define ST01_DISP_ENABLE 0x01
#define VBE_DISPI_INDEX_ID 0x0
#define VBE_DISPI_INDEX_XRES 0x1
#define VBE_DISPI_INDEX_YRES 0x2
#define VBE_DISPI_INDEX_BPP 0x3
#define VBE_DISPI_INDEX_ENABLE 0x4
#define VBE_DISPI_INDEX_BANK 0x5
#define VBE_DISPI_INDEX_VIRT_WIDTH 0x6
#define VBE_DISPI_INDEX_VIRT_HEIGHT 0x7
#define VBE_DISPI_INDEX_X_OFFSET 0x8
#define VBE_DISPI_INDEX_Y_OFFSET 0x9
#define VBE_DISPI_INDEX_VIDEO_MEMORY_64K 0xa
#define VBE_DISPI_INDEX_NB 0xb
#define VBE_DISPI_ID0 0xB0C0
#define VBE_DISPI_ID1 0xB0C1
#define VBE_DISPI_ID2 0xB0C2
#define VBE_DISPI_ID3 0xB0C3
#define VBE_DISPI_ID4 0xB0C4
#define VBE_DISPI_ID5 0xB0C5
#define VBE_DISPI_DISABLED 0x00
#define VBE_DISPI_ENABLED 0x01
#define VBE_DISPI_GETCAPS 0x02
#define VBE_DISPI_8BIT_DAC 0x20
#define VBE_DISPI_LFB_ENABLED 0x40
#define VBE_DISPI_NOCLEARMEM 0x80
#define FB_ALLOC_ALIGN (1 << 20)
#define MAX_TEXT_WIDTH 132
#define MAX_TEXT_HEIGHT 60
struct FBDevice {
/* the following is set by the device */
int width;
int height;
int stride; /* current stride in bytes */
uint8_t *fb_data; /* current pointer to the pixel data */
};
struct VGAState {
FBDevice *fb_dev;
int graphic_mode;
uint32_t cursor_blink_time;
int cursor_visible_phase;
uint32_t retrace_time;
int retrace_phase;
uint8_t *vga_ram;
int vga_ram_size;
uint8_t sr_index;
uint8_t sr[8];
uint8_t gr_index;
uint8_t gr[16];
uint8_t ar_index;
uint8_t ar[21];
int ar_flip_flop;
uint8_t cr_index;
uint8_t cr[256]; /* CRT registers */
uint8_t msr; /* Misc Output Register */
uint8_t fcr; /* Feature Control Register */
uint8_t st00; /* status 0 */
uint8_t st01; /* status 1 */
uint8_t dac_state;
uint8_t dac_sub_index;
uint8_t dac_read_index;
uint8_t dac_write_index;
uint8_t dac_8bit;
uint8_t dac_cache[3]; /* used when writing */
uint8_t palette[768];
int32_t bank_offset;
uint32_t latch;
/* text mode state */
uint32_t last_palette[16];
#ifndef FULL_UPDATE
uint16_t last_ch_attr[MAX_TEXT_WIDTH * MAX_TEXT_HEIGHT];
#endif
uint32_t last_width;
uint32_t last_height;
uint16_t last_line_offset;
uint16_t last_start_addr;
uint16_t last_cursor_offset;
uint8_t last_cursor_start;
uint8_t last_cursor_end;
/* VBE extension */
uint16_t vbe_index;
uint16_t vbe_regs[VBE_DISPI_INDEX_NB];
uint32_t vbe_start_addr;
uint32_t vbe_line_offset;
#ifdef SCALE_3_2
uint8_t tmpbuf[720 * 3 * 2];
#endif
};
#ifdef BUILD_ESP32
#include "esp_private/system_internal.h"
static uint32_t get_uticks()
{
return esp_system_get_time();
}
#else
#include <time.h>
static uint32_t get_uticks()
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return ((uint32_t) ts.tv_sec * 1000000 +
(uint32_t) ts.tv_nsec / 1000);
}
#endif
static int after_eq(uint32_t a, uint32_t b)
{
return (a - b) < (1u << 31);
}
#if BPP == 32
static void vga_draw_glyph8(uint8_t *d, int linesize,
const uint8_t *font_ptr, int h,
uint32_t fgcol, uint32_t bgcol)
{
uint32_t font_data, xorcol;
xorcol = bgcol ^ fgcol;
do {
font_data = font_ptr[0];
((uint32_t *)d)[0] = (-((font_data >> 7)) & xorcol) ^ bgcol;
((uint32_t *)d)[1] = (-((font_data >> 6) & 1) & xorcol) ^ bgcol;
((uint32_t *)d)[2] = (-((font_data >> 5) & 1) & xorcol) ^ bgcol;
((uint32_t *)d)[3] = (-((font_data >> 4) & 1) & xorcol) ^ bgcol;
((uint32_t *)d)[4] = (-((font_data >> 3) & 1) & xorcol) ^ bgcol;
((uint32_t *)d)[5] = (-((font_data >> 2) & 1) & xorcol) ^ bgcol;
((uint32_t *)d)[6] = (-((font_data >> 1) & 1) & xorcol) ^ bgcol;
((uint32_t *)d)[7] = (-((font_data >> 0) & 1) & xorcol) ^ bgcol;
font_ptr += 4;
d += linesize;
} while (--h);
}
static void vga_draw_glyph9(uint8_t *d, int linesize,
const uint8_t *font_ptr, int h,
uint32_t fgcol, uint32_t bgcol,
int dup9)
{
uint32_t font_data, xorcol, v;
xorcol = bgcol ^ fgcol;
do {
font_data = font_ptr[0];
((uint32_t *)d)[0] = (-((font_data >> 7)) & xorcol) ^ bgcol;
((uint32_t *)d)[1] = (-((font_data >> 6) & 1) & xorcol) ^ bgcol;
((uint32_t *)d)[2] = (-((font_data >> 5) & 1) & xorcol) ^ bgcol;
((uint32_t *)d)[3] = (-((font_data >> 4) & 1) & xorcol) ^ bgcol;
((uint32_t *)d)[4] = (-((font_data >> 3) & 1) & xorcol) ^ bgcol;
((uint32_t *)d)[5] = (-((font_data >> 2) & 1) & xorcol) ^ bgcol;
((uint32_t *)d)[6] = (-((font_data >> 1) & 1) & xorcol) ^ bgcol;
v = (-((font_data >> 0) & 1) & xorcol) ^ bgcol;
((uint32_t *)d)[7] = v;
if (dup9)
((uint32_t *)d)[8] = v;
else
((uint32_t *)d)[8] = bgcol;
font_ptr += 4;
d += linesize;
} while (--h);
}
#elif BPP == 16
static void vga_draw_glyph8(uint8_t *d, int linesize,
const uint8_t *font_ptr, int h,
uint32_t fgcol, uint32_t bgcol)
{
uint32_t font_data, xorcol;
xorcol = bgcol ^ fgcol;
do {
font_data = font_ptr[0];
((uint16_t *)d)[0] = (-((font_data >> 7)) & xorcol) ^ bgcol;
((uint16_t *)d)[1] = (-((font_data >> 6) & 1) & xorcol) ^ bgcol;
((uint16_t *)d)[2] = (-((font_data >> 5) & 1) & xorcol) ^ bgcol;
((uint16_t *)d)[3] = (-((font_data >> 4) & 1) & xorcol) ^ bgcol;
((uint16_t *)d)[4] = (-((font_data >> 3) & 1) & xorcol) ^ bgcol;
((uint16_t *)d)[5] = (-((font_data >> 2) & 1) & xorcol) ^ bgcol;
((uint16_t *)d)[6] = (-((font_data >> 1) & 1) & xorcol) ^ bgcol;
((uint16_t *)d)[7] = (-((font_data >> 0) & 1) & xorcol) ^ bgcol;
font_ptr += 4;
d += linesize;
} while (--h);
}
static void vga_draw_glyph9(uint8_t *d, int linesize,
const uint8_t *font_ptr, int h,
uint32_t fgcol, uint32_t bgcol,
int dup9)
{
uint32_t font_data, xorcol, v;
xorcol = bgcol ^ fgcol;
do {
font_data = font_ptr[0];
((uint16_t *)d)[0] = ((-((font_data >> 7)) & xorcol) ^ bgcol);
((uint16_t *)d)[1] = ((-((font_data >> 6) & 1) & xorcol) ^ bgcol);
((uint16_t *)d)[2] = ((-((font_data >> 5) & 1) & xorcol) ^ bgcol);
((uint16_t *)d)[3] = ((-((font_data >> 4) & 1) & xorcol) ^ bgcol);
((uint16_t *)d)[4] = ((-((font_data >> 3) & 1) & xorcol) ^ bgcol);
((uint16_t *)d)[5] = ((-((font_data >> 2) & 1) & xorcol) ^ bgcol);
((uint16_t *)d)[6] = ((-((font_data >> 1) & 1) & xorcol) ^ bgcol);
v = (-((font_data >> 0) & 1) & xorcol) ^ bgcol;
((uint16_t *)d)[7] = v;
if (dup9)
((uint16_t *)d)[8] = v;
else
((uint16_t *)d)[8] = bgcol;
font_ptr += 4;
d += linesize;
} while (--h);
}
static inline uint32_t c69(uint16_t c)
{
// 0000 0000 0000 rrrr rggg gggb bbbb
// 0000 0rrr rr00 0ggg ggg0 000b bbbb
return (c & 0x1f) | ((c & 0x7e0) << 4) | ((c & 0xf800) << 7);
}
static inline uint16_t c96(uint32_t c)
{
// 0000 0rrr rr00 0ggg ggg0 000b bbbb
// 0000 0000 0000 rrrr rggg gggb bbbb
uint16_t t = (c & 0x1f) | ((c & 0x7e00) >> 4) | ((c & 0x7c0000) >> 7);
#ifdef BUILD_ESP32
return (t << 8) | (t >> 8);
#else
return t;
#endif
}
static void scale_3_2(uint8_t *dst, int dst_stride, uint8_t *src, int w)
{
const static int shift[4][4] = {
{ 2, 0, 1, 0 },
{ 1, 2, 0, 0 },
{ 0, 0, 2, 1 },
{ 0, 1, 0, 2 }
};
int ww = w / 3 * 2;
int idx = 0;
for (int j = 0; j < 2; j++, idx ^= 2,
#ifdef SWAPXY
dst += BPP / 8
#else
dst += dst_stride
#endif
) {
uint8_t *dst1 = dst;
uint8_t *src1 = src + (j * w) * (BPP / 8);
for (int k = 0; k < ww; k++, idx ^= 1) {
int kk = k / 2 * 3 + (k & 1);
uint16_t *p0 = (uint16_t *) (src1 + kk * (BPP / 8));
uint16_t *p1 = p0 + 1;
uint16_t *p2 = p0 + w;
uint16_t *p3 = p1 + w;
int sh0 = shift[idx][0];
int sh1 = shift[idx][1];
int sh2 = shift[idx][2];
int sh3 = shift[idx][3];
*(uint16_t *)dst1 = c96(((c69(*p0) << sh0) + (c69(*p1) << sh1) +
(c69(*p2) << sh2) + (c69(*p3) << sh3)) >> 3);
#ifdef SWAPXY
dst1 += dst_stride;
#else
dst1 += BPP / 8;
#endif
}
}
}
#else
#error "bad bpp"
#endif
static const uint8_t cursor_glyph[32] = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
};
#if BPP == 32
static inline int c6_to_8(int v)
{
int b;
v &= 0x3f;
b = v & 1;
return (v << 2) | (b << 1) | b;
}
static inline unsigned int rgb_to_pixel(unsigned int r, unsigned int g,
unsigned int b)
{
return (r << 16) | (g << 8) | b;
}
static int update_palette256(VGAState *s, uint32_t *palette)
{
int full_update, i;
uint32_t v, col;
full_update = 0;
v = 0;
for(i = 0; i < 256; i++) {
if (s->dac_8bit) {
col = rgb_to_pixel(s->palette[v],
s->palette[v + 1],
s->palette[v + 2]);
} else {
col = rgb_to_pixel(c6_to_8(s->palette[v]),
c6_to_8(s->palette[v + 1]),
c6_to_8(s->palette[v + 2]));
}
if (col != palette[i]) {
full_update = 1;
palette[i] = col;
}
v += 3;
}
return full_update;
}
static int update_palette16(VGAState *s, uint32_t *palette)
{
int full_update, i;
uint32_t v, col;
full_update = 0;
for(i = 0; i < 16; i++) {
v = s->ar[i];
if (s->ar[0x10] & 0x80)
v = ((s->ar[0x14] & 0xf) << 4) | (v & 0xf);
else
v = ((s->ar[0x14] & 0xc) << 4) | (v & 0x3f);
v = v * 3;
col = (c6_to_8(s->palette[v]) << 16) |
(c6_to_8(s->palette[v + 1]) << 8) |
c6_to_8(s->palette[v + 2]);
if (col != palette[i]) {
full_update = 1;
palette[i] = col;
}
}
return full_update;
}
#elif BPP == 16
static int update_palette256(VGAState *s, uint32_t *palette)
{
int full_update, i;
uint32_t v, col;
full_update = 0;
v = 0;
for(i = 0; i < 256; i++) {
if (s->dac_8bit) {
col = ((s->palette[v + 2] >> 3)) |
((s->palette[v + 1] >> 2) << 5) |
((s->palette[v] >> 3) << 11);
} else {
col = (s->palette[v + 2] >> 1) |
((s->palette[v + 1]) << 5) |
((s->palette[v] >> 1) << 11);
}
if (col != palette[i]) {
full_update = 1;
palette[i] = col;
}
v += 3;
}
return full_update;
}
static int update_palette16(VGAState *s, uint32_t *palette)
{
int full_update, i;
uint32_t v, col;
full_update = 0;
for(i = 0; i < 16; i++) {
v = s->ar[i];
if (s->ar[0x10] & 0x80)
v = ((s->ar[0x14] & 0xf) << 4) | (v & 0xf);
else
v = ((s->ar[0x14] & 0xc) << 4) | (v & 0x3f);
v = v * 3;
col = (s->palette[v + 2] >> 1) |
((s->palette[v + 1]) << 5) |
((s->palette[v] >> 1) << 11);
if (col != palette[i]) {
full_update = 1;
palette[i] = col;
}
}
return full_update;
}
#else
#error "bad bpp"
#endif
/* VGA CRT controller register indices */
#define VGA_CRTC_H_TOTAL 0
#define VGA_CRTC_H_DISP 1
#define VGA_CRTC_H_BLANK_START 2
#define VGA_CRTC_H_BLANK_END 3
#define VGA_CRTC_H_SYNC_START 4
#define VGA_CRTC_H_SYNC_END 5
#define VGA_CRTC_V_TOTAL 6
#define VGA_CRTC_OVERFLOW 7
#define VGA_CRTC_PRESET_ROW 8
#define VGA_CRTC_MAX_SCAN 9
#define VGA_CRTC_CURSOR_START 0x0A
#define VGA_CRTC_CURSOR_END 0x0B
#define VGA_CRTC_START_HI 0x0C
#define VGA_CRTC_START_LO 0x0D
#define VGA_CRTC_CURSOR_HI 0x0E
#define VGA_CRTC_CURSOR_LO 0x0F
#define VGA_CRTC_V_SYNC_START 0x10
#define VGA_CRTC_V_SYNC_END 0x11
#define VGA_CRTC_V_DISP_END 0x12
#define VGA_CRTC_OFFSET 0x13
#define VGA_CRTC_UNDERLINE 0x14
#define VGA_CRTC_V_BLANK_START 0x15
#define VGA_CRTC_V_BLANK_END 0x16
#define VGA_CRTC_MODE 0x17
#define VGA_CRTC_LINE_COMPARE 0x18
#define VGA_CRTC_REGS VGA_CRT_C
/* VGA sequencer register indices */
#define VGA_SEQ_RESET 0x00
#define VGA_SEQ_CLOCK_MODE 0x01
#define VGA_SEQ_PLANE_WRITE 0x02
#define VGA_SEQ_CHARACTER_MAP 0x03
#define VGA_SEQ_MEMORY_MODE 0x04
/* VGA sequencer register bit masks */
#define VGA_SR01_CHAR_CLK_8DOTS 0x01 /* bit 0: character clocks 8 dots wide are generated */
#define VGA_SR01_SCREEN_OFF 0x20 /* bit 5: Screen is off */
#define VGA_SR02_ALL_PLANES 0x0F /* bits 3-0: enable access to all planes */
#define VGA_SR04_EXT_MEM 0x02 /* bit 1: allows complete mem access to 256K */
#define VGA_SR04_SEQ_MODE 0x04 /* bit 2: directs system to use a sequential addressing mode */
#define VGA_SR04_CHN_4M 0x08 /* bit 3: selects modulo 4 addressing for CPU access to display memory */
/* VGA graphics controller register indices */
#define VGA_GFX_SR_VALUE 0x00
#define VGA_GFX_SR_ENABLE 0x01
#define VGA_GFX_COMPARE_VALUE 0x02
#define VGA_GFX_DATA_ROTATE 0x03
#define VGA_GFX_PLANE_READ 0x04
#define VGA_GFX_MODE 0x05
#define VGA_GFX_MISC 0x06
#define VGA_GFX_COMPARE_MASK 0x07
#define VGA_GFX_BIT_MASK 0x08
/* VGA graphics controller bit masks */
#define VGA_GR06_GRAPHICS_MODE 0x01
static bool vbe_enabled(VGAState *s)
{
return s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED;
}
/*
* Sanity check vbe register writes.
*
* As we don't have a way to signal errors to the guest in the bochs
* dispi interface we'll go adjust the registers to the closest valid
* value.
*/
static void vbe_fixup_regs(VGAState *s)
{
uint16_t *r = s->vbe_regs;
uint32_t bits, linelength, maxy, offset;
if (!vbe_enabled(s)) {
/* vbe is turned off -- nothing to do */
return;
}
/* check depth */
switch (r[VBE_DISPI_INDEX_BPP]) {
case 4:
case 8:
case 16:
case 24:
case 32:
bits = r[VBE_DISPI_INDEX_BPP];
break;
case 15:
bits = 16;
break;
default:
bits = r[VBE_DISPI_INDEX_BPP] = 8;
break;
}
/* check width */
r[VBE_DISPI_INDEX_XRES] &= ~7u;
if (r[VBE_DISPI_INDEX_XRES] == 0) {
r[VBE_DISPI_INDEX_XRES] = 8;
}
// if (r[VBE_DISPI_INDEX_XRES] > VBE_DISPI_MAX_XRES) {
// r[VBE_DISPI_INDEX_XRES] = VBE_DISPI_MAX_XRES;
// }
r[VBE_DISPI_INDEX_VIRT_WIDTH] &= ~7u;
// if (r[VBE_DISPI_INDEX_VIRT_WIDTH] > VBE_DISPI_MAX_XRES) {
// r[VBE_DISPI_INDEX_VIRT_WIDTH] = VBE_DISPI_MAX_XRES;
// }
if (r[VBE_DISPI_INDEX_VIRT_WIDTH] < r[VBE_DISPI_INDEX_XRES]) {
r[VBE_DISPI_INDEX_VIRT_WIDTH] = r[VBE_DISPI_INDEX_XRES];
}
/* check height */
linelength = r[VBE_DISPI_INDEX_VIRT_WIDTH] * bits / 8;
// maxy = s->vbe_size / linelength;
if (r[VBE_DISPI_INDEX_YRES] == 0) {
r[VBE_DISPI_INDEX_YRES] = 1;
}
// if (r[VBE_DISPI_INDEX_YRES] > VBE_DISPI_MAX_YRES) {
// r[VBE_DISPI_INDEX_YRES] = VBE_DISPI_MAX_YRES;
// }
// if (r[VBE_DISPI_INDEX_YRES] > maxy) {
// r[VBE_DISPI_INDEX_YRES] = maxy;
// }
/* check offset */
// if (r[VBE_DISPI_INDEX_X_OFFSET] > VBE_DISPI_MAX_XRES) {
// r[VBE_DISPI_INDEX_X_OFFSET] = VBE_DISPI_MAX_XRES;
// }
// if (r[VBE_DISPI_INDEX_Y_OFFSET] > VBE_DISPI_MAX_YRES) {
// r[VBE_DISPI_INDEX_Y_OFFSET] = VBE_DISPI_MAX_YRES;
// }
offset = r[VBE_DISPI_INDEX_X_OFFSET] * bits / 8;
offset += r[VBE_DISPI_INDEX_Y_OFFSET] * linelength;
// if (offset + r[VBE_DISPI_INDEX_YRES] * linelength > s->vbe_size) {
// r[VBE_DISPI_INDEX_Y_OFFSET] = 0;
// offset = r[VBE_DISPI_INDEX_X_OFFSET] * bits / 8;
// if (offset + r[VBE_DISPI_INDEX_YRES] * linelength > s->vbe_size) {
// r[VBE_DISPI_INDEX_X_OFFSET] = 0;
// offset = 0;
// }
// }
/* update vga state */
// r[VBE_DISPI_INDEX_VIRT_HEIGHT] = maxy;
s->vbe_line_offset = linelength;
s->vbe_start_addr = offset / 4;
}
static void vbe_update_vgaregs(VGAState *s)
{
int h, shift_control;
if (!vbe_enabled(s)) {
/* vbe is turned off -- nothing to do */
return;
}
/* graphic mode + memory map 1 */
s->gr[VGA_GFX_MISC] = (s->gr[VGA_GFX_MISC] & ~0x0c) | 0x04 |
VGA_GR06_GRAPHICS_MODE;
s->cr[VGA_CRTC_MODE] |= 3; /* no CGA modes */
s->cr[VGA_CRTC_OFFSET] = s->vbe_line_offset >> 3;
/* width */
s->cr[VGA_CRTC_H_DISP] =
(s->vbe_regs[VBE_DISPI_INDEX_XRES] >> 3) - 1;
/* height (only meaningful if < 1024) */
h = s->vbe_regs[VBE_DISPI_INDEX_YRES] - 1;
s->cr[VGA_CRTC_V_DISP_END] = h;
s->cr[VGA_CRTC_OVERFLOW] = (s->cr[VGA_CRTC_OVERFLOW] & ~0x42) |
((h >> 7) & 0x02) | ((h >> 3) & 0x40);
/* line compare to 1023 */
s->cr[VGA_CRTC_LINE_COMPARE] = 0xff;
s->cr[VGA_CRTC_OVERFLOW] |= 0x10;
s->cr[VGA_CRTC_MAX_SCAN] |= 0x40;
if (s->vbe_regs[VBE_DISPI_INDEX_BPP] == 4) {
shift_control = 0;
s->sr/*_vbe*/[VGA_SEQ_CLOCK_MODE] &= ~8; /* no double line */
} else {
shift_control = 2;
/* set chain 4 mode */
s->sr/*_vbe*/[VGA_SEQ_MEMORY_MODE] |= VGA_SR04_CHN_4M;
/* activate all planes */
s->sr/*_vbe*/[VGA_SEQ_PLANE_WRITE] |= VGA_SR02_ALL_PLANES;
}
s->gr[VGA_GFX_MODE] = (s->gr[VGA_GFX_MODE] & ~0x60) |
(shift_control << 5);
s->cr[VGA_CRTC_MAX_SCAN] &= ~0x9f; /* no double scan */
}
/* the text refresh is just for debugging and initial boot message, so
it is very incomplete */
static void vga_text_refresh(VGAState *s,
SimpleFBDrawFunc *redraw_func, void *opaque,
int full_update)
{
FBDevice *fb_dev = s->fb_dev;
int width, height, cwidth, cheight, cy, cx, x1, y1, width1, height1;
int cx_min, cx_max, dup9;
uint32_t ch_attr, line_offset, start_addr, ch_addr, ch_addr1, ch, cattr;
uint8_t *vga_ram, *font_ptr, *dst;
uint32_t fgcol, bgcol, cursor_offset, cursor_start, cursor_end;
uint32_t now = get_uticks();
if (after_eq(now, s->cursor_blink_time)) {
s->cursor_blink_time = now + 266666;
s->cursor_visible_phase = !s->cursor_visible_phase;
}
full_update = full_update || update_palette16(s, s->last_palette);
vga_ram = s->vga_ram;
const uint8_t *font_base[2];
uint32_t v = s->sr[0x3];
font_base[0] = vga_ram + (((v >> 4) & 1) | ((v << 1) & 6)) * 8192 * 4 + 2;
font_base[1] = vga_ram + (((v >> 5) & 1) | ((v >> 1) & 6)) * 8192 * 4 + 2;
line_offset = s->cr[0x13];
line_offset <<= 3;
start_addr = s->cr[0x0d] | (s->cr[0x0c] << 8);
cheight = (s->cr[9] & 0x1f) + 1;
cwidth = 8;
if (!(s->sr[1] & 0x01))
cwidth++;
width = (s->cr[0x01] + 1);
height = s->cr[0x12] |
((s->cr[0x07] & 0x02) << 7) |
((s->cr[0x07] & 0x40) << 3);
height = (height + 1) / cheight;
width1 = width * cwidth;
height1 = height * cheight;
#ifdef SCALE_3_2
if (fb_dev->width * 3 / 2 < width1 || fb_dev->height * 3 / 2 < height1 ||
width > MAX_TEXT_WIDTH || height > MAX_TEXT_HEIGHT || cheight > 16)
return; /* not enough space */
x1 = (fb_dev->width * 3 / 2 - width1) / 3;
y1 = (fb_dev->height * 3 / 2 - height1) / 3;
full_update = 1;
#else
if (fb_dev->width < width1 || fb_dev->height < height1 ||
width > MAX_TEXT_WIDTH || height > MAX_TEXT_HEIGHT)
return; /* not enough space */
x1 = (fb_dev->width - width1) / 2;
y1 = (fb_dev->height - height1) / 2;
int stride = fb_dev->stride;
#endif
if (s->last_line_offset != line_offset ||
s->last_start_addr != start_addr ||
s->last_width != width ||
s->last_height != height) {
s->last_line_offset = line_offset;
s->last_start_addr = start_addr;
s->last_width = width;
s->last_height = height;
full_update = 1;
}
/* update cursor position */
cursor_offset = ((s->cr[0x0e] << 8) | s->cr[0x0f]) - start_addr;
cursor_start = s->cr[0xa];
cursor_end = s->cr[0xb];
if (cursor_offset != s->last_cursor_offset ||
cursor_start != s->last_cursor_start ||
cursor_end != s->last_cursor_end) {
#ifndef FULL_UPDATE
/* force refresh of characters with the cursor */
if (s->last_cursor_offset < MAX_TEXT_WIDTH * MAX_TEXT_HEIGHT)
s->last_ch_attr[s->last_cursor_offset] = -1;
if (cursor_offset < MAX_TEXT_WIDTH * MAX_TEXT_HEIGHT)
s->last_ch_attr[cursor_offset] = -1;
#endif
s->last_cursor_offset = cursor_offset;
s->last_cursor_start = cursor_start;
s->last_cursor_end = cursor_end;
}
ch_addr1 = (start_addr * 4);
cursor_offset = (start_addr + cursor_offset) * 4;
#if 0
printf("text refresh %dx%d font=%dx%d start_addr=0x%x line_offset=0x%x\n",
width, height, cwidth, cheight, start_addr, line_offset);
#endif
#ifdef SCALE_3_2
int cb = 6;
int nb = (width + cb - 1) / cb;
int cxbegin = 0;
int cxend = cb > width ? width : cb;
int stride = (cxend - cxbegin) * cwidth * (BPP / 8);
for (int b = 0; b < nb; b++)
{
int yt = 0;
int yy = 0;
ch_addr1 = (start_addr * 4) + cxbegin * 4;
#endif
for(cy = 0; cy < height; cy++) {
ch_addr = ch_addr1;
#ifdef SCALE_3_2
dst = s->tmpbuf + yt * stride;
#else
dst = fb_dev->fb_data + (y1 + cy * cheight) * stride + x1 * (BPP / 8);
#endif
cx_min = width;
cx_max = -1;
#ifdef SCALE_3_2
for(cx = 0; cx < cxend - cxbegin; cx++) {
#else
for(cx = 0; cx < width; cx++) {
#endif
ch_attr = *(uint16_t *)(vga_ram + (ch_addr & 0x1fffe));
#ifdef FULL_UPDATE
if (1) {
#else
if (full_update || ch_attr != s->last_ch_attr[cy * width + cx] || cursor_offset == ch_addr) {
s->last_ch_attr[cy * width + cx] = ch_attr;
#endif
cx_min = cx_min > cx ? cx : cx_min;
cx_max = cx_max < cx ? cx : cx_max;
ch = ch_attr & 0xff;
cattr = ch_attr >> 8;
font_ptr = font_base[(cattr >> 3) & 1] + 32 * 4 * ch;
bgcol = s->last_palette[cattr >> 4];
fgcol = s->last_palette[cattr & 0x0f];
if (cwidth == 8) {
vga_draw_glyph8(dst, stride, font_ptr, cheight,
fgcol, bgcol);
} else {
dup9 = 0;
if (ch >= 0xb0 && ch <= 0xdf && (s->ar[0x10] & 0x04))
dup9 = 1;
vga_draw_glyph9(dst, stride, font_ptr, cheight,
fgcol, bgcol, dup9);
}
/* cursor display */
if (cursor_offset == ch_addr && !(cursor_start & 0x20) && s->cursor_visible_phase) {
int line_start, line_last, h;
uint8_t *dst1;
line_start = cursor_start & 0x1f;
line_last = cursor_end & 0x1f;
if (line_last > cheight - 1)
line_last = cheight - 1;
if (line_last >= line_start && line_start < cheight) {
h = line_last - line_start + 1;
dst1 = dst + stride * line_start;
if (cwidth == 8) {
vga_draw_glyph8(dst1, stride,
cursor_glyph,
h, fgcol, bgcol);
} else {
vga_draw_glyph9(dst1, stride,
cursor_glyph,
h, fgcol, bgcol, 1);
}
}
}
}
ch_addr += 4;
dst += (BPP / 8) * cwidth;
}
#ifdef SCALE_3_2
int k;
for (k = 0; k < yt + cheight - 1; k += 3) {
#ifdef SWAPXY
int ii0 = (BPP / 8) * ((y1 + yy) + (x1 + cxbegin * cwidth * 2 / 3) * fb_dev->height);
#else
int ii0 = (BPP / 8) * ((y1 + yy) * fb_dev->width + x1 + cxbegin * cwidth * 2 / 3);
#endif
scale_3_2(fb_dev->fb_data + ii0, fb_dev->stride,
s->tmpbuf + k * stride, stride / (BPP / 8));
yy += 2;
}
yt = k - (yt + cheight - 1);
if (yt != 0) {
yt = 3 - yt;
memcpy(s->tmpbuf, s->tmpbuf + (k - 3) * stride, yt * stride);
}
#endif
// if (cx_max >= cx_min) {
// redraw_func(opaque,
// x1 + cx_min * cwidth, y1 + cy * cheight,
// (cx_max - cx_min + 1) * cwidth, cheight);
// }
ch_addr1 += line_offset;
}
#ifdef SCALE_3_2
cxbegin += cb;
cxend += cb;
if (cxend > width) cxend = width;
stride = (cxend - cxbegin) * cwidth * (BPP / 8);
}
#endif
redraw_func(opaque, 0, 0, fb_dev->width, fb_dev->height);
}
static void vga_graphic_refresh(VGAState *s,
SimpleFBDrawFunc *redraw_func, void *opaque,
int full_update)
{
FBDevice *fb_dev = s->fb_dev;
int w = (s->cr[0x01] + 1) * 8;
int h = s->cr[0x12] |
((s->cr[0x07] & 0x02) << 7) |
((s->cr[0x07] & 0x40) << 3);
h++;
int shift_control = (s->gr[0x05] >> 5) & 3;
int double_scan = (s->cr[0x09] >> 7);
int multi_scan, multi_run;
if (shift_control != 1) {
multi_scan = (((s->cr[0x09] & 0x1f) + 1) << double_scan) - 1;
} else {
/* in CGA modes, multi_scan is ignored */
/* XXX: is it correct ? */
multi_scan = double_scan;
}
multi_run = multi_scan;
uint32_t start_addr = s->cr[0x0d] | (s->cr[0x0c] << 8);
uint32_t line_offset = s->cr[0x13];
line_offset <<= 3;
uint32_t line_compare = s->cr[0x18] |
((s->cr[0x07] & 0x10) << 4) |
((s->cr[0x09] & 0x40) << 3);
if (vbe_enabled(s)) {
line_offset = s->vbe_line_offset;
start_addr = s->vbe_start_addr;
line_compare = 65535;
}
uint32_t addr1 = 4 * start_addr;
uint8_t *vram = s->vga_ram;
uint32_t palette[256];
int xdiv = 1;
int bpp = 4;
if (shift_control == 0 || shift_control == 1) {
update_palette16(s, palette);
if (s->sr[0x01] & 8) {
xdiv = 2;
if (shift_control == 1) // XXX
w *= 2;
}
} else {
if (!vbe_enabled(s)) {
update_palette256(s, palette);
xdiv = 2;
bpp = 8;
} else {
bpp = s->vbe_regs[VBE_DISPI_INDEX_BPP];
if (bpp == 8)
update_palette256(s, palette);
}
}
int y1 = 0;
int i0 = 0;
#ifdef SCALE_3_2
int hx = fb_dev->height * 3 / 2;
int wx = fb_dev->width * 3 / 2;
if (h < hx)
#ifdef SWAPXY
i0 += (hx - h) / 3 * (BPP / 8);
#else
i0 += (hx - h) / 3 * fb_dev->stride;
#endif
else
h = hx;
if (w < wx)
#ifdef SWAPXY
i0 += (wx - w) / 3 * fb_dev->stride;
#else
i0 += (wx - w) / 3 * (BPP / 8);
#endif
else
w = wx;
int yyt = 0;
int yy = 0;
#else
int hx = fb_dev->height;
int wx = fb_dev->width;
if (h < hx)
i0 += (hx - h) / 2 * fb_dev->stride;
else
h = hx;
if (w < wx)
i0 += (wx - w) / 2 * (BPP / 8);
else
w = wx;
#endif
for (int y = 0; y < h; y++) {
uint32_t addr = addr1;
if (!(s->cr[0x17] & 1)) {
int shift;
/* CGA compatibility handling */
shift = 14 + ((s->cr[0x17] >> 6) & 1);
addr = (addr & ~(1 << shift)) | ((y1 & 1) << shift);
}
if (!(s->cr[0x17] & 2)) {
addr = (addr & ~0x8000) | ((y1 & 2) << 14);
}
for (int x = 0; x < w; x++) {
int x1 = x / xdiv;
uint32_t color;
if (shift_control == 0) {
int k = ((vram[addr + 4 * (x1 >> 3)] >> (7 - (x1 & 7))) & 1) << 0;
k |= ((vram[addr + 4 * (x1 >> 3) + 1] >> (7 - (x1 & 7))) & 1) << 1;
k |= ((vram[addr + 4 * (x1 >> 3) + 2] >> (7 - (x1 & 7))) & 1) << 2;
k |= ((vram[addr + 4 * (x1 >> 3) + 3] >> (7 - (x1 & 7))) & 1) << 3;
color = palette[k];
} else if (shift_control == 1) {
int k = ((vram[addr + 4 * (x1 >> 3) + ((x1 & 4) >> 2)] >>
(6 - 2 * (x1 & 3))) & 3);
color = palette[k];
} else
#if BPP == 32
{
switch (bpp) {
case 8: {
int k = vram[addr + x1];
color = palette[k];
break;
}
case 15: {
int k = vram[addr + 2 * x1] | (vram[addr + 2 * x1 + 1] << 8);
int b = (k & ((1 << 5) - 1)) << 3;
int g = ((k >> 5) & ((1 << 5) - 1)) << 3;
int r = ((k >> 10) & ((1 << 5) - 1)) << 3;
color = b | (g << 8) | (r << 16);
break;
}