OSDN Git Service

Merge branch 'master' of github.com:sparky4/16
[proj16/16.git] / src / lib / 16_tail.c
1 /* Project 16 Source Code~\r
2  * Copyright (C) 2012-2017 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 #ifdef __DEBUG_InputMgr__\r
80         if(!dbg_nointest)\r
81 #endif\r
82         IN_Startup(gvar);\r
83 #endif\r
84         PM_Startup(gvar);\r
85         PM_UnlockMainMem(gvar);\r
86         CA_Startup(gvar);\r
87 }\r
88 \r
89 //===========================================================================\r
90 \r
91 /*\r
92 ==========================\r
93 =\r
94 = Shutdown16\r
95 =\r
96 = Shuts down all ID_?? managers\r
97 =\r
98 ==========================\r
99 */\r
100 \r
101 void Shutdown16(global_game_variables_t *gvar)\r
102 {\r
103         PM_Shutdown(gvar);\r
104 #ifdef __WATCOMC__\r
105 #ifdef __DEBUG_InputMgr__\r
106         if(!dbg_nointest)\r
107 #endif\r
108         IN_Shutdown(gvar);\r
109 #endif\r
110         CA_Shutdown(gvar);\r
111         MM_Shutdown(gvar);\r
112 }\r
113 \r
114 \r
115 //===========================================================================\r
116 \r
117 /*\r
118 ====================\r
119 =\r
120 = ReadConfig\r
121 =\r
122 ====================\r
123 */\r
124 \r
125 /*void ReadConfig(void)\r
126 {\r
127         int                     file;\r
128         SDMode          sd;\r
129         SMMode          sm;\r
130         SDSMode         sds;\r
131 \r
132 \r
133         if ( (file = open(configname,O_BINARY | O_RDONLY)) != -1)\r
134         {\r
135         //\r
136         // valid config file\r
137         //\r
138                 read(file,Scores,sizeof(HighScore) * MaxScores);\r
139 \r
140                 read(file,&sd,sizeof(sd));\r
141                 read(file,&sm,sizeof(sm));\r
142                 read(file,&sds,sizeof(sds));\r
143 \r
144                 read(file,&mouseenabled,sizeof(mouseenabled));\r
145                 read(file,&joystickenabled,sizeof(joystickenabled));\r
146                 read(file,&joypadenabled,sizeof(joypadenabled));\r
147                 read(file,&joystickprogressive,sizeof(joystickprogressive));\r
148                 read(file,&joystickport,sizeof(joystickport));\r
149 \r
150                 read(file,&dirscan,sizeof(dirscan));\r
151                 read(file,&buttonscan,sizeof(buttonscan));\r
152                 read(file,&buttonmouse,sizeof(buttonmouse));\r
153                 read(file,&buttonjoy,sizeof(buttonjoy));\r
154 \r
155                 read(file,&viewsize,sizeof(viewsize));\r
156                 read(file,&mouseadjustment,sizeof(mouseadjustment));\r
157 \r
158                 close(file);\r
159 \r
160                 if (sd == sdm_AdLib && !AdLibPresent && !SoundBlasterPresent)\r
161                 {\r
162                         sd = sdm_PC;\r
163                         sd = smm_Off;\r
164                 }\r
165 \r
166                 if ((sds == sds_SoundBlaster && !SoundBlasterPresent) ||\r
167                         (sds == sds_SoundSource && !SoundSourcePresent))\r
168                         sds = sds_Off;\r
169 \r
170                 if (!MousePresent)\r
171                         mouseenabled = false;\r
172                 if (!JoysPresent[joystickport])\r
173                         joystickenabled = false;\r
174 \r
175                 MainMenu[6].active=1;\r
176                 MainItems.curpos=0;\r
177         }\r
178         else\r
179         {\r
180         //\r
181         // no config file, so select by hardware\r
182         //\r
183                 if (SoundBlasterPresent || AdLibPresent)\r
184                 {\r
185                         sd = sdm_AdLib;\r
186                         sm = smm_AdLib;\r
187                 }\r
188                 else\r
189                 {\r
190                         sd = sdm_PC;\r
191                         sm = smm_Off;\r
192                 }\r
193 \r
194                 if (SoundBlasterPresent)\r
195                         sds = sds_SoundBlaster;\r
196                 else if (SoundSourcePresent)\r
197                         sds = sds_SoundSource;\r
198                 else\r
199                         sds = sds_Off;\r
200 \r
201                 if (MousePresent)\r
202                         mouseenabled = true;\r
203 \r
204                 joystickenabled = false;\r
205                 joypadenabled = false;\r
206                 joystickport = 0;\r
207                 joystickprogressive = false;\r
208 \r
209                 viewsize = 15;\r
210                 mouseadjustment=5;\r
211         }\r
212 \r
213         SD_SetMusicMode (sm);\r
214         SD_SetSoundMode (sd);\r
215         SD_SetDigiDevice (sds);\r
216 }*/\r
217 \r
218 \r
219 /*\r
220 ====================\r
221 =\r
222 = WriteConfig\r
223 =\r
224 ====================\r
225 */\r
226 \r
227 /*void WriteConfig(void)\r
228 {\r
229         int                     file;\r
230 \r
231         file = open(configname,O_CREAT | O_BINARY | O_WRONLY,\r
232                                 S_IREAD | S_IWRITE | S_IFREG);\r
233 \r
234         if (file != -1)\r
235         {\r
236                 write(file,Scores,sizeof(HighScore) * MaxScores);\r
237 \r
238                 write(file,&SoundMode,sizeof(SoundMode));\r
239                 write(file,&MusicMode,sizeof(MusicMode));\r
240                 write(file,&DigiMode,sizeof(DigiMode));\r
241 \r
242                 write(file,&mouseenabled,sizeof(mouseenabled));\r
243                 write(file,&joystickenabled,sizeof(joystickenabled));\r
244                 write(file,&joypadenabled,sizeof(joypadenabled));\r
245                 write(file,&joystickprogressive,sizeof(joystickprogressive));\r
246                 write(file,&joystickport,sizeof(joystickport));\r
247 \r
248                 write(file,&dirscan,sizeof(dirscan));\r
249                 write(file,&buttonscan,sizeof(buttonscan));\r
250                 write(file,&buttonmouse,sizeof(buttonmouse));\r
251                 write(file,&buttonjoy,sizeof(buttonjoy));\r
252 \r
253                 write(file,&viewsize,sizeof(viewsize));\r
254                 write(file,&mouseadjustment,sizeof(mouseadjustment));\r
255 \r
256                 close(file);\r
257         }\r
258 }*/\r
259 \r
260 //===========================================================================\r
261 \r
262 /*\r
263 ==================\r
264 =\r
265 = DebugMemory\r
266 =\r
267 ==================\r
268 */\r
269 \r
270 void DebugMemory_(global_game_variables_t *gvar, boolean q)\r
271 {\r
272         /*VW_FixRefreshBuffer ();\r
273         US_CenterWindow (16,7);\r
274 \r
275         US_CPrint ("Memory Usage");\r
276         US_CPrint ("------------");\r
277         US_Print ("Total     :");\r
278         US_PrintUnsigned (mminfo.mainmem/1024);\r
279         US_Print ("k\nFree      :");\r
280         US_PrintUnsigned (MM_UnusedMemory()/1024);\r
281         US_Print ("k\nWith purge:");\r
282         US_PrintUnsigned (MM_TotalFree()/1024);\r
283         US_Print ("k\n");\r
284         VW_UpdateScreen();*/\r
285         if(q){\r
286         printf("========================================\n");\r
287         printf("                DebugMemory_\n");\r
288         printf("========================================\n");}\r
289         if(q) { printf("Memory Usage\n");\r
290         printf("------------\n"); }else printf("        %c%c", 0xD3, 0xC4);\r
291         printf("Total:  "); if(q) printf("      "); printf("%uk", gvar->mmi.mainmem/1024);\r
292         if(q) printf("\n"); else printf("       ");\r
293         printf("Free:   "); if(q) printf("      "); printf("%uk", MM_UnusedMemory(gvar)/1024);\r
294         if(q) printf("\n"); else printf("       ");\r
295         printf("With purge:"); if(q) printf("   "); printf("%uk\n", MM_TotalFree(gvar)/1024);\r
296         if(q) printf("------------\n");\r
297 #ifdef __WATCOMC__\r
298         //IN_Ack ();\r
299 #endif\r
300         if(q) MM_ShowMemory (gvar);\r
301 }\r
302 \r
303 /*\r
304 ==========================\r
305 =\r
306 = ClearMemory\r
307 =\r
308 ==========================\r
309 */\r
310 \r
311 void ClearMemory (global_game_variables_t *gvar)\r
312 {\r
313         PM_UnlockMainMem(gvar);\r
314         //snd\r
315         MM_SortMem (gvar);\r
316 }\r
317 \r
318 /*\r
319 ==========================\r
320 =\r
321 = Quit\r
322 =\r
323 ==========================\r
324 */\r
325 \r
326 void Quit (global_game_variables_t *gvar, char *error)\r
327 {\r
328         //unsigned        finscreen;\r
329         memptr  screen=0;\r
330 \r
331         ClearMemory (gvar);\r
332         if (!*error)\r
333         {\r
334 // #ifndef JAPAN\r
335 //              CA_CacheGrChunk (ORDERSCREEN);\r
336 //              screen = grsegs[ORDERSCREEN];\r
337 // #endif\r
338 //              WriteConfig ();\r
339         }\r
340         else\r
341         {\r
342 //              CA_CacheGrChunk (ERRORSCREEN);\r
343 //              screen = grsegs[ERRORSCREEN];\r
344         }\r
345         Shutdown16(gvar);\r
346 \r
347         if (error && *error)\r
348         {\r
349                 movedata((unsigned)screen,7,0xb800,0,7*160);\r
350                 gotoxy (10,4);\r
351                 fprintf(stderr, "%s\n", error);\r
352                 gotoxy (1,8);\r
353                 exit(1);\r
354         }\r
355         else\r
356         if (!error || !(*error))\r
357         {\r
358                 clrscr();\r
359 #ifndef JAPAN\r
360                 movedata ((unsigned)screen,7,0xb800,0,4000);\r
361                 gotoxy(1,24);\r
362 #endif\r
363 //asm   mov     bh,0\r
364 //asm   mov     dh,23   // row\r
365 //asm   mov     dl,0    // collumn\r
366 //asm   mov ah,2\r
367 //asm   int     0x10\r
368         }\r
369 \r
370         exit(0);\r
371 }\r
372 \r
373 //===========================================================================\r
374 #ifndef __WATCOMC__\r
375 char global_temp_status_text[512];\r
376 #else\r
377 //\r
378 // for mary4 (XT)\r
379 // this is from my XT's BIOS\r
380 // http://www.phatcode.net/downloads.php?id=101\r
381 //\r
382 void turboXT(byte bakapee)\r
383 {\r
384         __asm {\r
385         push    ax\r
386         push    bx\r
387         push    cx\r
388         in      al, 61h                         //; Read equipment flags\r
389         xor     al, bakapee                     //;   toggle speed\r
390         out     61h, al                         //; Write new flags back\r
391 \r
392         mov     bx, 0F89h                       //; low pitch blip\r
393         and     al, 4                           //; Is turbo mode set?\r
394         jz      @@do_beep\r
395         mov     bx, 52Eh                        //; high pitch blip\r
396 \r
397 @@do_beep:\r
398         mov     al, 10110110b           //; Timer IC 8253 square waves\r
399         out     43h, al                         //;   channel 2, speaker\r
400         mov     ax, bx\r
401         out     42h, al                         //;   send low order\r
402         mov     al, ah                          //;   load high order\r
403         out     42h, al                         //;   send high order\r
404         in      al, 61h                         //; Read IC 8255 machine status\r
405         push    ax\r
406         or      al, 00000011b\r
407         out     61h, al                         //; Turn speaker on\r
408         mov     cx, 2000h\r
409 @@delay:\r
410         loop    @@delay\r
411         pop     ax\r
412         out     61h, al                         //; Turn speaker off\r
413         pop     cx\r
414         pop     bx\r
415         pop     ax\r
416         }\r
417 }\r
418 #endif\r
419 \r
420 const char *nibble_to_binary(nibble x)\r
421 {\r
422         static char b[9];\r
423         int z;\r
424 \r
425         b[0] = '\0';\r
426         for (z = 8; z > 0; z >>= 1)\r
427         {\r
428                 strcat(b, ((x & z) == z) ? "1" : "0");\r
429         }\r
430         return b;\r
431 }\r
432 \r
433 const char *boolean_to_binary(boolean x)\r
434 {\r
435         static char b[9];\r
436         int z;\r
437 \r
438         b[0] = '\0';\r
439         for (z = 1; z > 0; z >>= 1)\r
440         {\r
441                 strcat(b, ((x & z) == z) ? "1" : "0");\r
442         }\r
443         return b;\r
444 }\r
445 \r
446 void nibbletest()\r
447 {\r
448         nibble pee;\r
449         printf("nibbletest\n");\r
450         /* nibble to binary string */\r
451         for(pee=0;pee<18;pee++)\r
452                 printf("        %u %s\n", pee, nibble_to_binary(pee));\r
453         printf("        sizeof(nibble)=%s\n", nibble_to_binary(sizeof(nibble)));\r
454         printf("end of nibble test\n");\r
455 }\r
456 \r
457 void booleantest()\r
458 {\r
459         boolean pee;\r
460         printf("booleantest\n");\r
461         /* boolean to binary string */\r
462         for(pee=0;pee<4;pee++)\r
463                 printf("        %u %s\n", pee, boolean_to_binary(pee));\r
464         printf("        sizeof(boolean)=%s\n", boolean_to_binary(sizeof(boolean)));\r
465         printf("end of boolean test\n");\r
466 }\r