OSDN Git Service

i am sticking to lardge memory model for speed www
[proj16/16.git] / src / lib / 16_tail.c
1 /* Project 16 Source Code~\r
2  * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover\r
3  *\r
4  * This file is part of Project 16.\r
5  *\r
6  * Project 16 is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 3 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * Project 16 is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>, or\r
18  * write to the Free Software Foundation, Inc., 51 Franklin Street,\r
19  * Fifth Floor, Boston, MA 02110-1301 USA.\r
20  *\r
21  */\r
22 /*\r
23  * 16 library\r
24  */\r
25 \r
26 #include "src/lib/16_tail.h"\r
27 #include "src/lib/16text.h"\r
28 \r
29 /*\r
30 ==========================\r
31 =\r
32 = Startup16\r
33 =\r
34 = Load a few things right away\r
35 =\r
36 ==========================\r
37 */\r
38 \r
39 void Startup16(global_game_variables_t *gvar)\r
40 {\r
41 #ifdef __WATCOMC__\r
42         start_timer(gvar);\r
43 \r
44         // DOSLIB: check our environment\r
45         probe_dos();\r
46 \r
47         // DOSLIB: what CPU are we using?\r
48         // NTS: I can see from the makefile Sparky4 intends this to run on 8088 by the -0 switch in CFLAGS.\r
49         //      So this code by itself shouldn't care too much what CPU it's running on. Except that other\r
50         //      parts of this project (DOSLIB itself) rely on CPU detection to know what is appropriate for\r
51         //      the CPU to carry out tasks. --J.C.\r
52         cpu_probe();\r
53 \r
54         // DOSLIB: check for VGA\r
55         if (!probe_vga()) {\r
56                 printf("VGA probe failed\n");\r
57                 return;\r
58         }\r
59         // hardware must be VGA or higher!\r
60         if (!(vga_state.vga_flags & VGA_IS_VGA)) {\r
61                 printf("This program requires VGA or higher graphics hardware\n");\r
62                 return;\r
63         }\r
64 \r
65         if (_DEBUG_INIT() == 0) {\r
66 #ifdef DEBUGSERIAL\r
67                 printf("WARNING: Failed to initialize DEBUG output\n");\r
68 #endif\r
69         }\r
70         _DEBUG("Serial debug output started\n"); // NTS: All serial output must end messages with newline, or DOSBox-X will not emit text to log\r
71         _DEBUGF("Serial debug output printf test %u %u %u\n",1U,2U,3U);\r
72 \r
73         textInit();\r
74 #endif\r
75         gvar->mm.mmstarted=0;\r
76         gvar->pm.PMStarted=0;\r
77         MM_Startup(gvar);\r
78 #ifdef __WATCOMC__\r
79         IN_Startup(gvar);\r
80 #endif\r
81         PM_Startup(gvar);\r
82         PM_UnlockMainMem(gvar);\r
83         CA_Startup(gvar);\r
84 }\r
85 \r
86 //===========================================================================\r
87 \r
88 /*\r
89 ==========================\r
90 =\r
91 = Shutdown16\r
92 =\r
93 = Shuts down all ID_?? managers\r
94 =\r
95 ==========================\r
96 */\r
97 \r
98 void Shutdown16(global_game_variables_t *gvar)\r
99 {\r
100         PM_Shutdown(gvar);\r
101 #ifdef __WATCOMC__\r
102         IN_Shutdown(gvar);\r
103 #endif\r
104         CA_Shutdown(gvar);\r
105         MM_Shutdown(gvar);\r
106 }\r
107 \r
108 \r
109 //===========================================================================\r
110 \r
111 /*\r
112 ====================\r
113 =\r
114 = ReadConfig\r
115 =\r
116 ====================\r
117 */\r
118 \r
119 /*void ReadConfig(void)\r
120 {\r
121         int                     file;\r
122         SDMode          sd;\r
123         SMMode          sm;\r
124         SDSMode         sds;\r
125 \r
126 \r
127         if ( (file = open(configname,O_BINARY | O_RDONLY)) != -1)\r
128         {\r
129         //\r
130         // valid config file\r
131         //\r
132                 read(file,Scores,sizeof(HighScore) * MaxScores);\r
133 \r
134                 read(file,&sd,sizeof(sd));\r
135                 read(file,&sm,sizeof(sm));\r
136                 read(file,&sds,sizeof(sds));\r
137 \r
138                 read(file,&mouseenabled,sizeof(mouseenabled));\r
139                 read(file,&joystickenabled,sizeof(joystickenabled));\r
140                 read(file,&joypadenabled,sizeof(joypadenabled));\r
141                 read(file,&joystickprogressive,sizeof(joystickprogressive));\r
142                 read(file,&joystickport,sizeof(joystickport));\r
143 \r
144                 read(file,&dirscan,sizeof(dirscan));\r
145                 read(file,&buttonscan,sizeof(buttonscan));\r
146                 read(file,&buttonmouse,sizeof(buttonmouse));\r
147                 read(file,&buttonjoy,sizeof(buttonjoy));\r
148 \r
149                 read(file,&viewsize,sizeof(viewsize));\r
150                 read(file,&mouseadjustment,sizeof(mouseadjustment));\r
151 \r
152                 close(file);\r
153 \r
154                 if (sd == sdm_AdLib && !AdLibPresent && !SoundBlasterPresent)\r
155                 {\r
156                         sd = sdm_PC;\r
157                         sd = smm_Off;\r
158                 }\r
159 \r
160                 if ((sds == sds_SoundBlaster && !SoundBlasterPresent) ||\r
161                         (sds == sds_SoundSource && !SoundSourcePresent))\r
162                         sds = sds_Off;\r
163 \r
164                 if (!MousePresent)\r
165                         mouseenabled = false;\r
166                 if (!JoysPresent[joystickport])\r
167                         joystickenabled = false;\r
168 \r
169                 MainMenu[6].active=1;\r
170                 MainItems.curpos=0;\r
171         }\r
172         else\r
173         {\r
174         //\r
175         // no config file, so select by hardware\r
176         //\r
177                 if (SoundBlasterPresent || AdLibPresent)\r
178                 {\r
179                         sd = sdm_AdLib;\r
180                         sm = smm_AdLib;\r
181                 }\r
182                 else\r
183                 {\r
184                         sd = sdm_PC;\r
185                         sm = smm_Off;\r
186                 }\r
187 \r
188                 if (SoundBlasterPresent)\r
189                         sds = sds_SoundBlaster;\r
190                 else if (SoundSourcePresent)\r
191                         sds = sds_SoundSource;\r
192                 else\r
193                         sds = sds_Off;\r
194 \r
195                 if (MousePresent)\r
196                         mouseenabled = true;\r
197 \r
198                 joystickenabled = false;\r
199                 joypadenabled = false;\r
200                 joystickport = 0;\r
201                 joystickprogressive = false;\r
202 \r
203                 viewsize = 15;\r
204                 mouseadjustment=5;\r
205         }\r
206 \r
207         SD_SetMusicMode (sm);\r
208         SD_SetSoundMode (sd);\r
209         SD_SetDigiDevice (sds);\r
210 }*/\r
211 \r
212 \r
213 /*\r
214 ====================\r
215 =\r
216 = WriteConfig\r
217 =\r
218 ====================\r
219 */\r
220 \r
221 /*void WriteConfig(void)\r
222 {\r
223         int                     file;\r
224 \r
225         file = open(configname,O_CREAT | O_BINARY | O_WRONLY,\r
226                                 S_IREAD | S_IWRITE | S_IFREG);\r
227 \r
228         if (file != -1)\r
229         {\r
230                 write(file,Scores,sizeof(HighScore) * MaxScores);\r
231 \r
232                 write(file,&SoundMode,sizeof(SoundMode));\r
233                 write(file,&MusicMode,sizeof(MusicMode));\r
234                 write(file,&DigiMode,sizeof(DigiMode));\r
235 \r
236                 write(file,&mouseenabled,sizeof(mouseenabled));\r
237                 write(file,&joystickenabled,sizeof(joystickenabled));\r
238                 write(file,&joypadenabled,sizeof(joypadenabled));\r
239                 write(file,&joystickprogressive,sizeof(joystickprogressive));\r
240                 write(file,&joystickport,sizeof(joystickport));\r
241 \r
242                 write(file,&dirscan,sizeof(dirscan));\r
243                 write(file,&buttonscan,sizeof(buttonscan));\r
244                 write(file,&buttonmouse,sizeof(buttonmouse));\r
245                 write(file,&buttonjoy,sizeof(buttonjoy));\r
246 \r
247                 write(file,&viewsize,sizeof(viewsize));\r
248                 write(file,&mouseadjustment,sizeof(mouseadjustment));\r
249 \r
250                 close(file);\r
251         }\r
252 }*/\r
253 \r
254 //===========================================================================\r
255 \r
256 /*\r
257 ==================\r
258 =\r
259 = DebugMemory\r
260 =\r
261 ==================\r
262 */\r
263 \r
264 void DebugMemory_(global_game_variables_t *gvar, boolean q)\r
265 {\r
266         /*VW_FixRefreshBuffer ();\r
267         US_CenterWindow (16,7);\r
268 \r
269         US_CPrint ("Memory Usage");\r
270         US_CPrint ("------------");\r
271         US_Print ("Total     :");\r
272         US_PrintUnsigned (mminfo.mainmem/1024);\r
273         US_Print ("k\nFree      :");\r
274         US_PrintUnsigned (MM_UnusedMemory()/1024);\r
275         US_Print ("k\nWith purge:");\r
276         US_PrintUnsigned (MM_TotalFree()/1024);\r
277         US_Print ("k\n");\r
278         VW_UpdateScreen();*/\r
279         if(q){\r
280         printf("========================================\n");\r
281         printf("                DebugMemory_\n");\r
282         printf("========================================\n");}\r
283         if(q) { printf("Memory Usage\n");\r
284         printf("------------\n"); }else printf("        %c%c", 0xD3, 0xC4);\r
285         printf("Total:  "); if(q) printf("      "); printf("%uk", gvar->mmi.mainmem/1024);\r
286         if(q) printf("\n"); else printf("       ");\r
287         printf("Free:   "); if(q) printf("      "); printf("%uk", MM_UnusedMemory(gvar)/1024);\r
288         if(q) printf("\n"); else printf("       ");\r
289         printf("With purge:"); if(q) printf("   "); printf("%uk\n", MM_TotalFree(gvar)/1024);\r
290         if(q) printf("------------\n");\r
291 #ifdef __WATCOMC__\r
292         //IN_Ack ();\r
293 #endif\r
294         if(q) MM_ShowMemory (gvar);\r
295 }\r
296 \r
297 /*\r
298 ==========================\r
299 =\r
300 = ClearMemory\r
301 =\r
302 ==========================\r
303 */\r
304 \r
305 void ClearMemory (global_game_variables_t *gvar)\r
306 {\r
307         PM_UnlockMainMem(gvar);\r
308         //snd\r
309         MM_SortMem (gvar);\r
310 }\r
311 \r
312 /*\r
313 ==========================\r
314 =\r
315 = Quit\r
316 =\r
317 ==========================\r
318 */\r
319 \r
320 void Quit (global_game_variables_t *gvar, char *error)\r
321 {\r
322         //unsigned        finscreen;\r
323         memptr  screen=0;\r
324 \r
325         ClearMemory (gvar);\r
326         if (!*error)\r
327         {\r
328 // #ifndef JAPAN\r
329 //              CA_CacheGrChunk (ORDERSCREEN);\r
330 //              screen = grsegs[ORDERSCREEN];\r
331 // #endif\r
332 //              WriteConfig ();\r
333         }\r
334         else\r
335         {\r
336 //              CA_CacheGrChunk (ERRORSCREEN);\r
337 //              screen = grsegs[ERRORSCREEN];\r
338         }\r
339         Shutdown16(gvar);\r
340 \r
341         if (error && *error)\r
342         {\r
343                 movedata((unsigned)screen,7,0xb800,0,7*160);\r
344                 gotoxy (10,4);\r
345                 fprintf(stderr, "%s\n", error);\r
346                 gotoxy (1,8);\r
347                 exit(1);\r
348         }\r
349         else\r
350         if (!error || !(*error))\r
351         {\r
352                 clrscr();\r
353 #ifndef JAPAN\r
354                 movedata ((unsigned)screen,7,0xb800,0,4000);\r
355                 gotoxy(1,24);\r
356 #endif\r
357 //asm   mov     bh,0\r
358 //asm   mov     dh,23   // row\r
359 //asm   mov     dl,0    // collumn\r
360 //asm   mov ah,2\r
361 //asm   int     0x10\r
362         }\r
363 \r
364         exit(0);\r
365 }\r
366 \r
367 //===========================================================================\r
368 \r
369 const char *nibble_to_binary(nibble x)\r
370 {\r
371         static char b[9];\r
372         int z;\r
373 \r
374         b[0] = '\0';\r
375         for (z = 8; z > 0; z >>= 1)\r
376         {\r
377                 strcat(b, ((x & z) == z) ? "1" : "0");\r
378         }\r
379         return b;\r
380 }\r
381 \r
382 const char *boolean_to_binary(boolean x)\r
383 {\r
384         static char b[9];\r
385         int z;\r
386 \r
387         b[0] = '\0';\r
388         for (z = 1; z > 0; z >>= 1)\r
389         {\r
390                 strcat(b, ((x & z) == z) ? "1" : "0");\r
391         }\r
392         return b;\r
393 }\r
394 \r
395 void nibbletest()\r
396 {\r
397         nibble pee;\r
398         printf("nibbletest\n");\r
399         /* nibble to binary string */\r
400         for(pee=0;pee<18;pee++)\r
401                 printf("        %u %s\n", pee, nibble_to_binary(pee));\r
402         printf("        sizeof(nibble)=%s\n", nibble_to_binary(sizeof(nibble)));\r
403         printf("end of nibble test\n");\r
404 }\r
405 \r
406 void booleantest()\r
407 {\r
408         boolean pee;\r
409         printf("booleantest\n");\r
410         /* boolean to binary string */\r
411         for(pee=0;pee<4;pee++)\r
412                 printf("        %u %s\n", pee, boolean_to_binary(pee));\r
413         printf("        sizeof(boolean)=%s\n", boolean_to_binary(sizeof(boolean)));\r
414         printf("end of boolean test\n");\r
415 }\r