if 语句 根据逻辑条件的判断构成程序分支结构 if 布尔表达式 then 顺序语句 end if; 当布尔表达式为 true 时, 执行 then 后面的顺序语句; 为 false 时,执行 end if 以后的语句; if 布尔表达式 then 顺序语句 1 else 顺序语句 2 end if; 当布尔表达式为 true 时, 执行 then 后面的顺序语句 1; 为 false 时,执行顺序语句 2; if 布尔表达式 1 then 顺序语句 1 elseif 布尔表达式 2 then 顺序语句 2 end if; 当布尔表达式 1 为 true 时,执行顺序语句 1;布尔表达式 2 为 true 时,执行顺序语句 2; 各布尔表达式必须互斥,在同一时刻,必须有一个为 true, 也只能有一个为 true; if 布尔表达式 1 then 顺序语句 1 elseif 布尔表达式 2 then 顺序语句 2 else 顺序语句 3 end if; 当布尔表达式 1 为 true 时,执行顺序语句 1; 布尔表达式 2 为 true 时,执行顺序语句 2; 所有布尔表达式都为 false 时,执行顺序语句 3; 例:p.292 表 4-58 质数检测器的行为设计(采用 if 语句) 并进行数据类型转换 library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity prime7 is port (n: in std_logic_vector (3 downto 0); f: out std_logic); end prime7; architecture beh of prime7 is begin process(n) variable ni : integer; begin ni := conv_integer(n); if ni=1 or ni=2 then f<= '1'; elsif ni=3 or ni=5 or ni=7 or ni=11 or ni=13 then f<='1'; else f<= '0'; end if; end process; end beh; case 语句 与 if 语句类似,用于表达逻辑语句的条件选择; 如果与代入语句对比, if 语句可以对应于条件代入, case 语句对应于选择代入; case 语句适应性更广,综合也更为容 易; case 表达式 is when 选择值 1 => 顺序语句 1; when 选择值 2 => 顺序语句 2; end case; 选择值应互斥,并覆盖表达式所有的可能值; case 表达式 is when 选择值 1 => 顺序语句 1; when 选择值 2 => 顺序语句 2; when others => 顺序语句 3; end case; 没有列出的选择值都归入 others; 例:p.293 质数检测器的行为设计(采用 CASE 语句) 并进行数据类型转换 prime8 可与 prime3 对比 library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity prime8 is port (n: in std_logic_vector (3 downto 0); f: out std_logic); end prime8; architecture beh of prime8 is begin process(n) begin case conv_integer(n) is when 1|2 => f<= '1'; when 3|5|7|11|13 => f<= '1'; when others => f<= '0'; end case; end process; end beh; loop 语句 循环语句,可以反复执行若干顺序语句;每个循环语句 可以加以编号; 有限循环(指定循环次数) : 编号:for 循环变量 in 范围 loop 顺序语句; end loop 编号; 循环变量为整数类型,不需要说明;范围的数据类型也 必须为整数; 循环变量从范围最左边的值开始;每进行一次循环,指 标右移 1 位;直到全部循环完毕; 条件循环: 编号:while 布尔表达式 loop 顺序语句; end loop 编号; 不指定循环次数;每次循环前检测布尔表达式的值;当 布尔表达式为真时,执行循环;为假时,不进行循环;在循 环体内,应有语句能够改变布尔表达式中变量的值,避免无 限循环。 无条件循环: 编号: loop 顺序语句; end loop 编号; 通常内部设置有跳出循环的语句;只要不满足跳出条件, 就会一直循环下去; 跳出循环的语句 next 编号 when 布尔表达式; 执行到此语句时,检测布尔表达式,若为 true,则转为执 行编号所表示的语句(跳出本循环) ;若为 false,则继续进行 循环; 若没有编号,则当布尔表达式为 true 时,直接跳出本次 循环,开始下一次循环; 本语句可以作为 loop 内部的循环控制; exit 编号 when 布尔表达式; 执行到此语句时,检测布尔表达式,若为 true,则转为执 行编号所表示的语句;若为 false,则继续进行循环; 若没有编号,则当布尔表达式为 true 时,直接跳出循环, 执行循环之后的下一条语句; 本语句可以作为中止 loop 的语句; 注意:next 和 exit 通常不允许在条件或选择结构中使用(难 以实现电路硬件的综合); loop 语句通常用于高层次抽象设计;在电路综合时,常 被综合为多重电路结构形式,电路庞大,效率很低; 例:并入串出的移位寄存器设计 library ieee; use ieee.std_logic_1164.all; entity piso is port(clk,load: in std_logic; din: in std_logic_vector(3 downto 0); dout: out std_logic); end piso; architecture beh of piso is signal q: std_logic_vector(3 downto 0); begin process(clk,load) begin if load='0' then q<=din; dout<='0'; elsif clk'event and clk='1' then q(1)<=q(0); for i in 2 to 3 loop q(i)<=q(i-1); end loop; dout<=q(3); end if; end process; end beh; 例:p.294 表 4-63 质数检测器的行为设计(采用 FOR-LOOP 语句) 并进行数据类型转换 library ieee; use ieee.std_logic_1164.all; use ieee.std_logic_unsigned.all; entity prime9 is port (n: in std_logic_vector (15 downto 0); f: out std_logic); end prime9; architecture beh of prime9 is begin process(n) variable ni:integer; variable prime: boolean; begin ni:=conv_integer(n) ; prime:= true; if ni=1 or ni=2 then null; else for i in 2 to 253 loop if ni mod i=0 then prime:= false; end if; end loop; end if; if prime then f<='1'; else f<= '0'; end if; end process; end beh; 注意: 原程序应添加 use ieee.std_logic_unsigned.all;语句, 才能进行 CONV_INTEGER 函数运算; 编译器不支持任意的 MOD()运算; 行为设计的特点: 行为描述方式采用进程及内部的各类顺序语句进行设计, 与传统的软件设计思路最接近,也最抽象、最灵活;通常可 以方便地进行高层次的设计;行为设计中很多语句难以直接 实现电路综合,即使能综合,效率通常也比较差。因此行为 设计主要用于高层仿真,检验设计的基本结构。