|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
Code : in VHDLlibrary IEEE;use IEEE.std_logic_1164.all;use IEEE.std_logic_arith.all;entity mult is port ( Clk : in STD_LOGIC; Start : in STD_LOGIC; Done : out STD_LOGIC; Dout : out INTEGER);end mult;architecture sequential of mult is signal Count0, Count1 : UNSIGNED (8 downto 0):="000000000"; signal Count2, Count3 : UNSIGNED (8 downto 0):="000000000"; signal A, B, P, Sum : INTEGER;begin process(Clk) type STATE_VALUE is (S0, S1, S2, S3, S4, S5, S6, S7, S8); type RF is array (0 to 7, 0 to 7) of INTEGER; variable i0, j0, k0, k2 : INTEGER; variable i3, j3, k3 : INTEGER; variable State : STATE_VALUE := S0; ---------------------------------------- -- some random numbers for the A matrix ---------------------------------------- variable A_Matrix : RF := ( (1, 1, 2, 3, 4, 5, 6, 7), (8, 9, 0, 1, 2, 3, 4, 5), (6, 7, 8, 9, 1, 3, 4, 5), (4, 1, 2, 3, 4, 5, 6, 7), (8, 9, 0, 1, 2, 3, 4, 5), (6, 7, 8, 9, 1, 3, 4, 5), (7, 1, 2, 3, 4, 5, 6, 7), (8, 9, 0, 1, 2, 3, 4, 5)); ---------------------------------------- -- some random numbers for the B matrix ---------------------------------------- variable B_Matrix : RF := ( (6, 7, 8, 9, 1, 3, 4, 9), (0, 1, 2, 3, 4, 5, 6, 7), (8, 9, 0, 1, 2, 3, 4, 8), (0, 1, 2, 3, 4, 5, 6, 7), (6, 7, 8, 9, 1, 3, 4, 5), (8, 9, 0, 1, 2, 3, 4, 5), (8, 9, 0, 1, 2, 3, 4, 5), (0, 1, 2, 3, 4, 5, 6, 7)); variable C_Matrix : RF; begin if (Clk'event and Clk = '1') then i0 := CONV_INTEGER( Count0( 8 downto 6 ) ); j0 := CONV_INTEGER( Count0( 5 downto 3 ) ); k0 := CONV_INTEGER( Count0( 2 downto 0 ) ); k2 := CONV_INTEGER( Count2( 2 downto 0 ) ); i3 := CONV_INTEGER( Count3( 8 downto 6 ) ); j3 := CONV_INTEGER( Count3( 5 downto 3 ) ); k3 := CONV_INTEGER( Count3( 2 downto 0 ) ); case State is ------------------------------------------ -- State S0 (wait for start signal) ------------------------------------------ when S0 => Done <= '0'; Count0 <= "000000000"; if( Start = '1' ) then State := S1; else State := S0; end if; ------------------------------------------- -- State S1 (filling up of pipeline) ------------------------------------------- when S1 => A <= A_Matrix(i0, k0); B <= B_Matrix(k0, j0); Count1 <= Count0; Count0 <= Count0 + 1; State := S2; --------------------------------------- -- State S2 (more of filling up) --------------------------------------- when S2 => A <= A_Matrix(i0, k0); B <= B_Matrix(k0, j0); P <= A * B; Count2 <= Count1; Count1 <= Count0; Count0 <= Count0 + 1; State := S3; ------------------------------------------- -- State S3 (even more of filling up) ------------------------------------------- when S3 => A <= A_Matrix(i0, k0); B <= B_Matrix(k0, j0); P <= A * B; if (k2 = 0) then Sum <= P; else Sum <= Sum + P; end if; Count3 <= Count2; Count2 <= Count1; Count1 <= Count0; Count0 <= Count0 + 1; State := S4; ------------------------------------------------- -- State S4 (pipeline full, complete work) ------------------------------------------------- when S4 => A <= A_Matrix(i0, k0); B <= B_Matrix(k0, j0); P <= A * B; if (k2 = 0) then Sum <= P; else Sum <= Sum + P; end if; if (k3 = 7) then C_Matrix(i3, j3) := Sum; end if; Count3 <= Count2; Count2 <= Count1; Count1 <= Count0; ---------------------------------- -- check if all initiations done ---------------------------------- if (Count0 = 511) then State := S5; else State := S4; Count0 <= Count0 + 1; end if; ------------------------------------------------ -- State S5 (start flushing the pipeline) ------------------------------------------------ when S5 => P <= A * B; if (k2 = 0) then Sum <= P; else Sum <= Sum + P; end if; if (k3 = 7) then C_Matrix(i3, j3) := Sum; end if; Count3 <= Count2; Count2 <= Count1; State := S6; ------------------------------------- -- State S6 (more of flushing) ------------------------------------- when S6 => if (k2 = 0) then Sum <= P; else Sum <= Sum + P; end if; if (k3 = 7) then C_Matrix(i3, j3) := Sum; end if; Count3 <= Count2; State := S7; ------------------------------------------- -- State S7 (completion of flushing) ------------------------------------------- when S7 => if (k3 = 7) then C_Matrix(i3, j3) := Sum; end if; State := S8; Count0 <= "000000000"; Done <= '1'; ------------------------------------ -- State S8 (output the data) ------------------------------------ when S8 => if( Count0 = 63 ) then Count0 <= "000000000"; State := S0; else Count0 <= Count0 + 1; State := S8; end if; Dout <= C_Matrix( j0, k0 ); end case; end if; end process;end sequential; |
|