7 #include "lib\modex16.h"
\r
10 byte far* VGA=(byte far*) 0xA0000000; /* this points to video memory. */
\r
12 static void fadePalette(sbyte fade, sbyte start, word iter, byte *palette);
\r
13 static byte tmppal[PAL_SIZE];
\r
14 static struct pcxHeader {
\r
36 vgaSetMode(byte mode)
\r
40 regs.h.ah = SET_MODE;
\r
42 int86(VIDEO_INT, ®s, ®s);
\r
46 /* -========================= Entry Points ==========================- */
\r
50 dword far*ptr=(dword far*)VGA; /* used for faster screen clearing */
\r
52 0x0d06, /* vertical total */
\r
53 0x3e07, /* overflow (bit 8 of vertical counts) */
\r
54 0x4109, /* cell height (2 to double-scan */
\r
55 0xea10, /* v sync start */
\r
56 0xac11, /* v sync end and protect cr0-cr7 */
\r
57 0xdf12, /* vertical displayed */
\r
58 0x0014, /* turn off dword mode */
\r
59 0xe715, /* v blank start */
\r
60 0x0616, /* v blank end */
\r
61 0xe317 /* turn on byte mode */
\r
63 int CRTParmCount = sizeof(CRTParms) / sizeof(CRTParms[0]);
\r
65 /* TODO save current video mode and palette */
\r
66 vgaSetMode(VGA_256_COLOR_MODE);
\r
68 /* disable chain4 mode */
\r
69 outpw(SC_INDEX, 0x0604);
\r
71 /* synchronous reset while setting Misc Output */
\r
72 outpw(SC_INDEX, 0x0100);
\r
74 /* select 25 MHz dot clock & 60 Hz scanning rate */
\r
75 outp(MISC_OUTPUT, 0xe3);
\r
77 /* undo reset (restart sequencer) */
\r
78 outpw(SC_INDEX, 0x0300);
\r
80 /* reprogram the CRT controller */
\r
81 outp(CRTC_INDEX, 0x11); /* VSync End reg contains register write prot */
\r
82 outp(CRTC_DATA, 0x7f); /* get current write protect on varios regs */
\r
84 /* send the CRTParms */
\r
85 for(i=0; i<CRTParmCount; i++) {
\r
86 outpw(CRTC_INDEX, CRTParms[i]);
\r
89 /* clear video memory */
\r
90 outpw(SC_INDEX, 0x0f02);
\r
91 for(i=0; i<0x8000; i++) {
\r
98 /////////////////////////////////////////////////////////////////////////////
\r
100 // setvideo() - This function Manages the video modes //
\r
102 /////////////////////////////////////////////////////////////////////////////
\r
103 void setvideo(/*byte mode, */short vq){
\r
104 union REGS in, out;
\r
106 if(!vq){ // deinit the video
\r
107 // change to the video mode we were in before we switched to mode 13h
\r
109 in.h.al = old_mode;
\r
110 int86(0x10, &in, &out);
\r
112 }else if(vq==1){ // init the video
\r
113 // get old video mode
\r
115 int86(0x10, &in, &out);
\r
116 old_mode = out.h.al;
\r
123 modexDefaultPage() {
\r
126 /* default page values */
\r
130 page.width = SCREEN_WIDTH;
\r
131 page.height = SCREEN_HEIGHT;
\r
136 /* returns the next page in contiguous memory
\r
137 * the next page will be the same size as p, by default
\r
140 modexNextPage(page_t *p) {
\r
143 result.data = p->data + (p->width/4)*p->height; /* compute the offset */
\r
146 result.width = p->width;
\r
147 result.height = p->height;
\r
154 modexShowPage(page_t *page) {
\r
160 /* calculate offset */
\r
161 offset = (word) page->data;
\r
162 offset += page->dy * (page->width >> 2 );
\r
163 offset += page->dx >> 2;
\r
165 /* calculate crtcOffset according to virtual width */
\r
166 crtcOffset = page->width >> 3;
\r
168 high_address = HIGH_ADDRESS | (offset & 0xff00);
\r
169 low_address = LOW_ADDRESS | (offset << 8);
\r
171 /* wait for appropriate timing and then program CRTC */
\r
172 while ((inp(INPUT_STATUS_1) & DISPLAY_ENABLE));
\r
173 outpw(CRTC_INDEX, high_address);
\r
174 outpw(CRTC_INDEX, low_address);
\r
175 outp(CRTC_INDEX, 0x13);
\r
176 outp(CRTC_DATA, crtcOffset);
\r
178 /* wait for one retrace */
\r
179 while (!(inp(INPUT_STATUS_1) & VRETRACE));
\r
181 /* do PEL panning here */
\r
182 outp(AC_INDEX, 0x33);
\r
183 outp(AC_INDEX, (page->dx & 0x03) << 1);
\r
188 modexPanPage(page_t *page, int dx, int dy) {
\r
195 modexSelectPlane(byte plane) {
\r
196 outp(SC_INDEX, MAP_MASK); /* select plane */
\r
197 outp(SC_DATA, plane);
\r
202 modexClearRegion(page_t *page, int x, int y, int w, int h, byte color) {
\r
203 word pageOff = (word) page->data;
\r
204 word xoff=x/4; /* xoffset that begins each row */
\r
205 word scanCount=w/4; /* number of iterations per row (excluding right clip)*/
\r
206 word poffset = pageOff + y*(page->width/4) + xoff; /* starting offset */
\r
207 word nextRow = page->width/4-scanCount-1; /* loc of next row */
\r
208 byte lclip[] = {0x0f, 0x0e, 0x0c, 0x08}; /* clips for rectangles not on 4s */
\r
209 byte rclip[] = {0x00, 0x01, 0x03, 0x07};
\r
210 byte left = lclip[x&0x03];
\r
211 byte right = rclip[(x+w)&0x03];
\r
213 /* handle the case which requires an extra group */
\r
214 if((x & 0x03) && !((x+w) & 0x03)) {
\r
219 MOV AX, SCREEN_SEG ; go to the VGA memory
\r
221 MOV DI, poffset ; go to the first pixel
\r
222 MOV DX, SC_INDEX ; point to the map mask
\r
226 MOV AL, color ; get ready to write colors
\r
228 MOV CX, scanCount ; count the line
\r
229 MOV BL, AL ; remember color
\r
230 MOV AL, left ; do the left clip
\r
231 OUT DX, AL ; set the left clip
\r
232 MOV AL, BL ; restore color
\r
233 STOSB ; write the color
\r
235 JZ SCAN_DONE ; handle 1 group stuff
\r
237 ;-- write the main body of the scanline
\r
238 MOV BL, AL ; remember color
\r
239 MOV AL, 0x0f ; write to all pixels
\r
241 MOV AL, BL ; restore color
\r
242 REP STOSB ; write the color
\r
244 MOV BL, AL ; remeber color
\r
246 OUT DX, AL ; do the right clip
\r
247 MOV AL, BL ; restore color
\r
248 STOSB ; write pixel
\r
249 ADD DI, nextRow ; go to the next row
\r
257 modexDrawBmp(page_t *page, int x, int y, bitmap_t *bmp) {
\r
258 /* draw the region (the entire freakin bitmap) */
\r
259 modexDrawBmpRegion(page, x, y, 0, 0, bmp->width, bmp->height, bmp);
\r
264 modexDrawBmpRegion(page_t *page, int x, int y,
\r
265 int rx, int ry, int rw, int rh, bitmap_t *bmp) {
\r
266 word poffset = (word) page->data + y*(page->width/4) + x/4;
\r
267 byte *data = bmp->data;
\r
268 word bmpOffset = (word) data + ry * bmp->width + rx;
\r
271 byte plane = 1 << ((byte) x & 0x03);
\r
272 word scanCount = width/4 + (width%4 ? 1 :0);
\r
273 word nextPageRow = page->width/4 - scanCount;
\r
274 word nextBmpRow = (word) bmp->width - width;
\r
276 byte planeCounter = 4;
\r
279 MOV AX, SCREEN_SEG ; go to the VGA memory
\r
282 MOV DX, SC_INDEX ; point at the map mask register
\r
287 MOV DX, SC_DATA ; select the current plane
\r
291 ;-- begin plane painting
\r
292 MOV AX, height ; start the row counter
\r
293 MOV rowCounter, AX ;
\r
294 MOV DI, poffset ; go to the first pixel
\r
295 MOV SI, bmpOffset ; go to the bmp pixel
\r
297 MOV CX, width ; count the columns
\r
299 MOVSB ; copy the pixel
\r
300 SUB CX, 3 ; we skip the next 3
\r
301 ADD SI, 3 ; skip the bmp pixels
\r
302 LOOP SCAN_LOOP ; finish the scan
\r
304 MOV AX, nextPageRow
\r
305 ADD DI, AX ; go to the next row on screen
\r
307 ADD SI, AX ; go to the next row on bmp
\r
310 JNZ ROW_LOOP ; do all the rows
\r
311 ;-- end plane painting
\r
313 MOV AL, plane ; advance to the next plane
\r
315 AND AL, 0x0f ; mask the plane properly
\r
316 MOV plane, AL ; store the plane
\r
318 INC bmpOffset ; start bmp at the right spot
\r
321 JNZ PLANE_LOOP ; do all 4 planes
\r
327 modexDrawSprite(page_t *page, int x, int y, bitmap_t *bmp) {
\r
328 /* draw the whole sprite */
\r
329 modexDrawSpriteRegion(page, x, y, 0, 0, bmp->width, bmp->height, bmp);
\r
333 modexDrawSpriteRegion(page_t *page, int x, int y,
\r
334 int rx, int ry, int rw, int rh, bitmap_t *bmp) {
\r
335 word poffset = (word)page->data + y*(page->width/4) + x/4;
\r
336 byte *data = bmp->data;
\r
337 word bmpOffset = (word) data + ry * bmp->width + rx;
\r
340 byte plane = 1 << ((byte) x & 0x03);
\r
341 word scanCount = width/4 + (width%4 ? 1 :0);
\r
342 word nextPageRow = page->width/4 - scanCount;
\r
343 word nextBmpRow = (word) bmp->width - width;
\r
345 byte planeCounter = 4;
\r
348 MOV AX, SCREEN_SEG ; go to the VGA memory
\r
351 MOV DX, SC_INDEX ; point at the map mask register
\r
356 MOV DX, SC_DATA ; select the current plane
\r
360 ;-- begin plane painting
\r
361 MOV AX, height ; start the row counter
\r
362 MOV rowCounter, AX ;
\r
363 MOV DI, poffset ; go to the first pixel
\r
364 MOV SI, bmpOffset ; go to the bmp pixel
\r
366 MOV CX, width ; count the columns
\r
371 JNE DRAW_PIXEL ; draw non-zero pixels
\r
373 INC DI ; skip the transparent pixel
\r
377 MOVSB ; copy the pixel
\r
379 SUB CX, 3 ; we skip the next 3
\r
380 ADD SI, 3 ; skip the bmp pixels
\r
381 LOOP SCAN_LOOP ; finish the scan
\r
383 MOV AX, nextPageRow
\r
384 ADD DI, AX ; go to the next row on screen
\r
386 ADD SI, AX ; go to the next row on bmp
\r
389 JNZ ROW_LOOP ; do all the rows
\r
390 ;-- end plane painting
\r
392 MOV AL, plane ; advance to the next plane
\r
394 AND AL, 0x0f ; mask the plane properly
\r
395 MOV plane, AL ; store the plane
\r
397 INC bmpOffset ; start bmp at the right spot
\r
400 JNZ PLANE_LOOP ; do all 4 planes
\r
406 modexCopyPageRegion(page_t *dest, page_t src,
\r
409 word width, word height)
\r
415 /* fade and flash */
\r
417 modexFadeOn(word fade, byte *palette) {
\r
418 fadePalette(-fade, 64, 64/fade+1, palette);
\r
423 modexFadeOff(word fade, byte *palette) {
\r
424 fadePalette(fade, 0, 64/fade+1, palette);
\r
429 modexFlashOn(word fade, byte *palette) {
\r
430 fadePalette(fade, -64, 64/fade+1, palette);
\r
435 modexFlashOff(word fade, byte *palette) {
\r
436 fadePalette(-fade, 0, 64/fade+1, palette);
\r
441 fadePalette(sbyte fade, sbyte start, word iter, byte *palette) {
\r
445 /* handle the case where we just update */
\r
447 modexPalUpdate(palette);
\r
451 while(iter > 0) { /* FadeLoop */
\r
452 for(i=0; i<PAL_SIZE; i++) { /* loadpal_loop */
\r
453 tmppal[i] = palette[i] - dim;
\r
454 if(tmppal[i] > 127) {
\r
456 } else if(tmppal[i] > 63) {
\r
460 modexPalUpdate(tmppal);
\r
467 /* save and load */
\r
469 modexPalSave(byte *palette) {
\r
472 outp(PAL_READ_REG, 0); /* start at palette entry 0 */
\r
473 for(i=0; i<PAL_SIZE; i++) {
\r
474 palette[i] = inp(PAL_DATA_REG); /* read the palette data */
\r
482 ptr = malloc(PAL_SIZE);
\r
484 /* handle errors */
\r
486 printf("Could not allocate palette.\n");
\r
495 modexLoadPalFile(byte *filename, byte **palette) {
\r
499 /* free the palette if it exists */
\r
504 /* allocate the new palette */
\r
505 *palette = modexNewPal();
\r
507 /* open the file */
\r
508 file = fopen(filename, "rb");
\r
510 printf("Could not open palette file: %s\n", filename);
\r
514 /* read the file */
\r
516 while(!feof(file)) {
\r
517 *ptr++ = fgetc(file);
\r
525 modexSavePalFile(char *filename, byte *pal) {
\r
529 /* open the file for writing */
\r
530 file = fopen(filename, "wb");
\r
532 printf("Could not open %s for writing\n", filename);
\r
536 /* write the data to the file */
\r
537 fwrite(pal, 1, PAL_SIZE, file);
\r
545 fadePalette(-1, 64, 1, tmppal);
\r
551 fadePalette(-1, -64, 1, tmppal);
\r
557 modexPalUpdate(byte *p) {
\r
560 outp(PAL_WRITE_REG, 0); /* start at the beginning of palette */
\r
561 for(i=0; i<PAL_SIZE/2; i++) {
\r
562 outp(PAL_DATA_REG, p[i]);
\r
564 modexWaitBorder(); /* waits one retrace -- less flicker */
\r
565 for(i=PAL_SIZE/2; i<PAL_SIZE; i++) {
\r
566 outp(PAL_DATA_REG, p[i]);
\r
572 modexWaitBorder() {
\r
573 while(inp(INPUT_STATUS_1) & 8) {
\r
577 while(!(inp(INPUT_STATUS_1) & 8)) {
\r
584 modexLoadPcx(char *filename) {
\r
587 struct pcxHeader head;
\r
592 /* open the PCX file for reading */
\r
593 file = fopen(filename, "rb");
\r
595 printf("Could not open %s for reading.\n", filename);
\r
599 /* read the header */
\r
600 fread(&head, sizeof(char), sizeof(struct pcxHeader), file);
\r
602 /* make sure this is 8bpp */
\r
603 if(head.bpp != 8) {
\r
604 printf("I only know how to handle 8bpp pcx files!\n");
\r
609 /* allocate the buffer */
\r
610 result.width = head.xmax - head.xmin + 1;
\r
611 result.height = head.ymax - head.ymin + 1;
\r
612 bufSize = result.width * result.height;
\r
613 result.data = malloc(bufSize);
\r
615 printf("Could not allocate memory for bitmap data.");
\r
620 /* read the buffer in */
\r
623 /* get the run length and the value */
\r
624 count = fgetc(file);
\r
625 if(0xC0 == (count & 0xC0)) { /* this is the run count */
\r
633 /* write the pixel the specified number of times */
\r
634 for(; count && index < bufSize; count--,index++) {
\r
635 result.data[index] = val;
\r
637 } while(index < bufSize);
\r
639 /* handle the palette */
\r
640 fseek(file, -769, SEEK_END);
\r
642 result.palette = modexNewPal();
\r
643 if(head.version == 5 && val == 12) {
\r
644 /* use the vga palette */
\r
645 for(index=0; !feof(file) && index < PAL_SIZE; index++) {
\r
647 result.palette[index] = val >> 2;
\r
650 /* use the 16 color palette */
\r
651 for(index=0; index<48; index++) {
\r
652 result.palette[index] = head.pal16[index];
\r