VHDL Programming

(C. Jardin) #1

Data Types 93


IS VARIABLE result : BIT_VECTOR(0 TO (val’LENGTH -1));
BEGIN
result := val;
IF (val’LENGTH > 1) THEN
FOR i IN 0 TO (val’LENGTH -2) LOOP
result(i) := result(i  1);
END LOOP;
result(val’LENGTH -1) := 0;
ELSE
result(0) := 0;
END IF;
RETURN result;
END shift_right;
END mypack;

The package declaration (the first five lines of the model) declares two
subtypes:eightbitand fourbit. These two subtypes are subtypes of the
unconstrained base type BIT_VECTOR. These two types constrain the base
type to range 0 to 7 for type eightbitand range 0 to 3 for type fourbit.
In a typical hardware description language without unconstrained
types, two different shift-right functions would need to be written to han-
dle the two different-sized subtypes. One function would work with type
eightbit, and the other would work with type fourbit. With uncon-
strained types in VHDL, a single function can be written that will handle
both input types and return the correct type.
Based on the size of input argument val, the internal variable result
is created to be of the same size. Variable resultis then initialized to
the value of input argument val. This is necessary because the value of
input argument valcan only be read in the function; it cannot have a
value assigned to it in the function. If the size of input argument valis
greater than 1, then the shift-right function loops through the length of
the subtype value passed into the function. Each loop shifts one of the bits
of variable resultone bit to the right. If the size of input argument val
is less than 2, we treat this as a special case and return a single bit whose
value is ‘ 0 ’.

RECORD TYPES Record types group objects of many types together
as a single object. Each element of the record can be accessed by its field
name. Record elements can include elements of any type, including arrays
and records. The elements of a record can be of the same type or different
types. Like arrays, records are used to model abstract data elements.

Following is an example of a record type declaration:

TYPE optype IS ( add, sub, mpy, div, jmp );
Free download pdf