题解 | #序列检测器(Moore型)#
序列检测器(Moore型)
http://www.nowcoder.com/practice/d5c5b853b892402ea80d27879b8fbfd6
`timescale 1ns/1ns
module det_moore(
input clk ,
input rst_n ,
input din ,
output reg Y
);
parameter a = 3'b000, b = 3'b001, c = 3'b010, d = 3'b011, idle = 3'b111;
reg [2:0] sta , nsta;
always @(*) begin
nsta = idle;
case (sta)
idle : nsta = din ? a : idle;
a : nsta = din ? b : idle;
b : nsta = din ? b : c;
c : nsta = din ? d : idle;
d : nsta = din ? a : idle;
endcase
end
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
sta <= idle;
end else begin
sta <= nsta;
end
end
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
Y <= 1'b0;
end else if (sta==d) begin
Y <= 1'b1;
end else begin
Y <= 1'b0;
end
end
endmodule