OSDN Git Service

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