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