题目来源:https://www.nowcoder.com/practice/f96d0e94ec604592b502b0f1800ed8aa?tpId=302&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3DVerilog%25E7%25AF%2587%26topicId%3D302
请编写一个序列检测模块,输入信号端口为data,表示数据有效的指示信号端口为data_valid。当data_valid信号为高时,表示此刻的输入信号data有效,参与序列检测;当data_valid为低时,data无效,抛弃该时刻的输入。当输入序列的有效信号满足0110时,拉高序列匹配信号match。
模块的时序图如下:
代码实现:
module sequence_detect(
input clk,
input rst_n,
input data,
input data_valid,
output reg match
);
fsm
localparam IDLE = 3’d0,
S0 = 3’d1,
S1 = 3’d2,
S2 = 3’d3,
S3 = 3’d4;
reg [2:0] state, next_state;
always @(posedge clk or negedge rst_n) begin
if(!rst_n) state <= IDLE;
else state <= next_state;
end
always @(*) begin
case(state)
IDLE : next_state = data_valid ? (!data ? S0 : IDLE) : IDLE;
S0 : next_state = data_valid ? (data ? S1 : S0) : S0;
S1 : next_state = data_valid ? (data ? S2 : S0) : S1;
S2 : next_state = data_valid ? (!data ? S3 : S0) : S2;
S3 : next_state = data_valid ? (data ? S1 : S0) : IDLE;
default : next_state = IDLE;
endcase
end
//always @(*)
// match = state == S3;
always @(posedge clk or negedge rst_n) begin
if(!rst_n)
match <= 1’b0;
else
match <= next_state == S3;
end
/ shift_register //
reg [3:0] data_lock;
always @(posedge clk or negedge rst_n) begin
if(!rst_n) data_lock <= 4’d0;
else data_lock <= data_valid ? {data_lock[2:0], data} : data_lock;
end
always @(posedge clk or negedge rst_n) begin
if(!rst_n)
match <= 1’b0;
else
match <= {data_lock[2:0], data} == 4’b0110 & data_valid;
end
// always @(*) begin
// if(!rst_n)
// match = 1’b0;
// else
// match = data_lock == 4’b0110; //data_valid 无效后,不是一个clock
// end文章来源:https://www.toymoban.com/news/detail-824776.html
endmodule文章来源地址https://www.toymoban.com/news/detail-824776.html
到了这里,关于VL28 输入序列不连续的序列检测的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!