OpenCores
URL https://opencores.org/ocsvn/zipcpu/zipcpu/trunk

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [rtl/] [peripherals/] [zipcounter.v] - Blame information for rev 209

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 201 dgisselq
////////////////////////////////////////////////////////////////////////////////
2 2 dgisselq
//
3
// Filename:    zipcounter.v
4
//
5
// Project:     Zip CPU -- a small, lightweight, RISC CPU soft core
6
//
7
// Purpose:
8
//              A very, _very_ simple counter.  It's purpose doesn't really
9
//      include rollover, but it will interrupt on rollover.  It can be set,
10
//      although my design concept is that it can be reset.  It cannot be
11
//      halted.  It will always produce interrupts--whether or not they are 
12
//      handled interrupts is another question--that's up to the interrupt
13
//      controller.
14
//
15
//      My intention is to use this counter for process accounting: I should
16
//      be able to use this to count clock ticks of processor time assigned to
17
//      each task by resetting the counter at the beginning of every task
18
//      interval, and reading the result at the end of the interval.  As long
19
//      as the interval is less than 2^32 clocks, there should be no problem.
20
//      Similarly, this can be used to measure CPU wishbone bus stalls, 
21
//      prefetch stalls, or other CPU stalls (i.e. stalling as part of a JMP
22
//      instruction, or a read from the condition codes following a write).
23
//
24
//
25
// Creator:     Dan Gisselquist, Ph.D.
26 69 dgisselq
//              Gisselquist Technology, LLC
27 2 dgisselq
//
28 201 dgisselq
////////////////////////////////////////////////////////////////////////////////
29 2 dgisselq
//
30 209 dgisselq
// Copyright (C) 2015-2019, Gisselquist Technology, LLC
31 2 dgisselq
//
32
// This program is free software (firmware): you can redistribute it and/or
33
// modify it under the terms of  the GNU General Public License as published
34
// by the Free Software Foundation, either version 3 of the License, or (at
35
// your option) any later version.
36
//
37
// This program is distributed in the hope that it will be useful, but WITHOUT
38
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
39
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
40
// for more details.
41
//
42 201 dgisselq
// You should have received a copy of the GNU General Public License along
43
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
44
// target there if the PDF file isn't present.)  If not, see
45
// <http://www.gnu.org/licenses/> for a copy.
46
//
47 2 dgisselq
// License:     GPL, v3, as defined and found on www.gnu.org,
48
//              http://www.gnu.org/licenses/gpl.html
49
//
50
//
51 201 dgisselq
////////////////////////////////////////////////////////////////////////////////
52 2 dgisselq
//
53 201 dgisselq
//
54 209 dgisselq
`default_nettype        none
55
//
56
module  zipcounter(i_clk, i_reset, i_event,
57 2 dgisselq
                i_wb_cyc, i_wb_stb, i_wb_we, i_wb_data,
58
                        o_wb_ack, o_wb_stall, o_wb_data,
59
                o_int);
60
        parameter       BW = 32;
61 209 dgisselq
        //
62
        localparam      F_LGDEPTH = 2;
63
        //
64
        input   wire                    i_clk, i_reset, i_event;
65 2 dgisselq
        // Wishbone inputs
66 209 dgisselq
        input   wire                    i_wb_cyc, i_wb_stb, i_wb_we;
67
        input   wire    [(BW-1):0]       i_wb_data;
68 2 dgisselq
        // Wishbone outputs
69
        output  reg                     o_wb_ack;
70
        output  wire                    o_wb_stall;
71
        output  reg     [(BW-1):0]       o_wb_data;
72
        // Interrupt line
73
        output  reg                     o_int;
74
 
75 160 dgisselq
        initial o_int = 0;
76 2 dgisselq
        initial o_wb_data = 32'h00;
77
        always @(posedge i_clk)
78 209 dgisselq
                if (i_reset)
79
                        { o_int, o_wb_data } <= 0;
80
                else if ((i_wb_stb)&&(i_wb_we))
81 160 dgisselq
                        { o_int, o_wb_data } <= { 1'b0, i_wb_data };
82 209 dgisselq
                else if (i_event)
83 160 dgisselq
                        { o_int, o_wb_data } <= o_wb_data+{{(BW-1){1'b0}},1'b1};
84 2 dgisselq
                else
85
                        o_int <= 1'b0;
86
 
87
        initial o_wb_ack = 1'b0;
88
        always @(posedge i_clk)
89 209 dgisselq
        if (i_reset)
90
                o_wb_ack <= 1'b0;
91
        else
92
                o_wb_ack <= i_wb_stb;
93 2 dgisselq
        assign  o_wb_stall = 1'b0;
94 209 dgisselq
 
95
 
96
        // Make verilator happy
97
        // verilator lint_off UNUSED
98
        wire    unused;
99
        assign  unused = i_wb_cyc;
100
        // verilator lint_on  UNUSED
101
 
102
`ifdef  FORMAL
103
        reg     f_past_valid;
104
        initial f_past_valid = 1'b0;
105
        always @(posedge i_clk)
106
                f_past_valid <= 1'b1;
107
 
108
        always @(*)
109
        if (!f_past_valid)
110
                assume(i_reset);
111
 
112
        ////////////////////////////////////////////////
113
        //
114
        //
115
        // Assumptions about our inputs
116
        //
117
        //
118
        ////////////////////////////////////////////////
119
        //
120
 
121
        ////////////////////////////////////////////////
122
        //
123
        //
124
        // Bus interface properties
125
        //
126
        //
127
        ////////////////////////////////////////////////
128
        //
129
 
130
        // We never stall the bus
131
        always @(*)
132
                assert(!o_wb_stall);
133
 
134
        // We always ack every transaction on the following clock
135
        always @(posedge i_clk)
136
                assert(o_wb_ack == ((f_past_valid)&&(!$past(i_reset))
137
                                                &&($past(i_wb_stb))));
138
 
139
        wire    [(F_LGDEPTH-1):0]        f_nreqs, f_nacks, f_outstanding;
140
 
141
        fwb_slave #( .AW(1), .F_MAX_STALL(0),
142
                        .F_MAX_ACK_DELAY(1), .F_LGDEPTH(F_LGDEPTH)
143
                ) fwbi(i_clk, i_reset,
144
                i_wb_cyc, i_wb_stb, i_wb_we, 1'b0, i_wb_data, 4'hf,
145
                        o_wb_ack, o_wb_stall, o_wb_data, 1'b0,
146
                f_nreqs, f_nacks, f_outstanding);
147
 
148
        always @(*)
149
        if ((o_wb_ack)&&(i_wb_cyc))
150
                assert(f_outstanding==1);
151
        else
152
                assert(f_outstanding == 0);
153
 
154
        ////////////////////////////////////////////////
155
        //
156
        //
157
        // Assumptions about our outputs
158
        //
159
        //
160
        ////////////////////////////////////////////////
161
        //
162
 
163
        // Drop the interrupt line and reset the counter on any reset
164
        always @(posedge i_clk)
165
        if ((f_past_valid)&&($past(i_reset)))
166
                assert((!o_int)&&(o_wb_data == 0));
167
 
168
        // Clear the interrupt and set the counter on any write (other than
169
        // during a reset)
170
        always @(posedge i_clk)
171
        if ((f_past_valid)&&(!$past(i_reset))
172
                &&($past(i_wb_stb))&&($past(i_wb_we)))
173
                assert((!o_int)&&(o_wb_data == $past(i_wb_data)));
174
 
175
        // Normal logic of the routine itself
176
        always @(posedge i_clk)
177
        if ((f_past_valid)&&(!$past(i_reset))&&(!$past(i_wb_stb)))
178
        begin
179
                if (!$past(i_event))
180
                begin
181
                        // If the CE line wasn't set on the last clock, then the
182
                        // counter must not change, and the interrupt line must
183
                        // be low.
184
                        assert(o_wb_data == $past(o_wb_data));
185
                        assert(!o_int);
186
                end else // if ($past(i_event))
187
                begin
188
                        // Otherwise, if the CE line was high on the last clock,
189
                        // then our counter should have incremented.
190
                        assert(o_wb_data == $past(o_wb_data) + 1'b1);
191
 
192
                        // Likewise, if the counter rolled over, then the
193
                        // output interrupt, o_int, should be true.
194
                        if ($past(o_wb_data)=={(BW){1'b1}})
195
                                assert(o_int);
196
                        else
197
                                // In all other circumstances it should be clear
198
                                assert(!o_int);
199
                end
200
        end
201
 
202
        //
203
        // The output interrupt should never be true two clocks in a row
204
        always @(posedge i_clk)
205
        if ((f_past_valid)&&($past(o_int)))
206
                assert(!o_int);
207
 
208
`endif
209 2 dgisselq
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.