3

異なる幅のパルスを生成する次のコードを書きました。コードが選択行に従って単一のパルスを生成するようにします。選択行が

00 パルス幅 = 1us、01 パルス幅 = 10us 。. 11 パルス幅 = 1000 us

入力クロックは 10 Mhz です。しかし、コードによると、選択ラインの他の値を指定しない場合、連続パルスを取得しています.1つのパルスのみを達成するにはどうすればよいですか?

    module pulse(input wire [1:0] sel , //selection lines s1 s0
        input clk,
        input rst_n,
        output reg flag,    //for checking conditions
        output reg [13:0] Q,    // output of 14 bit counter
        output reg pulse,   //output pulse
        output reg count);  //also for checking conditions

wire flag_d , count_d;

assign flag_d = ( (sel == 2'b00 | sel == 2'b01 | sel == 2'b10 | sel == 2'b11) && count == 1'b0)? 1'b1 : flag;
assign count_d = ( (sel == 2'b00 | sel == 2'b01 | sel == 2'b10 | sel == 2'b11) && count == 1'b0)? 1'b1 : count;

always @(posedge clk , negedge rst_n)
begin
    if(!rst_n)
    begin
        Q <= 14'h0;
        count <= 1'b0;
        pulse <= 1'b0;
        flag <= 1'b0;
    end
    else
    begin

        flag <= flag_d;
        count <= count_d;

        if(flag)
        begin
            case(sel)
            2'b00: Q <= 14'd11;//count from 11 to 1
            2'b01: Q <= 14'd101;//count from 101 to 1
            2'b10: Q <= 14'd1001;//count from 1001 to 1
            2'b11: Q <= 14'd10001;//count from 10001 to 1
            default: Q <= 14'd0;    
            endcase

            flag <= 1'b0;
        end
        else
        begin
            if(Q != 14'h1 && Q != 14'h0)
            begin
                Q <= Q - 14'h1;
                pulse  <= 1'b1;
            end
            else
            begin
                pulse <= 1'b0;
                count <= 1'b0;
            end
        end  
    end
end 
endmodule

このコードは、回路の合成とハードウェアを考慮した適切なコーディング スタイルですか? そうでない場合、どのような変更を適用する必要がありますか?.

4

2 に答える 2

2

flag_dとのポイントがわかりませんでしたcount_d。にも( (sel == 2'b00 | sel == 2'b01 | sel == 2'b10 | sel == 2'b11) && count == 1'b0)簡略化し(count == 1'b0)ます。selX または Z であってはなりません。

次のようなものがもっと欲しいと思います:

reg [13:0] next_Q;
always @* begin
  if (Q==0) begin
    case(sel)
    2'b00 : next_Q = 14'd10;
    2'b01 : next_Q = 14'd100;
    2'b10 : next_Q = 14'd1000;
    2'b11 : next_Q = 14'd10000;
    endcase
  end
  else begin
    next_Q = Q - 1;
  end
end
always @(posedge clk, negedge rst_n) begin
  if (!rst_n) begin
    pulse <= 1'b0;
    Q <= 14'd0;
  end
  else begin
    // if (Q==0) pulse <= !pulse; // high and low pulse will have equal if sel is constant
    pulse <= (Q!=0); // or high pulse based on sel, low is one clk
    Q <= next_Q;
  end
end

作業例: http://www.edaplayground.com/x/GRv

于 2015-08-27T05:51:52.917 に答える
1
         module pulse(input wire [1:0] sel,         // No need for the sel to be wire 
        input   clk,
        input rst_n,
        output reg [13:0] Q,    
        output reg pulse,   
        input   input_stb,                  // Input is valid
        input   output_ack,     
        output  output_stb,         
        output  input_ack);                // 2 Flag model 

reg s_input_ack ;
reg s_output_stb;
 parameter get_inputs         = 4'd0,
        counter               = 4'd1;

always @(posedge clk , negedge rst_n)
begin

case (state)
    get_inputs:
    s_input_ack <= 1;
    if (s_input_ack && input_a_stb) 
    begin
    s_input_ack <= 0;
            case(sel)
            00: Q <= 14'd11;//00000000001010;
            01: Q <= 14'd101;//00000001100100;
            10: Q <= 14'd1001;//00001111101000;
            11: Q <= 14'd10001;//10011100010000;
            default: Q <= 14'd0;    
            endcase
    state <= counter;
    end

    counter:
        begin
        s_output_stb <= 1;
        if (s_output_stb && output_z_ack)
            begin
                        s_output_stb <= 0;

                if(Q != 14'h1 && Q != 14'h0)
                    begin
                    Q <= Q - 1'b1;
                    pulse  <= 1'b1;
                    end
                else
                    begin
                    pulse <= 1'b0;
                    end
                end
        state <= get_inputs;
        end 

endcase
        if(!rst_n)
    begin
        Q <= 14'h0;
        pulse <= 1'b0;
              s_input_ack <= 0;
              s_output_stb <= 0;
    end
      assign input_ack = s_input_ack;

assign output_stb = s_output_stb; end endmodule *Still needs work , add registers and necessary signals accordingly. Will edit at a later point in time

于 2015-08-26T07:26:12.313 に答える