-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsmcomp.v
57 lines (47 loc) · 911 Bytes
/
fsmcomp.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
// FSM for serial 2's complement
//Design
module test(input clk, input x,input rst, output reg y);
parameter S0=1'b0,S1=1'b1;
reg ps, ns;
initial
begin ps<=S1;ns<=S1;end
always@(posedge clk)
begin
if(rst)
ps<=S1;
else
ps<=ns;
end
always@(x or ps)
begin
case(ps)
S0:begin
ns<=S0;
if(x)y<=0;
else y<=1;
end
S1:begin
if(x) begin y<=1;ns<=S0;end
else begin y<=0;ns<=S1;end
end
default : begin ns<=S1;y<=0;
end
endcase
end
endmodule
// Test bench
module tb();
reg clk, rst, x;
wire y;
test t(clk, x, rst, y);
always #3 clk = ~clk;
initial begin
clk=1'b0;rst=1'b1;x=1'b0;
#20 rst=1'b0;
#20 x=1'b0;#1$display($time," x= %d , rst = %d , ",x,rst,y);
#20 x=1'b0;#1$display($time," x= %d , rst = %d , ",x,rst,y);
#20 x=1'b0;#1$display($time," x= %d , rst = %d , ",x,rst,y);
#20 x=1'b1;#1$display($time," x= %d , rst = %d , ",x,rst,y);
#20 x=1'b1;#1$display($time," x= %d , rst = %d , ",x,rst,y);
end
endmodule