ee 721 types and functions in vhdl
play

EE 721: Types and Functions in VHDL September 9, 2009 1 Overview - PDF document

EE 721: Types and Functions in VHDL September 9, 2009 1 Overview VHDL views the system being described as a network of signals and drivers. The signals carry values and the drivers use existing signal values to compute new values for signals.


  1. EE 721: Types and Functions in VHDL September 9, 2009 1 Overview VHDL views the system being described as a network of signals and drivers. The signals carry values and the drivers use existing signal values to compute new values for signals. All this requires the notion of the type of a particular signal value, and the notion of functions and operators for computations on values. VHDL offers a user complete flexibility in defining types and functions which operate on these types. In particular, VHDL is a strongly typed language: it forces the user to ensure that a function is called on values corresponding to the types it expects. 2 Types In VHDL, a type is an ordered and finite set of values. Depending on the form of the value set, a type is considered as either a scalar type (dimension of the value is 1) or a composite type (dimension can be greater than 1). The only assumption made by VHDL in the definition of types is that the integers exist, and that characters exist. Given these assumptions, the user has complete flexibility in the definition of types. In the rest of this note, we will describe the most important kinds of types that can be defined in VHDL 1 . 2.1 Enumerated Types An enumerated type is an ordered set of symbolic values, defined in the following manner. type MyColor is (v,i,b,g,y,o,r); This type definition says that the set of possible values for an object with type MyColor is the ordered list ( v, i, b, g, y, o, r ). Each element in this ordered list has a position-number and an index. The position values and indices of the set are as follows 1 Some types such as access types (pointers) and physical types (to specify delays) etc. will not be described here. 1

  2. literal value v i b g y o r position-number 0 1 2 3 4 5 6 order-index 0 1 2 3 4 5 6 Thus, the position-number of literal v is 0 and the position-number of literal r is 6. The elements of the ordered list are termed literals and can either be quoted characters (such as ’C’) or identifiers. The successor of a literal a is the literal b whose position-number is one higher than that of a (in this case, a is the predecessor of b ). The literal to the right of a literal a is the literal b whose order-index is 1 higher than that of a (in this case, a is the literal to the left of b ). 2.2 Integer Types Integer types are specified by a range of integer values. For example, type integer is range -2147483648 to 2147483647; type MyInteger is range 0 to 9; type YourInteger is range 9 downto 0; Each of these type definitions specifies an ordered range of values. Type MyIn- teger specifies the following range of values with corresponding position-values and indices: list-of-values 0 1 2 3 4 5 6 7 8 9 position-number 0 1 2 3 4 5 6 7 8 9 order-index 0 1 2 3 4 5 6 7 8 9 On the other hand, YourInteger specifies the following range list-of-values 9 8 7 6 5 4 3 2 1 0 position-value 9 8 7 6 5 4 3 2 1 0 order-index 0 1 2 3 4 5 6 7 8 9 2.3 Array Types Array types define sets of values that can have multiple dimensions. For exam- ple, consider the following type definitions type Word is array(0 to 9) of bit; type IWord is array(9 downto 0) of bit; type UWord is array(natural range <>) of bit; The first definition, of type Word, says that the set of values corresponding to type Word consists of 10-tuples of bits, with the indexing of the tuples being 0 to 9 as we go from left to right. The second definition, of type IWord, says that the set of values corresponding to type IWord consists of 10-tuples of bits, with the indexing of the tuples being 9 downto 0 as we go from left to right. The third definition, of type UWord, is more interesting. This says that UWord 2

  3. specifies sets of sets of values, each of which is an n-tuple of bits (indexed with a range whose limits are natural numbers)! Such a type is very powerful 2 . One can have multi-dimensional array types as well. type Mem is array(0 to 1023, 31 downto 0) of bit; type UMem is array(natural range <>, natural range <>) of bit; and so on. 2.4 Record Types Record types are very useful for managing information in a VHDL description. For example, type CktStatus is record rdy: bit; ack: bit; end record; defines a record type with two fields each of which is a bit. In this case, the two fields rdy and ack are kept together in the same record. This makes it clear that they are related and leads to more understandable code. 3 Sub-types Subtypes are very important in VHDL. A subtype of a type defines a subset of the possible set of values that a type can have. For example, given the type Colour defined above, we could define the fol- lowing sub-types subtype IR is Colour range r downto y; subtype UV is Colour range v to b; The definition of the subtype IR selects a range of values in the set corresponding to the type Colour. The position and index numbers for the values of IR are literal r o y position 2 1 0 index 0 1 2 Similarly, the definition of the subtype UV selects a range of values in the set corresponding to the type Colour. The position and index numbers for the values of UV are literal v i b g position 0 1 2 3 index 0 1 2 3 2 Why? This will become clearer later, but we have already hinted at the reason. 3

  4. Essentially, a value whose type is either IR or UV can always be used in a function which expects a value of type Colour. In other words, the use of subtypes allows us to define new types which are compatible with existing types and which can reuse existing VHDL code (such as functions). Subtypes of integer types can also be defined similarly. For example subtype MyInt is integer range 0 to 9; subtype YourInt is integer range 9 downto 0; Generally, a scalar subtype definition specifies a scalar type and selects a sub- range of values from the range of values corresponding to the scalar type. Subtypes of array-types can also be defined. If the array type is already constrained (such as IWord and Word above) then the only possible form of subtype is subtype NewWord is IWord; which basically says that the value set of NewWord is the same as that of IWord. But if the array type is unconstrained (such as UWord), then one can get subtypes by constraining the range of indices of UWord. For example subtype CWord is UWord(3 downto 0); subtype DWord is UWord(0 to 63); This mechanism of obtaining subtypes by constraining an unconstrained array type is especially useful. In general one can always create a subtype which is equivalent to an existing type (or subtype). subtype Ntype is Otype; 4 Functions and Operators When describing VHDL drivers, one has to be able to describe how they trans- form values on input signals to produce new values for the output signal corre- sponding to the driver. Clearly, we need a mechanism to expression computa- tions on values. In VHDL, this mechanism is provided by functions, which are classified into the following varieties: 1. User defined functions: those that are described by the programmer. 2. Function kind attributes: these are special functions that are provided by VHDL itself. For example, if T is an enumeration type, then the T’succ(x) function gives the successor literal to the literal x. For more details, see Chapter 6 from Perry’s book to get an idea of what these are. We will revisit them later. 4

  5. As far as user-defined functions are concerned, they need to be declared and defined (the declaration is not always necessary, but the definition must be available if simulation is to be performed). Consider the definition of a simple function which takes an input which is a bit vector type bit_vector is array(natural range <>) of bit; and returns its complement (elementwise). function Invert(x: bit_vector) is alias lx: bit_vector(1 to x’length) is x; variable r: bit_vector(1 to x’length); begin r := (others => ’0’); for I in 1 to lx’length loop if(lx(I) = ’0’) then r(I) := ’0’; end if; end loop; return(r); end function Invert; Several points can be noted here: • The function specifies input argument types and a return type. • It is possible for there to be more than one function with the same name. The compiler will match the argument and return types in the context in which the function was called in order to select the appropriate one. This is called function overloading. • At the point of call, argument values can be of subtypes of the argument types. The return value is of a subtype of the return type. • The alias is used to present a uniform view of the input argument value. • The return value has the required ordering of element values. The index type of the returned value has the required length, but its range cannot be specified from outside (the range is irrelevant in practice). • Most importantly, the function Invert can be called by passing it a value whose type is a subtype of bit vector. This is a powerful demonstration of the use of unconstrained types in VHDL 3 . User defined functions can also be in operator form: function "and" (x: bit; y: bit) return bit is -- etc.. 3 Why is this so special and important? 5

  6. and a call to the function is of the form c <= a and b; if a,b,c have type bit. 5 Further Reading Read up the material on types and functions in Perry’s book (Chapter 4, 5). 6

Recommend


More recommend