LF-Building-a-RISC-V-CPU-Core/risc-v_shell.tlv

131 lines
4.8 KiB
Plaintext
Raw Normal View History

2021-02-08 15:40:43 +00:00
\m4_TLV_version 1d: tl-x.org
\SV
2021-02-08 18:53:03 +00:00
// This code can be found in: https://github.com/stevehoover/LF-Building-a-RISC-V-CPU-Core/risc-v_shell.tlv
2021-02-08 15:40:43 +00:00
m4_include_lib(['https://raw.githubusercontent.com/stevehoover/warp-v_includes/1d1023ccf8e7b0a8cf8e8fc4f0a823ebb61008e3/risc-v_defs.tlv'])
2021-02-08 19:45:26 +00:00
m4_include_lib(['https://raw.githubusercontent.com/stevehoover/LF-Building-a-RISC-V-CPU-Core/main/lib/risc-v_shell_lib.tlv'])
2021-02-08 15:40:43 +00:00
2021-02-16 02:49:35 +00:00
//---------------------------------------------------------------------------------
2021-02-08 15:40:43 +00:00
// /====================\
// | Sum 1 to 9 Program |
// \====================/
//
2021-02-16 02:49:35 +00:00
// Program to test RV32I
2021-02-08 15:40:43 +00:00
// Add 1,2,3,...,9 (in that order).
//
// Regs:
// x12 (a2): 10
// x13 (a3): 1..10
// x14 (a4): Sum
2021-02-08 15:40:43 +00:00
//
m4_asm(ADDI, x14, x0, 0) // Initialize sum register a4 with 0
m4_asm(ADDI, x12, x0, 1010) // Store count of 10 in register a2.
m4_asm(ADDI, x13, x0, 1) // Initialize loop count register a3 with 0
2021-02-08 15:40:43 +00:00
// Loop:
m4_asm(ADD, x14, x13, x14) // Incremental summation
m4_asm(ADDI, x13, x13, 1) // Increment loop count by 1
m4_asm(BLT, x13, x12, 1111111111000) // If a3 is less than a2, branch to label named <loop>
// Test result value in x14, and set x31 to reflect pass/fail.
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).
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)
2021-02-16 02:55:36 +00:00
m4_asm_end()
2021-02-16 02:49:35 +00:00
m4_define(['M4_MAX_CYC'], 50)
//---------------------------------------------------------------------------------
2021-02-12 02:18:16 +00:00
2021-02-08 15:40:43 +00:00
2021-02-16 02:49:35 +00:00
\SV
m4_makerchip_module // (Expanded in Nav-TLV pane.)
2021-02-27 22:03:26 +00:00
/* verilator lint_on WIDTH */
2021-02-16 02:49:35 +00:00
\TLV
2022-01-10 21:50:30 +00:00
2021-02-09 13:56:03 +00:00
$reset = *reset;
2022-01-10 21:50:30 +00:00
2022-01-10 20:04:05 +00:00
// Program counter
2022-01-11 19:44:11 +00:00
$next_pc[31:0] = $reset ? 0 :
$taken_br ? $br_tgt_pc :
($pc + 4);
2022-01-10 19:01:15 +00:00
$pc[31:0] = >>1$next_pc;
2022-01-10 19:19:32 +00:00
2022-01-10 20:04:05 +00:00
// Instruction memory
2022-01-10 19:19:32 +00:00
`READONLY_MEM($pc, $$instr[31:0]);
2022-01-10 21:50:30 +00:00
2022-01-10 20:04:05 +00:00
// Decode instruction types
$is_u_instr = $instr[6:2] ==? 5'b0x101;
$is_i_instr = $instr[6:2] ==? 5'b0000x
|| $instr[6:2] ==? 5'b001x0
|| $instr[6:2] == 5'b11001;
$is_r_instr = $instr[6:2] ==? 5'b011x0
|| $instr[6:2] == 5'b01011
2022-01-10 20:06:15 +00:00
|| $instr[6:2] == 5'b10100;
2022-01-10 20:04:05 +00:00
$is_s_instr = $instr[6:2] ==? 5'b0100x;
$is_b_instr = $instr[6:2] == 5'b11000;
$is_j_instr = $instr[6:2] == 5'b11011;
2022-01-10 21:50:30 +00:00
2022-01-10 21:28:03 +00:00
// Extract instruction fields
$func3[2:0] = $instr[14:12];
$rs2[4:0] = $instr[24:20];
$rs1[4:0] = $instr[19:15];
$rd[4:0] = $instr[11:7];
$opcode[6:0] = $instr[6:0];
2022-01-10 21:50:30 +00:00
2022-01-10 21:28:03 +00:00
$func3_valid = $is_r_instr || $is_i_instr || $is_s_instr || $is_b_instr;
$rs2_valid = $is_r_instr || $is_s_instr || $is_b_instr;
$rs1_valid = $is_r_instr || $is_i_instr || $is_s_instr || $is_b_instr;
2022-01-11 19:20:03 +00:00
$rd_valid = $rd == 0 ? 0 :
$is_r_instr || $is_i_instr || $is_u_instr || $is_j_instr;
2022-01-10 21:28:03 +00:00
$imm_valid = $is_i_instr || $is_s_instr || $is_b_instr || $is_u_instr || $is_j_instr;
2022-01-10 21:50:30 +00:00
2022-01-11 19:46:04 +00:00
`BOGUS_USE($func3_valid $imm_valid)
2022-01-10 21:50:30 +00:00
2022-01-10 21:28:03 +00:00
$imm[31:0] = $is_i_instr ? { {21{$instr[31]}}, $instr[30:20] } :
$is_s_instr ? { {21{$instr[31]}}, $instr[30:25], $instr[11:7] } :
$is_b_instr ? { {20{$instr[31]}}, $instr[7], $instr[30:25], $instr[11:8], 1'b0 } :
$is_u_instr ? { $instr[31], $instr[30:12], 12'b0 } :
$is_j_instr ? { {12{$instr[31]}}, $instr[19:12], $instr[20], $instr[30:21], 1'b0 } :
32'b0;
2022-01-10 21:50:30 +00:00
// Decode instructions
$dec_bits[10:0] = {$instr[30], $func3, $opcode};
$is_beq = $dec_bits ==? 11'bx_000_110_0011;
$is_bne = $dec_bits ==? 11'bx_001_110_0011;
$is_blt = $dec_bits ==? 11'bx_100_110_0011;
$is_bge = $dec_bits ==? 11'bx_101_110_0011;
$is_bltu = $dec_bits ==? 11'bx_110_110_0011;
$is_bgeu = $dec_bits ==? 11'bx_111_110_0011;
$is_addi = $dec_bits ==? 11'bx_000_001_0011;
$is_add = $dec_bits == 11'b0_000_011_0011;
2022-01-11 19:44:11 +00:00
// Compute whether to branch
$taken_br = $is_beq ? $src1_value == $src2_value :
$is_bne ? $src1_value != $src2_value :
$is_blt ? ($src1_value < $src2_value) ^ ($src1_value[31] != $src2_value[31]) :
$is_bge ? ($src1_value >= $src2_value) ^ ($src1_value[31] != $src2_value[31]) :
$is_bltu ? $src1_value < $src2_value :
$is_bgeu ? $src1_value >= $src2_value :
0;
// Branch target
$br_tgt_pc[31:0] = $pc + $imm;
// Arithmetic Logic Unit
$result[31:0] =
$is_addi ? $src1_value + $imm :
$is_add ? $src1_value + $src2_value :
32'b0;
2022-01-11 19:50:47 +00:00
// Assert these to end simulation (before Makerchip cycle limit).
m4+tb()
*failed = *cyc_cnt > M4_MAX_CYC;
2022-01-11 18:44:57 +00:00
// Register file
2022-01-11 19:18:10 +00:00
m4+rf(32, 32, $reset, $rd_valid, $rd[4:0], $result, $rs1_valid, $rs1[4:0], $src1_value, $rs2_valid, $rs2[4:0], $src2_value)
//m4+dmem(32, 32, $reset, $addr[4:0], $wr_en, $wr_data[31:0], $rd_en, $rd_data)
2021-02-12 02:18:16 +00:00
m4+cpu_viz()
2021-02-08 15:40:43 +00:00
\SV
2022-01-10 20:04:05 +00:00
endmodule