| 1 |
2 |
sergeykhbr |
/**
|
| 2 |
|
|
* @file
|
| 3 |
|
|
* @copyright Copyright 2016 GNSS Sensor Ltd. All right reserved.
|
| 4 |
|
|
* @author Sergey Khabarov - sergeykhbr@gmail.com
|
| 5 |
|
|
* @brief CPU Instruction Execution stage.
|
| 6 |
|
|
*/
|
| 7 |
|
|
|
| 8 |
|
|
#ifndef __DEBUGGER_RIVERLIB_EXECUTE_H__
|
| 9 |
|
|
#define __DEBUGGER_RIVERLIB_EXECUTE_H__
|
| 10 |
|
|
|
| 11 |
|
|
#include <systemc.h>
|
| 12 |
|
|
#include "../river_cfg.h"
|
| 13 |
|
|
#include "arith/int_div.h"
|
| 14 |
|
|
#include "arith/int_mul.h"
|
| 15 |
|
|
#include "arith/shift.h"
|
| 16 |
|
|
|
| 17 |
|
|
namespace debugger {
|
| 18 |
|
|
|
| 19 |
|
|
SC_MODULE(InstrExecute) {
|
| 20 |
|
|
sc_in<bool> i_clk;
|
| 21 |
|
|
sc_in<bool> i_nrst; // Reset active LOW
|
| 22 |
|
|
sc_in<bool> i_pipeline_hold; // Hold execution by any reason
|
| 23 |
|
|
sc_in<bool> i_d_valid; // Decoded instruction is valid
|
| 24 |
|
|
sc_in<sc_uint<BUS_ADDR_WIDTH>> i_d_pc; // Instruction pointer on decoded instruction
|
| 25 |
|
|
sc_in<sc_uint<32>> i_d_instr; // Decoded instruction value
|
| 26 |
|
|
sc_in<bool> i_wb_done; // write back done (Used to clear hazardness)
|
| 27 |
|
|
sc_in<bool> i_memop_store; // Store to memory operation
|
| 28 |
|
|
sc_in<bool> i_memop_load; // Load from memoru operation
|
| 29 |
|
|
sc_in<bool> i_memop_sign_ext; // Load memory value with sign extending
|
| 30 |
|
|
sc_in<sc_uint<2>> i_memop_size; // Memory transaction size
|
| 31 |
|
|
sc_in<bool> i_unsigned_op; // Unsigned operands
|
| 32 |
|
|
sc_in<bool> i_rv32; // 32-bits instruction
|
| 33 |
|
|
sc_in<sc_bv<ISA_Total>> i_isa_type; // Type of the instruction's structure (ISA spec.)
|
| 34 |
|
|
sc_in<sc_bv<Instr_Total>> i_ivec; // One pulse per supported instruction.
|
| 35 |
|
|
sc_in<bool> i_ie; // Interrupt enable bit
|
| 36 |
|
|
sc_in<sc_uint<BUS_ADDR_WIDTH>> i_mtvec; // Interrupt descriptor table
|
| 37 |
|
|
sc_in<sc_uint<2>> i_mode; // Current processor mode
|
| 38 |
|
|
sc_in<bool> i_break_mode; // Behaviour on EBREAK instruction: 0 = halt; 1 = generate trap
|
| 39 |
|
|
sc_in<bool> i_unsup_exception; // Unsupported instruction exception
|
| 40 |
|
|
sc_in<bool> i_ext_irq; // External interrupt from PLIC (todo: timer & software interrupts)
|
| 41 |
|
|
sc_in<bool> i_dport_npc_write; // Write npc value from debug port
|
| 42 |
|
|
sc_in<sc_uint<BUS_ADDR_WIDTH>> i_dport_npc; // Debug port npc value to write
|
| 43 |
|
|
|
| 44 |
|
|
sc_out<sc_uint<5>> o_radr1; // Integer register index 1
|
| 45 |
|
|
sc_in<sc_uint<RISCV_ARCH>> i_rdata1; // Integer register value 1
|
| 46 |
|
|
sc_out<sc_uint<5>> o_radr2; // Integer register index 2
|
| 47 |
|
|
sc_in<sc_uint<RISCV_ARCH>> i_rdata2; // Integer register value 2
|
| 48 |
|
|
sc_out<sc_uint<5>> o_res_addr; // Address to store result of the instruction (0=do not store)
|
| 49 |
|
|
sc_out<sc_uint<RISCV_ARCH>> o_res_data; // Value to store
|
| 50 |
|
|
sc_out<bool> o_pipeline_hold; // Hold pipeline while 'writeback' not done or multi-clock instruction.
|
| 51 |
|
|
sc_out<bool> o_xret; // XRET instruction: MRET, URET or other.
|
| 52 |
|
|
sc_out<sc_uint<12>> o_csr_addr; // CSR address. 0 if not a CSR instruction with xret signals mode switching
|
| 53 |
|
|
sc_out<bool> o_csr_wena; // Write new CSR value
|
| 54 |
|
|
sc_in<sc_uint<RISCV_ARCH>> i_csr_rdata; // CSR current value
|
| 55 |
|
|
sc_out<sc_uint<RISCV_ARCH>> o_csr_wdata; // CSR new value
|
| 56 |
|
|
sc_out<bool> o_trap_ena; // Trap occurs pulse
|
| 57 |
|
|
sc_out<sc_uint<5>> o_trap_code; // bit[4] : 1=interrupt; 0=exception; bits[3:0]=code
|
| 58 |
|
|
sc_out<sc_uint<BUS_ADDR_WIDTH>> o_trap_pc; // trap on pc
|
| 59 |
|
|
|
| 60 |
|
|
sc_out<bool> o_memop_sign_ext; // Load data with sign extending
|
| 61 |
|
|
sc_out<bool> o_memop_load; // Load data instruction
|
| 62 |
|
|
sc_out<bool> o_memop_store; // Store data instruction
|
| 63 |
|
|
sc_out<sc_uint<2>> o_memop_size; // 0=1bytes; 1=2bytes; 2=4bytes; 3=8bytes
|
| 64 |
|
|
sc_out<sc_uint<BUS_ADDR_WIDTH>> o_memop_addr;// Memory access address
|
| 65 |
|
|
|
| 66 |
|
|
sc_out<bool> o_valid; // Output is valid
|
| 67 |
|
|
sc_out<sc_uint<BUS_ADDR_WIDTH>> o_pc; // Valid instruction pointer
|
| 68 |
|
|
sc_out<sc_uint<BUS_ADDR_WIDTH>> o_npc; // Next instruction pointer. Next decoded pc must match to this value or will be ignored.
|
| 69 |
|
|
sc_out<sc_uint<32>> o_instr; // Valid instruction value
|
| 70 |
|
|
sc_out<bool> o_breakpoint; // ebreak instruction
|
| 71 |
|
|
sc_out<bool> o_call; // CALL pseudo instruction detected
|
| 72 |
|
|
sc_out<bool> o_ret; // RET pseudoinstruction detected
|
| 73 |
|
|
|
| 74 |
|
|
void comb();
|
| 75 |
|
|
void registers();
|
| 76 |
|
|
|
| 77 |
|
|
SC_HAS_PROCESS(InstrExecute);
|
| 78 |
|
|
|
| 79 |
|
|
InstrExecute(sc_module_name name_);
|
| 80 |
|
|
virtual ~InstrExecute();
|
| 81 |
|
|
|
| 82 |
|
|
void generateVCD(sc_trace_file *i_vcd, sc_trace_file *o_vcd);
|
| 83 |
|
|
|
| 84 |
|
|
private:
|
| 85 |
|
|
enum EMultiCycleInstruction {
|
| 86 |
|
|
Multi_MUL,
|
| 87 |
|
|
Multi_DIV,
|
| 88 |
|
|
Multi_Total
|
| 89 |
|
|
};
|
| 90 |
|
|
|
| 91 |
|
|
struct multi_arith_type {
|
| 92 |
|
|
sc_signal<sc_uint<RISCV_ARCH>> arr[Multi_Total];
|
| 93 |
|
|
};
|
| 94 |
|
|
|
| 95 |
|
|
struct RegistersType {
|
| 96 |
|
|
sc_signal<bool> d_valid; // Valid decoded instruction latch
|
| 97 |
|
|
sc_signal<sc_uint<BUS_ADDR_WIDTH>> pc;
|
| 98 |
|
|
sc_signal<sc_uint<BUS_ADDR_WIDTH>> npc;
|
| 99 |
|
|
sc_signal<sc_uint<32>> instr;
|
| 100 |
|
|
sc_uint<5> res_addr;
|
| 101 |
|
|
sc_signal<sc_uint<RISCV_ARCH>> res_val;
|
| 102 |
|
|
sc_signal<bool> memop_load;
|
| 103 |
|
|
sc_signal<bool> memop_store;
|
| 104 |
|
|
bool memop_sign_ext;
|
| 105 |
|
|
sc_uint<2> memop_size;
|
| 106 |
|
|
sc_signal<sc_uint<BUS_ADDR_WIDTH>> memop_addr;
|
| 107 |
|
|
|
| 108 |
|
|
sc_signal<sc_uint<5>> multi_res_addr; // latched output reg. address while multi-cycle instruction
|
| 109 |
|
|
sc_signal<sc_uint<BUS_ADDR_WIDTH>> multi_pc; // latched pc-value while multi-cycle instruction
|
| 110 |
|
|
sc_signal<sc_uint<BUS_ADDR_WIDTH>> multi_npc; // latched npc-value while multi-cycle instruction
|
| 111 |
|
|
sc_signal<sc_uint<32>> multi_instr; // Multi-cycle instruction is under processing
|
| 112 |
|
|
sc_signal<bool> multi_ena[Multi_Total]; // Enable pulse for Operation that takes more than 1 clock
|
| 113 |
|
|
sc_signal<bool> multi_rv32; // Long operation with 32-bits operands
|
| 114 |
|
|
sc_signal<bool> multi_unsigned; // Long operation with unsiged operands
|
| 115 |
|
|
sc_signal<bool> multi_residual_high; // Flag for Divider module: 0=divsion output; 1=residual output
|
| 116 |
|
|
// Flag for multiplier: 0=usual; 1=get high bits
|
| 117 |
|
|
sc_signal<bool> multiclock_ena;
|
| 118 |
|
|
sc_signal<sc_uint<RISCV_ARCH>> multi_a1; // Multi-cycle operand 1
|
| 119 |
|
|
sc_signal<sc_uint<RISCV_ARCH>> multi_a2; // Multi-cycle operand 2
|
| 120 |
|
|
|
| 121 |
|
|
sc_signal<sc_uint<5>> hazard_addr0; // Updated register address on previous step
|
| 122 |
|
|
sc_signal<sc_uint<5>> hazard_addr1; // Updated register address on pre-previous step
|
| 123 |
|
|
sc_signal<sc_uint<2>> hazard_depth; // Number of modificated registers that wasn't done yet
|
| 124 |
|
|
|
| 125 |
|
|
sc_signal<bool> ext_irq_pulser; // Form 1 clock pulse from strob
|
| 126 |
|
|
sc_signal<bool> trap_ena; // Trap occur, switch mode
|
| 127 |
|
|
sc_signal<bool> breakpoint;
|
| 128 |
|
|
sc_uint<5> trap_code_waiting; // To avoid multi-cycle instruction collision
|
| 129 |
|
|
sc_signal<sc_uint<5>> trap_code; // bit[4] : 1 = interrupt; 0 = exception
|
| 130 |
|
|
// bit[3:0] : trap code
|
| 131 |
|
|
sc_signal<sc_uint<BUS_ADDR_WIDTH>> trap_pc; // pc that caused a trap
|
| 132 |
|
|
sc_signal<bool> call;
|
| 133 |
|
|
sc_signal<bool> ret;
|
| 134 |
|
|
} v, r;
|
| 135 |
|
|
sc_signal<bool> w_hazard_detected;
|
| 136 |
|
|
multi_arith_type wb_arith_res;
|
| 137 |
|
|
sc_signal<bool> w_arith_valid[Multi_Total];
|
| 138 |
|
|
sc_signal<bool> w_arith_busy[Multi_Total];
|
| 139 |
|
|
bool w_interrupt;
|
| 140 |
|
|
bool w_exception;
|
| 141 |
|
|
bool w_exception_store;
|
| 142 |
|
|
bool w_exception_load;
|
| 143 |
|
|
bool w_exception_xret;
|
| 144 |
|
|
sc_uint<5> wb_exception_code;
|
| 145 |
|
|
|
| 146 |
|
|
sc_signal<sc_uint<RISCV_ARCH>> wb_shifter_a1; // Shifters operand 1
|
| 147 |
|
|
sc_signal<sc_uint<6>> wb_shifter_a2; // Shifters operand 2
|
| 148 |
|
|
sc_signal<sc_uint<RISCV_ARCH>> wb_sll;
|
| 149 |
|
|
sc_signal<sc_uint<RISCV_ARCH>> wb_sllw;
|
| 150 |
|
|
sc_signal<sc_uint<RISCV_ARCH>> wb_srl;
|
| 151 |
|
|
sc_signal<sc_uint<RISCV_ARCH>> wb_srlw;
|
| 152 |
|
|
sc_signal<sc_uint<RISCV_ARCH>> wb_sra;
|
| 153 |
|
|
sc_signal<sc_uint<RISCV_ARCH>> wb_sraw;
|
| 154 |
|
|
|
| 155 |
|
|
IntMul *mul0;
|
| 156 |
|
|
IntDiv *div0;
|
| 157 |
|
|
Shifter *sh0;
|
| 158 |
|
|
};
|
| 159 |
|
|
|
| 160 |
|
|
|
| 161 |
|
|
} // namespace debugger
|
| 162 |
|
|
|
| 163 |
|
|
#endif // __DEBUGGER_RIVERLIB_EXECUTE_H__
|