IMO這個要求的屬性:
procedure mul_fixed (
signal a : in unsigned_fixed;
signal b : in unsigned_fixed;
signal c : out unsigned_fixed
) is
constant a_temp : unsigned(a'length - 1 downto 0) := to_unsigned(a);
constant b_temp : unsigned(b'length - 1 downto 0) := to_unsigned(b);
variable result : unsigned(a'length + b'length - 1 downto 0);
-- notice this might be negative if a, b are (? downto +n), which is correct
constant num_fractional : integer := 0 - a'right - b'right;
-- c integral might be bigger than integral/fractional part, make sure we only access valid indices in result
constant result_left : integer := min(result'length - 1, num_fractional + c'left);
constant result_right : integer := max(0 , num_fractional + c'right);
begin
result := a_temp * b_temp;
c <= (others => '0'); -- make sure all bits are defined
c(result_left - num_fractional downto result_right - num_fractional) <= result(result_left downto result_right);
end procedure mul_fixed;
凡
type unsigned_fixed is array(range <>) of std_logic;
和無符號轉換函數存在。
所以你會
...
signal a : unsigned_fixed(3 downto -10); -- 4Q10
signal b : unsigned_fixed(-1 downto -8); -- 0Q8
signal c : unsigned_fixed(3 downto -10); -- 4Q10
mul_fixed(a, b, c);
我知道,所有這些屬性看起來很嚇人的在第一,但我經常發現自己寫癡癡很多包,只是因爲我有不同的數據類型: -/ IMO一個應該花時間思考關於這一次,找出一個通用的解決方案並繼續前進 - 畢竟這就是VHDL的屬性。
- 請注意,我沒有進入到測試環境,而寫這,所以有可能會或可能不會需要到c分配結果的時候要一個類型轉換。
此外,如果可以的話,您至少應該看看定點庫。或者使用VHDL-2008和定點軟件包。
有你看在固定點庫? http://www.eda-stds.org/fphdl/ –
我沒有 - 謝謝你的鏈接。 – blueshift