วันศุกร์ที่ 4 มีนาคม พ.ศ. 2559

การออกแบบวงวรคูณ โดยภาษา VHDL

การออกแบบวงวรคูณ
 cr. http://cpre.kmutnb.ac.th/esl

cr. http://cpre.kmutnb.ac.th/esl








หลักการ เอา ตัวขวาสุดของ a ถ้าเป็น 1 ให้ เอา b มาบวกกับ product ถ้า a ตัวขวาสุดเป็น 0 ก็ไม่ต้องมาบวกกับ product แล้ว shift a ไปทางขา shift b ไปทางซ้าย


Input :
clk สัญญาณสาหรับควบคุมการทางานของของระบบ
a เลขฐาน2ขนาด 16 bit
b เลขฐาน2ขนาด 16 bit
 START ตัวบังคับการทางาน

Output:
P ผลการคูณ
DONE ตัวบอกการทางานการคูณ


                                                              Multiplication CODE VHDL
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.numeric_std.all;
use ieee.std_logic_unsigned.all;


entity mul is
Port (
                 clk : in std_logic;
                  a ,b: in std_logic_vector(15 downto 0);
                 START : in std_logic;
                   P : out std_logic_vector(31 downto 0);
                   DONE : out std_logic);
 end mul;
architecture Behavioral of mul is
signal keepA,keepB : std_logic_vector(31 downto 0) ; --รับค่า a และ b
signal count : integer := 0 ;
signal result : std_logic_vector(31 downto 0) ;
signal product : std_logic_vector(31 downto 0) ;
begin
    process(clk)
          begin
           if (rising_edge(clk) ) then
             if( START = '1' )then
                   keepA <= "0000000000000000"&a; -- ให้keepA รับค่าจาก a
                    keepB <= "0000000000000000"&b; -- ให้keepB รับค่าจาก b
                   DONE <= '0'; -- ให้ done เป้น 0 คือกาลังคูณอยู่
             elsif(keepA (0)='1')then
                    result <= keepB ;
                    product <= result + product;
                    keepA(30 downto 0) <= keepA(31 downto 1);--shift_right
                    keepA(31) <= '0';
                    keepB(31 downto 1) <= keepB(30 downto 0);--shift_left
                    keepB(0) <= '0';
                  count <= count +1 ;
             elsif(keepA (0)='0')then
                      result <= "00000000000000000000000000000000";
                      keepA(30 downto 0) <= keepA(31 downto 1);--shift_right
                      keepA(31) <= '0';
                      keepB(31 downto 1) <=keepB(30 downto 0);--shift_left
                      keepB(0) <= '0';
                      product <= result + product;
              end if;
           end if;
   end process;
P <= product;
end Behavioral;


ไม่มีความคิดเห็น:

แสดงความคิดเห็น