Bonjour,
je suis entrain de faire un code pour le calcul des racines carrée en VHDL et voici le code :
Code vhdl : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all; -- Removed unused 'ieee.std_logic_unsigned.all;'
 
entity racine_algo1 is
    generic(n : integer := 8);
    port(
        clk : in std_logic;
        debut : in std_logic;
        init : in std_logic;
        A : in unsigned(2*n-1 downto 0) := (others => '0');
        fini : out std_logic;
        resultat : out unsigned(n-1 downto 0)
    );
end racine_algo1;
 
architecture bhv of racine_algo1 is
begin
    process(clk, init)
        variable input : unsigned(2*n-1 downto 0);
        variable x : unsigned(4*n-1 downto 0);
        variable v : unsigned(n-1 downto 0);
        variable z : unsigned(n-1 downto 0);
    begin
        if init = '1' then
            fini <= '0';
            resultat <= (others => '0');
            input := (others => '0');
        elsif rising_edge(clk) then
            if debut = '1' then
                input := A;
                x := "01000000000000000000000000000000";
                v := "01000000";
                z := "10000000";
            end if;
            for i in n-2 downto 0 loop
                if x > input then
                    x := x + v * (v - z sll 1);
                    z := z + v;
                elsif x < input then
                    x := x + v * (v + z sll 1);
                    z := z + v;
                end if;
                v := v srl 1;
            end loop;
            if x > input then
                resultat <= z-1;
            else
                resultat <= z;
            end if;
        end if;
    end process;
end architecture;

Cependant, le testbench pour ce code ne marche pas je ne sais pas pourquoi, il me donne toujours la valeur 11111110 quand j'essaie d'initialiser l'entrée avec la valeur 10101010101010101010 = (43690 en décimal).
Voici le testbench :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use ieee.math_real.all;
-- Remove "ieee.std_logic_unsigned.all;" as it's not needed in this context
 
entity test_algo1 is
end test_algo1;
 
architecture sim of test_algo1 is
    component racine_algo1 is
        generic(n : integer := 8);
        port(
            clk : in std_logic;
            debut : in std_logic;            
            init : in std_logic;
            A : in unsigned(2*n-1 downto 0);
            fini : out std_logic;
            resultat : out unsigned(n-1 downto 0)
        );
    end component;
 
    constant clk_period : time := 10 ns;
    constant n : integer := 8;
    signal clk,init,fini,debut : std_logic := '0';
    signal A : unsigned(2*n-1 downto 0) := (others => '0'); -- Added signal A
    signal resultat : unsigned(n-1 downto 0); -- Added signal resultat
    signal sq_root : unsigned(n-1 downto 0) := (others => '0');
    signal error : integer := 0;
 
begin
    clk <= not clk after clk_period/2;
 
    DUT : entity work.racine_algo1 generic map(n => n)
        port map(
            clk => clk,
            debut => debut,
            init => init,
            A => A,
            fini => fini,
            resultat => resultat);
 
    process
        variable tmp,i : integer := 0;
    begin
	A <= "0000000010101010"; 
        init <= '1';
        wait for clk_period;
        init <= '0';
	while(i <= 2**N-1) loop
            A <= to_unsigned(i,2*n);  --convert 'i' from integer to unsigned format.
            wait until fini='1';    --wait until the 'done' output signal goes high.
            wait until falling_edge(Clk);   --we sample the output at the falling edge of the clock.
            tmp := integer(floor(sqrt(real(i)))); --Calculate the actual result.
            --if actual result and calculated result are different increment 'error' by 1.
            if (tmp /= to_integer(sq_root)) then  
                error <= error + 1;
            end if; 
            i := i-1;   --increment the loop index.
        end loop;
	debut <= '1';
        wait for clk_period;
        debut <= '0';
	wait;
    end process;
end sim;

Merci pour votre retour par avance.
Coridialement