Using for loop to design adder in vhdl - for-loop

I'm trying to create an m bit adder by instantiating multiple copies of the n bit adder using for/generate loop. This is my code so far, it fails to simulate by giving the error:
"Line 44: Not all partial formals of a_n have actual". The n bit adder is declared as component, I have successfully tested it and it works.
Please help by offering any suggestions to solve this problem
entity m_bit_adder is
generic (m : integer := 16; n : integer := 4);
Port ( A_m : in STD_LOGIC_VECTOR (m-1 downto 0);
B_m : in STD_LOGIC_VECTOR (m-1 downto 0);
Cin_m : in STD_LOGIC;
Cout_m : out STD_LOGIC;
S_m : out STD_LOGIC_VECTOR (m-1 downto 0));
end m_bit_adder;
architecture Behavioral of m_bit_adder is
component n_bit_adder is
generic (n_number : integer := 4);
Port ( A_n : in STD_LOGIC_vector(n-1 downto 0);
B_n : in STD_LOGIC_vector(n-1 downto 0);
Cin_n : in STD_LOGIC;
S_n : out STD_LOGIC_vector(n-1 downto 0);
Cout_n : out STD_LOGIC);
end component;
signal sig_m : std_logic_vector (m downto 0);
begin
m_bit_adder : for j in 0 to m-1 generate
n_bit : n_bit_adder generic map (n_number => n)
port map (
A_n(n-1) => A_m(j),
B_n(n-1)=> B_m(j),
S_n(n-1) => S_m(j),
Cin_n => sig_m(j),
Cout_n => sig_m(j+1)
);
end generate;
sig_m(0) <= Cin_m;
Cout_m <= sig_m(m);
end Behavioral;
This is my code for the n adder:
entity n_bit_adder is
generic (n : integer := 4);
Port ( A_n : in STD_LOGIC_vector(n-1 downto 0);
B_n : in STD_LOGIC_vector(n-1 downto 0);
Cin_n : in STD_LOGIC;
S_n : out STD_LOGIC_vector(n-1 downto 0);
Cout_n : out STD_LOGIC);
end n_bit_adder;
architecture Behavioral of n_bit_adder is
component adder
Port ( A : in STD_LOGIC;
B : in STD_LOGIC;
Cin : in STD_LOGIC;
S : out STD_LOGIC;
Cout : out STD_LOGIC);
end component;
signal sig_n : std_logic_vector (n downto 0);
begin
n_bit_adder : for i in 0 to n-1 generate
one_bit : adder
port map (
A => A_n(i),
B => B_n(i),
S => S_n(i),
Cin => sig_n(i),
Cout => sig_n(i+1)
);
end generate;
sig_n(0) <= Cin_n;
Cout_n <= sig_n(n);
end Behavioral;

The n_bit_adder is instantiated using only a single bit of the formal side with A_n(n-1) in:
n_bit : n_bit_adder generic map (n_number => n)
port map (
A_n(n-1) => A_m(j),
....
But the component for n_bit_adder has as std_logic_vector for A_n with multiple bits in:
component n_bit_adder is
generic (n_number : integer := 4);
Port ( A_n : in STD_LOGIC_vector(n-1 downto 0);
...
So for a std_logic_vector(3 downto 0) then A_n(2 downto 0) are not used on the formal side (left side) of the mapping, which is also what the error message says in "Not all partial formals of a_n have actual".
Note also, that it looks like different names n_number and n are used in component for generic and std_logic_vector length on ports.

Related

How to instantiate multiple components with variable size ports in vhdl?

I want to retiteratively elaborate a couple of components using for generate statements, these components have variable size ports and I don't have an idea of how assign these variable size ports to signals.
I'm using a package with this declaration:
library ieee;
use ieee.std_logic_1164.all;
PACKAGE Arrays_package IS
type Generic_ARRAY_type is array (integer range <>) of std_logic_vector;
END Arrays_package;
The components are Pre_pre_buffer_conv and Pre_buffer_conv in the code below.
How I can to declare the signal xdataout_preprebuffer?
library ieee;
use ieee.std_logic_1164.all;
use work.Arrays_package.all;
entity Naive_RNC_problem is
generic(
no_col_imag : integer := 28;
no_bits : integer := 9;
no_bits_fraction : natural := 8;
no_col_filt : integer := 3
);
port
(
CLOCK_50 : in std_logic;
KEY : in std_logic_vector(3 downto 0);
SW : in std_logic_vector(17 downto 0);
xdataout_pre_buffer_conv : out Generic_ARRAY_type(no_col_filt*no_col_filt-1 downto 0)((no_bits-1) downto 0)
);
end entity;
architecture rtl of Naive_RNC_problem is
component Image_ROM IS
PORT
(
address : IN STD_LOGIC_VECTOR (9 DOWNTO 0);
clock : IN STD_LOGIC := '1';
q : OUT STD_LOGIC_VECTOR (8 DOWNTO 0)
);
END component;
component generic_mask_nxn_v2 is
generic(
no_col_imag : integer := 28;
no_col_filt : integer := 3;
stride : integer := 1;
no_bits : integer := 10
);
Port (
-- ENTRADAS ------------------------
iclk, irst : in std_logic;
ien : in std_logic;
idatain : in std_logic_vector (no_bits-1 downto 0);
index : in integer;
iindex_ctrl : in std_logic_vector(7 downto 0);
-- SALIDAS -------------------------
odataout : out Generic_ARRAY_type(no_col_filt-1 downto 0)((no_col_filt*no_bits-1) downto 0) ;
omasken : out std_logic
);
end component;
component Pre_pre_buffer_conv is
generic
(
no_col_filt : integer := 3;
no_bits : integer := 10
);
port
(
-- ENTRADAS ------------------------
iclk, irst : in std_logic;
ien : in std_logic;
index : in integer;
iindex_ctrl : in std_logic_vector(7 downto 0);
idatain : in Generic_ARRAY_type(no_col_filt-1 downto 0)(no_col_filt*no_bits-1 downto 0);
-- SALIDAS -------------------------
odataout : out Generic_ARRAY_type(no_col_filt-1 downto 0)(no_col_filt*no_bits-1 downto 0)
);
end component;
component Pre_buffer_conv is
generic
(
no_col_filt : integer := 3;
no_bits : integer := 9
);
port
(
-- ENTRADAS ------------------------
iclk, irst : in std_logic;
ien : in std_logic;
idatain : in Generic_ARRAY_type(no_col_filt-1 downto 0)(no_col_filt*no_bits-1 downto 0);
isel_row : in std_logic_vector(3 downto 0);
isel_col : in std_logic_vector(3 downto 0);
-- SALIDAS -------------------------
odataout : out std_logic_vector (no_bits-1 downto 0)
);
end component;
component Top_Control_prepre_buffer is
port (-- ENTRADAS -----------
iclk, ireset : in std_logic;
iStart : in std_logic;
imasken_conv : in std_logic;
ifin_convolvers : in std_logic;
ifin_interfaz_Conv_pool : in std_logic;
-- SALIDAS -----------------------
oidle : out std_logic;
orst : out std_logic;
oen_line_buffer_conv : out std_logic;
odir_imagen : out std_logic_Vector(15 downto 0);
ocarga_prepre_buffer : out std_logic;
ocarga_pre_buffer_conv : out std_logic;
oind_pre_buffers_conv : out std_logic_Vector(15 downto 0);
oStart_convolver : out std_logic;
oinicia_interfaz_Conv_pool : out std_logic
);
end component;
--=======================================================
-- Signal declarations
--=======================================================
signal xRST : std_logic;
signal xrst_ctrl : std_logic;
signal xStart : std_logic;
signal xclk_50 : std_logic;
signal xaddress_memoria_MNIST_ROM, xaddress_MNIST_PORTA: STD_LOGIC_VECTOR (9 DOWNTO 0);
signal xdataout_MNIST_ROM : STD_LOGIC_VECTOR (8 DOWNTO 0);
signal xaddress_memoria_MNIST_ctrl : STD_LOGIC_VECTOR (15 DOWNTO 0);
signal xrst_buffer_conv : std_logic;
signal xdatain_buffer : std_logic_vector (no_bits-1 downto 0);
signal xdataout_buffer : Generic_ARRAY_type(no_col_filt-1 downto 0)((no_col_filt*no_bits-1) downto 0) ;
signal xmasken_buffer : std_logic;
signal xen_line_buffer_conv : std_logic;
signal xcarga_prepre_buffer : std_logic;
signal xsel_row_pre_buffer : std_logic_vector(3 downto 0);
signal xsel_col_pre_buffer : std_logic_vector(3 downto 0);
signal xstart_fill_buffers_conv : std_logic;
signal xcarga_pre_buffer_conv : std_logic;
signal xind_pre_buffers_conv_ctrl : std_logic_vector(15 downto 0);
signal xind_pre_buffers_conv : std_logic_vector(7 downto 0);
signal xrst_contol_prepre_buffer : std_logic;
signal xfin_convolver, xfin_Interfaz_conv_pool : std_logic;
-- how to declare this signal? ---------------------------
type my_type1 is array (no_col_filt-1 downto 0) of std_logic_vector((no_col_filt*no_bits-1) downto 0);
type my_type2 is array(no_col_filt*no_col_filt-1 downto 0) of my_type1;
signal xdataout_preprebuffer : my_type2;
----
--=======================================================
-- Structural coding
--=======================================================
begin
xRST <= not KEY(0);
xStart <= not KEY(1);
-- RELOJES ===============================================
xclk_50 <= CLOCK_50;
-- MEMORIA IMAGEN MNIST ===============================================
MemoriaROM_MNIST : Image_ROM port map (xaddress_memoria_MNIST_ROM, xclk_50, xdataout_MNIST_ROM);
xaddress_memoria_MNIST_ROM <= xaddress_memoria_MNIST_Ctrl(9 downto 0);
-- Line Buffer convolucion ==============================================
Buffer_mask : generic_mask_nxn_v2 generic map ( no_col_imag => no_col_imag,
no_col_filt => no_col_filt,
stride => 1,
no_bits => no_bits
)
port map (-- ENTRADAS ------------------------
xclk_50, xrst_buffer_conv,
xen_line_buffer_conv, xdatain_buffer,
0,
(others => '0'),
-- SALIDAS -------------------------
xdataout_buffer, -- this is a variable size port (no problem here)
xmasken_buffer
);
xrst_buffer_conv <= xRST or xrst_ctrl ;--(not xRST) or
xdatain_buffer <= xdataout_MNIST_ROM;
-- Control Prepre Buffer convolucion ==============================================
Prepre_buffer_ctrl : Top_Control_prepre_buffer port map (-- ENTRADAS -----------------------
xclk_50, xrst_contol_prepre_buffer,
xStart,
xmasken_buffer,
xfin_convolver,
xfin_Interfaz_conv_pool,
-- SALIDAS -----------------------
open, xrst_ctrl,
xen_line_buffer_conv,
xaddress_memoria_MNIST_ctrl,
xcarga_prepre_buffer, xcarga_pre_buffer_conv, xind_pre_buffers_conv_ctrl,
open,
open
);
xrst_contol_prepre_buffer <= xRST;
xfin_convolver <= '1';
xfin_Interfaz_conv_pool <= '1';
-- Pre pre Buffer convolucion ===========================================
pre_pre_buffers_conV : for i in 0 to (no_col_filt*no_col_filt-1) generate
pre_pre_buffer_conV_cmp : Pre_pre_buffer_conv generic map (no_col_filt => no_col_filt,
no_bits => no_bits
)
port map (-- ENTRADAS -----------------------
xclk_50, xrst_buffer_conv,
xcarga_prepre_buffer,
i,
xind_pre_buffers_conv,
xdataout_buffer, -- this is a variable size port (no problem here)
-- SALIDAS -----------------------
xdataout_preprebuffer(i) -- this is a variable size port (ERROR HERE)
);
end generate;
-- Pre Buffer convolucion ==============================================
pre_buffers_conV : for i in 0 to (no_col_filt*no_col_filt-1) generate
pre_buffer_conV_cmp : Pre_buffer_conv generic map (no_col_filt => no_col_filt,
no_bits => no_bits
)
port map (-- ENTRADAS -----------------------
xclk_50, xrst_buffer_conv,
xcarga_pre_buffer_conv,
xdataout_preprebuffer(i), -- this is a variable size port (ERROR HERE)
xsel_row_pre_buffer,
xsel_col_pre_buffer,
-- SALIDAS -----------------------
xdataout_pre_buffer_conv(i)
);
end generate;
xind_pre_buffers_conv <= xind_pre_buffers_conv_ctrl(7 downto 0);
xsel_row_pre_buffer <= (others => '0');
xsel_col_pre_buffer <= (others => '0');
end rtl;
The errors thrown by analysis & elaboration stage were:
Error (10381): VHDL Type Mismatch error at Naive_RNC_problem.vhd(228):
indexed name returns a value whose type does not match
"Generic_ARRAY_type", the type of the target expression
Error: Quartus Prime Analysis & Elaboration was unsuccessful. 1 error,
0 warnings
I believe that the signal xdataout_preprebuffer has to be declared as 2d array signal, but I don't know how to accomplish that.
Two changes.
An added type in the package:
library ieee;
use ieee.std_logic_1164.all;
package arrays_package is
type generic_array_type is array (integer range <>) of std_logic_vector;
type generic_array_of_generic_type is -- ADDED type
array (integer range <>) of generic_array_type;
end package arrays_package;
and the signal declaration:
signal xdataout_preprebuffer: generic_array_of_generic_type -- type mark
-- index subtype:
(no_col_filt * no_bits - 1 downto 0)
-- element constraint (generic_array_type):
-- index subtype:
(no_col_filt - 1 downto 0)
-- element index constraint (std_logic_vector):
(no_col_filt * no_bits - 1 downto 0);
After which your code will analyze (elaboration and simulation requires the entities to which all those components are bound during elaboration. With entity declarations and dummy architecture bodies for your components your code elaborates and simulates (showing associations and assignments don't have bounds errors).
The added type uses the -2008 unbounded array definition as does the original (See IEEE Std 1076-2008 5.3.2 Array types, 5.3.2.1 General,
5.3.2.2 Index constraints and discrete ranges, 6.3 Subtype declarations).
What we have is an unbound one-dimensional array type (generic_array_of_generic_type) whose element type (unbounded one-dimensional type generic_array_type) has it's own element unbounded one-dimensional array type (std_logic_vector). The index constraints for the unbounded array types are provided during object declaration.

Access to 2 elements of the same array in VHDL on each clock edge

How can I access 2 elements of a 2d array in the same process and on each clock edge?
entity test is
generic (N : integer := 8;
N_MAX : integer :=1944;
O_MAX : integer :=20;
M_MAX : integer :=324;
K_MAX : integer :=1620);
Port ( clk : in STD_LOGIC;
rst : in STD_LOGIC;
a : in STD_LOGIC_VECTOR(N-1 downto 0);
b : in STD_LOGIC_VECTOR(N-1 downto 0)`);
end test;
architecture Behavioral of test is
type t1 is array (0 to 323,0 to 19) of std_logic_vector(7 downto 0);
signal tab:t1;
`
Would appreciate some help,thank you in advance

Connect carry out to carry in for adder/subtractor in structural VHDL

So I have the following VHDL code to implement an Nbit adder/subtractor using only a 2:1 mux, an inverter (flips bit), and a full adder. I am having issues connecting the carry out of an adder to the next ones carry in while having the first adder have a carry in of i_Control. Any help would be greatly appreciated :).
library IEEE;
use IEEE.std_logic_1164.all;
use work.all;
entity add_subtract is
generic(N : integer := 16);
port(i_M : in std_logic_vector(N-1 downto 0);
i_N : in std_logic_vector(N-1 downto 0);
i_Control : in std_logic_vector(N-1 downto 0);
o_S : out std_logic_vector(N-1 downto 0));
end add_subtract;
architecture structure of add_subtract is
component bit_adder
port(i_X : in std_logic;
i_Y : in std_logic;
i_Cin : in std_logic;
o_Ss : out std_logic;
o_Couts : out std_logic);
end component;
component inverter
port(i_A : in std_logic;
o_F : out std_logic);
end component;
component bit_mux
port(i_X : in std_logic;
i_Y : in std_logic;
i_S : in std_logic;
o_N : out std_logic);
end component;
signal compvalue, muxvalue, addervalue : std_logic_vector(N-1 downto 0);
signal sel, carry : std_logic_vector(N-1 downto 0);
signal k : integer := 0;
begin
carry(0) <= i_Control(0);
G1: for i in 0 to N-1 generate
one_comp: inverter
port map(i_A => i_N(i),
o_F => compvalue(i));
mux: bit_mux
port map(i_X => i_N(i),
i_Y => compvalue(i),
i_S => i_Control(i),
o_N => muxvalue(i));
struct_adder: bit_adder
port map(i_X => i_M(i),
i_Y => muxvalue(i),
i_Cin => carry(i),
o_Ss => o_S(i),
o_Couts => carry(i));
end generate;
end structure;
Make the carry array one longer:
signal carry : std_logic_vector(N downto 0); -- was N-1
and change this:
o_Couts => carry(i));
to this:
o_Couts => carry(i+1));
in your generate statement while leaving the i_Cin carry input association as is.
If the last carry out isn't conveyed through an output port the net will get eaten during synthesis.

error in a vhdl code

i am new to vhdl. i have a code with me as follows (the sub prog compiles very fine). i can't fix the following error
** Error: C:/Users/acer/Desktop/alu new/ALU_VHDL.vhd(110): Illegal sequential statement.
** Error: C:/Users/acer/Desktop/alu new/ALU_VHDL.vhd(115): Illegal sequential statement.
** Error: C:/Users/acer/Desktop/alu new/ALU_VHDL.vhd(120): Illegal sequential statement.
** Error: C:/Users/acer/Desktop/alu new/ALU_VHDL.vhd(128): Illegal sequential statement.
** Warning: [14] C:/Users/acer/Desktop/alu new/ALU_VHDL.vhd(128): (vcom-1272) Length of formal "Remainder" is 4; length of actual is 8.
** Error: C:/Users/acer/Desktop/alu new/ALU_VHDL.vhd(138): VHDL Compiler exiting
the line nos are bold ones in the code here.they are the portmap ones
Can anyone please help me out with this. it would be very kind of you.
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
entity ALU_VHDL is
port
(
OperandA : in std_logic_vector(3 downto 0);
OperandB : in std_logic_vector(3 downto 0);
Operation: in std_logic_vector(2 downto 0);
Startt : in std_logic;
Ready : out std_logic;
Result_High : out std_logic_vector(3 downto 0);
Result_Low : out std_logic_vector(7 downto 0);
Errorsig : out std_logic;
Reset_n : in std_logic;
Clkk : in std_logic);
end entity ALU_VHDL;
architecture Behavioral of ALU_VHDL is
-- And gate
component AND_gate
port(
x,y : IN std_logic_vector(3 downto 0);
z : OUT std_logic_vector(3 downto 0));
end component;
-- OR Gate
component OR_gate
port(
x,y : IN std_logic_vector(3 downto 0);
z : OUT std_logic_vector(3 downto 0));
end component;
-- XOR gate
component XOR_gate
port(
x,y : IN std_logic_vector(3 downto 0);
z : OUT std_logic_vector(3 downto 0));
end component;
-- Adder
COMPONENT adder4
PORT
(
C : IN std_logic;
x,y : IN std_logic_vector(3 DOWNTO 0);
R : OUT std_logic_vector(3 DOWNTO 0);
C_out : OUT std_logic);
END COMPONENT;
-- Subtractor
COMPONENT Substractor4
PORT
(
br_in : IN std_logic;
x,y : IN std_logic_vector(3 DOWNTO 0);
R : OUT std_logic_vector(3 DOWNTO 0);
E : out std_logic);
END COMPONENT;
-- Multiplier
COMPONENT mult4by4
port(operA, operB: in std_logic_vector(3 downto 0);
sumOut: out std_logic_vector(7 downto 0));
END COMPONENT;
-- Division
COMPONENT Division
Port ( Dividend : in std_logic_vector(3 downto 0);
Divisor : in std_logic_vector(3 downto 0);
Start : in std_logic;
Clk : in std_logic;
Quotient : out std_logic_vector(3 downto 0);
Remainder : out std_logic_vector(3 downto 0);
Finish : out std_logic);
END COMPONENT;
begin
process(OperandA, OperandB, Startt, Operation) is
begin
case Operation is
when "000" =>
Result_High <= "XXXX";
when "001" =>
Result_High <= OperandA and OperandB;
when "010" =>
Result_High <= OperandA or OperandB;
when "011" =>
Result_High <= OperandA xor OperandB;
when "100" =>
-- Adder
**U05 : adder4 PORT MAP (C=>Startt,x=>OperandA,y=>OperandB,R=>Result_High,C_out=>Ready);**
when "101" =>
-- Substractor & Error signal
**U06 : Substractor4 PORT MAP (br_in=>Startt,x=>OperandA,y=>OperandB,R=>Result_High,E=>Errorsig);**
when "110" =>
-- multiplication
**U07 : mult4by4 PORT MAP (operA=>OperandA,operB=>OperandB,sumOut=>Result_Low);**
when "111" =>
-- Division
if (OperandB ="0000") then
Errorsig <= '1';
else
**U08 : Division PORT MAP (Dividend=>OperandA,Divisor=>OperandB,Start=>Startt,Clk=>Clkk,Quotient=>Result_High,Remainder=>Result_Low,Finish=>Ready);**
end if;
when others =>
Errorsig <= '1';
end case;
end process;
end architecture Behavioral;
You cannot instantiate entities within a process.
Move all entity instantiations out of the process (into the architecture body) and work from there.
If you want to in instantiate component depending on the value of 'Operation', like the zennehoy wrote, you should instantiate components out of the process and in this case statement only use signal connected to this components in instantiations and link it to port you want.
For the length issue change the "Remainder : out std_logic_vector(3 downto 0);"
to "Remainder : out std_logic_vector(7 downto 0);"

vhdl multipliers

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity Lab3_Adder1 is
Port ( cin : in STD_LOGIC;
a : in STD_LOGIC_VECTOR (3 downto 0);
b : in STD_LOGIC_VECTOR (3 downto 0);
s : out STD_LOGIC_VECTOR (3 downto 0);
cout : out STD_LOGIC);
end Lab3_Adder1;
architecture Behavioral of Lab3_Adder1 is
SIGNAL c : STD_LOGIC_VECTOR (4 DOWNTO 0);
begin
c(0) <= cin;
s <= a XOR b XOR c (3 DOWNTO 0);
c (4 DOWNTO 1) <= (a AND b) OR (a AND c(3 DOWNTO 0)) OR (b AND c(3 DOWNTO 0));
cout <= c(4);
end Behavioral;
Hello, it's the first time im using this forum. I'm doing a wallace tree multiplication on VHDL. The code above is the code for a full adder. I would like to know how do we call a function/component in a main code ? (like in C programing). I would to call this full adder in my main code.
(Sorry for my english if there is any mistake, im french)
You call functions in VHDL just as you do in C - either to initialise constants, signals or variables, or as sequential statements within a process. But that's not important just now.
But you don't call components! That would be like calling an object in C++ - it makes absolutely no sense!
In VHDL you can instantiate components or (simpler!) just entities, and use signals to interconnect their ports. This is (very very crudely) more like declaring objects and sending messages in an object oriented language. This is called "structural VHDL" and often appears at the top level of a VHDL design, to create and interconnect components like CPU, memory interface, FFT processor etc.
Given your entity
entity Lab3_Adder1 is
Port ( cin : in STD_LOGIC;
a : in STD_LOGIC_VECTOR (3 downto 0);
b : in STD_LOGIC_VECTOR (3 downto 0);
s : out STD_LOGIC_VECTOR (3 downto 0);
cout : out STD_LOGIC);
end Lab3_Adder1;
I could build an 8-bit adder for example as follows:
entity Adder_8bit is
Port ( cin : in STD_LOGIC;
a : in STD_LOGIC_VECTOR (7 downto 0);
b : in STD_LOGIC_VECTOR (7 downto 0);
s : out STD_LOGIC_VECTOR (7 downto 0);
cout : out STD_LOGIC);
end Adder_8bit;
architecture Structural of Adder_8bit is
signal carry_int : std_logic; -- between lower and upper halves
begin
-- We need to create and connect up two adders
LSB_adder : entity work.Lab3_Adder1
Port Map(
cin => cin,
a => a(3 downto 0),
b => b(3 downto 0),
s => s(3 downto 0),
cout => carry_int
);
MSB_adder : entity work.Lab3_Adder1
Port Map(
cin => carry_int,
a => a(7 downto 4),
b => b(7 downto 4),
s => s(7 downto 4),
cout => cout
);
end Structural;
You can define VHDL-Functions which replace combinational circuits and which can be called anywhere in the main VHDL-Code similar to C functions.
You need to define a package first where the function definitions go.
======= myAdders.vhdl ==============
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_unsigned.all;
package myAdders is
function Lab3_Adder1( cin : in STD_LOGIC;
a : in STD_LOGIC_VECTOR (3 downto 0);
b : in STD_LOGIC_VECTOR (3 downto 0);
s : out STD_LOGIC_VECTOR (3 downto 0)) return std_logic;
end Lab3_Adder1;
end myAdders;
package body myAdders is
function Lab3_Adder1 ( cin : in STD_LOGIC;
a : in STD_LOGIC_VECTOR (3 downto 0);
b : in STD_LOGIC_VECTOR (3 downto 0);
s : out STD_LOGIC_VECTOR (3 downto 0)) return std_logic is
variable c: std_logic_vector(4 downto 0);
begin
c(0) := cin;
s := a XOR b XOR c (3 DOWNTO 0);
c (4 DOWNTO 1) := (a AND b) OR (a AND c(3 DOWNTO 0)) OR (b AND c(3 DOWNTO 0));
return c(4);
end Lab3_Adder1;
end myAdders;
======= topLevel.vhdl ==============
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_unsigned.all;
use work.myAddres.all;
entity TopLevel is
Port (
cin : in STD_LOGIC;
a : in STD_LOGIC_VECTOR (3 downto 0);
b : in STD_LOGIC_VECTOR (3 downto 0);
c : out STD_LOGIC_VECTOR (3 downto 0)
);
end TopLevel;
architecture Structural of TopLevel is
signal carry : std_logic;
begin
carry <= Lab3_Adder1(cin, a, b, c);
... and so on ...
end Structural;

Resources