[FPGA]

Latch와 Flip Flop의 차이

Neo Park 2012. 4. 6. 14:11

 

HDL을 사용하여 하드웨어를 설계할 때 Latch와 Flip flop은 매우 중요한 요소입니다.

HDL은 하드웨어를 설계하는 언어임을 고려할 때 잘못된 코드는 쓸데없는 Latch를 생성시켜서 최적화 되지 못한

하드웨어를 생성시킬 수 있습니다.

따라서 어떻게 설계언어를 기술하여 쓸테없는 래치가 생성되지 않게 할 수 있을까 하는 문제는 꼭 생각해 보아야 할 것이므로

이번에는 래치와 플립플롭에 대한 설명을 해 보고자 합니다.

이 내용은 Weng Fook Lee의 저서인 “VHDL coding and logic synthesis with SYNOPSYS®”, Academic Press 을 참조하였습니다.

 

1. Latch

 

 


래치는 상기 그림에서 clock이 로직 1일 때 입력데이터(data_in)을 출력데이터 (data_out)으로 보내며,

최종 출력된 값은 clock이 로직 0인동안 계속 유지되는 특성을 가집니다.


(래치를 합성하는 코드예)
Library IEEE;
Use IEEE.std_logic_1164.all;
entity latch_ent is
port (
       data_in : IN std_logic;
       clock : IN std_logic;
       data_out: OUT std_logic
);
end entity latch_ent;

 

architecture latch_arch of latch_ent is
begin
Process(data_in, clock)
begin
    IF (Clock = ‘1’) then
        data_out <= data_in;
     end if;
end process;
end architecture latch_arch;

 


2. 코드 작성시 Latch생성 피하기
많은 설계자들은 합성가능한 VHDL코딩을 배우면서, 원하지 않는 래치가 합성되어 생성되는 것을 종종 보게 됩니다.

이러한 래치들은 합성툴에서 자동으로 생성시키는 것이며 이는 설계자가 코드내에 중요한 구조를 빠뜨렸기 때문입니다.
원치않는 래치가 생성되는 것은 주로 설계한 VHDL 코드가 지정되어야 하는 전체 조건이 가지수 모두에 대하여 정의되지

않았을 경우에 생성됩니다.

예를 들어 IF 문을 사용하는 경우, 그 가능한 조건들에 대하여 모두 동작정의가 되어 있지 않으면 래치가 합성됩니다.

 


(IF문에서 원치않는 래치 생성의 예)
Library IEEE;
USE IEEE.std_logic_1164.all;
entity ifwl_ent is
port (
       input1 : in std_logic;
       input2 : in std_logic
       input3 : in std_logic;
       input4: in std_logic;
       selector : in std_logic_vector(1 downto 0);
       output1 : out std_logic
);
end entity ifwl_ent;
architecture ifwl_arch of ifwl_ent is
       signal internal : std_logic=’0’;
       begin

      

       process (input1, input2, ipnut3, input4, selector)
       begin
              if (selector = “00”) then              
                     internal <= input1;
              elsif (selector = “01”) then
                     internal <= input2;
              elsif (selector = “10” ) then
                     internal <= input3;
              end if;
       end process;
output1 <= internal;
end architecture ifwl_arch;


위의 예에서 selector는 “00”, “01”, “10”, “11” 에대하여 정의가 되어야 하나 “11”에 대한 정의가 안되어 있으므로 래치가 생성됩니다.

위의 if문을 아래와 같이 바꾸면 모든 조건에 대한 정의가 되므로 래치는 생성되지 않습니다

 

process (input1, input2, ipnut3, input4, selector)
begin
       if (selector = “00”) then
              internal <= input1;
       elsif (selector = “01”) then
              internal <= input2;
       elsif (selector = “10” ) then
              internal <= input3;
       elsif (selector = “11” ) then
              internal <= input4;
       end if;
end process;.

 

 

그러다면 case 문은 어떨까? 역시 마찬가지 겠죠?

Case문에 있어서도 모든 조건에 대하여 동작 정의가 되지 않으면 래치가 생성됩니다.


( 래치가 생성되는 case문 예 )
Library IEEE;
use IEEE.std_logic_1164.all;

 

entity casewl_ent is
port (
       input1: in std_logic;
       input2: in std_logic;
       input3: in std_logic;
       input4: in std_logic;
       selector : in std_logic_vector (2 downto 0);
       output1: out std_logic
);
end entity casewl_ent;;
architecture casewl_arch of casewl_ent is
       begin
       process (input1, input2, input3, input4, selector )
              begin
              case selector is
              when “000” =>
                     output1 <= input1;
              when “001” =>
                     output1 <= input2;
              when “010” =>
                     output1 <= input3;
              when others =>
              NULL;
              end case;
       end process;
end architecture casewl_arch;


이와 같은 코드에서 case문을 보면 3비트 selector가 가질수 있는 조건은 8가지 이지만 3가지 경우에서만 정의되어있고

그 외는 정의가 되어있지 않으므로 output1의 이전값을 저장하는 래치가 생성됩니다.

이러한 케이스 문을 다음과 같이 바꾸어 봅시다.
이와 같이 when others부에 output <=’0’ 으로써 모든 경우에대하여 로직값을 정의 하였으므로

합성된 회로는 래치를 생성하지 않을 것입니다.
물론 default 값을 정의 하여 래치가 생성되지 않도록 할 수도 있으나 VHDL코딩을 연습하는데 있어서는

모든 조건을 기술하는(full coverage) IF문과 case문을 작성하는 것이 좋다고할 수 있습니다.

 

case selector is
       when “000” =>
              output1 <= input1;
       when “001” =>
              output1 <= input2;
       when “010” =>
              output1 <= input3;
       when others =>
              output1 <= ‘0’;
       end case;

 

3. Flip-Flop
플립플롭은 래치와 비슷하지만, 다른즘은 클럭이 Low에서 high로 변할 때(positive edge triggered filp-flop)또는

클럭이 high에서 Low로 변할 때 (negative edge triggered flip-flop)일때만 입력데이터를 출력데이터로 전달합니다.

 

 

 

 

(Flip-flop 의 예)
Library IEEE;
USE IEEE.std_logic_1164.all;
entity flop_ent is
port (
       data_in : IN std_logic;
       clock : IN std_logic;
       data_out : OUT std_logic
);
end entity flop_ent;

 

architecture flop_arch of flop_ent is
begin
process ( data_in, clock )
       begin
       if (clock = ‘1’ and clock’event) then
              data_out <= data_in;
       end if;
       end process;
end architecture flop_arch;


이와 같이 clock 에 변화가 있고 그변화가 ‘1’로 되는 시점에서, 즉 상승모서리시점에서 입력데이터는 출력 데이터로

전달되는 flip-flop이 생성됩니다.