ch10.08.htm
来自「介绍asci设计的一本书」· HTM 代码 · 共 128 行
HTM
128 行
<HTML>
<HEAD>
<META NAME="GENERATOR" CONTENT="Adobe PageMill 2.0 Mac">
<TITLE> 10.8 Type Declarations</TITLE>
</HEAD><!--#include file="top.html"--><!--#include file="header.html"--><br><!--#include file="AmazonAsic.html"-->
<P><A NAME="pgfId=10949"></A><A HREF="CH10.htm">Chapter start</A> <A HREF="CH10.07.htm">Previous
page</A> <A HREF="CH10.09.htm">Next page</A></P>
<H2>10.8 Type Declarations</H2>
<P><P CLASS="BodyAfterHead"><A NAME="pgfId=114101"></A>In some programming
languages you must declare objects to be integer, real, Boolean, and so
on. VHDL (and ADA, the DoD programming language to which VHDL is related)
goes further: You must declare the type of an object, and there are strict
rules on mixing objects of different types. We say VHDL is strongly typed.
For example, you can use one type for temperatures in Centigrade and a different
type for Fahrenheit, even though both types are real numbers. If you try
to add a temperature in Centigrade to a temperature in Fahrenheit, VHDL
catches your error and tells you that you have a type mismatch.</P>
<P><P CLASS="Body"><A NAME="pgfId=10827"></A>This is the formal (expanded)
BNF definition of a type declaration:</P>
<PRE>type_declaration ::=
<B>type</B> identifier ;
| <B>type</B> identifier <B>is</B>
(identifier|'graphic_character' {, identifier|'graphic_character'}) ;
| range_constraint ; | physical_type_definition ;
| record_type_definition ; | <B>access</B> subtype_indication ;
| <B>file</B> <B>of</B> <I>type_</I>name ; | <B>file</B> <B>of</B> <I>subtype_</I>name ;
| <B>array</B> index_constraint <B>of</B> <I>element_</I>subtype_indication ;
| <B>array</B>
(type_name|subtype_name <B>range</B> <>
{, type_name|subtype_name <B>range</B> <>}) <B>of</B>
<I>element_</I>subtype_indication ;</PRE>
<P><P CLASS="Body"><A NAME="pgfId=1496"></A>There are four type classes
in VHDL [<A HREF="../../VHDL/LRM/HTML/1076_3.HTM#3">VHDL LRM3</A>]: scalar types,
composite types, access types, and file types. The scalar types are: integer
type, floating-point type, physical type, and enumeration type. Integer
and enumeration types are discrete types. Integer, floating-point, and physical
types are numeric types. The range of an integer is implementation dependent
but is guaranteed to include -2147483647 to +2147483647. Notice the integer
range is symmetric and equal to -(2<SUP>31</SUP>- 1) to (2<SUP>31</SUP>-
1). Floating-point size is implementation dependent, but the range includes
the bounds -1.0E38 and +1.0E38, and must include a minimum of six decimal
digits of precision. Physical types correspond to time, voltage, current,
and so on and have dimensions--a unit of measure (seconds, for example).
Access types are pointers, useful in abstract data structures, but less
so in ASIC design. File types are used for file I/O.</P>
<P><P CLASS="Body"><A NAME="pgfId=256504"></A>You may also declare a subset
of an existing type, known as a subtype, in a subtype declaration. We shall
discuss the different treatment of types and subtypes in expressions in
Section 10.12.</P>
<P><P CLASS="Body"><A NAME="pgfId=220886"></A>Here are some examples of
scalar type [<A HREF="../../VHDL/LRM/HTML/1076_4.HTM#4.1">VHDL LRM4.1</A>] and subtype
declarations [<A HREF="../../VHDL/LRM/HTML/1076_4.HTM#4.2">VHDL LRM4.2</A>]:</P>
<PRE><B>entity</B> Declaration_1 <B>is</B> <B>end</B>; <B>architecture</B> Behave <B>of</B> Declaration_1 <B>is</B>
<B>type</B> F <B>is</B> <B>range</B> 32 <B>to</B> 212; -- Integer type, ascending range.
<B>type</B> C <B>is</B> <B>range</B> 0 <B>to</B> 100; -- Range 0 to 100 is the range constraint.
<B>subtype</B> G <B>is</B> INTEGER <B>range</B> 9 <B>to</B> 0; -- Base type INTEGER, descending.
-- This is illegal<B>:</B> type Bad100 is INTEGER range 0 to 100;
-- don't use INTEGER in declaration of type (but OK in subtype).
<B>type</B> Rainbow <B>is</B> (R, O, Y, G, B, I, V); -- An enumeration type.
-- Enumeration types always have an ascending range.
<B>type</B> MVL4 <B>is</B> ('X', '0', '1', 'Z');
-- Note that 'X' and 'x' are different character literals.
-- The default initial value is MVL4'LEFT = 'X'.
-- We say '0' and '1' (already enumeration literals
-- for predefined type BIT) are overloaded.
-- Illegal enumeration type: type Bad4 is ("X", "0", "1", "Z");
-- Enumeration literals must be character literals or identifiers.
<B>begin</B> <B>end</B>;</PRE>
<P><P CLASS="Body"><A NAME="pgfId=7417"></A>The most common composite type
is the array type [<A HREF="../../VHDL/LRM/HTML/1076_3.HTM#3.2.1">VHDL LRM3.2.1</A>].
The following examples illustrate the semantics of array declarations:</P>
<PRE><B>entity</B> Arrays_1 <B>is</B> <B>end</B>; <B>architecture</B> Behave <B>of</B> Arrays_1 <B>is</B>
<B>type</B> Word <B>is</B> <B>array</B> (0 <B>to</B> 31) <B>of</B> BIT; -- a 32-bit array, ascending
<B>type</B> Byte <B>is</B> <B>array</B> (NATURAL <B>range</B> 7 <B>downto</B> 0) <B>of</B> BIT; -- descending
<B>type</B> BigBit <B>is</B> <B>array</B> (NATURAL <B>range</B> <>) <B>of</B> BIT;
-- We call <> a box, it means the range is undefined for now.
-- We call BigBit an unconstrained array.
-- This is OK, we constrain the range of an object that uses
-- type BigBit when we declare the object, like this:
<B>subtype</B> Nibble <B>is</B> BigBit(3 <B>downto</B> 0);
<B>type</B> T1 <B>is</B> <B>array</B> (POSITIVE <B>range</B> 1 <B>to</B> 32) <B>of</B> BIT;
-- T1, a constrained array declaration, is equivalent to a type T2
-- with the following three declarations:
<B>subtype</B> index_subtype <B>is</B> POSITIVE <B>range</B> 1 <B>to</B> 32;
<B>type</B> array_type <B>is</B> array (index_subtype <B>range</B> <>) of BIT;
<B>subtype</B> T2 <B>is</B> array_type (index_subtype);
-- We refer to index_subtype and array_type as being
-- anonymous subtypes of T1 (since they don't really exist).
<B>begin</B> <B>end</B>;</PRE>
<P><P CLASS="Body"><A NAME="pgfId=7529"></A>You can assign values to an
array using aggregate notation [<A HREF="../../VHDL/LRM/HTML/1076_7.HTM#7.3.2">VHDL
LRM7.3.2</A>]:</P>
<PRE><B>entity</B> Aggregate_1 <B>is</B> <B>end</B>; <B>architecture</B> Behave <B>of</B> Aggregate_1 <B>is</B>
<B>type</B> D <B>is</B> <B>array</B> (0 <B>to</B> 3) <B>of</B> BIT; <B>type</B> Mask <B>is</B> <B>array</B> (1 <B>to</B> 2) <B>of</B> BIT;
<B>signal</B> MyData : D := ('0', <B>others</B> => '1'); -- positional aggregate
<B>signal</B> MyMask : Mask := (2 => '0', 1 => '1'); -- named aggregate
<B>begin</B> <B>end</B>;</PRE>
<P><P CLASS="Body"><A NAME="pgfId=21065"></A>The other composite type is
the record type that groups elements together:</P>
<PRE><B>entity</B> Record_2 <B>is</B> <B>end</B>; <B>architecture</B> Behave <B>of</B> Record_2 <B>is</B>
<B>type</B> Complex <B>is</B> <B>record </B>real : INTEGER; imag : INTEGER; <B>end</B> <B>record</B>;
<B>signal</B> s1 : Complex := (0, <B>others</B> => 1); <B>signal</B> s2: Complex;
<B>begin</B> s2 <= (imag => 2, real => 1); <B>end</B>;</PRE>
<P><HR ALIGN="LEFT"></P>
<P><A HREF="CH10.htm">Chapter start</A> <A HREF="CH10.07.htm">Previous page</A> <A HREF="CH10.09.htm">Next page</A>
</BODY>
<!--#include file="Copyright.html"--><!--#include file="footer.html"-->
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?