/
VGADriver.cs
1676 lines (1560 loc) · 160 KB
/
VGADriver.cs
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
//#define COSMOSDEBUG
using System;
using System.Collections.Generic;
using System.Drawing;
using Cosmos.Core;
using Cosmos.Debug.Kernel;
/*****************************************************************************
Sets VGA-compatible video modes without using the BIOS
* http://bos.asmhackers.net/docs/vga_without_bios/
* adapted from code by Chris Giese <geezer@execpc.com> http://www.execpc.com/~geezer
* by Stephen Remde
//*/
namespace Cosmos.HAL
{
public class VGADriver
{
internal Debugger mDebugger = new Debugger("HAL", "VGA");
private const byte _NumSeqRegs = 5;
private const byte _NumCRTCRegs = 25;
private const byte _NumGCRegs = 9;
private const byte _NumACRegs = 21;
Mode _Mode;
ScreenSize _ScreenSize;
ColorDepth _ColorDepth;
private readonly Core.IOGroup.VGA _IO = new Core.IOGroup.VGA();
private void WriteVGARegisters(byte[] aRegisters)
{
int xIdx = 0;
byte i;
/* write MISCELLANEOUS reg */
_IO.MiscellaneousOutput_Write.Byte = aRegisters[xIdx++];
/* write SEQUENCER regs */
for (i = 0; i < _NumSeqRegs; i++)
{
_IO.Sequencer_Index.Byte = i;
_IO.Sequencer_Data.Byte = aRegisters[xIdx++];
}
/* unlock CRTC registers */
_IO.CRTController_Index.Byte = 0x03;
_IO.CRTController_Data.Byte = (byte)(_IO.CRTController_Data.Byte | 0x80);
_IO.CRTController_Index.Byte = 0x11;
_IO.CRTController_Data.Byte = (byte)(_IO.CRTController_Data.Byte & 0x7F);
/* make sure they remain unlocked */
aRegisters[0x03] |= 0x80;
aRegisters[0x11] &= 0x7f;
/* write CRTC regs */
for (i = 0; i < _NumCRTCRegs; i++)
{
_IO.CRTController_Index.Byte = i;
_IO.CRTController_Data.Byte = aRegisters[xIdx++];
}
/* write GRAPHICS CONTROLLER regs */
for (i = 0; i < _NumGCRegs; i++)
{
_IO.GraphicsController_Index.Byte = i;
_IO.GraphicsController_Data.Byte = aRegisters[xIdx++];
}
/* write ATTRIBUTE CONTROLLER regs */
for (i = 0; i < _NumACRegs; i++)
{
var _ = _IO.Instat_Read.Byte;
_IO.AttributeController_Index.Byte = i;
_IO.AttributeController_Write.Byte = aRegisters[xIdx++];
}
/* lock 16-color palette and unblank display */
_ = _IO.Instat_Read.Byte;
_IO.AttributeController_Index.Byte = 0x20;
mDebugger.Send("Finished writing VGA registers");
}
/// <summary>
/// Set plane.
/// </summary>
/// <param name="p">p to set.</param>
private void SetPlane(byte aP)
{
byte pmask;
aP &= 3;
pmask = (byte)(1 << aP);
/* set read plane */
_IO.GraphicsController_Index.Byte = 4;
_IO.GraphicsController_Data.Byte = aP;
/* set write plane */
_IO.Sequencer_Index.Byte = 2;
_IO.Sequencer_Data.Byte = pmask;
}
//int offset = 0xb8000;
/// <summary>
/// Get frame buffer segment.
/// </summary>
/// <returns>MemoryBlock08 value.</returns>
/// <exception cref="Exception">Thrown when unable to determine memory segment.</exception>
private MemoryBlock GetFramebufferSegment()
{
_IO.GraphicsController_Index.Byte = 6;
int seg = _IO.GraphicsController_Data.Byte;
mDebugger.Send($"VGA: raw seg value: {seg}");
seg >>= 2;
seg &= 3;
switch (seg)
{
case 0:
case 1:
return _IO.VGAMemoryBlock;
case 2:
return _IO.MonochromeTextMemoryBlock;
case 3:
return _IO.CGATextMemoryBlock;
}
throw new Exception("Unable to determine memory segment!");
}
/// <summary>
/// Write font.
/// </summary>
/// <param name="font">Font.</param>
/// <param name="font_height">Font height.</param>
/// <exception cref="Exception">Thrown when unable to determine memory segment.</exception>
public void WriteFont(byte[] aFont, byte aFontHeight)
{
byte seq2, seq4, gc4, gc5, gc6;
/* save registers
set_plane() modifies GC 4 and SEQ 2, so save them as well */
_IO.Sequencer_Index.Byte = 2;
seq2 = _IO.Sequencer_Data.Byte;
_IO.Sequencer_Index.Byte = 4;
seq4 = _IO.Sequencer_Data.Byte;
/* turn off even-odd addressing (set flat addressing)
assume: chain-4 addressing already off */
_IO.Sequencer_Data.Byte = (byte)(seq4 | 0x04);
_IO.GraphicsController_Index.Byte = 4;
gc4 = _IO.GraphicsController_Data.Byte;
_IO.GraphicsController_Index.Byte = 5;
gc5 = _IO.GraphicsController_Data.Byte;
/* turn off even-odd addressing */
_IO.GraphicsController_Data.Byte = (byte)(gc5 & ~0x10);
_IO.GraphicsController_Index.Byte = 6;
gc6 = _IO.GraphicsController_Data.Byte;
/* turn off even-odd addressing */
_IO.GraphicsController_Data.Byte = (byte)(gc6 & ~0x02);
/* write font to plane P4 */
SetPlane(2);
/* write font 0 */
var seg = GetFramebufferSegment();
for (uint i = 0; i < 256; i++)
{
for (uint j = 0; j < aFontHeight; j++)
{
seg[(i * 32) + j] = aFont[(i * aFontHeight) + j];
}
}
/* restore registers */
_IO.Sequencer_Index.Byte = 2;
_IO.Sequencer_Data.Byte = seq2;
_IO.Sequencer_Index.Byte = 4;
_IO.Sequencer_Data.Byte = seq4;
_IO.GraphicsController_Index.Byte = 4;
_IO.GraphicsController_Data.Byte = gc4;
_IO.GraphicsController_Index.Byte = 5;
_IO.GraphicsController_Data.Byte = gc5;
_IO.GraphicsController_Index.Byte = 6;
_IO.GraphicsController_Data.Byte = gc6;
}
public VGADriver()
{
}
/// <summary>
/// The closest color in the palette will be found to be drawn
/// This is quite slow, so whenever possible use the index of the color
/// </summary>
/// <param name="aX"></param>
/// <param name="aY"></param>
/// <param name="aColor"></param>
public void SetPixel(uint aX, uint aY, Color aColor)
{
//Find the closest index
var index = GetClosestColorInPalette(aColor);
//Draw
SetPixel(aX, aY, index);
}
private static Dictionary<int, uint> colorToPalette = new Dictionary<int, uint>();
public uint GetClosestColorInPalette(Color aColor)
{
if (colorToPalette.ContainsKey(aColor.ToArgb()))
{
return colorToPalette[aColor.ToArgb()];
}
uint index = 0;
double diff = 1000000;
for (uint i = 0; i < 2 << ((int)_ColorDepth - 1); i++) //iterate over the total palette
{
var paletteColor = _Palette[i];
var colorDiff = (aColor.R - paletteColor.R) * (aColor.R - paletteColor.R) * 0.3 + //Taken from https://stackoverflow.com/questions/1847092/given-an-rgb-value-what-would-be-the-best-way-to-find-the-closest-match-in-the-d
(aColor.G - paletteColor.G) * (aColor.G - paletteColor.G) * 0.59 + (aColor.B - paletteColor.B) * (aColor.B - paletteColor.B) * 0.11;
//mDebugger.Send($"Standard Color: {aColor.R} {aColor.G} {aColor.B} Comparing to: {paletteColor.R} {paletteColor.G} {paletteColor.B} Diff: {colorDiff}");
if (colorDiff < diff)
{
index = i;
diff = colorDiff;
if (diff == 0)
{
break;
}
}
}
colorToPalette.Add(aColor.ToArgb(), index);
//mDebugger.Send("Chosen index: " + index);
return index;
}
/// <summary>
///
/// </summary>
/// <param name="aX"></param>
/// <param name="aY"></param>
/// <param name="aColor">The index of the color in the palette</param>
public void SetPixel(uint aX, uint aY, uint aColor)
{
//Global.mDebugger.Send($"Setting pixel: ({x}, {y}) to {color}");
if(_Mode == Mode.Text)
{
throw new Exception("Cannot set pixel in text mode");
}
switch (_ScreenSize)
{
case ScreenSize.Size640x480:
switch (_ColorDepth)
{
case ColorDepth.BitDepth2:
throw new NotImplementedException();
case ColorDepth.BitDepth4:
SetPixel640x480x4(aX, aY, aColor);
break;
case ColorDepth.BitDepth8:
throw new NotImplementedException();
case ColorDepth.BitDepth16:
throw new NotImplementedException();
default:
throw new NotImplementedException();
}
break;
case ScreenSize.Size720x480:
switch (_ColorDepth)
{
case ColorDepth.BitDepth2:
throw new NotImplementedException();
case ColorDepth.BitDepth4:
SetPixel720x480x4(aX, aY, aColor);
break;
case ColorDepth.BitDepth8:
throw new NotImplementedException();
case ColorDepth.BitDepth16:
throw new NotImplementedException();
default:
throw new NotImplementedException();
}
break;
case ScreenSize.Size320x200:
switch (_ColorDepth)
{
case ColorDepth.BitDepth2:
throw new NotImplementedException();
case ColorDepth.BitDepth4:
throw new NotImplementedException();
case ColorDepth.BitDepth8:
SetPixel320x200x8(aX, aY, aColor);
break;
case ColorDepth.BitDepth16:
throw new NotImplementedException();
default:
throw new NotImplementedException();
}
break;
default:
throw new NotImplementedException();
}
}
public uint GetPixel(uint aX, uint aY)
{
Global.mDebugger.Send($"GetPixel: ({aX},{aY})");
if (_Mode == Mode.Text)
{
throw new Exception("Cannot get pixel in text mode");
}
return _ScreenSize switch
{
ScreenSize.Size640x480 => _ColorDepth switch
{
ColorDepth.BitDepth2 => throw new NotImplementedException(),
ColorDepth.BitDepth4 => GetPixel640x480x4(aX, aY),
ColorDepth.BitDepth8 => throw new NotImplementedException(),
ColorDepth.BitDepth16 => throw new NotImplementedException(),
_ => throw new NotImplementedException(),
},
ScreenSize.Size720x480 => _ColorDepth switch
{
ColorDepth.BitDepth2 => throw new NotImplementedException(),
ColorDepth.BitDepth4 => GetPixel720x480x4(aX, aY),
ColorDepth.BitDepth8 => throw new NotImplementedException(),
ColorDepth.BitDepth16 => throw new NotImplementedException(),
_ => throw new NotImplementedException(),
},
ScreenSize.Size320x200 => _ColorDepth switch
{
ColorDepth.BitDepth2 => throw new NotImplementedException(),
ColorDepth.BitDepth4 => throw new NotImplementedException(),
ColorDepth.BitDepth8 => GetPixel320x200x8(aX, aY),
ColorDepth.BitDepth16 => throw new NotImplementedException(),
_ => throw new NotImplementedException(),
},
_ => throw new NotImplementedException(),
};
}
public enum TextSize
{
Size40x25,
Size40x50,
Size80x25,
Size80x50,
Size90x30,
Size90x60
};
public enum ScreenSize
{
/// <summary>
/// 640x480 graphics mode - 2 and 4 bit color depth available
/// </summary>
Size640x480,
/// <summary>
/// 720x480 graphics mode - 16 bit color depth available
/// </summary>
Size720x480,
/// <summary>
/// 320x200 graphics mode - 4 and 8 bit color depth available
/// </summary>
Size320x200
};
public enum ColorDepth
{
BitDepth2=2,
BitDepth4=4,
BitDepth8=8,
BitDepth16=16
};
/// <summary>
/// Set text size.
/// </summary>
/// <param name="aSize">A size to set.</param>
/// <exception cref="Exception">Thrown when text size invalid / unable to determine memory segment.</exception>
public void SetTextMode(TextSize aSize)
{
mDebugger.Send("Setting TextMode:" + aSize.ToString());
_Mode = Mode.Text;
switch (aSize)
{
case TextSize.Size40x25:
WriteVGARegisters(_G_40x25_text);
WriteFont(_G_8x16_font, 16);
break;
case TextSize.Size40x50:
WriteVGARegisters(_G_40x50_text);
WriteFont(_G_8x8_font, 8);
break;
case TextSize.Size80x25:
WriteVGARegisters(_G_80x25_text);
WriteFont(_G_8x16_font, 16);
break;
case TextSize.Size80x50:
WriteVGARegisters(_G_80x50_text);
WriteFont(_G_8x8_font, 8);
break;
case TextSize.Size90x30:
WriteVGARegisters(_G_90x30_text);
WriteFont(_G_8x16_font, 16);
break;
case TextSize.Size90x60:
WriteVGARegisters(_G_90x60_text);
WriteFont(_G_8x8_font, 8);
break;
default:
throw new Exception("Invalid text size.");
}
//Clear the memory
_IO.CGATextMemoryBlock.Fill(0);
int[] colors = new int[]
{
0, 168, 43008, 43176, 11010048, 11010216, 11053056, 11053224, 84, 252, 43092, 43260, 11010132, 11010300, 11053140, 11053308, 21504, 21672, 64512, 64680, 11031552, 11031720, 11074560, 11074728, 21588, 21756, 64596, 64764, 11031636, 11031804, 11074644, 11074812, 5505024, 5505192, 5548032, 5548200, 16515072, 16515240, 16558080, 16558248, 5505108, 5505276, 5548116, 5548284, 16515156, 16515324, 16558164, 16558332, 5526528, 5526696, 5569536, 5569704, 16536576, 16536744, 16579584, 16579752, 5526612, 5526780, 5569620, 5569788, 16536660, 16536828, 16579668, 16579836, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
for (int i = 0; i < 256; i++)
{
SetPaletteEntry(i, Color.FromArgb(colors[i]));
}
}
/// <summary>
/// Set graphics mode.
/// </summary>
/// <param name="aSize">A screen size.</param>
/// <param name="aDepth">A color depth.</param>
/// <exception cref="Exception">Thrown when aDepth not supported for the aSize / unknown screen size.</exception>
public void SetGraphicsMode(ScreenSize aSize, ColorDepth aDepth)
{
mDebugger.Send("Setting GraphicsMode:" + ((int)aSize).ToString() + " - " + ((int)aDepth).ToString());
_ScreenSize = aSize;
_ColorDepth = aDepth;
_Mode = Mode.Graphical;
switch (aSize)
{
case ScreenSize.Size320x200:
if (aDepth == ColorDepth.BitDepth8)
{
mDebugger.Send("Setting graphic mode to 320x200@256");
WriteVGARegisters(_G_320x200x8);
PixelWidth = 320;
PixelHeight = 200;
Colors = 256;
}
else if (aDepth == ColorDepth.BitDepth4)
{
mDebugger.Send("Setting graphic mode to 320x200@16");
WriteVGARegisters(_G_320x200x4);
PixelWidth = 320;
PixelHeight = 200;
Colors = 16;
}
else
{
throw new Exception($"Unsupported color depth {aDepth} passed for specified screen size");
}
break;
case ScreenSize.Size640x480:
if (aDepth == ColorDepth.BitDepth2)
{
mDebugger.Send("Setting graphic mode to 640x480@4");
WriteVGARegisters(_G_640x480x2);
PixelWidth = 640;
PixelHeight = 480;
Colors = 4;
}
else if (aDepth == ColorDepth.BitDepth4)
{
mDebugger.Send("Setting graphic mode to 640x480@16");
WriteVGARegisters(_G_640x480x4);
PixelWidth = 640;
PixelHeight = 480;
Colors = 16;
}
else
{
throw new Exception($"Unsupported color depth {aDepth} passed for specified screen size");
}
break;
case ScreenSize.Size720x480:
if (aDepth == ColorDepth.BitDepth4)
{
mDebugger.Send("Setting graphic mode to 720x480@16");
WriteVGARegisters(_G_720x480x4);
PixelWidth = 720;
PixelHeight = 480;
Colors = 16;
}
else
{
throw new Exception($"Unsupported color depth {aDepth} passed for specified screen size");
}
break;
default:
throw new Exception("Unknown screen size");
}
// Clear the cached results as the palette will change
colorToPalette = new Dictionary<int, uint>();
// Initialize the palette
_Palette[0] = Color.Black;
switch (aDepth)
{
case ColorDepth.BitDepth2:
//There are two different color modes available, Assume we are using palette 0
_Palette[1] = Color.Green;
_Palette[2] = Color.Red;
_Palette[3] = Color.Brown;
//_Palette[1] = Color.Cyan;
//_Palette[2] = Color.Magenta;
//_Palette[3] = Color.Gray;
break;
case ColorDepth.BitDepth4:
_Palette[1] = Color.Blue;
_Palette[2] = Color.Green;
_Palette[3] = Color.Cyan;
_Palette[4] = Color.Red;
_Palette[5] = Color.DarkMagenta;
_Palette[6] = Color.Brown;
_Palette[7] = Color.LightGray;
_Palette[8] = Color.DarkGray;
_Palette[9] = Color.LightBlue;
_Palette[10] = Color.LightGreen;
_Palette[11] = Color.LightCyan;
_Palette[12] = Color.Pink;
_Palette[13] = Color.Magenta;
_Palette[14] = Color.Yellow;
_Palette[15] = Color.White;
break;
case ColorDepth.BitDepth8:
int[] colors = new int[256] // Credits to https://commons.wikimedia.org/w/index.php?title=User:Psychonaut/ipalette.sh&oldid=8607095
{
0x000000,0x0000AA,0x00AA00,0x00AAAA,0xAA0000,0xAA00AA,0xAA5500,0xAAAAAA,0x555555,0x5555FF,
0x55FF55,0x55FFFF,0xFF5555,0xFF55FF,0xFFFF55,0xFFFFFF,0x000000,0x101010,0x202020,0x353535,
0x454545,0x555555,0x656565,0x757575,0x8A8A8A,0x9A9A9A,0xAAAAAA,0xBABABA,0xCACACA,0xDFDFDF,
0xEFEFEF,0xFFFFFF,0x0000FF,0x4100FF,0x8200FF,0xBE00FF,0xFF00FF,0xFF00BE,0xFF0082,0xFF0041,
0xFF0000,0xFF4100,0xFF8200,0xFFBE00,0xFFFF00,0xBEFF00,0x82FF00,0x41FF00,0x00FF00,0x00FF41,
0x00FF82,0x00FFBE,0x00FFFF,0x00BEFF,0x0082FF,0x0041FF,0x8282FF,0x9E82FF,0xBE82FF,0xDF82FF,
0xFF82FF,0xFF82DF,0xFF82BE,0xFF829E,0xFF8282,0xFF9E82,0xFFBE82,0xFFDF82,0xFFFF82,0xDFFF82,
0xBEFF82,0x9EFF82,0x82FF82,0x82FF9E,0x82FFBE,0x82FFDF,0x82FFFF,0x82DFFF,0x82BEFF,0x829EFF,
0xBABAFF,0xCABAFF,0xDFBAFF,0xEFBAFF,0xFFBAFF,0xFFBAEF,0xFFBADF,0xFFBACA,0xFFBABA,0xFFCABA,
0xFFDFBA,0xFFEFBA,0xFFFFBA,0xEFFFBA,0xDFFFBA,0xCAFFBA,0xBAFFBA,0xBAFFCA,0xBAFFDF,0xBAFFEF,
0xBAFFFF,0xBAEFFF,0xBADFFF,0xBACAFF,0x000071,0x1C0071,0x390071,0x550071,0x710071,0x710055,
0x710039,0x71001C,0x710000,0x711C00,0x713900,0x715500,0x717100,0x557100,0x397100,0x1C7100,
0x007100,0x00711C,0x007139,0x007155,0x007171,0x005571,0x003971,0x001C71,0x393971,0x453971,
0x553971,0x613971,0x713971,0x713961,0x713955,0x713945,0x713939,0x714539,0x715539,0x716139,
0x717139,0x617139,0x557139,0x457139,0x397139,0x397145,0x397155,0x397161,0x397171,0x396171,
0x395571,0x394571,0x515171,0x595171,0x615171,0x695171,0x715171,0x715169,0x715161,0x715159,
0x715151,0x715951,0x716151,0x716951,0x717151,0x697151,0x617151,0x597151,0x517151,0x517159,
0x517161,0x517169,0x517171,0x516971,0x516171,0x515971,0x000041,0x100041,0x200041,0x310041,
0x410041,0x410031,0x410020,0x410010,0x410000,0x411000,0x412000,0x413100,0x414100,0x314100,
0x204100,0x104100,0x004100,0x004110,0x004120,0x004131,0x004141,0x003141,0x002041,0x001041,
0x202041,0x282041,0x312041,0x392041,0x412041,0x412039,0x412031,0x412028,0x412020,0x412820,
0x413120,0x413920,0x414120,0x394120,0x314120,0x284120,0x204120,0x204128,0x204131,0x204139,
0x204141,0x203941,0x203141,0x202841,0x2D2D41,0x312D41,0x352D41,0x3D2D41,0x412D41,0x412D3D,
0x412D35,0x412D31,0x412D2D,0x41312D,0x41352D,0x413D2D,0x41412D,0x3D412D,0x35412D,0x31412D,
0x2D412D,0x2D4131,0x2D4135,0x2D413D,0x2D4141,0x2D3D41,0x2D3541,0x2D3141,0x000000,0x000000,
0x000000,0x000000,0x000000,0x000000,0x000000,0x000000
};
for (int i = 0; i < 256; i++)
{
_Palette[i] = Color.FromArgb(colors[i]);
}
break;
case ColorDepth.BitDepth16:
break;
default:
break;
}
for (int i = 0; i < (2 << ((int)aDepth - 1)); i++)
{
SetPaletteEntry(i, _Palette[i]);
}
}
public void SetPixel320x200x8(uint aX, uint aY, uint aC)
{
uint where = (aY * 320) + aX;
byte color = (byte)(aC & 0xFF);
_IO.VGAMemoryBlock.Bytes[where] = color;
}
public uint GetPixel320x200x8(uint aX, uint aY)
{
mDebugger.Send($"GetPixel320x200x8({aX},{aY})");
return _IO.VGAMemoryBlock.Bytes[(aY * 320) + aX];
}
public void SetPixel640x480x4(uint aX, uint aY, uint aC)
{
uint offset = (uint)(aX / 8 + (PixelWidth / 8) * aY);
aX = (aX & 7) * 1;
uint mask = (byte)(0x80 >> (int)aX);
uint pmask = 1;
for (byte p = 0; p < 4; p++)
{
SetPlane(p);
if ((pmask & aC) != 0)
{
_IO.VGAMemoryBlock.Bytes[offset] = (byte)(_IO.VGAMemoryBlock.Bytes[offset] | mask);
}
else
{
_IO.VGAMemoryBlock.Bytes[offset] = (byte)(_IO.VGAMemoryBlock.Bytes[offset] & ~mask);
}
pmask <<= 1;
}
}
public uint GetPixel640x480x4(uint aX, uint aY)
{
mDebugger.Send($"GetPixel640x480x4({aX},{aY})");
uint offset = (uint)(aX / 8 + (PixelWidth / 8) * aY);
uint pmask = 1;
uint color = 0;
for (byte p = 0; p < 4; p++)
{
SetPlane(p);
if (_IO.VGAMemoryBlock.Bytes[offset] == 255)
{
color += pmask;
}
pmask <<= 1;
}
return color;
}
public void SetPixel720x480x4(uint aX, uint aY, uint aC)
{
uint offset = (uint)(aX / 8 + (PixelWidth / 8) * aY);
aX = (aX & 7) * 1;
uint mask = (byte)(0x80 >> (int)aX);
uint pmask = 1;
for (byte p = 0; p < 4; p++)
{
SetPlane(p);
if ((pmask & aC) != 0)
{
_IO.VGAMemoryBlock.Bytes[offset] = (byte)(_IO.VGAMemoryBlock.Bytes[offset] | mask);
}
else
{
_IO.VGAMemoryBlock.Bytes[offset] = (byte)(_IO.VGAMemoryBlock.Bytes[offset] & ~mask);
}
pmask <<= 1;
}
}
public uint GetPixel720x480x4(uint aX, uint aY)
{
mDebugger.Send($"GetPixel720x480x4({aX},{aY})");
uint offset = (uint)(aX / 8 + (PixelWidth / 8) * aY);
int pixelOffset = (int)(7 - aX % 8);
uint pmask = 1;
uint color = 0;
for (byte p = 0; p < 4; p++)
{
SetPlane(p);
var v = _IO.VGAMemoryBlock.Bytes[offset];
if ((v & (1 << pixelOffset)) != 0)
{
color += pmask;
}
pmask <<= 1;
}
return (uint)_Palette[color].ToArgb();
}
/// <summary>
/// Get and set pixel width.
/// </summary>
public int PixelWidth { private set; get; }
/// <summary>
/// Get and set pixel height.
/// </summary>
public int PixelHeight { private set; get; }
/// <summary>
/// Get and set colors.
/// </summary>
public int Colors { private set; get; }
/// <summary>
/// Draw Filled Rectangle
/// </summary>
/// <param name="aX">X Position.</param>
/// <param name="aY">Y Position.</param>
/// <param name="aW">Rectangle width</param>
/// <param name="aH">Rectangle height</param>
/// <param name="aColor">Rectangle color.</param>
/// <exception cref="Exception">Thrown when Textmode enabled.</exception>
public void DrawFilledRectangle(int aX, int aY, int aW, int aH, uint aColor)
{
if (_Mode == Mode.Text)
{
throw new Exception("Cannot draw filled rectangle in text mode");
}
switch (_ScreenSize)
{
case ScreenSize.Size640x480:
switch (_ColorDepth)
{
case ColorDepth.BitDepth2:
throw new NotImplementedException();
case ColorDepth.BitDepth4:
for (uint x = (uint)aX; x < aX + aW; x++)
{
for (uint y = (uint)aY; y < aY + aH; y++)
{
SetPixel640x480x4(x, y, aColor);
}
}
break;
case ColorDepth.BitDepth8:
throw new NotImplementedException();
case ColorDepth.BitDepth16:
throw new NotImplementedException();
default:
throw new NotImplementedException();
}
break;
case ScreenSize.Size720x480:
switch (_ColorDepth)
{
case ColorDepth.BitDepth2:
throw new NotImplementedException();
case ColorDepth.BitDepth4:
for (uint x = (uint)aX; x < aX + aW; x++)
{
for (uint y = (uint)aY; y < aY + aH; y++)
{
SetPixel720x480x4(x, y, aColor);
}
}
break;
case ColorDepth.BitDepth8:
throw new NotImplementedException();
case ColorDepth.BitDepth16:
throw new NotImplementedException();
default:
throw new NotImplementedException();
}
break;
case ScreenSize.Size320x200:
switch (_ColorDepth)
{
case ColorDepth.BitDepth2:
throw new NotImplementedException();
case ColorDepth.BitDepth4:
throw new NotImplementedException();
case ColorDepth.BitDepth8:
for (uint x = (uint)aX; x < aX + aW; x++)
{
for (uint y = (uint)aY; y < aY + aH; y++)
{
SetPixel320x200x8(x, y, aColor);
}
}
break;
case ColorDepth.BitDepth16:
throw new NotImplementedException();
default:
throw new NotImplementedException();
}
break;
default:
throw new NotImplementedException();
}
}
private readonly Color[] _Palette = new Color[256];
//Get all the colors from the palette
public void ReadPalette()
{
mDebugger.Send("Reading set palette");
_IO.DACIndex_Read.Word = 0;
string values = "";
for (int i = 0; i < 256; i++)
{
int red = _IO.DAC_Data.Byte << 2;
int green = _IO.DAC_Data.Byte << 2;
int blue = _IO.DAC_Data.Byte << 2;
var argb = (red << 16) | (green << 8) | blue;
_Palette[i] = Color.FromArgb(argb);
}
}
public Color GetPaletteEntry(int aIndex)
{
return _Palette[aIndex];
}
public void SetPaletteEntry(int aIndex, Color aColor)
{
_Palette[aIndex] = aColor;
SetPaletteEntry(aIndex, aColor.R, aColor.G, aColor.B);
}
/// <summary>
/// Set palette entry.
/// </summary>
/// <param name="aIndex">Index.</param>
/// <param name="aR">Red.</param>
/// <param name="aG">Green.</param>
/// <param name="aB">Blue.</param>
public void SetPaletteEntry(int aIndex, byte aR, byte aG, byte aB)
{
_Palette[aIndex] = Color.FromArgb(aR, aG, aB);
_IO.DACIndex_Write.Byte = (byte)aIndex;
_IO.DAC_Data.Byte = (byte)(aR >> 2);
_IO.DAC_Data.Byte = (byte)(aG >> 2);
_IO.DAC_Data.Byte = (byte)(aB >> 2);
}
#region FONTS
private static readonly byte[] _G_8x8_font = new byte[]
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x7E, 0x81, 0xA5, 0x81, 0xBD, 0x99, 0x81, 0x7E,
0x7E, 0xFF, 0xDB, 0xFF, 0xC3, 0xE7, 0xFF, 0x7E,
0x6C, 0xFE, 0xFE, 0xFE, 0x7C, 0x38, 0x10, 0x00,
0x10, 0x38, 0x7C, 0xFE, 0x7C, 0x38, 0x10, 0x00,
0x38, 0x7C, 0x38, 0xFE, 0xFE, 0x92, 0x10, 0x7C,
0x00, 0x10, 0x38, 0x7C, 0xFE, 0x7C, 0x38, 0x7C,
0x00, 0x00, 0x18, 0x3C, 0x3C, 0x18, 0x00, 0x00,
0xFF, 0xFF, 0xE7, 0xC3, 0xC3, 0xE7, 0xFF, 0xFF,
0x00, 0x3C, 0x66, 0x42, 0x42, 0x66, 0x3C, 0x00,
0xFF, 0xC3, 0x99, 0xBD, 0xBD, 0x99, 0xC3, 0xFF,
0x0F, 0x07, 0x0F, 0x7D, 0xCC, 0xCC, 0xCC, 0x78,
0x3C, 0x66, 0x66, 0x66, 0x3C, 0x18, 0x7E, 0x18,
0x3F, 0x33, 0x3F, 0x30, 0x30, 0x70, 0xF0, 0xE0,
0x7F, 0x63, 0x7F, 0x63, 0x63, 0x67, 0xE6, 0xC0,
0x99, 0x5A, 0x3C, 0xE7, 0xE7, 0x3C, 0x5A, 0x99,
0x80, 0xE0, 0xF8, 0xFE, 0xF8, 0xE0, 0x80, 0x00,
0x02, 0x0E, 0x3E, 0xFE, 0x3E, 0x0E, 0x02, 0x00,
0x18, 0x3C, 0x7E, 0x18, 0x18, 0x7E, 0x3C, 0x18,
0x66, 0x66, 0x66, 0x66, 0x66, 0x00, 0x66, 0x00,
0x7F, 0xDB, 0xDB, 0x7B, 0x1B, 0x1B, 0x1B, 0x00,
0x3E, 0x63, 0x38, 0x6C, 0x6C, 0x38, 0x86, 0xFC,
0x00, 0x00, 0x00, 0x00, 0x7E, 0x7E, 0x7E, 0x00,
0x18, 0x3C, 0x7E, 0x18, 0x7E, 0x3C, 0x18, 0xFF,
0x18, 0x3C, 0x7E, 0x18, 0x18, 0x18, 0x18, 0x00,
0x18, 0x18, 0x18, 0x18, 0x7E, 0x3C, 0x18, 0x00,
0x00, 0x18, 0x0C, 0xFE, 0x0C, 0x18, 0x00, 0x00,
0x00, 0x30, 0x60, 0xFE, 0x60, 0x30, 0x00, 0x00,
0x00, 0x00, 0xC0, 0xC0, 0xC0, 0xFE, 0x00, 0x00,
0x00, 0x24, 0x66, 0xFF, 0x66, 0x24, 0x00, 0x00,
0x00, 0x18, 0x3C, 0x7E, 0xFF, 0xFF, 0x00, 0x00,
0x00, 0xFF, 0xFF, 0x7E, 0x3C, 0x18, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x18, 0x3C, 0x3C, 0x18, 0x18, 0x00, 0x18, 0x00,
0x6C, 0x6C, 0x6C, 0x00, 0x00, 0x00, 0x00, 0x00,
0x6C, 0x6C, 0xFE, 0x6C, 0xFE, 0x6C, 0x6C, 0x00,
0x18, 0x7E, 0xC0, 0x7C, 0x06, 0xFC, 0x18, 0x00,
0x00, 0xC6, 0xCC, 0x18, 0x30, 0x66, 0xC6, 0x00,
0x38, 0x6C, 0x38, 0x76, 0xDC, 0xCC, 0x76, 0x00,
0x30, 0x30, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00,
0x18, 0x30, 0x60, 0x60, 0x60, 0x30, 0x18, 0x00,
0x60, 0x30, 0x18, 0x18, 0x18, 0x30, 0x60, 0x00,
0x00, 0x66, 0x3C, 0xFF, 0x3C, 0x66, 0x00, 0x00,
0x00, 0x18, 0x18, 0x7E, 0x18, 0x18, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x30,
0x00, 0x00, 0x00, 0x7E, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x00,
0x06, 0x0C, 0x18, 0x30, 0x60, 0xC0, 0x80, 0x00,
0x7C, 0xCE, 0xDE, 0xF6, 0xE6, 0xC6, 0x7C, 0x00,
0x30, 0x70, 0x30, 0x30, 0x30, 0x30, 0xFC, 0x00,
0x78, 0xCC, 0x0C, 0x38, 0x60, 0xCC, 0xFC, 0x00,
0x78, 0xCC, 0x0C, 0x38, 0x0C, 0xCC, 0x78, 0x00,
0x1C, 0x3C, 0x6C, 0xCC, 0xFE, 0x0C, 0x1E, 0x00,
0xFC, 0xC0, 0xF8, 0x0C, 0x0C, 0xCC, 0x78, 0x00,
0x38, 0x60, 0xC0, 0xF8, 0xCC, 0xCC, 0x78, 0x00,
0xFC, 0xCC, 0x0C, 0x18, 0x30, 0x30, 0x30, 0x00,
0x78, 0xCC, 0xCC, 0x78, 0xCC, 0xCC, 0x78, 0x00,
0x78, 0xCC, 0xCC, 0x7C, 0x0C, 0x18, 0x70, 0x00,
0x00, 0x18, 0x18, 0x00, 0x00, 0x18, 0x18, 0x00,
0x00, 0x18, 0x18, 0x00, 0x00, 0x18, 0x18, 0x30,
0x18, 0x30, 0x60, 0xC0, 0x60, 0x30, 0x18, 0x00,
0x00, 0x00, 0x7E, 0x00, 0x7E, 0x00, 0x00, 0x00,
0x60, 0x30, 0x18, 0x0C, 0x18, 0x30, 0x60, 0x00,
0x3C, 0x66, 0x0C, 0x18, 0x18, 0x00, 0x18, 0x00,
0x7C, 0xC6, 0xDE, 0xDE, 0xDC, 0xC0, 0x7C, 0x00,
0x30, 0x78, 0xCC, 0xCC, 0xFC, 0xCC, 0xCC, 0x00,
0xFC, 0x66, 0x66, 0x7C, 0x66, 0x66, 0xFC, 0x00,
0x3C, 0x66, 0xC0, 0xC0, 0xC0, 0x66, 0x3C, 0x00,
0xF8, 0x6C, 0x66, 0x66, 0x66, 0x6C, 0xF8, 0x00,
0xFE, 0x62, 0x68, 0x78, 0x68, 0x62, 0xFE, 0x00,
0xFE, 0x62, 0x68, 0x78, 0x68, 0x60, 0xF0, 0x00,
0x3C, 0x66, 0xC0, 0xC0, 0xCE, 0x66, 0x3A, 0x00,
0xCC, 0xCC, 0xCC, 0xFC, 0xCC, 0xCC, 0xCC, 0x00,
0x78, 0x30, 0x30, 0x30, 0x30, 0x30, 0x78, 0x00,
0x1E, 0x0C, 0x0C, 0x0C, 0xCC, 0xCC, 0x78, 0x00,
0xE6, 0x66, 0x6C, 0x78, 0x6C, 0x66, 0xE6, 0x00,
0xF0, 0x60, 0x60, 0x60, 0x62, 0x66, 0xFE, 0x00,
0xC6, 0xEE, 0xFE, 0xFE, 0xD6, 0xC6, 0xC6, 0x00,
0xC6, 0xE6, 0xF6, 0xDE, 0xCE, 0xC6, 0xC6, 0x00,
0x38, 0x6C, 0xC6, 0xC6, 0xC6, 0x6C, 0x38, 0x00,
0xFC, 0x66, 0x66, 0x7C, 0x60, 0x60, 0xF0, 0x00,
0x7C, 0xC6, 0xC6, 0xC6, 0xD6, 0x7C, 0x0E, 0x00,
0xFC, 0x66, 0x66, 0x7C, 0x6C, 0x66, 0xE6, 0x00,
0x7C, 0xC6, 0xE0, 0x78, 0x0E, 0xC6, 0x7C, 0x00,
0xFC, 0xB4, 0x30, 0x30, 0x30, 0x30, 0x78, 0x00,
0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xFC, 0x00,
0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0x78, 0x30, 0x00,
0xC6, 0xC6, 0xC6, 0xC6, 0xD6, 0xFE, 0x6C, 0x00,
0xC6, 0xC6, 0x6C, 0x38, 0x6C, 0xC6, 0xC6, 0x00,
0xCC, 0xCC, 0xCC, 0x78, 0x30, 0x30, 0x78, 0x00,
0xFE, 0xC6, 0x8C, 0x18, 0x32, 0x66, 0xFE, 0x00,
0x78, 0x60, 0x60, 0x60, 0x60, 0x60, 0x78, 0x00,
0xC0, 0x60, 0x30, 0x18, 0x0C, 0x06, 0x02, 0x00,
0x78, 0x18, 0x18, 0x18, 0x18, 0x18, 0x78, 0x00,
0x10, 0x38, 0x6C, 0xC6, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF,
0x30, 0x30, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x78, 0x0C, 0x7C, 0xCC, 0x76, 0x00,
0xE0, 0x60, 0x60, 0x7C, 0x66, 0x66, 0xDC, 0x00,
0x00, 0x00, 0x78, 0xCC, 0xC0, 0xCC, 0x78, 0x00,
0x1C, 0x0C, 0x0C, 0x7C, 0xCC, 0xCC, 0x76, 0x00,
0x00, 0x00, 0x78, 0xCC, 0xFC, 0xC0, 0x78, 0x00,
0x38, 0x6C, 0x64, 0xF0, 0x60, 0x60, 0xF0, 0x00,
0x00, 0x00, 0x76, 0xCC, 0xCC, 0x7C, 0x0C, 0xF8,
0xE0, 0x60, 0x6C, 0x76, 0x66, 0x66, 0xE6, 0x00,
0x30, 0x00, 0x70, 0x30, 0x30, 0x30, 0x78, 0x00,
0x0C, 0x00, 0x1C, 0x0C, 0x0C, 0xCC, 0xCC, 0x78,
0xE0, 0x60, 0x66, 0x6C, 0x78, 0x6C, 0xE6, 0x00,
0x70, 0x30, 0x30, 0x30, 0x30, 0x30, 0x78, 0x00,
0x00, 0x00, 0xCC, 0xFE, 0xFE, 0xD6, 0xD6, 0x00,
0x00, 0x00, 0xB8, 0xCC, 0xCC, 0xCC, 0xCC, 0x00,
0x00, 0x00, 0x78, 0xCC, 0xCC, 0xCC, 0x78, 0x00,
0x00, 0x00, 0xDC, 0x66, 0x66, 0x7C, 0x60, 0xF0,
0x00, 0x00, 0x76, 0xCC, 0xCC, 0x7C, 0x0C, 0x1E,
0x00, 0x00, 0xDC, 0x76, 0x62, 0x60, 0xF0, 0x00,
0x00, 0x00, 0x7C, 0xC0, 0x70, 0x1C, 0xF8, 0x00,
0x10, 0x30, 0xFC, 0x30, 0x30, 0x34, 0x18, 0x00,
0x00, 0x00, 0xCC, 0xCC, 0xCC, 0xCC, 0x76, 0x00,
0x00, 0x00, 0xCC, 0xCC, 0xCC, 0x78, 0x30, 0x00,
0x00, 0x00, 0xC6, 0xC6, 0xD6, 0xFE, 0x6C, 0x00,
0x00, 0x00, 0xC6, 0x6C, 0x38, 0x6C, 0xC6, 0x00,
0x00, 0x00, 0xCC, 0xCC, 0xCC, 0x7C, 0x0C, 0xF8,
0x00, 0x00, 0xFC, 0x98, 0x30, 0x64, 0xFC, 0x00,
0x1C, 0x30, 0x30, 0xE0, 0x30, 0x30, 0x1C, 0x00,
0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00,
0xE0, 0x30, 0x30, 0x1C, 0x30, 0x30, 0xE0, 0x00,
0x76, 0xDC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x10, 0x38, 0x6C, 0xC6, 0xC6, 0xFE, 0x00,
0x7C, 0xC6, 0xC0, 0xC6, 0x7C, 0x0C, 0x06, 0x7C,
0x00, 0xCC, 0x00, 0xCC, 0xCC, 0xCC, 0x76, 0x00,
0x1C, 0x00, 0x78, 0xCC, 0xFC, 0xC0, 0x78, 0x00,