BEHAVIORAL MODELING 2 Loop statement 2 Repeatedly executes
BEHAVIORAL MODELING -2
Loop statement 2 • Repeatedly executes a sequence of statements. • Syntax is [loop-label : ] iteration-scheme loop sequential-statements end loop [loop-label] ; DSD 2/11/2022
Types of loops 3 • Three types: – For loop – while Loop – Loop statement DSD 2/11/2022
For loop 4 • The for loop execute for a specific number of iterations based on a controlling value. for identifier in range DSD 2/11/2022
For loop - operation 5 • A new integer variable is declared with the named identifier (implicit declaration). • Identifier is assigned the first value of the range and the sequence of statements is executed once. • Identifier is assigned the next value of the range and the sequence of statements is executed once • Identifier is assigned the last value of the range and the sequence of statements is executed for the last time and execution continues with the statement following end loop. DSD 2/11/2022
For loop 6 • The loop index is automatically and implicitly declared within the loop statement • The loop index is declared locally for this loop. • If a variable /signal with the same index name is used elsewhere within the same process or architecture( but not in the same loop), it is treated as a distinct object • The loop index cannot be assigned a value or altered in the body of the loop. DSD 2/11/2022
For loop 7 • The range must be a computable integer range. • In each iteration the loop parameter takes one value from the specified range , starting from the leftmost value within the range. • The discrete range can also be specified in the form of a discrete type including an enumerated type. DSD 2/11/2022
For loop 8 Example Variable c , d : bit_vector ( 2 downto 0 ) ; for I in 2 downto 0 loop c (I) : = d (I) ; end loop ; DSD 2/11/2022
For loop 9 Example for I in 0 to 15 loop FIFO (I) <= ‘ 0’; end loop ; For Loop used to asynchronously clear the FIFO array. DSD 2/11/2022
While loop 10 • The syntax is while boolean-expression • A Boolean condition is evaluated before execution • The loop is executed if the result is true • The loop stops for a false condition DSD 2/11/2022
While statement 11 Example Variable count : integer : = 0 ; while count < 20 loop x : = x +1; j : = j +1; count : = count + 1 ; end loop; DSD 2/11/2022
While loop 12 • The condition is evaluated prior to the execution of the loop statements. • The sequence of statement will never be executed if the condition is initially FALSE. • An infinite loop is possible if the condition is always TRUE. • Requires an additional overhead of declaring, initializing and incrementing the loop variable • The variable can be initialized – upon declaration or – within the process DSD 2/11/2022
Simple loop 13 • basic loop has no iteration scheme • all statements are continuously executed • Syntax is loop { sequence of statements} end loop; • Must contain a wait or an exit statement to prevent an infinite loop. DSD 2/11/2022
Basic loop 14 Example loop x : = x +1; end loop ; -- never ending loop DSD 2/11/2022
Loop 15 Example signal clock : bit : = ‘ 0’; c 1 : process(clock) begin loop 1 : Loop clock <= not clock after 5 ns; end loop 1; end process ; DSD 2/11/2022
Loop 16 Example Variable B : integer : = ‘ 0’; loop 2 : loop B : = B +1 ; exit loop 2 when B > 10 ; end loop 2; DSD 2/11/2022
Exit statement 17 • Used inside a loop • Used to exit a loop • Used to terminate the entire loop if a given condition is True. • Can be used to check for an illegal condition. • Exit [loop_label] [when condition]; • Syntax • exit ; • exit [loop_label] when [condition] DSD ; 2/11/2022
Exit statement 18 • If loop_label is present, the loop with the given label is terminated when condition is true. • If loop_label is absent, the innermost loop is terminated when condition is True. • If the condition is omitted, the appropriate loop is always terminated. DSD 2/11/2022
Exit loop 19 • Example loop x : x+1; exit when x > 99; end loop; DSD 2/11/2022
Example 20 SUM : =1; J: =0; L 3: loop J : = J+21; SUM : = SUM*10; if SUM > 100 then exit L 3; -- “exit; ” is also sufficient end if; end loop L 3; DSD 2/11/2022
Next statement 21 • Next statement is used inside a loop • Terminates the current iteration and starts the next iteration • The format is as follows: Next [loop_label] [when condition] • Syntax Next ; Next [loop_label] [when condition] ; DSD 2/11/2022
Next statement 22 • The next statement is executed – If the condition specified after When clause is TRUE or – If there is no condition at all. • Skips all statements below it until the end of the loop. • Passes the control to the first statement in the next iteration DSD 2/11/2022
Next statement 23 • A next statement with no loop label terminates the current iteration of the innermost enclosing loop. • When a loop label is specified , the current iteration of that named loop is terminated DSD 2/11/2022
Next statement 24 FIFO_array : process (reset, clock) begin if (reset = ‘ 0’ ) then for I in 15 downto 0 loop. if (I = 8) then next ; else FIFO ( I ) <= (others => ‘ 0’) ; end if ; end loop ; end if; end process ; DSD 2/11/2022
Next statement 25 Example for j in 10 down to 5 loop if SUM< TOTAL_SUM then SUM: = SUM + 2; elsif SUM = TOTAL _ SUM then next ; -- go to end loop else null; end if; K: = K+ 1; end loop; DSD 2/11/2022
Example 26 • L 4: for K in 10 downto 1 loop -- statements section 1 L 5 : loop -- statements section 2 Next L 4 when WR_DONe = ‘ 1’; --statements section 3 End loop L 5; -- statements section 4 End loop L 4; DSD 2/11/2022
- Slides: 26