⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 init.c

📁 简单的虚拟机
💻 C
字号:
#include <stdio.h>#include <sys/stat.h>#include <stdlib.h>#include <stdbool.h>#include "dbgdata.h"#include "init.h"bool initcpu(struct CmdLine cl){	int fileSize;	FILE* fptr;	struct HeaderRec hr;	struct RelocRec* rptr=NULL;	int bytecodeStart;	int bytecodeEnd;	int exec_bytes;	int total_bytes;	/*1) open executable and read header */	fileSize = getFileSize(cl.binFile);	if(fileSize==0)	{		printf("initcpu(): executable has zero size!\n");		return false;	}	if(!(fptr=fopen(cl.binFile,"rb")))	{		printf("initcpu(): cannot open %s!\n",cl.binFile);		return false;	}	if(readDebugData(&hr,rptr,fptr)==false)		return false;	/*2) validate header data */	if(hr.magic!=SIGNATURE)	{		printf("initcpu(): file not DOS executable!\n");		if(rptr)			free(rptr);		fclose(fptr);		return false;	}	/*3) user header data to allocate RAM and init cpu registers */	bytecodeStart = hr.hdsize * 16;	bytecodeEnd = fileSize - 1;	exec_bytes = bytecodeEnd - bytecodeStart + 1;	if(hr.minparas * 16 > AVAILMEM)	{		printf("initcpu(): not enough memory to init runtime\n");		if(rptr)			free(rptr);		fclose(fptr);		return false;	}	total_bytes = (hr.maxparas*16)>AVAILMEM?AVAILMEM:(hr.maxparas*16);	if(exec_bytes>total_bytes)	{		printf("initcpu(): error exe file, ram alloc mismatch!\n");		if(rptr)			free(rptr);		fclose(fptr);		return false;	}		printf("initcpu(): allocating %d bytes\n",total_bytes);	cpu.ram = (byte*)malloc(total_bytes); 	cpu.greg.ax=0;	cpu.greg.cx=0;	cpu.greg.dx=0;	cpu.greg.bx=0;	cpu.greg.sp=hr.sp;	cpu.greg.bp=0;	cpu.greg.si=0;	cpu.greg.di=0;	cpu.sreg.es=0;	cpu.sreg.cs=hr.cs;	cpu.sreg.ss=hr.ss;	cpu.sreg.ds=0;	cpu.sreg.fs=0;	cpu.sreg.gs=0;	cpu.ip=hr.ip;	cpu.flags=0;	cpu.status=true;	if(cpu.sreg.ss!=0)	{		cpu.sreg.ds=cpu.sreg.ss+(cpu.greg.sp>>4)+1;		cpu.ram[(cpu.sreg.ds<<4)]=0xCD;		cpu.ram[(cpu.sreg.ds<<4)+1]=0x20;	}	/*4) load bytecode into ram */
	fseek(fptr,bytecodeStart,SEEK_SET);	fread(cpu.ram, 1, exec_bytes, fptr);
		if(feof(fptr)||ferror(fptr))		{
			printf("initcpu(): error exe file, exec bytes mismatch!\n");
				if(rptr)					free(rptr);			fclose(fptr);			return false;
		}	/*relocating*/	/**************************************************************************/	if(rptr)		free(rptr);	fclose(fptr);
		return true;}int getFileSize(char *name){	char ret;	struct stat buffer;	ret = lstat(name,&buffer);	if(ret==0)	{		return (int)buffer.st_size;	}	else	{		return 0;	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -