Best practice, design and code

From Robin

(Difference between revisions)
Jump to: navigation, search
(Sequential logic)
(Sequential logic)
Line 5: Line 5:
= RTL code =
= RTL code =
== Sequential logic ==
== Sequential logic ==
-
* Separate registers from combinational logic
+
'''Separate registers from combinational logic'''
-
By separating registers from combinational logic you will reduce the risk of creating more registers than you planned to do. By keeping combinational logic in processes that are solely combinational, you will get the full benefit of warnings from the toolchain if you accidentally create latches or registers within the code.  
+
* By separating registers from combinational logic you will reduce the risk of creating more registers than you planned to do. By keeping combinational logic in processes that are solely combinational, you will get the full benefit of warnings from the toolchain if you accidentally create latches or registers within the code.  
-
* Register reset should be stated where the registers are assigned
+
'''Register reset should be stated where the registers are assigned'''
-
Reset is for clearing registers to have them in a predictable state. Although synchronous reset can be stated as combinational logic, the use of reset is setting registers, not interfering with the combinational code function. This may seem as a slight contradiction to the bullet above, but for the sake of modifiability and general readability, it makes sense to keep reset   
+
* Reset is for clearing registers to have them in a predictable state. Although synchronous reset can be stated as combinational logic, the use of reset is setting registers, not interfering with the combinational code function. This may seem as a slight contradiction to the bullet above, but for the sake of modifiability and general readability, it makes sense to keep reset   
'''Sequental logic example:'''   
'''Sequental logic example:'''   
-
  REGISTERS: process(clk) is  
+
  REGISTERS: '''process'''(clk) '''is'''
-
   begin  
+
   '''begin'''
-
     if rising_edge(clk) then  
+
     '''if''' rising_edge(clk) '''then'''
-
       if reset then  
+
       '''if''' reset '''then'''
         r_signal <= '0';
         r_signal <= '0';
-
         r_vector <= (others => '0');
+
         r_vector <= ('''others''' => '0');
-
       else
+
       '''else'''
         r_signal <= next_signal;
         r_signal <= next_signal;
         r_vector <= next_vector;
         r_vector <= next_vector;
-
       end if;
+
       '''end if''';
-
     end if;
+
     '''end if''';
-
   end process;   
+
   '''end process''';   
    
    
  COMBINATIONAL_LOGIC:
  COMBINATIONAL_LOGIC:
-
  next_signal <= valid_a and valid_b;
+
  next_signal <= valid_a '''and''' valid_b;
-
  next_vector <= std_logic_vector("0" & unsigned(a) + "0" & unsigned(b));
+
  next_vector <= '''std_logic_vector'''("0" & '''unsigned'''(a) + "0" & '''unsigned'''(b));
== FSM==
== FSM==

Revision as of 12:44, 15 March 2023


The rules noted here are guidelines that will help creating designs that are easy to verify, read and maintain

RTL code

Sequential logic

Separate registers from combinational logic

  • By separating registers from combinational logic you will reduce the risk of creating more registers than you planned to do. By keeping combinational logic in processes that are solely combinational, you will get the full benefit of warnings from the toolchain if you accidentally create latches or registers within the code.

Register reset should be stated where the registers are assigned

  • Reset is for clearing registers to have them in a predictable state. Although synchronous reset can be stated as combinational logic, the use of reset is setting registers, not interfering with the combinational code function. This may seem as a slight contradiction to the bullet above, but for the sake of modifiability and general readability, it makes sense to keep reset

Sequental logic example:

REGISTERS: process(clk) is 
  begin 
    if rising_edge(clk) then 
      if reset then 
        r_signal <= '0';
        r_vector <= (others => '0');
      else
        r_signal <= next_signal;
        r_vector <= next_vector;
      end if;
    end if;
  end process;  
  
COMBINATIONAL_LOGIC:
next_signal <= valid_a and valid_b;
next_vector <= std_logic_vector("0" & unsigned(a) + "0" & unsigned(b));

FSM

  • Keep register assignment separate from combinational logic (as with/see sequential logic)
  • Keep calculation of next state separate from state output

While this is stating the same criteria (state tree) twice, it makes reading the state machine behavior easier. In contrast, having next_state logic mixed with state output, debugging one will include debugging the other. It does not take much logic before the smokescreen effect is notable when reading code.

FSM Example:

Personal tools
Front page