筆記:以C/C++概念來說正確的VHDL除頻器
我昨天還為下面這樣的寫法而驚呼,想說這樣都能Compile,那VHDL是否可寫遞迴? 這個程式碼是一個除頻器,我希望能夠利用 generic 於 Compile time 動態改變程式碼內容的特性,進而造就一個"只"需要指定數的數字,就能動態的計算出數字所需要的 bit 數目,如此一來,我只需要在使用component的時候,設定數數,就能完成除頻器,好用的不得了。 VHDL程式碼 01 library IEEE; 02 use ieee.std_logic_1164.all; 03 use ieee.numeric_std.all; 04 05 entity Vhdl1 is 06 generic(COUNT_NUMBER : integer := 16); 07 port(clk: in std_logic; 08 clkout: out std_logic); 09 end entity Vhdl1; 10 11 architecture main of Vhdl1 is 12 function getCountNumberBits (number :integer) 13 return integer is 14 variable c: integer := 0; 15 variable n: integer := number; 16 begin 17 while n /= 0 loop 18 n := n /2; 19 c := c + 1; 20 end loop; 21 return c; 22 end function getCountNumberBits; 23 begin 24 count: process(clk) is 25 variable COUNTED_NUMBER: integer := COUNT_NUMBER-1; 26 variable COUNTED_NUMBER_BITS: integer := getCountNumberBits(COUNTED_NUMBER); 27 variable cnt: unsigned(COUNTED_NUMBER_BITS-1 downto 0) := (others=>'0...