OSDN Git Service

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