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