sam93
Junior Member level 1
- Joined
- Jul 16, 2015
- Messages
- 15
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 241
Code VHDL - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 process (CLK_20M) constant a : std_logic := "1000111101110000000"; --293760 variable counter: std_logic_vector(25 downto 0) ; ;--26 bits 67108863 variable count:std_logic_vector(25 downto 0) ; ;--26 bits variable rps: std_logic_vector(7 downto 0) ; -- 8 bits 255 begin if rising_edge(CLK_20M) then counter:=counter + 1; if (counter > 20000000) then counter := (others => '0'); elsif rising_edge(ENCODER_HALLSENSOR_B) then count := count + 1; end if; end if; rpss := count/a;
x / y = (x * (2**N / y)) / 2**N
Division is one of the more complicated basic operations. It makes sense to make use of better implementations when possible.
In this case, you either want to do division by multiplication by a constant, or long division by FSM.
Multiplication by a constant basically does:
x / y = (x * (2**N / y)) / 2**N
eg, you will find the value (2**N / y) and use this as the multiplier. Then you will multiply this with x. Then you will keep only the msbs that correspond to the integer portion.
counter:=counter + 1;
if (counter > 20000000) then
counter := (others => '0');
Code VHDL - [expand] 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 library ieee; use ieee.std_logic_1164.all; use IEEE.STD_LOGIC_unSIGNED.ALL; use ieee.numeric_std.all; use work.asci_types.all; entity dc1 is port (CLK_20M: in std_logic; dividend: in std_logic_vector(25 downto 0):="00000001001001001111100000"; divisor: in std_logic_vector(18 downto 0):="1001001001111100000"; quotient: out std_logic_vector(7 downto 0)); end dc1; architecture Behavioral of dc1 is signal a: std_logic_vector(25 downto 0); signal b: std_logic_vector(18 downto 0); signal q: std_logic_vector(7 downto 0); begin a<= dividend; b <= divisor; div_3: Process (CLK_20M,a,b) variable d: std_logic_vector(18 downto 0); variable c: std_logic_vector(7 downto 0); variable temp: std_logic_vector(19 downto 0); Begin if (a(25 downto 7) > b) then --7 d:= a(25 downto 7) - b; c(7) := '1'; else C(7) := '0'; temp := (a(25 downto 7) & a(6)); d:= (temp - b); end if; if (d > b) then --6 c(6) := '1'; d:= d-b; else C(6) := '0'; temp := (d & a(5)); d:= temp-b; end if; if (d>b) then --5 c(5) := '1'; d:= d-b; else C(5) := '0'; temp := (d & a(4)); d:= temp-b; end if; if (d>b) then --4 c(4) := '1'; d:=d-b; else C(4) := '0'; temp := (d & a(3)); d:= temp-b; end if; if (d>b) then --3 c(3) := '1'; d:= d-b; else C(3) := '0'; temp := (d & a(2)); d:= temp-b; end if; if (d>b) then --2 c(2) := '1'; d:= d-b; else C(2) := '0'; temp := (d & a(1)); d:= temp-b; end if; if (d > b) then --1 c(1) := '1'; d:=d-b; else C(1) := '0'; temp := (d & a(0)); d:= temp-b; end if; if (d>b) then --0 c(0) := '1'; else C(0) := '0'; end if; quotient <= c; end process; End Behavioral;
Code VHDL - [expand] 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 67 68 69 70 71 72 73 74 LIBRARY ieee; USE ieee.std_logic_1164.ALL; use IEEE.STD_LOGIC_unSIGNED.ALL; use ieee.numeric_std.all; use work.asci_types.all; -- Uncomment the following library declaration if using -- arithmetic functions with Signed or Unsigned values --USE ieee.numeric_std.ALL; ENTITY test IS END test; ARCHITECTURE behavioral OF test IS -- Component Declaration for the Unit Under Test (UUT) COMPONENT dc1 PORT( CLK_20M : IN std_logic; dividend : IN std_logic_vector(25 downto 0):="00000001001001001111100000"; divisor : IN std_logic_vector(18 downto 0):="1001001001111100000"; quotient : OUT std_logic_vector(7 downto 0) ); END COMPONENT; --Inputs signal dividend: std_logic_vector(25 downto 0):="00000001001001001111100000"; signal divisor: std_logic_vector(18 downto 0):="1001001001111100000"; signal quotient: std_logic_vector(7 downto 0); signal CLK_20M : std_logic := '0'; -- Clock period definitions constant CLK_20M_period : time := 50 ns; BEGIN -- Instantiate the Unit Under Test (UUT) uut: dc1 PORT MAP ( CLK_20M => CLK_20M, dividend => dividend, divisor => divisor, quotient => quotient ); -- Clock process definitions CLK_20M_process :process begin CLK_20M <= '0'; wait for CLK_20M_period/2; CLK_20M <= '1'; wait for CLK_20M_period/2; end process; -- Stimulus process stim_proc: process begin -- hold reset state for 100 ns. wait for 500 ns; wait for CLK_20M_period*10; -- insert stimulus here wait; end process; END behavioral;
Code VHDL - [expand] 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 library ieee; use ieee.std_logic_1164.all; use IEEE.STD_LOGIC_unSIGNED.ALL; use ieee.numeric_std.all; use work.asci_types.all; entity dc1 is generic ( n: integer :=7); port (CLK_20M: in std_logic; dividend: in std_logic_vector(25 downto 0):="01111111111111111111111111"; divisor: in std_logic_vector(19 downto 0):="01001001001111100000"; quotient: out std_logic_vector(7 downto 0)); end dc1; architecture Behavioral of dc1 is signal a: std_logic_vector(25 downto 0); signal b: std_logic_vector(19 downto 0); signal q: std_logic_vector(7 downto 0); signal d: std_logic_vector(19 downto 0); begin a<= dividend; b <= divisor; div_3: Process (a,b) Begin if (a(25 downto 7) > b) then --7 d<= (a(25 downto 7)- b); q(n) <= '1'; else q(n) <= '0'; d<= (a(25 downto 6) -b); end if; if (d>b) then --6 q(n-1) <= '1'; d<= d-b; else q(n-1) <= '0'; d<= (d & a(n-2) -b); end if; if (d>b) then --5 q(n-2) <= '1'; d<= d-b; else q(n-2) <= '0'; d<= (d & a(n-3) -b); end if; if (d>b) then --4 q(n-3)<= '1'; d<= d-b; else q(n-3) <= '0'; d<= (d & a(n-4) -b); end if; if (d>b) then --3 q(n-4) <= '1'; d<= d-b; else q(n-4) <= '0'; d<= (d & a(n-5) -b); end if; if (d>b) then --2 q(n-5) <= '1'; d<= d-b; else q(n-5) <= '0'; d<= (d & a(n-6) -b); end if; if (d>b) then --1 q(n-6) <= '1'; d<= d-b; else q(n-6) <= '0'; d<= (d & a(n-7) -b); end if; if (d>b) then --0 q(n-7)<= '1'; else q(n-7) <= '0'; end if; end process; quotient <= q; End Behavioral;
hi ads-ee! As you said I changed variables to signals, but simulation could not be complete!! I faced to this message "ERROR: In process dc1.vhd:div_3
Target Size 20 and source size 21 for array dimension 0 does not match."
and the quotient is not correct yet!
Code VHDL - [expand] 1 2 3 4 5 library ieee; use ieee.std_logic_1164.all; use IEEE.STD_LOGIC_unSIGNED.ALL; use ieee.numeric_std.all; use work.asci_types.all;
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?