-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgs.cpp
1621 lines (1387 loc) · 37.9 KB
/
gs.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
/*
VGATEST
Use at your own risk.
Copyright (C) 2019 Marco Bortolin
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <conio.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdarg.h>
#include "common.h"
#include "utils.h"
#include "gs.h"
#define VGA_ADDR ((uint8_t *) 0xa0000)
#define CGA_ADDR ((uint8_t *) 0xb8000)
int32_t SIN_ACOS[1024];
GfxScreen::GfxScreen()
{
m_error = e_none;
m_origMode = getBIOSMode();
if(!isVga()) {
m_error = e_notVgaDisplay;
return;
}
m_fontAddr = NULL;
m_fontHeight = 14;
m_videoMem = VGA_ADDR;
m_activeOffset = 0;
m_maxx = 0;
m_maxy = 0;
m_width = 0;
m_height = 0;
m_scanlines = 0;
m_pages = 0;
m_lineSize = 0;
m_pageSize = 0;
m_chained = 0;
m_modeName = NULL;
m_colors = 0;
m_crtc_addr = 0;
m_isr1_addr = 0;
m_overscanColor = 0;
m_putPixelFn = NULL;
m_getPixelFn = NULL;
m_clearFn = NULL;
m_drawTextFn = NULL;
// precompute the sin(arccos(x)) table for circles
for(int i=0;i<1024;i++) {
SIN_ACOS[i] = sin(acos((float)i/1024))*0x10000L;
}
}
GfxScreen::~GfxScreen()
{
}
void GfxScreen::setMode(int16_t mode)
{
m_error = e_none;
m_modeName = NULL;
m_videoMem = VGA_ADDR;
switch (mode)
{
case v_b320x200_04h: mode_b320x200_04h(); break;
case v_b640x200_06h: mode_b640x200_06h(); break;
case v_b320x200_0Dh: mode_b320x200_0Dh(); break;
case v_b640x200_0Eh: mode_b640x200_0Eh(); break;
case v_b640x350_0Fh: mode_b640x350_0Fh(); break;
case v_b640x350_10h: mode_b640x350_10h(); break;
case v_b640x480_12h: mode_b640x480_12h(); break;
case v_b320x200_13h: mode_b320x200_13h(); break;
case v_t160x120: mode_t160x120(); break;
case v_t296x220: mode_t296x220(); break;
case v_t256x256_Q: mode_t256x256_Q(); break;
case v_t320x200_Y: mode_t320x200_Y(); break;
case v_t320x240_X: mode_t320x240_X(); break;
case v_t320x400: mode_t320x400(); break;
case v_t360x270: mode_t360x270(); break;
case v_t360x360: mode_t360x360(); break;
case v_t360x480: mode_t360x480(); break;
case v_t400x300: mode_t400x300(); break;
default:
m_error = e_modeNotSupported;
break;
}
if (m_error != e_none) {
return;
}
m_fontAddr = getFont(m_fontHeight);
m_activeOffset = m_videoMem;
if(inp(MOR_READ) & 1) {
m_crtc_addr = CRTC_ADDR_COL;
m_isr1_addr = ISR1_ADDR_COL;
} else {
m_crtc_addr = CRTC_ADDR_MONO;
m_isr1_addr = ISR1_ADDR_MONO;
}
m_maxx = m_width - 1;
m_maxy = m_height - 1;
if(m_crtc_addr == CRTC_ADDR_COL) {
ACR_OUT_COL(ACR_OVERSCAN, m_overscanColor);
}
clear(color(c_black));
}
void GfxScreen::resetMode()
{
setBIOSMode(m_origMode);
m_width = 0;
m_height = 0;
m_scanlines = 0;
m_maxx = 0;
m_maxy = 0;
m_pages = 0;
m_lineSize = 0;
m_pageSize = 0;
m_chained = 0;
m_modeName = NULL;
m_crtc_addr = 0;
m_isr1_addr = 0;
m_putPixelFn = NULL;
m_getPixelFn = NULL;
m_clearFn = NULL;
m_drawTextFn = NULL;
}
void GfxScreen::clear_odd_even(int row, int lines, uint32_t color)
{
/* address section differs depending on odd/even scanline */
uint8_t *even_ptr = m_activeOffset;
uint8_t *odd_ptr = m_activeOffset + 0x2000;
while(lines--) {
int y = row + lines;
int bank_line = y / 2;
uint8_t *base;
if(y & 1) {
base = odd_ptr;
} else {
base = even_ptr;
}
fillLong(base + (m_lineSize * bank_line), color, m_lineSize/4);
}
}
void GfxScreen::clear1(int row, int lines, uint8_t color)
{
uint32_t c = 0;
if(color & 1) {
c = 0xffffffff;
}
clear_odd_even(row, lines, c);
}
void GfxScreen::clear2(int row, int lines, uint8_t color)
{
color &= 3;
uint32_t c = color | (color << 2) | (color << 4) | (color << 6);
c = (c << 8) | c;
c = (c << 16) | c;
clear_odd_even(row, lines, c);
}
void GfxScreen::clear4(int row, int lines, uint8_t color)
{
setPlanarRWMode(0,0);
// enable all planes
SEQ_OUT(SEQ_MAPMASK, 0x0F);
// CPU data to all planes will be
// replaced by set/reset value
GCR_OUT(GCR_EN_SETRESET, 0x0F);
// set/reset value
GCR_OUT(GCR_SETRESET, color);
// enable all bits
GCR_OUT(GCR_BITMASK, 0xff);
// since set/reset is enabled for all planes, the written value is ignored
fillLong(m_activeOffset + (m_lineSize * row), 0, (m_lineSize*lines)/4);
// disable set/reset
GCR_OUT(GCR_EN_SETRESET, 0x00);
setPlanarRWMode(0,2);
}
void GfxScreen::clear8(int row, int lines, uint8_t color)
{
uint32_t c = color;
c = (c << 8) | color;
c = (c << 8) | color;
c = (c << 8) | color;
outp(SEQ_ADDR, 0x02); // enable all planes
outp(SEQ_DATA, 0x0F);
fillLong(m_activeOffset + (m_lineSize * row), c, (m_width*lines)/16);
}
void GfxScreen::clear8chained(int row, int lines, uint8_t color)
{
uint32_t c = color;
c = (c << 8) | color;
c = (c << 8) | color;
c = (c << 8) | color;
fillLong(m_activeOffset + (m_lineSize * row), c, (m_width*lines)/4);
}
void GfxScreen::clear(uint8_t color)
{
clear(0, m_height, color);
}
int32_t GfxScreen::getPageOffset(uint8_t page)
{
// only planar modes have more than 1 page
// so if current mode is chained (m_pageSize / 4) is wrong but irrelevant
return (page % m_pages) * (m_pageSize / 4);
}
void GfxScreen::setActivePage(uint8_t page)
{
m_activeOffset = m_videoMem + getPageOffset(page);
}
void GfxScreen::setVisiblePage(uint8_t page)
{
if(m_pages == 1) {
vsync();
return;
}
int32_t offset = getPageOffset(page);
// wait for display disable
while(inp(m_isr1_addr) & 0x01);
// set start address
setStartAddress(offset);
// wait for vertical retrace
while(!(inp(m_isr1_addr) & 0x08));
}
void GfxScreen::putPixel1(int16_t x, int16_t y, uint8_t color)
{
uint8_t *ptr = m_activeOffset + 0x2000*(y%2) + m_lineSize*(y/2) + (x/8);
int mask = (1<<7) >> (x%8);
uint8_t temp = *ptr;
color &= 1;
color <<= 7;
color >>= (x%8);
temp &= ~mask;
temp |= color;
*ptr = temp;
}
void GfxScreen::putPixel2(int16_t x, int16_t y, uint8_t color)
{
uint8_t *ptr = m_activeOffset + 0x2000*(y%2) + m_lineSize*(y/2) + (2*x/8);
int mask = (3<<6) >> (2*x%8);
uint8_t temp = *ptr;
color &= 3;
color <<= 6;
color >>= (2*x%8);
temp &= ~mask;
temp |= color;
*ptr = temp;
}
void GfxScreen::putPixel4(int16_t x, int16_t y, uint8_t color)
{
// clip to the mode width and height
if(x < 0 || x > m_maxx || y < 0 || y > m_maxy) {
return;
}
uint8_t bitmask = 1 << ((x & 0x07) ^ 0x07);
outp(GCR_ADDR, GCR_BITMASK);
outp(GCR_DATA, bitmask);
int offset = y*m_lineSize + x/8;
volatile uint8_t dummy = *(m_activeOffset + offset); // load latches
*(m_activeOffset + offset) = color % 16;
}
void GfxScreen::putPixel8(int16_t x, int16_t y, uint8_t color)
{
// clip to the mode width and height
if (x < 0 || x > m_maxx || y < 0 || y > m_maxy) {
return;
}
// set the mask so that only one pixel gets written
outp(SEQ_ADDR, SEQ_MAPMASK);
outp(SEQ_DATA, 1 << (x & 0x3));
// put pixel in memory
*(m_activeOffset + (m_lineSize * y) + (x >> 2)) = color;
}
void GfxScreen::putPixel8chained(int16_t x, int16_t y, uint8_t color)
{
// clip to the mode width and height
if (x < 0 || x > m_maxx || y < 0 || y > m_maxy) {
return;
}
// put pixel in memory
*(m_activeOffset + (m_lineSize * y) + x) = color;
}
int16_t GfxScreen::getPixel1(int16_t x, int16_t y)
{
uint8_t *ptr = m_activeOffset + 0x2000*(y%2) + m_lineSize*(y/2) + (x/8);
int mask = (1<<7) >> (x%8);
uint8_t temp = *ptr;
return (bool)(temp & mask);
}
int16_t GfxScreen::getPixel2(int16_t x, int16_t y)
{
uint8_t *ptr = m_activeOffset + 0x2000*(y%2) + m_lineSize*(y/2) + (2*x/8);
uint8_t temp = *ptr;
temp >>= 6 - (2*x%8);
temp &= 0x3;
return temp;
}
int16_t GfxScreen::getPixel4(int16_t x, int16_t y)
{
if(x < 0 || x > m_maxx || y < 0 || y > m_maxy) {
return -1;
}
uint8_t *cell = (m_activeOffset + (m_lineSize * y) + x/8);
int16_t value = 0;
uint8_t bit = x & 7;
GCR_OUT(GCR_READMAP_SEL, 0);
value |= ((*cell)>>bit)&1;
GCR_OUT(GCR_READMAP_SEL, 1);
value |= (((*cell)>>bit)&1) << 1;
GCR_OUT(GCR_READMAP_SEL, 2);
value |= (((*cell)>>bit)&1) << 2;
GCR_OUT(GCR_READMAP_SEL, 3);
value |= (((*cell)>>bit)&1) << 3;
return value;
}
int16_t GfxScreen::getPixel8(int16_t x, int16_t y)
{
// clip to the mode width and height
if (x < 0 || x > m_maxx || y < 0 || y > m_maxy) {
return -1;
}
// set the mask so that only one pixel gets read
outp(GCR_ADDR, 0x04);
outp(GCR_DATA, x & 0x3);
return *(m_activeOffset + (m_lineSize * y) + (x >> 2));
}
int16_t GfxScreen::getPixel8chained(int16_t x, int16_t y)
{
// clip to the mode width and height
if (x < 0 || x > m_maxx || y < 0 || y > m_maxy) {
return -1;
}
return *(m_activeOffset + (m_lineSize * y) + x);
}
void GfxScreen::drawLine(int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint8_t color)
{
// Lines are NOT clipped.
//
// Algorithm derived from:
// Digital Line Drawing by Paul Heckbert
// from "Graphics Gems", Academic Press, 1990
//
int16_t x = x1;
int16_t y = y1;
int16_t dx = x2 - x1;
int16_t ax = abs(dx) << 1;
int16_t sx = sign(dx);
int16_t dy = y2 - y1;
int16_t ay = abs(dy) << 1;
int16_t sy = sign(dy);
int16_t d;
if(ax > ay) {
// x dominant
d = ay - (ax >> 1);
while(true) {
putPixel(x, y, color);
if(x == x2) {
return;
}
if(d >= 0) {
y += sy;
d -= ax;
}
x += sx;
d += ay;
}
} else {
// y dominant
d = ax - (ay >> 1);
while(true) {
putPixel(x, y, color);
if(y == y2) {
return;
}
if(d >= 0) {
x += sx;
d -= ay;
}
y += sy;
d += ax;
}
}
}
void GfxScreen::drawCircle(int16_t cx, int16_t cy, int16_t r, uint8_t color)
{
int32_t n=0, invradius=(1/(float)r)*0x10000L;
int16_t dx=0, dy=r-1;
putPixel(cx, cy, color);
while(dx<=dy)
{
putPixel(cx+dy, cy-dx, color); // octant 0
putPixel(cx+dx, cy-dy, color); // octant 1
putPixel(cx-dx, cy-dy, color); // octant 2
putPixel(cx-dy, cy-dx, color); // octant 3
putPixel(cx-dy, cy+dx, color); // octant 4
putPixel(cx-dx, cy+dy, color); // octant 5
putPixel(cx+dx, cy+dy, color); // octant 6
putPixel(cx+dy, cy+dx, color); // octant 7
dx++;
n += invradius;
dy = (int16_t)((r * SIN_ACOS[(int16_t)(n>>6)]) >> 16);
}
}
void GfxScreen::drawRectangle(int16_t x, int16_t y, int16_t width, int16_t height, uint8_t color)
{
int x2 = x+width-1;
int y2 = y+height-1;
drawLine(x, y, x2, y, color);
drawLine(x, y, x, y2, color);
drawLine(x2, y, x2, y2, color);
drawLine(x, y2, x2, y2, color);
}
void GfxScreen::drawChar8(int16_t x, int16_t y, uint8_t color, char c)
{
uint8_t far *font = m_fontAddr + (c * m_fontHeight);
for (int i = 0; i < m_fontHeight; i++) {
uint8_t mask = *font;
for (int j = 0; j < 8; j++) {
if (mask & 0x80) {
putPixel(x + j, y + i, color);
}
mask <<= 1;
}
font++;
}
}
void GfxScreen::drawChar4(int16_t x, int16_t y, uint8_t color, char c)
{
// draws a character in 4-bit / 16-color modes
// C++ version of LISTING 26.1,
// Graphics Programming Black Book by Michael Abrash.
// This function needs write mode 3
uint8_t* vga_offset = m_activeOffset + (y * m_lineSize) + x / 8;
uint8_t xbit = x & 7;
uint8_t far *font = m_fontAddr + c * m_fontHeight;
outp(GCR_ADDR, GCR_ROTATE);
uint8_t rot = inp(GCR_DATA);
rot &= 0xe0;
rot |= xbit;
outp(GCR_DATA, rot);
uint8_t leftmask = 0xff >> xbit;
uint8_t rightmask = 0xff << (-xbit + 8);
volatile uint8_t dummy;
for(uint8_t i=0; i<m_fontHeight; i++) {
GCR_OUT(GCR_BITMASK, leftmask);
dummy = *vga_offset;
*vga_offset = *font;
GCR_OUT(GCR_BITMASK, rightmask);
dummy = *vga_offset;
*vga_offset = *font;
font++;
vga_offset += m_lineSize;
}
}
void GfxScreen::printText(int16_t x, int16_t y, uint8_t color, const char *fmt, ...)
{
char buf[200];
va_list ap;
va_start(ap, fmt);
vsnprintf(buf, 200, fmt, ap);
va_end(ap);
drawText(x, y, color, buf);
}
void GfxScreen::drawText8(int16_t x, int16_t y, uint8_t color, const char *string)
{
while(*string) {
drawChar8(x, y, color, *string++);
x += 8;
}
}
void GfxScreen::drawText4(int16_t x, int16_t y, uint8_t color, const char *string)
{
setPlanarRWMode(0,3);
outp(GCR_ADDR, GCR_SETRESET);
uint8_t setrst = inp(GCR_DATA);
setrst &= 0xf0;
setrst |= color & 0x0f;
outp(GCR_DATA, setrst);
while(*string) {
drawChar4(x, y, color, *string++);
x += 8;
}
setPlanarRWMode(0,2);
}
int32_t middleMask[4][4] =
{
{0x1, 0x3, 0x7, 0xf},
{0x0, 0x2, 0x6, 0xe},
{0x0, 0x0, 0x4, 0xc},
{0x0, 0x0, 0x0, 0x8},
};
int32_t leftMask[4] = {0xf, 0xe, 0xc, 0x8};
int32_t rightMask[4] = {0x1, 0x3, 0x7, 0xf};
void GfxScreen::fillRect8(int16_t x, int16_t y, int16_t width, int16_t height, uint8_t color)
{
int32_t x1 = x;
int32_t x2 = x + width - 1;
int32_t y1 = y;
int32_t y2 = y + height - 1;
// clip to the screen.
if(x1 < 0) {
x1 = 0;
}
if(x2 > m_maxx) {
x2 = m_maxx;
}
if(y1 < 0) {
y1 = 0;
}
if (y2 > m_maxy) {
y2 = m_maxy;
}
if(y2 < y1 || x2 < x1) {
return;
}
// in unchained 8bit modes we want to paint from the top down
// and make use of the 4 pixel wide vertical bands
int32_t leftBand = x1 >> 2;
int32_t rightBand = x2 >> 2;
int32_t leftBit = x1 & 3;
int32_t rightBit = x2 & 3;
uint8_t *top;
uint8_t *where;
int32_t mask;
if(leftBand == rightBand) {
// the whole rectangle is in one band
mask = middleMask[leftBit][rightBit];
outp(SEQ_ADDR, 0x02);
outp(SEQ_DATA, mask);
top = m_activeOffset + (m_lineSize * y1) + leftBand;
for(int32_t i = y1; i <= y2; i++) {
*top = color;
top += m_lineSize;
}
} else {
// spans 2 or more bands
mask = leftMask[leftBit];
outp(SEQ_ADDR, 0x02);
outp(SEQ_DATA, mask);
top = m_activeOffset + (m_lineSize * y1) + leftBand;
where = top;
// fill the left edge
for(int32_t i = y1; i <= y2; i++) {
*where = color;
where += m_lineSize;
}
top++;
outp(SEQ_ADDR, 0x02);
outp(SEQ_DATA, 0x0f);
int32_t bands = rightBand - (leftBand + 1);
if(bands > 0) {
where = top;
// fill the middle
for(int32_t i = y1; i <= y2; i++) {
memset(where, color, bands);
where += m_lineSize;
}
top += bands;
}
mask = rightMask[rightBit];
outp(SEQ_ADDR, 0x02);
outp(SEQ_DATA, mask);
where = top;
// fill the right edge
for(int32_t i = y1; i <= y2; i++) {
*where = color;
where += m_lineSize;
}
}
}
void GfxScreen::fillRect8chained(int16_t x, int16_t y, int16_t width, int16_t height, uint8_t color)
{
if(x<0 || y<0 || x>m_maxx || y>m_maxy || width==0 || height==0) {
return;
}
if(x + width > m_width) {
width = m_width - x;
}
if(y + height > m_height) {
height = m_height - y;
}
uint32_t c = color;
c = (c << 8) | color;
c = (c << 8) | color;
c = (c << 8) | color;
uint8_t *lineptr = m_activeOffset + (m_lineSize * y) + x;
int dwords = width / 4;
int bytes = width - dwords * 4;
int linestep = dwords * 4 + (m_width - width);
for(int l=0; l<height; l++) {
for(int b=0; b<bytes; b++) {
*lineptr++ = color;
}
fillLong(lineptr, c, dwords);
lineptr += linestep;
}
}
//---------------------------------------------------
//
// Set one entry in the DAC color palette (256 color mode).
//
void GfxScreen::setColor256(int16_t index, uint16_t r, uint16_t g, uint16_t b)
{
if (index < 0 || index > 255) {
return;
}
while(!(inp(m_isr1_addr) & 0x01)); // wait for blanking
outp(PAL_WRITE_ADDR, index);
outp(PAL_DATA, r);
outp(PAL_DATA, g);
outp(PAL_DATA, b);
}
void GfxScreen::setPalette256(int16_t start, int16_t count, s_color *p)
{
int16_t i;
if(start < 0 || (start + count - 1) > 255) {
return;
}
vsync();
outp(PAL_WRITE_ADDR, start);
for (i = 0; i < count; i++) {
outp(PAL_DATA, p->red);
outp(PAL_DATA, p->green);
outp(PAL_DATA, p->blue);
p++;
}
}
void GfxScreen::setSplitScreen(uint16_t scanline)
{
if(scanline < m_scanlines) {
::setSplitScreen(m_crtc_addr, scanline);
}
}
void GfxScreen::setHPanning(uint8_t hPan)
{
uint16_t isra = 0x03da;
if(m_crtc_addr == CRTC_ADDR_MONO) {
isra = 0x03ba;
}
ACR_OUT(isra, ACR_HPELPAN, hPan);
}
void GfxScreen::setVPanning(uint8_t vPan)
{
int prs;
CRTC_IN(m_crtc_addr, CRTC_PRESET_ROW_SCAN, prs);
prs &= ~0x1f;
prs |= (vPan & 0x1f);
CRTC_OUT(m_crtc_addr, CRTC_PRESET_ROW_SCAN, prs);
}
void GfxScreen::setPanningMode(bool mode)
{
uint16_t isra = 0x03da;
if(m_crtc_addr == CRTC_ADDR_MONO) {
isra = 0x03ba;
}
uint8_t reg;
ACR_IN(isra, ACR_ATTMODE, reg);
reg &= ~0x20;
reg |= (mode << 5);
ACR_OUT(isra, ACR_ATTMODE, reg);
}
void GfxScreen::setStartAddress(uint16_t address)
{
::setStartAddress(m_crtc_addr, address);
}
void GfxScreen::vsync()
{
// wait until any previous retrace has ended
while(inp(m_isr1_addr) & 0x08);
// wait until a new retrace has just begun
while(!(inp(m_isr1_addr) & 0x08));
}
void GfxScreen::wait_disp_enable()
{
while((inp(m_isr1_addr) & 0x01));
}
void GfxScreen::setPlanarRWMode(int rmode, int wmode)
{
outp(GCR_ADDR, 0x05); // R/W mode
uint8_t mode = inp(GCR_DATA) & 0xF4;
mode |= ((rmode&1)<<3) | (wmode&3);
outp(GCR_DATA, mode);
}
void GfxScreen::setStdVGAColorMap()
{
for(int i=0; i<256; i++) m_cval[i] = i;
for(int i=0; i<256; i++) m_cmap[i] = i;
}
//****************************************************************************//
// MODE SETTING
//****************************************************************************//
void selectVGAFreq(int hpels, int lines)
{
uint8_t reg = 0x23; // bit 0,1,5
switch(hpels) {
case 640:
case 320:
break;
case 720:
case 360:
reg |= 0x04;
break;
}
switch(lines) {
default:
case 400: reg |= 0x40; break;
case 350: reg |= 0x80; break;
case 480: reg |= 0xc0; break;
}
SEQ_OUT(0x00,0x01); // synchronous reset while setting Misc Output
outp(MOR_ADDR, reg);
SEQ_OUT(0x00,0x03); // undo reset (restart sequencer)
}
void GfxScreen::mode_b320x200_04h()
{
setBIOSMode(0x04);
m_videoMem = CGA_ADDR;
m_width = 320;
m_height = 200;
m_scanlines = 400;
m_pages = 1;
m_lineOffset = 40;
m_lineSize = 80;
m_chained = 0;
m_pageSize = 16000;
m_modeName = "Mode 04h 320x200x4";
m_colors = 4;
m_putPixelFn = putPixel2;
m_getPixelFn = getPixel2;
m_clearFn = clear2;
m_drawTextFn = drawText8;
memset(m_cval, 0, 256);
m_cval[0] = 0;
m_cval[1] = 1;
m_cval[2] = 2;
m_cval[3] = 3;
m_cmap[c_black ] = 0;
m_cmap[c_blue ] = 1;
m_cmap[c_green ] = 1;
m_cmap[c_cyan ] = 1;
m_cmap[c_red ] = 2;
m_cmap[c_magenta] = 2;
m_cmap[c_brown ] = 0;
m_cmap[c_lgray ] = 3;
m_cmap[c_dgray ] = 0;
m_cmap[c_lblue ] = 1;
m_cmap[c_lgreen ] = 1;
m_cmap[c_lcyan ] = 1;
m_cmap[c_lred ] = 2;
m_cmap[c_pink ] = 2;
m_cmap[c_yellow ] = 3;
m_cmap[c_white ] = 3;
for(int i=16; i<256; i++) {
m_cmap[i] = i%2;
}
}
void GfxScreen::mode_b640x200_06h()
{
setBIOSMode(0x06);
m_videoMem = CGA_ADDR;
m_width = 640;
m_height = 200;
m_scanlines = 400;
m_pages = 1;
m_lineOffset = 40;
m_lineSize = 80;
m_chained = 0;
m_pageSize = 16000;
m_modeName = "Mode 06h 640x200x2";
m_colors = 2;
m_putPixelFn = putPixel1;
m_getPixelFn = getPixel1;
m_clearFn = clear1;
m_drawTextFn = drawText8;
memset(m_cval, 0, 256);
m_cval[0] = 0;
m_cval[1] = 1;
m_cmap[c_black ] = 0;
m_cmap[c_blue ] = 0;
m_cmap[c_green ] = 1;
m_cmap[c_cyan ] = 1;
m_cmap[c_red ] = 1;
m_cmap[c_magenta] = 1;
m_cmap[c_brown ] = 0;
m_cmap[c_lgray ] = 1;
m_cmap[c_dgray ] = 0;
m_cmap[c_lblue ] = 1;
m_cmap[c_lgreen ] = 1;
m_cmap[c_lcyan ] = 1;
m_cmap[c_lred ] = 1;
m_cmap[c_pink ] = 1;
m_cmap[c_yellow ] = 1;
m_cmap[c_white ] = 1;
for(int i=16; i<256; i++) {
m_cmap[i] = i%2;
}
}
void GfxScreen::mode_b320x200_0Dh()
{
setBIOSMode(0x0d);
m_width = 320;
m_height = 200;
m_scanlines = 400;
m_pages = 8;
m_lineOffset = 40;
m_lineSize = 40;
m_chained = 0;
m_pageSize = 32000;
m_modeName = "Mode 0Dh 320x200x16";
m_colors = 16;
m_putPixelFn = putPixel4;
m_getPixelFn = getPixel4;
m_clearFn = clear4;
m_drawTextFn = drawText4;
// put pixel needs write mode 2
setPlanarRWMode(0,2);
setStdVGAColorMap();
}
void GfxScreen::mode_b640x200_0Eh()
{
setBIOSMode(0x0e);
m_width = 640;
m_height = 200;
m_scanlines = 400;
m_pages = 4;
m_lineOffset = 80;
m_lineSize = 80;
m_chained = 0;
m_pageSize = 64000;
m_modeName = "Mode 0Eh 640x200x16";
m_colors = 16;
m_putPixelFn = putPixel4;
m_getPixelFn = getPixel4;
m_clearFn = clear4;
m_drawTextFn = drawText4;
// put pixel needs write mode 2
setPlanarRWMode(0,2);
setStdVGAColorMap();
}
void GfxScreen::mode_b640x350_0Fh()
{
setBIOSMode(0x0f);