forked from aolofsson/oh
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoh_counter.v
73 lines (60 loc) · 1.84 KB
/
oh_counter.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
//#############################################################################
//# Function: Binary to gray encoder #
//#############################################################################
//# Author: Andreas Olofsson #
//# License: MIT (see LICENSE file in OH! repository) #
//#############################################################################
module oh_counter #(parameter DW = 32, // width of data inputs
parameter TYPE = "INCREMENT" // also DECREMENT, GRAY, LFSR
)
(
input clk, // clk input
input in, // input to count
input en, // enable counter
input load, // load counter
input [DW-1:0] load_data,// load data
output [DW-1:0] count, // current count value
output carry, // carry out from counter
output zero // counter is zero
);
// local variables
reg [DW-1:0] count;
reg carry;
wire [DW-1:0] count_in;
wire carry_in;
// configure counter based on type
generate
if(TYPE=="INCREMENT")
begin
assign {carry_in,count_in[DW-1:0]} = count[DW-1:0] + in;
end
else if(TYPE=="DECREMENT")
begin
assign count_in[DW-1:0] = count[DW-1:0] + in;
end
else if (TYPE=="GRAY")
begin
initial
$display ("NOT IMPLEMENTED");
end
else if (TYPE=="LFSR")
begin
initial
$display ("NOT IMPLEMENTED");
end
endgenerate
// counter
always @(posedge clk)
if(load)
begin
carry <= 1'b0;
count[DW-1:0] <= load_data[DW-1:0];
end
else if (en)
begin
carry <= carry_in;
count[DW-1:0] <= count_in[DW-1:0];
end
// counter expired
assign zero = ~(count[DW-1:0]);
endmodule // oh_counter