Search code examples
casevhdlfpgaxilinx

How do I rewrite this VHDL code to prevent latches?


architecture Behavioral of REGISTERS is

type REG_FILE_TYPE is array(0 to 15) of STD_LOGIC_VECTOR(15 downto 0);


signal REG_ARRAY : REG_FILE_TYPE:= (others => X"0000");
begin
process(WRITE_ENABLE,REG_CLOCK) is
 begin  
 
    case (WRITE_ENABLE) is
        when '1' => REG_ARRAY(to_integer(unsigned(WRITE_ADDRESS))) <= REG_INPUT;
        when '0' => NULL;
        when others => NULL;
    end case;
    
end process;

Solution

  • You forgot to add the clock to the process. If there's no clock, and the synthesizer infers that you have a need some memory for the implementation, then it has no other option than to add latches. This is almost always a mistake.

    Besides that, I don't like your case structure here, where a simple if-then would do. This is how I'd implement it:

    process(REG_CLOCK) is
      begin  
        if rising_edge(REG_CLOCK) then
          if WRITE_ENABLE = '0' then
            REG_ARRAY(to_integer(unsigned(WRITE_ADDRESS))) <= REG_INPUT;
          end if;
        end if:
    end process;