[HDLBits] Exams/ece241 2013 q4

[HDLBits] Exams/ece241 2013 q4_第1张图片

Also include an active-high synchronous reset that resets the state machine to a state equivalent to if the water level had been low for a long time (no sensors asserted, and all four outputs asserted).

module top_module (
    input clk,
    input reset,
    input [3:1] s,
    output fr3,
    output fr2,
    output fr1,
    output dfr
); 
    /*这里不用传统的传感器作为基本状态单元,而是传感器的状态相比上次(有差异时)的变化六种变化作为基础单元。
    	这里共6种:000->001,001->011,011->111,111->011,011->001,001->000
        将000、001、011、111分别定义为abcd,用*to*表示转移。
        每次状态变化都可能是上升或不变或下降,根据不同情况给出状态转移。
        题外话,为什么不考虑111->000这种情况呢,因为进水阀开关的决定因素是当前水位
        比上一次变化时水位降低了还是升高了,只要能看出来升高还是降低即可。
    */	
	parameter atob=0,btoc=1,ctod=2,dtoc=3,ctob=4,btoa=5;
    reg [2:0]state,next;
    
    always@(*) begin
        case(state)
            atob:next<=s[2]?btoc:(s[1]?atob:btoa);
            btoc:next<=s[3]?ctod:(s[2]?btoc:ctob);
            ctod:next<=s[3]?ctod:dtoc;
            dtoc:next<=s[3]?ctod:(s[2]?dtoc:ctob);
            ctob:next<=s[2]?btoc:(s[1]?ctob:btoa);
            btoa:next<=s[1]?atob:btoa;
        endcase
    end
    //这里由于状态已经考虑了传感器在之前时间的变化,故这里是要写水位控制的next
    always@(posedge clk) begin
        if(reset)
            state<=btoa;
        else
            state<=next;           
    end
    always@(*) begin
        case(state)
            atob:{fr3,fr2,fr1,dfr}=4'b0110;
            btoc:{fr3,fr2,fr1,dfr}=4'b0010;
            ctod:{fr3,fr2,fr1,dfr}=4'b0000;
            dtoc:{fr3,fr2,fr1,dfr}=4'b0011;
            ctob:{fr3,fr2,fr1,dfr}=4'b0111;
            btoa:{fr3,fr2,fr1,dfr}=4'b1111;
        endcase
    end
    
    
endmodule

你可能感兴趣的:(HDLBits,fpga开发,fpga,verilog)