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