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
99 /* TODO restore original mode and palette */
\r
100 vgaSetMode(TEXT_MODE);
\r
105 modexDefaultPage() {
\r
108 /* default page values */
\r
112 page.width = SCREEN_WIDTH;
\r
113 page.height = SCREEN_HEIGHT;
\r
118 /* returns the next page in contiguous memory
\r
119 * the next page will be the same size as p, by default
\r
122 modexNextPage(page_t *p) {
\r
125 result.data = p->data + (p->width/4)*p->height; /* compute the offset */
\r
128 result.width = p->width;
\r
129 result.height = p->height;
\r
136 modexShowPage(page_t *page) {
\r
142 /* calculate offset */
\r
143 offset = (word) page->data;
\r
144 offset += page->dy * (page->width >> 2 );
\r
145 offset += page->dx >> 2;
\r
147 /* calculate crtcOffset according to virtual width */
\r
148 crtcOffset = page->width >> 3;
\r
150 high_address = HIGH_ADDRESS | (offset & 0xff00);
\r
151 low_address = LOW_ADDRESS | (offset << 8);
\r
153 /* wait for appropriate timing and then program CRTC */
\r
154 while ((inp(INPUT_STATUS_1) & DISPLAY_ENABLE));
\r
155 outpw(CRTC_INDEX, high_address);
\r
156 outpw(CRTC_INDEX, low_address);
\r
157 outp(CRTC_INDEX, 0x13);
\r
158 outp(CRTC_DATA, crtcOffset);
\r
160 /* wait for one retrace */
\r
161 while (!(inp(INPUT_STATUS_1) & VRETRACE));
\r
163 /* do PEL panning here */
\r
164 outp(AC_INDEX, 0x33);
\r
165 outp(AC_INDEX, (page->dx & 0x03) << 1);
\r
170 modexPanPage(page_t *page, int dx, int dy) {
\r
177 modexSelectPlane(byte plane) {
\r
178 outp(SC_INDEX, MAP_MASK); /* select plane */
\r
179 outp(SC_DATA, plane);
\r
184 modexClearRegion(page_t *page, int x, int y, int w, int h, byte color) {
\r
185 word pageOff = (word) page->data;
\r
186 word xoff=x/4; /* xoffset that begins each row */
\r
187 word scanCount=w/4; /* number of iterations per row (excluding right clip)*/
\r
188 word poffset = pageOff + y*(page->width/4) + xoff; /* starting offset */
\r
189 word nextRow = page->width/4-scanCount-1; /* loc of next row */
\r
190 byte lclip[] = {0x0f, 0x0e, 0x0c, 0x08}; /* clips for rectangles not on 4s */
\r
191 byte rclip[] = {0x00, 0x01, 0x03, 0x07};
\r
192 byte left = lclip[x&0x03];
\r
193 byte right = rclip[(x+w)&0x03];
\r
195 /* handle the case which requires an extra group */
\r
196 if((x & 0x03) && !((x+w) & 0x03)) {
\r
201 MOV AX, SCREEN_SEG ; go to the VGA memory
\r
203 MOV DI, poffset ; go to the first pixel
\r
204 MOV DX, SC_INDEX ; point to the map mask
\r
208 MOV AL, color ; get ready to write colors
\r
210 MOV CX, scanCount ; count the line
\r
211 MOV BL, AL ; remember color
\r
212 MOV AL, left ; do the left clip
\r
213 OUT DX, AL ; set the left clip
\r
214 MOV AL, BL ; restore color
\r
215 STOSB ; write the color
\r
217 JZ SCAN_DONE ; handle 1 group stuff
\r
219 ;-- write the main body of the scanline
\r
220 MOV BL, AL ; remember color
\r
221 MOV AL, 0x0f ; write to all pixels
\r
223 MOV AL, BL ; restore color
\r
224 REP STOSB ; write the color
\r
226 MOV BL, AL ; remeber color
\r
228 OUT DX, AL ; do the right clip
\r
229 MOV AL, BL ; restore color
\r
230 STOSB ; write pixel
\r
231 ADD DI, nextRow ; go to the next row
\r
239 modexDrawBmp(page_t *page, int x, int y, bitmap_t *bmp) {
\r
240 /* draw the region (the entire freakin bitmap) */
\r
241 modexDrawBmpRegion(page, x, y, 0, 0, bmp->width, bmp->height, bmp);
\r
246 modexDrawBmpRegion(page_t *page, int x, int y,
\r
247 int rx, int ry, int rw, int rh, bitmap_t *bmp) {
\r
248 word poffset = (word) page->data + y*(page->width/4) + x/4;
\r
249 byte *data = bmp->data;
\r
250 word bmpOffset = (word) data + ry * bmp->width + rx;
\r
253 byte plane = 1 << ((byte) x & 0x03);
\r
254 word scanCount = width/4 + (width%4 ? 1 :0);
\r
255 word nextPageRow = page->width/4 - scanCount;
\r
256 word nextBmpRow = (word) bmp->width - width;
\r
258 byte planeCounter = 4;
\r
261 MOV AX, SCREEN_SEG ; go to the VGA memory
\r
264 MOV DX, SC_INDEX ; point at the map mask register
\r
269 MOV DX, SC_DATA ; select the current plane
\r
273 ;-- begin plane painting
\r
274 MOV AX, height ; start the row counter
\r
275 MOV rowCounter, AX ;
\r
276 MOV DI, poffset ; go to the first pixel
\r
277 MOV SI, bmpOffset ; go to the bmp pixel
\r
279 MOV CX, width ; count the columns
\r
281 MOVSB ; copy the pixel
\r
282 SUB CX, 3 ; we skip the next 3
\r
283 ADD SI, 3 ; skip the bmp pixels
\r
284 LOOP SCAN_LOOP ; finish the scan
\r
286 MOV AX, nextPageRow
\r
287 ADD DI, AX ; go to the next row on screen
\r
289 ADD SI, AX ; go to the next row on bmp
\r
292 JNZ ROW_LOOP ; do all the rows
\r
293 ;-- end plane painting
\r
295 MOV AL, plane ; advance to the next plane
\r
297 AND AL, 0x0f ; mask the plane properly
\r
298 MOV plane, AL ; store the plane
\r
300 INC bmpOffset ; start bmp at the right spot
\r
303 JNZ PLANE_LOOP ; do all 4 planes
\r
309 modexDrawSprite(page_t *page, int x, int y, bitmap_t *bmp) {
\r
310 /* draw the whole sprite */
\r
311 modexDrawSpriteRegion(page, x, y, 0, 0, bmp->width, bmp->height, bmp);
\r
315 modexDrawSpriteRegion(page_t *page, int x, int y,
\r
316 int rx, int ry, int rw, int rh, bitmap_t *bmp) {
\r
317 word poffset = (word)page->data + y*(page->width/4) + x/4;
\r
318 byte *data = bmp->data;
\r
319 word bmpOffset = (word) data + ry * bmp->width + rx;
\r
322 byte plane = 1 << ((byte) x & 0x03);
\r
323 word scanCount = width/4 + (width%4 ? 1 :0);
\r
324 word nextPageRow = page->width/4 - scanCount;
\r
325 word nextBmpRow = (word) bmp->width - width;
\r
327 byte planeCounter = 4;
\r
330 MOV AX, SCREEN_SEG ; go to the VGA memory
\r
333 MOV DX, SC_INDEX ; point at the map mask register
\r
338 MOV DX, SC_DATA ; select the current plane
\r
342 ;-- begin plane painting
\r
343 MOV AX, height ; start the row counter
\r
344 MOV rowCounter, AX ;
\r
345 MOV DI, poffset ; go to the first pixel
\r
346 MOV SI, bmpOffset ; go to the bmp pixel
\r
348 MOV CX, width ; count the columns
\r
353 JNE DRAW_PIXEL ; draw non-zero pixels
\r
355 INC DI ; skip the transparent pixel
\r
359 MOVSB ; copy the pixel
\r
361 SUB CX, 3 ; we skip the next 3
\r
362 ADD SI, 3 ; skip the bmp pixels
\r
363 LOOP SCAN_LOOP ; finish the scan
\r
365 MOV AX, nextPageRow
\r
366 ADD DI, AX ; go to the next row on screen
\r
368 ADD SI, AX ; go to the next row on bmp
\r
371 JNZ ROW_LOOP ; do all the rows
\r
372 ;-- end plane painting
\r
374 MOV AL, plane ; advance to the next plane
\r
376 AND AL, 0x0f ; mask the plane properly
\r
377 MOV plane, AL ; store the plane
\r
379 INC bmpOffset ; start bmp at the right spot
\r
382 JNZ PLANE_LOOP ; do all 4 planes
\r
388 modexCopyPageRegion(page_t *dest, page_t src,
\r
391 word width, word height)
\r
397 /* fade and flash */
\r
399 modexFadeOn(word fade, byte *palette) {
\r
400 fadePalette(-fade, 64, 64/fade+1, palette);
\r
405 modexFadeOff(word fade, byte *palette) {
\r
406 fadePalette(fade, 0, 64/fade+1, palette);
\r
411 modexFlashOn(word fade, byte *palette) {
\r
412 fadePalette(fade, -64, 64/fade+1, palette);
\r
417 modexFlashOff(word fade, byte *palette) {
\r
418 fadePalette(-fade, 0, 64/fade+1, palette);
\r
423 fadePalette(sbyte fade, sbyte start, word iter, byte *palette) {
\r
427 /* handle the case where we just update */
\r
429 modexPalUpdate(palette);
\r
433 while(iter > 0) { /* FadeLoop */
\r
434 for(i=0; i<PAL_SIZE; i++) { /* loadpal_loop */
\r
435 tmppal[i] = palette[i] - dim;
\r
436 if(tmppal[i] > 127) {
\r
438 } else if(tmppal[i] > 63) {
\r
442 modexPalUpdate(tmppal);
\r
449 /* save and load */
\r
451 modexPalSave(byte *palette) {
\r
454 outp(PAL_READ_REG, 0); /* start at palette entry 0 */
\r
455 for(i=0; i<PAL_SIZE; i++) {
\r
456 palette[i] = inp(PAL_DATA_REG); /* read the palette data */
\r
464 ptr = malloc(PAL_SIZE);
\r
466 /* handle errors */
\r
468 printf("Could not allocate palette.\n");
\r
477 modexLoadPalFile(byte *filename, byte **palette) {
\r
481 /* free the palette if it exists */
\r
486 /* allocate the new palette */
\r
487 *palette = modexNewPal();
\r
489 /* open the file */
\r
490 file = fopen(filename, "rb");
\r
492 printf("Could not open palette file: %s\n", filename);
\r
496 /* read the file */
\r
498 while(!feof(file)) {
\r
499 *ptr++ = fgetc(file);
\r
507 modexSavePalFile(char *filename, byte *pal) {
\r
511 /* open the file for writing */
\r
512 file = fopen(filename, "wb");
\r
514 printf("Could not open %s for writing\n", filename);
\r
518 /* write the data to the file */
\r
519 fwrite(pal, 1, PAL_SIZE, file);
\r
527 fadePalette(-1, 64, 1, tmppal);
\r
533 fadePalette(-1, -64, 1, tmppal);
\r
539 modexPalUpdate(byte *p) {
\r
542 outp(PAL_WRITE_REG, 0); /* start at the beginning of palette */
\r
543 for(i=0; i<PAL_SIZE/2; i++) {
\r
544 outp(PAL_DATA_REG, p[i]);
\r
546 modexWaitBorder(); /* waits one retrace -- less flicker */
\r
547 for(i=PAL_SIZE/2; i<PAL_SIZE; i++) {
\r
548 outp(PAL_DATA_REG, p[i]);
\r
554 modexWaitBorder() {
\r
555 while(inp(INPUT_STATUS_1) & 8) {
\r
559 while(!(inp(INPUT_STATUS_1) & 8)) {
\r
566 modexLoadPcx(char *filename) {
\r
569 struct pcxHeader head;
\r
574 /* open the PCX file for reading */
\r
575 file = fopen(filename, "rb");
\r
577 printf("Could not open %s for reading.\n", filename);
\r
581 /* read the header */
\r
582 fread(&head, sizeof(char), sizeof(struct pcxHeader), file);
\r
584 /* make sure this is 8bpp */
\r
585 if(head.bpp != 8) {
\r
586 printf("I only know how to handle 8bpp pcx files!\n");
\r
591 /* allocate the buffer */
\r
592 result.width = head.xmax - head.xmin + 1;
\r
593 result.height = head.ymax - head.ymin + 1;
\r
594 bufSize = result.width * result.height;
\r
595 result.data = malloc(bufSize);
\r
597 printf("Could not allocate memory for bitmap data.");
\r
602 /* read the buffer in */
\r
605 /* get the run length and the value */
\r
606 count = fgetc(file);
\r
607 if(0xC0 == (count & 0xC0)) { /* this is the run count */
\r
615 /* write the pixel the specified number of times */
\r
616 for(; count && index < bufSize; count--,index++) {
\r
617 result.data[index] = val;
\r
619 } while(index < bufSize);
\r
621 /* handle the palette */
\r
622 fseek(file, -769, SEEK_END);
\r
624 result.palette = modexNewPal();
\r
625 if(head.version == 5 && val == 12) {
\r
626 /* use the vga palette */
\r
627 for(index=0; !feof(file) && index < PAL_SIZE; index++) {
\r
629 result.palette[index] = val >> 2;
\r
632 /* use the 16 color palette */
\r
633 for(index=0; index<48; index++) {
\r
634 result.palette[index] = head.pal16[index];
\r