VHDL Codes
VHDL Codes
---------------------------------------------------
-- Simple ALU Module (ESD book Figure 2.5)
-- ALU stands for arithmetic logic unit.
-- It perform multiple operations according to
-- the control bits.
-- we use 2's complement subtraction in this example
-- two 2-bit inputs & carry-bit ignored
---------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
---------------------------------------------------
entity ALU is
end ALU;
---------------------------------------------------
process(A,B,Sel)
begin
case Sel is
when "00" =>
Res <= A + B;
when "01" =>
Res <= A + (not B) + 1;
when "10" =>
Res <= A and B;
when "11" =>
Res <= A or B;
when others =>
Res <= "XX";
end case;
end process;
end behv;
RAM MODULE
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
--------------------------------------------------------------
entity SRAM is
generic( width: integer:=4;
depth: integer:=4;
addr: integer:=2);
port( Clock: in std_logic;
Enable: in std_logic;
Read: in std_logic;
Write: in std_logic;
Read_Addr: in std_logic_vector(addr-1 downto 0);
Write_Addr: in std_logic_vector(addr-1 downto 0);
Data_in: in std_logic_vector(width-1 downto 0);
Data_out: out std_logic_vector(width-1 downto 0)
);
end SRAM;
--------------------------------------------------------------
begin
end behav;
ROM MODULE
-- 32*8 ROM module (ESD Book Chapter 5)
-- ROM model has predefined content for read only purpose
--------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity ROM is
port( Clock : in std_logic;
Reset : in std_logic;
Enable : in std_logic;
Read : in std_logic;
Address : in std_logic_vector(4 downto 0);
Data_out: out std_logic_vector(7 downto 0)
);
end ROM;
--------------------------------------------------------------
begin
process(Clock, Reset, Read, Address)
begin
if( Reset = '1' ) then
Data_out <= "ZZZZZZZZ";
elsif( Clock'event and Clock = '1' ) then
if Enable = '1' then
if( Read = '1' ) then
Data_out <= Content(conv_integer(Address));
else
Data_out <= "ZZZZZZZZ";
end if;
end if;
end if;
end process;
end Behav;
GCD CALCULATOR
-- Behvaior Design of GCD calculator
----------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use work.all;
-----------------------------------------------------
entity gcd1 is
port( Data_X: in unsigned(3 downto 0);
Data_Y: in unsigned(3 downto 0);
Data_out: out unsigned(3 downto 0)
);
end gcd1;
for i in 0 to 15 loop
if (tmp_X/=tmp_Y) then
if (tmp_X < tmp_Y) then
tmp_Y := tmp_Y - tmp_X;
else
tmp_X := tmp_X - tmp_Y;
end if;
else
Data_out <= tmp_X;
end if;
end loop;
end process;
end behv;
FSM CODE
-- GCD design using FSMD (ESD book Figure 2.9)
-- GCD algorithm behavior modeling (GCD.vhd)
-- the calculator has two 4-bit inputs and one output
--
-- NOTE: idle state required to obtain
-- the correct synthesis results
--------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use work.all;
--------------------------------------------------------------
entity gcd is
--------------------------------------------------------------
process(rst, clk)
begin
end process;
end FSMD;