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

📄 cpu.c

📁 32cpu的代码
💻 C
字号:
#include <stdio.h>
#include "cpu.h"



//#include "mmu.c"
//#include "cache.c"
//#include "biu.c"
//#include "mult_div.c"
//   unsigned long instr_out[16384];
//   unsigned long instr_out_1[16384];
//   unsigned long instr_out_2[16384];
//   unsigned long mem_data[1000][2];
      FILE *lf_fp_code,*reg_trace_fp,*pc_trace_fp,*sysad_trace_fp;

//   int  Reset,Soft_reset,NMI,Interrupt0,Interrupt1,Interrupt2,Interrupt3,Interrupt4,Interrupt5;
//   int  tlb_fm_flag;   //tlb_fm_flag  0:tlb   1:fm
//    unsigned long sysad_addr;
//     long sysad_data[4];
void cpu(int reset_cold,int reset_soft,int nmi_in,int int0_in,int int1_in,int int2_in,int int3_in,int int4_in,int int5_in,int addr_translate_type){
	   unsigned long success_end_flag=0xa0410000,failure_end_flag=0x1000ffff;
	long change_nbit(char op_array[],int num);
	long change_signed_nbit(char op_array[],int num);
    unsigned long icache(unsigned long virtual_addr,unsigned read_invalidate_flag);
    long dcache(unsigned long virtual_addr,long data_in,unsigned data_width,unsigned read_write_flag);
   char*  change_file_name(unsigned long physical_addr);

   void  reg_initialize(); 
   void  core(); 
//   void mmu(unsigned tlb_instr_flag);
/////////////////////////////temp vairable define//////////////
   int i,j,k,counter;   ///temp variable
   char  m,n;   ///temp variable



////////////////////Load machine code file///////////////////////

   int lf_i;
   unsigned long first_instr,second_num;

   Reset=1;
/*   char  temp[5]={"mem_"};
   char  filename[20]={"mem_"};
   printf("aaa\n");
   strcat(filename,change_file_name(0xbfc00000));
   printf("aaa\n");
   printf("%s\n",filename);
   printf("bbb\n");*/
  /* unsigned long abc;
   char tef;
   rs=10000;rt=30;
   HI=divu_hi(rs,rt);
   LO=divu_lo(rs,rt);
   printf(" HI=%d, LO=%d\n",HI,LO);
   abc=0xbfc00000;
	    tef=icache(abc)[0];
   printf("haha%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n",icache(abc)[0],icache(abc)[1],icache(abc)[2],icache(abc)[3],icache(abc)[4],icache(abc)[5],icache(abc)[6],icache(abc)[7],icache(abc)[8],icache(abc)[9],icache(abc)[10],icache(abc)[11],icache(abc)[30],icache(abc)[31]);
*/
//load mem_prog0

/*
   if((mem_prog0_fp=fopen("D:\\genghx\\temp\\Debug\\mem_prog0.dat","r"))==NULL)
   { 
   printf("cannot open the machine code file!\n");
   exit(0);
}
printf("a\n");
   for(lf_i=0;lf_i<16384;lf_i=lf_i+1){
   fscanf(mem_prog0_fp,"@%8x\t\t%8x\n",&second_num,&first_instr);

   instr_out[(second_num&(0x0000ffff))/4]=first_instr;
//   instr_out[lf_i+1]=second_instr;
//      printf("%08x_%08x\n",instr_out[lf_i+1],instr_out[lf_i]);
//   fread(&instr_out[lf_i],33,1,mem_prog0_fp);
   if(feof(mem_prog0_fp)){
	   for(i=0;i<5;i++){
	   	instr_out[lf_i+i+2]=0x00000000;
		//   for(j=0;j<32;j++){
		//   instr_out[lf_i+i][j]='0';
		 //  }
	   }
       break;
   }
}
 printf("b\n");
  for(i=0;i<108;i=i+2){
  	printf("%08x_%08x\n",instr_out[i+1],instr_out[i]);
  	}
   fclose(mem_prog0_fp);
//load mem_prog1   
   if((mem_prog1_fp=fopen("D:\\genghx\\temp\\Debug\\mem_prog1.dat","r"))==NULL)
   { 
   printf("cannot open the machine code file!\n");
   exit(0);
   }
   for(lf_i=0;lf_i<10000;lf_i=lf_i+1){
   fscanf(mem_prog1_fp,"@%8x\t\t%8x\n",&second_num,&first_instr);
   instr_out_1[(second_num&(0x0000ffff))/4]=first_instr;
//   instr_out_1[lf_i]=first_instr;
//   instr_out_1[lf_i+1]=second_instr;
//   fscanf(mem_prog1_fp,"%8x_%8x",instr_out_1[lf_i+1],instr_out_1[lf_i]);
//   fread(&instr_out_1[lf_i],33,1,mem_prog1_fp);
   if(feof(mem_prog1_fp)){
	   for(i=0;i<5;i++){
	   	instr_out_1[lf_i+i+2]=0x00000000;
	//	   for(j=0;j<32;j++){
	//	   instr_out_1[lf_i+i][j]='0';
	//	   }
	   }
       break;
   }
   }
   fclose(mem_prog1_fp);
 //load mem_prog2
   if((mem_prog2_fp=fopen("D:\\genghx\\temp\\Debug\\mem_prog2.dat","r"))==NULL)
   { 
   printf("cannot open the machine code file!\n");
   exit(0);
   }
   for(lf_i=0;lf_i<10000;lf_i=lf_i+1){
   fscanf(mem_prog2_fp,"@%8x\t\t%8x\n",&second_num,&first_instr);
   instr_out_2[(second_num&(0x0000ffff))/4]=first_instr;
//   instr_out_2[lf_i]=first_instr;
//   instr_out_2[lf_i+1]=second_instr;
//   fscanf(mem_prog1_fp,"%8x_%8x",instr_out_1[lf_i+1],instr_out_1[lf_i]);
//   fread(&instr_out_1[lf_i],33,1,mem_prog1_fp);
   if(feof(mem_prog2_fp)){
	   for(i=0;i<5;i++){
	   	instr_out_2[lf_i+i+2]=0x00000000;
	//	   for(j=0;j<32;j++){
	//	   instr_out_1[lf_i+i][j]='0';
	//	   }
	   }
       break;
   }
   }
   fclose(mem_prog2_fp);
   
  
 /////////Open mem_data file  ///////
     if((mem_data_fp=fopen("D:\\genghx\\temp\\Debug\\mem_data.dat","w+"))==NULL)
        { 
        printf("cannot open the mem_data file!\n");
        exit(0);
      } */
  ////////Open reg trace file /////////
     if((reg_trace_fp=fopen("D:\\genghx\\temp\\Debug\\reg_trace.dat","w"))==NULL)
        { 
        printf("cannot open the reg trace file!\n");
        exit(0);
      }
   ////////Open pc trace file /////////
     if((pc_trace_fp=fopen("D:\\genghx\\temp\\Debug\\pc_trace.dat","w"))==NULL)
         { 
         printf("cannot open the pc trace file!\n");
         exit(0);
      }
     if((sysad_trace_fp=fopen("D:\\genghx\\temp\\Debug\\sysad_trace.dat","w"))==NULL)
         { 
         printf("cannot open the sysad trace file!\n");
         exit(0);
      }
//   for(i=0;i<32;i++){
//   printf("%c",instruction[203][i]);
//   }
///////////////////////////end////////////////////////////////

///////////////////////////Initialize register////////////////
        reg_initialize();	
        Reset=reset_cold;
        Soft_reset=reset_soft;
        NMI=nmi_in;
        Interrupt0=int0_in;
        Interrupt1=int1_in;
        Interrupt2=int2_in;
        Interrupt3=int3_in;
        Interrupt4=int4_in;
        Interrupt5=int5_in;
        tlb_fm_flag=addr_translate_type;   
//	printf("the 12 cp0 register = %x \n",CPR[30]);
///////////////////////////end////////////////////////////////////////////////

fprintf(reg_trace_fp,"   pc   \tinstruction\tregister addr\tregister value\n");


// read config file


///////////////////////////start pipeline cycle///////////////////////////////
printf("\n*****************Start Pipeline Cycle***********************\n\n");
	do{
                core();
                
///////////////////////////judge end//////////////////////////////
    last_instruction=instruction_op;
	if(last_instruction == success_end_flag){
		printf("\n\nSuccessfully store 0x24 to memory address 0x1000ffff\n\n");
		printf("\n*****************End Pipeline Cycle***********************\n\n");
	break;
        }
        if(last_instruction == failure_end_flag){
        	printf("\n\nThe vector fail, please check the code!\n\n");
		printf("\n*****************End Pipeline Cycle***********************\n\n");
	break;
        	}
///////////////////////////end/////////////////////////////////////
	}while(1);
/*      for(i=0;i<1000;i++){
      	if(mem_data[i][0] != 0){
      	fprintf(mem_data_fp,"@0x%08x\t\t0x%08x\n",mem_data[i][0],mem_data[i][1]);
          }
        }*/
       //  fprintf(pc_trace_fp,"0x%08x\t0x%08x\n",0xbfc00000+pc_wp*4,pc_address_wp);
       //   fprintf(pc_trace_fp,"0x%08x\t0x%08x\n",0xbfc00000+pc_mp*4,pc_address_mp);
       //    fprintf(pc_trace_fp,"0x%08x\t0x%08x\n",0xbfc00000+pc_ep*4,pc_address_ep);
           
          fprintf(pc_trace_fp,"0x%08x\n",pc_address_wp);
          fprintf(pc_trace_fp,"0x%08x\n",pc_address_mp);
           fprintf(pc_trace_fp,"0x%08x\n",pc_address_ep);
           
   fclose(reg_trace_fp);
   fclose(pc_trace_fp);
   fclose(sysad_trace_fp);
//   fclose(mem_data_fp);
   end_pipeline();
}


///////////////////////////sub function definiation////////////////////////////////




end_pipeline(){
auto   char end;
   printf("please input Enter key to exit:");
   scanf("%c",&end);
   if(end){
   	   exit(0);
	   printf("end!\n");
   }
}

long change_nbit(char op_array[],int num){
auto long temp,i;
     temp=0;
for(i=0;i<num;i++){
    if(op_array[i]=='1'){
    temp=temp*2+1;
    }else if(op_array[i]=='0'){
    temp=temp*2;
    }else{
    printf("chang_nbit has error!\n");
    }

}
    return(temp);
}


long change_signed_nbit(char op_array[],int num){
auto long temp,i;
     temp=0;
for(i=0;i<num;i++){
    if(op_array[i]=='1'){
    temp=temp*2+1;
    }else if(op_array[i]=='0'){
    temp=temp*2;
    }else{
    printf("chang_nbit has error!\n");
    }

}

  if(op_array[0]=='1'){
  	temp=temp-65536;
  	}
    return(temp);
}


void check_cp0_reg(){
	auto  char  error_cp0;
	int num;
	if((CPR[0] != Index) || (CPR[1] != Random) || (CPR[2] != Entrylo0) || (CPR[3] != Entrylo1) || (CPR[4] != Context)){
                 printf("\n\nCPR and cor processor reg not equal! CPR[0]CPR[1]CPR[2]CPR[3]CPR[4]\n\nplease input Enter key to exit:");
                        scanf("%c",&error_cp0);
                        if(error_cp0){
   	                 exit(0);
	                 printf("end!\n");
                      }
	      
	}else if((CPR[5] != Pagemask) || (CPR[6] != Wired) || (CPR[8] != Badvaddr) || (CPR[9] != Count) || (CPR[10] != Entryhi)){
                 printf("\n\nCPR and cor processor reg not equal! CPR[5]CPR[6]CPR[8]CPR[9]CPR[10]\n\nplease input Enter key to exit:");
                        scanf("%c",&error_cp0);
                        if(error_cp0){
   	                 exit(0);
	                 printf("end!\n");
                      }
		
	}else if((CPR[11] != Compare) || (CPR[12] != Status) || (CPR[13] != Cause) || (CPR[14] != EPC) || (CPR[15] != Processor_Identification)){
                 printf("\n\nCPR and cor processor reg not equal! CPR[11]CPR[12]CPR[13]CPR[14]CPR[15]\n\nplease input Enter key to exit:");
                        scanf("%c",&error_cp0);
                        if(error_cp0){
   	                 exit(0);
	                 printf("end!\n");
                      }
	}else if((CPR[16] != Config) || (CPR[32] != Config1) || (CPR[17] != Load_Linked_Address) || (CPR[18] != Watchlo) || (CPR[19] != Watchhi)){
                 printf("\n\nCPR and cor processor reg not equal! CPR[16]CPR[32]CPR[17]CPR[18]CPR[19]\n\nplease input Enter key to exit:");
                        scanf("%c",&error_cp0);
                        if(error_cp0){
   	                 exit(0);
	                 printf("end!\n");
                      }
	}else if((CPR[26] != Errctl) || (CPR[28] != Taglo) || (CPR[33] != Datalo) || (CPR[30] != ErrorEPC) || (CPR[31] != Desave)){
                 printf("\n\nCPR and cor processor reg not equal! CPR[26]CPR[28]CPR[33]CPR[30]CPR[31]\n\nplease input Enter key to exit:");
                        scanf("%c",&error_cp0);
                        if(error_cp0){
   	                 exit(0);
	                 printf("end!\n");
                      }
	}else{
	        num=num + 1;
	        //printf(" ");
	}
}

void  reg_initialize(){
	int i;
///////////////////////////Initialize register////////////////
   for(i=0;i<32;i++){
	   GPR[i]=0;	
   }
	   instruction_wp=0x00000000;
	   instruction_mp=0x00000000;
	   instruction_ep=0x00000000;
	   instruction_dp=0x00000000;
	   instruction_ip=0x00000000;
    for(i=0;i<34;i++){
	   CPR[i]=0; 
   }
         Index=0;
         Random=0;
         Entrylo0=0;
         Entrylo1=0;
         Context=0;
         Pagemask=0;
         Wired=0;
         Badvaddr=0;
         Count=0;
         Entryhi=0;
         Compare=0;
         Status=0;
         Cause=0;
         EPC=0;
         Processor_Identification=0;
         Config=0;
         Config1=0;
         Load_Linked_Address=0;
         Watchlo=0;
         Watchhi=0;
         Errctl=0;
         Taglo=0;
         Datalo=0;
         ErrorEPC=0;
         Desave=0;
        CPR[9]=0x0000001b;
        Count=0x0000001b;
        CPR[12]=0x00400004;
        Status=0x00400004;
        CPR[13]=0x00008000;
        Cause=0x00008000;
        CPR[15]=0x00018009;
        Processor_Identification=0x00018009;
        CPR[16]=0x80050082;
        Config=0x80050082;
        CPR[23]=0x00008000;
        Debug=0x00008000;
        CPR[32]=0x1e984c0a;
        Config1=0x1e984c0a;
/*   	CPR[12]=0x4100e4;
   	Status=0x4100e4;
	CPR[1]=0x2d;
	CPR[9]=0xa1;
	CPR[12]=0x4100e4;
	CPR[13]=0x8000;
	CPR[15]=0x6400;
	CPR[16]=0x22482;
	CPR[30]=0xbfc00000;*/
   	pc_ip=-1;
   	pc_dp=-2;
   	pc_ep=-3;
   	pc_mp=-4;
   	pc_wp=-5;
	pc_pre=0;
	pc_address_ip=0xbfc00000 - 0x4;
	pc_address_dp=0xbfc00000 - 0x8;
	pc_address_ep=0xbfc00000 - 0xc;
	pc_address_mp=0xbfc00000 - 0x10;
	pc_address_wp=0xbfc00000 - 0x14;
//	pc_next=0;
	opa_dp=0;
	opb_dp=0;
	branch_flag=0;
	branch_flag_ep=0;
	branch_flag_mp=0;
	branch_flag_wp=0;
	trap_flag=0;
	trap_flag_ep=0;
	trap_flag_mp=0;
	exception_flag=1;
	result_first_wp=0;
	result_second_wp=0;
	jump_flag=0;
	instruction_type=0;
	normal_instr_num=0;
	normal_instr_num_wp=0;
	normal_instr_num_mp=0;
	normal_instr_num_ep=0;
	normal_instr_num_dp=0;
	special_instr_num=0;
	special_instr_num_ep=0;
	special_instr_num_dp=0;
	special2_instr_num=0;
	special2_instr_num_ep=0;
	special2_instr_num_dp=0;
	regimm_instr_num=0;
	regimm_instr_num_ep=0;
	regimm_instr_num_dp=0;
	cp0_instr_num=0;
	cp0_instr_num_ep=0;
	cp0_instr_num_dp=0;
	cp0_co_instr_num=0;
	cp0_co_instr_num_wp=0;
	cp0_co_instr_num_mp=0;
	cp0_co_instr_num_ep=0;
	cp0_co_instr_num_dp=0;
	//counter=0;
	load_store_flag_dp=0;
	load_store_flag_ep=0;
	load_store_flag_mp=0;
	load_store_flag_wp=0;
	result_regnum_wp=0;
	result_regnum_mp=0;
	result_regnum_ep=0;
	result_regnum_dp=0;
	write_trace_flag_wp=0;
	write_trace_flag_mp=0;
	write_trace_flag_ep=0;
	write_trace_flag_dp=0;
	instruction_type_ep=0;
	instruction_type_dp=0;
	ip_stop=0;
	dp_stop=0;
	ep_stop=0;
	mp_stop=0;
	wp_stop=0;
	tlb_fm_flag=0;
	ERL=0;
	exception_flag_ip=0;
	exception_flag_dp=0;
	exception_flag_ep=0;
	exception_flag_mp=0;
	exception_invalid_instr_flag=0;
//	printf("the 12 cp0 register = %x \n",CPR[30]);
///////////////////////////end////////////////////////////////////////////////
	
	}

⌨️ 快捷键说明

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