I was writing VHDL code in order to find the numbers in a set ranging from 0 to 7 which do not have any common divisors with the other numbers in the set. I tried to implement it on BASYS 3 board. It is working on BASYS 3 but when I tried to write a test bench for my code, I got lots of U's and UU's.Why do you think this is the case? How can I write a proper test bench? I'm a beginner so any idea would help.
TOP MODULE:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;
-- Uncomment the following library declaration if instantiating
-- any Xilinx leaf cells in this code.
--library UNISIM;
--use UNISIM.VComponents.all;
entity Top is
Port ( Basys_Clock_Top : in STD_LOGIC;
New_Clock_Top : out std_logic_vector(3 downto 0);
SegDisp_Top : out std_logic_vector(6 downto 0);
Binary_Top : out std_logic_vector(3 downto 0);
F : out STD_LOGIC);
end Top;
architecture Behavioral of Top is
--clock component
component NewClock
Port ( New_Clock : out std_logic_vector(3 downto 0);
Basys_Clock : in STD_LOGIC);
end component;
--ssd component
component SSD
Port ( Basys_Clock : in STD_LOGIC;
Binary : in std_logic_vector(3 downto 0);
SegDisplay : out std_logic_vector(6 downto 0));
end component;
--signals
signal X, Y, Z, Cont : std_logic;
signal BCD_Top : std_logic_vector(3 downto 0);
begin
--port maps
NewClockModule : NewClock port map( New_Clock => New_Clock_Top, Basys_Clock => Basys_Clock_Top);
SSDModule : SSD port map( Basys_Clock => Basys_Clock_Top, Binary => BCD_Top, SegDisplay => SegDisp_Top);
--input assignment
New_Clock_Top(0) <= Z;
New_Clock_Top(1) <= Y;
New_Clock_Top(2) <= X;
Binary_Top <= "1110";
F <= Z or ((not X) and Y);
F <= Cont;
process(BCD_Top, Cont)
begin
if(Cont = '1') then
BCD_Top(0) <= Z;
BCD_Top(1) <= Y;
BCD_Top(2) <= X;
BCD_Top(3) <= '0';
else
BCD_Top <= "1111";
end if;
end process;
end Behavioral;
This is the test bench:
TEST BENCH:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;
-- Uncomment the following library declaration if instantiating
-- any Xilinx leaf cells in this code.
--library UNISIM;
--use UNISIM.VComponents.all;
entity TestBench is
-- Port ( );
end TestBench;
architecture Behavioral of TestBench is
component Top
Port ( Basys_Clock_Top : in STD_LOGIC;
New_Clock_Top : out std_logic_vector(3 downto 0);
SegDisp_Top : out std_logic_vector(6 downto 0);
Binary_Top : out std_logic_vector(3 downto 0);
F : out STD_LOGIC);
end component;
--signals
signal Basys_Clock_Top : STD_LOGIC;
signal New_Clock_Top : std_logic_vector(3 downto 0);
signal Binary_Top : std_logic_vector(3 downto 0);
signal SegDisp_Top : std_logic_vector(6 downto 0);
signal F : std_logic;
begin
uut : Top Port Map ( Basys_Clock_Top => Basys_Clock_Top, New_Clock_Top => New_Clock_Top, SegDisp_Top => SegDisp_Top, Binary_Top => Binary_Top, F => F);
stim_proc : process
begin
Basys_Clock_Top <= '0';
wait for 10 ps;
Basys_Clock_Top <= '1';
wait for 10 ps;
Basys_Clock_Top <= '0';
end process;
end Behavioral;
One thing I notice: in your TOP module, X, Y, Z, and Cont are not assigned anything. But you use their values....which will therefore be U
Related
hi i' trying to do a 4 bit ripple carry adder with VHDL. The problem is that i'm trying to do a testbench to simulate it in ModelSim, but it doesn't work. This is the code and also the code reported by ModelSim:
Full adder code:
library ieee;
use ieee.std_logic_1164.all;
entity fullAdder is
port( -- Input of the full-adder
a : in std_logic;
-- Input of the full-adder
b : in std_logic;
-- Carry input
c_i : in std_logic;
-- Output of the full-adder
o : out std_logic;
-- Carry output
c_o : out std_logic
);
end fullAdder;
architecture data_flow of fullAdder is
begin
o <= a xor b xor c_i;
c_o <= (a and b) or (b and c_i) or (c_i and a);
end data_flow;
Ripple carry adder code:
library ieee;
use ieee.std_logic_1164.all;
entity Ripple_Carry_Adder is
Port (
A: in std_logic_vector (3 downto 0);
B:in std_logic_vector (3 downto 0);
Cin:in std_logic;
S:out std_logic_vector(3 downto 0);
Cout:out std_logic
);
end Ripple_Carry_Adder;
architecture data_flow2 of Ripple_Carry_Adder is
component fullAdder
Port(
A:in std_logic;
B:in std_logic;
Cin:in std_logic;
S:out std_logic;
Cout:out std_logic
);
end component;
signal c1,c2,c3:STD_LOGIC;
begin
FA1:fullAdder port map(A(0),B(0), Cin, S(0), c1);
FA2:fullAdder port map(A(1),B(1), c1, S(1), c2);
FA3:fullAdder port map(A(2),B(2), c2, S(2), c3);
FA4:fullAdder port map(A(3),B(3), c3, S(3), Cout);
end data_flow2;
code of Ripple carry adder testbench:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
use IEEE.NUMERIC_STD.ALL;
ENTITY ripple_carry_adder_tb is
end ripple_carry_adder_tb;
ARCHITECTURE behavior OF ripple_carry_adder_tb is
constant T_CLK : time := 10 ns; -- Clock period
constant T_RESET : time := 25 ns; -- Period before the reset deassertion
COMPONENT Ripple_Carry_Adder
PORT (
A:in std_logic_vector(3 downto 0);
B:in std_logic_vector(3 downto 0);
Cin:in std_logic;
S:out std_logic_vector(3 downto 0);
Cout:out std_logic
);
END COMPONENT;
signal A_tb:std_logic_vector(3 downto 0):="0000";
signal B_tb:std_logic_vector(3 downto 0):="0000";
signal Cin_tb:std_logic:='0';
signal S_tb:std_logic_vector(3 downto 0);
signal Cout_tb:std_logic;
signal clk_tb : std_logic := '0'; -- clock signal, intialized to '0'
signal rst_tb : std_logic := '0'; -- reset signal
signal end_sim : std_logic := '1';
BEGIN
clk_tb <= (not(clk_tb) and end_sim) after T_CLK / 2; -- The clock toggles after T_CLK / 2 when end_sim is high. When end_sim is forced low, the clock stops toggling and the simulation ends.
rst_tb <= '1' after T_RESET;
RP_1: Ripple_Carry_Adder PORT MAP(A=>A_tb,B=>B_tb,Cin=>Cin_tb,S=>S_tb,Cout=>Cout_tb);
d_process: process(clk_tb, rst_tb) -- process used to make the testbench signals change synchronously with the rising edge of the clock
variable t : integer := 0; -- variable used to count the clock cycle after the reset
begin
if(rst_tb = '0') then
A_tb <= "0000";
B_tb <= "0000";
Cin_tb<='0';
t := 0;
elsif(rising_edge(clk_tb)) then
A_tb<=A_tb+1;
B_tb<=B_tb+1;
t := t + 1;
if (t>32) then
end_sim <= '0';
end if;
end if;
end process;
END;
and this is errors reported by ModelSim when i trying to start simulation:
# ** Fatal: (vsim-3817) Port "c_i" of entity "fulladder" is not in the component being instantiated.
# Time: 0 ns Iteration: 0 Instance: /ripple_carry_adder_tb/RP_1/FA1 File:
C:/Users/utente/Desktop/full_adder.vhd Line: 11
# FATAL ERROR while loading design
# Error loading design
Why doesn't work? Thanks
I am trying to run post synthesis functional simulation. When i run the code for behavioral simulation, i get the output and everything runs fine. Bu when i run the post synthesis i get the following error:
ERROR: [VRFC 10-3146] binding entity 'rippleadder_nbit' does not have generic 'n' [C:/Users/gauta/Assignment4/Assignment4.srcs/sim_1/new/tb_ripplenbit.vhd:41]
Can someone explain me what i need to do please. I am a novice in Vivado and very confused on how to use this
My Rippleadder Code is:
entity rippleadder_nbit is
generic(n: natural);
Port ( cin_ra : in STD_LOGIC;
a : in STD_LOGIC_VECTOR (n-1 downto 0);
b : in STD_LOGIC_VECTOR (n-1 downto 0);
s_ra : out STD_LOGIC_VECTOR (n-1 downto 0);
cout_ra : out STD_LOGIC);
end rippleadder_nbit;
architecture Behavioral of rippleadder_nbit is
component fulladder port(
x_fa : in STD_LOGIC;
y_fa : in STD_LOGIC;
z_fa : in STD_LOGIC;
s_fa : out STD_LOGIC;
c_fa : out STD_LOGIC);
end component;
signal r: std_logic_vector(n downto 0);
begin
r(0) <= cin_ra;
cout_ra <= r(n);
FA: for i in 0 to n-1 generate
FA_i : fulladder port map(r(i),a(i),b(i),s_ra(i),r(i+1));
end generate;
end Behavioral;
my testbench is as follows:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
use IEEE.NUMERIC_STD.ALL;
-- Uncomment the following library declaration if instantiating
-- any Xilinx leaf cells in this code.
--library UNISIM;
--use UNISIM.VComponents.all;
entity tb_ripplenbit is
-- Port ( s: std_logic_vector(2 downto 0);
-- cout: std_logic);
end tb_ripplenbit;
architecture Behavioral of tb_ripplenbit is
component rippleadder_nbit
generic(n: natural);
Port ( cin_ra : in STD_LOGIC;
a : in STD_LOGIC_VECTOR (n-1 downto 0);
b : in STD_LOGIC_VECTOR (n-1 downto 0);
s_ra : out STD_LOGIC_VECTOR (n-1 downto 0);
cout_ra : out STD_LOGIC);
end component;
signal a,b,sin : STD_LOGIC_VECTOR (3 downto 0);
signal cin,carry_out : std_logic;
constant c : integer :=4;
begin
a <= "0000", "0001" after 50 ns, "0101" after 100ns;
b <= "0010", "0011" after 50 ns, "1010" after 100 ns;
cin <= '1', '0' after 50 ns;
UUT1 : rippleadder_nbit generic map(n => c) port map(cin_ra => cin,a=>a,b=>b,s_ra=>sin,cout_ra =>carry_out);
end Behavioral;
In post-synthesis/post-implementation, the generics(constant) are deleted and usage of those generics are replaced with the constant value
In test bench, you had instance w.r.t to behavioural model(with generic involved) so the same test bench won't be applicable for post-synth/post-implementation simulation
Source: Xilinx Forums
I need to create a simple 8-bit processor that will add and subtract two registers. The result of addition and subtraction must be saved in register A. Data in registers A and B should be entered using the D_IN input.
Then I send register A to the D_OUT output.
Unfortunately, when I try to add these two registers together, I get the error "UUUUUUUU"
Thats my vhdl code
----------------------------------------------------------------------------------
-- Company:
-- Engineer:
--
-- Create Date: 11:43:43 05/27/2020
-- Design Name:
-- Module Name: projekt - Behavioral
-- Project Name:
-- Target Devices:
-- Tool versions:
-- Description:
--
-- Dependencies:
--
-- Revision:
-- Revision 0.01 - File Created
-- Additional Comments:
--
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.std_logic_unsigned.all;
use IEEE.NUMERIC_STD.ALL;
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;
-- Uncomment the following library declaration if instantiating
-- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;
entity projekt is
Port (
S : in STD_LOGIC_VECTOR(3 downto 0);
D_IN : in STD_LOGIC_VECTOR(7 downto 0);
D_OUT : out STD_LOGIC_VECTOR(7 downto 0);
A_Out : out STD_LOGIC_VECTOR(7 downto 0);
C : out std_logic
);
end projekt;
architecture Behavioral of projekt is
signal tmp: std_logic_vector (8 downto 0);
signal A : std_logic_vector(7 downto 0);
signal B : std_logic_vector(7 downto 0);
begin
process(A,B,S,D_IN) is
begin
case(S) is
when "0000" =>
A <= A+B;
when "0001" =>
A <= A-B;
when "0010" =>
A <= D_IN;
when "0011" =>
B <= D_IN;
when "0100" =>
B <= A;
when "0101" =>
A <= B;
when "0110" =>
D_OUT <= A;
when others =>
end case;
end process;
tmp <= ('0' & A) + ('0' & B);
C <= tmp(8);
end Behavioral;
And thats my testbench
--------------------------------------------------------------------------------
-- Company:
-- Engineer:
--
-- Create Date: 11:58:50 05/27/2020
-- Design Name:
-- Module Name: /home/ise/projekt/projektTB.vhd
-- Project Name: projekt
-- Target Device:
-- Tool versions:
-- Description:
--
-- VHDL Test Bench Created by ISE for module: projekt
--
-- Dependencies:
--
-- Revision:
-- Revision 0.01 - File Created
-- Additional Comments:
--
-- Notes:
-- This testbench has been automatically generated using types std_logic and
-- std_logic_vector for the ports of the unit under test. Xilinx recommends
-- that these types always be used for the top-level I/O of a design in order
-- to guarantee that the testbench will bind correctly to the post-implementation
-- simulation model.
--------------------------------------------------------------------------------
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--USE ieee.numeric_std.ALL;
ENTITY projektTB IS
END projektTB;
ARCHITECTURE behavior OF projektTB IS
-- Component Declaration for the Unit Under Test (UUT)
COMPONENT projekt
PORT(
S : IN std_logic_vector(3 downto 0);
D_IN : IN std_logic_vector(7 downto 0);
D_OUT : OUT std_logic_vector(7 downto 0);
A_Out : OUT std_logic_vector(7 downto 0);
C : OUT std_logic
);
END COMPONENT;
--Inputs
signal A : std_logic_vector(7 downto 0) := (others => '0');
signal B : std_logic_vector(7 downto 0) := (others => '0');
signal S : std_logic_vector(3 downto 0) := (others => '0');
signal D_IN : std_logic_vector(7 downto 0) := (others => '0');
--Outputs
signal A_Out : std_logic_vector(7 downto 0);
signal D_OUT : std_logic_vector(7 downto 0) := (others => '0');
signal C : std_logic;
-- No clocks detected in port list. Replace <clock> below with
-- appropriate port name
constant clk_period : time := 10 ns;
BEGIN
-- Instantiate the Unit Under Test (UUT)
uut: projekt PORT MAP (
S => S,
D_IN => D_IN,
D_OUT => D_OUT,
A_Out => A_Out,
C => C
);
-- Stimulus process
stim_proc: process
begin
-- hold reset state for 100 ns.
D_IN <= "00000001";
S <= "0010";
wait for 100 ns;
D_IN <= "00000001";
S <= "0011";
wait for 100 ns;
S <= "0000";
wait for 100 ns;
S <= "0110";
-- insert stimulus here
wait;
end process;
END;
When im doing that (result of adding A+B in D_Out, not in A) everything is good. But i need this in A .
----------------------------------------------------------------------------------
-- Company:
-- Engineer:
--
-- Create Date: 11:43:43 05/27/2020
-- Design Name:
-- Module Name: projekt - Behavioral
-- Project Name:
-- Target Devices:
-- Tool versions:
-- Description:
--
-- Dependencies:
--
-- Revision:
-- Revision 0.01 - File Created
-- Additional Comments:
--
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use ieee.std_logic_unsigned.all;
use IEEE.NUMERIC_STD.ALL;
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;
-- Uncomment the following library declaration if instantiating
-- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;
entity projekt is
Port (
S : in STD_LOGIC_VECTOR(3 downto 0);
D_IN : in STD_LOGIC_VECTOR(7 downto 0);
D_OUT : out STD_LOGIC_VECTOR(7 downto 0);
C : out std_logic
);
end projekt;
architecture Behavioral of projekt is
signal tmp: std_logic_vector (8 downto 0);
signal A : std_logic_vector(7 downto 0);
signal B : std_logic_vector(7 downto 0);
signal test : std_logic_vector (7 downto 0);
signal tmpA : integer;
signal tmpB : integer;
signal tmpSum : integer;
begin
process(A,B,S,D_IN,tmpA,tmpB,tmpSum) is
begin
case(S) is
when "0000" =>
D_OUT <= A+B;
when "0001" =>
D_OUT <= A-B;
when "0010" =>
A <= D_IN;
when "0011" =>
B <= D_IN;
when "0100" =>
B <= A;
when "0101" =>
A <= B;
when "0110" =>
D_OUT <= A;
when others =>
end case;
end process;
tmp <= ('0' & A) + ('0' & B);
C <= tmp(8);
end Behavioral;
I have this scheme
I have to write structural VHDL design for it.
So these are my components:
MUX:
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity mux is
port(
A : in STD_LOGIC_VECTOR(7 downto 0);
B : in STD_LOGIC_VECTOR(7 downto 0);
Sel : in bit;
Z : out STD_LOGIC_VECTOR(7 downto 0)
);
end mux;
architecture Beh of mux is
begin
Z <= A when Sel='1'else
B;
end Beh;
REG:
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity reg is
port(
C : STD_LOGIC;
LD : in bit;
Reg_in : in STD_LOGIC_VECTOR(7 downto 0);
R_out : out STD_LOGIC_VECTOR(7 downto 0)
);
end reg;
architecture Beh of reg is
begin
process (C)
begin
if (rising_edge (C)) then
if (LD = '1') then
R_out <= Reg_in;
end if;
end if;
end process;
end Beh;
TOP:
library IEEE;
use IEEE.STD_LOGIC_1164.all;
entity top is
port(
LDA, LDB, S1, S0 : in bit;
CLK : in STD_LOGIC;
X, Y : in STD_LOGIC_VECTOR(7 downto 0);
RB : out STD_LOGIC_VECTOR(7 downto 0)
);
end top;
architecture Beh of top is
signal regB_out : STD_LOGIC_VECTOR(7 downto 0);
signal regA_out : STD_LOGIC_VECTOR(7 downto 0);
signal mux1_res : STD_LOGIC_VECTOR(7 downto 0);
signal mux2_res : STD_LOGIC_VECTOR(7 downto 0);
begin
Mux1: entity mux(Beh)
port map
(
A => X,
B => regB_out,
Sel => S1,
Z => mux1_res
);
RegA: entity reg(Beh)
port map
(
LD => LDA,
C => CLK,
Reg_in => mux1_res,
R_out => regA_out
);
Mux2: entity mux(Beh)
port map
(
A => regA_out,
B => Y,
Sel => S0,
Z => mux2_res
);
RB<=regB_out;
RegB: entity reg(Beh)
port map
(
LD => LDB,
C => CLK,
Reg_in => mux2_res,
R_out => regB_out
);
end Beh;
I am not sure I wrote bind between RB, regB_out and B correctly. And in the waveform when S1 and S0 both equal 0, I get nonsence for 1 CLK period. Like on the screenshot at 600ns '01' on RB shouldn't be there. Can some one help me to find mistakes?
TestBench:
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_signed.all;
use IEEE.numeric_std.all;
ENTITY tbt is
END tbt;
ARCHITECTURE behavior OF tbt IS
COMPONENT TOP
PORT (
CLK : in STD_LOGIC;
LDA, LDB, S1, S0 : in bit;
X, Y : in STD_LOGIC_VECTOR(7 downto 0);
RB : out STD_LOGIC_VECTOR(7 downto 0)
);
END COMPONENT;
signal CLK_sig : std_logic;
signal LDA_sig, LDB_sig, S1_sig, S0_sig : bit :='0';
signal X_sig, Y_sig, RB_sig : std_logic_vector(7 downto 0):="00000000";
constant CLK_period : time := 100 ns;
constant s_per : time := 50 ns;
begin
-------------------------------------------------------------
uut: TOP PORT MAP (
CLK => CLK_sig,
LDA => LDA_sig,
LDB => LDB_sig,
S1 => S1_sig,
S0 => S0_sig,
X => X_sig,
Y => Y_sig,
RB=> RB_sig
);
-------------------------------------------------------------
CLK_process :process
begin
CLK_sig <= '0';
wait for CLK_period/2;
CLK_sig <= '1';
wait for CLK_period/2;
end process;
-------------------------------------------------------------
stim_proc: process
variable itertion_backwards : integer := 255;
variable itertion_forward : integer := 0;
begin
wait for CLK_period;
for itertion_forward in 0 to 254 loop
X_sig <= STD_LOGIC_VECTOR(TO_SIGNED(INTEGER(itertion_forward),8));
Y_sig <= STD_LOGIC_VECTOR(TO_SIGNED(INTEGER(itertion_backwards),8));
wait for CLK_period;
S1_sig<= not S1_sig;
wait for CLK_period;
S0_sig<= not S0_sig;
wait for CLK_period;
LDA_sig<= not LDA_sig;
wait for CLK_period;
LDB_sig<= not LDB_sig;
itertion_backwards := itertion_backwards - 1;
end loop;
wait;
end process;
end;
i have created the structural and the behavioral code for a 1-bit ALU,as well as a control circuit .The control circuit decides the operation that will be conducted between two variables : a,b .
Here is my behavioral part of the code :
library ieee;
use ieee.std_logic_1164.all;
package erotima2 is
-- AND2 declaration
component myAND2
port (outnotA,outnotB: in std_logic; outAND: out std_logic);
end component;
-- OR2 declaration
component myOR2
port (outnotA,outnotB: in std_logic; outOR: out std_logic);
end component;
-- XOR2 declaration
component myXOR2
port (outnotA,outnotB: in std_logic; outXOR: out std_logic);
end component;
--fulladder declaration
component fulladder
port(CarryIn,outnotA,outnotB: in std_logic; sum,CarryOut: out std_logic);
end component;
--Ainvert declaration
component notA
port(a: in std_logic; signala: std_logic_vector(0 downto 0); outnotA: out std_logic);
end component;
--Binvert declaration
component notB
port(b: in std_logic; signalb: std_logic_vector(0 downto 0); outnotB: out std_logic);
end component;
--ControlCircuit declaration--
component ControlCircuit
port (
opcode : in std_logic_vector (2 downto 0);
signala,signalb : out std_logic_vector(0 downto 0);
operation : out std_logic_vector (1 downto 0);
CarryIn: out std_logic);
end component;
--mux4to1 declaration
component mux4to1
port(outAND, outOR, sum, outXOR: in std_logic; operation: in std_logic_vector(1 downto 0); Result: out std_logic);
end component;
end package erotima2;
--2 input AND gate
library ieee;
use ieee.std_logic_1164.all;
entity myAND2 is
port (outnotA,outnotB: in std_logic; outAND: out std_logic);
end myAND2;
architecture model_conc of myAND2 is
begin
outAND<= outnotA and outnotB;
end model_conc;
-- 2 input OR gate
library ieee;
use ieee.std_logic_1164.all;
entity myOR2 is
port (outnotA,outnotB: in std_logic; outOR: out std_logic);
end myOR2;
architecture model_conc2 of myOR2 is
begin
outOR <= outnotA or outnotB;
end model_conc2;
--2 input XOR gate
library ieee;
use ieee.std_logic_1164.all;
entity myXOR2 is
port(outnotA,outnotB: in std_logic; outXOR: out std_logic);
end myXOR2;
architecture model_conc3 of myXOR2 is
begin
outXOR <= outnotA xor outnotB;
end model_conc3;
--3 input full adder
library ieee;
use ieee.std_logic_1164.all;
entity fulladder is
port(CarryIn,outnotA,outnotB: in std_logic; sum,CarryOut: out std_logic);
end fulladder;
architecture model_conc4 of fulladder is
begin
CarryOut <= (outnotB and CarryIn) or (outnotA and CarryIn) or (outnotA and outnotB);
sum <= (outnotA and not outnotB and not CarryIn) or (not outnotA and outnotB and not CarryIn) or (not outnotA and not outnotB and CarryIn) or (outnotA and outnotB and CarryIn);
end model_conc4;
--1 input notA
library ieee;
use ieee.std_logic_1164.all;
entity notA is
port(a: in std_logic; signala:std_logic_vector(0 downto 0); outnotA: out std_logic);
end notA;
architecture model_conc6 of notA is
begin
with signala select
outnotA <= a when "0",
not a when others;
end model_conc6;
--1 input notB
library ieee;
use ieee.std_logic_1164.all;
entity notB is
port(b: in std_logic; signalb: std_logic_vector(0 downto 0); outnotB: out std_logic);
end notB;
architecture model_conc5 of notB is
begin
with signalb select
outnotB <= b when "0",
not b when others;
end model_conc5;
--4 input MUX
library ieee;
use ieee.std_logic_1164.all;
entity mux4to1 is
port(outAND, outOR, sum, outXOR: in std_logic; operation: in std_logic_vector(1 downto 0); Result: out std_logic);
end mux4to1;
architecture model_conc7 of mux4to1 is
begin
with operation select
Result<= outAND when "00",
outOR when "01",
sum when "10",
outXOR when OTHERS;
end model_conc7 ;
The behavioral part defines the logic gates of AND,OR,XOR, a full adder for numerical addition and substraction. It also contains a 4-to-1 multiplexer that chooses (depending on the value of the "operation" variable) which operation the alu will do. Lastly there is a function that inverts the variables in order to be more efficient with our logic gate usage( using the DeMorgan theorem so we don't have to create a NOR gate). The control unit initializes the variable inputs, as well as the carryIn variable of the full adder, depending on the variable "opcode". A board with every possible combination
Next is the Control Circuit part of the code, which implements the previous board.
`
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity ControlCircuit is
port (
opcode :in std_logic_vector (2 downto 0);
signala, signalb : out std_logic_vector(0 downto 0);
operation : out std_logic_vector(1 downto 0);
CarryIn : out std_logic);
end ControlCircuit;
architecture model_conc9 of ControlCircuit is
--signal outAND,outOR,outXOR,sum,outnotA,outnotB : std_logic;
--signal operation : out std_logic_vector(1 downto 0);
begin
process(opcode)
begin
case opcode is
--AND--
when "000"=>
operation <= "00";
signala <= "0";
signalb <= "0";
CarryIn <= '0';
--OR--
when "001" =>
operation <= "01";
signala <= "0";
signalb <= "0";
CarryIn <= '0';
--ADD--
when "011" =>
operation <= "10";
signala <= "0";
signalb <= "0";
CarryIn <= '0';
--SUB--
when "010" =>
operation <= "10";
signala <= "0";
signalb <="1";
CarryIn <= '1';
--NOR--
when "101"=>
operation <= "00";
signala <= "1";
signalb <= "1";
CarryIn <= '0';
--xor
when "100" =>
operation <= "11";
signala <= "0";
signalb <= "0";
CarryIn <= '0';
--Adiafores times--
when others =>
operation <= "00";
signala <= "0";
signalb <= "0";
CarryIn <= '0';
end case;
end process;
end model_conc9;
`
Lastly here is the code that uses all the previous parts and and an RTL diagram that shows the code's result
library IEEE;
use ieee.std_logic_1164.all;
use work.erotima2.all;
entity structural is
port (a,b: in std_logic;
opcode : in std_logic_vector ( 2 downto 0);
Result,CarryOut : out std_logic);
end structural;
architecture alu of structural is
signal outAND,outOR,outXOR,sum,outnotA,outnotB,CarryIn : std_logic;
signal signala,signalb : std_logic_vector (0 downto 0);
signal operation : std_logic_vector (1 downto 0);
begin
u0 : myAND2 port map (outnotA,outnotB,outAND);
u1 : myOR2 port map (outnotA,outnotB,outOR);
u2 : myXOR2 port map (outnotA,outnotB,outXOR);
u3 : fulladder port map (CarryIn,outnotA,outnotB,sum,CarryOut);
u4 : notA port map (a,signala,outnotA);
u5 : notB port map (b,signalb,outnotB);
u6 : mux4to1 port map (outAND, outOR,sum, outXOR, operation, Result );
u8 : ControlCircuit port map(opcode,signala,signalb,operation,CarryIn);
end alu;
Now for the tough part, i need to use the 1-bit ALU 16 times as a component, to create a 16-bit ALU. It is important to keep the control circuit independent from the rest of the code. I have tried using an std_logic_vector ( 15 downto 0) but it did not work and i would like to use the previous code segments as a component. Can anyone give any tips or ideas that will help connect 16 1-bit ALUs to a complete 16-bit ALU? Thanks in advance for those who read this massive wall of text.
Your recent comment
Yes i understand that my code is weird but we were intsructed to invert the inputs according to this diagram . As for the duplicate post, i checked before posting and they were implemented only structurally, while in my case i need to write the behavioral part too.
Explains the issue, misspellings aside. You'll notice your architecture structural of entity structural doesn't match the signals shown on the above 1 bit alu diagram which doesn't contain an instantiated ControlCircuit.
If you were to provide a design unit that matched the above diagram you can hook up the 1 bit alu carry chain while deriving the carryin for the lsb from the control block which provides a + 1 and inversion for subtraction:
library ieee;
use ieee.std_logic_1164.all;
entity alu_16_bit is
port (
a: in std_logic_vector (15 downto 0);
b: in std_logic_vector (15 downto 0);
opcode: in std_logic_vector (2 downto 0);
result: out std_logic_vector (15 downto 0);
carryout: out std_logic
);
end entity;
architecture foo of alu_16_bit is
component alu_1_bit is
port (
a: in std_logic;
b: in std_logic;
ainvert: in std_logic;
binvert: in std_logic;
carryin: in std_logic;
operation: in std_logic_vector (1 downto 0);
result: out std_logic;
carryout: out std_logic
);
end component;
component controlcircuit is
port (
opcode: in std_logic_vector(2 downto 0);
ainvert: out std_logic;
binvert: out std_logic;
operation: out std_logic_vector(1 downto 0);
carryin: out std_logic -- invert a or b, add + 1 for subtract
);
end component;
signal ainvert: std_logic;
signal binvert: std_logic;
signal operation: std_logic_vector (1 downto 0);
signal carry: std_logic_vector (16 downto 0);
begin
CONTROL_CIRCUIT:
controlcircuit
port map (
opcode => opcode,
ainvert => ainvert,
binvert => binvert,
operation => operation,
carryin => carry(0) -- for + 1 durring subtract
);
GEN_ALU:
for i in 0 to 15 generate
ALU:
alu_1_bit
port map (
a => a(i),
b => b(i),
ainvert => ainvert,
binvert => binvert,
carryin => carry(i),
operation => operation,
result => result(i),
carryout => carry(i + 1)
);
end generate;
carryout <= carry(16) when operation = "10" else '0';
end architecture;
This represents moving ControlCircuit out of structural - only one copy is needed, renaming structural alu_1_bit and making the ports match.
There's a new top level alu_16_bit containing a single instance of ControlCircuit along with sixteen instances of alu_1_bit elaborated from the generate statement using the generate parameter i to index into arrays values for connections.
This design has been behaviorally implemented independently using the Opcode table you provided the link to:
as well as an independent fulladder used in alu_1_bit and appears functional.
This implies your design units haven't been validated.