电子设计自动化 电子设计自动化 授课教师:何 授课教师:何 旭 旭 第四章数据类型 第一节数据类型定义 第二节提取对象的信息 第一节数据类型定义 VHDL 数据类型分为: 数量类型(Scalar Types) 组合类型(Composite Types) 文件类型(File Types) 寻址类型(Access Types) 对象(Objects)是指装载指定类型值的容器。 对象可以是信号、变量或常量。 算子或子程序可对对象值进行处理。 例:PORT (d0, d1, sel : IN bit; q : OUT bit); 在预定义的package “standard”中,定义bit类型 是值为0或1的集合。 TYPE bit IS (‘0’ , ‘1’); 一旦一个对象被声明为某一类型,那么在 类型声明所界定的范围内可以对对象进行 运算(或操作)。 例:bit类型对象,可以定其值为‘0’或 ‘1’,但不能定其值为10 (越界) 类型声明的格式为: type declaration … TYPE identifier IS type_definition; * 注意:identifier不能为预定义类型。 子类型声明的格式为: subtype declaration … SUBTYPE identifier IS subtype_indication; 子类型并不是一个新类型,它是基类型的子集的一个新名字。 例:TYPE control_valves IS (on ,off, standby, shutdown); SUBTYPE off_controls IS control_valves RANGE off TO shutdown; 一、数量类型 数量类型用相应的比例来说明。 包括: 物理类型(physical types) 浮点类型(floating types) 枚举类型(enumeration types) 整数类型(integer types) 1. 物理类型 可测量的量,用可测量基本单位的整数倍表达。 格式为: physical type definition … RANGE range UNITS identifiers; --基准单位声名 identifier = abstract_literal name; END UNITS Range的结构为: range … attribute_name 或simple_expression TO simple_expression 或simple_expression DOWNTO simple_expression 预定义类型: “time”。 例:TYPE measure IS RANGE 0 TO 1,000,000,000 UNITS mm; cm= 10 mm; dm= 10 cm; m = 1000 mm; END UNITS; 用measure类型的例子: process_size: PROCESS (sig1) VARIABLE w,h : measure; BEGIN w: = (100cm + 1 m) - 10 mm; h: = 20 cm + w; sig2 <= z; --其它地方声明。 END PROCESS process_size; 2. 浮点类型 定义了一个近于实数的数集合。 由于精度有限,如7/3 =2.3333可取为2.333 格式:floating type definition… RANGE range 预定义浮点类型: “real”。 例:定义half_hour 类型为: TYPE half_hour IS RANGE 0.0 TO 29.99; 执行下列运算:    test_time: PROCESS VARIABLE test_t1: half_hour; BEGIN test_t1: =15.0+15.00; END PROCESS test_time; 例:定义half_hour 类型为: TYPE half_hour IS RANGE 0.0 TO 29.99; 执行下列运算:    test_time: PROCESS VARIABLE test_t1: half_hour; BEGIN test_t1: =15.0+15.00; END PROCESS test_time; Error!   3. 枚举类型    定义一个用户化的数值集合。 格式: enumeration type definition … (enumeration_literal1, …,enumeration_literaln) enumeration literal 可为标识符(字母、下划线、数字)        或符号字(单引号括起的图形符号) 例: TYPE wire_color IS (red, black, green) TYPE traffic_light IS (red, yellow, green,flashing) 预定义枚举类型: bit,boolean,character,severity_level. 4. 整数类型   格式: integer type definition …RANGE range 指定range时,编译器必须知道其值。 预定义整数类型: integer 例: TYPE test_int IS RANGE 0 TO black; 例: TYPE test_int IS RANGE 0 TO black; Error! 例: TYPE test_int IS RANGE 0 TO black; TYPE test_integer IS RANGE 0 TO ext_val; 假定ext_val为外部输入参数 Error! 例: TYPE test_int IS RANGE 0 TO black; TYPE test_integer IS RANGE 0 TO ext_val; 假定ext_val为外部输入参数 Error! Error!         二、组合类型 用一个标识符代表一组值。 分为:数组类型(Array Types) 记录类型(Record Types)    1. 数组类型 相同类型的元素的集合。可以为 一或多维数组。 可用指针(index)指向元素。 数组定义: 无约束数组定义(unconstrained) (大小不定) 受约束数组定义(constrained) (大小指定) 格式:受约束数组: constrained array definition … ARRY index_constraint OF subtype_indication 无约束数组: unconstrained array definition … ARRY (type_mark RANGE< >) OF subtype_indication 例:受约束的: TYPE arr1 IS ARRY (0 TO 4) OF integer; 无约束的: TYPE mem_arr IS ARRY (0 TO 1023) OF integer; TYPE arr2 IS ARRY (integer RANGE< >) OF mem_arr; 2. 记录类型 元素可为各种类型。 格式: record type definition … RECORD identifier_list:element_subtype_definition … END RECORD 例:TYPE half_day IS (am,pm); TYPE clock_time IS RECORD hour: integer RANGE 1 TO 12; minute, second: integer RANGE 1 TO 60; ampm: half_day; END RECORD; 用法:VARIABLE time_of_day: clock_time; …. time_of_day.minute:=35; start_hour := time_of_day.hour; 三、文件类型 定义外部文件所存数据类型 格式: file type definition …… FILE OF type_mark 四、寻址类型 其值指向其它对象 格式: Access_type_definition …… ACCESS subtype_indication 第二节提取对象的信息 利用属性(attribute)提取对象信息用于运算 和测试 可供提取信息的对象类型有: 数组(Arrays) 块(Blocks) 信号(Signals) 类型(Types) 属性定义格式: attribute name …… prefix ’attribute_simple_name 对象或函数调用 例:属性’event LIBRARY my_lib; USE my_lib.my_qsim_logic.ALL; ENTITY incomplete_counter IS PORT (clock, data : IN my_qsim_state; q_out : INOUT my_qsim_state); END incomplete_counter; ARCHITECTURE behav OF incomplete_counter IS BEGIN q_out <= data WHEN clock’event AND clock=‘1’ ELSE q_out; END behav; ‘0’ ‘1’ 例:属性’left和’right TYPE high_byte IS RANGE 28 TO 31; …… FOR i IN high_byte ’left TO high_byte’right LOOP …… END LOOP; 例:属性’pos(x) 返回x的位置值 TYPE opcode IS (mov, lda, sta, jmp, ret, add, sub, nop, hlt); CONSTANT stop: integer := opcode ’pos(hlt); 例:属性’pos(x) 返回x的位置值 TYPE opcode IS (mov, lda, sta, jmp, ret, add, sub, nop, hlt); CONSTANT stop: integer := opcode ’pos(hlt); 0 1 2 3 4 5 6 7 8 ~~~~~~~~~~~ Stop = 8