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