Global training solutions for engineers creating the world's electronics

Encapsulation in VHDL

You can hardly turn the page of a software journal today without being reminded of the benefits of object oriented (OO) techniques. Although not an OO language, OO principles can still be applied to VHDL. A cornerstone of object-oriented programming (OOP) is encapsulation. Let's look at applying the principle of encapsulation to VHDL.

Encapsulation is a mechanism used for information hiding. Encapsulation allows access to the contents of a data structure only through function calls which operate on that data.

In VHDL, we can group a data structure and its associated functions in a package. However, a VHDL package is an abstract data type (ADT) when used in this way. VHDL does not support encapsulation through a package because the data structure is accessible without recourse to the package's functions. This means that encapsulation in a true OO sense actually relies on VHDL programmer discipline. That's the tip - become a disciplined programmer! For example,

package matrix_class is
  use maths.maths_class.all;
  type integer_matrix is array (INTEGER range <>, INTEGER range <>) of INTEGER;
  type matrix_element is record
    row    : INTEGER;
    column : INTEGER;
  end record;

  function get_element (
    matrix : integer_matrix;
    location : matrix_element;
    ) return integer;

  function set_element (
    matrix : integer_matrix;
    element : INTEGER;
    location : matrix_element;
  ) return integer;

  function "*" (
    multiplicand : integer_matrix;
    multiplier   : integer_matrix
  ) return integer_matrix;
end matrix_class;

This package provides definitions for a data type and some functions used in the development of matrix-oriented algorithms. Note that in true encapsulation style, get_ and set_ functions are provided for accessing elements of the integer_matrix data structure. The advantage of using encapsulation is that we don't need to remember the structure of the data type to access an element, we just need to remember the name of the access functions. This is a general point, but not that applicable here unless we consider that the function can also include exception handling code (for example, if we try to access an element not in the range of the matrix, we are warned). In use,

a(3, 0) := get_element (b, (0,3));
-- exception handling code in the function body
-- as opposed to a(3, 0) := b(0,3); plus all the exception handling code!

Because VHDL does not enforce encapsulation through the rules of the language, the principle of grouping a data type definition and its associated functions in a package is sometimes referred to as pseudo-private data typing.

 

Prev    Next

Your e-mail comments are welcome - send email

Copyright 1995-2002 Doulos

Great training!! Excellent Instructor, Excellent facility ...Met all my expectations.
Henry Hastings
Lockheed Martin

View more references