-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcga.cpp
630 lines (548 loc) · 12.2 KB
/
cga.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
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <math.h>
#include "image.h"
#include "types.h"
#include "memory.h"
#include "cga.h"
#ifdef __GNUC__
#include <iostream>
using namespace std;
#endif
/* mode, width, height, bytes_per_line, base_addr, bpp, Bpp, colors, vsync_reg, scale */
const adapter::video_mode cga::video_modes[] = {
{TEXT,80,25,80,0xb800,8,8,0xF,0x3da,0},
{CGALO1,320,200,80,0xb800,2,8,0xF,0x3da,0},
{CGALO2,320,200,80,0xb800,2,8,0xF,0x3da,0},
{CGAHI,640,200,80,0xb800,1,8,0xF,0x3da,0},
{X16,160,100,160,0xb800,1,8,0xF,0x3da,4}
};
cga::cga(void)
{
_savedvmode = getmode();
pal_reg.reg=0;
}
cga::~cga(void)
{
setmode(_savedvmode);
}
bool cga::setup(void)
{
for (unsigned long int i=0;i<sizeof(video_modes);i++) {
if (video_modes[i].mode == _vmode) {
_row_bytes = video_modes[i].bytes;
_width = video_modes[i].x;
_height = video_modes[i].y;
bpp = video_modes[i].bpp;
Bpp = 8/bpp;
planes = video_modes[i].planes;
colors = video_modes[i].colors;
SR = video_modes[i].sr;
buf_size = _row_bytes*_height;
#ifdef __BORLANDC__
_buffer = (unsigned char far *) MK_FP(video_modes[i].fp,0);
screen.size(_width,_height);
#endif
return true;
}
}
return false;
}
void cga::setmode(Mode mode)
{
#ifdef __BORLANDC__
_AL=(unsigned char) mode;
_AH=0;
geninterrupt(0x10);
#endif
}
bool cga::graphmode(Mode mode)
{
switch (mode) {
case TEXT:
return textmode();
case X16:
setpalette(palette::TEXT_PAL);
return x16mode();
case CGALO1:
setpalette(palette::CGA1HI_PAL);
break;
case CGALO2:
setpalette(palette::CGA2HI_PAL);
break;
case CGAHI:
setpalette(palette::BW_PAL);
break;
case UNDEF:
case COMPAQ:
case HERC:
case BWTEXT:
case PCJRLO:
case PCJRMED:
case PCJRHI:
case EGALO:
case EGAMED:
case EGAMONO:
case EGAHI:
case VGAMONO:
case VGAHI:
case VGALO:
case SVGA:
case XGA:
case HD:
case FHD:
return false;
}
_vmode=mode;
setmode(_vmode);
return setup();
}
bool cga::textmode(void)
{
#ifdef __BORLANDC__
setmode(TEXT);
return setup();
#else
return true;
#endif
}
bool cga::x16mode(void)
{
#ifdef __BORLANDC__
unsigned char status;
setmode(TEXT);
/* set mode control register for 80x25 text mode and disable video output */
outportb(CRTCa, 1);
/*
These settings put the 6845 into "graphics" mode without actually
switching the CGA controller into graphics mode. The register
values are directly copied from CGA graphics mode register
settings. The 6845 does not directly display graphics, the
6845 only generates addresses and sync signals, the CGA
attribute controller either displays character ROM data or color
pixel data, this is external to the 6845 and keeps the CGA card
in text mode.
ref: HELPPC
*/
/* set vert total lines to 127 */
write_crtc(CRTCb,0x04,0x7f);
/* set vert displayed char rows to 100 */
write_crtc(CRTCb,0x06,0x64);
/* set vert sync position to 112 */
write_crtc(CRTCb,0x07,0x70);
/* set char scan line count to 1 */
write_crtc(CRTCb,0x09,0x01);
/* re-enable the video output in 80x25 text mode */
outportb(CRTCa, 9);
_vmode=X16;
return setup();
#else
return true;
#endif
}
void cga::write_crtc(unsigned int port, unsigned char reg, unsigned char val)
{
#ifdef __BORLANDC__
outportb(port, reg);
outportb(port+1,val);
#endif
}
adapter::Mode cga::getmode(void)
{
#ifdef __BORLANDC__
_AX=0x0f00;
geninterrupt(0x10);
_vmode=(Mode)_AL;
return (_vmode);
#else
return _vmode;
#endif
}
void cga::cls(void)
{
screen.clear();
update();
}
void cga::update(void)
{
vsync();
translate(screen._buffer);
}
void cga::translate(ptr_t src)
{
#ifdef __BORLANDC__
switch (_vmode) {
case TEXT:
_CX=buf_size>>1;
_DI=FP_OFF(_buffer);
_SI=FP_OFF(src);
_BX=FP_SEG(src);
_DS=_BX;
_AX=FP_SEG(_buffer);
_ES=_AX;
xlatetext:
asm {
lodsw // 5
mov bx, ax
shl ax,8
mov al,0xDB
stosw
mov ax, bx
mov al,0xDB
stosw
loop xlatetext // 15
} // 44
break;
case X16:
#ifdef i86
_CX=buf_size>>1;
_DI=FP_OFF(_buffer);
_SI=FP_OFF(src);
_BX=FP_SEG(src);
_DS=_BX;
_AX=FP_SEG(_buffer);
_ES=_AX;
xlate16:
asm {
lodsw // 5
shl al, 4 // 3
or ah, al // 2
mov al, 0xde // 2
stosw // 4
loop xlate16 // 15
} // 44
#else
_CX=buf_size>>2;
_DI=FP_OFF(_buffer);
_SI=FP_OFF(src);
_BX=FP_SEG(src);
_DS=_BX;
_AX=FP_SEG(_buffer);
_ES=_AX;
xlate32:
asm {
lodsd // 5
//and eax, 0x0F0F0F0F // 2
shl al, 4 // 3
or ah, al // 2
mov al, 0xde // 2
ror eax, 16 // 3
shl al, 4 // 3
or ah, al // 2
mov al, 0xde // 2
rol eax, 16 // 3
stosd // 4
loop xlate32 // 15
} // 44
#endif
break;
case CGALO1:
case CGALO2:
_DX=_height>>1;
_SI=FP_OFF(src);
_DI=FP_OFF(_buffer);
_CX=FP_SEG(src);
_AX=FP_SEG(_buffer);
_DS=_CX;
_ES=_AX;
#ifdef i86
xlatelo:
asm {
mov cx, 80
}
xevenlo:
asm {
lodsw // 5
// HHHH HHHH LLLL LLLL
shl al, 2 // 3 0000 00xx 0000 xx00
or al, ah // 2 0000 00xx 0000 xxxx
shl al, 4 // 2 0000 00xx xxxx 0000
mov bx, ax
lodsw
shl al, 2 // 3 0000 00xx 0000 xx00
or al, ah // 2 0000 00xx 0000 xxxx
or al, bl // 2 0000 0000 xxxx xxxx
stosb // 4
loop xevenlo //
push di
add di, 0x1FB0
mov cx, 80
} // 44
xoddlo:
asm {
lodsw // 5
// HHHH HHHH LLLL LLLL
shl al, 2 // 3 0000 00xx 0000 xx00
or al, ah // 2 0000 00xx 0000 xxxx
shl al, 4 // 2 0000 00xx xxxx 0000
mov bx, ax
lodsw
shl al, 2 // 3 0000 00xx 0000 xx00
or al, ah // 2 0000 00xx 0000 xxxx
or al, bl // 2 0000 0000 xxxx xxxx
stosb // 4
loop xoddlo //
pop di
dec dx
jnz xlatelo
} // 44
#else
xlatelo:
asm {
mov cx,80
}
xevenlo:
asm {
lodsd // 5
// HHHH HHHH LLLL LLLL
shl al, 2 // 3 0000 00xx 0000 00xx 0000 00xx 0000 xx00
or al, ah // 2 0000 00xx 0000 00xx 0000 00xx 0000 xxxx
ror eax, 16 // 3 0000 00xx 0000 xxxx 0000 00xx 0000 00xx
shl al, 2 // 3 0000 00xx 0000 xxxx 0000 00xx 0000 xx00
or al, ah // 2 0000 00xx 0000 xxxx 0000 00xx 0000 xxxx
shl ax, 12 // 0000 00xx 0000 xxxx xxxx 0000 0000 0000
shr eax,12 // 0000 0000 0000 0000 00xx 0000 xxxx xxxx
stosb // 4
loop xevenlo //
push di
add di, 0x1FB0
mov cx,80
} // 44
xoddlo:
asm {
lodsd // 5
// HHHH HHHH LLLL LLLL
shl al, 2 // 3 0000 00xx 0000 00xx 0000 00xx 0000 xx00
or al, ah // 2 0000 00xx 0000 00xx 0000 00xx 0000 xxxx
ror eax, 16 // 3 0000 00xx 0000 xxxx 0000 00xx 0000 00xx
shl al, 2 // 3 0000 00xx 0000 xxxx 0000 00xx 0000 xx00
or al, ah // 2 0000 00xx 0000 xxxx 0000 00xx 0000 xxxx
shl ax, 12 // 0000 00xx 0000 xxxx xxxx 0000 0000 0000
shr eax,12 // 0000 0000 0000 0000 00xx 0000 xxxx xxxx
stosb // 4
loop xoddlo //
pop di
dec dx
jnz xlatelo
} // 44
#endif
break;
case CGAHI:
_DX=_height>>2;
_SI=FP_OFF(src);
_DI=FP_OFF(_buffer);
_CX=FP_SEG(src);
_AX=FP_SEG(_buffer);
// _BX=_row_bytes;
_DS=_CX;
_ES=_AX;
#ifdef i86
xlatehi:
asm {
mov cx,80
}
xevenhi:
asm {
lodsw // 5
// HHHH HHHH LLLL LLLL
shr ax, 1
rcl bx, 1
shr ax, 8
rcl bx, 1
lodsw
shr ax, 1
rcl bx, 1
shr ax, 8
rcl bx, 1
lodsw
shr ax, 1
rcl bx, 1
shr ax, 8
rcl bx, 1
lodsw
shr ax, 1
rcl bx, 1
shr ax, 8
rcl bx, 1
// mov ax,bx
mov es:[di],bl
inc di
// stosb // 4
loop xevenhi //
push di
add di, 0x1FB0
mov cx,80
} // 44
xoddhi:
asm {
lodsw // 5
// HHHH HHHH LLLL LLLL
shr ax, 1
rcl bx, 1
shr ax, 8
rcl bx, 1
lodsw
shr ax, 1
rcl bx, 1
shr ax, 8
rcl bx, 1
lodsw
shr ax, 1
rcl bx, 1
shr ax, 8
rcl bx, 1
lodsw
shr ax, 1
rcl bx, 1
shr ax, 8
rcl bx, 1
// mov ax,bx
mov es:[di],bl
inc di
// HHHH HHHH LLLL LLLL
loop xoddhi //
pop di
dec dx
jnz xlatehi
} // 44
#else
xlatehi:
asm {
mov cx,80
}
xevenhi:
asm {
lodsd // 5
// HHHH HHHH LLLL LLLL
shr eax, 1
rcl bx, 1
shr eax, 8
rcl bx, 1
shr eax, 8
rcl bx, 1
shr eax, 8
rcl bx, 1
lodsd
shr eax, 1
rcl bx, 1
shr eax, 8
rcl bx, 1
shr eax, 8
rcl bx, 1
shr eax, 8
rcl bx, 1
// mov ax,bx
mov es:[di],bl
inc di
// stosb // 4
loop xevenhi //
push di
add di, 0x1FB0
mov cx,80
} // 44
xoddhi:
asm {
lodsd // 5
// HHHH HHHH LLLL LLLL
shr eax, 1
rcl bx, 1
shr eax, 8
rcl bx, 1
shr eax, 8
rcl bx, 1
shr eax, 8
rcl bx, 1
lodsd
shr eax, 1
rcl bx, 1
shr eax, 8
rcl bx, 1
shr eax, 8
rcl bx, 1
shr eax, 8
rcl bx, 1
// mov ax,bx
mov es:[di],bl
inc di
// HHHH HHHH LLLL LLLL
loop xoddhi //
pop di
dec dx
jnz xlatehi
} // 44
#endif
break;
}
#endif
}
void cga::vsync(void)
{
#ifdef __BORLANDC__
unsigned char p;
do {
p=inportb(SR);
} while (p & 8);
do {
p=inportb(SR);
} while (!(p & 8));
#endif
}
bool cga::setpalette(palette::pal_type pal)
{
_cur_palette = pal;
switch (pal) {
case palette::CGA0_PAL:
pal_reg.data.pal = 0;
pal_reg.data.fg_int = 0;
if (_vmode != adapter::CGALO2) setmode(adapter::CGALO1);
break;
case palette::CGA1_PAL:
pal_reg.data.pal = 1;
pal_reg.data.fg_int = 0;
if (_vmode != adapter::CGALO2) setmode(adapter::CGALO1);
break;
case palette::CGA0HI_PAL:
pal_reg.data.pal = 0;
pal_reg.data.fg_int = 1;
if (_vmode != adapter::CGALO2) setmode(adapter::CGALO1);
break;
case palette::CGA1HI_PAL:
pal_reg.data.pal = 1;
pal_reg.data.fg_int = 1;
if (_vmode != adapter::CGALO2) setmode(adapter::CGALO1);
break;
case palette::CGA2_PAL:
pal_reg.data.pal = 0;
pal_reg.data.fg_int = 0;
if (_vmode != adapter::CGALO2) setmode(adapter::CGALO2);
break;
case palette::CGA2HI_PAL:
pal_reg.data.pal = 0;
pal_reg.data.fg_int = 1;
if (_vmode != adapter::CGALO2) setmode(adapter::CGALO2);
break;
case palette::BW_PAL:
pal_reg.data.pal = 0;
pal_reg.data.fg_int = 0;
break;
case palette::TEXT_PAL:
case palette::VGA_PAL:
case palette::RGB_PAL:
case palette::WEB_PAL:
case palette::X11_PAL:
case palette::CUSTOM:
case palette::NONE:
return false;
}
#ifdef __BORLANDC__
outportb(CGA_PAL_REG,pal_reg.reg);
#endif
return true;
}
bool cga::setpalette(palette::pal_t *pal, int palette_entries)
{
// TODO
return false;
}