-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsm4.v
65 lines (42 loc) · 999 Bytes
/
fsm4.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
// This code implements the following fsm - https://hdlbits.01xz.net/wiki/Fsm1
module top_module(
input clk,
input areset,
input in,
output reg out);
initial
state<=B;
parameter A=0, B=1;
reg state, next_state;
always @(*) begin
case(state)
A: begin out=1'b0; if(in) begin next_state=A; end
else begin next_state=B; end end
B: begin out=1'b1; if(in) begin next_state=B; end
else begin next_state=A; end end
endcase
end
always @(posedge clk, posedge areset) begin
if(areset) state<=B;
else
state<=next_state;
end
endmodule
// Testbench
module test;
wire out;
reg clk,areset, in;
top_module t(clk,areset,in,out);
always #5 clk=~clk;
initial begin
clk=1'b0;
areset=1'b0;
in=1'b0;
#6; in=1'b1;
#10; in=1'b0;
#10; in=1'b1;
#10 areset=1;
#20 $finish;
end
initial $monitor($time," ",areset," ",in," ",out);
endmodule