7 #include "src\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
16 vgaSetMode(byte mode)
\r
20 regs.h.ah = SET_MODE;
\r
22 int86(VIDEO_INT, ®s, ®s);
\r
26 /* -========================= Entry Points ==========================- */
\r
30 dword far*ptr=(dword far*)VGA; /* used for faster screen clearing */
\r
32 0x0d06, /* vertical total */
\r
33 0x3e07, /* overflow (bit 8 of vertical counts) */
\r
34 0x4109, /* cell height (2 to double-scan */
\r
35 0xea10, /* v sync start */
\r
36 0xac11, /* v sync end and protect cr0-cr7 */
\r
37 0xdf12, /* vertical displayed */
\r
38 0x0014, /* turn off dword mode */
\r
39 0xe715, /* v blank start */
\r
40 0x0616, /* v blank end */
\r
41 0xe317 /* turn on byte mode */
\r
43 int CRTParmCount = sizeof(CRTParms) / sizeof(CRTParms[0]);
\r
45 /* TODO save current video mode and palette */
\r
46 vgaSetMode(VGA_256_COLOR_MODE);
\r
48 /* disable chain4 mode */
\r
49 outpw(SC_INDEX, 0x0604);
\r
51 /* synchronous reset while setting Misc Output */
\r
52 outpw(SC_INDEX, 0x0100);
\r
54 /* select 25 MHz dot clock & 60 Hz scanning rate */
\r
55 outp(MISC_OUTPUT, 0xe3);
\r
57 /* undo reset (restart sequencer) */
\r
58 outpw(SC_INDEX, 0x0300);
\r
60 /* reprogram the CRT controller */
\r
61 outp(CRTC_INDEX, 0x11); /* VSync End reg contains register write prot */
\r
62 outp(CRTC_DATA, 0x7f); /* get current write protect on varios regs */
\r
64 /* send the CRTParms */
\r
65 for(i=0; i<CRTParmCount; i++) {
\r
66 outpw(CRTC_INDEX, CRTParms[i]);
\r
69 /* clear video memory */
\r
70 outpw(SC_INDEX, 0x0f02);
\r
71 for(i=0; i<0x8000; i++) {
\r
79 /* TODO restore original mode and palette */
\r
80 vgaSetMode(TEXT_MODE);
\r
85 modexDefaultPage() {
\r
88 /* default page values */
\r
92 page.width = SCREEN_WIDTH;
\r
93 page.height = SCREEN_HEIGHT;
\r
98 /* returns the next page in contiguous memory
\r
99 * the next page will be the same size as p, by default
\r
102 modexNextPage(page_t *p) {
\r
105 result.data = p->data + (p->width/4)*p->height; /* compute the offset */
\r
108 result.width = p->width;
\r
109 result.height = p->height;
\r
116 modexShowPage(page_t *page) {
\r
122 /* calculate offset */
\r
123 offset = (word) page->data;
\r
124 offset += page->dy * (page->width >> 2 );
\r
125 offset += page->dx >> 2;
\r
127 /* calculate crtcOffset according to virtual width */
\r
128 crtcOffset = page->width >> 3;
\r
130 high_address = HIGH_ADDRESS | (offset & 0xff00);
\r
131 low_address = LOW_ADDRESS | (offset << 8);
\r
133 /* wait for appropriate timing and then program CRTC */
\r
134 while ((inp(INPUT_STATUS_1) & DISPLAY_ENABLE));
\r
135 outpw(CRTC_INDEX, high_address);
\r
136 outpw(CRTC_INDEX, low_address);
\r
137 outp(CRTC_INDEX, 0x13);
\r
138 outp(CRTC_DATA, crtcOffset);
\r
140 /* wait for one retrace */
\r
141 while (!(inp(INPUT_STATUS_1) & VRETRACE));
\r
143 /* do PEL panning here */
\r
144 outp(AC_INDEX, 0x33);
\r
145 outp(AC_INDEX, (page->dx & 0x03) << 1);
\r
150 modexPanPage(page_t *page, int dx, int dy) {
\r
157 modexSelectPlane(byte plane) {
\r
158 outp(SC_INDEX, MAP_MASK); /* select plane */
\r
159 outp(SC_DATA, plane);
\r
164 modexClearRegion(page_t *page, int x, int y, int w, int h, byte color) {
\r
165 word pageOff = (word) page->data;
\r
166 word xoff=x/4; /* xoffset that begins each row */
\r
167 word scanCount=w/4; /* number of iterations per row (excluding right clip)*/
\r
168 word poffset = pageOff + y*(page->width/4) + xoff; /* starting offset */
\r
169 word nextRow = page->width/4-scanCount-1; /* loc of next row */
\r
170 byte lclip[] = {0x0f, 0x0e, 0x0c, 0x08}; /* clips for rectangles not on 4s */
\r
171 byte rclip[] = {0x00, 0x01, 0x03, 0x07};
\r
172 byte left = lclip[x&0x03];
\r
173 byte right = rclip[(x+w)&0x03];
\r
175 /* handle the case which requires an extra group */
\r
176 if((x & 0x03) && !((x+w) & 0x03)) {
\r
181 MOV AX, SCREEN_SEG ; go to the VGA memory
\r
183 MOV DI, poffset ; go to the first pixel
\r
184 MOV DX, SC_INDEX ; point to the map mask
\r
188 MOV AL, color ; get ready to write colors
\r
190 MOV CX, scanCount ; count the line
\r
191 MOV BL, AL ; remember color
\r
192 MOV AL, left ; do the left clip
\r
193 OUT DX, AL ; set the left clip
\r
194 MOV AL, BL ; restore color
\r
195 STOSB ; write the color
\r
197 JZ SCAN_DONE ; handle 1 group stuff
\r
199 ;-- write the main body of the scanline
\r
200 MOV BL, AL ; remember color
\r
201 MOV AL, 0x0f ; write to all pixels
\r
203 MOV AL, BL ; restore color
\r
204 REP STOSB ; write the color
\r
206 MOV BL, AL ; remeber color
\r
208 OUT DX, AL ; do the right clip
\r
209 MOV AL, BL ; restore color
\r
210 STOSB ; write pixel
\r
211 ADD DI, nextRow ; go to the next row
\r
219 modexDrawBmp(page_t *page, int x, int y, bitmap_t *bmp) {
\r
220 /* draw the region (the entire freakin bitmap) */
\r
221 modexDrawBmpRegion(page, x, y, 0, 0, bmp->width, bmp->height, bmp);
\r
226 modexDrawBmpRegion(page_t *page, int x, int y,
\r
227 int rx, int ry, int rw, int rh, bitmap_t *bmp) {
\r
228 word poffset = (word) page->data + y*(page->width/4) + x/4;
\r
229 byte *data = bmp->data;
\r
230 word bmpOffset = (word) data + ry * bmp->width + rx;
\r
233 byte plane = 1 << ((byte) x & 0x03);
\r
234 word scanCount = width/4 + (width%4 ? 1 :0);
\r
235 word nextPageRow = page->width/4 - scanCount;
\r
236 word nextBmpRow = (word) bmp->width - width;
\r
238 byte planeCounter = 4;
\r
240 //code is a bit slow here
\r
242 MOV AX, SCREEN_SEG ; go to the VGA memory
\r
245 MOV DX, SC_INDEX ; point at the map mask register
\r
250 MOV DX, SC_DATA ; select the current plane
\r
254 ;-- begin plane painting
\r
255 MOV AX, height ; start the row counter
\r
256 MOV rowCounter, AX ;
\r
257 MOV DI, poffset ; go to the first pixel
\r
258 MOV SI, bmpOffset ; go to the bmp pixel
\r
260 MOV CX, width ; count the columns
\r
262 MOVSB ; copy the pixel
\r
263 SUB CX, 3 ; we skip the next 3
\r
264 ADD SI, 3 ; skip the bmp pixels
\r
265 LOOP SCAN_LOOP ; finish the scan
\r
267 MOV AX, nextPageRow
\r
268 ADD DI, AX ; go to the next row on screen
\r
270 ADD SI, AX ; go to the next row on bmp
\r
273 JNZ ROW_LOOP ; do all the rows
\r
274 ;-- end plane painting
\r
276 MOV AL, plane ; advance to the next plane
\r
278 AND AL, 0x0f ; mask the plane properly
\r
279 MOV plane, AL ; store the plane
\r
281 INC bmpOffset ; start bmp at the right spot
\r
284 JNZ PLANE_LOOP ; do all 4 planes
\r
290 modexDrawPlanarBuf(page_t *page, int x, int y, planar_buf_t *bmp) {
\r
291 /* TODO - adapt from test code */
293 for(plane=0; plane < 4; plane++)
301 modexDrawSprite(page_t *page, int x, int y, bitmap_t *bmp) {
\r
302 /* draw the whole sprite */
\r
303 modexDrawSpriteRegion(page, x, y, 0, 0, bmp->width, bmp->height, bmp);
\r
307 modexDrawSpriteRegion(page_t *page, int x, int y,
\r
308 int rx, int ry, int rw, int rh, bitmap_t *bmp) {
\r
309 word poffset = (word)page->data + y*(page->width/4) + x/4;
\r
310 byte *data = bmp->data;
\r
311 word bmpOffset = (word) data + ry * bmp->width + rx;
\r
314 byte plane = 1 << ((byte) x & 0x03);
\r
315 word scanCount = width/4 + (width%4 ? 1 :0);
\r
316 word nextPageRow = page->width/4 - scanCount;
\r
317 word nextBmpRow = (word) bmp->width - width;
\r
319 byte planeCounter = 4;
\r
322 MOV AX, SCREEN_SEG ; go to the VGA memory
\r
325 MOV DX, SC_INDEX ; point at the map mask register
\r
330 MOV DX, SC_DATA ; select the current plane
\r
334 ;-- begin plane painting
\r
335 MOV AX, height ; start the row counter
\r
336 MOV rowCounter, AX ;
\r
337 MOV DI, poffset ; go to the first pixel
\r
338 MOV SI, bmpOffset ; go to the bmp pixel
\r
340 MOV CX, width ; count the columns
\r
345 JNE DRAW_PIXEL ; draw non-zero pixels
\r
347 INC DI ; skip the transparent pixel
\r
351 MOVSB ; copy the pixel
\r
353 SUB CX, 3 ; we skip the next 3
\r
354 ADD SI, 3 ; skip the bmp pixels
\r
355 LOOP SCAN_LOOP ; finish the scan
\r
357 MOV AX, nextPageRow
\r
358 ADD DI, AX ; go to the next row on screen
\r
360 ADD SI, AX ; go to the next row on bmp
\r
363 JNZ ROW_LOOP ; do all the rows
\r
364 ;-- end plane painting
\r
366 MOV AL, plane ; advance to the next plane
\r
368 AND AL, 0x0f ; mask the plane properly
\r
369 MOV plane, AL ; store the plane
\r
371 INC bmpOffset ; start bmp at the right spot
\r
374 JNZ PLANE_LOOP ; do all 4 planes
\r
379 /* copy a region of video memory from one page to another.
\r
380 * It assumes that the left edge of the tile is the same on both
\r
381 * regions and the memory areas do not overlap.
\r
384 modexCopyPageRegion(page_t *dest, page_t *src,
\r
387 word width, word height)
\r
389 word doffset = (word)dest->data + dy*(dest->width/4) + dx/4;
\r
390 word soffset = (word)src->data + sy*(src->width/4) + sx/4;
\r
391 word scans = width/4;
\r
392 word nextSrcRow = src->width/4 - scans - 1;
\r
393 word nextDestRow = dest->width/4 - scans - 1;
\r
394 byte lclip[] = {0x0f, 0x0e, 0x0c, 0x08}; /* clips for rectangles not on 4s */
\r
395 byte rclip[] = {0x0f, 0x01, 0x03, 0x07};
\r
396 byte left = lclip[sx&0x03];
\r
397 byte right = rclip[(sx+width)&0x03];
\r
400 MOV AX, SCREEN_SEG ; work in the vga space
\r
405 MOV DX, GC_INDEX ; turn off cpu bits
\r
409 MOV AX, SC_INDEX ; point to the mask register
\r
419 MOV CX, scans ; the number of latches
\r
421 MOV AL, left ; do the left column
\r
426 MOV AL, 0fh ; do the inner columns
\r
428 REP MOVSB ; copy the pixels
\r
430 MOV AL, right ; do the right column
\r
435 MOV AX, SI ; go the start of the next row
\r
436 ADD AX, nextSrcRow ;
\r
439 ADD AX, nextDestRow ;
\r
442 DEC height ; do the rest of the actions
\r
445 MOV DX, GC_INDEX+1 ; go back to CPU data
\r
446 MOV AL, 0ffh ; none from latches
\r
452 /* fade and flash */
\r
454 modexFadeOn(word fade, byte *palette) {
\r
455 fadePalette(-fade, 64, 64/fade+1, palette);
\r
460 modexFadeOff(word fade, byte *palette) {
\r
461 fadePalette(fade, 0, 64/fade+1, palette);
\r
466 modexFlashOn(word fade, byte *palette) {
\r
467 fadePalette(fade, -64, 64/fade+1, palette);
\r
472 modexFlashOff(word fade, byte *palette) {
\r
473 fadePalette(-fade, 0, 64/fade+1, palette);
\r
478 fadePalette(sbyte fade, sbyte start, word iter, byte *palette) {
\r
482 /* handle the case where we just update */
\r
484 modexPalUpdate(palette);
\r
488 while(iter > 0) { /* FadeLoop */
\r
489 for(i=0; i<PAL_SIZE; i++) { /* loadpal_loop */
\r
490 tmppal[i] = palette[i] - dim;
\r
491 if(tmppal[i] > 127) {
\r
493 } else if(tmppal[i] > 63) {
\r
497 modexPalUpdate(tmppal);
\r
504 /* save and load */
\r
506 modexPalSave(byte *palette) {
\r
509 outp(PAL_READ_REG, 0); /* start at palette entry 0 */
\r
510 for(i=0; i<PAL_SIZE; i++) {
\r
511 palette[i] = inp(PAL_DATA_REG); /* read the palette data */
\r
519 ptr = malloc(PAL_SIZE);
\r
521 /* handle errors */
\r
523 printf("Could not allocate palette.\n");
\r
532 modexLoadPalFile(byte *filename, byte **palette) {
\r
536 /* free the palette if it exists */
\r
541 /* allocate the new palette */
\r
542 *palette = modexNewPal();
\r
544 /* open the file */
\r
545 file = fopen(filename, "rb");
\r
547 printf("Could not open palette file: %s\n", filename);
\r
551 /* read the file */
\r
553 while(!feof(file)) {
\r
554 *ptr++ = fgetc(file);
\r
562 modexSavePalFile(char *filename, byte *pal) {
\r
566 /* open the file for writing */
\r
567 file = fopen(filename, "wb");
\r
569 printf("Could not open %s for writing\n", filename);
\r
573 /* write the data to the file */
\r
574 fwrite(pal, 1, PAL_SIZE, file);
\r
582 fadePalette(-1, 64, 1, tmppal);
\r
588 fadePalette(-1, -64, 1, tmppal);
\r
594 modexPalUpdate(byte *p) {
\r
597 outp(PAL_WRITE_REG, 0); /* start at the beginning of palette */
\r
598 for(i=0; i<PAL_SIZE/2; i++) {
\r
599 outp(PAL_DATA_REG, p[i]);
\r
601 modexWaitBorder(); /* waits one retrace -- less flicker */
\r
602 for(i=PAL_SIZE/2; i<PAL_SIZE; i++) {
\r
603 outp(PAL_DATA_REG, p[i]);
\r
609 modexWaitBorder() {
\r
610 while(inp(INPUT_STATUS_1) & 8) {
\r
614 while(!(inp(INPUT_STATUS_1) & 8)) {
\r