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

📄 dongfeng.c

📁 一个MIPS虚拟机的源码
💻 C
字号:
#ifdef EXTERN
#undef EXTERN
#endif
#define EXTERN 


#include "global.h"

static void useage()
{
	fprintf(stderr,"*---DongFeng MIPS Simulator V0.1---*\n");
	fprintf(stderr,"*Useage: [-d] -f [rom file path]------*\n");
	fprintf(stderr,"*--------------------------------*\n");
	
}
static boolean get_options(int argc,char ** argv)
{
	int len;
	int i;
	
	if (argc==1)
		{
			fprintf(stderr,"You should give the rom file path!\n");
			return false;
		}
	for (i=1;i<argc;i++)
		{
			if ( strncasecmp(argv[i],"-d",strlen(argv[i]) )==0)
				{
					options.debug_mode = true;
					debug.break_point_header=NULL;
					debug.break_point_tail = NULL;
					debug.break_point_number = 0;
					debug.current_mode=STEP;
				}
			else if(strncasecmp(argv[i],"-f",strlen(argv[i]))==0)
				{
					i++;
					if (i>=argc)
						{
							fprintf(stderr,"You should give the rom file path!\n");
							return false;
						}
					len = strlen(argv[i])+1;
					if ((options.rom_file_path = malloc(len))==NULL)
						{
							fprintf(stderr,"Malloc a memory region error!\n");
							return false;
						}
					strncpy(options.rom_file_path,argv[i],len);
					options.rom_file_path[len-1]='\0';
				}
			else if(strncasecmp(argv[i],"-keybord",strlen(argv[i]))==0)
				{
					i++;
					if (i>=argc)
						{
							options.keybord[0]='\0';
							return true;
						}
					len = strlen(argv[i])+1;
					strncpy(options.keybord,argv[i],len);
					options.keybord[len-1]='\0';
				}
			else if(strncasecmp(argv[i],"-display",strlen(argv[i]))==0)
				{
					i++;
					if (i>=argc)
						{
							options.display[0]='\0';
							return true;
						}
					len = strlen(argv[i])+1;
					strncpy(options.display,argv[i],len);
					options.display[len-1]='\0';
				}
			else
				useage();
			
		}
	//printf(options.rom_file_path);
	//printf("%s \n",options.keybord);
	//printf("%s \n",options.display);
	
	if (options.rom_file_path==NULL)
		{
			//useage();
			return false;
		}
			
	
	
	//sprintf(debug_message,"rom_file_path is :%s\n",options.rom_file_path);
	//DEBUG_PRINT(debug_message);
	
	return true;
	
}

static boolean get_host_end()
{
	/*get host machine end,
	will change the value of global variable "host_end(define in global.h)" */
	union {
		int as_int;
		char as_char[4];
		} either;

	either.as_int = 0x12345678;
	
	if ((sizeof(int) == 4)&& (either.as_char[0]==0x78))
		{
			host_end = LITTLEEND;
			return true;
		}
	else if ((sizeof(int) == 4)&& (either.as_char[0]==0x12))
		{
			host_end = BIGEND;
			return true;
		}
	else
		{
			fprintf(stderr,"Can not dectect host end!\n");
			return false;
		}
	}
static void set_target_end()
{
	target_end = LITTLEEND;

}

static void init_cpu_registers()
{
	int i;
	for (i=0;i<32;i++)
		{
			cpu_register[i]=0x00000000;
		}
	hi = 0x00000000;
	lo = 0x00000000;
	
	
}

static void init_cp0_register()
{
	cp0_register[INDEX] = 0x00000000;
	/* set the init value = 63*/
	cp0_register[RANDOM]=CP0_RANDOM_UPPER_BOUND <<8;
	cp0_register[ENTRYLO]=0x00000000;
	cp0_register[CONTEXT]=0x00000000;
	cp0_register[BADVADDR]=0x00000000;
	cp0_register[ENTRYHI]=0x00000000;
	/* CU1=0(we don not implement FP in this version) CU0=1 BEV=1 KUC=0(kernel)*/
	cp0_register[SR]=(0x00000000 |  CP0_SR_CU0_MASK | CP0_SR_BEV_MASK) &\
					~CP0_SR_KUC_MASK ;
	
	cp0_register[CAUSE]=0x00000000;
	cp0_register[EPC]=0x00000000;
	cp0_register[PRID]= 0x00000201; //R3000 IMP=2  version = 1
	
	
}
static void init_interrupt_controller()
{
	int i;
	interrupt_controller.connected_devices = 2;
	//for (i=0;i<INTERRUPT_LINE_NUMBER;i++)
	//	interrupt_controller.interrupt_line.interrupt_word=0x00000000;
	interrupt_controller.connected_devices=0;
}

static void init_console()
{
	int fd;

	console.keybord.data_reg = 0x00000000 & CONSOLE_KEYBORD_DATA_MASK;
	console.keybord.control_reg = CONSOLE_KEYBORD_UNREADY;
	
	strcpy(console.keybord.device_name,"Keybord");
	/*connect interrupt controller line 3 to keybord*/
	connect_to_interrupt_controller(console.keybord, 3);
	//fprintf(stdout,"Connect %s to Interrupt controller line %d\n",console.keybord.device_name ,3);
	if (options.keybord[0]!='\0')
		{
			console.keybord_fd = open(options.keybord, O_RDWR | O_NONBLOCK);
			if (console.keybord_fd==0)
				console.keybord_fd = open(ttyname(0), O_RDWR | O_NONBLOCK);
		}
	else
		console.keybord_fd = open(ttyname(0), O_RDWR | O_NONBLOCK);
	
	//fprintf(stdout,"keybord_fd is  %d\n", console.keybord_fd);
	
	console.display.control_reg = CONSOLE_DISPLAY_UNREADY;
	console.display.data_reg= 0x00000000 & CONSOLE_DISPLAY_DATA_MASK;
	strcpy(console.keybord.device_name , "Display");
	/*connect interrupt controller line 4 to keybord*/
	connect_to_interrupt_controller(console.display, 4);
	//fprintf(stdout,"Connect %s to Interrupt controller line %d\n",console.keybord.device_name ,4);
	//console.display_fd = ttyname(0);
	if (options.display[0]!='\0')
		{
			console.display_fd = open(options.display, O_RDWR | O_NONBLOCK);
			if (console.display_fd==0)
				console.display_fd = open(ttyname(0), O_RDWR | O_NONBLOCK);
		}
	else
		console.display_fd = open(ttyname(0), O_RDWR | O_NONBLOCK);
	//fprintf(stdout,"display_fd is  %d\n",console.display_fd);
	

}
static void init_cache()
{
	int i;
		
	for (i=0;i<DCACHE_SIZE/4;i++)
		{
			dcache[i].cache_line = 0x00000000;
			dcache[i].tag = 0x00000000;
			dcache[i].flag=0x00000000;
		}
		
		for (i=0;i<ICACHE_SIZE/4;i++)
		{
			icache[i].cache_line = 0x00000000;
			icache[i].tag = 0x00000000;
			icache[i].flag=0x00000000;
		}
		
	
	
}


static void init_ram()
{
	int i;
	for (i=0;i<RAM_SIZE;i++)
		ram[i]=0x00;
}

static void init_tlb()
{
	int i;
	for (i=0;i<TLBENTRY_NO;i++)
		{
			tlb[i].entryhi=0x00000000;
			tlb[i].entrylo=0x00000000;
		}
}

static boolean load_rom()
{
	FILE * fp;
	int i;
	int rom_file_size;
	
	//printf("options.rom_file_path %s" , options.rom_file_path);
	fp=fopen(options.rom_file_path,"r");
	//printf("options.rom_file_path %s" , options.rom_file_path);
	if(fp	== NULL) 
		{
			fprintf(stderr,"rom file can not be find!\n");
			return false;
		}
	fseek(fp,0,SEEK_END);
 	rom_file_size = ftell(fp);
  	rewind(fp);
	if (rom_file_size%4 !=0)
		{
			fprintf(stderr,"rom_file_size can not be divied by 4!\n");
			fclose(fp);
			return false;
		}
	
	//sprintf(debug_message,"rom_file_size is :%d bytes\n",rom_file_size);
	//DEBUG_PRINT(debug_message);
	if (rom_file_size!= ROM_SIZE)
		{
			fprintf(stderr,"rom_file_size can not equal %d!\n",ROM_SIZE);
			fclose(fp);
			return false;
		}

	for (i=0;i<ROM_SIZE;i++)
		{
			//UINT8 temp[4];
			fread(&rom[i] ,1,1,fp);
			//rom[i] = host2word(temp);
		}
	//printf("ROM 42 %x",rom[42]);
	
	return true;


}

static boolean simulate_init()
{
	//DEBUG_PRINT("Now we are in simulate_init\n");
	
	if (!get_host_end())
		{
		fprintf(stderr,"Error occus when get_host_end() ,aborting!\n");
		return false;
		}
	set_target_end();
	init_cpu_registers();
	init_cp0_register();
	init_cache();
	init_ram();
	init_tlb();
	init_interrupt_controller();
	init_console();
	if (!load_rom())
		{
			fprintf(stderr,"Error occus when load_rom ,aborting!\n");
			return false;
		}

	
	
}
static boolean simulate_start()
{
	return cpu_simulate_start();
}
int main(int argc, char ** argv)
{
	if (!get_options(argc,argv))
		{
			fprintf(stderr,"Error occus when get_options ,aborting!\n");
			return false;
		}
	if (!simulate_init())
		{
			fprintf(stderr,"Error occus when simulate_init ,aborting!\n");
			return false;
		}
	//fprintf(stdout,"/*---------------------------------------*/");
	fprintf(stdout,"\nNow we are entering the magic matrix   ^_^\n");
	//fprintf(stdout,"/*---------------------------------------*/\n");
	if (!simulate_start())
		{
			fprintf(stderr,"Error occus when simulate_start ,aborting!\n");
			return false;
		}

	
}

⌨️ 快捷键说明

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