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