Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. #include "utils.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. /* Sign extends the given field to a 32-bit integer where field is
  6. * interpreted an n-bit integer. */
  7. int sign_extend_number( unsigned int field, unsigned int n) {
  8. /* YOUR CODE HERE */
  9. if ((field >> (n - 1)) == 1) {
  10. return ((field << (32 - n) >> (32 - n)) | (0xFFFFFFFF << n));
  11. }
  12. return (field << (32 - n) >> (32 - n));
  13. }
  14.  
  15. /* Unpacks the 32-bit machine code instruction given into the correct
  16. * type within the instruction struct */
  17. Instruction parse_instruction(uint32_t instruction_bits) {
  18. /* YOUR CODE HERE */
  19. Instruction instruction;
  20. unsigned int opcode;
  21. opcode = instruction_bits << (32 - 7) >> (32 - 7);
  22. switch(opcode) {
  23. case 0x33:
  24. instruction.rtype.opcode = opcode;
  25. instruction.rtype.rd = instruction_bits >> 7 << 20 >> 20;
  26. instruction.rtype.funct3 = instruction_bits >> 12 << 17 >> 17;
  27. instruction.rtype.rs1 = instruction_bits >> 15 << 12 >> 12;
  28. instruction.rtype.rs2 = instruction_bits >> 20 << 7 >> 7;
  29. instruction.rtype.funct7 = instruction_bits >> 25;
  30. case 0x13:
  31. instruction.itype.opcode = opcode;
  32. instruction.itype.rd = instruction_bits >> 7 << 20 >> 20;
  33. instruction.itype.funct3 = instruction_bits >> 12 << 17 >> 17;
  34. instruction.itype.rs1 = instruction_bits >> 15 << 12 >> 12;
  35. instruction.itype.imm = instruction_bits >> 20;
  36. case 0x3:
  37. instruction.itype.opcode = opcode;
  38. instruction.itype.rd = instruction_bits >> 7 << 20 >> 20;
  39. instruction.itype.funct3 = instruction_bits >> 12 << 17 >> 17;
  40. instruction.itype.rs1 = instruction_bits >> 15 << 12 >> 12;
  41. instruction.itype.imm = instruction_bits >> 20;
  42. case 0x23:
  43. instruction.stype.opcode = opcode;
  44. instruction.stype.imm5 = instruction_bits >> 7 << 20 >> 20;
  45. instruction.stype.funct3 = instruction_bits >> 12 << 17 >> 17;
  46. instruction.stype.rs1 = instruction_bits >> 15 << 12 >> 12;
  47. instruction.stype.rs2 = instruction_bits >> 20 << 7 >> 7;
  48. instruction.stype.imm7 = instruction_bits >> 25;
  49. case 0x63:
  50. instruction.sbtype.opcode = opcode;
  51. instruction.sbtype.imm5 = instruction_bits >> 7 << 20 >> 20;
  52. instruction.sbtype.funct3 = instruction_bits >> 12 << 17 >> 17;
  53. instruction.sbtype.rs1 = instruction_bits >> 15 << 12 >> 12;
  54. instruction.sbtype.rs2 = instruction_bits >> 20 << 7 >> 7;
  55. instruction.sbtype.imm7 = instruction_bits >> 25;
  56. case 0x37:
  57. instruction.utype.opcode = opcode;
  58. instruction.utype.rd = instruction_bits >> 7 << 20 >> 20;
  59. instruction.utype.imm = instruction_bits >> 12;
  60. case 0x6F:
  61. instruction.ujtype.opcode = opcode;
  62. instruction.ujtype.rd = instruction_bits >> 7 << 20 >> 20;
  63. instruction.ujtype.imm = instruction_bits >> 12;
  64. case 0x73:
  65. instruction.itype.opcode = opcode;
  66. instruction.itype.rd = instruction_bits >> 7 << 20 >> 20;
  67. instruction.itype.funct3 = instruction_bits >> 12 << 17 >> 17;
  68. instruction.itype.rs1 = instruction_bits >> 15 << 12 >> 12;
  69. instruction.itype.imm = instruction_bits >> 20;
  70. default: // undefined opcode
  71. instruction.utype.opcode = opcode;
  72. instruction.utype.rd = instruction_bits >> 7 << 20 >> 20;
  73. instruction.utype.imm = instruction_bits >> 12;
  74. }
  75. return instruction;
  76. }
  77.  
  78. /* Return the number of bytes (from the current PC) to the branch label using the given
  79. * branch instruction */
  80. int get_branch_offset(Instruction instruction) {
  81. /* YOUR CODE HERE */
  82. unsigned int eleventh = instruction.sbtype.imm5 << 31 >> 31 << 11;
  83. unsigned int twelfth = instruction.sbtype.imm7 << 25 >> 31 << 12;
  84. unsigned int firsttofourth = instruction.sbtype.imm5 >> 1 << 1;
  85. unsigned int fifthtotenth = instruction.sbtype.imm7 << 26 >> 26 << 5;
  86. unsigned int imm = (twelfth | eleventh | fifthtotenth | firsttofourth);
  87. return imm;
  88. }
  89.  
  90. /* Returns the number of bytes (from the current PC) to the jump label using the given
  91. * jump instruction */
  92. int get_jump_offset(Instruction instruction) {
  93. /* YOUR CODE HERE */
  94. unsigned int twentieth = instruction.ujtype.imm << 12 >> 31 << 20;
  95. unsigned int twelfthtonineteenth = instruction.ujtype.imm << 24 >> 24 << 12;
  96. unsigned int eleventh = instruction.ujtype.imm << 23 >> 31 << 11;
  97. unsigned int firsttotenth = instruction.ujtype.imm >> 9 << 21 >> 21 << 1;
  98. unsigned int imm = (twentieth | twelfthtonineteenth | eleventh | firsttotenth);
  99. return imm;
  100. }
  101.  
  102. int get_store_offset(Instruction instruction) {
  103. /* YOUR CODE HERE */
  104. return sign_extend_number((instruction.stype.imm7 << 5) | instruction.stype.imm5, 12);
  105. }
  106.  
  107. void handle_invalid_instruction(Instruction instruction) {
  108. printf("Invalid Instruction: 0x%08x\n", instruction.bits);
  109. }
  110.  
  111. void handle_invalid_read(Address address) {
  112. printf("Bad Read. Address: 0x%08x\n", address);
  113. exit(-1);
  114. }
  115.  
  116. void handle_invalid_write(Address address) {
  117. printf("Bad Write. Address: 0x%08x\n", address);
  118. exit(-1);
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement