📄 sys_dos.c
字号:
/*
Copyright (C) 1996-1997 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/time.h>
#include <sys/types.h>
#include <dir.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>
#include <dpmi.h>
#include <sys/nearptr.h>
#include <conio.h>
// 2000-07-16 DOSQuake/DJGPP mem detection fix by Norberto Alfredo Bensa start
#include <crt0.h>
int _crt0_startup_flags = _CRT0_FLAG_UNIX_SBRK;
// 2000-07-16 DOSQuake/DJGPP mem detection fix by Norberto Alfredo Bensa end
#include "quakedef.h"
#include "dosisms.h"
#define MINIMUM_WIN_MEMORY 0x800000
#define MINIMUM_WIN_MEMORY_LEVELPAK (MINIMUM_WIN_MEMORY + 0x100000)
int end_of_memory;
qboolean lockmem, lockunlockmem, unlockmem;
static int win95;
#define STDOUT 1
#define KEYBUF_SIZE 256
static unsigned char keybuf[KEYBUF_SIZE];
static int keybuf_head=0;
static int keybuf_tail=0;
static quakeparms_t quakeparms;
int sys_checksum;
// 2000-07-28 DOSQuake time running too fast fix by Norberto Alfredo Bensa start
/*
static double curtime = 0.0;
static double lastcurtime = 0.0;
static double oldtime = 0.0;
*/
// 2000-07-28 DOSQuake time running too fast fix by Norberto Alfredo Bensa end
// 2000-07-16 DOSQuake wrong variable definition fix by Ender/Norberto Alfredo Bensa start
//static qboolean isDedicated;
qboolean isDedicated;
// 2000-07-16 DOSQuake wrong variable definition fix by Ender/Norberto Alfredo Bensa end
static int minmem;
float fptest_temp;
extern char start_of_memory __asm__("start");
//=============================================================================
// this is totally dependent on cwsdpmi putting the stack right after tge
// global data
// This does evil things in a Win95 DOS box!!!
#if 0
extern byte end;
#define CHECKBYTE 0xed
void Sys_InitStackCheck (void)
{
int i;
for (i=0 ; i<128*1024 ; i++)
(&end)[i] = CHECKBYTE;
}
void Sys_StackCheck (void)
{
int i;
for (i=0 ; i<128*1024 ; i++)
if ( (&end)[i] != CHECKBYTE )
break;
Con_Printf ("%i undisturbed stack bytes\n", i);
if (end != CHECKBYTE)
Sys_Error ("System stack overflow!");
}
#endif
//=============================================================================
byte scantokey[128] =
{
// 0 1 2 3 4 5 6 7
// 8 9 A B C D E F
0 , 27, '1', '2', '3', '4', '5', '6',
'7', '8', '9', '0', '-', '=', K_BACKSPACE, 9, // 0
'q', 'w', 'e', 'r', 't', 'y', 'u', 'i',
'o', 'p', '[', ']', 13 , K_CTRL,'a', 's', // 1
'd', 'f', 'g', 'h', 'j', 'k', 'l', ';',
'\'' , '`', K_SHIFT,'\\', 'z', 'x', 'c', 'v', // 2
'b', 'n', 'm', ',', '.', '/', K_SHIFT,'*',
K_ALT,' ', 0 , K_F1, K_F2, K_F3, K_F4, K_F5, // 3
K_F6, K_F7, K_F8, K_F9, K_F10,0 , 0 , K_HOME,
K_UPARROW,K_PGUP,'-',K_LEFTARROW,'5',K_RIGHTARROW,'+',K_END, //4
K_DOWNARROW,K_PGDN,K_INS,K_DEL,0,0, 0, K_F11,
K_F12,0 , 0 , 0 , 0 , 0 , 0 , 0, // 5
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0, // 6
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 // 7
};
byte shiftscantokey[128] =
{
// 0 1 2 3 4 5 6 7
// 8 9 A B C D E F
0 , 27, '!', '@', '#', '$', '%', '^',
'&', '*', '(', ')', '_', '+', K_BACKSPACE, 9, // 0
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I',
'O', 'P', '{', '}', 13 , K_CTRL,'A', 'S', // 1
'D', 'F', 'G', 'H', 'J', 'K', 'L', ':',
'"' , '~', K_SHIFT,'|', 'Z', 'X', 'C', 'V', // 2
'B', 'N', 'M', '<', '>', '?', K_SHIFT,'*',
K_ALT,' ', 0 , K_F1, K_F2, K_F3, K_F4, K_F5, // 3
K_F6, K_F7, K_F8, K_F9, K_F10,0 , 0 , K_HOME,
K_UPARROW,K_PGUP,'_',K_LEFTARROW,'%',K_RIGHTARROW,'+',K_END, //4
K_DOWNARROW,K_PGDN,K_INS,K_DEL,0,0, 0, K_F11,
K_F12,0 , 0 , 0 , 0 , 0 , 0 , 0, // 5
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0, // 6
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0,
0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 // 7
};
void TrapKey(void)
{
// static int ctrl=0;
keybuf[keybuf_head] = dos_inportb(0x60);
dos_outportb(0x20, 0x20);
/*
if (scantokey[keybuf[keybuf_head]&0x7f] == K_CTRL)
ctrl=keybuf[keybuf_head]&0x80;
if (ctrl && scantokey[keybuf[keybuf_head]&0x7f] == 'c')
Sys_Error("ctrl-c hit\n");
*/
keybuf_head = (keybuf_head + 1) & (KEYBUF_SIZE-1);
}
#define SC_UPARROW 0x48
#define SC_DOWNARROW 0x50
#define SC_LEFTARROW 0x4b
#define SC_RIGHTARROW 0x4d
#define SC_LEFTSHIFT 0x2a
#define SC_RIGHTSHIFT 0x36
#define SC_RIGHTARROW 0x4d
void MaskExceptions (void);
// 2000-07-28 DOSQuake time running too fast fix by Norberto Alfredo Bensa start
/*
void Sys_InitFloatTime (void);
*/
// 2000-07-28 DOSQuake time running too fast fix by Norberto Alfredo Bensa end
void Sys_PushFPCW_SetHigh (void);
void Sys_PopFPCW (void);
#define LEAVE_FOR_CACHE (512*1024) //FIXME: tune
#define LOCKED_FOR_MALLOC (128*1024) //FIXME: tune
void Sys_DetectWin95 (void)
{
__dpmi_regs r;
r.x.ax = 0x160a; /* Get Windows Version */
__dpmi_int(0x2f, &r);
if(r.x.ax || r.h.bh < 4) /* Not windows or earlier than Win95 */
{
win95 = 0;
lockmem = true;
lockunlockmem = false;
unlockmem = true;
}
else
{
win95 = 1;
lockunlockmem = COM_CheckParm ("-winlockunlock");
if (lockunlockmem)
lockmem = true;
else
lockmem = COM_CheckParm ("-winlock");
unlockmem = lockmem && !lockunlockmem;
}
}
void *dos_getmaxlockedmem(int *size)
{
__dpmi_free_mem_info meminfo;
__dpmi_meminfo info;
int working_size;
void *working_memory;
int last_locked;
int extra, i, j, allocsize;
static char *msg = "Locking data...";
int m, n;
byte *x;
// first lock all the current executing image so the locked count will
// be accurate. It doesn't hurt to lock the memory multiple times
last_locked = __djgpp_selector_limit + 1;
info.size = last_locked - 4096;
info.address = __djgpp_base_address + 4096;
if (lockmem)
{
if(__dpmi_lock_linear_region(&info))
{
Sys_Error ("Lock of current memory at 0x%lx for %ldKb failed!\n",
info.address, info.size/1024);
}
}
__dpmi_get_free_memory_information(&meminfo);
if (!win95) /* Not windows or earlier than Win95 */
{
working_size = meminfo.maximum_locked_page_allocation_in_pages * 4096;
}
else
{
working_size = meminfo.largest_available_free_block_in_bytes -
LEAVE_FOR_CACHE;
}
working_size &= ~0xffff; /* Round down to 64K */
working_size += 0x10000;
do
{
working_size -= 0x10000; /* Decrease 64K and try again */
working_memory = sbrk(working_size);
} while (working_memory == (void *)-1);
extra = 0xfffc - ((unsigned)sbrk(0) & 0xffff);
if (extra > 0)
{
sbrk(extra);
working_size += extra;
}
// now grab the memory
info.address = last_locked + __djgpp_base_address;
if (!win95)
{
info.size = __djgpp_selector_limit + 1 - last_locked;
while (info.size > 0 && __dpmi_lock_linear_region(&info))
{
info.size -= 0x1000;
working_size -= 0x1000;
sbrk(-0x1000);
}
}
else
{ /* Win95 section */
j = COM_CheckParm("-winmem");
if (standard_quake)
minmem = MINIMUM_WIN_MEMORY;
else
minmem = MINIMUM_WIN_MEMORY_LEVELPAK;
if (j)
{
allocsize = ((int)(Q_atoi(com_argv[j+1]))) * 0x100000 +
LOCKED_FOR_MALLOC;
if (allocsize < (minmem + LOCKED_FOR_MALLOC))
allocsize = minmem + LOCKED_FOR_MALLOC;
}
else
{
allocsize = minmem + LOCKED_FOR_MALLOC;
}
if (!lockmem)
{
// we won't lock, just sbrk the memory
info.size = allocsize;
goto UpdateSbrk;
}
// lock the memory down
write (STDOUT, msg, strlen (msg));
for (j=allocsize ; j>(minmem + LOCKED_FOR_MALLOC) ;
j -= 0x100000)
{
info.size = j;
if (!__dpmi_lock_linear_region(&info))
goto Locked;
write (STDOUT, ".", 1);
}
// finally, try with the absolute minimum amount
for (i=0 ; i<10 ; i++)
{
info.size = minmem + LOCKED_FOR_MALLOC;
if (!__dpmi_lock_linear_region(&info))
goto Locked;
}
Sys_Error ("Can't lock memory; %d Mb lockable RAM required. "
"Try shrinking smartdrv.", info.size / 0x100000);
Locked:
UpdateSbrk:
info.address += info.size;
info.address -= __djgpp_base_address + 4; // ending point, malloc align
working_size = info.address - (int)working_memory;
sbrk(info.address-(int)sbrk(0)); // negative adjustment
}
if (lockunlockmem)
{
__dpmi_unlock_linear_region (&info);
printf ("Locked and unlocked %d Mb data\n", working_size / 0x100000);
}
else if (lockmem)
{
printf ("Locked %d Mb data\n", working_size / 0x100000);
}
else
{
printf ("Allocated %d Mb data\n", working_size / 0x100000);
}
// touch all the memory to make sure it's there. The 16-page skip is to
// keep Win 95 from thinking we're trying to page ourselves in (we are
// doing that, of course, but there's no reason we shouldn't)
x = (byte *)working_memory;
for (n=0 ; n<4 ; n++)
{
for (m=0 ; m<(working_size - 16 * 0x1000) ; m += 4)
{
sys_checksum += *(int *)&x[m];
sys_checksum += *(int *)&x[m + 16 * 0x1000];
}
}
// give some of what we locked back for malloc before returning. Done
// by cheating and passing a negative value to sbrk
working_size -= LOCKED_FOR_MALLOC;
sbrk( -(LOCKED_FOR_MALLOC));
*size = working_size;
return working_memory;
}
/*
============
Sys_FileTime
returns -1 if not present
============
*/
int Sys_FileTime (char *path)
{
struct stat buf;
if (stat (path,&buf) == -1)
return -1;
return buf.st_mtime;
}
void Sys_mkdir (char *path)
{
mkdir (path, 0777);
}
void Sys_Sleep(void)
{
}
char *Sys_ConsoleInput(void)
{
static char text[256];
static int len = 0;
char ch;
if (!isDedicated)
return NULL;
if (! kbhit())
return NULL;
ch = getche();
switch (ch)
{
case '\r':
putch('\n');
if (len)
{
text[len] = 0;
len = 0;
return text;
}
break;
case '\b':
putch(' ');
if (len)
{
len--;
putch('\b');
}
break;
default:
text[len] = ch;
len = (len + 1) & 0xff;
break;
}
return NULL;
}
void Sys_Init(void)
{
MaskExceptions ();
Sys_SetFPCW ();
// 2000-07-28 DOSQuake time running too fast fix by Norberto Alfredo Bensa start
/*
dos_outportb(0x43, 0x34); // set system timer to mode 2
dos_outportb(0x40, 0); // for the Sys_FloatTime() function
dos_outportb(0x40, 0);
Sys_InitFloatTime ();
*/
// 2000-07-28 DOSQuake time running too fast fix by Norberto Alfredo Bensa end
_go32_interrupt_stack_size = 4 * 1024;;
_go32_rmcb_stack_size = 4 * 1024;
}
void Sys_Shutdown(void)
{
if (!isDedicated)
dos_restoreintr(9);
if (unlockmem)
{
dos_unlockmem (&start_of_memory,
end_of_memory - (int)&start_of_memory);
dos_unlockmem (quakeparms.membase, quakeparms.memsize);
}
}
#define SC_RSHIFT 0x36
#define SC_LSHIFT 0x2a
void Sys_SendKeyEvents (void)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -