| 1 |
2 |
dgisselq |
///////////////////////////////////////////////////////////////////////////
|
| 2 |
|
|
//
|
| 3 |
|
|
// Filename: ziptimer.v
|
| 4 |
|
|
//
|
| 5 |
|
|
// Project: Zip CPU -- a small, lightweight, RISC CPU soft core
|
| 6 |
|
|
//
|
| 7 |
|
|
// Purpose:
|
| 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. Writing a '0' starts the timer, '1' stops it.
|
| 26 |
|
|
// Thus, ignoring this bit sets it to start.
|
| 27 |
|
|
// AutoReload. If set, then on reset the timer automatically
|
| 28 |
|
|
// loads the last set value and starts over. This is
|
| 29 |
|
|
// useful for distinguishing between a one-time interrupt
|
| 30 |
|
|
// timer, and a repetitive interval timer.
|
| 31 |
|
|
// (COUNT: If set, the timer only ticks whenever an external
|
| 32 |
|
|
// line goes high. What this external line is ... is
|
| 33 |
|
|
// not specified here. This, however, breaks my
|
| 34 |
|
|
// interface ideal of having our peripheral set not depend
|
| 35 |
|
|
// upon anything. Hence, this is an advanced option
|
| 36 |
|
|
// enabled at compile time only.)
|
| 37 |
|
|
// (INTEN. Interrupt enable--reaching zero always creates an
|
| 38 |
|
|
// interrupt, so this control bit isn't needed. The
|
| 39 |
|
|
// interrupt controller can be used to mask the interrupt.)
|
| 40 |
|
|
// (COUNT-DOWN/UP: This timer is *only* a count-down timer.
|
| 41 |
|
|
// There is no means of setting it to count up.)
|
| 42 |
|
|
// WatchDog
|
| 43 |
|
|
// This timer can be implemented as a watchdog timer simply by
|
| 44 |
|
|
// connecting the interrupt line to the reset line of the CPU.
|
| 45 |
|
|
// When the timer then expires, it will trigger a CPU reset.
|
| 46 |
|
|
//
|
| 47 |
|
|
//
|
| 48 |
|
|
// Creator: Dan Gisselquist, Ph.D.
|
| 49 |
|
|
// Gisselquist Tecnology, LLC
|
| 50 |
|
|
//
|
| 51 |
|
|
///////////////////////////////////////////////////////////////////////////
|
| 52 |
|
|
//
|
| 53 |
|
|
// Copyright (C) 2015, Gisselquist Technology, LLC
|
| 54 |
|
|
//
|
| 55 |
|
|
// This program is free software (firmware): you can redistribute it and/or
|
| 56 |
|
|
// modify it under the terms of the GNU General Public License as published
|
| 57 |
|
|
// by the Free Software Foundation, either version 3 of the License, or (at
|
| 58 |
|
|
// your option) any later version.
|
| 59 |
|
|
//
|
| 60 |
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
| 61 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
|
| 62 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
| 63 |
|
|
// for more details.
|
| 64 |
|
|
//
|
| 65 |
|
|
// License: GPL, v3, as defined and found on www.gnu.org,
|
| 66 |
|
|
// http://www.gnu.org/licenses/gpl.html
|
| 67 |
|
|
//
|
| 68 |
|
|
//
|
| 69 |
|
|
///////////////////////////////////////////////////////////////////////////
|
| 70 |
|
|
//
|
| 71 |
|
|
module ziptimer(i_clk, i_rst, i_ce,
|
| 72 |
|
|
i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data,
|
| 73 |
|
|
o_wb_ack, o_wb_stall, o_wb_data,
|
| 74 |
|
|
o_int);
|
| 75 |
|
|
parameter BW = 32, VW = (BW-2);
|
| 76 |
|
|
input i_clk, i_rst, i_ce;
|
| 77 |
|
|
// Wishbone inputs
|
| 78 |
|
|
input i_wb_cyc, i_wb_stb, i_wb_we;
|
| 79 |
|
|
input [(BW-1):0] i_wb_data;
|
| 80 |
|
|
// Wishbone outputs
|
| 81 |
|
|
output reg o_wb_ack;
|
| 82 |
|
|
output wire o_wb_stall;
|
| 83 |
|
|
output wire [(BW-1):0] o_wb_data;
|
| 84 |
|
|
// Interrupt line
|
| 85 |
|
|
output reg o_int;
|
| 86 |
|
|
|
| 87 |
|
|
reg r_auto_reload, r_running;
|
| 88 |
|
|
reg [(VW-1):0] r_reload_value;
|
| 89 |
|
|
initial r_running = 1'b0;
|
| 90 |
|
|
initial r_auto_reload = 1'b0;
|
| 91 |
|
|
always @(posedge i_clk)
|
| 92 |
|
|
if (i_rst)
|
| 93 |
|
|
begin
|
| 94 |
|
|
r_running <= 1'b0;
|
| 95 |
|
|
r_auto_reload <= 1'b0;
|
| 96 |
|
|
end else if ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_we))
|
| 97 |
|
|
begin
|
| 98 |
|
|
r_running <= (~i_wb_data[(BW-1)])&&(|i_wb_data[(BW-2):0]);
|
| 99 |
|
|
r_auto_reload <= (i_wb_data[(BW-2)]);
|
| 100 |
|
|
|
| 101 |
|
|
// If setting auto-reload mode, and the value to other
|
| 102 |
|
|
// than zero, set the auto-reload value
|
| 103 |
|
|
if ((i_wb_data[(BW-2)])&&(|i_wb_data[(BW-3):0]))
|
| 104 |
|
|
r_reload_value <= i_wb_data[(BW-3):0];
|
| 105 |
|
|
end
|
| 106 |
|
|
|
| 107 |
|
|
reg [(VW-1):0] r_value;
|
| 108 |
|
|
initial r_value = 0;
|
| 109 |
|
|
always @(posedge i_clk)
|
| 110 |
|
|
if ((r_running)&&(|r_value)&&(i_ce))
|
| 111 |
|
|
begin
|
| 112 |
|
|
r_value <= r_value - 1;
|
| 113 |
|
|
end else if ((r_running)&&(r_auto_reload))
|
| 114 |
|
|
r_value <= r_reload_value;
|
| 115 |
|
|
else if ((~r_running)&&(i_wb_cyc)&&(i_wb_stb)&&(i_wb_we))
|
| 116 |
|
|
r_value <= i_wb_data[(VW-1):0];
|
| 117 |
|
|
|
| 118 |
|
|
// Set the interrupt on our last tick.
|
| 119 |
|
|
initial o_int = 1'b0;
|
| 120 |
|
|
always @(posedge i_clk)
|
| 121 |
|
|
if (i_ce)
|
| 122 |
|
|
o_int <= (r_running)&&(r_value == { {(VW-1){1'b0}}, 1'b1 });
|
| 123 |
|
|
else
|
| 124 |
|
|
o_int <= 1'b0;
|
| 125 |
|
|
|
| 126 |
|
|
initial o_wb_ack = 1'b0;
|
| 127 |
|
|
always @(posedge i_clk)
|
| 128 |
|
|
o_wb_ack <= (i_wb_cyc)&&(i_wb_stb);
|
| 129 |
|
|
assign o_wb_stall = 1'b0;
|
| 130 |
|
|
|
| 131 |
|
|
assign o_wb_data = { ~r_running, r_auto_reload, r_value };
|
| 132 |
|
|
|
| 133 |
|
|
endmodule
|