Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 1 | /* Simple 32-bit counter-timer for Caravel. */ |
| 2 | |
| 3 | module counter_timer_wb # ( |
Tim Edwards | c47465c | 2020-10-09 16:30:22 -0400 | [diff] [blame] | 4 | parameter BASE_ADR = 32'h2400_0000, |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 5 | parameter CONFIG = 8'h00, |
| 6 | parameter VALUE = 8'h04, |
| 7 | parameter DATA = 8'h08 |
| 8 | ) ( |
| 9 | input wb_clk_i, |
| 10 | input wb_rst_i, |
Tim Edwards | 4814a40 | 2020-10-19 19:43:52 -0400 | [diff] [blame] | 11 | input strobe_in, |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 12 | input [31:0] wb_adr_i, |
| 13 | input [31:0] wb_dat_i, |
| 14 | input [3:0] wb_sel_i, |
| 15 | input wb_we_i, |
| 16 | input wb_cyc_i, |
| 17 | input wb_stb_i, |
| 18 | |
| 19 | output wb_ack_o, |
| 20 | output [31:0] wb_dat_o, |
Tim Edwards | 4814a40 | 2020-10-19 19:43:52 -0400 | [diff] [blame] | 21 | output strobe_out, |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 22 | output irq |
| 23 | ); |
| 24 | wire [31:0] counter_timer_reg_cfg_do; |
| 25 | wire [31:0] counter_timer_reg_val_do; |
| 26 | wire [31:0] counter_timer_reg_dat_do; |
| 27 | |
| 28 | wire resetn = ~wb_rst_i; |
| 29 | wire valid = wb_stb_i && wb_cyc_i; |
| 30 | wire counter_timer_reg_cfg_sel = valid && (wb_adr_i == (BASE_ADR | CONFIG)); |
| 31 | wire counter_timer_reg_val_sel = valid && (wb_adr_i == (BASE_ADR | VALUE)); |
| 32 | wire counter_timer_reg_dat_sel = valid && (wb_adr_i == (BASE_ADR | DATA)); |
| 33 | |
| 34 | wire reg_cfg_we = (counter_timer_reg_cfg_sel) ? |
| 35 | (wb_sel_i[0] & {wb_we_i}): 1'b0; |
| 36 | wire [3:0] reg_val_we = (counter_timer_reg_val_sel) ? |
| 37 | (wb_sel_i & {4{wb_we_i}}): 4'b0000; |
| 38 | wire [3:0] reg_dat_we = (counter_timer_reg_dat_sel) ? |
| 39 | (wb_sel_i & {4{wb_we_i}}): 4'b0000; |
| 40 | |
| 41 | wire [31:0] mem_wdata = wb_dat_i; |
| 42 | wire reg_dat_re = counter_timer_reg_dat_sel && !wb_sel_i && ~wb_we_i; |
| 43 | |
| 44 | assign wb_dat_o = (counter_timer_reg_cfg_sel) ? counter_timer_reg_cfg_do : |
| 45 | (counter_timer_reg_val_sel) ? counter_timer_reg_val_do : |
| 46 | counter_timer_reg_dat_do; |
| 47 | assign wb_ack_o = counter_timer_reg_cfg_sel || counter_timer_reg_val_sel || |
| 48 | counter_timer_reg_dat_sel; |
| 49 | |
| 50 | counter_timer counter_timer_inst ( |
| 51 | .resetn(resetn), |
| 52 | .clkin(wb_clk_i), |
Tim Edwards | 4814a40 | 2020-10-19 19:43:52 -0400 | [diff] [blame] | 53 | .strobe_in(strobe_in), |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 54 | .reg_val_we(reg_val_we), |
| 55 | .reg_val_di(mem_wdata), |
| 56 | .reg_val_do(counter_timer_reg_val_do), |
| 57 | .reg_cfg_we(reg_cfg_we), |
| 58 | .reg_cfg_di(mem_wdata), |
| 59 | .reg_cfg_do(counter_timer_reg_cfg_do), |
| 60 | .reg_dat_we(reg_dat_we), |
| 61 | .reg_dat_di(mem_wdata), |
| 62 | .reg_dat_do(counter_timer_reg_dat_do), |
Tim Edwards | 4814a40 | 2020-10-19 19:43:52 -0400 | [diff] [blame] | 63 | .strobe_out(strobe_out), |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 64 | .irq_out(irq) |
| 65 | ); |
| 66 | |
| 67 | endmodule |
| 68 | |
| 69 | module counter_timer ( |
| 70 | input resetn, |
| 71 | input clkin, |
Tim Edwards | 4814a40 | 2020-10-19 19:43:52 -0400 | [diff] [blame] | 72 | input strobe_in, |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 73 | |
| 74 | input [3:0] reg_val_we, |
| 75 | input [31:0] reg_val_di, |
| 76 | output [31:0] reg_val_do, |
| 77 | |
| 78 | input reg_cfg_we, |
| 79 | input [31:0] reg_cfg_di, |
| 80 | output [31:0] reg_cfg_do, |
| 81 | |
| 82 | input [3:0] reg_dat_we, |
| 83 | input [31:0] reg_dat_di, |
| 84 | output [31:0] reg_dat_do, |
Tim Edwards | 4814a40 | 2020-10-19 19:43:52 -0400 | [diff] [blame] | 85 | output strobe_out, |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 86 | output irq_out |
| 87 | ); |
| 88 | |
| 89 | reg [31:0] value_cur; |
| 90 | reg [31:0] value_reset; |
Tim Edwards | 4814a40 | 2020-10-19 19:43:52 -0400 | [diff] [blame] | 91 | reg strobe_out; |
| 92 | wire irq_out; |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 93 | |
| 94 | reg enable; // Enable (start) the counter/timer |
Tim Edwards | c47465c | 2020-10-09 16:30:22 -0400 | [diff] [blame] | 95 | reg lastenable; // Previous state of enable (catch rising/falling edge) |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 96 | reg oneshot; // Set oneshot (1) mode or continuous (0) mode |
| 97 | reg updown; // Count up (1) or down (0) |
| 98 | reg irq_ena; // Enable interrupt on timeout |
Tim Edwards | 4814a40 | 2020-10-19 19:43:52 -0400 | [diff] [blame] | 99 | reg chain; // Chain to a secondary timer |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 100 | |
| 101 | // Configuration register |
| 102 | |
Tim Edwards | 4814a40 | 2020-10-19 19:43:52 -0400 | [diff] [blame] | 103 | assign reg_cfg_do = {27'd0, irq_ena, chain, updown, oneshot, enable}; |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 104 | |
| 105 | always @(posedge clkin or negedge resetn) begin |
| 106 | if (resetn == 1'b0) begin |
| 107 | enable <= 1'b0; |
Tim Edwards | c47465c | 2020-10-09 16:30:22 -0400 | [diff] [blame] | 108 | lastenable <= 1'b0; |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 109 | oneshot <= 1'b0; |
| 110 | updown <= 1'b0; |
Tim Edwards | 4814a40 | 2020-10-19 19:43:52 -0400 | [diff] [blame] | 111 | chain <= 1'b0; |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 112 | irq_ena <= 1'b0; |
| 113 | end else begin |
Tim Edwards | c47465c | 2020-10-09 16:30:22 -0400 | [diff] [blame] | 114 | lastenable <= enable; |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 115 | if (reg_cfg_we) begin |
| 116 | enable <= reg_cfg_di[0]; |
| 117 | oneshot <= reg_cfg_di[1]; |
| 118 | updown <= reg_cfg_di[2]; |
Tim Edwards | 4814a40 | 2020-10-19 19:43:52 -0400 | [diff] [blame] | 119 | chain <= reg_cfg_di[3]; |
| 120 | irq_ena <= reg_cfg_di[4]; |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 121 | end |
| 122 | end |
| 123 | end |
| 124 | |
| 125 | // Counter/timer reset value register |
| 126 | |
| 127 | assign reg_val_do = value_reset; |
| 128 | |
| 129 | always @(posedge clkin or negedge resetn) begin |
| 130 | if (resetn == 1'b0) begin |
| 131 | value_reset <= 32'd0; |
| 132 | end else begin |
Tim Edwards | c47465c | 2020-10-09 16:30:22 -0400 | [diff] [blame] | 133 | if (reg_val_we[3]) value_reset[31:24] <= reg_val_di[31:24]; |
| 134 | if (reg_val_we[2]) value_reset[23:16] <= reg_val_di[23:16]; |
| 135 | if (reg_val_we[1]) value_reset[15:8] <= reg_val_di[15:8]; |
| 136 | if (reg_val_we[0]) value_reset[7:0] <= reg_val_di[7:0]; |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 137 | end |
| 138 | end |
| 139 | |
| 140 | assign reg_dat_do = value_cur; |
| 141 | |
| 142 | // Counter/timer current value register and timer implementation |
| 143 | |
Tim Edwards | 4814a40 | 2020-10-19 19:43:52 -0400 | [diff] [blame] | 144 | assign irq_out = (irq_ena) ? strobe_out : 1'b0; |
| 145 | |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 146 | always @(posedge clkin or negedge resetn) begin |
| 147 | if (resetn == 1'b0) begin |
| 148 | value_cur <= 32'd0; |
Tim Edwards | 4814a40 | 2020-10-19 19:43:52 -0400 | [diff] [blame] | 149 | strobe_out <= 1'b0; |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 150 | end else begin |
| 151 | if (reg_dat_we != 4'b0000) begin |
| 152 | if (reg_dat_we[3] == 1'b1) value_cur[31:24] <= reg_dat_di[31:24]; |
| 153 | if (reg_dat_we[2] == 1'b1) value_cur[23:16] <= reg_dat_di[23:16]; |
| 154 | if (reg_dat_we[1] == 1'b1) value_cur[15:8] <= reg_dat_di[15:8]; |
| 155 | if (reg_dat_we[0] == 1'b1) value_cur[7:0] <= reg_dat_di[7:0]; |
| 156 | end else if (enable == 1'b1) begin |
| 157 | if (updown == 1'b1) begin |
Tim Edwards | c47465c | 2020-10-09 16:30:22 -0400 | [diff] [blame] | 158 | if (lastenable == 1'b0) begin |
| 159 | value_cur <= 32'd0; |
| 160 | end else if (value_cur == value_reset) begin |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 161 | if (oneshot != 1'b1) begin |
| 162 | value_cur <= 32'd0; |
| 163 | end |
Tim Edwards | 4814a40 | 2020-10-19 19:43:52 -0400 | [diff] [blame] | 164 | strobe_out <= 1'b1; |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 165 | end else begin |
Tim Edwards | 4814a40 | 2020-10-19 19:43:52 -0400 | [diff] [blame] | 166 | if ((chain == 1'b0) || ((chain == 1'b1) && (strobe_in == 1'b1))) begin |
| 167 | value_cur <= value_cur + 1; // count up |
| 168 | strobe_out <= 1'b0; |
| 169 | end |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 170 | end |
| 171 | end else begin |
Tim Edwards | c47465c | 2020-10-09 16:30:22 -0400 | [diff] [blame] | 172 | if (lastenable == 1'b0) begin |
| 173 | value_cur <= value_reset; |
| 174 | end else if (value_cur == 32'd0) begin |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 175 | if (oneshot != 1'b1) begin |
| 176 | value_cur <= value_reset; |
| 177 | end |
Tim Edwards | 4814a40 | 2020-10-19 19:43:52 -0400 | [diff] [blame] | 178 | strobe_out <= 1'b1; |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 179 | end else begin |
Tim Edwards | 4814a40 | 2020-10-19 19:43:52 -0400 | [diff] [blame] | 180 | if ((chain == 1'b0) || ((chain == 1'b1) && (strobe_in == 1'b1))) begin |
| 181 | value_cur <= value_cur - 1; // count down |
| 182 | strobe_out <= 1'b0; |
| 183 | end |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 184 | end |
| 185 | end |
| 186 | end else begin |
Tim Edwards | 4814a40 | 2020-10-19 19:43:52 -0400 | [diff] [blame] | 187 | strobe_out <= 1'b0; |
Tim Edwards | b5d5b27 | 2020-10-02 22:27:50 -0400 | [diff] [blame] | 188 | end |
| 189 | end |
| 190 | end |
| 191 | |
| 192 | endmodule |