1.1 diff -r 000000000000 -r cd0b58aa6f83 lm32_trace.v 1.2 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 +++ b/lm32_trace.v Sun Apr 04 20:40:03 2010 +0100 1.4 @@ -0,0 +1,298 @@ 1.5 +// ============================================================================= 1.6 +// COPYRIGHT NOTICE 1.7 +// Copyright 2006 (c) Lattice Semiconductor Corporation 1.8 +// ALL RIGHTS RESERVED 1.9 +// This confidential and proprietary software may be used only as authorised by 1.10 +// a licensing agreement from Lattice Semiconductor Corporation. 1.11 +// The entire notice above must be reproduced on all authorized copies and 1.12 +// copies may only be made to the extent permitted by a licensing agreement from 1.13 +// Lattice Semiconductor Corporation. 1.14 +// 1.15 +// Lattice Semiconductor Corporation TEL : 1-800-Lattice (USA and Canada) 1.16 +// 5555 NE Moore Court 408-826-6000 (other locations) 1.17 +// Hillsboro, OR 97124 web : http://www.latticesemi.com/ 1.18 +// U.S.A email: techsupport@latticesemi.com 1.19 +// =============================================================================/ 1.20 +// FILE DETAILS 1.21 +// Project : LatticeMico32 1.22 +// File : lm32_trace.v 1.23 +// Title : PC Trace and associated logic. 1.24 +// Dependencies : lm32_include.v, lm32_functions.v 1.25 +// Version : 6.1.17 1.26 +// : Initial Release 1.27 +// Version : 7.0SP2, 3.0 1.28 +// : No Change 1.29 +// Version : 3.1 1.30 +// : No Change 1.31 +// ============================================================================= 1.32 + 1.33 +`include "lm32_include.v" 1.34 +`include "system_conf.v" 1.35 + 1.36 +`ifdef CFG_TRACE_ENABLED 1.37 +module lm32_trace( 1.38 + // ----- Inputs ------- 1.39 + clk_i, 1.40 + rst_i, 1.41 + stb_i, 1.42 + we_i, 1.43 + sel_i, 1.44 + dat_i, 1.45 + adr_i, 1.46 + 1.47 + trace_pc, 1.48 + trace_eid, 1.49 + trace_eret, 1.50 + trace_bret, 1.51 + trace_pc_valid, 1.52 + trace_exception, 1.53 + 1.54 + // -- outputs 1.55 + ack_o, 1.56 + dat_o); 1.57 + 1.58 + input clk_i; 1.59 + input rst_i; 1.60 + input stb_i; 1.61 + input we_i; 1.62 + input [3:0] sel_i; 1.63 + input [`LM32_WORD_RNG] dat_i; 1.64 + input [`LM32_WORD_RNG] adr_i; 1.65 + input [`LM32_PC_RNG] trace_pc; 1.66 + input [`LM32_EID_RNG] trace_eid; 1.67 + input trace_eret; 1.68 + input trace_bret; 1.69 + input trace_pc_valid; 1.70 + input trace_exception; 1.71 + // -- outputs 1.72 + output ack_o; 1.73 + output [`LM32_WORD_RNG] dat_o; 1.74 + reg ovrflw; 1.75 + 1.76 + function integer clogb2; 1.77 + input [31:0] value; 1.78 + begin 1.79 + for (clogb2 = 0; value > 0; clogb2 = clogb2 + 1) 1.80 + value = value >> 1; 1.81 + end 1.82 + endfunction 1.83 + 1.84 + // instantiate the trace memory 1.85 + parameter mem_data_width = `LM32_PC_WIDTH; 1.86 + parameter mem_addr_width = clogb2(`CFG_TRACE_DEPTH-1); 1.87 + 1.88 + wire [`LM32_PC_RNG] trace_dat_o; 1.89 + wire [mem_addr_width-1:0] trace_raddr; 1.90 + wire [mem_addr_width-1:0] trace_waddr; 1.91 + reg trace_we; 1.92 + wire trace_be, trace_last; 1.93 + wire rw_creg = adr_i[12]; 1.94 + 1.95 + lm32_ram #(.data_width (mem_data_width), 1.96 + .address_width (mem_addr_width)) 1.97 + trace_mem (.read_clk (clk_i), 1.98 + .write_clk (clk_i), 1.99 + .reset (rst_i), 1.100 + .read_address (adr_i[mem_addr_width+1:2]), 1.101 + .write_address (trace_waddr), 1.102 + .enable_read (`TRUE), 1.103 + .enable_write ((trace_we | trace_be) & trace_pc_valid & !trace_last), 1.104 + .write_enable (`TRUE), 1.105 + .write_data (trace_pc), 1.106 + .read_data (trace_dat_o)); 1.107 + 1.108 + // trigger type & stop type 1.109 + // trig_type [0] = start capture when bret 1.110 + // trig_type [1] = start capture when eret 1.111 + // trig_type [2] = start capture when PC within a range 1.112 + // trig_type [3] = start when an exception happens (other than breakpoint) 1.113 + // trig_type [4] = start when a breakpoint exception happens 1.114 + 1.115 + 1.116 + reg [4:0] trig_type; // at address 0 1.117 + reg [4:0] stop_type; // at address 16 1.118 + reg [`LM32_WORD_RNG] trace_len; // at address 4 1.119 + reg [`LM32_WORD_RNG] pc_low; // at address 8 1.120 + reg [`LM32_WORD_RNG] pc_high; // at address 12 1.121 + reg trace_start,trace_stop; 1.122 + reg ack_o; 1.123 + reg mem_valid; 1.124 + reg [`LM32_WORD_RNG] reg_dat_o; 1.125 + reg started; 1.126 + reg capturing; 1.127 + assign dat_o = (rw_creg ? reg_dat_o : trace_dat_o); 1.128 + 1.129 + initial begin 1.130 + trig_type <= 0; 1.131 + stop_type <= 0; 1.132 + trace_len <= 0; 1.133 + pc_low <= 0; 1.134 + pc_high <= 0; 1.135 + trace_start <= 0; 1.136 + trace_stop <= 0; 1.137 + ack_o <= 0; 1.138 + reg_dat_o <= 0; 1.139 + mem_valid <= 0; 1.140 + started <= 0; 1.141 + capturing <= 0; 1.142 + end 1.143 + 1.144 + // the host side control 1.145 + always @(posedge clk_i `CFG_RESET_SENSITIVITY) 1.146 + begin 1.147 + if (rst_i == `TRUE) begin 1.148 + trig_type <= 0; 1.149 + trace_stop <= 0; 1.150 + trace_start <= 0; 1.151 + pc_low <= 0; 1.152 + pc_high <= 0; 1.153 + ack_o <= 0; 1.154 + end else begin 1.155 + if (stb_i == `TRUE && ack_o == `FALSE) begin 1.156 + if (rw_creg) begin // control register access 1.157 + ack_o <= `TRUE; 1.158 + if (we_i == `TRUE) begin 1.159 + case ({adr_i[11:2],2'b0}) 1.160 + // write to trig type 1.161 + 12'd0: 1.162 + begin 1.163 + if (sel_i[0]) begin 1.164 + trig_type[4:0] <= dat_i[4:0]; 1.165 + end 1.166 + if (sel_i[3]) begin 1.167 + trace_start <= dat_i[31]; 1.168 + trace_stop <= dat_i[30]; 1.169 + end 1.170 + end 1.171 + 12'd8: 1.172 + begin 1.173 + if (sel_i[3]) pc_low[31:24] <= dat_i[31:24]; 1.174 + if (sel_i[2]) pc_low[23:16] <= dat_i[23:16]; 1.175 + if (sel_i[1]) pc_low[15:8] <= dat_i[15:8]; 1.176 + if (sel_i[0]) pc_low[7:0] <= dat_i[7:0]; 1.177 + end 1.178 + 12'd12: 1.179 + begin 1.180 + if (sel_i[3]) pc_high[31:24] <= dat_i[31:24]; 1.181 + if (sel_i[2]) pc_high[23:16] <= dat_i[23:16]; 1.182 + if (sel_i[1]) pc_high[15:8] <= dat_i[15:8]; 1.183 + if (sel_i[0]) pc_high[7:0] <= dat_i[7:0]; 1.184 + end 1.185 + 12'd16: 1.186 + begin 1.187 + if (sel_i[0])begin 1.188 + stop_type[4:0] <= dat_i[4:0]; 1.189 + end 1.190 + end 1.191 + endcase 1.192 + end else begin // read control registers 1.193 + case ({adr_i[11:2],2'b0}) 1.194 + // read the trig type 1.195 + 12'd0: 1.196 + reg_dat_o <= {22'b1,capturing,mem_valid,ovrflw,trace_we,started,trig_type}; 1.197 + 12'd4: 1.198 + reg_dat_o <= trace_len; 1.199 + 12'd8: 1.200 + reg_dat_o <= pc_low; 1.201 + 12'd12: 1.202 + reg_dat_o <= pc_high; 1.203 + default: 1.204 + reg_dat_o <= {27'b0,stop_type}; 1.205 + endcase 1.206 + end // else: !if(we_i == `TRUE) 1.207 + end else // read / write memory 1.208 + if (we_i == `FALSE) begin 1.209 + ack_o <= `TRUE; 1.210 + end else 1.211 + ack_o <= `FALSE; 1.212 + // not allowed to write to trace memory 1.213 + end else begin // if (stb_i == `TRUE) 1.214 + trace_start <= `FALSE; 1.215 + trace_stop <= `FALSE; 1.216 + ack_o <= `FALSE; 1.217 + end // else: !if(stb_i == `TRUE) 1.218 + end // else: !if(rst_i == `TRUE) 1.219 + end 1.220 + 1.221 + wire [`LM32_WORD_RNG] trace_pc_tmp = {trace_pc,2'b0}; 1.222 + 1.223 + // trace state machine 1.224 + reg [2:0] tstate; 1.225 + wire pc_in_range = {trace_pc,2'b0} >= pc_low && 1.226 + {trace_pc,2'b0} <= pc_high; 1.227 + 1.228 + assign trace_waddr = trace_len[mem_addr_width-1:0]; 1.229 + 1.230 + wire trace_begin = ((trig_type[0] & trace_bret) || 1.231 + (trig_type[1] & trace_eret) || 1.232 + (trig_type[2] & pc_in_range & trace_pc_valid) || 1.233 + (trig_type[3] & trace_exception & (trace_eid != `LM32_EID_BREAKPOINT)) || 1.234 + (trig_type[4] & trace_exception & (trace_eid == `LM32_EID_BREAKPOINT)) 1.235 + ); 1.236 + 1.237 + 1.238 + wire trace_end = (trace_stop || 1.239 + (stop_type[0] & trace_bret) || 1.240 + (stop_type[1] & trace_eret) || 1.241 + (stop_type[2] & !pc_in_range & trace_pc_valid) || 1.242 + (stop_type[3] & trace_exception & (trace_eid != `LM32_EID_BREAKPOINT)) || 1.243 + (stop_type[4] & trace_exception & (trace_eid == `LM32_EID_BREAKPOINT)) 1.244 + ); 1.245 + 1.246 + assign trace_be = (trace_begin & (tstate == 3'd1)); 1.247 + assign trace_last = (trace_stop & (tstate == 3'd2)); 1.248 + 1.249 + always @(posedge clk_i `CFG_RESET_SENSITIVITY) 1.250 + begin 1.251 + if (rst_i == `TRUE) begin 1.252 + tstate <= 0; 1.253 + trace_we <= 0; 1.254 + trace_len <= 0; 1.255 + ovrflw <= `FALSE; 1.256 + mem_valid <= 0; 1.257 + started <= 0; 1.258 + capturing <= 0; 1.259 + end else begin 1.260 + case (tstate) 1.261 + 3'd0: 1.262 + // start capture 1.263 + if (trace_start) begin 1.264 + tstate <= 3'd1; 1.265 + mem_valid <= 0; 1.266 + started <= 1; 1.267 + end 1.268 + 3'd1: 1.269 + begin 1.270 + // wait for trigger 1.271 + if (trace_begin) begin 1.272 + capturing <= 1; 1.273 + tstate <= 3'd2; 1.274 + trace_we <= `TRUE; 1.275 + trace_len <= 0; 1.276 + ovrflw <= `FALSE; 1.277 + end 1.278 + end // case: 3'd1 1.279 + 1.280 + 3'd2: 1.281 + begin 1.282 + if (trace_pc_valid) begin 1.283 + if (trace_len[mem_addr_width]) 1.284 + trace_len <= 0; 1.285 + else 1.286 + trace_len <= trace_len + 1; 1.287 + end 1.288 + if (!ovrflw) ovrflw <= trace_len[mem_addr_width]; 1.289 + // wait for stop condition 1.290 + if (trace_end) begin 1.291 + tstate <= 3'd0; 1.292 + trace_we <= 0; 1.293 + mem_valid <= 1; 1.294 + started <= 0; 1.295 + capturing <= 0; 1.296 + end 1.297 + end // case: 3'd2 1.298 + endcase 1.299 + end // else: !if(rst_i == `TRUE) 1.300 + end 1.301 +endmodule 1.302 +`endif