1717// /////////////////////////////////////////////////////////////////////////////
1818
1919module r5p_mouse_trace
20- import riscv_isa_pkg:: * ;
21- import riscv_isa_i_pkg :: * ;
22- import tcb_pkg :: * ;
20+ import tcb_pkg:: * ;
2321# (
24- // trace file name
25- string FILE = " "
22+ // trace file name
23+ string FILE = " "
2624)(
27- // instruction execution phase
28- input logic [3 - 1 : 0 ] pha,
29- // TCB system bus
30- tcb_if.mon tcb
25+ // instruction execution phase
26+ input logic [3 - 1 : 0 ] pha,
27+ // TCB system bus
28+ tcb_if.mon tcb
3129);
3230
31+ import riscv_isa_pkg :: * ;
32+ import riscv_isa_i_pkg :: * ;
33+ import riscv_asm_pkg :: * ;
34+ import trace_spike_pkg;
35+
3336// //////////////////////////////////////////////////////////////////////////////
34- // local parameters and signals
37+ // local parameters
3538// //////////////////////////////////////////////////////////////////////////////
3639
37- import riscv_asm_pkg :: * ;
38- import tcb_pkg :: * ;
39-
40- // TODO: try to share this table with RTL, while keeping Verilog2005 compatibility ?
41- // FSM phases (GPR access phases can be decoded from a single bit)
42- localparam logic [3 - 1 : 0 ] IF = 3'b000 ; // instruction fetch
43- localparam logic [3 - 1 : 0 ] RS1 = 3'b101 ; // read register source 1
44- localparam logic [3 - 1 : 0 ] RS2 = 3'b110 ; // read register source 1
45- localparam logic [3 - 1 : 0 ] MLD = 3'b001 ; // memory load
46- localparam logic [3 - 1 : 0 ] MST = 3'b010 ; // memory store
47- localparam logic [3 - 1 : 0 ] EXE = 3'b011 ; // execute (only used to evaluate branching condition)
48- localparam logic [3 - 1 : 0 ] WB = 3'b100 ; // GPR write-back
40+ // TODO: try to share this table with RTL, while keeping Verilog2005 compatibility ?
41+ // FSM phases (GPR access phases can be decoded from a single bit)
42+ localparam logic [3 - 1 : 0 ] IF = 3'b000 ; // instruction fetch
43+ localparam logic [3 - 1 : 0 ] RS1 = 3'b101 ; // read register source 1
44+ localparam logic [3 - 1 : 0 ] RS2 = 3'b110 ; // read register source 1
45+ localparam logic [3 - 1 : 0 ] MLD = 3'b001 ; // memory load
46+ localparam logic [3 - 1 : 0 ] MST = 3'b010 ; // memory store
47+ localparam logic [3 - 1 : 0 ] EXE = 3'b011 ; // execute (only used to evaluate branching condition)
48+ localparam logic [3 - 1 : 0 ] WB = 3'b100 ; // GPR write-back
4949
50- // trace file name and descriptor
51- string fn; // file name
52- int fd;
50+ // //////////////////////////////////////////////////////////////////////////////
51+ // local signals
52+ // //////////////////////////////////////////////////////////////////////////////
5353
54- // print-out delay queue
55- string str_if [$]; // instruction fetch
56- string str_wb [$]; // GPR write-back
57- string str_ld [$]; // load
58- string str_st [$]; // store
54+ // IFU
55+ logic ifu_vld; // valid
56+ logic [XLEN - 1 : 0 ] ifu_adr; // PC (IFU address)
57+ logic [XLEN - 1 : 0 ] ifu_ins; // instruction
58+ logic ifu_ill; // instruction is illegal
59+ // WBU (write back to destination register)
60+ logic wbu_vld; // valid
61+ logic [ 5 - 1 : 0 ] wbu_idx; // index of destination register
62+ logic [XLEN - 1 : 0 ] wbu_dat; // data
63+ // LSU
64+ logic lsu_vld; // valid
65+ logic lsu_wen; // enable
66+ logic [ 5 - 1 : 0 ] lsu_idx; // index of data source register
67+ logic [XLEN - 1 : 0 ] lsu_adr; // PC (IFU address)
68+ logic [XLEN - 1 : 0 ] lsu_siz; // load/store size
69+ logic [XLEN - 1 : 0 ] lsu_wdt; // write data (store)
70+ logic [XLEN - 1 : 0 ] lsu_rdt; // read data (load)
5971
6072// //////////////////////////////////////////////////////////////////////////////
61- // tracing (matching spike simulator logs)
73+ // tracing
6274// //////////////////////////////////////////////////////////////////////////////
6375
64- // format GPR string with desired whitespace
65- function string format_gpr (logic [5 - 1 : 0 ] idx);
66- if (idx < 10 ) return ($sformatf (" x%0d " , idx));
67- else return ($sformatf (" x%0d " , idx));
68- endfunction : format_gpr
76+ initial begin
77+ ifu_vld = 1'b0 ;
78+ end
6979
70- // prepare string for each execution phase
71- always_ff @ (posedge tcb.clk)
72- begin
73- if ($past (tcb.trn)) begin
74- // instruction fetch
75- if ($past (pha) == IF ) begin
76- str_if.push_front ($sformatf (" 0x%8h (0x%8h )" , $past (tcb.req.adr), tcb.rsp.rdt));
77- end
78- // GPR write-back (rs1/rs2 reads are not logged)
79- if ($past (pha) == WB ) begin
80- str_wb.push_front ($sformatf (" %s 0x%8h " , format_gpr ($past (tcb.req.adr[2 + : 5 ])), $past (tcb.req.wdt)));
81- end
82- // memory load
83- if ($past (pha) == MLD ) begin
84- str_ld.push_front ($sformatf (" mem 0x%8h " , $past (tcb.req.adr)));
85- end
86- // memory store
87- if ($past (pha) == MST ) begin
88- case ($past (tcb.req.siz))
89- 2'd0 : str_st.push_front ($sformatf (" mem 0x%8h 0x%2h " , $past (tcb.req.adr), $past (tcb.req.wdt[ 8 - 1 : 0 ])));
90- 2'd1 : str_st.push_front ($sformatf (" mem 0x%8h 0x%4h " , $past (tcb.req.adr), $past (tcb.req.wdt[16 - 1 : 0 ])));
91- 2'd2 : str_st.push_front ($sformatf (" mem 0x%8h 0x%8h " , $past (tcb.req.adr), $past (tcb.req.wdt[32 - 1 : 0 ])));
80+ // prepare string for each execution phase
81+ always_ff @ (posedge tcb.clk)
82+ if (tcb.rst) begin
83+ ifu_vld = 1'b0 ;
84+ end else if ($past (tcb.trn)) begin
85+ case ($past (pha))
86+ IF : begin
87+ // log instruction trace
88+ if (ifu_vld) begin
89+ string str = trace_spike :: trace (
90+ .core (0 ),
91+ // IFU
92+ .ifu_adr,
93+ .ifu_ins,
94+ .ifu_ill,
95+ // WBU (write back to destination register)
96+ .wbu_vld,
97+ .wbu_idx,
98+ .wbu_dat,
99+ // LSU
100+ .lsu_vld,
101+ .lsu_wen,
102+ .lsu_idx,
103+ .lsu_adr,
104+ .lsu_siz,
105+ .lsu_wdt,
106+ .lsu_rdt
107+ );
108+ $fwrite (fd, str);
109+ end
110+ // instruction fetch
111+ ifu_vld <= 1'b1 ;
112+ ifu_adr <= $past (tcb.req.adr);
113+ ifu_ins <= tcb.rsp.rdt ;
114+ ifu_ill <= 1'b0 ; // TODO;
115+ // clear write-back/load/store valid
116+ wbu_vld <= 1'b0 ;
117+ lsu_vld <= 1'b0 ;
118+ end
119+ WB : begin
120+ // GPR write-back (rs1/rs2 reads are not logged)
121+ wbu_vld <= 1'b1 ;
122+ wbu_idx <= $past (tcb.req.adr[2 + : 5 ]);
123+ wbu_dat <= $past (tcb.req.wdt);
124+ end
125+ MLD : begin
126+ // memory load
127+ lsu_vld <= 1'b1 ;
128+ lsu_wen <= 1'b0 ; // read access
129+ lsu_idx <= 'x ; // destination register is defined with `wbu_idx`
130+ lsu_adr <= $past (tcb.req.adr);
131+ lsu_siz <= $past (tcb.req.siz);
132+ lsu_rdt <= tcb.req.rdt ;
133+ end
134+ MST : begin
135+ // memory store
136+ lsu_vld <= 1'b1 ;
137+ lsu_wen <= 1'b0 ; // read access
138+ lsu_idx <= 'x ; // destination register is defined with `wbu_idx`
139+ lsu_adr <= $past (tcb.req.adr);
140+ lsu_siz <= $past (tcb.req.siz);
141+ lsu_wdt <= $past (tcb.req.wdt);
142+ lsu_rdt <= tcb.req.rdt ;
143+ end
92144 endcase
93- end
94145 end
95- end
96146
97147 // prepare string for committed instruction
98148 always_ff @ (posedge tcb.clk)
@@ -105,7 +155,6 @@ module r5p_mouse_trace
105155 if ($past (pha) == IF ) begin
106156 // skip first fetch
107157 if (~ $past (tcb.rst,3 )) begin
108- $fwrite (fd, " core 0: 3%s%s%s%s \n " , str_if.pop_back (), str_wb.pop_back (), str_ld.pop_back (), str_st.pop_back ());
109158 end
110159 end
111160 end
0 commit comments