I am looking at this example and below answer which is a nice solution to produce two's complement:
library ieee;
use ieee.numeric_std.all;
entity twoscomplement is
generic
(
Nbits : positive := 8
);
port
(
A : in unsigned (Nbits-1 downto 0);
Y : out signed (Nbits downto 0)
);
end entity twoscomplement;
architecture a1 of twoscomplement is
begin
Y <= -signed(resize(A, Y'length));
end architecture;
I want to use the said example to have two's complement and then make a "16-bit subtractor". The code will look like the following:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
entity subtractor_16bit is
Port ( a : in STD_LOGIC_VECTOR(15 downto 0);
b : in STD_LOGIC_VECTOR(15 downto 0);
cin : in STD_LOGIC;
sum : out STD_LOGIC_VECTOR(15 downto 0);
cout : out STD_LOGIC;
over : out STD_LOGIC
);
end subtractor_16bit;
architecture Behavioral of subtractor_16bit is
component fulladder_16bit is
Port (
a : in STD_LOGIC_VECTOR(15 downto 0);
b : in STD_LOGIC_VECTOR(15 downto 0);
cin : in STD_LOGIC;
sum : out STD_LOGIC_VECTOR(15 downto 0);
cout : out STD_LOGIC;
over : out STD_LOGIC
);
end component;
component twoscomplement is
Port (
A : in unsigned (15 downto 0);
C : out signed (15 downto 0)
);
end component;
signal n1 : STD_LOGIC_VECTOR(15 downto 0);
begin
twoscomplement_1: twoscomplement port map (a => a ,c => n1); --ERROR
fulladder_16bit_1: fulladder_16bit port map (a => a, b => n1, sum => sum , cin => cin, cout => cout, over => over);
end Behavioral;
However, I am receiving an error saying: Error: type error near a; current type std_logic_vector; expected type unsigned.
Kindly help me to solve this problem.
twoscomplement port map (a => a ,c => n1); --ERROR. C in twoscomplement is declared as type signed while n1 is defined as type std_logic_vector. the size of A and C have a default generic value that isn't modified here, length 8 and 9 respectively while A is 16 long as is C. The generic needs to be be passed and the length of n1 adjusted. Two's complement can also be preformed in an expression without a structural element. - user1155120twoscomplementand you can already see an example of type casting... - JHBonariustype_conversion ::= type_mark ( expression )Explicit type conversions are expressions converting between closely related types. Two array types are closely related if and only if the types have the same dimensionality and the element types are closely related. A type is closely related to itself. IEEE Std 1076-2008 9.3.6 Type conversions. Array types signed and std_logic_vector are closely related having the same element base type. - user1155120