You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
7.0 KiB

  1. \m4_TLV_version 1d: tl-x.org
  2. \SV
  3. // This code can be found in: https://github.com/stevehoover/LF-Building-a-RISC-V-CPU-Core/risc-v_shell.tlv
  4. m4_include_lib(['https://raw.githubusercontent.com/stevehoover/warp-v_includes/1d1023ccf8e7b0a8cf8e8fc4f0a823ebb61008e3/risc-v_defs.tlv'])
  5. m4_include_lib(['https://raw.githubusercontent.com/stevehoover/LF-Building-a-RISC-V-CPU-Core/main/lib/risc-v_shell_lib.tlv'])
  6. //---------------------------------------------------------------------------------
  7. m4_test_prog()
  8. //---------------------------------------------------------------------------------
  9. \SV
  10. m4_makerchip_module // (Expanded in Nav-TLV pane.)
  11. /* verilator lint_on WIDTH */
  12. \TLV
  13. $reset = *reset;
  14. // Program counter
  15. $next_pc[31:0] = $reset ? 32'b0 :
  16. $taken_br || $is_jal ? $br_tgt_br :
  17. $is_jalr ? $jalr_tgt_pc :
  18. $pc + 4;
  19. $pc[31:0] = >>1$next_pc;
  20. // Instruction memory
  21. `READONLY_MEM($pc, $$instr[31:0])
  22. // Decode
  23. // Decode instruction type
  24. $is_r_instr = $instr[6:2] == 5'b01011 ||
  25. $instr[6:2] == 5'b01100 ||
  26. $instr[6:2] == 5'b01110 ||
  27. $instr[6:2] == 5'b10100;
  28. $is_i_instr = $instr[6:2] ==? 5'b0000x ||
  29. $instr[6:2] ==? 5'b001x0 ||
  30. $instr[6:2] == 5'b11001;
  31. $is_s_instr = $instr[6:2] ==? 5'b0100x;
  32. $is_b_instr = $instr[6:2] == 5'b11000;
  33. $is_u_instr = $instr[6:2] ==? 5'b0x101;
  34. $is_j_instr = $instr[6:2] == 5'b11011;
  35. // Extract instruction fields
  36. $opcode[6:0] = $instr[6:0];
  37. $rd[4:0] = $instr[11:7];
  38. $funct3[2:0] = $instr[14:12];
  39. $rs1[4:0] = $instr[19:15];
  40. $rs2[4:0] = $instr[24:20];
  41. $funct7[6:0] = $instr[31:25];
  42. $imm[31:0] = $is_i_instr ? { {21{$instr[31]}}, $instr[30:20] } :
  43. $is_s_instr ? { {21{$instr[31]}}, $instr[30:25], $instr[11:7] } :
  44. $is_b_instr ? { {20{$instr[31]}}, $instr[7], $instr[30:25],
  45. $instr[11:8], 1'b0 } :
  46. $is_u_instr ? { $instr[31], $instr[30:12], 12'b0 } :
  47. $is_j_instr ? { {12{$instr[31]}}, $instr[19:12], $instr[20],
  48. $instr[30:21], 1'b0 } :
  49. 32'b0;
  50. // Calculate instruction fields valids
  51. $rd_valid = $is_r_instr || $is_i_instr || $is_u_instr || $is_j_instr;
  52. $funct3_valid = $is_r_instr || $is_i_instr || $is_s_instr || $is_b_instr;
  53. $rs1_valid = $funct3_valid;
  54. $rs2_valid = $is_r_instr || $is_s_instr || $is_b_instr;
  55. $funct7_valid = $is_r_instr;
  56. $imm_valid = !$is_r_instr;
  57. // Instruction code decoding
  58. $dec_bits[10:0] = { $funct7[5], $funct3, $opcode };
  59. $is_beq = $dec_bits ==? 11'bx_000_1100011;
  60. $is_bne = $dec_bits ==? 11'bx_001_1100011;
  61. $is_blt = $dec_bits ==? 11'bx_100_1100011;
  62. $is_bge = $dec_bits ==? 11'bx_101_1100011;
  63. $is_bltu = $dec_bits ==? 11'bx_110_1100011;
  64. $is_bgeu = $dec_bits ==? 11'bx_111_1100011;
  65. $is_addi = $dec_bits ==? 11'bx_000_0010011;
  66. $is_add = $dec_bits == 11'b0_000_0110011;
  67. $is_lui = $dec_bits ==? 11'bx_xxx_0110111;
  68. $is_auipc = $dec_bits ==? 11'bx_xxx_0010111;
  69. $is_jal = $dec_bits ==? 11'bx_xxx_1101111;
  70. $is_jalr = $dec_bits ==? 11'bx_000_1100111;
  71. $is_slti = $dec_bits ==? 11'bx_010_0010011;
  72. $is_sltiu = $dec_bits ==? 11'bx_011_0010011;
  73. $is_xori = $dec_bits ==? 11'bx_100_0010011;
  74. $is_ori = $dec_bits ==? 11'bx_110_0010011;
  75. $is_andi = $dec_bits ==? 11'bx_111_0010011;
  76. $is_slli = $dec_bits ==? 11'b0_001_0010011;
  77. $is_srli = $dec_bits ==? 11'b0_101_0010011;
  78. $is_srai = $dec_bits ==? 11'b1_101_0010011;
  79. $is_sub = $dec_bits ==? 11'b1_000_0110011;
  80. $is_sll = $dec_bits ==? 11'b0_001_0110011;
  81. $is_slt = $dec_bits ==? 11'b0_010_0110011;
  82. $is_sltu = $dec_bits ==? 11'b0_011_0110011;
  83. $is_xor = $dec_bits ==? 11'b0_100_0110011;
  84. $is_srl = $dec_bits ==? 11'b0_101_0110011;
  85. $is_sra = $dec_bits ==? 11'b1_101_0110011;
  86. $is_or = $dec_bits ==? 11'b0_110_0110011;
  87. $is_and = $dec_bits ==? 11'b0_111_0110011;
  88. // LB, LH, LW, LBU, LHU
  89. $is_load = $opcode == 7'b0000011;
  90. // SB, SH, SW
  91. $is_store = $is_s_instr;
  92. // ALU
  93. // Some subexpressions
  94. // SLTU & SLTI (set if less than, unsigned)
  95. $sltu_rslt[31:0] = {31'b0, $src1_value < $src2_value};
  96. $sltiu_rslt[31:0] = {31'b0, $src1_value < $imm};
  97. // SRA & SRAI (shift right, arithmetic)
  98. // sign-extended src1
  99. $sext_src1[63:0] = { {32{$src1_value[31]}}, $src1_value };
  100. // 64-bit sign-extended result
  101. $sra_rslt[63:0] = $sext_src1 >> $src2_value[4:0];
  102. $srai_rslt[63:0] = $sext_src1 >> $imm[4:0];
  103. // ALU
  104. $result[31:0] = $is_andi ? $src1_value & $imm :
  105. $is_ori ? $src1_value | $imm :
  106. $is_xori ? $src1_value ^ $imm :
  107. $is_addi | $is_load | $is_store ? $src1_value + $imm :
  108. $is_slli ? $src1_value << $imm[5:0] :
  109. $is_srli ? $src1_value >> $imm[5:0] :
  110. $is_and ? $src1_value & $src2_value :
  111. $is_or ? $src1_value | $src2_value :
  112. $is_xor ? $src1_value ^ $src2_value :
  113. $is_add ? $src1_value + $src2_value :
  114. $is_sub ? $src1_value - $src2_value :
  115. $is_sll ? $src1_value << $src2_value[4:0] :
  116. $is_srl ? $src1_value >> $src2_value[4:0] :
  117. $is_sltu ? $sltu_rslt :
  118. $is_sltiu ? $sltiu_rslt :
  119. $is_lui ? {$imm[31:12], 12'b0} :
  120. $is_auipc ? $pc + $imm :
  121. $is_jal ? $pc + 4 :
  122. $is_jalr ? $pc + 4 :
  123. $is_slt ? (($src1_value[31] == $src2_value[31]) ?
  124. $sltu_rslt :
  125. {31'b0, $src1_value[31]}) :
  126. $is_slti ? (($src1_value[31] == $imm[31]) ?
  127. $sltiu_rslt :
  128. {31'b0, $src1_value[31]}) :
  129. $is_sra ? $sra_rslt[31:0] :
  130. $is_srai ? $srai_rslt[31:0] :
  131. 32'b0;
  132. // Branch logic
  133. $taken_br = $is_beq ? $src1_value == $src2_value :
  134. $is_bne ? $src1_value != $src2_value :
  135. $is_blt ? ($src1_value < $src2_value) ^
  136. ($src1_value[31] != $src2_value[31]) :
  137. $is_bge ? ($src1_value >= $src2_value) ^
  138. ($src1_value[31] != $src2_value[31]) :
  139. $is_bltu ? $src1_value < $src2_value :
  140. $is_bgeu ? $src1_value >= $src2_value :
  141. 1'b0;
  142. $br_tgt_br[31:0] = $pc + $imm;
  143. $jalr_tgt_pc[31:0] = $src1_value + $imm;
  144. // Assert these to end simulation (before Makerchip cycle limit).
  145. m4+tb();
  146. *failed = *cyc_cnt > M4_MAX_CYC;
  147. m4+rf(32, 32, $reset, $rd != 5'b00000 ? $rd_valid : 1'b0, $rd, $is_load ? $ld_data : $result, $rs1_valid, $rs1, $src1_value, $rs2_valid, $rs2, $src2_value)
  148. m4+dmem(32, 32, $reset, $result[6:2], $is_store, $src2_value, $is_load, $ld_data)
  149. m4+cpu_viz()
  150. \SV
  151. endmodule