同步HDLC成帧涉及对数据的连续比特流进行解码,以查找指示帧(数据包)开始和结束的比特模式。恰好看到6个连续的1(即0111110)是一个“标志”,指示帧边界。为了避免数据流意外包含“标志”,发送方在每5个连续1s后插入一个零,接收方必须检测并丢弃该零。如果连续出现7个或更多的1,我们还需要发出错误信号。
创建一个有限状态机来识别这三个序列:
011110:信号位需要丢弃(光盘)。
0111110:标记帧的开始/结束(标记)。
01111111...: 错误(7个或更多的1)(错误)。
重置FSM时,其状态应与之前的输入为0时相同。
状态图如提示:
module top_module(
input clk,
input reset, // Synchronous reset
input in,
output disc,
output flag,
output err);
parameter none=0,one=1,two=2,three=3,four=4,five=5,six=6,discard=7,flag0=8,error=9;
reg [3:0] state,next_state;
always @(posedge clk)
begin
if(reset) state<=0;
else
state<=next_state;
end
always @(*)
begin
case(state)
none:next_state<=(in)?one:none;
one:next_state<=(in)?two:none;
two:next_state<=(in)?three:none;
three:next_state<=(in)?four:none;
four:next_state<=(in)?five:none;
five:next_state<=(in)?six:discard;
six:next_state<=(in)?error:flag0;
discard:next_state<=(in)?one:none;
flag0:next_state<=(in)?one:none;
error:next_state<=(in)?error:none;
endcase
end
assign disc = (state==discard);
assign flag = (state==flag0);
assign err = (state==error);
endmodule
这一题比较简单,状态转换图有了就很简单了,感觉FSM关键就在怎么把图弄清楚了。