|
| 1 | +#include <fcntl.h> |
| 2 | +#include <cstdint> |
| 3 | +#include <csignal> |
| 4 | +#include <iostream> |
| 5 | +#include <fstream> |
| 6 | +#include <memory> |
| 7 | +#include <ctime> |
| 8 | +#include <string> |
| 9 | + |
| 10 | +#include "verilated_vcd_c.h" |
| 11 | +#include "Vdecoder_sim.h" |
| 12 | + |
| 13 | +void INThandler(int signal) { |
| 14 | + std::cout << "\nCaught ctrl-c\n"; |
| 15 | + std::exit(0); |
| 16 | +} |
| 17 | + |
| 18 | +void test_instruction(Vdecoder_sim *top, const std::string& instr_name, uint32_t instr); |
| 19 | +void cycle(Vdecoder_sim *top); |
| 20 | + |
| 21 | +int main(int argc, char** argv, char** env) { |
| 22 | + Verilated::commandArgs(argc, argv); |
| 23 | + |
| 24 | + Vdecoder_sim *top = new Vdecoder_sim; |
| 25 | + std::signal(SIGINT, INThandler); |
| 26 | + |
| 27 | + top->wb_clk = 0; |
| 28 | + top->wb_rdt = 0; |
| 29 | + top->wb_en = 0; |
| 30 | + |
| 31 | + std::cout << "Testing decoder\n"; |
| 32 | + test_instruction(top, "jalr", 0x0000006F); |
| 33 | + test_instruction(top, "mret", 0x30200073); |
| 34 | + test_instruction(top, "ebreak", 0x00100073); |
| 35 | + test_instruction(top, "wfi", 0x10500073); |
| 36 | + test_instruction(top, "add", 0x00000033); |
| 37 | + test_instruction(top, "addi", 0x00000013); |
| 38 | + test_instruction(top, "sub", 0x40000033); |
| 39 | + test_instruction(top, "and", 0x00007033); |
| 40 | + test_instruction(top, "andi", 0x00007013); |
| 41 | + test_instruction(top, "or", 0x00006033); |
| 42 | + test_instruction(top, "ori", 0x00006013); |
| 43 | + test_instruction(top, "xor", 0x00004033); |
| 44 | + test_instruction(top, "xori", 0x00004013); |
| 45 | + test_instruction(top, "sll", 0x00001033); |
| 46 | + test_instruction(top, "slli", 0x00001013); |
| 47 | + test_instruction(top, "srl", 0x00005033); |
| 48 | + test_instruction(top, "srli", 0x00005013); |
| 49 | + test_instruction(top, "sra", 0x40005033); |
| 50 | + test_instruction(top, "srai", 0x40005013); |
| 51 | + test_instruction(top, "lui", 0x00000037); |
| 52 | + test_instruction(top, "auipc", 0x00000017); |
| 53 | + test_instruction(top, "lw", 0x00002003); |
| 54 | + test_instruction(top, "lh", 0x00001003); |
| 55 | + test_instruction(top, "lhu", 0x00005003); |
| 56 | + test_instruction(top, "lb", 0x00000003); |
| 57 | + test_instruction(top, "lbu", 0x00004003); |
| 58 | + test_instruction(top, "sw", 0x00002023); |
| 59 | + test_instruction(top, "sh", 0x00001023); |
| 60 | + test_instruction(top, "sb", 0x00000023); |
| 61 | + test_instruction(top, "jal", 0x0000006F); |
| 62 | + test_instruction(top, "jalr", 0x00000067); |
| 63 | + test_instruction(top, "beq", 0x00000063); |
| 64 | + test_instruction(top, "bne", 0x00001063); |
| 65 | + test_instruction(top, "blt", 0x00004063); |
| 66 | + test_instruction(top, "bge", 0x00005063); |
| 67 | + test_instruction(top, "bltu", 0x00006063); |
| 68 | + test_instruction(top, "bgeu", 0x00007063); |
| 69 | + test_instruction(top, "slt", 0x00002033); |
| 70 | + test_instruction(top, "slti", 0x00002013); |
| 71 | + test_instruction(top, "sltu", 0x00003033); |
| 72 | + test_instruction(top, "sltiu", 0x00003013); |
| 73 | + test_instruction(top, "ecall", 0x00000073); |
| 74 | + test_instruction(top, "ebreak", 0x00100073); |
| 75 | + test_instruction(top, "fence", 0x0000000F); |
| 76 | + test_instruction(top, "fence.i", 0x0000100F); |
| 77 | + |
| 78 | + test_instruction(top, "csrrw", 0x00001073); |
| 79 | + test_instruction(top, "csrrs", 0x00002073); |
| 80 | + test_instruction(top, "csrrc", 0x00003073); |
| 81 | + test_instruction(top, "csrrwi", 0x00005073); |
| 82 | + test_instruction(top, "csrrsi", 0x00006073); |
| 83 | + test_instruction(top, "csrrci", 0x00007073); |
| 84 | + return 0; |
| 85 | +} |
| 86 | + |
| 87 | +void cycle(Vdecoder_sim *top) { |
| 88 | + top->eval(); |
| 89 | + top->wb_clk = 0; |
| 90 | + top->eval(); |
| 91 | + top->wb_clk = 1; |
| 92 | + top->eval(); |
| 93 | +} |
| 94 | + |
| 95 | +void test_instruction(Vdecoder_sim *top, const std::string& instr_name, uint32_t instr) { |
| 96 | + std::ofstream outfile(instr_name + ".txt"); |
| 97 | + |
| 98 | + std::cout << "Testing " << instr_name << "\n"; |
| 99 | + outfile << "Testing " << instr_name << "\n"; |
| 100 | + |
| 101 | + top->wb_rdt = instr >> 2; |
| 102 | + top->wb_en = 1; |
| 103 | + cycle(top); |
| 104 | + |
| 105 | + outfile << "jal_or_jalr: " << std::to_string(top->jal_or_jalr) << std::endl; |
| 106 | + outfile << "ebreak: " << std::to_string(top->ebreak) << std::endl; |
| 107 | + outfile << "mret: " << std::to_string(top->mret) << std::endl; |
| 108 | + outfile << "wfi: " << std::to_string(top->wfi) << std::endl; |
| 109 | + outfile << "sh_right: " << std::to_string(top->sh_right) << std::endl; |
| 110 | + outfile << "bne_or_bge: " << std::to_string(top->bne_or_bge) << std::endl; |
| 111 | + outfile << "cond_branch: " << std::to_string(top->cond_branch) << std::endl; |
| 112 | + outfile << "e_op: " << std::to_string(top->e_op) << std::endl; |
| 113 | + outfile << "branch_op: " << std::to_string(top->branch_op) << std::endl; |
| 114 | + outfile << "shift_op: " << std::to_string(top->shift_op) << std::endl; |
| 115 | + outfile << "slt_or_branch: " << std::to_string(top->slt_or_branch) << std::endl; |
| 116 | + outfile << "rd_op: " << std::to_string(top->rd_op) << std::endl; |
| 117 | + outfile << "two_stage_op: " << std::to_string(top->two_stage_op) << std::endl; |
| 118 | + outfile << "dbus_en: " << std::to_string(top->dbus_en) << std::endl; |
| 119 | + outfile << "mdu_op: " << std::to_string(top->mdu_op) << std::endl; |
| 120 | + outfile << "ext_funct3: " << std::to_string(top->ext_funct3) << std::endl; |
| 121 | + outfile << "bufreg_rs1_en: " << std::to_string(top->bufreg_rs1_en) << std::endl; |
| 122 | + outfile << "bufreg_imm_en: " << std::to_string(top->bufreg_imm_en) << std::endl; |
| 123 | + outfile << "bufreg_clr_lsb: " << std::to_string(top->bufreg_clr_lsb) << std::endl; |
| 124 | + outfile << "bufreg_sh_signed: " << std::to_string(top->bufreg_sh_signed) << std::endl; |
| 125 | + outfile << "ctrl_utype: " << std::to_string(top->ctrl_utype) << std::endl; |
| 126 | + outfile << "ctrl_pc_rel: " << std::to_string(top->ctrl_pc_rel) << std::endl; |
| 127 | + outfile << "alu_sub: " << std::to_string(top->alu_sub) << std::endl; |
| 128 | + outfile << "alu_bool_op: " << std::to_string(top->alu_bool_op) << std::endl; |
| 129 | + outfile << "alu_cmp_eq: " << std::to_string(top->alu_cmp_eq) << std::endl; |
| 130 | + outfile << "alu_cmp_sig: " << std::to_string(top->alu_cmp_sig) << std::endl; |
| 131 | + outfile << "alu_rd_sel: " << std::to_string(top->alu_rd_sel) << std::endl; |
| 132 | + outfile << "mem_signed: " << std::to_string(top->mem_signed) << std::endl; |
| 133 | + outfile << "mem_word: " << std::to_string(top->mem_word) << std::endl; |
| 134 | + outfile << "mem_half: " << std::to_string(top->mem_half) << std::endl; |
| 135 | + outfile << "mem_cmd: " << std::to_string(top->mem_cmd) << std::endl; |
| 136 | + outfile << "csr_en: " << std::to_string(top->csr_en) << std::endl; |
| 137 | + outfile << "csr_addr: " << std::to_string(top->csr_addr) << std::endl; |
| 138 | + outfile << "csr_mstatus_en: " << std::to_string(top->csr_mstatus_en) << std::endl; |
| 139 | + outfile << "csr_mie_en: " << std::to_string(top->csr_mie_en) << std::endl; |
| 140 | + outfile << "csr_mcause_en: " << std::to_string(top->csr_mcause_en) << std::endl; |
| 141 | + outfile << "csr_source: " << std::to_string(top->csr_source) << std::endl; |
| 142 | + outfile << "csr_d_sel: " << std::to_string(top->csr_d_sel) << std::endl; |
| 143 | + outfile << "csr_imm_en: " << std::to_string(top->csr_imm_en) << std::endl; |
| 144 | + outfile << "mtval_pc: " << std::to_string(top->mtval_pc) << std::endl; |
| 145 | + outfile << "immdec_ctrl: " << std::to_string(top->immdec_ctrl) << std::endl; |
| 146 | + outfile << "immdec_en: " << std::to_string(top->immdec_en) << std::endl; |
| 147 | + outfile << "op_b_source: " << std::to_string(top->op_b_source) << std::endl; |
| 148 | + outfile << "rd_mem_en: " << std::to_string(top->rd_mem_en) << std::endl; |
| 149 | + outfile << "rd_csr_en: " << std::to_string(top->rd_csr_en) << std::endl; |
| 150 | + outfile << "rd_alu_en: " << std::to_string(top->rd_alu_en) << std::endl; |
| 151 | + |
| 152 | + assert(!(top->wfi && instr_name != "wfi")); |
| 153 | + |
| 154 | + top->wb_en = 0; |
| 155 | + cycle(top); |
| 156 | + outfile.close(); |
| 157 | +} |
0 commit comments