2021年5月6日 星期四

HBLbits_Verilog Basic_Exams/review2015 fancytimer

HBLbits_Verilog Basic_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 (countersequence recognizer FSMFSM 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位元數目值delay ,其值(delay +1)*1000作為計數總數。

delay[3:0] = 4'b1110 =14 , (14+1)*1000=15000



module top_module (
    input clk,
    input reset,      // Synchronous reset
    input data,
    output [3:0] count,
    output counting,
    output done,
    input ack );
    parameter [3:0] S=4'd0,S1=4'd1,S11=4'd2,S110=4'd3,B0=4'd4;
    parameter [3:0] B1=4'd5,B2=4'd6,B3=4'd7,COUNT=4'd8,WAIT=4'd9;
    reg [3:0] cs,ns;
    
    always @(posedge clk)begin
        if(reset)
            cs <= S;
        else
            cs <= ns;
    end
    
    always @(*)begin
        ns = S;
        case(cs)
            S:    ns= data? S1:S;
            S1:   ns= data? S11:S;
            S11:  ns= data? S11:S110;
            S110: ns= data? B0:S;
            B0:   ns= B1;
            B1:   ns= B2;
            B2:   ns= B3;
            B3:   ns= COUNT;
            COUNT:ns= (count == 0 && end_cnt)? WAIT:COUNT;
            WAIT: ns= ack? S:WAIT;
            default: ns=S;
        endcase
    end
    
    reg [3:0] delay = 4'd0;
    always @(posedge clk)begin
        if(reset)
            delay <= 4'd0;
        else begin
            case(cs)
                B0:delay<= {delay[2:0],data};
                B1:delay<= {delay[2:0],data};
                B2:delay<= {delay[2:0],data};
                B3:delay<= {delay[2:0],data};
                COUNT:begin
                    if(end_cnt)
                        delay <= delay -1;
                end
            endcase
        end
    end
    
    reg [9:0] cnt;
    wire add_cnt,end_cnt;
    always @(posedge clk)begin
        if(reset)
            cnt <= 10'd0;
        else if(add_cnt)begin
            if(end_cnt)begin
                cnt <=0;
            end
            else begin
                cnt <= cnt + 1'b1;     
            end
        end
    end
    assign add_cnt = (cs == COUNT)&(delay>=0);
    assign end_cnt = add_cnt && (cnt==999);
    assign count   = delay;
    assign counting= (cs==COUNT);
    assign done    = (cs==WAIT);
endmodule


這裡的狀態機必須精確計數(delay [3:0] + 1* 1000個時鐘週期。例如,delay = 0表示計數1000個週期,而delay = 5表示計數6000個週期。同時輸出當前剩餘時間。這應該是等於延遲為1000個迴圈,然後延遲-11000個週期,依此類推,直到它為01000次迴圈。當電路不計數時,count [3:0]輸出無關緊要。

module top_module (
    input clk,
    input reset,      // Synchronous reset
    input data,
    output [3:0] count,
    output counting,
    output done,
    input ack );
 
    parameter S=0, S1=1, S11=2, S110=3, B0=4, B1=5, B2=6, B3=7, COUNT_REG=8, WAIT=9;
    reg [3:0]   current_state;
    reg [3:0]   next_state;
    reg [3:0]   par_in;
   
    reg [15:0]  counter;


    always@(posedge clk)begin
        if(reset)begin
            counter <= 16'd0;
        end
        else if(next_state == WAIT)begin
            counter <= 16'd0;
        end
        else if(next_state == COUNT_REG)begin
            counter <= counter + 1'b1;
        end
    end
   
    reg [3:0]   a;
    always@(*)begin
        if(counter <= 1000)begin
            a = 4'd0;
        end
        if(counter > 1000 && counter <= 2000)begin
            a = 4'd1;
        end
        if(counter > 2000 && counter <= 3000)begin
            a = 4'd2;
        end
        if(counter > 3000 && counter <= 4000)begin
            a = 4'd3;
        end
        if(counter > 4000 && counter <= 5000)begin
            a = 4'd4;
        end
        if(counter > 5000 && counter <= 6000)begin
            a = 4'd5;
        end
        if(counter > 6000 && counter <= 7000)begin
            a = 4'd6;
        end
        if(counter > 7000 && counter <= 8000)begin
            a = 4'd7;
        end
        if(counter > 8000 && counter <= 9000)begin
            a = 4'd8;
        end
        if(counter > 9000 && counter <= 10000)begin
            a = 4'd9;
        end
        if(counter > 10000 && counter <= 11000)begin
            a = 4'd10;
        end
        if(counter > 11000 && counter <= 12000)begin
            a = 4'd11;
        end
        if(counter > 12000 && counter <= 13000)begin
            a = 4'd12;
        end
        if(counter > 13000 && counter <= 14000)begin
            a = 4'd13;
        end
        if(counter > 14000 && counter <= 15000)begin
            a = 4'd14;
        end
        if(counter > 15000 && counter <= 16000)begin
            a = 4'd15;
        end
    end
   
    wire b;
    assign b = (counter == (par_in + 1) * 1000) ? 1'b1 : 1'b0;
   
   
    always@(posedge clk)begin
        if(reset)begin
            current_state <= S;
        end
        else begin
            current_state <= next_state;
        end
    end
   
    always@(*)begin
        case(current_state)
            S:begin
                next_state = data ? S1 : S;
            end
            S1:begin
                next_state = data ? S11 : S;
            end
            S11:begin
                next_state = data ? S11 : S110;
            end
            S110:begin
                next_state = data ? B0 : S;
            end
            B0:begin
                next_state = B1;
                par_in[3] = data;
            end
            B1:begin
                next_state = B2;
                par_in[2] = data;
            end
            B2:begin
                next_state = B3;
                par_in[1] = data;
            end
            B3:begin
                next_state = COUNT_REG;
                par_in[0] = data;
            end
            COUNT_REG:begin
                next_state = b ? WAIT : COUNT_REG;
            end
            WAIT:begin
                next_state = ack ? S : WAIT;
            end
            default:begin
                next_state = S;
            end
        endcase
    end
   
    assign count = (current_state == COUNT_REG) ? (par_in - a) : 4'd0;
    assign counting = (current_state == COUNT_REG);
    assign done = (current_state == WAIT);
   
endmodule

沒有留言:

張貼留言

2024_09 作業3 以Node-Red 為主

 2024_09 作業3  (以Node-Red 為主  Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker  2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...