EmbDev.net

Forum: FPGA, VHDL & Verilog Generic Adder in VHDL


von Martin (Guest)


Rate this post
useful
not useful
Hello,
as I just started learning VHDL I still have a number of problems 
getting things running.
I would like to write a generic adder which adds to input values bit by 
bit.
In order to realize the generic functionality of the adder three 
std_logic_vectors were introduced which contain the input and the output 
(when the input was processed).

To check, if everything works correctly, I wrote a test bench. And 
that's where the problems start. When running the syntax check I get an 
error message telling me that n is not declared (in the section where 
the signals are declared).

VHDL code of the test bench:
1
LIBRARY ieee;
2
USE ieee.std_logic_1164.ALL;
3
USE ieee.std_logic_unsigned.all;
4
USE ieee.numeric_std.ALL;
5
6
ENTITY adder_tb IS
7
END adder_tb;
8
9
architecture behaviour of adder_tb is
10
  component adder
11
    generic(n : integer := 4);
12
    port(
13
      x : IN std_logic_vector(n-1 downto 0);
14
      y : IN std_logic_vector(n-1 downto 0);
15
      sum : OUT std_logic_vector(n-1 downto 0 );
16
      cout : OUT std_logic
17
    ); 
18
  end component;
19
20
  signal x_sig, y_sig, sum_sig : std_logic_vector(n-1 downto 0);
21
  signal cout_sig : std_logic := '0';
22
  
23
begin
24
    uut: adder PORT MAP (
25
          x => x_sig,
26
          y => y_sig,
27
          sum => sum_sig,
28
          cout => cout_sig
29
        );
30
end behaviour;


Does anybody see the problem? Thanks very much in advace!!!!

von Morin (Guest)


Rate this post
useful
not useful
The problem is in the line
1
signal x_sig, y_sig, sum_sig : std_logic_vector(n-1 downto 0);

n is not declared at this point. You have to add it as a generic or 
local in adder_tb.

You might think that the line
1
    generic(n : integer := 4);
declares n to be 4. However, this declaration is local to the component 
'adder' and does not extend to the signal declaration for x_sig and the 
like.

von Hartmut S. (scheff)


Rate this post
useful
not useful
Hi Martin,

the error arises at the line

signal x_sig, y_sig, sum_sig : std_logic_vector(n-1 downto 0);

thats because your architecture doesn't know "n". "n" is defined in the 
component part only and will be forwarded to "adder" only. So add a 
"signal n : integer := 4;" before the signal definitions in the 
architecture and it will run.

von Martin (Guest)


Rate this post
useful
not useful
Hi,
thanks for the quick reply. Unfortunately it didn't quite help. The 
syntax check tells me there is a syntax error in the line where I 
declare n and that n is still not declared in the line below. Seems 
strange to me...

von Hartmut S. (scheff)


Rate this post
useful
not useful
Sorry I was too fast: The correct answer ist "constant n : integer := 
4;". A signal can't do the job.

von Martin (Guest)


Rate this post
useful
not useful
Hi,
thanks a lot. It works!!!

von CHIRAZ (Guest)


Attached files:

Rate this post
useful
not useful
Hi, I would like to implement a generic adder in VHDL  for integers does 
any body can help me thank you.

von Lothar M. (Company: Titel) (lkmiller) (Moderator)


Rate this post
useful
not useful
CHIRAZ wrote:
> Hi, I would like to implement a generic adder in VHDL  for integers does
> any body can help me thank you.
What is the problem with your adder?
How did you find the problem?
What do you expect and wht happens instead?

Here "start" is not needed in teh sensitivity list:
1
PROCESS (clk,reset,start)

Why do you have a clock in your design?
1
   ELSIF(clk'event and clk='1') then
A adder is a completely logic device. There is no register needed.

: Edited by Moderator
Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.