习题笔记 Exams/review2015 fancytimer

本文描述了一个电路设计,它接收特定输入模式(1101)启动定时器,根据接收到的4位数据确定延迟时间,然后计数并通知用户。当计时结束,等待用户确认后重置,继续搜索新的开始序列。

We want to create a timer with one input that:

  1. is started when a particular input pattern (1101) is detected,
  2. shifts in 4 more bits to determine the duration to delay,
  3. waits for the counters to finish counting, and
  4. notifies the user and waits for the user to acknowledge the timer.

The serial data is available on the data input pin. When the pattern 1101 is received, the circuit must then shift in the next 4 bits, most-significant-bit first. These 4 bits determine the duration of the timer delay. I'll refer to this as the delay[3:0].

After that, the state machine asserts its counting output to indicate it is counting. The state machine must count for exactly (delay[3:0] + 1) * 1000 clock cycles. e.g., delay=0 means count 1000 cycles, and delay=5 means count 6000 cycles. Also output the current remaining time. This should be equal to delay for 1000 cycles, then delay-1 for 1000 cycles, and so on until it is 0 for 1000 cycles. When the circuit isn't counting, the count[3:0] output is don't-care (whatever value is convenient for you to implement).

At that point, the circuit must assert done to notify the user the timer has timed out, and waits until input ack is 1 before being reset to look for the next occurrence of the start sequence (1101).

The circuit should reset into a state where it begins searching for the input sequence 1101.

Here is an example of the expected inputs and outputs. The 'x' states may be slightly confusing to read. They indicate that the FSM should not care about that particular input signal in that cycle. For example, once the 1101 and delay[3:0] have been read, the circuit no longer looks at the data input until it resumes searching after everything else is done. In this example, the circuit counts for 2000 clock cycles because the delay[3:0] value was 4'b0001. The last few cycles starts another count with delay[3:0] = 4'b1110, which will count for 15000 cycles.

module top_module (
    input clk,
    input reset,      // Synchronous reset
    input data,
    output [3:0] count,
    output counting,
    output done,
    input ack );

    parameter s0=0,s_1=1,s_11=2,s_110=3,sf_1=4,sf_2=5,sf_3=6,sf_4=7,s_count=8,s_done=9;
    reg[3:0] state,next_state;
    
    wire done_counting;
    wire shift_ena;
    wire c_in;

    reg[9:0] count_1000;
    reg[3:0] count_delay;
    reg[3:0] reg_sf;

    
    always @(posedge clk)
    begin if (reset) state <= s0;
          else state <= next_state;
    end

    always @(*)
    begin
        case(state)
        s0:    next_state = (data ? s_1:s0);  
        s_1:   next_state = (data ? s_11:s0);   //s1:1xxx
        s_11:  next_state = (data ? s_11:s_110);   //s2:11xx
        s_110: next_state = (data ? sf_1:s0);   //s3:110x
        sf_1:  next_state = sf_2;               //1101 & shift_ena=1
        sf_2:  next_state = sf_3;
        sf_3:  next_state = sf_4;
        sf_4:  next_state = s_count;     
        s_count: next_state = (done_counting ? s_done : s_count);
        s_done: next_state = (ack ? s0:s_done);

        endcase
    end 

    //reg_sf
    always @(posedge clk)
    begin if(reset) reg_sf <= 0;
          else if(shift_ena) reg_sf <= {reg_sf[2:0],data};
    end

    //1000 count
    always @(posedge clk)
    begin if(reset) count_1000 <= 999;
            else if(state==s_count) 
              begin 
                if(c_in) count_1000 <= 999;
                else count_1000 <= count_1000 - 1 ;
              end
    end

    assign c_in = (count_1000 == 0);

    //delay count
    always @(posedge clk)
      begin if (reset) count_delay <= 0;
          else if (state==sf_4) count_delay <= {reg_sf[2:0],data} ;  
          else if (c_in && count_delay ) count_delay <= count_delay-1;
      end

    assign done_counting = ((count_delay==0) & (count_1000==0));


    assign shift_ena = (state==sf_1)|(state==sf_2)|(state==sf_3)|(state==sf_4);
    assign counting = (state==s_count);
    assign done = (state==s_done);
    assign count = count_delay;



endmodule

在数字逻辑设计中,有限状态机(FSM)是描述时序电路行为的重要工具。根据提供的参考资料[^1],Moore 型状态机的输出仅取决于当前状态,并且该类型的状态机会通过不同的状态来反映不同的输入序列历史。 在实际设计中,状态数目的优化是一个关键问题。一种常见的优化策略是将某些检测逻辑集中在特定状态中完成,从而减少整体状态数目[^2]。例如,在一个基于序列检测的任务中,可以将对特定输入序列(如 `101`)的检测集中到某个状态中进行,这样虽然减少了状态数量,但可能增加了次态转移逻辑的复杂度。 针对 2014 年某考试中第 3 题关于 FSM 的题目,尽管没有直接提供具体的考题内容,但可以根据常规的 FSM 设计原则推测其大致结构。通常这类题目要求设计一个能够响应特定输入序列的状态机,涉及状态图绘制、状态编码、次态逻辑推导以及输出逻辑实现等步骤。 以下是一个简化的 Verilog 实现示例,用于说明如何构建一个 Moore 型状态机: ```verilog module fsm_example ( input clk, input reset, input x, output reg out ); // 状态定义 typedef enum logic [2:0] { A, B, C, D, E, F } state_t; state_t current_state, next_state; // 状态寄存器 always_ff @(posedge clk or posedge reset) begin if (reset) current_state <= A; else current_state <= next_state; end // 次态逻辑 always_comb begin case (current_state) A: next_state = x ? B : A; B: next_state = x ? C : A; C: next_state = x ? C : D; // 连续三个周期为 101 的部分逻辑 D: next_state = x ? E : F; E: next_state = F; F: next_state = A; default: next_state = A; endcase end // 输出逻辑(Moore 型) always_ff @(posedge clk or posedge reset) begin if (reset) out <= 1'b0; else out <= (current_state == D || current_state == E); end endmodule ``` 此代码片段展示了状态从 `A` 到 `F` 的迁移过程,其中状态 `C` 和 `D` 被用来检测特定输入序列并决定下一步状态跳转。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值