Skip to content
Tutorials Center
My learning

Topics

Lesson 4 of 37 ยท Getting Started

VHDL Syntax Basics

Learn VHDL syntax: case-insensitivity, identifiers, comments, literals, operators and their precedence, and how to read a VHDL file without getting lost.

Before you write larger designs, get comfortable with the small rules of the language: how names, numbers, comments and operators look. VHDL's syntax is derived from Ada, so it reads like structured English with plenty of keywords. It is wordy, but very regular.

Case, whitespace and comments

VHDL is case-insensitive. Clk, CLK and clk are the same identifier, and ENTITY equals entity. Whitespace and line breaks are free-form; statements end with a semicolon. Most style guides use lowercase for keywords and names.

Comments start with -- and run to the end of the line. VHDL-2008 adds block comments between /* and */.

VHDL
-- a single-line comment
y <= a and b;   -- comment after code
/* VHDL-2008:
   a block comment */

Identifiers

An identifier names an entity, signal, type and so on. The rules:

  • start with a letter, then use letters, digits and single underscores;
  • no two underscores in a row and no trailing underscore (data__in and data_ are illegal);
  • must not be a reserved word (entity, signal, in, out, process, begin, end, is, port, map, type, case, when, and more).

Good names say what a signal means: data_valid, wr_en, next_state. Because case does not matter, you cannot have both Data and data in the same scope.

Literals

Kind Examples Notes
Integer 42, 1_000_000, 16#FF#, 2#1010# Underscores help readability; base#digits# for other bases
Real 3.14, 1.0e-6 Must contain a decimal point
Character '0', '1', 'Z' Single quotes: one value of an enumeration
String "hello" Double quotes
Bit string "1010", x"FF", b"0110", o"17" Hex: each digit is 4 bits. x"FF" is "11111111"
Physical 10 ns, 5 us, 100 MHz (user-defined) Number plus unit; time is built in

A frequent beginner error is using ' for multi-bit values. '1' is one bit (a character); "0101" is a vector of four bits.

Operators and precedence

VHDL groups operators into classes. From lowest to highest precedence (VHDL-2008 adds the condition operator ?? and the reduction forms, left out here):

  1. Logical: and, or, nand, nor, xor, xnor
  2. Relational: =, /=, <, <=, >, >=
  3. Shift: sll, srl, sla, sra, rol, ror
  4. Adding: +, -, & (concatenation)
  5. Sign: unary +, -
  6. Multiplying: *, /, mod, rem
  7. Miscellaneous: **, abs, not

Two traps catch everybody. First, the logical operators have the same precedence among themselves, and mixing different ones without parentheses is an error. a and b or c does not compile; write (a and b) or c. (Repeating the same operator, a and b and c, is fine.) Second, not binds tighter than anything else: not a and b means (not a) and b.

VHDL
y <= (a and b) or (not c);     -- parentheses are required here
z <= not (a or b);             -- not applied to the whole group
w <= "10" & "01";              -- concatenation gives "1001"

The & operator is concatenation, not logical AND. It glues bits and vectors together and is used constantly to build buses.

The structure of a VHDL file

A file is a sequence of design units: entity, architecture, package, package body, configuration. Each unit may start with a context clause (library and use). A skeleton:

VHDL
library ieee;                       -- context clause
use ieee.std_logic_1164.all;

entity thing is                     -- primary unit
  port ( ... );
end entity thing;

architecture rtl of thing is        -- secondary unit
  -- declarations (signals, types, constants, components)
begin
  -- statements (concurrent)
end architecture rtl;

The area between is and begin is the declarative part: you create names there. The area after begin is the statement part: you use the names there.

Modeling operators in C

Two operators trip up C programmers. The shift and rotate operators in VHDL include arithmetic and rotating versions, and concatenation has no C equivalent. This stand-in imitates them for 8-bit values so you can predict results before you meet them in real VHDL.

C Stand-in (C): shift, rotate and concatenation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

(s >> 2 on a negative signed value is arithmetic on virtually every compiler, which is what sra does in VHDL.)

Reading numbers

This second stand-in converts the VHDL literal styles into decimal so you can double check what you write: 16#FF# is 255, x"FF" is the same bits, 2#1010# is 10.

C Stand-in (C): based literals

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Write bit patterns in hex (x"A5") when the width is a multiple of four, and in binary ("10100101") when individual bits carry separate meaning, such as control flags.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Are Data_In and data_in the same identifier in VHDL?

    VHDL does not distinguish upper and lower case in identifiers or keywords.
  2. 2. Which expression is legal VHDL?

    Different logical operators may not be mixed without parentheses. && does not exist in VHDL.
  3. 3. What is x"3C" as a bit string?

    Each hex digit expands to four bits: 3 is 0011 and C is 1100.
  4. 4. What does "10" & "01" produce?

    The ampersand concatenates, so the two vectors are joined in order.

Summary

  • VHDL is case-insensitive; comments use -- (and /* */ in VHDL-2008).
  • Identifiers start with a letter, avoid double or trailing underscores, and avoid reserved words.
  • '1' is a character (one bit); "1010" and x"FF" are bit strings.
  • Mixing different logical operators requires parentheses; & concatenates.
  • A file has a context clause followed by design units; declarations go before begin.

Next: Entity Declarations and Ports