0

私はモデルシムで VHDL を学び始めたばかりです。

基本的に私が作成しようとしているのは、1 桁のアップ/ダウン BCD カウンター用の合成可能な VHDL コードです。カウンターは、「有効」が「1」の場合にカウントされます。それ以外の場合は同じままです。入力「Init」が初期化されると、カウンターは「Direction」入力の値に応じて 0 または 9 に設定されます。(「Direction」が「1」の場合はアップカウンターです)。

100 if と else を続けて使用する以外に、これを機能させるために使用するより良いツールがあるかどうか疑問に思っています。

これが私のコードです。現在、テストベンチを作成しているため、これが機能するかどうかはまだわかりません。ですから、間違いを見つけた場合は、私に指摘してください。

事前に感謝します。これが私のコードです

entity BCD_counter is
  port(clk, direction, init, enable: in bit;
    q_out: out integer);
end entity BCD_counter;

architecture behaviour of BCD_counter is
  signal q: integer;
begin
  process(clk)
  begin
    if(Clk'event and Clk = '1') then
      if(direction = '1') then -- counting up
        if(init = '1')then --initialize
          q<=0; -- reset to 0
        else
         if(enable = '1')then -- counting          
            if (q<9) then
              q<=q+1;
            else
              q<=0;
            end if;          
         else
          q<=q;
          end if;
        end if;
      elsif(direction = '0') then --counting down
        if(init = '1') then --initialize
          q<=9; --reset to 9
        else
          if(enable = '1') then --counting        
            if (q>0) then
              q<=q-1;
            else
              q<=9;
            end if;       
          else
            q<=q;
          end if;
        end if;
      end if;
    end if;

  end process;  
      q_out <= q;  
end architecture behaviour;
4

3 に答える 3

2

少し書き方が違いますが、こんな風に書きます。

architecture behaviour of BCD_counter is
  signal next_q : integer;
  signal q      : integer;
begin

  pReg : process
  begin  -- process pReg
    wait until clk'event and clk = '1';
    if init = '1' then
      q <= 0;
    else
      q <= next_q;
    end if;
  end process pReg;

  pCount : process (direction, enable, q)
  begin  -- process pCount
    next_q <= q;

    if enable = '1' then
      if direction = '1' then
        next_q <= q + 1;

        if q = 9 then
          next_q <= 0;
        end if;

      else
        next_q <= q - 1;

        if q = 0 then
          next_q <= 9;
        end if;

      end if;
    end if;
  end process pCount;

  q_out <= q;
end architecture behaviour;

注意点:

  • レジスタの動作は、ロジックとは別のプロセスにあります。このスタイルはきれいだと思います。他の人は異なる意見を持っており、通常はnext_blah信号を持つのが好きではないという結論に達します.
  • initリセット信号なので、他のすべてをオーバーライドし、リセット信号を登録プロセスに入れます。
  • カウンター プロセスの最初の行は、デフォルトで実行したいものです。この場合、q の次の状態を現在の状態にすると言います。
  • ifのチェックを外側にしておりenableます。有効になっていない場合は何もしないので、direction最初に確認する必要はありません。
  • 方向条件の各半分の内部では、コード構造は同じです。通常のケースを設定してから、ルールの例外を確認します。例: 上に行く場合、次の状態を q+1 に設定しますが、q が 9 の場合、q <= 0 でオーバーライドします。
  • 比較には>,を使用していません。<これらは よりも高価ですが==問題ありません。

または、登録プロセスに有効にすることもできます。少し短く、おそらくより明確です。また、この関数にレジスタのイネーブル ピンを使用することを期待していることも明らかです。

  pReg : process
  begin  -- process pReg
    wait until clk'event and clk = '1';
    if init = '1' then
      q <= 0;
    else
      if enable = '1' then
        q <= next_q;
      end if;
    end if;
  end process pReg;

  pCount : process (direction, q)
  begin  -- process pCount
    if direction = '1' then
      next_q <= q + 1;

      if q = 9 then
        next_q <= 0;
      end if;
    else
      next_q <= q - 1;

      if q = 0 then
        next_q <= 9;
      end if;
    end if;
  end process pCount;
于 2012-03-27T16:26:03.677 に答える
1

私の頭に浮かぶ唯一のことは、2つを省略できるということです

else
   q<=q;

これは、他の条件がチェックアウトされない場合に暗黙的に行われるためです。

于 2012-03-27T10:49:15.243 に答える
0

私はこれをたくさん行います-modulo_increment入力整数と「モジュラス」整数を取り、ラップアラウンド値を返すという関数を作成しました。

だからあなたはそれからすることができます

q <= modulo_increment(q, 10);
于 2012-03-27T12:42:42.980 に答える