Forum: FPGA, VHDL & Co. n-Bit Comparator will nicht funktionieren


von muhh (Gast)


Lesenswert?

Hey Leute,

ich schreibe gerade an einem n-Bit Comparator, den man über einen 
generic-Parameter auf n-Bit einstellen kann. Allerdings bekomme ich bei 
der Synthese immer warnings und die Simulation funktioniert für mehr als 
1Bit nicht, da kommt für Q dann dauerhaft 0 raus.

Wisst ihr woran das liegt?

Vielen Dank für eure Hilfe :)

Hier der Code des nBit-Comp:
1
library IEEE;
2
use IEEE.STD_LOGIC_1164.ALL;
3
4
entity nbit_comp is
5
   generic ( NBit : positive := 2 );
6
    port (  D0 : in   std_logic_vector(NBit-1 downto 0);
7
        D1 : in   std_logic_vector(NBit-1 downto 0);
8
        Q   : out std_logic );
9
end nbit_comp;
10
11
architecture Behavioral of nbit_comp is
12
13
  signal Qi : std_logic;
14
  signal Yi : std_logic_vector(NBit-1 downto 0);
15
  signal D0i : std_logic_vector(NBit-1 downto 0);
16
  signal D1i : std_logic_vector(NBit-1 downto 0);
17
  
18
  component x_nor
19
    port ( A : in   std_logic;
20
         B : in   std_logic;
21
         Y : out  std_logic );
22
  end component;
23
  
24
begin
25
26
  COMP:    for K in 0 to NBit-1 generate
27
      XN:    x_nor port map (  A => D0i(K),
28
                B => D1i(K),
29
                Y => Yi(K) );
30
31
  end generate COMP;
32
33
  process(Yi, Qi)
34
  begin
35
    for K in 0 to NBit-1 loop
36
      if K=0 then
37
        Qi <= Yi(0);
38
      elsif K>0 then
39
        Qi <= Qi and Yi(K);
40
      end if;
41
    end loop;
42
  end process;
43
  
44
  D0i <= D0;
45
  D1i <= D1;
46
  Q <= Qi;
47
48
end Behavioral;

Und der Code des XNOR:
1
library IEEE;
2
use IEEE.STD_LOGIC_1164.ALL;
3
4
entity x_nor is
5
    port ( A : in  STD_LOGIC;
6
           B : in  STD_LOGIC;
7
           Y : out  STD_LOGIC);
8
end x_nor;
9
10
architecture Behavioral of x_nor is
11
12
begin
13
14
  Y <= (A and B) or ((not A) and (not B));
15
16
end Behavioral;

Und die Fehlermeldungen bei der Synthese:
1
WARNING:Xst:646 - Signal <Yi<0>> is assigned but never used. This unconnected signal will be trimmed during the optimization process.
2
WARNING:Xst:524 - All outputs of the instance <COMP[0].XN> of the block <x_nor> are unconnected in block <nbit_comp>.
3
   This instance will be removed from the design along with all underlying logic
4
WARNING:Xst:2170 - Unit nbit_comp : the following signal(s) form a combinatorial loop: Q.

von Lothar M. (Firma: Titel) (lkmiller) (Moderator) Benutzerseite


Lesenswert?

muhh schrieb:
> the following signal(s) form a combinatorial loop: Q.
Der Synthesizer sieht, dass Q und Qi gleich sind und meckert am Q herum, 
obwohl er eigentlich Qi meint: du machst eine nicht getaktete Zuweisung 
von Qi an Qi.
http://www.lothar-miller.de/s9y/archives/42-Kombinatorische-Schleifen.html
Klar kannst du jetzt sagen: aber das müsste doch gehen, weil ja noch ein 
weiteres Signal mit reinkommt. Aber: der Synthesizer sieht das nicht.

Und deshalb ist genau dieser Prozess hier ...
1
  process(Yi, Qi)
2
  begin
3
    for K in 0 to NBit-1 loop
4
      if K=0 then
5
        Qi <= Yi(0);
6
      elsif K>0 then
7
        Qi <= Qi and Yi(K);
8
      end if;
9
    end loop;
10
  end process;
... das Paradebeispiel, wo eine Variable nötig und sinnvoll ist. 
Probiers mal so:
1
  process(Yi, Qi)
2
   variable tmp : std_logic;
3
  begin
4
    for K in 0 to NBit-1 loop
5
      if K=0 then
6
        tmp := Yi(0);
7
      elsif K>0 then
8
        tmp := tmp and Yi(K);
9
      end if;
10
    end loop;
11
    Qi <= tmp;
12
  end process;
Oder kürzer so:
1
  process(Yi, Qi)
2
   variable tmp : std_logic;
3
  begin
4
    for K in 0 to NBit-1 loop
5
      if K=0 then
6
        tmp := Yi(0);
7
      else
8
        tmp := tmp and Yi(K);
9
      end if;
10
    end loop;
11
    Qi <= tmp;
12
  end process;
Oder mit einem passenden Startwert noch kürzer so:
1
  process(Yi, Qi)
2
   variable tmp : std_logic;
3
  begin
4
    tmp := '1';
5
    for K in 0 to NBit-1 loop
6
       tmp := tmp and Yi(K);
7
    end loop;
8
    Qi <= tmp;
9
  end process;


Warum schreibst du das hier:
1
  component x_nor
2
    port ( A : in   std_logic;
3
         B : in   std_logic;
4
         Y : out  std_logic );
5
  end component;
6
  
7
begin
8
9
  COMP:    for K in 0 to NBit-1 generate
10
      XN:    x_nor port map (  A => D0i(K),
11
                B => D1i(K),
12
                Y => Yi(K) );
13
14
  end generate COMP;
nicht einfach so:
1
  Y <= not A xor B;

Und warum ist hier ein elsif?
1
      if K=0 then
2
        Qi <= Yi(0);
3
      elsif K>0 then
4
        Qi <= Qi and Yi(K);
5
      end if;
Was könnte K ausser 0 sonst noch sein??

Oder das Ganze einfach so:
1
  Q <= '1' when A=B else '0';

> die Simulation
Welche denn?

> D0i <= D0;
> D1i <= D1;
Diese Zuweisung von Eingängen an interne Signale ist unnötig.

: Bearbeitet durch Moderator
von W. M. (muhh)


Lesenswert?

Hallo Lothar,

danke für deine Antowort, Gott sei Dank hab ich so viele Fehler gemacht, 
habe gerade einiges draus gelernt ;) Ich habe deinen Ratschlag befolgt 
und dabei ist folgender (funktionierender) Code bei rausgekommen:
1
library IEEE;
2
use IEEE.STD_LOGIC_1164.ALL;
3
4
entity nbit_comp is
5
   generic ( NBit : positive := 16 );
6
    port (  D0 : in   std_logic_vector(NBit-1 downto 0);
7
        D1 : in   std_logic_vector(NBit-1 downto 0);
8
        Q   : out std_logic );
9
end nbit_comp;
10
11
architecture Behavioral of nbit_comp is
12
13
  signal Y : std_logic_vector(NBit-1 downto 0);
14
15
begin
16
17
  process(D0, D1, Y)
18
    variable tmp : std_logic; 
19
  begin
20
    tmp := '1';
21
    for K in 0 to NBit-1 loop
22
      Y(K) <= not D0(K) xor D1(K);
23
      tmp := tmp and Y(K);
24
    end loop;
25
    Q <= tmp;
26
  end process;
27
28
end Behavioral;

Würde man das so in einem FPGA implementieren?

von Lothar M. (Firma: Titel) (lkmiller) (Moderator) Benutzerseite


Lesenswert?

Willy M. schrieb:
> Würde man das so in einem FPGA implementieren?
Nein.

"Man" würde das so machen:
1
library IEEE;
2
use IEEE.STD_LOGIC_1164.ALL;
3
4
entity nbit_comp is
5
    port (  D0, D1 : in  std_logic_vector;
6
            Q  : out std_logic );
7
end nbit_comp;
8
9
architecture Behavioral of nbit_comp is
10
begin
11
    Q <= '1' when D0=D1 else '0';
12
end Behavioral;
Generischer gehts es nicht: es muss keine explizite Wortbreite angegeben 
werden...

Warum von Hand irgendwelches Zeugs zusammenbasteln, was die eingebundene 
Bibliothek selber schon hergibt?

: Bearbeitet durch Moderator
von Duke Scarring (Gast)


Lesenswert?

Willy M. schrieb:
> Würde man das so in einem FPGA implementieren?
Für sowas "kleines" würde ich keine Komponente erstellen, sondern 
einfach
1
if D0 = D1 then
2
  ...
3
end if;
schreiben.

Duke

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.