Advertisement
Guest User

Untitled

a guest
May 25th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 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]);
  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. //Cycle execution
  49. if(cycles == 0){
  50. while(status != 0){
  51. status = xcpu_execute(&c);
  52. if(status == 0){
  53. printf("MESSAGE: CPU has halted.\n");
  54. break;
  55. }
  56. }
  57. }
  58.  
  59. else{
  60. for(k = 1; k <= cycles; k++){
  61. status = xcpu_execute(&c);
  62. if(status == 0){
  63. printf("MESSAGE: CPU has halted.\n");
  64. break;
  65. }
  66. }
  67. if(status==1) printf("WARNING: CPU has timed-out.\n");
  68. }
  69. return 0;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement