VHDL Case-When 문 마스터하기: 효율적 멀티플렉서 설계
Case-When 문은 시그널, 변수, 표현식에 따라 VHDL 프로그램을 여러 경로 중 하나로 이끕니다. 긴 if-then-elsif-else 체인보다 깔끔하고 읽기 쉬운 대안이며, 특히 여러 이산 값을 다룰 때 유용합니다.
많은 프로그래밍 언어가 switch나 case 구문을 쓰는데, VHDL에서 case 구문은 멀티플렉서와 다른 선택 로직 구축의 필수 요소입니다.
이 글은 Basic VHDL Tutorials 시리즈의 일부입니다.
기본 문법
case 문의 정식 구조:
case <expression> is when <choice> => -- code for this branch when <choice> => -- code for this branch ... end case;
<expression>은 보통 시그널이나 변수입니다. 케이스에 많은 when 분기가 있어도 시뮬레이션 중 하나만 실행됩니다.
각 <choice>는 다음 형태가 될 수 있습니다:
- 단일 값, 예:
when "11" =>
- 범위, 예:
when 5 to 10 =>
- 값 목록, 예:
when 1|3|5 =>
others 키워드: 명시되지 않은 모든 값과 매칭.
실전 예제: 4-to-1 멀티플렉서 만들기
아래는 if-then-elsif와 case 두 방식으로 4-to-1 멀티플렉서를 구현한 완전한 VHDL 코드입니다.
library ieee; use ieee.std_logic_1164.all; use ieee.numeric_std.all; entity T14_CaseWhenTb is end entity; architecture sim of T14_CaseWhenTb is signal Sig1 : unsigned(7 downto 0) := x"AA"; signal Sig2 : unsigned(7 downto 0) := x"BB"; signal Sig3 : unsigned(7 downto 0) := x"CC"; signal Sig4 : unsigned(7 downto 0) := x"DD"; signal Sel : unsigned(1 downto 0) := (others => '0'); signal Output1 : unsigned(7 downto 0); signal Output2 : unsigned(7 downto 0); begin -- Stimuli for the selector signal process is begin wait for 10 ns; Sel <= Sel + 1; wait for 10 ns; Sel <= Sel + 1; wait for 10 ns; Sel <= Sel + 1; wait for 10 ns; Sel <= Sel + 1; wait for 10 ns; Sel <= "UU"; wait; end process; -- MUX using if-then-else process(Sel, Sig1, Sig2, Sig3, Sig4) is begin if Sel = "00" then Output1 <= Sig1; elsif Sel = "01" then Output1 <= Sig2; elsif Sel = "10" then Output1 <= Sig3; elsif Sel = "11" then Output1 <= Sig4; else Output1 <= (others => 'X'); end if; end process; -- Equivalent MUX using a case statement process(Sel, Sig1, Sig2, Sig3, Sig4) is begin case Sel is when "00" => Output2 <= Sig1; when "01" => Output2 &= Sig2; when "10" => Output2 <= Sig3; when "11" => Output2 <= Sig4; when others => Output2 <= (others => 'X'); end case; end process; end architecture;
ModelSim에서 시뮬레이션 실행 후 파형은 Output1과 Output2가 동일한 동작을 보임을 보여줍니다. 셀렉터 Sel이 미정의 값(예: "UU")을 가질 때 when others => 절이 'X'를 출력해 알 수 없는 상태를 알립니다.
콘솔 출력에 50ns에서 메타값 비교 경고가 나옵니다:
VSIM 2> run # ** Warning: NUMERIC_STD."=": metavalue detected, returning FALSE # Time: 50 ns Iteration: 1 Instance: /t14_casewhentb # ** Warning: NUMERIC_STD."=": metavalue detected, returning FALSE # Time: 50 ns Iteration: 1 Instance: /t14_casewhentb # ** Warning: NUMERIC_STD."=": metavalue detected, returning FALSE # Time: 50 ns Iteration: 1 Instance: /t14_casewhentb # ** Warning: NUMERIC_STD."=": metavalue detected, returning FALSE # Time: 50 ns Iteration: 1 Instance: /t14_casewhentb
핵심 정리
- 긴
if-then-elsif 체인을 case 문으로 대체해 가독성 향상.
when others =>로 기본 경로 구현해 예상치 못한 값 처리.
- 멀티플렉서는 VHDL에서
case 구문으로 가장 깔끔하게 표현된다.
다음 튜토리얼로 넘어가 더 고급 VHDL 개념을 배우세요.