| 1 |
3 |
dgisselq |
///////////////////////////////////////////////////////////////////////////
|
| 2 |
|
|
//
|
| 3 |
|
|
// Filename: ziptimer.v
|
| 4 |
|
|
//
|
| 5 |
|
|
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core
|
| 6 |
|
|
//
|
| 7 |
|
|
// Purpose: A lighter weight implementation of the Zip Timer.
|
| 8 |
|
|
//
|
| 9 |
|
|
// Interface:
|
| 10 |
|
|
// Two options:
|
| 11 |
|
|
// 1. One combined register for both control and value, and ...
|
| 12 |
|
|
// The reload value is set any time the timer data value is "set".
|
| 13 |
|
|
// Reading the register returns the timer value. Controls are
|
| 14 |
|
|
// set so that writing a value to the timer automatically starts
|
| 15 |
|
|
// it counting down.
|
| 16 |
|
|
// 2. Two registers, one for control one for value.
|
| 17 |
|
|
// The control register would have the reload value in it.
|
| 18 |
|
|
// On the clock when the interface is set to zero the interrupt is set.
|
| 19 |
|
|
// Hence setting the timer to zero will disable the timer without
|
| 20 |
|
|
// setting any interrupts. Thus setting it to five will count
|
| 21 |
|
|
// 5 clocks: 5, 4, 3, 2, 1, Interrupt.
|
| 22 |
|
|
//
|
| 23 |
|
|
//
|
| 24 |
|
|
// Control bits:
|
| 25 |
|
|
// (Start_n/Stop. This bit has been dropped. Writing to this
|
| 26 |
|
|
// timer any value but zero starts it. Writing a zero
|
| 27 |
|
|
// clears and stops it.)
|
| 28 |
|
|
// AutoReload. If set, then on reset the timer automatically
|
| 29 |
|
|
// loads the last set value and starts over. This is
|
| 30 |
|
|
// useful for distinguishing between a one-time interrupt
|
| 31 |
|
|
// timer, and a repetitive interval timer.
|
| 32 |
|
|
// (INTEN. Interrupt enable--reaching zero always creates an
|
| 33 |
|
|
// interrupt, so this control bit isn't needed. The
|
| 34 |
|
|
// interrupt controller can be used to mask the interrupt.)
|
| 35 |
|
|
// (COUNT-DOWN/UP: This timer is *only* a count-down timer.
|
| 36 |
|
|
// There is no means of setting it to count up.)
|
| 37 |
|
|
// WatchDog
|
| 38 |
|
|
// This timer can be implemented as a watchdog timer simply by
|
| 39 |
|
|
// connecting the interrupt line to the reset line of the CPU.
|
| 40 |
|
|
// When the timer then expires, it will trigger a CPU reset.
|
| 41 |
|
|
//
|
| 42 |
|
|
//
|
| 43 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
| 44 |
|
|
// Gisselquist Technology, LLC
|
| 45 |
|
|
//
|
| 46 |
|
|
///////////////////////////////////////////////////////////////////////////
|
| 47 |
|
|
//
|
| 48 |
|
|
// Copyright (C) 2015, Gisselquist Technology, LLC
|
| 49 |
|
|
//
|
| 50 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
| 51 |
|
|
// modify it under the terms of the GNU General Public License as published
|
| 52 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
| 53 |
|
|
// your option) any later version.
|
| 54 |
|
|
//
|
| 55 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
| 56 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
| 57 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
| 58 |
|
|
// for more details.
|
| 59 |
|
|
//
|
| 60 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
| 61 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
| 62 |
|
|
//
|
| 63 |
|
|
//
|
| 64 |
|
|
///////////////////////////////////////////////////////////////////////////
|
| 65 |
|
|
//
|
| 66 |
|
|
module ziptimer(i_clk, i_rst, i_ce,
|
| 67 |
|
|
i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data,
|
| 68 |
|
|
o_wb_ack, o_wb_stall, o_wb_data,
|
| 69 |
|
|
o_int);
|
| 70 |
|
|
parameter BW = 32, VW = (BW-1), RELOADABLE=1;
|
| 71 |
|
|
input i_clk, i_rst, i_ce;
|
| 72 |
|
|
// Wishbone inputs
|
| 73 |
|
|
input i_wb_cyc, i_wb_stb, i_wb_we;
|
| 74 |
|
|
input [(BW-1):0] i_wb_data;
|
| 75 |
|
|
// Wishbone outputs
|
| 76 |
|
|
output reg o_wb_ack;
|
| 77 |
|
|
output wire o_wb_stall;
|
| 78 |
|
|
output wire [(BW-1):0] o_wb_data;
|
| 79 |
|
|
// Interrupt line
|
| 80 |
|
|
output reg o_int;
|
| 81 |
|
|
|
| 82 |
|
|
reg r_running;
|
| 83 |
|
|
|
| 84 |
|
|
wire wb_write;
|
| 85 |
|
|
assign wb_write = ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_we));
|
| 86 |
|
|
|
| 87 |
|
|
wire auto_reload;
|
| 88 |
|
|
wire [(VW-1):0] reload_value;
|
| 89 |
|
|
|
| 90 |
|
|
initial r_running = 1'b0;
|
| 91 |
|
|
always @(posedge i_clk)
|
| 92 |
|
|
if (i_rst)
|
| 93 |
|
|
r_running <= 1'b0;
|
| 94 |
|
|
else if (wb_write)
|
| 95 |
|
|
r_running <= (|i_wb_data[(VW-1):0]);
|
| 96 |
|
|
else if ((o_int)&&(~auto_reload))
|
| 97 |
|
|
r_running <= 1'b0;
|
| 98 |
|
|
|
| 99 |
|
|
generate
|
| 100 |
|
|
if (RELOADABLE != 0)
|
| 101 |
|
|
begin
|
| 102 |
|
|
reg r_auto_reload;
|
| 103 |
|
|
reg [(VW-1):0] r_reload_value;
|
| 104 |
|
|
|
| 105 |
|
|
initial r_auto_reload = 1'b0;
|
| 106 |
|
|
|
| 107 |
|
|
always @(posedge i_clk)
|
| 108 |
|
|
if (wb_write)
|
| 109 |
|
|
r_auto_reload <= (i_wb_data[(BW-1)]);
|
| 110 |
|
|
|
| 111 |
|
|
assign auto_reload = r_auto_reload;
|
| 112 |
|
|
|
| 113 |
|
|
// If setting auto-reload mode, and the value to other
|
| 114 |
|
|
// than zero, set the auto-reload value
|
| 115 |
|
|
always @(posedge i_clk)
|
| 116 |
|
|
if ((wb_write)&&(i_wb_data[(BW-1)])&&(|i_wb_data[(VW-1):0]))
|
| 117 |
|
|
r_reload_value <= i_wb_data[(VW-1):0];
|
| 118 |
|
|
assign reload_value = r_reload_value;
|
| 119 |
|
|
end else begin
|
| 120 |
|
|
assign auto_reload = 1'b0;
|
| 121 |
|
|
assign reload_value = 0;
|
| 122 |
|
|
end endgenerate
|
| 123 |
|
|
|
| 124 |
|
|
|
| 125 |
|
|
reg [(VW-1):0] r_value;
|
| 126 |
|
|
initial r_value = 0;
|
| 127 |
|
|
always @(posedge i_clk)
|
| 128 |
|
|
if (wb_write)
|
| 129 |
|
|
r_value <= i_wb_data[(VW-1):0];
|
| 130 |
|
|
else if ((r_running)&&(i_ce)&&(~o_int))
|
| 131 |
|
|
r_value <= r_value + {(VW){1'b1}}; // r_value - 1;
|
| 132 |
|
|
else if ((r_running)&&(auto_reload)&&(o_int))
|
| 133 |
|
|
r_value <= reload_value;
|
| 134 |
|
|
|
| 135 |
|
|
// Set the interrupt on our last tick, as we transition from one to
|
| 136 |
|
|
// zero.
|
| 137 |
|
|
initial o_int = 1'b0;
|
| 138 |
|
|
always @(posedge i_clk)
|
| 139 |
|
|
if (i_rst)
|
| 140 |
|
|
o_int <= 1'b0;
|
| 141 |
|
|
else if (i_ce)
|
| 142 |
|
|
o_int <= (r_running)&&(r_value == { {(VW-1){1'b0}}, 1'b1 });
|
| 143 |
|
|
else
|
| 144 |
|
|
o_int <= 1'b0;
|
| 145 |
|
|
|
| 146 |
|
|
initial o_wb_ack = 1'b0;
|
| 147 |
|
|
always @(posedge i_clk)
|
| 148 |
|
|
o_wb_ack <= (i_wb_cyc)&&(i_wb_stb);
|
| 149 |
|
|
assign o_wb_stall = 1'b0;
|
| 150 |
|
|
|
| 151 |
|
|
generate
|
| 152 |
|
|
if (VW < BW-1)
|
| 153 |
|
|
assign o_wb_data = { auto_reload, {(BW-1-VW){1'b0}}, r_value };
|
| 154 |
|
|
else
|
| 155 |
|
|
assign o_wb_data = { auto_reload, r_value };
|
| 156 |
|
|
endgenerate
|
| 157 |
|
|
|
| 158 |
|
|
endmodule
|