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.

131 lines
5.3 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. // /====================\
  8. // | Sum 1 to 9 Program |
  9. // \====================/
  10. //
  11. // Program to test RV32I
  12. // Add 1,2,3,...,9 (in that order).
  13. //
  14. // Regs:
  15. // x12 (a2): 10
  16. // x13 (a3): 1..10
  17. // x14 (a4): Sum
  18. //
  19. m4_asm(ADDI, x14, x0, 0) // Initialize sum register a4 with 0
  20. m4_asm(ADDI, x12, x0, 1010) // Store count of 10 in register a2.
  21. m4_asm(ADDI, x13, x0, 1) // Initialize loop count register a3 with 0
  22. // Loop:
  23. m4_asm(ADD, x14, x13, x14) // Incremental summation
  24. m4_asm(ADDI, x13, x13, 1) // Increment loop count by 1
  25. m4_asm(BLT, x13, x12, 1111111111000) // If a3 is less than a2, branch to label named <loop>
  26. m4_asm(ADDI, x0, x0, 1010) // Test for ignored write to reg 0
  27. // Test result value in x14, and set x30 to reflect pass/fail.
  28. m4_asm(ADDI, x30, x14, 111111010100) // Subtract expected value of 44 to set x30 to 1 if and only iff the result is 45 (1 + 2 + ... + 9).
  29. m4_asm(BGE, x0, x0, 0) // Done. Jump to itself (infinite loop). (Up to 20-bit signed immediate plus implicit 0 bit (unlike JALR) provides byte address; last immediate bit should also be 0)
  30. m4_asm_end()
  31. m4_define(['M4_MAX_CYC'], 50)
  32. //---------------------------------------------------------------------------------
  33. \SV
  34. m4_makerchip_module // (Expanded in Nav-TLV pane.)
  35. /* verilator lint_on WIDTH */
  36. \TLV
  37. $reset = *reset;
  38. // Program counter
  39. $next_pc[31:0] = $reset ? 32'b0 :
  40. $taken_br ? $br_tgt_br :
  41. $pc + 4;
  42. $pc[31:0] = >>1$next_pc;
  43. // Instruction memory
  44. `READONLY_MEM($pc, $$instr[31:0])
  45. // Decode
  46. // Decode instruction type
  47. $is_r_instr = $instr[6:2] == 5'b01011 ||
  48. $instr[6:2] == 5'b01100 ||
  49. $instr[6:2] == 5'b01110 ||
  50. $instr[6:2] == 5'b10100;
  51. $is_i_instr = $instr[6:2] ==? 5'b0000x ||
  52. $instr[6:2] ==? 5'b001x0 ||
  53. $instr[6:2] == 5'b11001;
  54. $is_s_instr = $instr[6:2] ==? 5'b0100x;
  55. $is_b_instr = $instr[6:2] == 5'b11000;
  56. $is_u_instr = $instr[6:2] ==? 5'b0x101;
  57. $is_j_instr = $instr[6:2] == 5'b11011;
  58. // Extract instruction fields
  59. $opcode[6:0] = $instr[6:0];
  60. $rd[4:0] = $instr[11:7];
  61. $funct3[2:0] = $instr[14:12];
  62. $rs1[4:0] = $instr[19:15];
  63. $rs2[4:0] = $instr[24:20];
  64. $funct7[6:0] = $instr[31:25];
  65. $imm[31:0] = $is_i_instr ? { {21{$instr[31]}}, $instr[30:20] } :
  66. $is_s_instr ? { {21{$instr[31]}}, $instr[30:25], $instr[11:7] } :
  67. $is_b_instr ? { {20{$instr[31]}}, $instr[7], $instr[30:25],
  68. $instr[11:8], 1'b0 } :
  69. $is_u_instr ? { $instr[31], $instr[30:12], 12'b0 } :
  70. $is_j_instr ? { {12{$instr[31]}}, $instr[19:12], $instr[20],
  71. $instr[30:21], 1'b0 } :
  72. 32'b0;
  73. // Calculate instruction fields valids
  74. $rd_valid = $is_r_instr || $is_i_instr || $is_u_instr || $is_j_instr;
  75. $funct3_valid = $is_r_instr || $is_i_instr || $is_s_instr || $is_b_instr;
  76. $rs1_valid = $funct3_valid;
  77. $rs2_valid = $is_r_instr || $is_s_instr || $is_b_instr;
  78. $funct7_valid = $is_r_instr;
  79. $imm_valid = !$is_r_instr;
  80. // Instruction code decoding
  81. $dec_bits[10:0] = { $funct7[5], $funct3, $opcode };
  82. $is_beq = $dec_bits ==? 11'bx_000_1100011;
  83. $is_bne = $dec_bits ==? 11'bx_001_1100011;
  84. $is_blt = $dec_bits ==? 11'bx_100_1100011;
  85. $is_bge = $dec_bits ==? 11'bx_101_1100011;
  86. $is_bltu = $dec_bits ==? 11'bx_110_1100011;
  87. $is_bgeu = $dec_bits ==? 11'bx_111_1100011;
  88. $is_addi = $dec_bits ==? 11'bx_000_0010011;
  89. $is_add = $dec_bits == 11'b0_000_0110011;
  90. // ALU
  91. $result[31:0] = $is_addi ? $src1_value + $imm :
  92. $is_add ? $src1_value + $src2_value :
  93. 32'b0;
  94. // Branch logic
  95. $taken_br = $is_beq ? $src1_value == $src2_value :
  96. $is_bne ? $src1_value != $src2_value :
  97. $is_blt ? ($src1_value < $src2_value) ^
  98. ($src1_value[31] != $src2_value[31]) :
  99. $is_bge ? ($src1_value >= $src2_value) ^
  100. ($src1_value[31] != $src2_value[31]) :
  101. $is_bltu ? $src1_value < $src2_value :
  102. $is_bgeu ? $src1_value >= $src2_value :
  103. 1'b0;
  104. $br_tgt_br[31:0] = $pc + $imm;
  105. // Assert these to end simulation (before Makerchip cycle limit).
  106. //*passed
  107. m4+tb();
  108. *failed = *cyc_cnt > M4_MAX_CYC;
  109. `BOGUS_USE($rd $rd_valid $rs1 $rs1_valid $rs2 $rs2_valid
  110. $funct3 $funct3_valid $funct7 $funct7_valid $imm_valid $imm)
  111. m4+rf(32, 32, $reset, $rd != 5'b00000 ? $rd_valid : 1'b0, $rd, $result, $rs1_valid, $rs1, $src1_value, $rs2_valid, $rs2, $src2_value)
  112. //m4+dmem(32, 32, $reset, $addr[4:0], $wr_en, $wr_data[31:0], $rd_en, $rd_data)
  113. m4+cpu_viz()
  114. \SV
  115. endmodule