1. Good Modelsim Video Tutorials can be found at:
http://www.innofour.com/News/Multimedia/Series-of-ModelSim-Demonstrations
http://www.innofour.com/News/Multimedia/Series-of-ModelSim-Demonstrations
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.std_logic_unsigned.all;
entity CounterSTD is
port
(
clk,rst : in std_logic;
counter : out std_logic_vector(3 downto 0)
);
end CounterSTD;
architecture Behavioral of CounterSTD is
signal count : std_logic_vector(3 downto 0);
begin
counter0 : process (clk, rst) is
begin
if rst = '1' then
count<= (others => '0');
elsif rising_edge(clk) then
count <= count + 1;
end if;
end process counter0;
counter <= count;
end Behavioral;
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity counterwithsyncreset is
port (
clk : in std_logic;
rst : in std_logic;
counter: out std_logic_vector(3 downto 0)
);
end entity counterwithsyncreset;
architecture RTL of counterwithsyncreset is
signal count: std_logic_vector(3 downto 0);
begin
counter0 : process (clk) is
begin
if rising_edge(clk) then
if rst = '1' then
count <= ((others => '0'));
else
count <= count + 1;
end if;
end if;
end process counter0;
counter <= count;
end architecture RTL;
library ieee;
use ieee.std_logic_1164.all;
entity counterintegersyncreset is
port (
clk : in std_logic;
rst : in std_logic;
counter: out integer range 0 to 10
);
end entity counterintegersyncreset;
architecture RTL of counterintegersyncreset is
signal count: integer range 0 to 10;
begin
counter0 : process (clk) is
begin
if rising_edge(clk) then
if rst = '1' then
count <= 0;
else
count <= count +1 ;
end if;
end if;
end process counter0;
counter <= count;
end architecture RTL;
I use std_logic at top level ports, but internally I was using ranged integers all over the place
unsigned
and signed
.ieee.numeric_std
, not ieee.std_logic_arith
aren't you?integer
s where I can, but if I explicitly want "roll-over n-bit counters", I tend to use unsigned
.library ieee;
use ieee.std_logic_1164.all;
entity counterintegersyncreset is
port (
clk : in std_logic;
rst : in std_logic;
counter: out integer range 0 to 10
);
end entity counterintegersyncreset;
architecture RTL of counterintegersyncreset is
signal count: integer range 0 to 10;
begin
counter0 : process (clk) is
begin
if rising_edge(clk) then
if rst = '1' then
count <= 0;
else
if (count = 10) then
count <= 0;
end if;
count <= count +1 ;
end if;
end if;
end process counter0;
counter <= count;
end architecture RTL;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity DFlipFlop is
port
(
clk,D,ResetN : in std_logic;
Q : out std_logic
);
end DFlipFlop;
architecture Behavioral of DFlipFlop is
begin
DFlipFlop : process (clk,ResetN)
begin
if (resetn = '0') then
Q <= '0';
elsif rising_edge(clk) then
Q <= D;
end if;
--
end process DFlipFlop;
end Behavioral;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity DFlipFlop is
port
(
clk,D,ResetN : in std_logic;
Q : out std_logic
);
end DFlipFlop;
architecture Behavioral of DFlipFlop is
begin
DFlipFlop : process (clk,ResetN)
begin
if rising_edge(clk) then
if (ResetN = '0') then
Q <='0';
else
Q <= D;
end if;
end if;
end process DFlipFlop;
end Behavioral;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity encoder4to2 is
port
(
ilines : in std_logic_vector(3 downto 0);
dout : out std_logic_vector(1 downto 0);
valid : out std_logic
);
end encoder4to2;
architecture Behavioral of encoder4to2 is
begin
encoder4 : with ilines select
dout <=
"00" when "0001",
"01" when "0010",
"10" when "0100",
"11" when "1000",
"11" when others;
validout : with ilines select
valid <=
'1' when "0001",
'1' when "0010",
'1' when "0100",
'1' when "1000",
'0' when others;
end Behavioral;
library ieee;
use ieee.std_logic_1164.all;
use IEEE.Std_Logic_Unsigned.all ;
use IEEE.Std_Logic_Arith.all;
use work.simpkg.all;
entity tb_encoder4to2 is
end tb_encoder4to2;
architecture Test of tb_encoder4to2 is
-- Declare/Import DUT
component encoder4to2
port(ilines : in std_logic_vector(3 downto 0);
dout : out std_logic_vector(1 downto 0);
valid : out std_logic);
end component encoder4to2;
-- Declare Internal Signals which act as Input and Output to DUT
signal clk : std_logic; --- clock driver signal
-- Constants ---
constant period : time := 10 ns;
constant update : time := 1 ns;
signal ilines : std_logic_vector(3 downto 0);
signal dout : std_logic_vector(1 downto 0);
signal valid : std_logic;
--Start of Test Bench
begin
-- Define the Clock required for simulation --
testbenchclk : process
begin
clk <= '0';
wait for period / 2;
clk <= '1';
wait for period / 2;
end process testbenchclk;
-- Instantiate DUT -----
dut:component encoder4to2
port map(ilines => ilines,
dout => dout,
valid => valid);
-- Stim Loop --
stim_loop : process
variable expected:std_logic_vector(1 downto 0);
begin
for i in 0 to 15 loop
-- Apply Stim to DUT Inputs & update
ilines <= CONV_STD_LOGIC_VECTOR(i,4);
wait for update ;
-- Equations for expected response
case ilines is
when "0001" => expected := "00";
when "0010" => expected := "01";
when "0100" => expected := "10";
when "1000" => expected := "11";
when others => null;
end case;
--- Check response
assert (expected = dout )
report "Test Failed" & CR &
"dut.stimulus= "& vec2str(ilines) & "; " &"dut.response =" & vec2str(dout) & "; "&"expected =" & vec2str(expected)&"; "
severity error;
-- Next Stim after a delay
wait for period;
end loop;
-- Wait Indefinitely No more Stim
wait;
end process stim_loop;
--End of Test Bench --
end architecture Test;
at 1 ns: Error: Test Failed
dut.stimulus= 0000; dut.response =11; expected =XX;
at 34 ns: Error: Test Failed
dut.stimulus= 0011; dut.response =11; expected =01;
at 56 ns: Error: Test Failed
dut.stimulus= 0101; dut.response =11; expected =10;
at 67 ns: Error: Test Failed
dut.stimulus= 0110; dut.response =11; expected =10;
at 78 ns: Error: Test Failed
dut.stimulus= 0111; dut.response =11; expected =10;
library ieee;
use ieee.std_logic_1164.all;
use IEEE.Std_Logic_Unsigned.all ;
use IEEE.Std_Logic_Arith.all;
use work.simpkg.all;
entity tb_encoder4to2 is
end tb_encoder4to2;
architecture Test of tb_encoder4to2 is
-- Declare/Import DUT
component encoder4to2
port(ilines : in std_logic_vector(3 downto 0);
dout : out std_logic_vector(1 downto 0);
valid : out std_logic);
end component encoder4to2;
-- Declare Internal Signals which act as Input and Output to DUT
signal clk : std_logic; --- clock driver signal
-- Constants ---
constant period : time := 10 ns;
constant update : time := 1 ns;
signal ilines : std_logic_vector(3 downto 0);
signal dout : std_logic_vector(1 downto 0);
signal valid : std_logic;
--Start of Test Bench
begin
-- Define the Clock required for simulation --
testbenchclk : process
begin
clk <= '0';
wait for period / 2;
clk <= '1';
wait for period / 2;
end process testbenchclk;
-- Instantiate DUT -----
dut:component encoder4to2
port map(ilines => ilines,
dout => dout,
valid => valid);
-- Stim Loop --
stim_loop : process
variable expected:std_logic_vector(1 downto 0);
begin
for i in 0 to 15 loop
-- Apply Stim to DUT Inputs & update
ilines <= CONV_STD_LOGIC_VECTOR(i,4);
wait for update ;
-- Equations for expected response
case ilines is
when "0001" => expected := "00";
when "0010" => expected := "01";
when "0100" => expected := "10";
when "1000" => expected := "11";
when others => expected := "11";
end case;
--- Check response
assert (expected = dout )
report "Test Failed" & CR &
"dut.stimulus= "& vec2str(ilines) & "; " &"dut.response =" & vec2str(dout) & "; "&"expected =" & vec2str(expected)&"; "
severity error;
-- Next Stim after a delay
wait for period;
end loop;
-- Wait Indefinitely No more Stim
wait;
end process stim_loop;
--End of Test Bench --
end architecture Test;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity PE4to2 is
port
(
w : in std_logic_vector(3 downto 0);
z : out std_logic_vector(1 downto 0);
valid : out std_logic
);
end PE4to2;
architecture Behavioral of PE4to2 is
begin
pen : with w select
z <=
"00" when "0001",
"01" when "0010",
"01" when "0011",
"10" when "0100",
"10" when "0101",
"10" when "0110",
"10" when "0111",
"11" when others;
validsig : with w select
valid <=
'0' when "0000",
'1' when others;
end Behavioral;
library ieee;
use ieee.std_logic_1164.all;
use IEEE.Std_Logic_Unsigned.all ;
use IEEE.Std_Logic_Arith.all;
use std.textio.all;
use work.simpkg.all;
entity tb_PE4to2 is
end tb_PE4to2;
architecture Test of tb_PE4to2 is
-- Declare/Import DUT
component PE4to2
port(w : in std_logic_vector(3 downto 0);
z : out std_logic_vector(1 downto 0);
valid : out std_logic);
end component PE4to2;
-- Functions
function leading1 (s : std_logic_vector) return integer is
begin
for i in s'range loop
if (s(i) = '1') then
return i; -- Return for loop index
end if;
end loop;
return 0;
end function leading1;
-- Declare Internal Signals which act as Input and Output to DUT
signal clk : std_logic; --- clock driver signal
-- Constants ---
constant period : time := 10 ns;
constant update : time := 1 ns;
signal w : std_logic_vector(3 downto 0);
signal z : std_logic_vector(1 downto 0);
signal valid : std_logic;
--Start of Test Bench
begin
-- Define the Clock required for simulation --
testbenchclk : process
begin
clk <= '0';
wait for period / 2;
clk <= '1';
wait for period / 2;
end process testbenchclk;
-- Instantiate DUT -----
dut:component PE4to2
port map(w => w,
z => z,
valid => valid);
-- Response Equation
-- Stim Loop --
stim_loop : process
variable expected: integer:=0;
begin
for i in 0 to 15 loop
-- Apply Stim to DUT Inputs & update
w <= CONV_STD_LOGIC_VECTOR(i,4);
wait for update ;
--report "Input =" & integer'image(i);
-- Equations for response
expected:= leading1(w);
--- Check response
if ( w /= "0000") then
assert (z = expected)
report "Test Failed " & CR &
"dut.stimulus= "& vec2str(w) & "; " &"dut.response =" & vec2str(z) & "; "&"expected =" & integer'image(expected)&"; "
severity error;
else
-- Input = 0000 is a special where output is don't cares and valid signal should be false
assert (valid = '0')
report "Test failed" & CR &
"dut stimulus = 0000, Expected Valid to be false" & "found expected to be = " & std2str(valid)
severity error;
end if;
-- Next Stim after a delay
wait for period;
end loop;
-- Wait Indefinitely No more Stim
wait;
end process stim_loop;
--End of Test Bench --
end architecture Test;
library ieee;
use ieee.std_logic_1164.all;
entity Decodervvhdl is
port (
w : in std_logic_vector(1 downto 0);
y : out std_logic_vector(3 downto 0);
en : in std_logic
);
end entity Decodervvhdl;
architecture RTL of Decodervvhdl is
signal Enw : std_logic_vector(2 downto 0);
begin
Enw <= en & w ;
decoder : with Enw select
y <=
"0001" when "100",
"0010" when "101",
"0100" when "110",
"1000" when "111",
"0000" when others;
end architecture RTL;
library ieee;
use ieee.std_logic_1164.all;
use IEEE.Std_Logic_Unsigned.all ;
use IEEE.Std_Logic_Arith.all;
use work.simpkg.all;
use std.textio.all;
entity tb_decoder is
end tb_decoder;
architecture Test of tb_decoder is
-- Declare/Import DUT
component Decodervvhdl
port(w : in std_logic_vector(1 downto 0);
y : out std_logic_vector(3 downto 0);
en : in std_logic);
end component Decodervvhdl;
-- Declare Internal Signals which act as Input and Output to DUT
signal clk : std_logic; --- clock driver signal
-- Constants ---
constant period : time := 10 ns;
constant update : time := 1 ns;
signal w : std_logic_vector(1 downto 0);
signal y : std_logic_vector(3 downto 0);
signal en : std_logic;
--Start of Test Bench
begin
-- Define the Clock required for simulation --
testbenchclk : process
begin
clk <= '0';
wait for period / 2;
clk <= '1';
wait for period / 2;
end process testbenchclk;
-- Instantiate DUT -----
dut:component Decodervvhdl
port map(w => w,
y => y,
en => en);
-- Stim Loop --
stim_loop : process
variable expected: integer:=1;
begin
for i in 0 to 3 loop
-- Apply Stim to DUT Inputs & update
w <= CONV_STD_LOGIC_VECTOR(i,2);
en <= '1';
wait for update ;
--- Check DUT response
assert (y = CONV_STD_LOGIC_VECTOR(expected,4))
report "Test Failed" &
"stimulus =" & vec2str(w) &
"dut. response =" & vec2str(y) &
"expected.response="& vec2str(CONV_STD_LOGIC_VECTOR(expected,4))
severity error;
--- Next Expected response
expected := expected*2;
-- Next Stim after a delay
wait for period;
end loop;
wait ;
end process stim_loop;
--End of Test Bench --
end architecture Test;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity SRFlipflop is
port
(
set,reset : in std_logic;
q, q_not : in std_logic
);
end SRFlipflop;
architecture Behavioral of SRFlipflop is
component nor_component
port(a, b : in std_logic;
y : out std_logic);
end component nor_component;
begin
n_1:entity work.nor_component
port map(a => set,
b => q,
y => q_not);
n_2:component nor_component
port map(a => q_not,
b => reset,
y => q);
end Behavioral;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity SRFlipflop is
port
(
set,reset : in std_logic;
q, q_not : inout std_logic
);
end SRFlipflop;
architecture Behavioral of SRFlipflop is
component nor_component
port(a, b : in std_logic;
y : out std_logic);
end component nor_component;
begin
n_1:entity work.nor_component
port map(a => set,
b => q,
y => q_not);
n_2:component nor_component
port map(a => q_not,
b => reset,
y => q);
end Behavioral;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity magcomp is
port
(
A : in std_logic_vector(3 downto 0);
B : in std_logic_vector(3 downto 0);
greater : out std_logic
);
end magcomp;
architecture Behavioral of magcomp is
signal a3 : std_logic;
signal x3 : std_logic;
signal a2 : std_logic;
signal x2 : std_logic;
signal a1 : std_logic;
signal x1 : std_logic;
signal a0 : std_logic;
signal x0 : std_logic;
begin
a3 <= A(3) and not B(3);
a2 <= A(2) and not B(2);
a1 <= A(1) and not B(1);
a0 <= A(0) and not B(0);
x3 <= (a(3) and b(3)) or (not a(3) and not b(3));
x2 <= (a(2) and b(2)) or (not a(2) and not b(2));
x1 <= (a(1) and b(1)) or (not a(1) and not b(1));
x0 <= (a(0) and b(0)) or (not a(0) and not b(0));
greater <= a3 or (x3 and a2) or (x3 and x2 and a1) or (x3 and x2 and x1 and a0);
end Behavioral;
library ieee;
use ieee.std_logic_1164.all;
use IEEE.Std_Logic_Unsigned.all ;
use IEEE.Std_Logic_Arith.all;
use ieee.numeric_std;
use work.magcomp_pkg.all;
entity tb_magcomp is
end tb_magcomp;
architecture Test of tb_magcomp is
-- Declare/Import DUT
-- Declare Internal Signals which act as Input and Output to DUT
signal clk : std_logic; --- clock driver signal
signal A : std_logic_vector(3 downto 0):=(others => '0');
signal B : std_logic_vector(3 downto 0):=(others => '0');
signal greater : std_logic:='0';
signal expout: std_logic;
constant period : time := 10 ns;
constant update : time := 1 ns;
--Start of Test Bench
begin
-- Define the Clock required for simulation --
testbenchclk : process
begin
clk <= '0';
wait for period / 2;
clk <= '1';
wait for period / 2;
end process testbenchclk;
-- Instantiate DUT -----
dut:component magcomp
port map(A => A,
B => B,
greater => greater);
-- Create Stimulus Signal ---
stim : process
begin
wait for 0 ns;
for i in 0 to 15 loop
A<= CONV_STD_LOGIC_VECTOR(i,4);
for j in 0 to 15 loop
B <= CONV_STD_LOGIC_VECTOR(j,4);
wait for 0 ns;
if (a>b) then
expout <= '1';
else
expout <= '0';
end if;
wait for update;
assert(greater = expout) report "incorrect answer" &"A="& integer'image(CONV_INTEGER(unsigned(a)))&"and B=" &integer'image(CONV_INTEGER(unsigned(a))) severity error;
wait for period;
end loop;
end loop;
wait;
--test
end process stim;
--End of Test Bench --
end architecture Test;
---------- Stimulus to DUT-----
stim:process
begin
A <= "00";
B <= "00";
wait for 10 ns;
A<= "01";
B<= "01";
wait for 10 ns;
A <= "10";
B <= "10";
wait for 10 ns;
A <= "11";
B <= "11";
wait;
end process stim;
---------- Stimulus to DUT-----
stim:process
constant period: time := 10 ns;
begin
A <= "00";
B <= "00";
wait for period;
A<= "01";
B<= "01";
wait for period;
A <= "10";
B <= "10";
wait for period;
A <= "11";
B <= "11";
wait;
end process stim;
A<= "01";
B<= "01";
assert (P = "0001") report "TEST FAILED";
An example code is shown below:---------- Stimulus to DUT-----
stim:process
constant period: time := 10 ns;
constant update: time := 1 ns;
begin
-- Case#1:
wait for period;
A <= "00";
B <= "00";
wait for update;
assert (P = "0000")report "TEST FAILED" severity error;
-- Case#2:
wait for period;
A<= "01";
B<= "01";
wait for update;
assert (P = "0001")report "TEST FAILED" severity error;
-- Case#3:
wait for period;
A <= "10";
B <= "10";
wait for update;
assert (P = "0010") report "TEST FAILED" severity error;
-- Case#4:
wait for period;
A <= "11";
B <= "11";
wait for update;
assert (P = "1001") report "TEST FAILED" severity error;
wait;
end process stim;
---------- Stimulus to DUT-----
stim:process
constant period: time := 10 ns;
constant update: time := 1 ns;
begin
--- Apply Stimulus recursively
for i in 0 to 3 loop
wait for period;
A <= CONV_STD_LOGIC_VECTOR(i,2);
B <= CONV_STD_LOGIC_VECTOR(i,2);
wait for update;
assert (P = i*i)report "TEST FAILED" severity error;
end loop;
wait;
end process stim;
library ieee;
use ieee.std_logic_1164.all;
use work.adderpackage.all;
entity mult2x2 is
port (
A,B : in std_logic_vector(1 downto 0);
P : out std_logic_vector (3 downto 0)
);
end entity mult2x2;
architecture RTL of mult2x2 is
signal p00,p10,p01,p11: std_logic;
signal c1,c2:std_logic;
--- Full Adder Circuit
component fa
port(a, b, cin : in std_logic;
s, cout : out std_logic);
end component fa;
-- Half Adder Circuit
component ha
port(a, b : in std_logic;
s, cout : out std_logic);
end component ha;
begin
p00 <= A(0) and B(0);
p01 <= A(1) and B(0);
p10 <= A(0) and B(1);
p11 <= A(1) and B(1);
P(0) <= p00;
S0:component ha
port map(a => p01,
b => p10,
s => P(1),
cout => C1);
S2:component ha
port map(a => C1,
b => p11,
s => P(2),
cout => c2);
P(3) <= c2;
end architecture RTL;
library ieee;
use ieee.std_logic_1164.all;
use work.adderpackage.all;
entity mult2x2 is
port (
A,B : in std_logic_vector(1 downto 0);
P : out std_logic_vector (3 downto 0)
);
end entity mult2x2;
architecture RTL of mult2x2 is
signal p00,p10,p01,p11: std_logic;
signal c1,c2:std_logic;
begin
p00 <= A(0) and B(0);
p01 <= A(1) and B(0);
p10 <= A(0) and B(1);
p11 <= A(1) and B(1);
P(0) <= p00;
S0:component ha
port map(a => p01,
b => p10,
s => P(1),
cout => C1);
S2:component ha
port map(a => C1,
b => p11,
s => P(2),
cout => c2);
P(3) <= c2;
end architecture RTL;
library ieee;
use ieee.std_logic_1164.all;
package adderpackage is
--- Full Adder Circuit
component fa
port(a, b, cin : in std_logic;
s, cout : out std_logic);
end component fa;
-- Half Adder Circuit
component ha
port(a, b : in std_logic;
s, cout : out std_logic);
end component ha;
end package adderpackage;
package body adderpackage is
end package body adderpackage;