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

การออกแบบวงจรหารด้วยภาษา VHDL

การออกแบบวงจรหาร
Cr. http://cpre.kmutnb.ac.th/esl


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


Simulation




หลักการ ให้ r เป็น a ถ้า a น้อยกว่า b จะ shift b ไปทางขวา และ shift left q และ ให้ 0 ไว้ท้าย ถ้า a มากว่าเท่ากับ b จะเอา b ลบ r และ shift left q และ ใส่ 1 ไว้ท้าย และ shift right b เพื่อเลื่อนหลัก

Input :
    clk สัญญาณสาหรับควบคุมการทางานของของระบบ
    a ตัวตั้งขนาด 32 bit
    b ตัวหารขนาด 16 bit
    start ขา บังคับการทางาน เมื่อให้เป็น 1 จะเริ่มการหาร

Output:
   q เป็น ผลหารขนาด 16 bit
   r เป็น เศษผลหารขนาด 16 bit
   done ตัวบอกสถานะการทางานการหาร
Division Code VHDL
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.numeric_std.ALL;
use IEEe.STD_LOGIc_ARITH.ALL;
use IEEE.std_logic_unsigned.all;

entity division is
generic (WIDTH :integer := 32);
 port ( clk : in std_logic;
   START : in std_logic;
   a : in std_logic_vector(WIDTH-1 downto 0);
   b : in std_logic_vector((WIDTH/2)-1 downto 0);
DONE : out std_logic;
   q,r : out std_logic_vector((WIDTH/2)-1 downto 0)
   
 );
end division;

architecture Behavioral of division is
signal temp_b : std_logic_vector(WIDTH-1 downto 0);
signal temp_q : std_logic_vector((WIDTH/2)-1 downto 0);
signal temp_r : std_logic_vector(WIDTH-1 downto 0);
signal done_1 : std_logic := '1';  -- เอาไว้ตรวจสอบดูว่าหารเสร็จหรือยัง
signal count : integer := 0;

begin
 process(clk)
  begin
   if rising_edge(clk) then
    done <= done_1;
    if START = '1' then
     temp_r <= a;
     temp_b <= b&conv_std_logic_vector(0,WIDTH/2);--conv_std_logic_vector แปลงฐาน10 เป็นฐาน2
     done_1 <= '0';  -- done_1เป็น 0 คือกำลังหารอยู่
   
    elsif count = (WIDTH/2)+1 then
     count <= 0;  -- เริ่มนับ countใหม่
     done_1 <= '1';  -- หารเสร็จเเล้ว
    
    elsif temp_r >= temp_b and done_1 = '0' then
     temp_r <= temp_r-temp_b;
     temp_q <= temp_q((WIDTH/2)-2 downto 0)&'1';  -- shift left
     temp_b <= '0'&temp_b(WIDTH-1 downto 1);  -- shift right
     count <= count + 1;
     
    elsif temp_b >= temp_r and done_1 = '0' then
     temp_q <= temp_q((WIDTH/2)-2 downto 0)&'0';  -- shift left
     temp_b <= '0'&temp_b(WIDTH-1 downto 1);  -- shift right
     count <= count + 1;
    
    elsif done_1 = '1' or (temp_r = conv_std_logic_vector(0,WIDTH)) then
     q <= temp_q;
     r <= temp_r((WIDTH/2)-1 downto 0);
    end if;
   end if;
 end process;
end Behavioral;



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

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