-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter.v
136 lines (92 loc) · 2.93 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
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
module DFF2(x,y);
input x;
output [1:0] y;
assign y = 1 << x ;
endmodule
module DFF(clk,in,out);
parameter n=1;//width
input clk;
input [n-1:0] in;
output [n-1:0] out;
reg [n-1:0] out;
always @(posedge clk)
out = in;
endmodule
module Mux2(a1, a0, s, b);
parameter k = 4;
input [k-1:0] a1, a0; // inputs
input [2-1:0] s; // one-hot select
output[k-1:0] b;
assign b = ({k{s[1]}} & a1) |
({k{s[0]}} & a0) ;
endmodule // Mux2
module Mux4(a3, a2, a1, a0, s, b) ;
parameter k = 1 ;
input [k-1:0] a3, a2, a1, a0 ; // inputs
input [3:0] s ; // one-hot select
output[k-1:0] b ;
assign b = ({k{s[3]}} & a3) |
({k{s[2]}} & a2) |
({k{s[1]}} & a1) |
({k{s[0]}} & a0) ;
endmodule // Mux4
module Sat_Count(clk, rst, up, down, load, max, in, out) ;
parameter n = 4 ;
input clk, rst, up, down, load, max ;
input [n-1:0] in ;
output [n-1:0] out ;
wire [n-1:0] next, outpm1, outup, outdown ;
wire [n-1:0] maxC;
wire [n-1:0] mux2Outpt;
wire [1:0] getMax;
reg [n-1:0] next, outpm1, outup, outdown ;
DFF2 maxDDF2 (max, getMax) ;
Mux2 #(n) muxSat (in, maxC, getMax, mux2Outpt);
DFF #(n) maxcount(clk, mux2Outpt, maxC) ;
assign outup = (maxC > out) ? out + {{n-1{down}},1'b1} : maxC;
assign outdown = ( 0 < out) ? out + {{n-1{down}},1'b1} : 0;
assign outpm1 = ({down} > 0) ? {outdown} : {outup};
DFF #(n) count(clk,next,out);
Mux4 #(n) mux(out, in, outpm1,
{n{1'b0}},//A ZERO
{(~rst & ~up & ~down & ~load),//ALL OFF
(~rst & load),//LOAD
(~rst & (up | down)),//UP OR DOWN
rst}, //RESET
next) ;//OUTPUT
endmodule
//==================================
module TestBench ;
parameter n=4;
reg clk, rst;
reg up,down,load,max ;
reg [n-1:0] in;
wire [n-1:0] out;
Sat_Count test(clk,rst,up,down,load,max,in,out);
initial begin
clk = 1 ; #5 clk = 0 ;
$display("Clock|Reset|Up|Down|Load|Max| In|MaxCounter| Out");
$display("-----+-----+--+----+----+---+------+----------+-----");
forever
begin
$display(" %b| %b| %b| %b| %b| %b| %b| %b|%b",clk,rst, up,down,load,max, in,test.maxC,out);
#5 clk = 1 ;
#5 clk = 0 ;
end
end
// input stimuli
initial begin
rst = 0 ; up=0; down=0; load=0;max =0; in=4'b0000;
#10 up=0;down=0;load=0;max=0;in=4'b0000;
#10 up=0;down=0;load=0;max=0;in=4'b1000;
#10 up=0;down=0;load=0;max=1;in=4'b1000;
#50 up=0;down=0;load=0;max=0;in=4'b0000;
#10 up=0;down=0;load=1;max=0;in=4'b0000;
#10 up=0;down=0;load=0;max=0;in=4'b0000;
#10 up=1;down=0;load=0;max=0;in=4'b0000;
#100 up=0;down=0;load=0;max=0;in=4'b0000;
#10 up=0;down=1;load=0;max=0;in=4'b0000;
#100
$stop ;
end
endmodule