第7章 VHDL语句
发布时间:2021-06-11
发布时间:2021-06-11
KX
康芯科技
EDA技术与VHDL第7章
VHDL语句
KX
康芯科技
7.1 顺序语句7.1.1 赋值语句信号赋值语句 变量赋值语句
7.1.2 IF语句 7.1.3 CASE语句选择值 [ |选择值 ]
单个普通数值,如6。数值选择范围,如(2 TO 4),表示取值为2、3或4。 并列数值,如3 5,表示取值为3或者5。
混合方式,以上三种方式的混合。
【例7-1】 LIBRARY IEEE; USE IEEE.STD_LOGIC_1164.ALL; ENTITY mux41 IS PORT (s4,s3, s2,s1 : IN STD_LOGIC; z4,z3, z2,z1 : OUT STD_LOGIC); END mux41; ARCHITECTURE activ OF mux41 IS SIGNAL sel : INTEGER RANGE 0 TO 15; BEGIN PROCESS (sel ,s4,s3,s2,s1 ) BEGIN sel<= 0 ; -- 输入初始值 IF (s1 ='1') THEN sel <= sel+1 ; ELSIF (s2 ='1') THEN sel <= sel+2 ; ELSIF (s3 ='1') THEN sel <= sel+4 ; ELSIF (s4 ='1') THEN sel <= sel+8 ; ELSE NULL; -- 注意,这里使用了空操作语句 END IF ; z1<='0' ; z2<='0'; z3<='0'; z4<='0'; --输入初始值 CASE sel IS WHEN 0 => z1<='1' ; -- 当sel=0时选中 WHEN 1 3 => z2<='1' ; -- 当sel为1或3时选中 WHEN 4 To 7 2 => z3<='1'; -- 当sel为2、4、5、6或7时选中 WHEN OTHERS => z4<='1' ; -- 当sel为8~15中任一值时选中 END CASE ; END PROCESS ; END activ ;
KX
康芯科技
KX
康芯科技
7.1 顺序语句7.1.3 CASE语句【例7-2】 SIGNAL value : INTEGER RANGE 0 TO 15; SIGNAL out1 : STD_LOGIC ; ... CASE value IS -- 缺少以WHEN引导的条件句 END CASE; ... CASE value IS WHEN 0 => out1<= '1' ; -- value2~15的值未包括进去 WHEN 1 => out1<= '0' ; END CASE ... CASE value IS WHEN 0 TO 10 => out1<= '1'; -- 选择值中5~10的值有重叠 WHEN 5 TO 15 => out1<= '0'; END CASE;
【例7-3】 LIBRARY IEEE; USE IEEE.STD_LOGIC_1164.ALL; USE IEEE.STD_LOGIC_UNSIGNED.ALL; ENTITY alu IS PORT( a, b : IN STD_LOGIC_VECTOR (7 DOWNTO 0); opcode: IN STD_LOGIC_VECTOR (1 DOWNTO 0); result: OUT STD_LOGIC_VECTOR (7 DOWNTO 0) ); END alu; ARCHITECTURE behave OF alu IS CONSTANT plus : STD_LOGIC_VECTOR (1 DOWNTO 0) := CONSTANT minus : STD_LOGIC_VECTOR (1 DOWNTO 0) := CONSTANT equal : STD_LOGIC_VECTOR (1 DOWNTO 0) := CONSTANT not_equal: STD_LOGIC_VECTOR (1 DOWNTO 0) := BEGIN PROCESS (opcode,a,b) BEGIN CASE opcode IS WHEN plus => result <= a + b; -- a、b相加 WHEN minus => result <= a - b; -- a、b相减 WHEN equal => -- a、b相等 IF (a = b) THEN result <= x"01"; ELSE result <= x"00"; END IF; WHEN not_equal => -- a、b不相等 IF (a /= b) THEN result <= x"01"; ELSE result <= x"00"; END IF; END CASE; END PROCESS; END behave;
KX
康芯科技
b"00"; b"01"; b"10"; b"11";
KX
康芯科技
7.1 顺序语句7.1.4 LOOP语句(1) 单个LOOP语句,其语法格式如下: [ LOOP标号:] LOOP
顺序语句END LOOP [ LOOP标号 ];... L2 : LOOP a := a+1; EXIT L2 WHEN a >10 ; END LOOP L2; ... -- 当a大于10时跳出循环
KX
康芯科技
7.1 顺序语句7.1.4 LOOP语句
(2) FOR_LOOP语句,语法格式如下:[LOOP标号:] FOR 循环变量,IN 顺序语句 END LOOP [L
OOP标号]; 循环次数范围 LOOP
7.1 顺序语句7.1.4 LOOP语句【例7-4】 LIBRARY IEEE; USE IEEE.STD_LOGIC_1164.ALL; ENTITY p_check IS PORT ( a : IN STD_LOGIC_VECTOR (7 DOWNTO 0); y : OUT STD_LOGIC ); END p_check; ARCHITECTURE opt OF p_check IS SIGNAL tmp :STD_LOGIC ; BEGIN PROCESS(a) BEGIN tmp <='0'; FOR n IN 0 TO 7 LOOP tmp <= tmp XOR a(n); END LOOP ; y <= tmp; END PROCESS; END opt;
KX
康芯科技
KX
康芯科技
7.1 顺序语句7.1.4 LOOP语句【例7-5】 SIGNAL a, b, c : STD_LOGIC_VECTOR (1 TO 3); ... FOR n IN 1 To 3 LOOP a(n) <= b(n) AND c(n); END LOOP; 此段程序等效于顺序执行以下三个信号赋值操作: a(1)<=b(1) AND c(1); a(2)<=b(2) AND c(2); a(3)<=b(3) AND c(3);
KX
康芯科技
7.1 顺序语句7.1.5 NEXT语句NEXT; NEXT LOOP标号; NEXT LOOP标号 WHEN 条件表达式; -- 第一种语句格式 -- 第二种语句格式 -- 第三种语句格式
【例7-6】 ... L1 : FOR cnt_value IN 1 TO 8 LOOP s1 : a(cnt_value) := '0'; NEXT WHEN (b=c); s2 : a(cnt_value + 8 ):= '0'; END LOOP L1;
KX
康芯科技
7.1 顺序语句7.1.5 NEXT语句
【例7-7】 ...
L_x : FOR cnt_value IN 1 TO 8 LOOPs1 : a(cnt_value):= '0'; k := 0; L_y : LOOP s2 : b(k) := '0'; NEXT L_x WHEN (e>f);
s3 : b(k+8) := '0';k := k+1; NEXT LOOP L_y ;
NEXT LOOP L_x ;...
KX
康芯科技
7.1 顺序语句7.1.6 EXIT语句
EXIT; EXIT LOOP标号; EXIT LOOP标号 WHEN 条件表达式;
-- 第一种语句格式 -- 第二种语句格式 -- 第三种语句格式
KX
康芯科技
7.1 顺序语句7.1.6 EXIT语句【例7-8】 SIGNAL a, b : STD_LOGIC_VECTOR (1 DOWNTO 0); SIGNAL a_less_then_b : Boolean; ... a_less_then_b <= FALSE ; -- 设初始值 FOR i IN 1 DOWNTO 0 LOOP IF (a(i)='1' AND b(i)='0') THEN a_less_then_b <= FALSE ; -- a > b EXIT ; ELSIF (a(i)='0' AND b(i)='1') THEN a_less_then_b <= TRUE ; -- a < b EXIT; ELSE NULL; END IF; END LOOP; -- 当 i=1时返回LOOP语句继续比较
KX
康芯科技
7.1 顺序语句7.1.7 WAIT语句WAIT;WAIT ON 信号表; WAIT UNTIL 条件表达式; WAIT FOR 时间表达式;
-- 第一种语句格式-- 第二种语句格式 -- 第三种语句格式 -- 第四种语句格式,超时等待语句
KX
康芯科技
7.1 顺序语句7.1.7 WAIT语句【例7-9】 SIGNAL s1,s2 : STD_LOGIC;
...PROCESS BEGIN
...WAIT ON s1,s2 ; END PROCESS ;
KX
康芯科技
7.1 顺序语句7.1.7 WAIT语句
【例7-10】 (a) WAIT_UNTIL结构 ... Wait until enable ='1'; ... (b) WAIT_ON结构 LOOP Wait on enable; EXIT WHEN enable ='1'; END LOOP;
KX
康芯科技
7.1 顺序语句7.1.7 WAIT语句WAIT UNTIL 信号=Value ; WAIT UNTIL 信号’EVENT AND 信号=Value; WAIT UNTIL NOT 信号’STABLE AND 信号=Value; -- (1) -- (2) -- (3)
WAIT UNTIL clock ='1';
WAIT UNTIL rising_edge(clock) ;WAIT UNTIL NOT clock’STABLE AND clock ='1'; WAIT UNTIL clock ='1' AND clock’EVENT;
KX
康芯科技
7.1 顺序语句7.1.7 WAIT语句【例7-11】 PROCESS BEGIN WAIT UNTIL clk ='1'
; ave <= a; WAIT UNTIL clk ='1'; ave <= ave + a; WAIT UNTIL clk ='1'; ave <= ave + a; WAIT UNTIL clk ='1'; ave <= (ave + a)/4 ; END PROCESS ;
KX
康芯科技
7.1 顺序语句7.1.7 WAIT语句【例7-12】 PROCESS BEGIN
rst_loop : LOOPWAIT UNTIL clock ='1' AND clock’EVENT; -- 等待时钟信号 NEXT rst_loop WHEN (rst='1'); -- 检测复位信号rst
x <= a ;
-- 无复位信号,执行赋值操作
WAIT UNTIL clock ='1' AND clock’EVENT; -- 等待时钟信号 NEXT rst_loop When (rst='1'); y <= b ; END LOOP rs -- 检测复位信号rst -- 无复位信号,执行赋值操作
【例7-13】 LIBRARY IEEE; USE IEEE.STD_LOGIC_1164.ALL; ENTITY shifter IS PORT ( data : IN STD_LOGIC_VECTOR (7 DOWNTO 0); shift_left: IN STD_LOGIC; shift_right: IN STD_LOGIC; clk: IN STD_LOGIC; reset : IN STD_LOGIC; mode : IN STD_LOGIC_VECTOR (1 DOWNTO 0); qout : BUFFER STD_LOGIC_VECTOR (7 DOWNTO 0) ); END shifter; ARCHITECTURE behave OF shifter IS SIGNAL enable: STD_LOGIC; BEGIN PROCESS BEGIN WAIT UNTIL (RISING_EDGE(clk) ); --等待时钟上升沿 IF (reset = '1') THEN qout <= "00000000"; ELSE CASE mode IS WHEN "01" => qout<=shift_right & qout(7 DOWNTO 1); WHEN "10" => qout<=qout(6 DOWNTO 0) & shift_left; WHEN "11" => qout <= data; WHEN OTHERS => NULL; END CASE; END IF; END PROCESS; END behave;
KX
康芯科技
--右移 --左移 -- 并行加载
KX
康芯科技
7.1 顺序语句7.1.8 RETURN语句
返回语句RETURN有两种语句格式:
RETURN; RETURN 表达式;
-- 第一种语句格式 -- 第二种语句格式