]> 4ch.mooo.com Git - 16.git/blob - src/lib/modex16.c
font rendering = fail
[16.git] / src / lib / modex16.c
1 #include <dos.h>
2 #include <string.h>
3 #include <mem.h>
4 #include <conio.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include "src/lib/modex16.h"
8
9
10 byte far* VGA=(byte far*) 0xA0000000;   /* this points to video memory. */
11 word text_mask[16] = {
12         0x0002, 0x0102, 0x0202, 0x0302,\r
13         0x0402, 0x0502, 0x0602, 0x0702,\r
14         0x0802, 0x0902, 0x0A02, 0x0B02,\r
15         0x0C02, 0x0D02, 0x0E02, 0x0F02
16 };
17
18 static void fadePalette(sbyte fade, sbyte start, word iter, byte *palette);
19 static byte tmppal[PAL_SIZE];
20
21 static void
22 vgaSetMode(byte mode)
23 {
24   union REGS regs;
25
26   regs.h.ah = SET_MODE;
27   regs.h.al = mode;
28   int86(VIDEO_INT, &regs, &regs);
29 }
30
31
32 /* -========================= Entry  Points ==========================- */
33 void
34 modexEnter() {
35     word i;
36     dword far*ptr=(dword far*)VGA;      /* used for faster screen clearing */
37     word CRTParms[] = {
38         0x0d06,         /* vertical total */
39         0x3e07,         /* overflow (bit 8 of vertical counts) */
40         0x4109,         /* cell height (2 to double-scan */
41         0xea10,         /* v sync start */
42         0xac11,         /* v sync end and protect cr0-cr7 */
43         0xdf12,         /* vertical displayed */
44         0x0014,         /* turn off dword mode */
45         0xe715,         /* v blank start */
46         0x0616,         /* v blank end */
47         0xe317          /* turn on byte mode */
48     };
49     int CRTParmCount = sizeof(CRTParms) / sizeof(CRTParms[0]);
50
51     /* TODO save current video mode and palette */
52     vgaSetMode(VGA_256_COLOR_MODE);
53
54     /* disable chain4 mode */
55     outpw(SC_INDEX, 0x0604);
56
57     /* synchronous reset while setting Misc Output */
58     outpw(SC_INDEX, 0x0100);
59
60     /* select 25 MHz dot clock & 60 Hz scanning rate */
61     outp(MISC_OUTPUT, 0xe3);
62
63     /* undo reset (restart sequencer) */
64     outpw(SC_INDEX, 0x0300);
65
66     /* reprogram the CRT controller */
67     outp(CRTC_INDEX, 0x11); /* VSync End reg contains register write prot */
68     outp(CRTC_DATA, 0x7f);  /* get current write protect on varios regs */
69
70     /* send the CRTParms */
71     for(i=0; i<CRTParmCount; i++) {
72         outpw(CRTC_INDEX, CRTParms[i]);
73     }
74
75     /* clear video memory */
76     outpw(SC_INDEX, 0x0f02);
77     for(i=0; i<0x8000; i++) {
78         ptr[i] = 0x0000;
79     }
80 }
81
82
83 void
84 modexLeave() {
85     /* TODO restore original mode and palette */
86     vgaSetMode(TEXT_MODE);
87 }
88
89
90 page_t
91 modexDefaultPage() {
92     page_t page;
93
94     /* default page values */
95     page.data = VGA;
96     page.dx = 0;
97     page.dy = 0;
98     page.width = SCREEN_WIDTH;
99     page.height = SCREEN_HEIGHT;
100
101     return page;
102 }
103
104 /* returns the next page in contiguous memory
105  * the next page will be the same size as p, by default
106  */
107 page_t
108 modexNextPage(page_t *p) {
109     page_t result;
110
111     result.data = p->data + (p->width/4)*p->height;  /* compute the offset */
112     result.dx = 0;
113     result.dy = 0;
114     result.width = p->width;
115     result.height = p->height;
116
117     return result;
118 }
119
120
121 void
122 modexShowPage(page_t *page) {
123     word high_address;
124     word low_address;
125     word offset;
126     byte crtcOffset;
127
128     /* calculate offset */
129     offset = (word) page->data;
130     offset += page->dy * (page->width >> 2 );
131     offset += page->dx >> 2;
132
133     /* calculate crtcOffset according to virtual width */
134     crtcOffset = page->width >> 3;
135
136     high_address = HIGH_ADDRESS | (offset & 0xff00);
137     low_address  = LOW_ADDRESS  | (offset << 8);
138
139     /* wait for appropriate timing and then program CRTC */
140     while ((inp(INPUT_STATUS_1) & DISPLAY_ENABLE));
141     outpw(CRTC_INDEX, high_address);
142     outpw(CRTC_INDEX, low_address);
143     outp(CRTC_INDEX, 0x13);
144     outp(CRTC_DATA, crtcOffset);
145
146     /*  wait for one retrace */
147     while (!(inp(INPUT_STATUS_1) & VRETRACE)); 
148
149     /* do PEL panning here */
150     outp(AC_INDEX, 0x33);
151     outp(AC_INDEX, (page->dx & 0x03) << 1);
152 }
153
154
155 void
156 modexPanPage(page_t *page, int dx, int dy) {
157     page->dx = dx;
158     page->dy = dy;
159 }
160
161
162 void
163 modexSelectPlane(byte plane) {
164     outp(SC_INDEX, MAP_MASK);          /* select plane */
165     outp(SC_DATA,  plane);
166 }
167
168
169 void
170 modexClearRegion(page_t *page, int x, int y, int w, int h, byte  color) {
171     word pageOff = (word) page->data;
172     word xoff=x/4;       /* xoffset that begins each row */
173     word scanCount=w/4;  /* number of iterations per row (excluding right clip)*/
174     word poffset = pageOff + y*(page->width/4) + xoff; /* starting offset */
175     word nextRow = page->width/4-scanCount-1;  /* loc of next row */
176     byte lclip[] = {0x0f, 0x0e, 0x0c, 0x08};  /* clips for rectangles not on 4s */
177     byte rclip[] = {0x00, 0x01, 0x03, 0x07};
178     byte left = lclip[x&0x03];
179     byte right = rclip[(x+w)&0x03];
180
181     /* handle the case which requires an extra group */
182     if((x & 0x03) && !((x+w) & 0x03)) {
183       right=0x0f;
184     }
185
186     __asm {
187                 MOV AX, SCREEN_SEG      ; go to the VGA memory
188                 MOV ES, AX
189                 MOV DI, poffset         ; go to the first pixel
190                 MOV DX, SC_INDEX        ; point to the map mask
191                 MOV AL, MAP_MASK
192                 OUT DX, AL
193                 INC DX
194                 MOV AL, color           ; get ready to write colors
195         SCAN_START:
196                 MOV CX, scanCount       ; count the line
197                 MOV BL, AL              ; remember color
198                 MOV AL, left            ; do the left clip
199                 OUT DX, AL              ; set the left clip
200                 MOV AL, BL              ; restore color
201                 STOSB                   ; write the color
202                 DEC CX
203                 JZ SCAN_DONE            ; handle 1 group stuff
204
205                 ;-- write the main body of the scanline
206                 MOV BL, AL              ; remember color
207                 MOV AL, 0x0f            ; write to all pixels
208                 OUT DX, AL
209                 MOV AL, BL              ; restore color
210                 REP STOSB               ; write the color
211         SCAN_DONE:
212                 MOV BL, AL              ; remeber color
213                 MOV AL, right
214                 OUT DX, AL              ; do the right clip
215                 MOV AL, BL              ; restore color
216                 STOSB                   ; write pixel
217                 ADD DI, nextRow         ; go to the next row
218                 DEC h
219                 JNZ SCAN_START
220     }
221 }
222
223
224 void
225 modexDrawBmp(page_t *page, int x, int y, bitmap_t *bmp) {
226     /* draw the region (the entire freakin bitmap) */
227     modexDrawBmpRegion(page, x, y, 0, 0, bmp->width, bmp->height, bmp);
228 }
229
230
231 void
232 modexDrawBmpRegion(page_t *page, int x, int y,
233                    int rx, int ry, int rw, int rh, bitmap_t *bmp) {
234     word poffset = (word) page->data  + y*(page->width/4) + x/4;
235     byte *data = bmp->data;//+bmp->offset;
236     word bmpOffset = (word) data + ry * bmp->width + rx;
237     word width = rw;
238     word height = rh;
239     byte plane = 1 << ((byte) x & 0x03);
240     word scanCount = width/4 + (width%4 ? 1 :0);
241     word nextPageRow = page->width/4 - scanCount;
242     word nextBmpRow = (word) bmp->width - width;
243     word rowCounter;
244     byte planeCounter = 4;
245
246         //code is a bit slow here
247     __asm {
248                 MOV AX, SCREEN_SEG      ; go to the VGA memory
249                 MOV ES, AX
250
251                 MOV DX, SC_INDEX        ; point at the map mask register
252                 MOV AL, MAP_MASK        ;
253                 OUT DX, AL              ;
254
255         PLANE_LOOP:
256                 MOV DX, SC_DATA         ; select the current plane
257                 MOV AL, plane           ;
258                 OUT DX, AL              ;
259
260                 ;-- begin plane painting
261                 MOV AX, height          ; start the row counter
262                 MOV rowCounter, AX      ; 
263                 MOV DI, poffset         ; go to the first pixel
264                 MOV SI, bmpOffset       ; go to the bmp pixel
265         ROW_LOOP:
266                 MOV CX, width           ; count the columns
267         SCAN_LOOP:
268                 MOVSB                   ; copy the pixel
269                 SUB CX, 3               ; we skip the next 3
270                 ADD SI, 3               ; skip the bmp pixels
271                 LOOP SCAN_LOOP          ; finish the scan
272
273                 MOV AX, nextPageRow
274                 ADD DI, AX              ; go to the next row on screen
275                 MOV AX, nextBmpRow
276                 ADD SI, AX              ; go to the next row on bmp
277
278                 DEC rowCounter
279                 JNZ ROW_LOOP            ; do all the rows
280                 ;-- end plane painting
281
282                 MOV AL, plane           ; advance to the next plane
283                 SHL AL, 1               ;
284                 AND AL, 0x0f            ; mask the plane properly
285                 MOV plane, AL           ; store the plane
286
287                 INC bmpOffset           ; start bmp at the right spot
288
289                 DEC planeCounter
290                 JNZ PLANE_LOOP          ; do all 4 planes
291     }
292 }
293
294
295 void
296 modexDrawPlanarBuf(page_t *page, int x, int y, planar_buf_t *bmp) {
297     /* TODO - adapt from test code */
298         int plane;
299         for(plane=0; plane < 4; plane++)
300         {
301                 //fack
302         }
303 }
304
305
306 void
307 modexDrawSprite(page_t *page, int x, int y, bitmap_t *bmp) {
308     /* draw the whole sprite */
309     modexDrawSpriteRegion(page, x, y, 0, 0, bmp->width, bmp->height, bmp);
310 }
311
312 void
313 modexDrawSpriteRegion(page_t *page, int x, int y,
314                       int rx, int ry, int rw, int rh, bitmap_t *bmp) {
315     word poffset = (word)page->data + y*(page->width/4) + x/4;
316     byte *data = bmp->data;//+bmp->offset;
317     word bmpOffset = (word) data + ry * bmp->width + rx;
318     word width = rw;
319     word height = rh;
320     byte plane = 1 << ((byte) x & 0x03);
321     word scanCount = width/4 + (width%4 ? 1 :0);
322     word nextPageRow = page->width/4 - scanCount;
323     word nextBmpRow = (word) bmp->width - width;
324     word rowCounter;
325     byte planeCounter = 4;
326
327     __asm {
328                 MOV AX, SCREEN_SEG      ; go to the VGA memory
329                 MOV ES, AX
330
331                 MOV DX, SC_INDEX        ; point at the map mask register
332                 MOV AL, MAP_MASK        ;
333                 OUT DX, AL              ;
334
335         PLANE_LOOP:
336                 MOV DX, SC_DATA         ; select the current plane
337                 MOV AL, plane           ;
338                 OUT DX, AL              ;
339
340                 ;-- begin plane painting
341                 MOV AX, height          ; start the row counter
342                 MOV rowCounter, AX      ; 
343                 MOV DI, poffset         ; go to the first pixel
344                 MOV SI, bmpOffset       ; go to the bmp pixel
345         ROW_LOOP:
346                 MOV CX, width           ; count the columns
347         SCAN_LOOP:
348                 LODSB
349                 DEC SI
350                 CMP AL, 0
351                 JNE DRAW_PIXEL          ; draw non-zero pixels
352
353                 INC DI                  ; skip the transparent pixel
354                 ADD SI, 1
355                 JMP NEXT_PIXEL
356         DRAW_PIXEL:
357                 MOVSB                   ; copy the pixel
358         NEXT_PIXEL:
359                 SUB CX, 3               ; we skip the next 3
360                 ADD SI, 3               ; skip the bmp pixels
361                 LOOP SCAN_LOOP          ; finish the scan
362
363                 MOV AX, nextPageRow
364                 ADD DI, AX              ; go to the next row on screen
365                 MOV AX, nextBmpRow
366                 ADD SI, AX              ; go to the next row on bmp
367
368                 DEC rowCounter
369                 JNZ ROW_LOOP            ; do all the rows
370                 ;-- end plane painting
371
372                 MOV AL, plane           ; advance to the next plane
373                 SHL AL, 1               ;
374                 AND AL, 0x0f            ; mask the plane properly
375                 MOV plane, AL           ; store the plane
376
377                 INC bmpOffset           ; start bmp at the right spot
378
379                 DEC planeCounter
380                 JNZ PLANE_LOOP          ; do all 4 planes
381     }
382 }
383
384
385 /* copy a region of video memory from one page to another.
386  * It assumes that the left edge of the tile is the same on both
387  * regions and the memory areas do not overlap.
388  */
389 void
390 modexCopyPageRegion(page_t *dest, page_t *src,
391                     word sx, word sy,
392                     word dx, word dy,
393                     word width, word height)
394 {
395     word doffset = (word)dest->data + dy*(dest->width/4) + dx/4;
396     word soffset = (word)src->data + sy*(src->width/4) + sx/4;
397     word scans   = width/4;
398     word nextSrcRow = src->width/4 - scans - 1;
399     word nextDestRow = dest->width/4 - scans - 1;
400     byte lclip[] = {0x0f, 0x0e, 0x0c, 0x08};  /* clips for rectangles not on 4s */
401     byte rclip[] = {0x0f, 0x01, 0x03, 0x07};
402     byte left = lclip[sx&0x03];
403     byte right = rclip[(sx+width)&0x03];
404
405     __asm {
406                 MOV AX, SCREEN_SEG      ; work in the vga space
407                 MOV ES, AX              ;
408                 MOV DI, doffset         ;
409                 MOV SI, soffset         ;
410
411                 MOV DX, GC_INDEX        ; turn off cpu bits
412                 MOV AX, 0008h           ;
413                 OUT DX, AX
414
415                 MOV AX, SC_INDEX        ; point to the mask register
416                 MOV DX, AX              ;
417                 MOV AL, MAP_MASK        ;
418                 OUT DX, AL              ;
419                 INC DX                  ;
420
421         ROW_START:
422                 PUSH DS
423                 MOV AX, ES
424                 MOV DS, AX
425                 MOV CX, scans           ; the number of latches
426
427                 MOV AL, left            ; do the left column
428                 OUT DX, AL              ;
429                 MOVSB                   ;
430                 DEC CX                  ;
431
432                 MOV AL, 0fh             ; do the inner columns
433                 OUT DX, AL
434                 REP MOVSB               ; copy the pixels
435
436                 MOV AL, right           ; do the right column
437                 OUT DX, AL
438                 MOVSB
439                 POP DS
440
441                 MOV AX, SI              ; go the start of the next row
442                 ADD AX, nextSrcRow      ;
443                 MOV SI, AX              ;
444                 MOV AX, DI              ;
445                 ADD AX, nextDestRow     ;
446                 MOV DI, AX              ;
447
448                 DEC height              ; do the rest of the actions
449                 JNZ ROW_START           ;
450
451                 MOV DX, GC_INDEX+1      ; go back to CPU data
452                 MOV AL, 0ffh            ; none from latches
453                 OUT DX, AL              ;
454     }
455 }
456
457
458 /* fade and flash */
459 void
460 modexFadeOn(word fade, byte *palette) {
461     fadePalette(-fade, 64, 64/fade+1, palette);
462 }
463
464
465 void
466 modexFadeOff(word fade, byte *palette) {
467     fadePalette(fade, 0, 64/fade+1, palette);
468 }
469
470
471 void
472 modexFlashOn(word fade, byte *palette) {
473     fadePalette(fade, -64, 64/fade+1, palette);
474 }
475
476
477 void
478 modexFlashOff(word fade, byte *palette) {
479     fadePalette(-fade, 0, 64/fade+1, palette);
480 }
481
482
483 static void
484 fadePalette(sbyte fade, sbyte start, word iter, byte *palette) {
485     word i;
486     byte dim = start;
487
488     /* handle the case where we just update */
489     if(iter == 0) {
490         modexPalUpdate1(palette);
491         return;
492     }
493
494     while(iter > 0) {  /* FadeLoop */
495         for(i=0; i<PAL_SIZE; i++) { /* loadpal_loop */
496             tmppal[i] = palette[i] - dim;
497             if(tmppal[i] > 127) {
498                 tmppal[i] = 0;
499             } else if(tmppal[i] > 63) {
500                 tmppal[i] = 63;
501             }
502         }
503         modexPalUpdate1(tmppal);
504         iter--;
505         dim += fade;
506     }
507 }
508
509
510 /* save and load */
511 void
512 modexPalSave(byte *palette) {
513     int  i;
514
515     outp(PAL_READ_REG, 0);      /* start at palette entry 0 */
516     for(i=0; i<PAL_SIZE; i++) {
517         palette[i] = inp(PAL_DATA_REG); /* read the palette data */
518     }
519 }
520
521
522 byte *
523 modexNewPal() {
524     byte *ptr;
525     ptr = malloc(PAL_SIZE);
526
527     /* handle errors */
528     if(!ptr) {
529         printf("Could not allocate palette.\n");
530         exit(-1);
531     }
532
533     return ptr;
534 }
535
536
537 void
538 modexLoadPalFile(byte *filename, byte **palette) {
539     FILE *file;
540     byte *ptr;
541
542     /* free the palette if it exists */
543     if(*palette) {
544         free(*palette);
545     }
546
547     /* allocate the new palette */
548     *palette = modexNewPal();
549
550     /* open the file */
551     file = fopen(filename, "rb");
552     if(!file) {
553         printf("Could not open palette file: %s\n", filename);
554         exit(-2);
555     }
556
557     /* read the file */
558     ptr = *palette;
559     while(!feof(file)) {
560         *ptr++ = fgetc(file);
561     }
562
563     fclose(file);
564 }
565
566
567 void
568 modexSavePalFile(char *filename, byte *pal) {
569     unsigned int i;
570     FILE *file;
571
572     /* open the file for writing */
573     file = fopen(filename, "wb");
574     if(!file) {
575         printf("Could not open %s for writing\n", filename);
576         exit(-2);
577     }
578
579     /* write the data to the file */
580     fwrite(pal, 1, PAL_SIZE, file);
581     fclose(file);
582 }
583
584
585 /* blanking */
586 void
587 modexPalBlack() {
588     fadePalette(-1, 64, 1, tmppal);
589 }
590
591
592 void
593 modexPalWhite() {
594     fadePalette(-1, -64, 1, tmppal);
595 }
596
597
598 /* utility */
599 void
600 modexPalUpdate(bitmap_t *bmp, word *i, word qp, word aqoffset)
601 {
602         byte *p = bmp->palette;
603         word w=0;
604         word q=0;
605         word qq=0;
606         static word a[PAL_SIZE];        //palette array of change values!
607         word z=0, aq=0, aa=0, pp=0;
608
609         modexWaitBorder();
610         if((*i)==0)
611         {
612                 memset(a, -1, sizeof(a));
613                 outp(PAL_WRITE_REG, 0);  /* start at the beginning of palette */
614         }
615         else if(qp==0)
616         {
617                 q=(*i);
618         }
619         else
620         {
621                 q=(*i);
622                 qq=(*i)/3;
623 //              printf("q: %02d\n", (q));
624 //              printf("qq: %02d\n", (qq));
625                 //printf("      (*i)-q=%02d\n", (*i)-q);
626                 outp(PAL_WRITE_REG, qq);  /* start at the beginning of palette */
627         }
628         if((*i)<PAL_SIZE/2 && w==0)
629         {
630                 for(; (*i)<PAL_SIZE/2; (*i)++)
631                 {
632                         //if(i%3==0 && (p[i+5]==p[i+4] && p[i+4]==p[i+3] && p[i+3]==p[i+2] && p[i+2]==p[i+1] && p[i+1]==p[i] && p[i+5]==p[i]))
633 //____                  if((qp>0)&&((*i)-q)%3==0 && (p[((*i)-q)]==p[((*i)-q)+3] && p[((*i)-q)+1]==p[((*i)-q)+4] && p[((*i)-q)+2]==p[((*i)-q)+5])) outp(PAL_DATA_REG, p[(*i)-q]); else
634                         if(((((*i)-q)%3==0)) && (p[((*i)-q)]==p[((*i)-q)+3] && p[((*i)-q)+1]==p[((*i)-q)+4] && p[((*i)-q)+2]==p[((*i)-q)+5]))
635                         {
636                                 w++;
637                                 break;
638                         }
639                         else if(qp>0 && (*i)>=(qp) && (*i)<((qp)+3))
640                         {
641                                 //printf("qp=%d\n", qp);
642                                 //printf("              (*i)=%d a[%d]=%d\n", (*i), qp, a[qp]);
643                                 printf("                %d's color=%d\n", (*i), (a[qp])-(bmp->offset*3)+qp);
644                                 //outp(PAL_DATA_REG, p[((a[qp])-(bmp->offset*3)+qp)]);// fix this shit!
645                                 if((*i)+1==(qp)+3){ w++; /*(*i)++;*/ break; }
646                         }
647                         else
648                         {
649                                 if(bmp->offset==0 && (*i)<3 && q==0) outp(PAL_DATA_REG, 0);
650                                 else
651                                 if(qp==0) outp(PAL_DATA_REG, p[(*i)-q]);
652                                 else{ //outp(PAL_DATA_REG, p[((*i)-(bmp->offset*3)+qp)]);
653                                 printf("p[]=%d  qp=%d   p[]-qp=%d\n", ((*i)-(bmp->offset*3)), qp, ((*i)-(bmp->offset*3))+qp); }
654                         }
655                 }
656                 //if(qp>0) printf("qp=%d\n", qp);
657                 //if(qp>0) printf("                                             (*i)=%d\n", (*i)/3);
658         }
659         modexWaitBorder();          /* waits one retrace -- less flicker */
660         if((*i)>=PAL_SIZE/2 && w==0)
661         {
662                 for(; (*i)<PAL_SIZE; (*i)++)
663                 {
664 //____                  if((qp>0)&&((*i)-q)%3==0 && (p[((*i)-q)]==p[((*i)-q)+3] && p[((*i)-q)+1]==p[((*i)-q)+4] && p[((*i)-q)+2]==p[((*i)-q)+5])) outp(PAL_DATA_REG, p[(*i)-q]); else
665                         if(((((*i)-q)%3==0)) && (p[((*i)-q)]==p[((*i)-q)+3] && p[((*i)-q)+1]==p[((*i)-q)+4] && p[((*i)-q)+2]==p[((*i)-q)+5]))
666                         {
667                                 w++;
668                                 break;
669                         }
670                         else if(qp>0 && (*i)>=(qp) && (*i)<((qp)+3))
671                         {
672                                 //printf("qp=%d\n", qp);
673                                 //printf("              (*i)=%d a[%d]=%d\n", (*i), qp, a[qp]);
674                                 printf("                %d's color=%d\n", (*i), (a[qp]-(bmp->offset*3)+qp));
675                                 //outp(PAL_DATA_REG, p[((a[qp])-(bmp->offset*3)+qp)]);// fix this shit!
676                                 if((*i)+1==(qp)+3){ w++; /*(*i)++;*/ break; }
677                         }
678                         else
679                         {
680                                 if(qp==0) outp(PAL_DATA_REG, p[(*i)-q]);
681                                 else{ //outp(PAL_DATA_REG, p[((*i)-(bmp->offset*3)+qp)]);
682                                 printf("p[]=%d  qp=%d   p[]-qp=%d\n", ((*i)-(bmp->offset*3)), qp, ((*i)-(bmp->offset*3))+qp); }
683                         }
684                 }
685                 //printf("                                              (*i)=%d\n", (*i)/3);
686         }
687
688 printf("\nqqqqqqqq\n\n");
689
690         //palette checker~
691         if(q>0 && qp==0)
692         {
693                 long lq;
694                 long bufSize = (bmp->width * bmp->height);
695                 pp = q;
696                 //printf("1(*i)=%02d\n", (*i)/3);
697                 //printf("1z=%02d\n", z/3);
698                 chkcolor(bmp, &q, &a, &aa, &z, i);
699                 //printf("2(*i)=%02d\n", (*i)/3);
700                 //printf("2z=%02d\n", z/3);
701                 aq=0;
702 aqpee:
703                 while(aq<=aa)
704                 {
705 //                      printf("a[%02d]=(%d)\n", aq, a[aq]);
706                         if(a[aq]==-1) aq++;
707                         else { aqoffset++; break; }
708                 }
709 //update the image data here!
710         for(lq=0; lq<bufSize; lq++)
711         {
712                                 /*
713                                                                         note to self
714                                                                         use a[qp] instead of bmp->offset for this spot!
715                                                                         NO! wwww
716                                 */
717
718                                 /*
719                                 Facking bloody point the values of the changed palette to correct values.... major confusion! wwww
720                                 */
721
722                 //(offset/bmp->offset)*bmp->offset
723
724
725                 //printf("%02d ",bmp->data[lq]+bmp->offset);
726                 //if(lq > 0 && lq%bmp->width==0) printf("\n");
727                 //printf("%02d_", bmp->data[lq]+bmp->offset);
728                 /*if(bmp->data[lq]+bmp->offset==aq)
729                 {
730                         //printf("%02d", bmp->data[lq]);
731                         //printf("\n%02d\n", bmp->offset);
732                         printf("aq=%02d ", aq);
733                         printf("a[aq]=%02d      ", a[aq]);
734                         printf("a[aq]+aqpp=%02d ", a[aq]+aqpp);
735                         printf("a[aq]-aqpp=%02d\n", a[aq]-aqpp);
736                         //bmp->data[lq]=((bmp->data[lq]+bmp->offset)-a[aq]);
737 //++++                  bmp->data[lq]=a[aq]-aqpp;
738 //                      printf("_%d ", bmp->data[lq]);
739                         //if(lq > 0 && lq%bmp->width==0) printf("\n");
740                 }
741                 else if(bmp->data[lq]+bmp->offset < ((*i)/3)-aqpp)
742                 {
743                         if(bmp->data[lq]+bmp->offset >= aq)
744                         {
745                                 bmp->data[lq]=(bmp->data[lq]+bmp->offset)-aqpp;//-((z-(*i))/3);
746                                 //printf("_%d ", bmp->data[lq]+bmp->offset)-aqpp-((z-(*i))/3);
747                         }
748                         else bmp->data[lq]+=(bmp->offset-aqpp);
749                 }*/
750
751                 //printf("%02d`", bmp->data[lq]);
752                 //if(lq > 0 && lq%bmp->width==0) printf("\n");
753         }
754
755 //printf("              aq=%02d\n", aq);
756 //printf("              aa=%02d\n", aa);
757
758         //update the palette~
759         modexPalUpdate(bmp, &pp, aq, aqoffset);
760         (*i)=pp;
761
762         if(aq<aa){ pp=q; aq++; goto aqpee; }
763         }
764 }
765
766 void
767 modexPalUpdate1(byte *p)
768 {
769         int i;
770         modexWaitBorder();
771         outp(PAL_WRITE_REG, 0);  /* start at the beginning of palette */
772         for(i=0; i<PAL_SIZE/2; i++)
773         {
774                 outp(PAL_DATA_REG, p[i]);
775         }
776         modexWaitBorder();          /* waits one retrace -- less flicker */
777         for(; i<PAL_SIZE; i++)
778         {
779                 outp(PAL_DATA_REG, p[(i)]);
780         }
781 }
782
783 void
784 modexPalUpdate0(byte *p)
785 {
786         int i;
787         modexWaitBorder();
788         outp(PAL_WRITE_REG, 0);  /* start at the beginning of palette */
789         for(i=0; i<PAL_SIZE/2; i++)
790         {
791                 outp(PAL_DATA_REG, rand());
792         }
793         modexWaitBorder();          /* waits one retrace -- less flicker */
794         for(; i<PAL_SIZE; i++)
795         {
796                 outp(PAL_DATA_REG, rand());
797         }
798 }
799
800 //color checker~
801 //i want to make another vesion that checks the palette when the palette is being appened~
802 void chkcolor(bitmap_t *bmp, word *q, word *a, word *aa, word *z, word *i/*, word *offset*/)
803 {
804                 byte *pal;
805                 word zz=0;
806                 pal = modexNewPal();
807                 modexPalSave(pal);
808                 //printf("q: %02d\n", (*q));
809                 printf("chkcolor start~\n");
810                 printf("1                               (*z): %d\n", (*z)/3);
811                 printf("1                               (*i): %d\n", (*i)/3);
812 //              printf("1 offset of color in palette    (*q): %d\n", (*q)/3);
813                 printf("wwwwwwwwwwwwwwww\n");
814                 //check palette for dups
815                 for(; (*z)<PAL_SIZE; (*z)+=3)
816                 {
817                         //printf("\n            z: %d\n", (*z));
818                         //printf("              q: %d\n", (*q));
819                         //printf("              z+q: %d\n\n", ((*z)+(*q)));
820                         //if((*z)%3==0)
821                         //{
822 //----                          if(pal[(*z)]==pal[(*z)+3] && pal[(*z)+1]==pal[(*z)+4] && pal[(*z)+2]==pal[(*z)+5])
823                                 if((*z)==(*i))
824                                 {
825 //                                      printf("\n%d    [%02d][%02d][%02d]\n", (*z), pal[(*z)], pal[(*z)+1], pal[(*z)+2]);
826 //                                      printf("%d      [%02d][%02d][%02d]\n\n", (*z)+3, pal[(*z)+3], pal[(*z)+4], pal[(*z)+5]);
827 //0000                                  (*z)-=3;
828                                         break;
829                                 }
830                                 else for(zz=0; zz<(*q); zz+=3)
831                                 {
832                                         //printf("zz: %02d\n", zz/3);
833                                         if(zz%3==0)
834                                         {
835                                                 if(pal[((*z)+(*q))]==pal[((*z)+(*q))+3] && pal[((*z)+(*q))+1]==pal[((*z)+(*q))+4] && pal[((*z)+(*q))+2]==pal[((*z)+(*q))+5])    //break if duplicate colors found in palette because it have reached the end of the current data of the palette
836                                                 {
837 //                                                      (*z)-=3;
838 //                                                      (*i)-=3;
839 //                                                      printf("\nzq1:%d[%02d][%02d][%02d]\n", (zz+q), pal[(zz+q)], pal[(zz+q)+1], pal[(zz+q)+2]);
840 //                                                      printf("zq2:%d[%02d][%02d][%02d]\n\n", (zz+q)+3, pal[(zz+q)+3], pal[(zz+q)+4], pal[(zz+q)+5]);
841                                                         break;
842                                                 }
843                                                 else if(pal[zz]==pal[((*z)+(*q))] && pal[zz+1]==pal[((*z)+(*q))+1] && pal[zz+2]==pal[((*z)+(*q))+2])
844                                                 {
845 //                                                      printf("\n\nwwwwwwwwwwwwwwww\n");
846 //                                                      printf("        zq: %d  [%02d][%02d][%02d] value that is needing to be changed~\n", ((*z)+(*q))/3, pal[((*z)+(*q))], pal[((*z)+(*q))+1], pal[((*z)+(*q))+2]);
847 //                                                      printf("        zz: %d  [%02d][%02d][%02d] value that the previous value is going to change to~\n", (zz)/3, pal[zz], pal[zz+1], pal[zz+2]);
848 //                                                      //printf("      zv: %d  [%02d][%02d][%02d] wwww\n", (zz-z+q)/3, pal[(zz-z+q)], pal[(zz-z+q)+1], pal[(zz-z+q)+2]);
849 //                                                      printf("        z : %d  [%02d][%02d][%02d] offset value~\n", (*z)/3, pal[(*z)], pal[(*z)+1], pal[(*z)+2]);
850 //++++                                                  (*i)--;
851 //                                                      (*z)--;
852                                                         //expand dong here
853 /*
854 planned features that i plan to implement~
855 image that has values on the pallete list!
856 wwww
857 no... wait.... no wwww
858 */
859                                                         //for(zzii=0; zzii<3; zzii++)
860                                                         //{
861                                                                 //printf("z+q: %d\n\n", ((*z)+(*q)));
862                                                                 a[(((*z)+(*q)))]=zz;
863                                                         //}
864                                                         (*aa)=(((*z)+(*q)));
865                                                         printf("!!                                      a[%02d]: %d\n", (((*z)+(*q))/3), zz/3);
866 //                                                      printf("\n              aa: %d\n\n", (*aa));
867 //                                                      printf("        a[%02d]=(%02d) offset array i think the palette should be updated again~\n", ((*z)+(*q))/3, a[((*z)+(*q))/3]);
868 //                                                      printf("wwwwwwwwwwwwwwww\n\n");
869                                                 }
870                                                 /*else
871                                                 {
872                                                         printf("================\n");
873                                                         printf("zq: %d  [%02d][%02d][%02d]\n", ((*z)+(*q))/3, pal[((*z)+(*q))], pal[((*z)+(*q))+1], pal[((*z)+(*q))+2]);
874                                                         printf("zz: %d  [%02d][%02d][%02d]\n", (zz)/3, pal[zz], pal[zz+1], pal[zz+2]);
875                                                         printf("z : %d  [%02d][%02d][%02d]\n", (*z)/3, pal[(*z)], pal[(*z)+1], pal[(*z)+2]);
876                                                         printf("================\n");
877                                                 }*/
878                                                 //printf("[%d]", (zz+q));
879                                         }
880                                 }
881                 }
882                 printf("wwwwwwwwwwwwwwww\n");
883                 printf("2                               (*z): %d\n", (*z)/3);
884                 printf("2                               (*i): %d\n", (*i)/3);
885 //              printf("2 offset of color in palette    (*q): %d\n", (*q)/3);
886                 printf("chkcolor end~\n");
887                 free(pal);
888 }
889
890 void
891 modexWaitBorder() {
892     while(inp(INPUT_STATUS_1)  & 8)  {
893         /* spin */
894     }
895
896     while(!(inp(INPUT_STATUS_1)  & 8))  {
897         /* spin */
898     }
899 }
900
901 /*****************************************************************************\r
902 find 8x8 font in VGA BIOS ROM\r
903 *****************************************************************************/\r
904 byte far *bios_8x8_font(void)\r
905 {\r
906         byte far *font;
907         regs_t regs;
908 \r
909 /* use BIOS INT 10h AX=1130h to find font #3 (8x8) in ROM */\r
910         memset(&regs, 0, sizeof(regs)); /* for Watcom C */\r
911         regs.w.ax = 0x1130;\r
912         regs.w.bx = 0x0300;\r
913         intr(0x10, &regs);\r
914         font = (byte far *)MK_FP(regs.w.es, regs.w.bp);\r
915         return font;\r
916 }
917
918 /*****************************************************************************\r
919 *****************************************************************************/\r
920 void bputs(page_t *pee, int x, int y, const char *s)\r
921 {
922         //int i, skip;\r
923         byte far *font;
924         byte far *font_pntr;
925         //byte c, temp;
926 \r
927         font = bios_8x8_font();
928         //skip = 2 - ((pee->width/4) << 3);
929         //printf("font=%Fp\n", font);\r
930         for(; *s != '\0'; s++)\r
931         {\r
932                 //src.raster = font + 8 * (*s);
933                 //BLOODY!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!111111111111!!!11!!11!111!11!!1111!!111!11!!1!!!11!11!!1!!111!11!!
934 //              (*(bmp->data)) = (*(font + 8 * (*s)));
935                 font_pntr = (*(font + 8 * (*s)));
936 //              font_pntr = font + (c << 3);
937 //              i=8;\r
938 //              while (i--) {\r
939 //                      temp = *font_pntr++;\r
940 //                      outpw(SC_INDEX, text_mask[temp & 0x0F]);\r
941                         //*vga_ptr++ = color;\r
942 \r
943 //                      outpw(SC_INDEX, text_mask[temp >> 4]);\r
944                         //*vga_ptr-- = color;\r
945                         //vga_ptr += widthBytes;\r
946 //              }
947
948                 //printf("fontoffset=%Fp\n", font + 8 * (*s));
949                 //printf("*fontoffset=%s\n", *(font + 8 * (*s)));
950                 //printf("w.data=%Fp\n", (w.data));
951                 //printf("*w.data=%s\n", *(w.data));
952                 //blit1(&src, bmp, x, y);
953 //              modexDrawSprite(page, x, y, bmp);
954 //              modexDrawBmp(page, x, y, bmp);
955 //              printf("%x\n", (*(font + 8 * (*s))));
956                 //_fmemset(VGA, *(font + 8 * (*s)), _msize(font));
957                 //draw text?!?! wwww
958
959                 modexClearRegion(pee, x, y, 8, 8, 4);
960 //              x += 8;\r
961         }
962 //      printf("\n");\r
963 }