-
Notifications
You must be signed in to change notification settings - Fork 2
/
counters.v
171 lines (143 loc) · 4.01 KB
/
counters.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* ----------------------------------------- *
* Title : Counters *
* Project : Verilog Utility Modules *
* ----------------------------------------- *
* File : counters.v *
* Author : Yigit Suoglu *
* Last Edit : 23/11/2020 *
* Licence : CERN-OHL-W *
* ----------------------------------------- *
* Description : Collection of counters *
* ----------------------------------------- */
/* ------------------------------------------------------ *
+ General working principles: +
+ Count up to limit (if limit is 0 count indefinitly) +
+ Only count up when enabled and not done +
+ Keep start high for one cycle to start counting +
+ (counters should be disabled) +
* ------------------------------------------------------ */
module counter_p#(parameter COUNTER_WIDTH = 8)(clk, start, en, done, count, limit);
input clk, en, start;
input [(COUNTER_WIDTH-1):0] limit;
output done;
output reg [(COUNTER_WIDTH-1):0] count;
reg start_del;
wire start_edge;
assign start_edge = ~start_del & start;
assign done = (limit == count) & (|limit); //when limit is 0, keep counting
always@(posedge clk)
begin
start_del <= start;
end
always@(posedge clk)
begin
if(start_edge)
begin
count <= {{(COUNTER_WIDTH-1){1'b0}}, en};
end
else
begin
count <= count + {{(COUNTER_WIDTH-1){1'b0}},(en & (~done))};
end
end
endmodule//counter parameterised
module counter8bit(clk, start, en, done, count, limit);
input clk, en, start;
input [7:0] limit;
output done;
output reg [7:0] count;
reg start_del;
wire start_edge;
assign start_edge = ~start_del & start;
assign done = (limit == count) & (|limit); //when limit is 0, keep counting
always@(posedge clk)
begin
start_del <= start;
end
always@(posedge clk)
begin
if(start_edge)
begin
count <= {7'b0, en};
end
else
begin
count <= count + {7'b0,(en & (~done))};
end
end
endmodule//counter8bit
module counter16bit(clk, start, en, done, count, limit);
input clk, en, start;
input [15:0] limit;
output done;
output reg [15:0] count;
reg start_del;
wire start_edge;
assign start_edge = ~start_del & start;
assign done = (limit == count) & (|limit); //when limit is 0, keep counting
always@(posedge clk)
begin
start_del <= start;
end
always@(posedge clk)
begin
if(start_edge)
begin
count <= {15'b0, en};
end
else
begin
count <= count + {15'b0,(en & (~done))};
end
end
endmodule//counter16bit
module counter32bit(clk, start, en, done, count, limit);
input clk, en, start;
input [31:0] limit;
output done;
output reg [31:0] count;
reg start_del;
wire start_edge;
assign start_edge = ~start_del & start;
assign done = (limit == count) & (|limit); //when limit is 0, keep counting
always@(posedge clk)
begin
start_del <= start;
end
always@(posedge clk)
begin
if(start_edge)
begin
count <= {31'b0, en};
end
else
begin
count <= count + {31'b0,(en & (~done))};
end
end
endmodule//counter32bit
module counter64bit(clk, start, en, done, count, limit);
input clk, en, start;
input [63:0] limit;
output done;
output reg [63:0] count;
reg start_del;
wire start_edge;
assign start_edge = ~start_del & start;
assign done = (limit == count) & (|limit); //when limit is 0, keep counting
always@(posedge clk)
begin
start_del <= start;
end
always@(posedge clk)
begin
if(start_edge)
begin
count <= {63'b0, en};
end
else
begin
count <= count + {63'b0,(en & (~done))};
end
end
endmodule//counter64bit