📄 vhdl主程序的简单注释.txt
字号:
library ieee;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
--Travis Gilbert
-- last modified 21 Febur 2009
-- FP Multiplier (11-bit: 1 bit sign, 7 bits exponent, 3 bits mantissa)
-- this unit takes a and b as inputs, multiplies them, and gives product as output
entity fmul is
port(a4, b4 : in std_logic_vector (10 downto 0);
p4 : out std_logic_vector (10 downto 0);
-- testflag :out std_logic;
ov4: out std_logic);
--underflow:out std_logic);
end fmul;
architecture Behavioral of fmul is
begin
process(a4,b4)
-- variable declaration
variable m1,m2,temp:std_logic_vector (3 downto 0); --temporary storage of mantissa including implicit '1'
variable tempm:std_logic_vector (7 downto 0); --temporary storage of mantissa
variable m:std_logic_vector (2 downto 0); --final mantissa result
variable e:std_logic_vector (6 downto 0); --resultant exponent
variable e1,e2:std_logic_vector(7 downto 0); --temporary storage of exponents
variable tempe:std_logic_vector(7 downto 0); --temporary resultant exponent
variable s,as,bs : std_logic; --final sign bit,s and signs as and bs of a and b
variable bias:std_logic_vector(6 downto 0); --exponent bias
begin
m1 := "1" & a4(2 downto 0); --m1, mantissa of 'a' including implicit '1'
m2 := "1" & b4(2 downto 0); --m1, mantissa of 'b' including implicit '1'
e1 := "0" & a4(9 downto 3); --exponent of a to e1 (add in leading zero to
-- e1 and e2 for extra precision and for
-- overflow detection)
e2 := "0" & b4(9 downto 3); --exponent of b to e2
as:=a4(10); --sign of a
bs:=b4(10); --sign of b
bias:="0111111"; --exponent bias
ov4 <= '0'; --set overflow flag to cleared (no overflow)
tempm:="00000000"; --clear mantissa for calculation
--********** to determine sign of resultant **********--
s := as XOR bs;
--**********multiply the mantissas**********--
--*** NaN and infinity check ***---
if (e1 = "1111111") then --NaN and infinity check (multiplicand)
if(m1 = "1000") then --multiplicand is infinity//此时被乘数无穷大
if(e2 = "1111111" and m2 > "1000") then --multiplier is NaN//此时乘数非数,为特殊指示
e:="1111111"; --NaN * anything = NaN//非数乘任何数都是非数值
m:="100"; -- NaN = X 1111111 100
else --infinity * anything (except NaN) is infinity
e:="1111111"; -- +/- infinity = 0/1 1111111 000
m:="000";
end if;
else -- multiplicand is NaN//被乘数是非数值
e:="1111111"; --NaN * anything = NaN
m:="100"; -- NaN = X 1111111 100
end if;
elsif(e2 = "1111111") then --NaN and infinity check (multiplier)
if(m2 = "1000") then --multiplier is infinity
if(e1 = "1111111" and m1 > "1000") then -- multiplicand is NaN
e:="1111111"; --NaN * anything = NaN
m:="100"; -- NaN = X 1111111 100
else --infinity * anything (except NaN) is infinity
e:="1111111"; -- +/- infinity = 0/1 1111111 000
m:="000";
end if;
else -- multiplier is NaN
e:="1111111"; --NaN * anything = NaN
m:="100"; -- NaN = X 1111111 100
end if;
--*** zero check ***-
elsif (m1 = "1000" and e1 ="00000000") then --change exponent to all 0s as well
tempm:= "00000000"; -- if one value is zero, then output zero//其中一个乘数为零则结果为零
e:="0000000";
--testflag <='1';
elsif(m2 = "1000" and e2 ="00000000") then
tempm:= "00000000"; -- if one value is zero, then output zero //其中一个乘数为零则结果为零
e:="0000000";
--testflag <= '1';
else
tempm := m1 * m2; --determine mantissa
--**********determine the resultant exponent**********--
tempe := e1 + e2;
--*** check for underflow, 2^-63 is the smallest exponent we can have ***--
if( tempe < bias ) then
s:='1';
e:="1111111"; -- error (give -infinity)
m:="000"; -- also needed for error condition
ov4 <= '1';
-- underflow <= '1'; -- set underflow flag//向下溢出
else
tempe := tempe - bias; --now tempe has the result "e1 + e2 - bias"
--these operations result in the normal calculation
-- of the resultant exponent defined as: e = e1 + e2 - bias
--*** check for overflow, 2^64 is the largest exponent we can have ***--
if( tempe > "1111111" ) then
e:="1111111"; -- error (give infinity)
m:="000"; -- also needed for error condition
ov4 <= '1'; -- set overflow flag//上溢出
else --no overflow
e:=tempe(6 downto 0); --cut off our extra bit of precision
end if;
end if;
end if; --from beginning NaN/infinity check
if( tempm(7) = '0' ) then --if bit 7 is 0, then our leading 1 is in bit 6
-- this is because the smallest mantissa is 1000
-- and 1000 * 1000 = 0100 0000
-- this means that our leading one will never be
-- in a lower bit than bit 6
m := tempm(5 downto 3); -- take off leading one since it's implied
-- and cut off extra precision from the multiplication
-- testflag <= '1';
else
m := tempm(6 downto 4); --take off leading one since it's implied
-- and cut off extra precision from the multiplication
e:=e+1; --increment exponent if leading one is in bit 7
-- this is because 1.111 * 1.000 = 11.110000 so we have
-- to shift our exponent to put the decimal in the right
-- place (1.1110000 instead of 11.110000)
--testflag <= '1';
end if;
--**********assemble final result**********--
p4 <= s & e & m;
end process;
end Behavioral;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -