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
387 /* copy a region of video memory from one page to another.
\r
388 * It assumes that the left edge of the tile is the same on both
\r
389 * regions and the memory areas do not overlap.
\r
392 modexCopyPageRegion(page_t *dest, page_t *src,
\r
395 word width, word height)
\r
397 word doffset = (word)dest->data + dy*(dest->width/4) + dx/4;
\r
398 word soffset = (word)src->data + sy*(src->width/4) + sx/4;
\r
399 word scans = width/4;
\r
400 word nextSrcRow = src->width/4 - scans - 1;
\r
401 word nextDestRow = dest->width/4 - scans - 1;
\r
402 byte lclip[] = {0x0f, 0x0e, 0x0c, 0x08}; /* clips for rectangles not on 4s */
\r
403 byte rclip[] = {0x0f, 0x01, 0x03, 0x07};
\r
404 byte left = lclip[sx&0x03];
\r
405 byte right = rclip[(sx+width)&0x03];
\r
408 MOV AX, SCREEN_SEG ; work in the vga space
\r
413 MOV DX, GC_INDEX ; turn off cpu bits
\r
417 MOV AX, SC_INDEX ; point to the mask register
\r
427 MOV CX, scans ; the number of latches
\r
429 MOV AL, left ; do the left column
\r
434 MOV AL, 0fh ; do the inner columns
\r
436 REP MOVSB ; copy the pixels
\r
438 MOV AL, right ; do the right column
\r
443 MOV AX, SI ; go the start of the next row
\r
444 ADD AX, nextSrcRow ;
\r
447 ADD AX, nextDestRow ;
\r
450 DEC height ; do the rest of the actions
\r
453 MOV DX, GC_INDEX+1 ; go back to CPU data
\r
454 MOV AL, 0ffh ; none from latches
\r
460 /* fade and flash */
\r
462 modexFadeOn(word fade, byte *palette) {
\r
463 fadePalette(-fade, 64, 64/fade+1, palette);
\r
468 modexFadeOff(word fade, byte *palette) {
\r
469 fadePalette(fade, 0, 64/fade+1, palette);
\r
474 modexFlashOn(word fade, byte *palette) {
\r
475 fadePalette(fade, -64, 64/fade+1, palette);
\r
480 modexFlashOff(word fade, byte *palette) {
\r
481 fadePalette(-fade, 0, 64/fade+1, palette);
\r
486 fadePalette(sbyte fade, sbyte start, word iter, byte *palette) {
\r
490 /* handle the case where we just update */
\r
492 modexPalUpdate(palette);
\r
496 while(iter > 0) { /* FadeLoop */
\r
497 for(i=0; i<PAL_SIZE; i++) { /* loadpal_loop */
\r
498 tmppal[i] = palette[i] - dim;
\r
499 if(tmppal[i] > 127) {
\r
501 } else if(tmppal[i] > 63) {
\r
505 modexPalUpdate(tmppal);
\r
512 /* save and load */
\r
514 modexPalSave(byte *palette) {
\r
517 outp(PAL_READ_REG, 0); /* start at palette entry 0 */
\r
518 for(i=0; i<PAL_SIZE; i++) {
\r
519 palette[i] = inp(PAL_DATA_REG); /* read the palette data */
\r
527 ptr = malloc(PAL_SIZE);
\r
529 /* handle errors */
\r
531 printf("Could not allocate palette.\n");
\r
540 modexLoadPalFile(byte *filename, byte **palette) {
\r
544 /* free the palette if it exists */
\r
549 /* allocate the new palette */
\r
550 *palette = modexNewPal();
\r
552 /* open the file */
\r
553 file = fopen(filename, "rb");
\r
555 printf("Could not open palette file: %s\n", filename);
\r
559 /* read the file */
\r
561 while(!feof(file)) {
\r
562 *ptr++ = fgetc(file);
\r
570 modexSavePalFile(char *filename, byte *pal) {
\r
574 /* open the file for writing */
\r
575 file = fopen(filename, "wb");
\r
577 printf("Could not open %s for writing\n", filename);
\r
581 /* write the data to the file */
\r
582 fwrite(pal, 1, PAL_SIZE, file);
\r
590 fadePalette(-1, 64, 1, tmppal);
\r
596 fadePalette(-1, -64, 1, tmppal);
\r
602 modexPalUpdate(byte *p) {
\r
605 outp(PAL_WRITE_REG, 0); /* start at the beginning of palette */
\r
606 for(i=0; i<PAL_SIZE/2; i++) {
\r
607 outp(PAL_DATA_REG, p[i]);
\r
609 modexWaitBorder(); /* waits one retrace -- less flicker */
\r
610 for(i=PAL_SIZE/2; i<PAL_SIZE; i++) {
\r
611 outp(PAL_DATA_REG, p[i]);
\r
617 modexWaitBorder() {
\r
618 while(inp(INPUT_STATUS_1) & 8) {
\r
622 while(!(inp(INPUT_STATUS_1) & 8)) {
\r
629 modexLoadPcx(char *filename) {
\r
632 struct pcxHeader head;
\r
637 /* open the PCX file for reading */
\r
638 file = fopen(filename, "rb");
\r
640 printf("Could not open %s for reading.\n", filename);
\r
644 /* read the header */
\r
645 fread(&head, sizeof(char), sizeof(struct pcxHeader), file);
\r
647 /* make sure this is 8bpp */
\r
648 if(head.bpp != 8) {
\r
649 printf("I only know how to handle 8bpp pcx files!\n");
\r
654 /* allocate the buffer */
\r
655 result.width = head.xmax - head.xmin + 1;
\r
656 result.height = head.ymax - head.ymin + 1;
\r
657 bufSize = result.width * result.height;
\r
658 result.data = malloc(bufSize);
\r
660 printf("Could not allocate memory for bitmap data.");
\r
665 /* read the buffer in */
\r
668 /* get the run length and the value */
\r
669 count = fgetc(file);
\r
670 if(0xC0 == (count & 0xC0)) { /* this is the run count */
\r
678 /* write the pixel the specified number of times */
\r
679 for(; count && index < bufSize; count--,index++) {
\r
680 result.data[index] = val;
\r
682 } while(index < bufSize);
\r
684 /* handle the palette */
\r
685 fseek(file, -769, SEEK_END);
\r
687 result.palette = modexNewPal();
\r
688 if(head.version == 5 && val == 12) {
\r
689 /* use the vga palette */
\r
690 for(index=0; !feof(file) && index < PAL_SIZE; index++) {
\r
692 result.palette[index] = val >> 2;
\r
695 /* use the 16 color palette */
\r
696 for(index=0; index<48; index++) {
\r
697 result.palette[index] = head.pal16[index];
\r