错题集:HDLBits Serial two‘scomplementer (Moore/Mealy FSM)

摘要:

1、独热码编码的状态机,在编写状态复位时要留意,这个容易被忽略。

之前做独热码编码的状态机,系统都默认设置了状态转换,所以我们都是只写了三段式的其中两端,也就是状态转换条件和输出,这次补码器的题三段式全部要自己写,我就遇上了bug。

主要就是复位时,不应该用if(areset)state[0]=1;这样的方式,这样好像是指定了复位时的状态,但其实并没有唯一确定state的值和当前状态,应该用if(areset)state=2'b01;才行。

这样才明确了复位时的唯一状态,否则,系统其实并不知道state[1]是0还是1,也可能认为复位时,既可以是state[0]=1的状态,也可以是state[1]=0的状态,从而报错。

2、我对独热码编码状态机还不熟练,以下对两道题(Moore型和Mealy型)做一个总结。

【Q5a: Serial two's complementer (Moore FSM)】

产生一个Moore型状态机,实现求2的补码的功能(假设输入的都是无符号的负数)

  • 一般编码方式,代码如下:
module top_module (
    input clk,
    input areset,
    input x,
    output z
); 
    parameter [2:0] RES=0,S0=1,S1=2,ONE=3,ZERO=4;
    reg [2:0] state;
    reg [2:0] next_state;
    
    always@(*)begin
        case(state)
            RES: next_state = x?S1:S0;
             S0: next_state = x?S1:S0;
             S1: next_state = x?ZERO:ONE;
            ONE: next_state = x?ZERO:ONE;
           ZERO: next_state = x?ZERO:ONE;
        default: next_state = RES;
        endcase
    end
    
    always@(posedge clk or posedge areset)begin
        if(areset)
            state <= RES;
        else
            state <= next_state;
    end
    
    assign z = (state==S1)||(state==ONE);
        
endmodule
  • 独热码编码方式,代码如下:(两种写法)

写法一:

用assign语句和基本逻辑门,来产生某个次态的所有状态转换条件。

module top_module (
    input clk,
    input areset,
    input x,
    output z
); 
    
    parameter A=0,B=1,C=2,D=3,E=4;
    reg [4:0] state;
    reg [4:0] next_state;
    
    assign next_state[A]=(areset);
    assign next_state[B]=(state[A]&~x)|(state[B]&~x);
    assign next_state[C]=(state[A]&x)|(state[B]&x);
    assign next_state[D]=(state[C]&~x)|(state[D]&~x)|(state[E]&~x);
    assign next_state[E]=(state[C]&x)|(state[D]&x)|(state[E]&x);
    
    always@(posedge clk or posedge areset)begin
        if(areset)
            state <= 2'b00001;
        else
            state <= next_state;
    end
    
    assign z = (state[C])|(state[D]);
        
endmodule

写法二:

用case语句来产生某状态对应的下一个状态。

module top_module (
    input clk,
    input areset,
    input x,
    output z
); 
    parameter RES=5'b00001,S0=5'b00010,S1=5'b00100,ONE=5'b01000,ZERO=5'b10000;
//太久没写代码了,这种写法,一直报错,原来是把位宽写错了,一直错写成了2'b00001
    reg [4:0] state;
    reg [4:0] next_state;
    
    always@(*)begin
        case(state)
            RES: next_state = x?S1:S0;
             S0: next_state = x?S1:S0;
             S1: next_state = x?ZERO:ONE;
            ONE: next_state = x?ZERO:ONE;
           ZERO: next_state = x?ZERO:ONE;
        default: next_state = RES;
        endcase
    end
    
    always@(posedge clk or posedge areset)begin
        if(areset)
            state <= RES;
        else
            state <= next_state;
    end
    
    assign z = (state==S1)||(state==ONE);
        
endmodule

【Q5b: Serial two's complementer (Mealy FSM)】

产生一个Mealy型状态机,实现求2的补码的功能(假设输入的都是无符号的负数)。

  • 独热码编码,代码如下:

module top_module (
    input clk,
    input areset,
    input x,
    output z
); 
    parameter A=0,B=1;
    reg [1:0] state;
    reg [1:0] next_state;
    
    assign next_state[A] = (areset)|(state[A]&~x);
    assign next_state[B] = (state[A]&x)|(state[B]);
    
    always@(posedge clk or posedge areset)begin
        if(areset)
            state <= 2'b01;
        
        else
            state <= next_state;
    end

    assign z = (state[A]&x)|(state[B]&~x);
    
endmodule

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值