-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUVGADRV.PAS
267 lines (233 loc) · 6.73 KB
/
UVGADRV.PAS
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
{
UVGADrv Unit
VGA graphics adapter driver
2022 LRT
}
unit
uvgadrv;
interface
uses
uexc, uclasses, types, locale, uobject, uviddrv, int10, math,
ubitmap, ubitmap8, upalette, ulog;
type
PVGADriver = ^TVGADriver;
TVGADriver = object (TVideoDriver)
public
constructor init;
destructor done; virtual;
function isAvailable: boolean; virtual;
function supportsPalette: boolean; virtual;
function getCurrentVideoMode: PVideoMode; virtual;
function getBitmap: PBitmap; virtual;
procedure getVideoModes(var list:PVideoMode; var count: word); virtual;
procedure setVideoMode(index: word); virtual;
procedure setTextMode; virtual;
procedure setPalette(palette: PColorPalette); virtual;
function getDriverName: string; virtual;
function getClassName: string; virtual;
function getClassId: word; virtual;
private
_currentVideoMode: PVideoMode;
_bitmap: PBitmap;
end;
{ this represents the VGA video memory for mode 13th as a bitmap }
PVGAMode13Bitmap = ^TVGAMode13Bitmap;
TVGAMode13Bitmap = object (TBitmap)
public
constructor init;
destructor done; virtual;
procedure setPixel(x, y: word; color: TColor); virtual;
procedure setPixelBlock(x1, y1, x2, y2: word; color: TColor); virtual;
procedure merge(src: PBitmap; x, y: word); virtual;
function getPixel(x, y: word): TColor; virtual;
procedure clear(color: TColor); virtual;
function getColorCount: longint; virtual;
function getBitsPerPixel: byte; virtual;
function getClassName: string; virtual;
function getClassId: word; virtual;
end;
implementation
const
C_VGA_VIDEO_MODES_COUNT = 1;
C_VGA_VIDEO_MODES: array[0..C_VGA_VIDEO_MODES_COUNT-1] of TVideoMode = (
(resX: 320; resY: 200; colorCount: 256)
);
{ TVGADriver }
constructor TVGADriver.init;
begin
inherited init;
_currentVideoMode := nil;
_bitmap := nil;
end;
destructor TVGADriver.done;
begin
if _bitmap <> nil then _bitmap^.release;
inherited done;
end;
function TVGADriver.isAvailable: boolean;
var
a, i: byte;
res : boolean;
begin
res := int10.getVideoDisplayCombination(a, i);
isAvailable := res and (a >= C_DISPLAY_CODE_VGA_MONO);
end;
function TVGADriver.supportsPalette: boolean;
begin
supportsPalette := true;
end;
function TVGADriver.getCurrentVideoMode: PVideoMode;
begin
getCurrentVideoMode := _currentVideoMode;
end;
function TVGADriver.getBitmap: PBitmap;
begin
getBitmap := _bitmap;
end;
procedure TVGADriver.getVideoModes(var list:PVideoMode; var count: word);
begin
list := @C_VGA_VIDEO_MODES;
count := C_VGA_VIDEO_MODES_COUNT;
end;
procedure TVGADriver.setVideoMode(index: word);
const
C_VGA_MODES: array[0..C_VGA_VIDEO_MODES_COUNT-1] of byte = ( $13 );
begin
int10.setVideoMode(C_VGA_MODES[index]);
_currentVideoMode := @C_VGA_VIDEO_MODES;
inc(_currentVideoMode, index);
if _bitmap <> nil then _bitmap^.release;
case index of
0: _bitmap := new(PVGAMode13Bitmap, init);
else
iassert(false, @self, 0, S_ERR_UNSUPPORTED_ACTION);
end;
end;
procedure TVGADriver.setTextMode;
begin
int10.setVideoMode(C_VIDMODE_CGA_T_80x25_16);
end;
procedure TVGADriver.setPalette(palette: PColorPalette);
var
rgbColors: PRGBColor;
count, c, size: word;
shiftedColors: PRGBColor;
oldColor, newColor: PRGBColor;
begin
palette^.getColors(rgbColors, count);
size := count * sizeof(TRGBColor);
getMem(shiftedColors, size);
c := count;
oldColor := rgbColors;
newColor := shiftedColors;
while c>0 do
begin
newColor^.r := oldColor^.r shr 2; { needs to be shifted twice to the }
newColor^.g := oldColor^.g shr 2; { right because the palette uses }
newColor^.b := oldColor^.b shr 2; { 0..255 and VGA expects 0..63 }
inc(oldColor);
inc(newColor);
dec(c);
end;
setDACColorRegistersBlock(shiftedColors, count, 0);
freeMem(shiftedColors, size);
end;
function TVGADriver.getDriverName: string;
begin
getDriverName := 'VGA';
end;
function TVGADriver.getClassName: string;
begin
getClassName := 'TVGADriver';
end;
function TVGADriver.getClassId: word;
begin
getClassId := C_CLASS_ID_VGADriver;
end;
{ TVGAMode13Bitmap public }
constructor TVGAMode13Bitmap.init;
begin
inherited initWithSize(320, 200);
end;
destructor TVGAMode13Bitmap.done;
begin
inherited done;
end;
procedure TVGAMode13Bitmap.setPixel(x, y: word; color: TColor);
begin
if (x>319) or (y>199) then exit;
mem[$A000 : y shl 8 + y shl 6 + x] := color;
end;
procedure TVGAMode13Bitmap.setPixelBlock(x1, y1, x2, y2: word; color: TColor);
var
p: pByte;
w, y: word;
begin
w := x2-x1+1;
p := ptr($A000, y2 shl 8 + y2 shl 6 + x1);
for y:= y2-y1 downto 0 do
begin
fillchar(p^, w, color);
dec(p, 320);
end;
end;
procedure TVGAMode13Bitmap.merge(src: PBitmap; x, y: word);
var
sw, sh: word;
sr, sp, tp: pbyte;
sx, sy, i: word;
begin
sw := src^.getWidth;
sh := src^.getHeight;
if (src^.getBitsPerPixel = 8) then
begin
if (x = 0) and (y = 0) and (sw = 320) and (sh = 200) then
Move(src^.getRawPixels^, ptr($A000, 0)^, sw * sh)
else
begin
sw := src^.getWidth;
sh := src^.getHeight;
sr := src^.getRawPixels;
sx := minword(320 - x, sw);
sy := minword(200 - y, sh);
i := sy;
tp := ptr($A000, y shl 8 + y shl 6 + x);
sp := sr;
while i>0 do
begin
Move(sp^, tp^, sw);
dec(i);
inc(tp, 320);
inc(sp, sw);
end;
end;
end else
inherited merge(src, x, y);
end;
procedure TVGAMode13Bitmap.clear(color: TColor);
begin
fillchar(Ptr($A000,0)^, 64000, color);
end;
function TVGAMode13Bitmap.getPixel(x, y: word): TColor;
begin
if (x>319) or (y>199) then exit;
getPixel := mem[$A000 : y shl 8 + y shl 6 + x];
end;
function TVGAMode13Bitmap.getColorCount: longint;
begin
getColorCount := 256;
end;
function TVGAMode13Bitmap.getBitsPerPixel: byte;
begin
getBitsPerPixel := 8;
end;
function TVGAMode13Bitmap.getClassName: string;
begin
getClassName := 'TVGAMode13Bitmap';
end;
function TVGAMode13Bitmap.getClassId: word;
begin
getClassId := C_CLASS_ID_VGAMode13Bitmap;
end;
{ Other }
end.