fork download
  1. // ECE 201 MP4
  2. // Stage 1
  3. // Need to be translated to MIPS assembly language and run on SPIM
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #define MAXMEM 13
  7. // variables in the simulator program that simulate features of the
  8. // MIPS architecture. Here, r[] and m[] are just arrays in the memory
  9. long r[32];
  10. long ir,pc,a,b,aluout;
  11. long m[MAXMEM] = {0x1284820, 0x1495020, 0x22A9020, 0x1284820, 0x1495020, 0x24A9820, 0x1284820, 0x1495020, 0x26AA020, 0x1284820, 0x1495020, 0x28AA820, 0x1000FFFF};
  12. // This function simulates the subset of R-type instructions
  13. // all other possible MIPS func codes return 0, rather than
  14. // the correct result
  15. long alufunc(long a, long b, int func)
  16. {
  17. long ia,ib;
  18. if (func == 32) //add
  19. return (a + b);
  20. else if (func == 34) //sub
  21. return (a - b);
  22. else if (func == 36) //and
  23. return (a & b);
  24. else if (func == 37) //or
  25. return (a | b);
  26. else if (func == 39) //nor
  27. return (~(a|b));
  28. else if (func == 42) //slt
  29. return ((a<b) ? 1 : 0);
  30. else
  31. return(0);
  32. }
  33. // This function extracts a subset of bits from the middle of a
  34. // 32-bit word. It is assumed 31 >= left >= right >= 0
  35. // For example, extract(0x12345678,23,16) returns 0x34
  36. long extract(long x, int left, int right)
  37. {
  38. unsigned long mask,result;
  39. mask = 1;
  40. if (left==31)
  41. mask = 0xFFFFFFFF;
  42. else
  43. mask = (mask << (left+1)) - 1;
  44. result = (x&mask);
  45. if (right!=0)
  46. result = result >> right;
  47. return (result);
  48. }
  49. //Print one hexadecimal char '0' .. '9' or 'A' .. 'F'
  50. void hexdig(long dig)
  51. {
  52. if (dig < 10)
  53. printf("%c",dig+'0');
  54. else
  55. printf("%c",dig+'A'-10);
  56. }
  57. // Print 32-bit value as hexadecimal
  58. // There is quite a bit that has to be done here to
  59. // translate to MIPS assembly language. The value needs to
  60. // be broken into 8 ASCII chars by calls to extract and hexdig
  61. void printhex(long x)
  62. {
  63. hexdig(extract(x,31,28));
  64. hexdig(extract(x,27,24));
  65. hexdig(extract(x,23,20));
  66. hexdig(extract(x,19,16));
  67. hexdig(extract(x,15,12));
  68. hexdig(extract(x,11,8));
  69. hexdig(extract(x,7,4));
  70. hexdig(extract(x,3,0));
  71. }
  72. // Print registers of interest in childish division program
  73. void traceout()
  74. {
  75. printf("pc="); //print a string in memory, e.g. syscall 4
  76. printhex(pc);
  77. printf(" ir=");
  78. printhex(ir);
  79. printf(" a=");
  80. printhex(a);
  81. printf(" b=");
  82. printhex(b);
  83. printf(" aluout=");
  84. printhex(aluout);
  85. printf("\n");
  86. printf(" v0=");
  87. printhex(r[2]);
  88. printf(" a0=");
  89. printhex(r[4]);
  90. printf(" a1=");
  91. printhex(r[5]);
  92. printf(" t1=");
  93. printhex(r[9]);
  94. printf(" t2=");
  95. printhex(r[10]);
  96. printf(" t4=");
  97. printhex(r[12]);
  98. printf("\n");
  99. printf(" s1=");
  100. printhex(r[17]);
  101. printf(" s2=");
  102. printhex(r[18]);
  103. printf(" s3=");
  104. printhex(r[19]);
  105. printf(" s4=");
  106. printhex(r[20]);
  107. printf(" s5=");
  108. printhex(r[21]);
  109. printf("\n");
  110. // getchar();
  111. }
  112. int main()
  113. {
  114. int opcode; //convenient to use local since compared multiple times
  115. r[8] = 6; //t0 = 6 as needed by Babbage
  116. r[10] = 1; //t2 = 1 as needed by Babbage
  117. r[17] = 1; //s1 = 1 as needed by Babbage
  118. pc = 0;
  119. ir = 0;
  120. while(ir != 0x1000ffff) //would be infinite loop in hardware
  121. {
  122. ir = m[pc];
  123. pc = pc + 4;
  124. a = r[extract(ir,25,21)];
  125. b = r[extract(ir,20,16)];
  126. traceout();
  127. opcode = extract(ir,31,26);
  128. if (opcode==0)
  129. { //r-type
  130. aluout = alufunc(a,b,extract(ir,10,0));
  131. if (extract(ir,15,11) != 0) //not in hardware--prevent losing 0 in R0
  132. {
  133. r[extract(ir,15,11)] = aluout;
  134. }
  135. }
  136. }
  137. }
Success #stdin #stdout 0s 5304KB
stdin
add $s0, $t2, $s0
stdout
pc=00000004 ir=01284820 a=00000000 b=00000006 aluout=00000000
 v0=00000000 a0=00000000 a1=00000000 t1=00000000 t2=00000001 t4=00000000
 s1=00000001 s2=00000000 s3=00000000 s4=00000000 s5=00000000
pc=00000008 ir=01495020 a=00000001 b=00000006 aluout=00000006
 v0=00000000 a0=00000000 a1=00000000 t1=00000006 t2=00000001 t4=00000000
 s1=00000001 s2=00000000 s3=00000000 s4=00000000 s5=00000000
pc=0000000C ir=026AA020 a=00000000 b=00000007 aluout=00000007
 v0=00000000 a0=00000000 a1=00000000 t1=00000006 t2=00000007 t4=00000000
 s1=00000001 s2=00000000 s3=00000000 s4=00000000 s5=00000000
pc=00000010 ir=1000FFFF a=00000000 b=00000000 aluout=00000007
 v0=00000000 a0=00000000 a1=00000000 t1=00000006 t2=00000007 t4=00000000
 s1=00000001 s2=00000000 s3=00000000 s4=00000007 s5=00000000