HDLBits学习:Exams/2013 q2bfsm

题目:

Consider a finite state machine that is used to control some type of motor. The FSM has inputs x and y, which come from the motor, and produces outputs f and g, which control the motor. There is also a clock input called clk and a reset input called resetn.

The FSM has to work as follows. As long as the reset input is asserted, the FSM stays in a beginning state, called state A. When the reset signal is de-asserted, then after the next clock edge the FSM has to set the output f to 1 for one clock cycle. Then, the FSM has to monitor the x input. When x has produced the values 1, 0, 1 in three successive clock cycles, then g should be set to 1 on the following clock cycle. While maintaining g = 1 the FSM has to monitor the y input. If y has the value 1 within at most two clock cycles, then the FSM should maintain g = 1 permanently (that is, until reset). But if y does not become 1 within two clock cycles, then the FSM should set g = 0 permanently (until reset).

(The original exam question asked for a state diagram only. But here, implement the FSM.)

AI翻译:

考虑一个用于控制某种类型电机的有限状态机。FSM 的输入 x 和 y 来自电机,并产生输出 f 和 g,用于控制电机。 还有一个称为 clk 的时钟输入和一个名为 resetn 的复位输入。

密克罗尼西亚联邦必须按照以下方式工作。只要复位输入被置位,FSM 就会保持在 起始状态,称为状态 A。当复位信号被取消置位时,则在下一个时钟之后 边沿 FSM 必须将输出 f 设置为 1,以表示一个时钟周期。然后,FSM 必须监控 x 输入。当 x 在三个连续的时钟周期中产生值 1、0、1 时,g 应 在下一个时钟周期设置为 1。在保持 g = 1 的同时,FSM 必须监控 y 输入。如果 y 在最多两个时钟周期内的值为 1,则 FSM 应保持 g = 1 永久(即,直到重置)。但是,如果 y 在两个时钟周期内没有变为 1,则 FSM 应永久设置 g = 0(直到重置)。

(最初的试题只要求提供状态图。但在这里,实施 FSM。

状态转换图:

实现代码如下:

module top_module (
    input clk,
    input resetn,    // active-low synchronous reset
    input x,
    input y,
    output reg f,
    output  g
); 
//状态空间
parameter  A      = 4'd0 ;
parameter  B_0    = 4'd1 ;
parameter  B_1    = 4'd2 ;
parameter  B_2    = 4'd3 ;
parameter  B_3    = 4'd4 ;
parameter  Y_0    = 4'd5 ;
parameter  Y_1    = 4'd6 ;
parameter  Y_00   = 4'd7 ;
parameter  Y_01   = 4'd8 ;
parameter  Y_none = 4'd9 ;

//状态寄存器
reg [3:0] state     ;
reg [3:0] next_state;
reg change_en;//转换使能信号
//采f为高电平的时刻
always@(posedge clk)begin
 if(f)
change_en<=1;
end
//状态如何跳转
    always @(*) begin
    case(state)
A     :next_state=B_0;
B_0   :next_state=(change_en&~f)?(x?B_1 :B_0):B_0 ;//保证采到f为高电平后的下一时钟后周期进行对x的判断
B_1   :next_state=x?B_1 :B_2 ;
B_2   :next_state=x?B_3 :B_0 ;
B_3   :next_state=y?Y_1 :Y_0 ;
Y_0   :next_state=y?Y_01:Y_00;
Y_1   :next_state=Y_1        ;
Y_00  :next_state=Y_none     ;
Y_01  :next_state=Y_01       ;
Y_none:next_state=Y_none     ;
  default:;
  endcase 
end
//状态何时跳转
always @(posedge clk) begin
    if(!resetn) begin
    f    <=0;//初始为低电平
    state<=A;
    end
    else begin
    f    <=(state==A);//使f高电平只维持一个时钟周期(捕捉边沿理论上可行,复杂一点)
    state<=next_state; 
    end
end
//(最多两个时钟周期)由于数量不多,这里用穷举法更简便,如果超过两个触发器则不合适用穷举法
assign g=(next_state==Y_0)|(next_state==Y_1)|(next_state==Y_01)|(next_state==Y_00);

endmodule

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值