-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounter.v
65 lines (53 loc) · 1.2 KB
/
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
// Increment / Decrement counter with Set / Reset
`include "dlatch.v"
module CounterCell
(
input logic a,
input logic b,
input logic d,
output logic s,
output logic c
);
logic a_;
assign s = a ^ b;
assign a_ = a ^ d;
assign c = a_ & b;
endmodule
module Counter
#(
parameter integer WIDTH = 8
)
(
input logic [WIDTH-1:0] a,
input logic set,
input logic rst,
input logic mode,
input logic clk,
output logic [WIDTH-1:0] out
);
// Wiring
logic [WIDTH-1:0] latch_ctr;
logic [WIDTH-1:0] ctrout;
logic [WIDTH-1:0] latchin;
logic [WIDTH:0] ctr_c;
// DLatch memory
DLatch#(.WIDTH(WIDTH)) state(.clk(clk), .rst(rst), .d(latchin), .q(latch_ctr));
// Generate counter circuitry
genvar i;
generate
for(i = 0; i < WIDTH; i++) begin
CounterCell ctr(
.a(latch_ctr[i]),
.b(ctr_c[i]),
.c(ctr_c[i+1]),
.s(ctrout[i]),
.d(mode)
);
end
endgenerate
assign ctr_c[0] = '1;
// Control Counter / Set modes
assign latchin = set ? a : ctrout;
// Assign output
assign out = latch_ctr;
endmodule