#4.Binary/Gray counter(이진,그레이 카운터)
논리회로설계실험(VHDL) 2020. 12. 27. 02:521. 배경이론
이번 실습은 Counter을 설계하는 실습으로, 논리회로도에서 State machine을 사용합니다. 이진수로 1씩 증가하는 counter와 graycode상태로 1씩 증가하는 counter로 두가지 작동방식의 counter을 설계합니다. 기본적인 State machine에 대한 설명은 생략합니다.
위 그림은 counter의 작동방식을 간단히 나타낸 그림입니다. 검은색화살표는 2진수 상태에서 입력이 1이 들어왔을 때, 이동하는 state를 알려주고, 빨간색 화살표는 gray code상에서 0과 1 입력에 따라 이동하는 state를 나타낸 것입니다.
gray code와 이진수는 다음과 같습니다.
2. Counter VHDL 코드
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
entity Counter is
Port ( clk : in STD_LOGIC; --포트 구성 clk(1bit) : 클럭선언
mode : in STD_LOGIC; --포트 구성 mode(1bit) : counter의 종류(gray/binary)를 결정해줄 mode선언
rst_n : in STD_LOGIC; --포트 구성 cnt(1bit) : reset값 선언
cnt : out STD_LOGIC_VECTOR (2 downto 0)); --포트 구성 output : cnt(3bit), 출력값 선언
end Counter;
architecture Behavioral of Counter is
signal state : STD_LOGIC_VECTOR (2 downto 0); --내부 신호 구성 state 현재상태
signal next_state : STD_LOGIC_VECTOR (2 downto 0); --내부 신호 구성 next_state 다음상태
begin
cnt <= state; --현재상태 state를 cnt로 출력
process(clk, rst_n) -- process 시작
begin
if(rst_n ='0') then --rst_n이 0이면 state를 000으로 리셋
state<="000";
elsif(clk='1' and clk'event)then --rst_n가 1이고 clk 가 rising edge마다 next_state를 state로 대입
state<=next_state;
end if; --if문 종료
end process; --process 종료
process(state, mode) --mode에 따라 state의 변화를 주는 process 시작, mode가 1일때 binary counter, 0일대 gray counter
begin
case state is --state에 따라 case문 시작
when "000"=> --state가 000일때 동작
if(mode='1')then --mode가 1일때 다음상태에 들어갈 값을 결정, binary counter
next_state <= "001"; --000->001
else --mode가 0일때 다음상태에 들어갈 값을 결정, gray counter
next_state <= "001"; --000->001
end if; --if문 종료
when "001"=> --state가 001일때 동작
if(mode='1')then --binary counter
next_state <= "010"; --001->010
else --gray counter
next_state <= "011"; --001->011
end if; --if문 종료
when "010"=> --state가 010일때 동작
if(mode='1')then --binary counter
next_state <= "011"; --010->011
else --gray counter
next_state <= "110"; --010->110
end if; --if문 종료
when "011"=> --state가 011일때 동작
if(mode='1')then --binary counter
next_state <= "100"; --011->100
else --gray counter
next_state <= "010"; --011->010
end if; --if문 종료
when "100"=> --state가 100일때 동작
if(mode='1')then --binary counter
next_state <= "101"; --100->101
else --gray counter
next_state <= "000"; --100->000
end if; --if문 종료
when "101"=> --state가 101일때 동작
if(mode='1')then --binary counter
next_state <= "110"; --101->110
else --gray counter
next_state <= "100"; --101->100
end if; --if문 종료
when "110"=> --state가 110일때 동작
if(mode='1')then --binary counter
next_state <= "111"; --110->111
else --gray counter
next_state <= "111"; --110->111
end if; --if문 종료
when others=> --state가 111일때 동작, 마지막 구문이므로 when others 이용
if(mode='1')then --binary counter
next_state <= "000"; --111->000
else --gray counter
next_state <= "101"; --111->101
end if; --if문 종료
end case; --case문 종료
end process; --process 종료
end Behavioral;
3. TestBench Code
LIBRARY ieee;
USE ieee.std_logic_1164.ALL;
ENTITY Counter_TB IS
END Counter_TB;
ARCHITECTURE behavior OF Counter_TB IS
-- Component Declaration for the Unit Under Test (UUT)
COMPONENT Counter
PORT(
clk : IN std_logic; --포트 구성 input : clk, 클럭 선언
mode : IN std_logic; --포트 구성 input : mode, counter의 종류(gray/binary)를 결정
rst_n : IN std_logic; --포트 구성 input : reset 변수 선언
cnt : OUT std_logic_vector(2 downto 0) --포트 구성 output : cnt(3bit)
);
END COMPONENT;
--Inputs
signal clk : std_logic := '0'; --signal 선언, input 값 clk, mode, rst_n이 0으로 초기화 된다.
signal mode : std_logic := '0';
signal rst_n : std_logic := '0';
--Outputs
signal cnt : std_logic_vector(2 downto 0); --signal 선언, output 값 cnt(3bit)
-- Clock period definitions
constant clk_period : time := 10 ns; --clk가 10ns의 주기를 갖도록 선언
BEGIN
-- Instantiate the Unit Under Test (UUT)
uut: Counter PORT MAP (
clk => clk,
mode => mode,
rst_n => rst_n,
cnt => cnt
);
-- Clock process definitions
clk_process :process --clk process 시작
begin
clk <= '0'; --clk가 0으로 바뀐후 5ns 딜레이
wait for clk_period/2;
clk <= '1'; --clk가 1로 바뀐후 5ns 딜레이
wait for clk_period/2;
end process;
-- Stimulus process
stim_proc: process
begin
wait for 30 ns; --30ns대기, 누적 30ns
rst_n <= '1'; --rst_n와 mode에 1을 대입한다(binary counter)
mode <= '1';
wait for 100 ns; --100ns대기, 누적 130ns (100ns동안 counter 동작)
rst_n <= '0'; --rst_n에 0대입(reset)
wait for 20 ns; --20ns대기, 누적 150ns
rst_n <= '1'; --rst_n에 1, mode에 0대입(gray counter)
mode <= '0';
wait for 100 ns; --100ns 대기, 누적 250ns(clk pulse가 25개 들어온 시간)
mode <= '1'; --mode에 1대입(mode high, binary counter)
wait for 50 ns; --50ns 대기, 누적 300ns
rst_n <= '0'; --rst_n에 0 대입(LOW)
wait;
end process;
END;
다음과 같은 wave form이 나오게 됩니다.
#5. RAM/ROM (0) | 2021.03.08 |
---|---|
#3. FlipFlop(플립플롭)을 이용한 ShiftRegister 구현 (0) | 2020.12.24 |
#2. 8 to 1 Multiplexer (0) | 2020.12.20 |
#1. Full adder(전가산기) (0) | 2020.10.22 |