Avoid using inout in VHDL - vhdl

I want to avoid using inout at the following code.
Is there any way I can do it? For example a helping signal?
entity LA_Unit is
Port ( Cin : in STD_LOGIC;
P : in STD_LOGIC_VECTOR (3 downto 0);
G : in STD_LOGIC_VECTOR (3 downto 0);
C3 : out STD_LOGIC;
C : inout STD_LOGIC_VECTOR (2 downto 0));
end LA_Unit;
architecture Behavioral of LA_Unit is
begin
C(0) <= (P(0) and Cin) xor G(0);
C(1) <= (P(1) and C(0)) xor G(1);
C(2) <= (P(2) and C(1)) xor G(2);
C3 <= (P(3) and C(2)) xor G(3);
end Behavioral;

If the purpose is simply to provide the intermediate value of C as an output to the module, there are different options to avoid inout.
If the tools support VHDL-2008, you can simply change inout to out, and then the C can still be read internally.
If the tools only support VHDL-2002, then you can still change the inout to out, but you then need an internal signal like:
architecture Behavioral of LA_Unit is
signal C_int : std_logic_vector(2 downto 0);
begin
C_int(0) <= (P(0) and Cin) xor G(0);
C_int(1) <= (P(1) and C_int(0)) xor G(1);
C_int(2) <= (P(2) and C_int(1)) xor G(2);
C3 <= (P(3) and C_int(2)) xor G(3);
C <= C_int;
end Behavioral;
As xvan also write, only use inout for toplevel ports on the chip, or for special test-bench use, since inout are not supported internally in a chip.

Use a signal as an intermediate for C(0) and C(1).
Inouts should only be used for hardware io ports, like a gpio port, or the data port on a memory bus.

There are 2 solutions:
Using buffer mode instead of inout.
entity LA_Unit is
Port ( Cin : in STD_LOGIC;
P : in STD_LOGIC_VECTOR (3 downto 0);
G : in STD_LOGIC_VECTOR (3 downto 0);
C3 : out STD_LOGIC;
C : buffer STD_LOGIC_VECTOR (2 downto 0));
end LA_Unit;
architecture Behavioral of LA_Unit is
begin
C(0) <= (P(0) and Cin) xor G(0);
C(1) <= (P(1) and C(0)) xor G(1);
C(2) <= (P(2) and C(1)) xor G(2);
C3 <= (P(3) and C(2)) xor G(3);
end Behavioral;
Some tools have problems with this mode.
An intermediate signal:
entity LA_Unit is
Port ( Cin : in STD_LOGIC;
P : in STD_LOGIC_VECTOR (3 downto 0);
G : in STD_LOGIC_VECTOR (3 downto 0);
C3 : out STD_LOGIC;
C : out STD_LOGIC_VECTOR (2 downto 0)
);
end entity;
architecture rtl of LA_Unit is
signal C_i : STD_LOGIC_VECTOR(3 downto 0);
begin
C_i(0) <= (P(0) and Cin) xor G(0);
C_i(1) <= (P(1) and C_i(0)) xor G(1);
C_i(2) <= (P(2) and C_i(1)) xor G(2);
C_i(3) <= (P(3) and C_i(2)) xor G(3);
C <= C_i(2 downto 0);
C3 <= C_i(3);
end architecture

Related

VHDL 4-bit multiplier based on 4-bit adder

i am a bit new to VHDL and i try to learn by examples. So long story short i began with some basic examples like creating this Full Adder.
entity FA is
Port ( A : in STD_LOGIC;
B : in STD_LOGIC;
Cin : in STD_LOGIC;
S : out STD_LOGIC;
Cout : out STD_LOGIC);
end FA;
architecture gate_level of FA is
begin
S <= A XOR B XOR Cin ;
Cout <= (A AND B) OR (Cin AND A) OR (Cin AND B) ;
end gate_level;
After that i tried to implement this 4-bit adder
And this is the code that i wrote.
entity Ripple_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_Adder;
architecture Behavioral of Ripple_Adder is
-- Full Adder VHDL Code Component Decalaration
component FA
Port ( A : in STD_LOGIC;
B : in STD_LOGIC;
Cin : in STD_LOGIC;
S : out STD_LOGIC;
Cout : out STD_LOGIC);
end component;
-- Intermediate Carry declaration
signal c1,c2,c3: STD_LOGIC;
begin
-- Port Mapping Full Adder 4 times
FA1: FA port map( A(0), B(0), Cin, S(0), c1);
FA2: FA port map( A(1), B(1), c1, S(1), c2);
FA3: FA port map( A(2), B(2), c2, S(2), c3);
FA4: FA port map( A(3), B(3), c3, S(3), Cout);
end Behavioral;
Also i used a 4_bit_adder test bench file and i found out that the output is right. Now i am trying to implement a 4 bit multiplier with the usage of the 4 bit adder but i am a bit stuck. Actually this is the multiplier that i am trying to implement.
the code i wrote is this, but i am stuck at the port map
--library
library IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_textio.all;
use IEEE.std_logic_unsigned.all;
--entity
entity multy is
port (x: in std_logic_vector(3 downto 0);
y: in std_logic_vector(3 downto 0);
p : out std_logic_vector(7 downto 0)
);
end multy ;
-- architecture
architecture rtl of multy is
component Ripple_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 andgate: std_logic_vector(15 downto 0);
signal sumout: std_logic_vector( 11 downto 0);
signal carry: std_logic_vector(11 downto 0);
begin
andgate(0) <= x(0) and y(0);
andgate(1) <= x(1) and y(0); --b0
andgate(2) <= x(2) and y(0); --b1
andgate(3) <= x(3) and y(0); --b2
B
andgate(4) <= x(0) and y(1);
andgate(5) <= x(1) and y(1);
andgate(6) <= x(2) and y(1);
andgate(7) <= x(3) and y(1);
andgate(8) <= x(0) and y(2);
andgate(9) <= x(1) and y(2);
andgate(10) <= x(2) and y(2);
andgate(11) <= x(3) and y(2);
andgate(12) <= x(0) and y(3);
andgate(13) <= x(1) and y(3);
andgate(14) <= x(2) and y(3);
andgate(15) <= x(3) and y(3);
--gates
cell_1: Ripple_Adder port map();
cell_2: Ripple_Adder port map();
cell_3: Ripple_Adder port map();
--Assigning p values
p(0) <= andgate(0);
p(1) <= sumout(0);
p(2) <= sumout(4);
p(3) <= sumout(8);
p(4) <= sumout(9);
p(5) <= sumout(10);
p(6) <= sumout(11);
p(7) <= carry(11);
end rtl ;
"I am stuck on the port map" isn't a specific problem statement.
With named association members of formal ports in maps could be associated individually as well as in whole as long as all members of the formal are associated - IEEE Std 1076-2008 6.5.7 Association lists:
A formal interface object shall be either an explicitly declared interface object or member (see 5.1) of such an interface object. In the former case, such a formal is said to be associated in whole. In the latter cases, named association shall be used to associate the formal and actual; the subelements of such a formal are said to be associated individually. Furthermore, every scalar subelement of the explicitly declared interface object shall be associated exactly once with an actual (or subelement thereof) in the same association list, and all such associations shall appear in a contiguous sequence within that association list. Each association element that associates a slice or subelement (or slice thereof) of an interface object shall identify the formal with a locally static name.
Note you have too many carry elements (only need two), don't need andgate(0), don't need sumout(0), sumout(4) or sumout(11 downo 8), there's an extraneous character in the multy architecture, you're missing context clauses and have unused use clauses.
Your code using array intermediary signals:
library ieee;
use ieee.std_logic_1164.all;
-- use ieee.std_logic_textio.all; -- NOT USED
-- use ieee.std_logic_unsigned.all; -- NOT USED
entity multy is
port (
x: in std_logic_vector (3 downto 0);
y: in std_logic_vector (3 downto 0);
p: out std_logic_vector (7 downto 0)
);
end entity multy;
architecture rtl of multy is
component Ripple_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;
-- AND Product terms:
signal G0, G1, G2: std_logic_vector (3 downto 0);
-- B Inputs (B0 has three bits of AND product)
signal B0, B1, B2: std_logic_vector (3 downto 0);
begin
-- y(1) thru y (3) AND products, assigned aggregates:
G0 <= (x(3) and y(1), x(2) and y(1), x(1) and y(1), x(0) and y(1));
G1 <= (x(3) and y(2), x(2) and y(2), x(1) and y(2), x(0) and y(2));
G2 <= (x(3) and y(3), x(2) and y(3), x(1) and y(3), x(0) and y(3));
-- y(0) AND products (and y0(3) '0'):
B0 <= ('0', x(3) and y(0), x(2) and y(0), x(1) and y(0));
-- named association:
cell_1:
Ripple_Adder
port map (
a => G0,
b => B0,
cin => '0',
cout => B1(3), -- named association can be in any order
S(3) => B1(2), -- individual elements of S, all are associated
S(2) => B1(1), -- all formal members must be provide contiguously
S(1) => B1(0),
S(0) => p(1)
);
cell_2:
Ripple_Adder
port map (
a => G1,
b => B1,
cin => '0',
cout => B2(3),
S(3) => B2(2),
S(2) => B2(1),
S(1) => B2(0),
S(0) => p(2)
);
cell_3:
Ripple_Adder
port map (
a => G2,
b => B2,
cin => '0',
cout => p(7),
S => p(6 downto 3) -- matching elements for formal
);
p(0) <= x(0) and y(0);
end architecture rtl;
And a borrowed testbench to demonstrate:
library ieee;
use ieee.std_logic_1164.all;
entity multy_tb is -- testbench
end entity;
architecture foo of multy_tb is
signal x, y: std_logic_vector (3 downto 0);
signal yp, rp: std_logic_vector (7 downto 0);
use ieee.numeric_std.all;
function to_string (inp: std_logic_vector) return string is
variable image_str: string (1 to inp'length);
alias input_str: std_logic_vector (1 to inp'length) is inp;
begin
for i in input_str'range loop
image_str(i) := character'VALUE(std_ulogic'IMAGE(input_str(i)));
end loop;
return image_str;
end function;
begin
DUT:
entity work.multy
port map (
x => x,
y => y,
p => yp
);
STIMULI:
process
begin
for i in 0 to 15 loop
x <= std_logic_vector(to_unsigned(i, x'length));
for j in 0 to 15 loop
y <= std_logic_vector(to_unsigned(j, y'length));
wait for 0 ns; -- assignments take effect
rp <= std_logic_vector(unsigned (x) * unsigned(y));
wait for 10 ns;
if yp /= rp then
report "multy error";
report HT & "expected " & to_string (rp);
report HT & "got " & to_string (yp);
end if;
end loop;
end loop;
wait;
end process;
end architecture;
The to_string function is included for pre -2008 simulators. Context clauses were added to FA and Ripple_Adder.

Unsigned multiplication in VHDL 4bit vector?

im making an ALU with an option to do A + 2B
but im having trouble getting my head around multiplying the 2B and getting the proper answer in my test bench.
EG: A = 0110 B = 0011
Equation is A + 2B
and im getting 0110
a snippit of my code is
entity ALU is
port( A :IN STD_LOGIC_VECTOR(3 DOWNTO 0) ;
B :IN STD_LOGIC_VECTOR(3 DOWNTO 0) ;
S0 :IN STD_LOGIC ;
S1 :IN STD_LOGIC ;
M :IN STD_LOGIC ;
C0 :IN STD_LOGIC ;
Cout :OUT STD_LOGIC ;
Z :OUT STD_LOGIC ;
F :OUT STD_LOGIC_VECTOR(3 DOWNTO 0));
SIGNAL VariableAlu : STD_LOGIC_VECTOR(3 DOWNTO 0);
SIGNAL FTEMP : STD_LOGIC_VECTOR(3 DOWNTO 0);
SIGNAL FTEMP2 : STD_LOGIC_VECTOR(4 DOWNTO 0);
SIGNAL ZTEMP : STD_LOGIC;
SIGNAL BTEMP1 : STD_LOGIC_VECTOR(4 DOWNTO 0);
END ALU ;
PROCESS(A,B,S0,S1,M,C0)
BEGIN
VariableAlu <= (S0 & S1 & C0 & M);
--M = 1 ARITHMETIC
(part that shifts it, lab teacher told us to do this)
BTEMP1(4 DOWNTO 1)<= B;
BTEMP1(0)<= '0';
when "1111" => FTEMP2 <= ((A) + BTEMP1);
any help would be greatly appreciated.
In addition to what GSM said, you can also just write what you want. I.e. a multiplication by 2. Synthesis software is smart enough to recognize what you are doing.
What you have to remember is that the result will be too large, so it has to be resized.
library IEEE;
use IEEE.std_logic_1164.all;
entity input_output_adder is
port (
input_a : in std_logic_vector(4 downto 0);
input_b : in std_logic_vector(4 downto 0);
output : out std_logic_vector(4 downto 0)
);
end entity;
architecture rtl of input_output_adder is
use IEEE.numeric_std.all;
begin
output <= std_logic_vector(unsigned(input_a) + resize((unsigned(input_b) * 2), 5));
end architecture;
This will result in only LUTs... nu multipliers.
Result from Vivado:
Result from Quartus:
There are a few things to note about your code. Firstly, for any arithmetic, avoid using SLV and stick with unsigned or signed types from the numeric_std library.
Your explicit shift (multiplication by 2) for the operand B:
BTEMP1(4 DOWNTO 1)<= B;
BTEMP1(0)<= '0';
Is, a) not required, and b) verbose. You can achieve this by simply doing BTEMP <= B & '0';, or better yet, don't even use an intermediary signal and assign directly to FTEMP2 in the switch statement. eg.
when "1111" => FTEMP2 <= std_logic_vector(unsigned(A) + unsigned(B&'0'));
Note the conversions in the above line. They are required, as by default, SLV's do not support the + operator (unless you use the std_logic_unsigned or std_logic_signed libraries). You will need to include the numeric_std library for this.
EDIT:
I also forgot to mention that FTEMP will potentially overflow for the given function; F <= A + 2B, where A and B are both 4 bits and F is 5 bits.

VHDL - Carry Look Ahead Adder isn't adding odd and even numbers

I coded a CLA 4-Bit adder for a project for EE, and while it'll add Even/Even and Odd/Odd, it won't add A and B if only one of them is Odd.
So basically:
0001 + 0001 = 0010
0001 + 0010 = 0010 (Doesn't detect 0001)
Help is much appreciated!
Code in top-level entity:
signal g1 : unsigned(3 downto 0);
signal g2 : unsigned(3 downto 0);
signal g3 : unsigned(3 downto 0);
signal p1 : unsigned(3 downto 0);
signal p2 : unsigned(3 downto 0);
signal p3 : unsigned(3 downto 0);
signal c1 : unsigned(4 downto 0);
signal c2 : unsigned(4 downto 0);
signal c3 : unsigned(4 downto 0);
signal ci1 : std_logic;
signal ci2 : std_logic;
signal ci3 : std_logic;
signal co1 : std_logic;
signal co2 : std_logic;
signal co3 : std_logic;
signal sum : unsigned(4 downto 0);
signal sum1 : unsigned(3 downto 0);
signal sum2 : unsigned(3 downto 0);
signal sum3 : unsigned(3 downto 0);
signal tn : unsigned(3 downto 0) := "1010";
--A + B
p1 <= unsigned(A xor B);
g1 <= unsigned(A and B);
k1 : CLA1 port map (p1,g1,c1,ci1);
sum1 <= (p1 xor c1(3 downto 0));
co1 <= c1(4);
--A + 1
p2 <= unsigned(A xor "0001");
g2 <= unsigned(A and "0001");
k2 : CLA2 port map (p2,g2,c2,ci2);
sum2 <= (p2 xor c2(3 downto 0));
co2 <= c2(4);
--A + A
p3 <= unsigned(A xor A);
g3 <= unsigned(A and A);
k3 : CLA3 port map (p3,g3,c3,ci3);
sum3 <= (p3 xor c3(3 downto 0));
co3 <= c3(4);
Code in CLA.vhd:
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity CLA1 is
port (
ci : in std_logic;
p : in unsigned(3 downto 0);
g : in unsigned(3 downto 0);
c : out unsigned(4 downto 0)
);
end CLA1;
architecture Behavioral of CLA1 is
begin
c(0) <= ci;
c(1) <= g(0) or (p(0) and ci);
c(2) <= g(1) or (p(1) and g(0)) or (p(1) and p(0) and ci);
c(3) <= g(2) or (p(2) and g(1)) or (p(2) and p(1) and g(0))
or (p(2) and p(1) and p(0) and ci);
c(4) <= g(3) or (p(3) and g(2)) or (p(3) and p(2) and g(1))
or (p(3) and p(2) and p(1) and g(0)) or (p(3)
and p(2) and p(1) and p(0) and ci);
end Behavioral;
You have done port map in wrong order. i.e. k1 : CLA1 port map (p1,g1,c1,ci1); But it suppose to be k1 : CLA1 port map (ci1,p1,g1,c1);
Do this change and try
You have several errors.
First your code example for the top level is incomplete it's not a Minimal, Complete, and Verifiable example, meaning someone else can't duplicate the error(s) you're encountering.
Second CLA2 and CLA3 appear to have u component declarations (otherwise your code wouldn't analyze, elaborate and simulate). This results in those components being unbound (and their signals not showing up in your node finder). That's correct by using CLA1 instead of CLA2 and CLA3 in k2 and k3.
Third as Aril points out you've messed up the positional port association in the instantiations of k1, k2 and k3 (and as Brian notes this is why we use formal port association). You could also note that k1 (which is bound during elaboration if the entity and architecture pair for CLA1 has previously been analyzed) shouldn't successfully elaborate. It indicates your code snippet doesn't match the description of your errors.
Fix those things and you get better results:
This waveform dump was produced using this stimulus:
STIMULUS:
process
begin
wait for 10 ns;
for i in 0 to 15 loop
B <= std_logic_vector(to_unsigned(i,4));
for j in 0 to 15 loop
A <= std_logic_vector(to_unsigned(j,4));
ci1 <= '0';
ci2 <= '0';
ci3 <= '0';
wait for 10 ns;
ci1 <= '1';
ci2 <= '1';
ci3 <= '1';
wait for 10 ns;
end loop;
end loop;
wait;
end process;
Notice the assumption that A and B are std_logic_vector subtypes, born out by the likes of:
p1 <= unsigned(A xor B);
g1 <= unsigned(A and B);
(All without doing your class work for you while telling you where your errors lie).

8 bit Ripple carry adder Port mappinng in VHDL

I wrote the code for 8 bit adder by usign 4 bit carry look ahead adder. i instantiated the 4 bit caryy look ahed adder using port map. but i think i am wrong for port mapping . plese any one can hel me , how can i correct the port mapping ..
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity adder_4_bit is
Port ( a,b : in STD_LOGIC_VECTOR (3 downto 0);
s : out STD_LOGIC_VECTOR (3 downto 0);
cout: out STD_LOGIC);
end adder_4_bit;
architecture Behavioral of adder_4_bit is
signal g, p, c, b1: STD_LOGIC_VECTOR(3 downto 0);
begin
g <= a and b;
p <= a xor b;
s(0) <= p(0) ;
c(0) <= g(0) or p(0) ;
s(1) <= p(1) xor c(0);
c(1) <= g(1) or (p(1) and g(0)) or (p(1) and p(0));
s(2) <= p(2) xor c(1);
c(2) <= g(2) or (p(2) and g(1)) or (p(2) and p(1) and g(0)) or (p(2) and p(1) and p(0));
s(3) <= p(3) xor c(2);
c(3) <= g(3) or (p(3) and g(2)) or (p(3) and p(2) and g(1)) or (p(3) and p(2) and p(1) and g(0)) or (p(3) and p(2) and p(1) and p(0));
cout <= c(3);
end Behavioral;
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
--********************************************************--
entity Adder_8_bit is
Port (
a,b : in STD_LOGIC_VECTOR (7 downto 0);
s : out STD_LOGIC_VECTOR (7 downto 0);
cout: out STD_LOGIC
);
end Adder_8_bit;k
---******************---
architecture Behavioral of Adder_8_bit is
component adder_4_bit is
Port (
A,B : in STD_LOGIC_VECTOR (3 downto 0);
S: out STD_LOGIC_VECTOR (3 downto 0);
cout: out STD_LOGIC
);
end component;
----
signal a1 ,b1 : STD_LOGIC_VECTOR (7 downto 0);
signal SUM : STD_LOGIC_VECTOR (7 downto 0);
begin
four_bit_adder1: adder_4_bit port map ( a1(3 downto 0) ,b1(3 downto 0) , SUM (3 downto 0) , cout) ;
four_bit_adder2: adder_4_bit port map ( a1(7 downto 4) ,b1(7 downto 4) , SUM (7 downto 4) , cout ) ;
end Behavioral;
You connect the signal cout to two drivers:
four_bit_adder1: adder_4_bit port map ( a1(3 downto 0) ,b1(3 downto 0) , SUM (3 downto 0) , cout);
four_bit_adder2: adder_4_bit port map ( a1(7 downto 4) ,b1(7 downto 4) , SUM (7 downto 4) , cout );
This is wrong, as the toplevel cout should have only 1 source, in this case the four_bit_adder2.
Your 4 bits adder also requires a carry in, otherwise it is impossible to chain two of them to form a 8 bits adder. The first adder's carry in is '0', the second adder's carry in is the first adder's carry out.

How to deal with signed numbers correctly and still use "+"

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
entity alu_16 is
Port ( a : in STD_LOGIC_VECTOR(15 downto 0);
b : in STD_LOGIC_VECTOR(15 downto 0);
sel : in STD_LOGIC_VECTOR (1 downto 0);
gt : out STD_LOGIC;
lt : out STD_LOGIC;
eq : out STD_LOGIC;
result : out SIGNED(15 downto 0);
overflow : out STD_LOGIC;
cout : in STD_LOGIC);
end alu_16;
architecture Behavioral of alu_16 is
signal inter_res : SIGNED(16 downto 0);
signal subtraction : SIGNED(16 downto 0);
signal addition : SIGNED (16 downto 0);
signal carry_in : STD_LOGIC;
signal carry_out : STD_LOGIC;
signal msb_bit_add : STD_LOGIC;
begin
gt <= '1' when a > b else '0';
lt <= '1' when a < b else '0';
eq <= '1' when a = b else '0';
subtraction <= signed(a) - signed(b);
addition <= signed(a) + signed(b);
with sel select
inter_res <= addition when "00",
subtraction when "01",
signed(a) AND signed(b) when "10",
signed(a) OR signed(b) when others;
carry_out <= inter_res(16);
msb_bit_add <= std_logic(a(15) + b(15));
carry_in <= msb_bit_add XOR inter_res(15);
overflow <= NOT(carry_in XOR carry_out);
result <= inter_res(15 downto 0);
end Behavioral;
So.. I'm trying to make a 16 bit signed adder without using a ripple carry adder. However, I am getting errors about overloading the + operator at the one bit add for msb_bit_add. Can anyone shed some light on what I should do on that line?
Thanks!

Resources