4

単純なフローチャートstate machineVerilogコードに変換しようとしています。しかし、私はどういうわけか以下にこだわっています.Verilogの知識がほとんどないので、おそらく何かが欠けています.

0 and 1ステートマシンは、sのカウントが13 で割り切れる場合 (または単純に、数値 1 の 3 回があった場合)の入力ストリームを検出します。

ここに画像の説明を入力

module example (
  input clk,
  input rst,
  input input1,
  output output
);

reg state;
reg nextstate;

localparam state2 = 3'd0;
localparam state1 = 3'd1;
localparam state0 = 3'd2;

always @(posedge clk or posedge rst) begin
  if (rst)
    state <= state0;
  else
    state <= nextstate;
end

always @* begin
  case(state)
    state0: begin
      if(input1)
        nextstate = state1;
      end
    state2: begin
      if(input1)
        nextstate = state0;
      end
    state1: begin
      if(input1)
        nextstate = state2;
      end
    default: nextstate = state0;
  endcase
end

always @* begin
  output1 = state0 & input1;
end

endmodule

わからない:

  • 入力 + 出力をregorとして定義する必要がありますかwire? またはinputですoutput!十分な?

  • のベクトル次元を指定する必要がありreg state, nextstateますか? はいの場合、どのディメンションを選択するかを知るにはどうすればよいですか?

  • のように最後にこの種のアサーションを記述できstate0 & input1ますか? または、使用する必要がありますstate = state0 & input1 = ??-はい、何ですか?

4

3 に答える 3

2

入力と出力をregまたはwireとして定義する必要がありますか? または入力と出力です!十分な?

入力は常にワイヤですが、それらに割り当てないので問題ありません。output reg出力はデフォルトでワイヤですが、代わりにレジスタが必要な場合は宣言することもできます。

reg 状態、nextstate のベクトル次元を指定する必要がありますか? はいの場合、どのディメンションを選択するかを知るにはどうすればよいですか?

0はい、次元を宣言する必要があります。そうしないと、Verilog がすべての状態をまたはに暗黙のうちに切り捨てたときに、設計が壊滅的に失敗します1。状態の幅は、状態名を定義するために使用する localparams の幅と同じにするか、より一般的には幅を にする必要がありますlog2(number of input states)

この種のアサーションを、state0 と input1 のように最後に記述できますか?

これはあなたが望むものではないと思います。State0 は単なる定数です。ステート マシンが state0 にあるかどうかを知りたい場合は、現在の状態を定数 state0 と比較する必要があります。また、ここではビットごとの AND が必要ない可能性が高いため、通常の and を使用し&&ます。次のようにする必要があります。

output = (state == state0) && input1;
于 2013-04-10T17:44:58.153 に答える
1

遅すぎますが、Verilog の学習のバンド ワゴンに乗って、あなたの問い合わせに挑戦することにしました。設計モジュールを作成し、そのためのテスト ベンチも作成しました。これは、期待どおりに機能します。

`timescale 1ns/1ns
`define WIDTH 4
module FSM_Detect_Stream_Top();

    reg in_clk;
    reg in_rst_n;
    reg [`WIDTH-1:0] in_input;

    wire out_ouput;

    FSM Test (.i_clk(in_clk), .i_rst_n(in_rst_n), .i_input(in_input), .o_output(out_output));

    initial
        begin
            in_clk = 1'b0; // clk at t=0
            #1 in_rst_n = 1'b1;
            #2 in_rst_n = 1'b0;
            #5 in_rst_n = 1'b1;
            @ (negedge in_clk)
                in_input = 2'b01;
            @ (posedge in_clk)  
                $display("output:%b ", out_output);
            @ (negedge in_clk)
                in_input = 2'b01;
            @ (posedge in_clk)  
                $display("output:%b ", out_output);
            @ (negedge in_clk)
                in_input = 2'b01;
            @ (posedge in_clk)  
                $display("output:%b ", out_output);
            @ (negedge in_clk)
                in_input = 2'b01;
            @ (posedge in_clk)  
                $display("output:%b ", out_output);
            @ (negedge in_clk)
                in_input = 2'b01;
            @ (posedge in_clk)  
                $display("output:%b ", out_output);
            @ (negedge in_clk)
                in_input = 2'b00;
            @ (posedge in_clk)  
                $display("output:%b ", out_output);
            @ (negedge in_clk)
                in_rst_n = 1'b0;
            @ (posedge in_clk)  
                $display("output:%b ", out_output);
            @ (negedge in_clk)
                in_input = 2'b00; in_rst_n = 1'b1;
            @ (posedge in_clk)  
                $display("output:%b ", out_output);
            $finish;
        end


    // Generating a 20ns width clock pulse with 50% duty cycle
    always
        #10 in_clk = ~in_clk;

endmodule // FSM_Detect_Stream_Top

//*********************************************************************************************
module FSM(input i_clk, input i_rst_n, input [`WIDTH-1:0] i_input, output reg o_output);
//*********************************************************************************************

    parameter S0 = 2'b00;   //FIRST STATE or DEFAULT
    parameter S1 = 2'b01;   //SECOND STATE
    parameter S2 = 2'b10;   //THIRD STATE
    parameter S3 = 2'b11;   //FOURTH STATE

    reg [`WIDTH-3:0] curr_state;
    reg [`WIDTH-3:0] next_state;

    //Sequential Logic for Storing Current State
    always @ (posedge i_clk or negedge i_rst_n) begin
        if(~i_rst_n)
            curr_state <= S0;
        else
            curr_state <= next_state;
        end

    //Combinational Logic for Next State
    always @ (curr_state or i_input)    begin
        case(curr_state)
            S0: begin
                if (i_input == 2'b01)
                    next_state <= S1;
                else
                    next_state <= S0;
            end
            S1: begin
                if (i_input == 2'b01)
                    next_state <= S2;
                else
                    next_state <= S1;
            end
            S2: begin
                if (i_input == 2'b01)
                    next_state <= S3;
                else
                    next_state <= S2;
            end
            S3: begin
                if (i_input == 2'b01)
                    next_state <= S0;
                else
                    next_state <= S3;
            end 
            default: next_state <= S0;
        endcase // curr_state
    end

    // Output Logic
    always @(posedge i_clk) begin
        if (~i_rst_n)
            o_output <= 1'b0;
        else begin
            case(curr_state)    
                S0: begin
                    if (i_input == 2'b01)
                        o_output <= 1'b1;
                    else
                        o_output <= 1'b1;
                    end
                S1: begin
                    if (i_input == 2'b01)
                        o_output <= 1'b1;
                    else
                        o_output <= 1'b0;
                    end
                S2: begin
                    if (i_input == 2'b01)
                        o_output <= 1'b1;
                    else 
                        o_output <= 1'b0;
                    end
                S3: begin
                    if (i_input == 2'b01)
                        o_output <= 1'b1;
                    else 
                        o_output <= 1'b0;
                    end
                default: o_output <= 1'b0;
            endcase
        end
    end 

endmodule
于 2018-03-18T03:35:35.410 に答える