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