un package con funzione somma e prodotto ;)

Sezione dedicata al linguaggio di descrizione hardware per logiche programmabili

un package con funzione somma e prodotto ;)

Postby christian007 » 24 Oct 2013, 14:32

Salve a tutti ,
sono Christian e sono un nuovo utente. Volevo esporvi il mio problema. Devo simulare con Aldec active student .

un package che contiene la funzione di somma e la funzione di prodotto
per due standard logic vector contenenti valori a X

supponga di avere 1X01+01X0, attualmente con l'unsigned avrebbe XXXX in uscita
in realta' non e' cosi'
1X01+
00X0=
---------
1XX1

perche' in alcuni casi so che se un operando ha un bit a X il carry interno e' 0

poi servirebbe una utility che dato un vettore con elementi a X ritorna un vettore con tutti questi a 1 o a 0 dipendentemente da un flag


Vi ringrazio in anticipo per la disponibilità.
Trovo questo forum molto bello.
christian007
 
Posts: 6
Joined: 24 Oct 2013, 10:49
Location: Ferrara

Re: un package con funzione somma e prodotto ;)

Postby Leonardo » 25 Oct 2013, 09:34

Salve Christian e benvenuto al forum.
Potresti postare il codice che hai scritto con l'unsigned?
Il mio blog di elettronica: http://electro-logic.blogspot.it
User avatar
Leonardo
 
Posts: 502
Joined: 29 May 2013, 22:31
Location: Parma

Re: un package con funzione somma e prodotto ;)

Postby christian007 » 29 Oct 2013, 12:24

Leonardo wrote:Salve Christian e benvenuto al forum.
Potresti postare il codice che hai scritto con l'unsigned?


Code: Select all
-------------------------------------------------------------------------------
--
-- Title       : \ addern2\
-- Design      : somma_prodotto
-- Author      : Christian
-- Company     : student
--
-------------------------------------------------------------------------------
--
-- File        : somma_prodotto.vhd
-- Generated   : Tue Oct 29 10:57:21 2013
-- From        : interface description file
-- By          : Itf2Vhdl ver. 1.22
--
-------------------------------------------------------------------------------
--
-- Description :
--
-------------------------------------------------------------------------------

   library ieee;    
   use ieee.std_logic_1164.all;    
   use ieee.numeric_std.all;--son definiti tipo di dato e degli stan- dard per gestire le operazioni di tipo numerico sui dati    
   entity addern2 is generic(n: natural:=8);       
      port(a,b: in std_logic_vector (n-1 downto 0);
           cin: in std_logic; --resto (carry)
           sum: out std_logic_vector(n-1 downto 0);
            ov:out std_logic); --over flow** vale '1' se il risultato non è contenuto in n bit
           cout:out std_logic); 
   
   --carry end entity addern2;
   
architecture signed3 of addern2 is
signal result, carry: unsigned(n downto 0);
constant zeros: unsigned(n-1 downto 0) := (others => '0');--riempe di 0 il vettore

begin p0: process(a,b,cin,result,carry) begin carry<=zeros & cin;
   
   result<=((a(n-1)& unsigned(a)) + (b(n-1)& (b)) + unsigned carry);    
   --signed(a) converte un vettore std ad un dato signed    
   --si incatenano mantenendo il segno per avere operandi della stessa dimensione
   
sum<=std_logic_vector(result(n-1 downto 0));--son tipo di dato diversi->occorre conversione sul vettore
cout<=result(n);

--il singolo bit non ha bisogno della conversione

if((result(n-1)='1') and (a(n-1)='0') and (b(n-1)='0'))    
then
ov<='1';
elsif((result(n-1)='0') and (a(n-1)='1') and (b(n-1)='1'))   
then
ov<='1'; else ov<='0';
end if;   
end process p0;
end architecture signed3;
christian007
 
Posts: 6
Joined: 24 Oct 2013, 10:49
Location: Ferrara

Re: un package con funzione somma e prodotto ;)

Postby Altero » 31 Oct 2013, 14:54

@cristian,
vorrei aiutarti, ma non mi è chiaro quello che chiedi.
ammetto di non essere un esperto in vhdl, ma non riesco a capire quale è il tuo problema!!

Prova a spiegare il tutto con parole più semplici, proprio per noi mortali. :)

>> Il codice è stato prodotto automaticamente da Lattice Symplify?

altero
Altero
 
Posts: 46
Joined: 20 Apr 2012, 09:33

Re: un package con funzione somma e prodotto ;)

Postby christian007 » 31 Oct 2013, 15:51

non lo ho scritto io con ALDEC -HDL FOR STUDENT TIPO (SIMULI LIKN).VORREI ILCODICE DI packege chi contiene fa la somma e il prodotto di due std_logic_vector in linguaggio vhdl
christian007
 
Posts: 6
Joined: 24 Oct 2013, 10:49
Location: Ferrara

Re: un package con funzione somma e prodotto ;)

Postby Leonardo » 31 Oct 2013, 20:13

Partiamo dalla definizione del tipo UNSIGNED:

Code: Select all
type UNSIGNED is array (NATURAL range <>) of STD_LOGIC;


Come si può notare è un array di STD_LOGIC quindi supporta intrinsecamente il tipo indefinito (ovvero 'X')

L'operatore somma in caso di valori contententi 'X' restituisce però un valore completamente non definito come è possibile dedurre dalla sua implementazione

Code: Select all
  function "+" (L, R: UNSIGNED) return UNSIGNED is
    constant SIZE: NATURAL := MAX(L'LENGTH, R'LENGTH);
    variable L01 : UNSIGNED(SIZE-1 downto 0);
    variable R01 : UNSIGNED(SIZE-1 downto 0);
    -- Synthesis directives :
    attribute SYNTHESIS_RETURN of L01:variable is "ADD" ;
  begin
    if ((L'LENGTH < 1) or (R'LENGTH < 1)) then return NAU;
    end if;
    L01 := TO_01(RESIZE(L, SIZE), 'X');
    if (L01(L01'LEFT)='X') then return L01;
    end if;
    R01 := TO_01(RESIZE(R, SIZE), 'X');
    if (R01(R01'LEFT)='X') then return R01;
    end if;
    return ADD_UNSIGNED(L01, R01, '0');
  end "+";


"Bypassando" i vari controlli è possibile utilizzare direttamente la funzione ADD_UNSIGNED così definita inserendola all'interno del proprio codice in quanto non dichiarata dall'interfaccia del package numeric_std.

Code: Select all
 
  -- this internal function computes the addition of two UNSIGNED
  -- with input CARRY
  -- * the two arguments are of the same length
  function ADD_UNSIGNED (L, R: UNSIGNED; C: STD_LOGIC) return UNSIGNED is
    constant L_LEFT: INTEGER := L'LENGTH-1;
    alias XL: UNSIGNED(L_LEFT downto 0) is L;
    alias XR: UNSIGNED(L_LEFT downto 0) is R;
    variable RESULT: UNSIGNED(L_LEFT downto 0);
    variable CBIT: STD_LOGIC := C;
  begin
    for I in 0 to L_LEFT loop
      RESULT(I) := CBIT xor XL(I) xor XR(I);
      CBIT := (CBIT and XL(I)) or (CBIT and XR(I)) or (XL(I) and XR(I));
    end loop;
    return RESULT;
  end ADD_UNSIGNED;


Tale funzione ritorna correttamente 1XX1 nella somma 1X01+00X0

Leonardo
Il mio blog di elettronica: http://electro-logic.blogspot.it
User avatar
Leonardo
 
Posts: 502
Joined: 29 May 2013, 22:31
Location: Parma

Re: un package con funzione somma e prodotto ;)

Postby christian007 » 02 Nov 2013, 10:20

grazie ma ,per la funzione prodotto come faccio allora ,come si potrebbe implementare insieme alla funzione somma grazie in anticipo
christian007
 
Posts: 6
Joined: 24 Oct 2013, 10:49
Location: Ferrara

Re: un package con funzione somma e prodotto ;)

Postby Leonardo » 02 Nov 2013, 10:47

Prova innanzitutto a pensare all'algoritmo da utilizzare per effettuare la moltiplicazione

Ciao
Il mio blog di elettronica: http://electro-logic.blogspot.it
User avatar
Leonardo
 
Posts: 502
Joined: 29 May 2013, 22:31
Location: Parma

Re: un package con funzione somma e prodotto ;)

Postby christian007 » 05 Nov 2013, 12:08

MI potresti verificare gli errori qua, non capisco niente


Code: Select all
 
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
use IEEE.NUMERIC_STD.ALL;

entity Multiplier is
Port ( a : in SIGNED (31 downto 0);
b : in SIGNED (31 downto 0);
p : out SIGNED (31 downto 0));
end Multiplier;

architecture aMultiplier of Multiplier is
begin

process (a, b) is

constant T_in_out : time := 20 ns;

variable negative_result : boolean;
variable op1 : signed(31 downto 0);
variable op2 : signed(31 downto 0);
variable result : signed(63 downto 0);
variable carry_in, carry: std_ulogic;
begin

op1 := a;
op2 := b;
-- controlla e ritorna il segno del risultato
negative_result := (op1(op1'left) = '1') xor (op2(op2'left) = '1');
-- porta positivi tutti e due gli operandi
if (op1(op1'left) = '1') then
carry := '1';
for index in op1'reverse_range loop
carry_in := carry;
carry := carry_in and not op1(index);
op1(index) := not op1(index) xor carry_in;
end loop;
end if;
if (op2(op2'left) = '1') then
carry := '1';
for index in op2'reverse_range loop
carry_in := carry;
carry := carry_in and not op2(index);
op2(index) := not op2(index) xor carry_in;
end loop;
end if;

-- esegue la moltiplicazione
result := (others => '0');

for count in op2'reverse_range loop
carry := '0';
if (op2(count) = '1') then

for index in op1'reverse_range loop
carry_in := carry;
carry := ((result(index + count) and op1(index)) or (carry_in and (result(index + count) xor op1(index))));
result(index + count) := result(index + count) xor op1(index) xor carry_in;
end loop;
result(count + 16) := carry;

end if;
end loop;

-- in base al segno del risultato calcolato prima
-- si riporta il risultato negativo se necessario
if negative_result then

carry := '1';
for index in p'reverse_range loop
carry_in := carry;
carry := carry_in and not result(index);
result(index) := not result(index) xor carry_in;
end loop;

end if;

-- Riporta nel segnale di uscita solamente
-- i 32 bit significativi   
p <= result(60 downto 29) after T_in_out;

end process;

end aMultiplier;
christian007
 
Posts: 6
Joined: 24 Oct 2013, 10:49
Location: Ferrara

Re: un package con funzione somma e prodotto ;)

Postby deluca » 05 Nov 2013, 16:06

@Christian,
http://www.hwupgrade.it/forum/archive/i ... 30279.html
facendo in quel modo ci ricavi poco e capisci ancora meno ;)

Ti consiglio di studiare bene il prb e valutare l'acquisto di un buon libro....
Su che testo stai studiando?
Ciao
Il mio sito: http://www.delucagiovanni.com ......e la chat: chat/
User avatar
deluca
Site Admin
 
Posts: 1104
Joined: 19 Jun 2011, 10:44
Location: 95123 - Catania (Italy)

Re: un package con funzione somma e prodotto ;)

Postby Leonardo » 05 Nov 2013, 21:37

@Christian
Concordo con Giovanni che andrai veramente poco lontano così facendo..
Il mio blog di elettronica: http://electro-logic.blogspot.it
User avatar
Leonardo
 
Posts: 502
Joined: 29 May 2013, 22:31
Location: Parma


Return to VHDL x FPGA

Who is online

Users browsing this forum: No registered users and 3 guests

cron