2021年7月3日 星期六

HDLBits/Building Larger Circuits/FSM: One-hot logic equations(Exams/review2015 fsmonehot)

HDLBits/Building Larger Circuits/FSM: One-hot logic equations(Exams/review2015 fsmonehot)


Given the following state machine with 3 inputs, 3 outputs, and 10 states:

Exams review2015 fsmonehot.png

https://hdlbits.01xz.net/wiki/File:Exams_review2015_fsmonehot.png

Derive next-state logic equations and output logic equations by inspection assuming the following one-hot encoding is used: (S, S1, S11, S110, B0, B1, B2, B3, Count, Wait) = (10'b0000000001, 10'b0000000010, 10'b0000000100, ... , 10'b1000000000)

Derive state transition and output logic equations by inspection assuming a one-hot encoding. Implement only the state transition logic and output logic (the combinational logic portion) for this state machine. (The testbench will test with non-one hot inputs to make sure you're not trying to do something more complicated).

Write code that generates the following equations:

  • B3_next -- next-state logic for state B1
  • S_next
  • S1_next
  • Count_next
  • Wait_next
  • done -- output logic
  • counting
  • shift_ena

Derive next-state logic equations and output logic equations by inspection assuming the following one-hot encoding is used: (S, S1, S11, S110, B0, B1, B2, B3, Count, Wait) = (10'b0000000001, 10'b0000000010, 10'b0000000100, … , 10'b1000000000)


Derive state transition and output logic equations by inspection assuming a one-hot encoding. Implement only the state transition logic and output logic (the combinational logic portion) for this state machine. (The testbench will test with non-one hot inputs to make sure you're not trying to do something more complicated).


module top_module(
    input d,
    input done_counting,
    input ack,
    input [9:0] state,    // 10-bit one-hot current state
    output B3_next,
    output S_next,
    output S1_next,
    output Count_next,
    output Wait_next,
    output done,
    output counting,
    output shift_ena
); //
    // You may use these parameters to access state bits using e.g., state[B2] instead of state[6].
    parameter S=0, S1=1, S11=2, S110=3, B0=4, B1=5, B2=6, B3=7, Count=8, Wait=9;
    // assign B3_next = ...;
    // assign S_next = ...;
    assign B3_next = state[B2];//?
    assign S_next = ~d & state[S] | ~d & state[S1] | ~d & state[S110] | ack & state[Wait];
    assign S1_next = d & state[S];
    assign Count_next = state[B3] | ~done_counting & state[Count];
    assign Wait_next = done_counting & state[Count] | ~ack & state[Wait];
    assign done = state[Wait];
    assign counting = state[Count];
    assign shift_ena = state[B0] | state[B1] | state[B2] |state[B3];
endmodule


HDLBits/Building Larger Circuits/The complete timer(Exams/review2015 fancytimer)

HDLBits/Building Larger Circuits/The complete timer(Exams/review2015 fancytimer)

This is the fifth component in a series of five exercises that builds a complex counter out of several smaller circuits. You may wish to do the four previous exercises first (counter, sequence recognizer FSM, FSM delay, and combined FSM).

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.

計時器:①當檢測到特定模式(1101)時啟動;②再移4位以確定延遲時間;③等待計數器完成計數,然後通知用戶並等待用戶確認計時器。

當接收到模式1101時,狀態機必須在4個時鐘周期內輸出shift_ena。此後,狀態機聲明其計數輸出以指示其正在等待計數器,並等待直到輸入done_counting為hi。


data在data輸入引腳上可用。當接收到模式1101時,電路必須隨後移入接下來的4位,即最高有效位在前。這4位確定定時器延遲的持續時間。將其稱為delay [3:0]。


此後,狀態機聲明其計數輸出以指示其正在計數。狀態機必須精確計數(delay [3:0] + 1)* 1000個時鐘周期。例如,delay = 0表示計數1000個周期,而delay = 5表示計數6000個周期。同時輸出當前剩余時間。這應該等於1000個周期的延遲,然後等於1000個周期的delay-1,依此類推,直到1000個周期為0。當電路不計數時,count [3:0]輸出無關緊要。此時,電路完成,以通知用戶計時器已超時,並等待直到輸入ack為1才reset,以查找下一次出現的啟動序列(1101)。電路應重置為開始搜索輸入序列1101的狀態。


這是預期輸入和輸出的示例。“ x”狀態可能會使閱讀有些混亂,它們表明FSM在該周期中不應關心該特定輸入信號。例如,一旦讀取了1101和delay [3:0],電路將不再查看數據輸入,直到在完成所有其他操作後恢覆搜索為止。在此示例中,電路的計數為2000個時鐘周期,因為delay [3:0]值為4'b0001。最後幾個周期以delay [3:0] = 4'b1110開始另一個計數,該計數將計數15000個周期。

module top_module (
    input clk,
    input reset,      // Synchronous reset
    input data,
    output [3:0] count,//輸出當前剩余時間
    output counting,
    output done,
    input ack );//與上一題目相比,shift_ena不需要輸出!
    parameter S = 0, S1 = 1, S2 = 2, S3 = 3, B0 = 4, B1 = 5, B2 = 6, B3 = 7, COUNT = 8, WAIT = 9;
    reg[3:0] state, next_state;
    reg[3:0] delay;
    reg[13:0] cnt_delay;//根據這個保持counting為1
    wire done_counting;
    wire shift_ena;//與上一題目相比,shift_ena不需要輸出!
    //狀態機第一部分
    always@(posedge clk)begin
        if(reset)
            state <= S;
        else
            state <= next_state;
    end
    //狀態機的第二部分,狀態機和上一題目的一致!
    always@(*)begin
        case(state)
            S:next_state=data?S1:S;
            S1:next_state=data?S2:S;
            S2:next_state=data?S2:S3;
            S3:next_state=data?B0:S;
            B0:next_state=B1;
            B1:next_state=B2;
            B2:next_state=B3;
            B3:next_state=COUNT;
            COUNT:next_state=done_counting?WAIT:COUNT;
            WAIT:next_state=ack?S:WAIT;
        endcase
    end
    //shift_ena和上一題目的一致,就是不需要輸出
    //shift_ena拉高,表示已經檢測到1101
    //後面就開始使用移位寄存器采取count數據,
    assign shift_ena = (state == B0 || state == B1 || state == B2 || state == B3);

    //使用移位寄存器采取count數據,
    always@(posedge clk)begin
        if(reset)
            delay <= 0;
        else if(shift_ena)
            delay <= {delay[2:0], data};            
    end
    //使用計數器,控制counting狀態
    always@(posedge clk)begin
        if(reset)
            cnt_delay <= 0;
        else
            case(state)
                COUNT:cnt_delay <= cnt_delay + 1;
                default:cnt_delay <= 0;
            endcase
    end
   //好好理解此處的題意   
    assign done_counting = (cnt_delay == (delay + 1) * 1000 - 1);
    assign count = delay - cnt_delay / 1000;
    assign counting = state == COUNT;
    assign done = state == WAIT;
endmodule

HDLBits/Building Larger Circuits/The complete FSM(Exams/review2015 fsm)

HDLBits/Building Larger Circuits/The complete FSM(Exams/review2015 fsm)

This is the fourth component in a series of five exercises that builds a complex counter out of several smaller circuits. See the final exercise for the overall design.

You may wish to do FSM: Enable shift register and FSM: Sequence recognizer first.

We want to create a timer that:

  1. is started when a particular 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.

In this problem, implement just the finite-state machine that controls the timer. The data path (counters and some comparators) are not included here.

The serial data is available on the data input pin. When the pattern 1101 is received, the state machine must then assert output shift_ena for exactly 4 clock cycles.

After that, the state machine asserts its counting output to indicate it is waiting for the counters, and waits until input done_counting is high.

At that point, the state machine 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 state machine 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 a 1101 pattern is detected, the FSM no longer looks at the data input until it resumes searching after everything else is done.

創建一個計時器,當檢測到序列1101時啟動,接著等待4位,然後計數器完成計數,最後等待用戶確認計數器。大家也看出來了,這道題其實就是前面幾道題目的組合。 


module top_module (
    input clk,
    input reset,      // Synchronous reset
    input data,
    output shift_ena,
    output counting,
    input done_counting,
    output done,
    input ack );

     parameter S0=0,S1=1,S2=2,S3=3;//序列檢測1101的狀態

    parameter S4=4,S5=5,S6=6,S7=7;//S4、S5、S6、S7保持shift_ena為1!

    parameter S8=8;//counting拉高,等待done_counting拉高,然後在下一狀態S9拉低counting

    parameter S9=9;//等待ack拉高時,傳出done信號,並進入S0狀態!


    reg [3:0] state,next;
    always@(posedge clk)
        if(reset)
            state <=S0;    
        else
            state <= next;
    always@(*)
        case(state)
            S0:next=data?S1:S0;
            S1:next=data?S2:S0;
            S2:next=data?S2:S3;
            S3:next=data?S4:S0;
            
            S4:next=S5;
            S5:next=S6;
            S6:next=S7;
            S7:next=S8;
            
            S8:next=done_counting?S9:S8;
            S9:next=ack?S0:S9;
            default:next=S0;
        endcase
    assign shift_ena= (state==S4||state==S5||state==S6||state==S7 ?1:0);
    assign counting=(state==S8?1:0);
    assign done= (state==S9?1:0);
endmodule


HDLBits/Building Larger Circuits/Enable shift register(Exams/review2015 fsmshift)

 HDLBits/Building Larger Circuits/Enable shift register(Exams/review2015 fsmshift)

This is the third component in a series of five exercises that builds a complex counter out of several smaller circuits. See the final exercise for the overall design.

As part of the FSM for controlling the shift register, we want the ability to enable the shift register for exactly 4 clock cycles whenever the proper bit pattern is detected. We handle sequence detection in Exams/review2015_fsmseq, so this portion of the FSM only handles enabling the shift register for 4 cycles.

Whenever the FSM is reset, assert shift_ena for 4 cycles, then 0 forever (until reset).

狀態機定義幾個狀態,然後當reset信號有效,進入狀態S0,reset信號消失就進入S1,然後進入S2、S3、S4,當進入S4狀態,下一個狀態就永遠保持為S4。然後shift_ena信號在狀態S0、S1、S2、S3時有效,這樣就完成了題目。


module top_module (
    input clk,
    input reset,      // Synchronous reset
    output shift_ena);
    parameter S0 = 3'd0, S1 = 3'd1, S2 = 3'd2, S3 = 3'd3, S4 = 3'd4;
    reg [2:0] state;
    reg [2:0] next_state;
    
    always@(posedge clk)begin
        if(reset)begin
            state <= S0;
        end
        else begin
            state <= next_state;
        end
    end
    
    always@(*)begin
        case(state)
            S0:begin
                next_state = S1;
            end
            S1:begin
                next_state = S2;
            end
            S2:begin
                next_state = S3;
            end
            S3:begin
                next_state = S4;
            end
            S4:begin
                next_state = S4;
            end
            default:begin
                next_state = S4;
            end
        endcase
    end
    
    assign shift_ena = (state == S0 || state == S1 || state == S2 || state == S3);
 endmodule

HDLBits/Building Larger Circuits/FSM: Sequence 1101 recognizer(Exams/review2015 fsmseq)

HDLBits/Building Larger Circuits/FSM: Sequence 1101 recognizer(Exams/review2015 fsmseq)

 This is the second component in a series of five exercises that builds a complex counter out of several smaller circuits. See the final exercise for the overall design.

Build a finite-state machine that searches for the sequence 1101 in an input bit stream. When the sequence is found, it should set start_shifting to 1, forever, until reset. Getting stuck in the final state is intended to model going to other states in a bigger FSM that is not yet implemented. We will be extending this FSM in the next few exercises.





module top_module (
    input clk,
    input reset,      // Synchronous reset
    input data,
    output reg start_shifting);
    
    parameter s0=0,s1=1,s2=2,s3=3,s4=4,s5=5;
    reg[2:0] state, next_state;

   always @(posedge clk) begin
        // State flip-flops with asynchronous reset
        if(reset)begin
           state <= s0; 
        end
        else begin
           state <= next_state; 
        end
    end
    always@(*) begin
case (state)
                    s0:    next_state=data?s1:s0;
                    s1:    next_state=data?s2:s0;
                    s2:    next_state=data?s2:s3;
                    s3:    next_state=data?s4:s0;
                    s4:    next_state=data?s2:s0;
                    default:    next_state=s0;
endcase
    end
   always@(posedge clk)
        if(reset)
            start_shifting <= 0;
    else if(next_state==s4) //注意此处的状态是next_state;
            start_shifting <= 1;
endmodule

HDLBits/Building Larger Circuits/4-bit shift register and down counter(Exams/review2015 shiftcount)

 HDLBits/Building Larger Circuits/4-bit shift register and down counter(Exams/review2015 shiftcount)

This is the first component in a series of five exercises that builds a complex counter out of several smaller circuits. See the final exercise for the overall design.

Build a four-bit shift register that also acts as a down counter. Data is shifted in most-significant-bit first when shift_ena is 1. The number currently in the shift register is decremented when count_ena is 1. Since the full system doesn't ever use shift_ena and count_ena together, it does not matter what your circuit does if both control inputs are 1 (This mainly means that it doesn't matter which case gets higher priority).


module top_module (
    input clk,
    input shift_ena,
    input count_ena,
    input data,
    output [3:0] q);


    always @(posedge clk) begin
        if (shift_ena)
                q={q[2:0],data};
        else if (count_ena)
                q=q - 1'b1;   //down count
        else
            q=q;
         
    end
endmodule





HDLBits/Building Larger Circuits/Counter with period 1000(Exams/review2015 count1k)

HDLBits/Building Larger Circuits/Counter with period 1000(Exams/review2015 count1k)

Build a counter that counts from 0 to 999, inclusive, with a period of 1000 cycles. The reset input is synchronous, and should reset the counter to 0.




module top_module (
    input clk,
    input reset,
    output [9:0] q);
    always@(posedge clk) begin
        if (reset)
            q=10'd0;
        else if (q==10'd999)
            q=10'd0;
        else
            q=q+1;
    end
endmodule


  清除項目編譯快取 執行 flutter clean 重新取得套件依賴 執行 flutter pub get 完整重新編譯並執行 flutter run 1. 新增專案套件 (pubspec.yaml) 在 Flutter 專案根目錄下的 pubspec.yaml 中,新增 ...