📄 intwin32.c
字号:
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ +
+ intwin32.c - the file has all the win32 interrupt code +
+ ( native system calls are needed to implement +
+ basic I/O functionality ) +
+ +
+ could not include this stuff in win32.c because +
+ we make use of system registers and globals which +
+ are defined later +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ handlefileIO +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void handlefileIO()
{
int retval;
switch((U1)R[$R1])
{
case 0:
{
char *modes[]={"rb","wb","ab","wb+","ab+"};
FILE *fptr;
if(R[$R3]>4){ R[$R4]=0; return; }
/*printf("opening %s\n",&RAM[R[$R2]]);*/
fptr = fopen(&RAM[R[$R2]],modes[R[$R3]]);
if(fptr==NULL)
{ R[$R4]=0; /*printf("cannot open file\n");*/ }
else{ R[$R4] = (U8)fptr; }
}break;
case 1:
{
R[$R3]=fclose((FILE *)R[$R2]);
/*if(R[$R3]){ printf("error closing file\n"); }
else{ printf("closed file OK\n"); }*/
}break;
case 2:
{
R[$R3]=fflush((FILE *)R[$R2]);
}break;
case 3:
{
R[$R3]=fflush(stdin);
}break;
case 4:
{
R[$R3]=fflush(stdout);
}break;
case 5:
{
R[$R3]=fflush(stderr);
}break;
case 6:
{
rewind((FILE *)R[$R2]);
}break;
case 7:
{
long pos;
pos = ftell((FILE *)R[$R2]);
if(pos==-1L){ R[$R3]=0; R[$R4]=1; }
else{ R[$R3]=pos; R[$R4]=0; }
}break;
case 8:
{
switch(R[$R4])
{
case 0:{ R[$R5] = fseek((FILE *)R[$R2],(long)R[$R3],SEEK_SET); }break;
case 1:{ R[$R5] = fseek((FILE *)R[$R2],(long)R[$R3],SEEK_END); }break;
case 2:{ R[$R5] = fseek((FILE *)R[$R2],(long)R[$R3],SEEK_CUR); }break;
default:{ R[$R5] = fseek((FILE *)R[$R2],(long)R[$R3],SEEK_SET); }
}
}break;
case 9:
{
R[$R3]=feof((FILE *)R[$R2]);
}break;
case 10:
{
R[$R3]=ferror((FILE *)R[$R2]);
}break;
case 11:
{
size_t nbytes;
nbytes = fread(&RAM[R[$R4]],1,(size_t)R[$R3],(FILE *)R[$R2]);
if(nbytes<(size_t)R[$R3])
{
if(feof((FILE *)R[$R2])){ R[$R5]=nbytes; R[$R6]=0; }
else if(ferror((FILE *)R[$R2])){ R[$R5]=nbytes; R[$R6]=1;}
else{ R[$R5]=nbytes; R[$R6]=0; }
}
else{ R[$R5]=nbytes; R[$R6]=0; }
}break;
case 12:
{
size_t nbytes;
nbytes = fwrite(&RAM[R[$R4]],1,(size_t)R[$R3],(FILE *)R[$R2]);
if(nbytes<(size_t)R[$R3])
{
if(feof((FILE *)R[$R2])){ R[$R5]=nbytes; R[$R6]=0; }
else if(ferror((FILE *)R[$R2])){ R[$R5]=nbytes; R[$R6]=1;}
else{ R[$R5]=nbytes; R[$R6]=0; }
}
else{ R[$R5]=nbytes; R[$R6]=0; }
}break;
case 13:
{
size_t nbytes;
nbytes = fread(&RAM[R[$R4]],1,(size_t)R[$R3],stdin);
if(nbytes<(size_t)R[$R3])
{
if(feof(stdin)){ R[$R5]=nbytes; R[$R6]=0; }
else if(ferror(stdin)){ R[$R5]=nbytes; R[$R6]=1;}
else{ R[$R5]=nbytes; R[$R6]=0; }
}
else{ R[$R5]=nbytes; R[$R6]=0; }
}break;
case 14:
{
size_t nbytes;
nbytes = fwrite(&RAM[R[$R4]],1,(size_t)R[$R3],stdout);
if(nbytes<(size_t)R[$R3])
{
if(feof(stdout)){ R[$R5]=nbytes; R[$R6]=0; }
else if(ferror(stdout)){ R[$R5]=nbytes; R[$R6]=1;}
else{ R[$R5]=nbytes; R[$R6]=0; }
}
else{ R[$R5]=nbytes; R[$R6]=0; }
}break;
case 15:
{
size_t nbytes;
nbytes = fwrite(&RAM[R[$R4]],1,(size_t)R[$R3],stderr);
if(nbytes<(size_t)R[$R3])
{
if(feof(stderr)){ R[$R5]=nbytes; R[$R6]=0; }
else if(ferror(stderr)){ R[$R5]=nbytes; R[$R6]=1;}
else{ R[$R5]=nbytes; R[$R6]=0; }
}
else{ R[$R5]=nbytes; R[$R6]=0; }
}break;
case 16:
{
retval = printf("%c",(char)R[$R2]);
if(retval < 0){ R[$R3]=0; R[$R4]=1; }
else{ R[$R3]=retval; R[$R4]=0; }
}break;
case 17:
{
retval = wprintf(L"%lc",(wchar_t)R[$R2]);
if(retval < 0){ R[$R3]=0; R[$R4]=1; }
else{ R[$R3]=retval; R[$R4]=0; }
}break;
case 18:
{
retval = printf("%s",(char *)&RAM[R[$R2]]);
if(retval < 0){ R[$R3]=0; R[$R4]=1; }
else{ R[$R3]=retval; R[$R4]=0; }
}break;
case 19:
{
retval = wprintf(L"%ls",(wchar_t *)&RAM[R[$R2]]);
if(retval < 0){ R[$R3]=0; R[$R4]=1; }
else{ R[$R3]=retval; R[$R4]=0; }
}break;
case 20: /* note signed vs. unsigned */
{
if(R[$R3]==1){ retval = printf("%I64u",R[$R2]); }
else{ retval = printf("%I64d",R[$R2]); }
if(retval < 0){ R[$R3]=0; R[$R4]=1; }
else{ R[$R3]=retval; R[$R4]=0; }
}break;
case 21:
{
retval = printf("%le",Rd[$D1]);
if(retval < 0){ R[$R3]=0; R[$R4]=1; }
else{ R[$R3]=retval; R[$R4]=0; }
}break;
case 22:
{
retval = scanf("%s",(char *)&RAM[R[$R2]]);
if((retval==EOF)||(retval==0)){ R[$R3]=0; }else{ R[$R3]=retval; }
}break;
case 23:
{
retval = wscanf(L"%ls",(wchar_t *)&RAM[R[$R2]]);
if((retval==EOF)||(retval==0)){ R[$R3]=0; }else{ R[$R3]=retval; }
}break;
case 24:
{
if(R[$R3]==1){ retval = scanf("%I64u",&R[$R2]); }
else{ retval = scanf("%I64d",&R[$R2]); }
if((retval==EOF)||(retval==0)){ R[$R3]=0; }else{ R[$R3]=retval; }
}break;
case 25:
{
retval = scanf("%le",&Rd[$D1]);
if((retval==EOF)||(retval==0)){ R[$R3]=0; }else{ R[$R3]=retval; }
}break;
default:
{
ERROR1_LVL2("INT 0 %lu function not handled",(U1)R[$R1]);
}
}
return;
}/*end handlefileIO*/
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ handleFileManagement +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#include<io.h>
#include<fcntl.h>
#include<sys\stat.h>
#include<direct.h>
void handleFileManagement()
{
int retval;
switch((U1)R[$R1])
{
case 0:
{
retval = _access(&RAM[R[$R2]],0); /*exist*/
if(retval==0){ R[$R3]=1; }
else{ R[$R3]=0; }
}break;
case 1:
{
DWORD result;
result = GetFileAttributes(&RAM[R[$R2]]);
if(result==0xFFFFFFFF)
{
R[$R3]=2;
}
else
{
if(result&FILE_ATTRIBUTE_DIRECTORY)
{
R[$R3]=1;
}
else
{
R[$R3]=0;
}
}
}break;
case 2:
{
retval = _access(&RAM[R[$R2]],4); /*read*/
if(retval==0){ R[$R3]=1; }
else{ R[$R3]=0; }
}break;
case 3:
{
retval = _access(&RAM[R[$R2]],2); /*write*/
if(retval==0){ R[$R3]=1; }
else{ R[$R3]=0; }
}break;
case 4:
{
int handle;
handle = _open(&RAM[R[$R2]],_O_RDONLY);
if(handle==-1)
{
R[$R3] = 0;
R[$R4] = 1;
}
else
{
long size;
size = _filelength(handle);
if(size==-1L)
{
R[$R3] = 0;
R[$R4] = 1;
}
else
{
R[$R3] = size;
R[$R4] = 0;
}
_close(handle);
}
}break;
case 5:
{
int handle;
handle = _open(&RAM[R[$R2]],_O_RDONLY);
if(handle==-1)
{
R[$R3] = 0;
R[$R4] = 1;
}
else
{
struct _stat fields;
retval = _fstat(handle,&fields);
if(retval==-1L)
{
R[$R3] = 0;
R[$R4] = 1;
}
else
{
/*
typedef long time_t
time_t st_mtime
*/
R[$R3] = fields.st_mtime;
R[$R4] = 0;
}
_close(handle);
}
}break;
case 6:
{
if(remove(&RAM[R[$R2]])==-1){ R[$R3]=1; }
else{ R[$R3]=0; }
}break;
case 7:
{
if(_mkdir(&RAM[R[$R2]])==-1){ R[$R3]=1; }
else{ R[$R3]=0; }
}break;
case 8:
{
if(_rmdir(&RAM[R[$R2]])==-1){ R[$R3]=1; }
else{ R[$R3]=0; }
}break;
case 9:
{
char full[_MAX_PATH];
if(_fullpath(full,&RAM[R[$R2]],_MAX_PATH)!=NULL)
{
int len;
len = strlen(full);
strcpy(&RAM[R[$R3]],full);
R[$R4]=0;
R[$R5]=len+1; /*include null*/
}
else
{
R[$R4]=1;
R[$R5]=0;
}
}break;
case 10:
{
R[$R2]=_MAX_PATH;
}break;
case 11:
{
/* execute dir /b */
FILE *pipe;
char buffer[16*1024];
strcpy(buffer,"dir ");
/*max windows filename is 215 chars in length*/
if(strlen(&RAM[R[$R2]])>215)
{
R[$R5]=0;
R[$R6]=1;
}
else
{
strcat(buffer,&RAM[R[$R2]]);
strcat(buffer," /b");
pipe = _popen(buffer,"rb");
if(pipe==NULL)
{
R[$R5]=0;
R[$R6]=1;
}
else
{
R[$R5]=fread(buffer,1,16*1024,pipe);
if(R[$R3]!=1)
{
strcpy(&RAM[R[$R4]],buffer);
}
R[$R6]=0;
_pclose(pipe);
}
}
}break;
case 12:
{
if(rename(&RAM[R[$R2]],&RAM[R[$R3]]))
{
R[$R4]=1;
}
else
{
R[$R4]=0;
}
}break;
default:
{
ERROR1_LVL2("INT 1 %lu function not handled",(U1)R[$R1]);
}
}
return;
}/*end handleFileManagement*/
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ handleFileManagement +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#include<process.h>
void handleProcessManagement()
{
switch((U1)R[$R1])
{
case 0:
{
BOOL retval;
STARTUPINFO sinfo;
PROCESS_INFORMATION pinfo;
GetStartupInfo(&sinfo);
retval = CreateProcess(NULL,
&RAM[R[$R2]],
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&sinfo,
&pinfo);
if(retval==FALSE)
{
R[$R4]=0;
R[$R5]=1;
}
else
{
R[$R4]=(int)pinfo.hProcess;
R[$R5]=0;
}
}break;
case 1:
{
BOOL retval;
int code;
code=STILL_ACTIVE;
while(code==STILL_ACTIVE)
{
retval = GetExitCodeProcess((HANDLE)R[$R2],&code);
}
if(retval==FALSE)
{
R[$R3]=0;
R[$R4]=1;
}
else
{
R[$R3]=code;
R[$R4]=0;
}
}break;
case 2:
{
int retval;
retval = system(&RAM[R[$R2]]);
if(retval==-1)
{
R[$R3]=0;
R[$R4]=1;
}
else
{
R[$R3]=retval;
R[$R4]=0;
}
}break;
case 3:
{
FILE *pipe;
pipe = _popen(&RAM[R[$R2]],"rb");
if(pipe==NULL)
{
R[$R5]=0;
R[$R6]=1;
}
else
{
R[$R5]=fread(&RAM[R[$R3]],1,(size_t)R[$R4],pipe);
RAM[R[$R3]+R[$R5]]='\0';
R[$R6]=0;
_pclose(pipe);
}
}break;
default:
{
ERROR1_LVL2("INT 2 %lu function not handled",(U1)R[$R1]);
}
}
return;
}/*end handleProcessManagement*/
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ handleTimeDateCall +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void handleTimeDateCall()
{
switch((U1)R[$R1])
{
case 0:
{
R[$R2]= time(NULL);
}break;
case 1:
{
struct tm *tptr;
time_t t;
t=(time_t)R[$R2];
tptr = gmtime(&t);
R[$R3]=(*tptr).tm_sec;
R[$R4]=(*tptr).tm_min;
R[$R5]=(*tptr).tm_hour;
R[$R6]=(*tptr).tm_mday;
R[$R7]=(*tptr).tm_mon;
R[$R8]=(*tptr).tm_year;
}break;
case 2:
{
struct tm *tptr;
time_t t;
t=(time_t)R[$R2];
tptr = localtime(&t);
R[$R3]=(*tptr).tm_sec;
R[$R4]=(*tptr).tm_min;
R[$R5]=(*tptr).tm_hour;
R[$R6]=(*tptr).tm_mday;
R[$R7]=(*tptr).tm_mon;
R[$R8]=(*tptr).tm_year;
}break;
case 3:
{
struct tm ts;
time_t t;
(ts).tm_sec= (int)R[$R2];
(ts).tm_min= (int)R[$R3];
(ts).tm_hour= (int)R[$R4];
(ts).tm_mday= (int)R[$R5];
(ts).tm_mon= (int)R[$R6];
(ts).tm_year= (int)R[$R7];
t = mktime(&ts);
R[$R8]=t;
}break;
case 4:
{
char datestr[26];
time_t t;
t = (time_t)R[$R2];
strcpy(datestr,ctime(&t));
strcpy(&RAM[R[$R3]],datestr);
RAM[R[$R3]+24]=0;
}break;
case 5:
{
R[$R4] = (long)difftime((time_t)R[$R3],(time_t)R[$R2]);
}break;
case 6:
{
R[$R2] = GetTickCount();
}break;
default:
{
ERROR1_LVL2("INT 4 %lu function not handled",(U1)R[$R1]);
}
}
return;
}/*end handleTimeDateCall*/
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ handleCommandLine +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void handleCommandLine()
{
switch((U1)R[$R1])
{
case 0:
{
R[$R2]= programArgs.nArgs;
}break;
case 1:
{
if(R[$R2]<programArgs.nArgs)
{
strncpy(&RAM[R[$R3]],programArgs.args[R[$R2]],256);
RAM[R[$R3]+256]=0;
R[$R4]=0;
R[$R5]= strlen(&RAM[R[$R3]]);
R[$R5]= R[$R5] + 1;
}
else
{
R[$R4]=1;
R[$R5]=0;
}
}break;
default:
{
ERROR1_LVL2("INT 5 %lu function not handled",(U1)R[$R1]);
}
}
return;
}/*end handleCommandLine*/
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ handleCommandLine +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
void handleMemoryStatus()
{
switch((U1)R[$R1])
{
case 0:
{
R[$R2]=R[$BE]+1;
R[$R3]=R[$HE]-R[$HS]+1;
R[$R4]=R[$TOP]-R[$SS]+1;
R[$R5]=R[$TOP]+1;
}break;
case 1:
{
MEMORYSTATUS ms;
GlobalMemoryStatus(&ms);
R[$R2]=ms.dwTotalPhys;
R[$R3]=ms.dwTotalPhys - ms.dwAvailPhys;
R[$R4]=ms.dwTotalPageFile;
R[$R5]=ms.dwTotalPageFile - ms.dwAvailPageFile;
}break;
default:
{
ERROR1_LVL2("INT 6 %lu function not handled",(U1)R[$R1]);
}
}
return;
}/*end handleMemoryStatus*/
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ handleAllocCall +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#define FREE 0
#define RESERVED 1
char *statStr[] = {"FREE","RESERVED"};
/* Memory block header = 17 bytes = 8 previous + 8 next + status */
#define MEM_HEADER 17
#define prev(i) *((U8 *)&memory[i])
#define next(i) *((U8 *)&memory[i+8])
#define status(i) memory[i+16]
#define size(i) next(i)-i-MEM_HEADER
/* if going to split free block, need at least 8 bytes in new free part */
#define MIN_FREE_BYTES 8
U1 *memory; /*alias for RAM */
U8 first; /*stores address of first byte of heap*/
U8 last; /*store address of last byte of heap + 1*/
/* prototypes------------------------------------------------------*/
void heapInit();
U8 alloc(U8 nbytes);
int currentNodeAlloc(U8 i,U8 nbytes);
void deAlloc(U8 address);
void printMemory();
/* definitions-----------------------------------------------------*/
void handleAllocCall()
{
switch((U1)R[$R1])
{
case 0:
{
R[$R3] = alloc(R[$R2]);
}break;
case 1:
{
deAlloc(R[$R2]);
}break;
case 2:
{
printMemory();
}break;
case 3:
{
heapInit();
}break;
default:
{
ERROR1_LVL2("INT 7 %lu function not handled",(U1)R[$R1]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -