当前位置:文档之家› vhdl语法格式(1)

vhdl语法格式(1)

上篇基础元素目录:数据类型数据对象运算符语句基本程序结构电路描述方式数据类型预定义类型bitbit_victorintegerstd_logicstd_logic_victor自定义类型枚举类型 type 新数据类型 is (元素1, 元素2, ...)例定义 type state_type is (s1, s2, s3. s4); -- 定义一个新类型state_type引用 signal state : state_type; -- 定义一个信号state,类型为state_type 数组类型 type 数组 is array (范围) of 数据类型;例定义 type byte is array (7 downto 0) of bit; -- 定义一个8bit的数组type word is array (31 downto 0) of bit; -- 定义一个32bit的数组数据对象端口声明端口 : in | out 数据类型; -- 端口在特性上等同于信号,但赋值在entity的port中赋值端口 <= 表达式;信号声明signal 信号 : 数据类型;赋值信号 <= 表达式;变量声明varable 变量 : 数据类型;赋值变量 := 表达式;常数声明常数 : 数据类型 := 数值;运算符算术运算 +, -, *并置运算 &关系运算 =, /=, <, <=, >, >=逻辑运算 and, or, not, nand, nor, xor, xnor语句并行语句⑴信号赋值语句简单信号赋值语句信号 <= 表达式;选择信号赋值语句 with 选择表达式 select信号 <= 表达式1 when 选择值1,表达式2 when 选择值2,......表达式n when others;条件信号赋值语句信号 <= 表达式1 when 条件关系式1 else表达式2 when 条件关系式2 else......表达式n when 条件n else表达式;⑵过程调用语句过程 (实参);⑶函数调用语句信号 <= 函数 (实参);⑷元件例化语句元件声明 component 元件实体 -- 将一个实体声明为元件 port (端口声明);end component;元件引用按位置引用标号 : 元件实体 port map (连接端口1, 连接端口2, ...);按名称引用标号 : 元件实体 port map (元件端口1 >= 连接端口1, 元件端口2 >= 连接端口2, ...);⑸生成语句格式 1 [标号:] for 循环变量 in 取值范围 generate声明语句,begin并行语句,end generate [标号];取值范围: 表达式 to 表达式; -- 递增方式,如1 to 5表达式 downto 表达式 ; -- 递减方式,如5 downto 1格式 2 [标号:] if 条件关系式 generate声明语句;begin并行语句,end generate [标号] ,⑹块语句块标号: block [(保护条件)]接口声明;类属声明;begin并行语句; -- 被保护的变量前需加上保留字guarded end block 块标号;带保护的块语句举例: entity latch isport( d, clk : in bit;q, qb : out bit);end latch;achetectire latch_guard of latch isbeginb1 : block(clk = 1)beginq <= guarded d after 5 ns;qb <= guarded not(d) after 7 ns;end block b1;end latch_guard⑺进程语句 [标号:] process (敏感信号)[声明语句;] --常量,变量,信号 begin顺序语句;end process [标号:];顺序语句⑴ 赋值语句 -- 在进程中信号 <= 表达式;变量 := 表达式;⑵ 流程控制语句if语句格式 1: if 条件关系式 then顺序语句;end if;格式 2: if 条件关系式 then顺序语句;else顺序语句;end if;格式 3: if 条件关系式1 then顺序语句;elsif 条件关系式2 then顺序语句;......else顺序语句;end if;case 语句 -- case 语句中,条件值有3种形式:值,值1 | 值2 |...| 值n,值 TO 值 -- 最后一行的顺序语句若为null,则有意引入锁存器case 条件表达式 iswhen 条件值 => 顺序语句;......when others => 顺序语句;end case;for_loop 语句 [标号]:for 循环变量 in 值 to 值 loop;顺序语句;end loop [标号];时钟边沿描述上升沿时钟event and时钟 = 1| rising_edge (时钟)下降沿时钟event and时钟 =0| falling_edge (时钟)程序基本结构-- 主程序与元件程序在同一文件中,library ieee;use 主程序entity 实体名 is --实体名必须与文件名相同port (端口声明;);end entity work1;architecture struc of work1 is[声明语句;] --常量,变量,信号,元件,函数等begin并行语句;end architecture struc;电路描述方式行为描述方式以用状态机描述电路为典型数据流 ( 寄存器 ) 描述方式即用逻辑表达式描述电路结构描述方式以用元件复用的方式描述电路为典型下篇复合元素和状态机目录元件 ---------- 1 单文件元件2 多文件元件函数 ---------- 3 单文件函数4 多文件函数过程 ---------- 5 单文件过程6 多文件过程moorl 状态机 -- 7 二进程moorl状态机8 三进程moorl状态机meaky 状态机 -- 9 二进程mealy状态机10 三进程mealy状态机状态机实例 ---- 11 交通灯之一12 交通灯之二附录 ---------- 13 状态转移图14 用户库的格式和用法单文件元件-- 主程序与元件程序在同一文件中,library ieee;use 主程序entity work1 isport ( r,s,t,u : in std_logic;v : out std_logic);end entity work1;architecture struc of work1 iscomponent ym -- 将实体ym声明为元件 port ( a,b : in std_logic;c : out std_logic);end component ym;component hm -- 将实体hm声明为元件 port ( a,b : in std_logic;c : out std_logic);end component hm;signal temp1,temp2 : std_logic;begin u1 : ym port map ( r, s, temp1 ); -- 元件例化 u2 : ym port map ( t, u, temp2 );u3 : hm port map ( temp1, temp2, v );end architecture struc;-- ym元件实体定义程序library ieee;use ym isport ( a,b : in std_logic;c : out std_logic);end entity ym;architecture ym1 of ym isbeginc <= a and b;end architecture ym1;-- hm元件实体定义程序library ieee;use hm isport ( a,b : in std_logic;c : out std_logic);end entity hm;architecture hm1 of hm isbeginc <= a or b;end architecture hm1;多文件元件-- 主程序文件和定义元件的程序文件都要添加到工程中-- 主程序文件,不需要...声明用户库文件library ieee;use zhu_map isport ( r,s,t,u : in std_logic;v : out std_logic);end entity zhu_map;architecture niu of zhu_map iscomponent ymport ( a,b : in std_logic;c : out std_logic);end component ym;component hmport ( a,b : in std_logic;c : out std_logic);end component hm;signal temp1,temp2 : std_logic;beginu1 : ym port map ( r, s, temp1 ); -- 元件例化 u2 : ym port map ( t, u, temp2 );u3 : hm port map ( temp1, temp2, v );end architecture niu;-- 定义元件实体的程序文件-- ym元件实体定义程序library ieee;use ym isport ( a,b : in std_logic;c : out std_logic);end entity ym;architecture ym1 of ym isbeginc <= a and b;end architecture ym1;-- hm元件实体定义程序library ieee;use hm isport ( a,b : in std_logic;c : out std_logic);end entity hm;architecture hm1 of hm isbeginc <= a or b;end architecture hm1;单文件函数library ieee;use func isport ( din1,din2 : in std_logic_vector( 0 to 3 );dout : out std_logic_vector( 0 to 3 ));end entity;architecture a of func is-- 定义函数function ls_xj ( d1, d2 : in std_logic_vector( 0 to 3 )) return std_logic_vector is variable temp : std_logic_vector( 0 to 3 );begintemp := d1 + d2;return temp;end function;-- 定义函数结束begindout <= ls_xj ( din1, din2 ); --调用函数end architecture;多文件函数-- 主程序文件和定义函数的程序文件都要添加到工程中-- 主程序文件,必须声明用户库文件library ieee;use -- 作为用户库entity zhu_func isport ( din1,din2 : in std_logic_vector( 0 to 3 );dout : out std_logic_vector( 0 to 3 ) );end;architecture niu of zhu_func isbegindout <= ls_xj ( din1, din2 ); -- 调用函数end;-- 定义函数的文件library ieee;use use_func is -- 声明function ls_xj ( d1, d2: in std_logic_vector( 0 to 3 ) ) return std_logic_vector;end use_func;package body use_func is -- 程序体function ls_xj ( d1, d2 : in std_logic_vector( 0 to 3 )) return std_logic_vector is variable temp : std_logic_vector( 0 to 3 );begintemp := d1 and d2;return temp;end function;end use_func;单文件过程library ieee;use call_proce isport ( d1 : in integer range 0 to 31;d2 : in integer range 0 to 31;fout : out integer range 0 to 31 );end;architecture a of call_proce is-- 过程定义procedure jfq ( din1, din2 : in integer range 0 to 31;dout : out integer range 0 to 31 ) isbegindout := din1 + din2;end;-- 过程定义结束beginprocess ( d1, d2 )variable fo : integer range 0 to 31;beginjfq ( d1, d2, fo ); -- 调用过程fout <= fo;end process;end;多文件过程-- 主程序文件和定义过程的程序文件都要添加到工程中-- 主程序文件,必须声明用户库文件library ieee;use -- 作为用户库entity zhu_proc isport ( d1, d2 : in integer range 0 to 31;fout : out integer range 0 to 31);end;architecture niu of zhu_proc isbeginprocess ( d1, d2 )variable fo : integer range 0 to 31;beginjfq ( d1, d2, fo ); -- 调用过程fout <= fo;end process;end;-- 定义过程的文件library ieee;use use_proc is -- 声明procedure jfq ( din1 : in integer range 0 to 31;din2 : in integer range 0 to 31;dout : out integer range 0 to 31 );end use_proc;package body use_proc is -- 程序体procedure jfq ( din1, din2 : in integer range 0 to 31;dout : out integer range 0 to 31 ) isbegindout := din1 + din2;end jfq;end use_proc;二进程moorl状态机library ieee;use moorl_1 isport ( reset : in std_logic;clock : in std_logic;din : in std_logic;dout : out std_logic_vector ( 2 downto 0 ) );end entity;architecture statemachine of moorl_1 istype state_type is ( s0, s1, s2, s3 );signal state : state_type;beginprocess( reset, clock ) -- 变换状态begin if reset = '1' thenstate <= s0;elsif rising_edge( clock ) thencase state iswhen s0 => if din = '1' thenstate <= s1;end if;when s1 => if din = '1' thenstate <= s2;end if;when s2 => if din = '1' thenstate <= s3;end if;when s3 => if din = '1' thenstate <= s0;elsestate <= s1;end if;end case;end if;end process;process( state ) -- 输出begincase state iswhen s0 => dout <= "001";when s1 => dout <= "011";when s2 => dout <= "101";when s3 => dout <= "111";end case;end process;end;三进程moorl状态机library ieee;use moorl_2 isport ( reset : in std_logic;clock : in std_logic;din : in std_logic;dout : out std_logic_vector( 2 downto 0 ) );end entity;architecture statemachine of moorl_2 istype state_type is ( s0, s1, s2, s3 );signal presentstate : state_type;signal nextstate : state_type;beginprocess ( reset, clock ) -- 更新当前状态 beginif reset = '1' thenpresentstate <= s0;elsif rising_edge ( clock ) thenpresentstate <= nextstate;end if;end process;process ( presentstate, din ) -- 生成下一个状态begincase presentstate iswhen s0 => if din = '1' thennextstate <= s1;elsenextstate <= s0;end if;--dout <= "001";when s1 => if din = '1' thennextstate <= s2;elsenextstate <= s1;end if;--dout <= "011";when s2 => if din = '1' thennextstate <= s3;elsenextstate <= s2;end if;--dout <= "101";when s3 => if din = '1' thennextstate <= s0;elsenextstate <= s1;--dout <= "111";end if;end case;end process;process ( presentstate ) -- 输出begincase presentstate iswhen s0 => dout <= "001";when s1 => dout <= "011";when s2 => dout <= "101";when s3 => dout <= "111";end case;end process;end;二进程mealy状态机library ieee;use mealy_1 isport ( reset : in std_logic;clock : in std_logic;din : in std_logic;dout : out std_logic_vector ( 2 downto 0 ) );end entity;architecture statemachine of mealy_1 istype state_type is ( s0, s1, s2, s3 );signal state : state_type;beginprocess ( reset, clock ) -- 变换状态beginif reset = '1' thenstate <= s0;elsif rising_edge ( clock ) thencase state iswhen s0 => if din = '1' then state <= s1;end if;when s1 => if din = '1' then state <= s2;end if;when s2 => if din = '1' then state <= s3;end if;when s3 => if din = '1' then state <= s0;elsestate <= s1;end if;end case;end if;end process;process ( state, din ) -- 输出 begincase state iswhen s0 => if din='0' thendout <="000";elsedout <="001";end if;when s1 => if din='0' thendout <="010";elsedout <="011";end if;when s2 => if din='0' thendout <="100";elsedout <="101";end if;when s3 => if din='0' thendout <="110";else dout <="111";end if;end case;end process;end architecture;三进程mealy状态机library ieee;use mealy_2 isport ( reset : in std_logic;clock : in std_logic;din : in std_logic;dout : out std_logic_vector( 2 downto 0 ) );end entity;architecture statemachine of mealy_2 istype state_type is ( s0, s1, s2, s3 );signal presentstate : state_type;signal nextstate : state_type;beginprocess ( reset, clock ) -- 更新当前状态 beginif reset = '1' thenpresentstate <= s0;elsif rising_edge ( clock ) thenpresentstate <= nextstate;end if;end process;process ( presentstate, din ) -- 生成次态 begincase presentstate iswhen s0 => if din ='1' thennextstate <= s1;elsenextstate <= s0;end if;when s1 => if din ='1' thennextstate <= s2;elsenextstate <= s1;end if;when s2 => if din ='1' thennextstate <= s3;elsenextstate <= s2;end if;when s3 => if din = '1' thennextstate <= s0;elsenextstate <= s1;end if;end case;end process;process ( presentstate, din ) -- 输出 begincase presentstate iswhen s0 => if din = '0' thendout <= "000";elsedout <= "001";end if;when s1 => if din = '0' thendout <= "010";elsedout <= "011";end if;when s2 => if din = '0' thendout <= "100";elsedout <= "101";end if;when s3 => if din = '0' thendout <= "110";elsedout <= "111";end if;end case;end process;end;用状态机设计交通灯之一-- 这是一个简单的状态机设计实例,根据时钟变换路口-- 的红,绿,黄三个信号灯,从程序中可以看出,时钟-- 的周期至少应为,例如30秒。

相关主题