Advertisement
Guest User

Lel

a guest
May 26th, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include "xcpu.h"
  5.  
  6. int main(int argc, char* argv[]){
  7.   int i = 0;
  8.   int k = 0;
  9.   int status;
  10.   int cycles;
  11.   char code;
  12.   FILE* file;
  13.   struct xcpu_context c;
  14.  
  15.   c.memory = malloc(sizeof(unsigned char)*0xFFFF);  //Memory allocation of 0s, 65536
  16.   c.pc=0;                                           //Program Counter = 0
  17.   c.state=0x0000;                                   //State initiated to 0
  18.   for(i=0;i<=15;i++){                               //All registers set to 0
  19.     c.regs[i]=0x0000;
  20.   }
  21.  
  22.   if(argc!=3){
  23.     printf("Format is xsim cycles filename\n");
  24.     exit(0);
  25.   }
  26.  
  27.   if((file=fopen(argv[2], "r")) == NULL){
  28.     printf("Unable to open File %s\n", argv[2]);
  29.     exit(0);
  30.   }
  31.  
  32.   cycles=atoi(argv[1]);                              //Transforms from int to string type
  33.  
  34.   if(cycles<0){
  35.     printf("Cycles must be 0 and higher\n");
  36.     exit(0);
  37.   }
  38.  
  39.   while(fread(&code, sizeof(char), 1, file)){
  40.     c.memory[k]=code;
  41.     k++;
  42.     if(k==0xFFFF){
  43.       printf("ERROR: Out of memory\n");
  44.       exit(0);
  45.     }
  46.   }
  47.  
  48.   if(cycles == 0){                                   //Cycle execution
  49.       while(status != 0){
  50.         status = xcpu_execute(&c);
  51.         if(status == 0){
  52.           printf("MESSAGE: CPU has halted.\n");
  53.           break;
  54.         }
  55.          if(((c.state&0x0002)>>1) == 0x0001){
  56.          xcpu_print(&c);
  57.         }
  58.       }
  59.   }
  60.  
  61.   else{
  62.     for(k = 0; k <= cycles; k++){
  63.         status = xcpu_execute(&c);
  64.         if(status == 0){
  65.           printf("MESSAGE: CPU has halted.\n");
  66.           break;
  67.         }
  68.          if((c.state&0x0002)>>1 == 1){
  69.          xcpu_print(&c);
  70.         }
  71.     }    
  72.     if(status==1) printf("WARNING: CPU has timed-out.\n");
  73.   }
  74.   return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement