1 
2 /*
3 
4    dos.c
5 
6 */
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <dos.h>
11 #include <dpmi.h>
12 #include <pc.h>
13 #include <sys/farptr.h>
14 #include <go32.h>
15 
16 #include "globals.h"
17 #include "sys.h"
18 #include "gameboy.h"
19 #include "joypad.h"
20 
21 short video = 0;
22 short keyb = 0;
23 unsigned long int GB_STDPAL[4];
24 int resolution=8;
25 int doublesize=0;
26 _go32_dpmi_seginfo keybinfo;
27 int exitnow=0;
28 
29 static long int keystat=0;
30 
vramdump(tilescreen,dataofs)31 void vramdump(tilescreen,dataofs)
32 int tilescreen;
33 int dataofs;
34 {
35   tilescreen=0;
36   dataofs=0;
37   return;
38 }
39 
drawscreen(void)40 void drawscreen(void) {
41   int x,y;
42 
43   int buffer=((200-GB_LCDYSCREENSIZE)/2)*320+(320-GB_LCDXSCREENSIZE)/2;
44   char *lcdpos=lcdbuffer+resolution;
45 
46   while ((inp(0x3DA)&8)>0);
47   WHILE ((inp(0x3DA)&8)==0);
48 
49   for (y=0;y<GB_LCDYSCREENSIZE;y++,buffer+=320,lcdpos+=24)
50     for (x=0;x<GB_LCDXSCREENSIZE;x++,lcdpos++) {
51       _farpokeb(video,buffer+x,*lcdpos);
52     }
53 
54 }
55 
newkeybintr(void)56 void newkeybintr(void) {
57   unsigned char c;
58 
59   keystat=c=inp(0x60);
60   if (c<128) {
61     /* keyboard make code */
62 
63     switch (c) {
64     case GBPAD_up:
65       newjoypadstate&=0xBF;
66       break;
67     case GBPAD_down:
68       newjoypadstate&=0x7F;
69       break;
70     case GBPAD_left:
71       newjoypadstate&=0xDF;
72       break;
73     case GBPAD_right:
74       newjoypadstate&=0xEF;
75       break;
76     case GBPAD_a:
77       newjoypadstate&=0xFE;
78       break;
79     case GBPAD_b:
80       newjoypadstate&=0xFD;
81       break;
82     case GBPAD_start:
83       newjoypadstate&=0xF7;
84       break;
85     case GBPAD_select:
86       newjoypadstate&=0xFB;
87       break;
88     case 16:
89     case 1:
90       exitnow=1;
91       break;
92     }
93   } else {
94     /* keyboard break code */
95 
96     switch (c&0x7F) {
97     case GBPAD_up:
98       newjoypadstate|=0x40;
99       break;
100     case GBPAD_down:
101       newjoypadstate|=0x80;
102       break;
103     case GBPAD_left:
104       newjoypadstate|=0x20;
105       break;
106     case GBPAD_right:
107       newjoypadstate|=0x10;
108       break;
109     case GBPAD_a:
110       newjoypadstate|=0x01;
111       break;
112     case GBPAD_b:
113       newjoypadstate|=0x02;
114       break;
115     case GBPAD_start:
116       newjoypadstate|=0x08;
117       break;
118     case GBPAD_select:
119       newjoypadstate|=0x04;
120       break;
121     }
122   }
123 
124   /* keyboard buffer clear */
125   _farpokeb(keyb,0x1A,_farpeekb(keyb,0x1C));
126 }
127 
joypad(void)128 void joypad(void) {
129   /*  printf("%04X (%0d)\n",(short int)newjoypadstate,(short int)keystat);*/
130 
131   if (exitnow) {
132     savestate();
133     tidyup();
134     exit(0);
135   }
136 }
137 
initsys(void)138 int initsys(void) {
139   union REGS r;
140   int i;
141   _go32_dpmi_seginfo newintr;
142 
143   _go32_dpmi_get_protected_mode_interrupt_vector(0x09,&keybinfo);
144 
145   newintr.pm_offset=(int)newkeybintr;
146   newintr.pm_selector=_my_cs();
147   _go32_dpmi_chain_protected_mode_interrupt_vector(0x09,&newintr);
148 
149   fprintf(OUTSTREAM,"Allocating lcd buffer (%d bytes)... "
150 	  ,GB_XBUFFERSIZE*GB_LCDYSCREENSIZE*(resolution/8));
151   lcdbuffer=malloc(GB_XBUFFERSIZE*GB_LCDYSCREENSIZE*(resolution/8));
152   if (lcdbuffer==NULL) {
153     out_failed;
154     return 1;
155   } else {
156     out_ok;
157   }
158 
159   r.x.ax=0x0013;
160   int86(0x10,&r,&r);
161 
162   video=__dpmi_segment_to_descriptor(0xa000);
163   keyb=__dpmi_segment_to_descriptor(0x0040);
164 
165   GB_STDPAL[0]=color_translate(0x7FFF);
166   GB_STDPAL[1]=color_translate(0x56B5);
167   GB_STDPAL[2]=color_translate(0x2D6B);
168   GB_STDPAL[3]=color_translate(0x0000);
169 
170   for (i=0;i<64;i++) {
171     outp(0x3C8,i);
172     outp(0x3C9,(i>>4)<<4);
173     outp(0x3C9,((i>>2)&3)<<4);
174     outp(0x3C9,(i&3)<<4);
175   }
176 
177   return 0;
178 }
179 
donesys(void)180 void donesys(void) {
181   union REGS r;
182 
183   _go32_dpmi_set_protected_mode_interrupt_vector(0x09,&keybinfo);
184 
185   r.x.ax=0x0003;
186   int86(0x10,&r,&r);
187 }
188 
color_translate(uint gbcol)189 ulong color_translate(uint gbcol)
190 {
191   return (((gbcol&0x1F)>>3)<<4)+((((gbcol>>5)&0x1F)>>3)<<2)+
192     (((gbcol>>10)&0x1F)>>3);
193 }
194