Building larger circuit

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<=1'b0;
        else if(q==10'd999)
            q<=1'b0;
        else
            q<=q+1'b1;
    end
endmodule

 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_enatogether, 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);
    reg [3:0] q_temp;
    always @(posedge clk) begin
        if(shift_ena) begin
            q_temp<={q_temp[2:0],data};
        end
        else if(count_ena) begin
            if(q_temp==4'd0) begin
                q_temp<=4'd15;
            end
            else begin
                q_temp<=q_temp-1'b1;
            end
        end
    end
    assign q=q_temp;
endmodule

 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 start_shifting);
    parameter IDLE=3'd0,S1=3'd1,S2=3'd2;
    parameter S3=3'd3,OUT=3'd4;
    reg [2:0] current_state, next_state;
    always @(*) begin
        case(current_state)
            IDLE:next_state=data?S1:IDLE;
            S1:next_state=data?S2:IDLE;
            S2:next_state=data?S2:S3;
            S3:next_state=data?OUT:IDLE;
            OUT:next_state=OUT;
            default:next_state=IDLE;
        endcase
    end
    always @(posedge clk) begin
        if(reset) begin
            current_state<=IDLE;
        end
        else begin
            current_state<=next_state;
        end
    end
    assign start_shifting=current_state==OUT;
endmodule

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

module top_module (
    input clk,
    input reset,      // Synchronous reset
    output shift_ena);
parameter IDLE=2'd0,ENA=2'd1,STOP=2'd2;
    reg [1:0] current_state, next_state;
    reg [2:0] counter;

    always @(*) begin
        case(current_state)
            IDLE:next_state=ENA;
            ENA:next_state=(counter==3'd3)?STOP:ENA;
            STOP:next_state=STOP;
            default:next_state=IDLE;
        endcase
    end

    always @(posedge clk) begin
        if(reset) begin
            current_state<=IDLE;
        end
        else begin
            current_state<=next_state;
        end
    end

    always @(posedge clk) begin
        if(reset) begin
            counter<=3'd0;
        end
        else begin
            case(next_state)
                IDLE:counter<=3'd0;
                ENA:counter<=counter+1'b1;
                STOP:counter<=3'd0;
                default:counter<=3'd0;
            endcase
        end      
    end
    assign shift_ena=current_state==ENA|current_state==IDLE;
endmodule

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 datainput until it resumes searching after everything else is done.

module top_module (
    input clk,
    input reset,      // Synchronous reset
    input data,
    output shift_ena,
    output counting,
    input done_counting,
    output done,
    input ack );
    parameter S=4'd0,S1=4'd1,S11=4'd2,S110=4'd3;
    parameter B0=4'd4,B1=4'd5,B2=4'd6,B3=4'd7;
    parameter Count=4'd8,Wait=4'd9;
    reg [3:0] current_state, next_state;
    always @(*) begin
        case(current_state)
            S:next_state=data?S1:S;
            S1:next_state=data?S11:S;
            S11:next_state=data?S11:S110;
            S110: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;
            default:next_state=S;
        endcase
    end
    always @(posedge clk) begin
        if(reset) begin
            current_state<=S;
        end
        else begin
            current_state<=next_state;
        end
    end
    assign shift_ena=current_state==B0|current_state==B1|current_state==B2|current_state==B3;
    assign counting=current_state==Count;
    assign done=current_state==Wait;
endmodule

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 IDLE = 4'd0, S1 = 4'd1, S2 = 4'd2, S3 = 4'd3;
    parameter C0 = 4'd4, C1 = 4'd5, C2 = 4'd6, C3 = 4'd7;
    parameter Count_1000 = 4'd8, Done = 4'd9;
 
    reg [3:0] current_state, next_state;
    reg [15:0] num;
    reg [3:0] delay;
    reg [3:0] already_count;
    wire count_state;
    assign count_state = (num == (delay + 1'b1)*1000) ? 1'b1 : 1'b0;
    always @(*) begin
        if(num <= 16'd1000) begin
            already_count = 4'd0;
        end
        else if(num > 16'd1000 && num <= 16'd2000) begin
            already_count = 4'd1;
        end
        else if(num > 16'd2000 && num <= 16'd3000) begin
            already_count = 4'd2;
        end
        else if(num > 16'd3000 && num <= 16'd4000) begin
            already_count = 4'd3;
        end
        else if(num > 16'd4000 && num <= 16'd5000) begin
            already_count = 4'd4;
        end
        else if(num > 16'd5000 && num <= 16'd6000) begin
            already_count = 4'd5;
        end
        else if(num > 16'd6000 && num <= 16'd7000) begin
            already_count = 4'd6;
        end
        else if(num > 16'd7000 && num <= 16'd8000) begin
            already_count = 4'd7;
        end
        else if(num > 16'd8000 && num <= 16'd9000) begin
            already_count = 4'd8;
        end
        else if(num > 16'd9000 && num <= 16'd10000) begin
            already_count = 4'd9;
        end
        else if(num > 16'd10000 && num <= 16'd11000) begin
            already_count = 4'd10;
        end
        else if(num > 16'd11000 && num <= 16'd12000) begin
            already_count = 4'd11;
        end
        else if(num > 16'd12000 && num <= 16'd13000) begin
            already_count = 4'd12;
        end
        else if(num > 16'd13000 && num <= 16'd14000) begin
            already_count = 4'd13;
        end
        else if(num > 16'd14000 && num <= 16'd15000) begin
            already_count = 4'd14;
        end
        else begin
            already_count = 4'd15;
        end
    end

    always @(posedge clk) begin
        if(reset) begin
            num <= 16'd0;
        end
        else if(next_state == Done) begin
            num <= 16'd0;
        end
        else if(next_state == Count_1000) begin
            num <= num + 16'd1;
        end
    end

    always @(*) begin
        case(current_state)
            IDLE:next_state = data ? S1 : IDLE;
            S1:next_state = data ? S2 : IDLE;
            S2:next_state = data ? S2 : S3;
            S3:next_state = data ? C0 : IDLE;
            C0:begin
                        next_state = C1;
                        delay[3] = data;
            end         
            C1:begin
                        next_state = C2;    
                        delay[2] = data;
            end         
            C2:begin
                        next_state = C3;    
                        delay[1] = data;
            end         
            C3:begin
                        next_state = Count_1000;    
                        delay[0] = data;
            end         
            Count_1000: next_state = count_state ? Done : Count_1000;
            Done:       next_state = ack ? IDLE : Done;
            default:    next_state = IDLE;
        endcase
    end
    always @(posedge clk) begin
        if(reset) begin
            current_state <= IDLE;
        end
        else begin
            current_state <= next_state;
        end
    end
    assign count = (current_state == Count_1000) ? (delay - already_count) : 4'd0;
    assign counting = (current_state == Count_1000);
    assign done = current_state == Done;
endmodule

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

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
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=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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值