Computer Science/VHDL

[VHDL] Loop Statement | 반복문

lww7438 2020. 12. 14. 13:01

Loop Statement

반복문


while Loop Statement

Example. while Loop

VARIABLE result, count : integer;

...

result := 0;
count  := 1;

WHILE(count <= 10) LOOP
    result := result + count;
    count  := count + 1;
END LOOP;

for Loop Statement

- VHDL에서 for Loop의 Iteration Variable은 1씩 증가(TO 구문)되거나 감소(DOWNTO 구문)된다.
  (다른 PL에서처럼, Iteratino Varible의 증감폭을 프로그래머가 설정할 수 없다.)

Example. for Loop

TYPE int_vec_type IS array(1 to 10) OF integer;
VARIABLE i_squared     : int_vec_type;
VARIABLE i_decremented : int_vec_type;

...

FOR i IN 1 TO 10 LOOP  -- don't have to declare i
    i_squared(i) := i * i;
END LOOP;

FOR i IN 10 DOWNTO 1 LOOP
    i_decremented(i) := i - 1;
END LOOP;


Example. for Loop

TYPE day_of_week IS (sun, mon, tue, wed, thu, fri, sat)
    ...
    
FOR i IN day_of_week LOOP
    IF i = sat THEN
        son <= mow_lawn;
    ELSIF i = sun THEN
        church <= family;
    ELSE
        dad <= go_to_work;
    END IF;

- User Defined Type을 for문의 조건식에 삽입하여 자동으로 Indexing하게 할 수 있다.


Example. for Loop with next Statement

PROCESS (a, b)
    CONSTANT max : integer := 255;
    TYPE d_type IS array (0 to max) OF boolean;
    VARIABLE done : d_type;
BEGIN
    FOR i IN 0 TO max LOOP
        IF (done(i) = true) THEN
            NEXT;
        ELSE
            done(i) := true;
        END IF;
        
        q(i) <= a(i) AND b(i);
    END LOOP;
END PROCESS;


* \(\texttt{NEXT}\) Statement (= \(\texttt{continue}\) Statement in C)
- Stops execution of this iteration and goes on to the next iteration

Example. for Loop with exit Statement

PROCESS(a)
    CONSTANT max : integer := 255;
    VARIABLE int_a : integer;
BEGIN
    int_a := a;
    FOR i IN 0 TO max LOOP
        IF (int_a <= 0) THEN
            EXIT;
        ELSE
            int_a := int_a - 1;
            q(i) <= 3.1416 / real(int_a * i);
        END IF;
    END LOOP;
    y <= q;
END PROCESS;


* \(\texttt{exit}\) Statement = (\(\texttt{break}\) Statement in C)
- Jumps out of a loop statement currently in execution

Example. for Loop with Loop Label

PROCESS(a)
BEGIN
    first_loop : FOR i IN 0 TO 100 LOOP
        second_loop : FOR j IN to 10 LOOP
            ...
            EXIT second_loop;
            ...
            EXIT first_loop;
        END LOOP;
    END LOOP;
END PROCESS;

\(\texttt{assert}\) Statement

- 코드에서의 오류를 확인할 때 사용하는 구문이다.
- Boolean Expression의 값을 확인하고,

  true이면, 아무 작업도 수행하지 않고,
  false이면, User-Specified Text String을 Standard Output을 통해 출력한다.

* Severity Level
1) Note : General information about the condition of a model.
2) Warning : Alerts designers of potential problem conditions.
3) Error : Alerts designers of conditions that will cause errors.
4) Failure : Alerts designers of conditions that have disastrous effects.

Example. D-Flip Flop Implementation with assert Statement

USE work.std_logic_1164.all;

ENTITY dff IS
    GENERIC( setup_time : time = 2ns;
             delay_time : time := 1ns );
    PORT( din, clk, : IN  std_logic;
              q, qb : OUT std_logic );
END dff;

ARCHITECTURE setup_check OF dff IS
BEGIN
    PROCESS(clk)
    BEGIN
        IF (clk = '1') AND (clk'event) THEN
            ASSERT(din'last_event >= setup_time)  -- setup time 이전에 데이터가 입력되어 있어야 한다.
                REPORT "Setup violation" SEVERITY ERROR;
            q <= din AFTER delay_time;
            qb <= NOT din AFTER delay_time;
        END IF;
    END PROCESS;
END setup_check;


* a'event Attribute

* a'last_event Attribute
- 가장 최근에 일어난 Event가 발생한 시각값이다.