cpu.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. import opcode
  2. from rvtypes import uint32, int32
  3. class cpu:
  4. def __init__(self,mem,arch='rv32i'):
  5. self.mem = mem
  6. self.ir = uint32(0)
  7. self.pc = uint32(0)
  8. self.registers = [uint32(0)] * 32
  9. self.isize = 0
  10. def handle_r(self):
  11. rd = opcode.get_rd(self.ir)
  12. rs1 = opcode.get_rs1(self.ir)
  13. rs2 = opcode.get_rs2(self.ir)
  14. fun3 = opcode.get_funct3(self.ir)
  15. fun7 = opcode.get_funct7(self.ir)
  16. if fun3 == 0x00:
  17. if fun7 == 0x00:
  18. self.registers[rd] = self.registers[rs1] + self.registers[rs2]
  19. elif fun7 == 0x20:
  20. self.registers[rd] = self.registers[rs1] - self.registers[rs2]
  21. else:
  22. raise Exception('unimplemented type r {} {}'.format(fun3, fun7))
  23. else:
  24. raise Exception('unimplemented type r {} {}'.format(fun3, fun7))
  25. def handle_i(self):
  26. rd = opcode.get_rd(self.ir)
  27. rs1 = opcode.get_rs1(self.ir)
  28. rs2 = opcode.get_rs2(self.ir)
  29. imm = opcode.get_imm12(self.ir)
  30. fun = opcode.get_funct3(self.ir)
  31. if fun == 0:
  32. self.registers[rd] = self.registers[rs1] + imm
  33. elif fun == 2:
  34. self.registers[rd] = self.registers[rs1] < imm
  35. else:
  36. raise Exception('unimplemented')
  37. def handle_s(self):
  38. rs1 = opcode.get_rs1(self.ir)
  39. rs2 = opcode.get_rs2(self.ir)
  40. imm = opcode.get_simm12(self.ir)
  41. fun = opcode.get_funct3(self.ir)
  42. wv = int(self.registers[rs2])
  43. addr = int(self.registers[rs1] + imm)
  44. self.mem.write(addr,wv)
  45. if fun > 0:
  46. self.mem.write(addr+1,wv>>8)
  47. if fun > 1:
  48. self.mem.write(addr+2,wv>>16)
  49. self.mem.write(addr+3,wv>>24)
  50. def handle_j(self):
  51. rd = opcode.get_rd(self.ir)
  52. rs1 = opcode.get_rs1(self.ir)
  53. rs2 = opcode.get_rs2(self.ir)
  54. fun = opcode.get_funct3(self.ir)
  55. imm20 = opcode.get_jimm20(self.ir)
  56. op = opcode.get_op(self.ir)
  57. if op == 0x6F:
  58. self.registers[rd] = uint32(self.pc)
  59. self.pc += imm20
  60. self.isize = 0
  61. else:
  62. print(hex(op))
  63. raise Exception('unimplemented')
  64. def handle_b(self):
  65. rd = opcode.get_rd(self.ir)
  66. rs1 = opcode.get_rs1(self.ir)
  67. rs2 = opcode.get_rs2(self.ir)
  68. fun = opcode.get_funct3(self.ir)
  69. imm20 = opcode.get_jimm20(self.ir)
  70. bimm12 = opcode.get_bimm12(self.ir)
  71. op = opcode.get_op(self.ir)
  72. if op == 0x63:
  73. if fun == 0x00:
  74. if self.registers[rs1] == self.registers[rs2]:
  75. self.pc += bimm12
  76. self.isize = 0
  77. elif fun == 0x01:
  78. if self.registers[rs1] != self.registers[rs2]:
  79. self.pc += bimm12
  80. self.isize = 0
  81. elif fun == 0x04:
  82. if int32(self.registers[rs1]) < int32(self.registers[rs2]):
  83. self.pc += bimm12
  84. self.isize = 0
  85. elif fun == 0x05:
  86. if int32(self.registers[rs1]) < int32(self.registers[rs2]):
  87. self.pc += bimm12
  88. self.isize = 0
  89. elif fun == 0x06:
  90. if self.registers[rs1] < self.registers[rs2]:
  91. self.pc += bimm12
  92. self.isize = 0
  93. elif fun == 0x07:
  94. if self.registers[rs1] >= self.registers[rs2]:
  95. self.pc += bimm12
  96. self.isize = 0
  97. else:
  98. raise Exception('unimplemented')
  99. else:
  100. raise Exception('unimplemented')
  101. def handle_u(self):
  102. rd = opcode.get_rd(self.ir)
  103. imm20 = opcode.get_jimm20(self.ir)
  104. op = opcode.get_op(self.ir)
  105. imm20 << 12
  106. if op == 0x37:
  107. self.registers[rd] = uint32(imm20)
  108. elif op == 0x17:
  109. self.registers[rd] = uint32(imm20 + self.pc)
  110. else:
  111. raise Exception('unimplemented')
  112. def handle_l(self):
  113. rd = opcode.get_rd(self.ir)
  114. rs1 = opcode.get_rs1(self.ir)
  115. imm = opcode.get_imm12(self.ir)
  116. fun = opcode.get_funct3(self.ir)
  117. addr = int(self.registers[rs1] + imm)
  118. op = opcode.get_op(self.ir)
  119. if op == 0x67: #jalr
  120. self.pc = int(addr)
  121. self.registers[rd] = uint32(self.pc)
  122. elif fun == 0x00 or fun == 0x04: #lb lbu
  123. rv = self.mem.read(addr)
  124. self.registers[rd] = uint32(rv)
  125. elif fun == 0x02:
  126. rv = self.mem.read(addr) + (self.mem.read(addr+1) << 8) + (self.mem.read(addr+2) << 16) + (self.mem.read(addr+3) << 24)
  127. self.registers[rd] = uint32(rv)
  128. else:
  129. raise Exception('unimplemented')
  130. def decompress(self):
  131. ir = 0
  132. cop = opcode.get_cop(self.ir)
  133. if self.opcode[1] == 'cr':
  134. f1 = opcode.get_funct1(self.ir)
  135. rs1 = opcode.get_crs1(self.ir)
  136. rs2 = opcode.get_crs2(self.ir)
  137. if f1 == 0:
  138. if rs2 == 0:
  139. ir = 0x00000067 | (rs2 << 7) | (rs1 << 15)
  140. else:
  141. ir = 0x00000013 | (rs1 << 7) | (rs2 << 15)
  142. if f1 == 1:
  143. pass
  144. return ir
  145. def execute(self):
  146. self.registers[0] = uint32(0)
  147. op = opcode.match_opcode(self.ir,'ic')
  148. if not op:
  149. return 1
  150. self.opcode = op
  151. print(op)
  152. if op[1][0] == 'c':
  153. self.ir = self.decompress()
  154. print(hex(self.ir))
  155. op = opcode.match_opcode(self.ir,'ic')
  156. if not op:
  157. return 1
  158. self.opcode = op
  159. print(op)
  160. if op[1] == 'r':
  161. self.handle_r()
  162. elif op[1] == 'i':
  163. self.handle_i()
  164. elif op[1] == 's':
  165. self.handle_s()
  166. elif op[1] == 'j':
  167. self.handle_j()
  168. elif op[1] == 'u':
  169. self.handle_u()
  170. elif op[1] == 'l':
  171. self.handle_l()
  172. elif op[1] == 'b':
  173. self.handle_b()
  174. else:
  175. return 1
  176. return 0
  177. def fetch(self):
  178. self.ir = self.mem.read(self.pc,mode='x')
  179. self.ir += self.mem.read(self.pc+1,mode='x') * 256
  180. self.isize = 2
  181. if self.ir & 0x03 == 0x03:
  182. self.ir += self.mem.read(self.pc+2,mode='x') * 65536
  183. self.ir += self.mem.read(self.pc+3,mode='x') * 16777216
  184. self.isize += 2
  185. def step(self):
  186. self.fetch()
  187. print(hex(self.ir))
  188. res = self.execute()
  189. self.pc += self.isize
  190. #print(hex(self.pc)+' '+repr(self.registers))
  191. return res