Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

vhdl code for dividing two number

Status
Not open for further replies.

mina.nms

Junior Member level 3
Joined
Sep 1, 2010
Messages
29
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,468
hi
would you plz help me?
what is the problem with this vhdl code for dividing two numbers by state machine?
Thanks


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
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.numeric_STD.ALL;
 
 
entity divi is
    Port ( a,b : in  STD_LOGIC_VECTOR (15 downto 0);
           clk,e_in,res : in  STD_LOGIC;
           e_out : out  STD_LOGIC;
              div_out : out STD_LOGIC_VECTOR (15 downto 0);
            mod_out  : out STD_LOGIC_VECTOR (15 downto 0));
end divi;
 
architecture Behavioral of divi is
  type state is (init,divid);
  signal s_reg, s_next : state;
  signal div1,b1,dive : Unsigned (15 downto 0);
  signal mod1,mode: Unsigned (15 downto 0);
  
begin
  process (clk,res)
  
  begin
      if (res ='1') then
        s_reg <= init;
        elsif (clk'event and clk='1') then
        s_reg <= s_next;
      end if;
    end process;
    
  process (s_reg,div1)  
  
  begin
     
  case s_reg is
  
       when init =>  div1 <="0000000000000000";
                     b1 <= unsigned (b) ;
                      mod1 <= unsigned (a) ;
                            e_out<='0';
                       --if (e_in ='1') then
                       s_next <= divid;
                         -- end if;
                    
         when divid => if (mod1>=0)then
                       mod1<=mod1-b1;
                       div1<=div1+1;
                            else
                            mode<=mod1+b1;
                           dive<=div1-1;
                            
                            --div_out <= STD_LOGIC_VECTOR (dive);
                      --mod_out <= STD_LOGIC_VECTOR (mode);
                              
                            e_out <='1';
                       --s_next <= init;
                            end if;
                            
                       
    end case;
    
    end process;
   
                        
end Behavioral;

 
Last edited by a moderator:

Hi,
First I think that you might have some latches from your combinatory process (the state machine).
They are some signals missing in the sensitivity list of your second process.
Second I don't really understand how you do the division. Do you got some kind of errors
or something?
 

Are You asking for Greatest Common Divisor Code??which is the largest positive integer that divides both numbers without remainder.
 

As DRO points out you have missing signals in your sensitivity list. You are missing signal assignment for all of the outputs in each branch of the FSM combinational process, that will produce latches as outputs not defined in a branch will need to hold their current state when the FSM takes that branch.

This is the reason why so many people use the single (clocked) process FSM style, it avoids these two process FSM style pitfalls.

Given the commenting out of the enable, I also suspect you were getting X's in your simulation so removed the enable to start the divide operation. The X out of the following code:
Code:
if (e_in ='1') then
  s_next <= divid;
end if;
was a result of not having an assignment for when e_in = '0' i.e.:
Code:
if (e_in = '1') then
  s_next <=divid;
else  -- specifies what to do when e_in = '0'
  s_next <= init;
end if;

As you didn't clarify exactly what was wrong, those are the first tier of problems in the code, that you need to correct.


Regards
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top