Đây là 2 chương trình FSM mẫu viết bằng ngôn ngữ verilog HDL, sử dụng cho 2 máy: moore và mealy... với các giải thuật và code được viết trong file.... các bạn có thể sử dụng để làm tài liệu ôn tập thi học kì môn ngôn ngữ mô tả phần cứng HDL.
Trang 1Moore machine – String 1011 Detector
module Moore_FSM(x, clk, rst, w);
input x, clk, rst;
output w;
reg w;
parameter [2:0] A = 3'b 000, B = 3'b 001, C = 3'b 010, D = 3'b 011, E = 3'b 100;
reg [2:0] p_state, n_state; // present and next states, respectively
always @(x or p_state) begin: Transitions
n_state = A; //Reset state case (p_state)
A:
if (x == 1'b 1) n_state = B;
else n_state = A;
B:
if (x == 1'b 1) n_state = B;
else n_state = C;
C:
if (x == 1'b 1) n_state = D;
else n_state = A;
D:
if (x == 1'b 1) n_state = E;
else n_state = C;
E:
if (x == 1'b 1) n_state = B;
else n_state = C;
default:
n_state = A; // reset state endcase
end
always @(x or p_state) begin: Outputing
w = 1'b 0;
w = (p_state == E);
end
always @(posedge clk) begin: Registering //Synchronous
if (rst) p_state = A; //reset state else p_state = n_state;
end
endmodule
Trang 2Mealy machine– String 1011 Detector
module Mealy_FSM(x, clk, rst, w);
input x, clk, rst;
output w;
reg w;
parameter [1:0] A = 2'b 00, B = 2'b 01, C = 2'b 10, D = 2'b 11;
reg [1:0] p_state, n_state; // present and next states, respectively
always @(x or p_state) begin: Transitions
n_state = A; //Reset state case (p_state)
A:
if (x == 1'b 1) n_state = B;
else n_state = A;
B:
if (x == 1'b 1) n_state = B;
else n_state = C;
C:
if (x == 1'b 1) n_state = D;
else n_state = A;
D:
if (x == 1'b 1) n_state = B;
else n_state = C;
default:
n_state = A; // reset state endcase
end
always @(x or p_state) begin: Outputing
w = 1'b 0;
w = (p_state == D)&(x == 1'b 1);
end
always @(posedge clk) begin: Registering //Synchronous
if (rst) p_state = A; //reset state else p_state = n_state;
end
endmodule