Example: Multiplexer using conditional signal assignment
Example: Multiplexer using selected signal assignment
The structure of a VHDL source code is based on the inclusion of libraries, packages, and the description of VHDL modules. Depending on how it implements a design, the easiest way to imagine how the modules communicate is to compare it to an electrical circuit. A module has input and output signals which provide the connection between the modules. Depending on the EDA tool, the link between two or more modules can be made by typing the ports directly, as shown in the example below, or by using tools that provide the modules as parts, similar to a spice program for the analog simulation. | |
Module 1 | |
Module 2 | |
Module 3 | |
… |
Libraries are a collection of packages that define the operators and operations in the logic utilized to describe a digital system. The IEEE library is commonly used; it defines a binary representation consisting of values of 0 and 1, and a standard logic representation (std_logic) with nine distinct logic values, of which the most important are: 0, 1, X (unknown) and Z (high impedance). To include a library in VHDL, the reserved word library is used in an instruction of this form:
Packages are a collection of elements, like data types, subprograms, constants, or operators, within a library. The reserved word for the inclusion of a package from a library is use, the instruction is written as:
For the IEEE library, to use the logic primitive’s description, it is required to include the package IEEE.std_logic_1164. For this, write the following code sequence:
The declaration for interface-signal uses this form:
For example, the next lines of code are the declaration for the entity of an and-gate with two inputs and one output:
The architecture descriptor – architecture – describes the function of the digital circuit. In this part of a module, one or more abstraction level presented above are combined to create an architecture. The code can set out how the circuit behaves using functional blocks and the connection between them, or a mathematical description of the logical functions (truth table, transition diagram, algorithm). The declaration of an architecture uses this form:
In the declaration, the signals and constants are assigned if they are needed. To describe logical primitives, the body of VHDL module architectures consists of:
Example: The and-gate architecture with two inputs described with fundamental logic and is:
Module instantiation is used to connect circuit elements. At the top level, the connection is made between modules, and the EDA tool creates the netlist for all the components and connections that are used. A VHDL design description written exclusively with component instantiation is known as “Structural VHDL”. This method is very similar to the schematic entry; in this case, instead of using graphic elements, it is implemented as text. One or more components can be declared in the architecture body. To instantiate a component inside a design, the following two steps are needed:
The declaration section of the architecture is included between the keyword “is” and “begin”:
For example, to instantiate the and-gate with two outputs modules in another architectural model, the simplest way is to copy the entity of and2 and paste it to the architectural body. After that, change the “entity” to “component”.
bit | ‘0’ or ‘1’ |
Boolean | FALSE or TRUE |
integer | -(231-1) to (231+1) |
real | -1.0E38 to 1.0E38 |
character | “printable character”. Ex ‘d’, ‘A’, ‘4’, ‘+’ |
std_logic time | ‘1’ Logic 1 ‘0’ - Logic 0 ‘Z’ - High impedance ‘W’ - Weak signal, can’t tell if 0 or 1 ‘L’ - Weak 0, pulldown ‘H’ - Weak 1, pullup ‘-‘ - Don’t care ‘U’ - Uninitialized ‘X’ - Unknown, multiple drivers Integer + unit (fs, ps, ns, us, ms, sec, min, hr) |
* users defined data, for example, enumeration:
VHDL is a strongly typed language, so different data types cannot be combined in the same instruction.
When designing with a hardware description language, data buses are often used. Data buses comprise multiple bit buses. The data types presented above allow single bits to be modeled in a design. A vector consists of one or more bits that are modeled similarly to one of the predefined data. The most basic and encountered types of vectors in VHDL are made up of several bit or std_logic types. The code below is an example of how to declare a vector type signal in VHDL.
The [ range ] field is used to determine the number of bits in the vector and the location of the most significant and least significant bits. The key word used to describe the range value in VHDL are: downto and to. The next lines present a declaration of an 8-bit vector using the keywords:
When writing VHDL code, it is often necessary to do a data conversion. Two general methods to convert between data types are available. The first method is to cast the signal to the correct type. In this method, it is converted between the signed, unsigned and std_logic_vector.
The code below is an example demonstrating the syntax to cast signals or data.
The second way employs functions. This method is used to convert between the signed or unsigned types and the integer type. To use a suitable conversion function, it is needed to include either the numeric_std or the std_logicarith packages. These packages are available in the IEEE library.
When assigning data to a vector type, VHDL uses quotation marks (“) instead of apostrophes. Also, it can specify to use a hexadecimal notation by adding an ‘x’ to the start of the data. This works if the number of bits is a factor of four. Using octal values can be done by adding an ‘o’ to the start of the data. This works only with VHDL-2008. The code below gives some examples of how to assign data to vector types in VHDL.
Because VHDL is similar to other programming languages, it allows variables. These are declared in the architecture declaration area.
Example: In this example, several variables of each of the above types are declared and used in a small code combination:
VHDL has a set of operators that can be used in creating designs. Predefined operators:
binary operators | and | logical and |
or | logical or | |
and | the logical complement of and | |
nor | the logical complement of or | |
xor | logical exclusive of or | |
xnor | the logical complement of exclusive of or | |
relational operators | = | test for equality |
/= | test for inequality | |
< | test for less than | |
⇐ | test for less than or equal | |
> | test for greater than | |
>= | test for greater than or equal | |
shifting operators | sll | shift left logical |
srl | shift right logical | |
sla | shift left arithmetic | |
sra | shift right arithmetic | |
rol | rotate left | |
ror | rotate right | |
additive operators | + | addition |
- | subtraction | |
& | concatenation | |
sign operators | + | unary plus |
- | unary minus | |
multiplicator operators | * | multiplication |
/ | division | |
mod | modulo | |
rem | remainder | |
other operators | not | compliment |
abs | absolute value | |
** | exponentiation | |
assignment operator | <= | assignment of signals |
:= | assignment of variables and signal initialization | |
timing operators | after, wait |
Example 1: After declaring the variables, f is assigned the value between a and b using a binary operator. The binary operator “and” can be replaced with another binary operator according to the designer's need. This operation is performed after the delay; for this task, the timing operator “after” is used.
Example 2: Uses the timing operator “wait” to implement a breakpoint and wait for an event specified to happen:
Example 3: Uses different shift operators to make changes at a bit level.
Example 4: The combined use of several operators:
Get the latest updates on new products and upcoming sales
Full Training Programs
Free Online Training Events
Free Technical Resources
Find a training course, vhdl-2008: easier to use.
Many of the enhancements in VHDL-2008 are intended to make VHDL easier to use. These are all fairly minor additions to the language or changes to the syntax. Nevertheless, they will make a real difference in day-to-day VHDL design.
How many times have you wanted to write something like this:
where A and B are STD_LOGIC ? You haven’t been able to, because VHDL’s if statement requires a boolean expression, not a STD_LOGIC one. You have to write this instead:
VHDL-2008 introduces a new operator, ?? . It ’s called the condition operator and it converts a STD_LOGIC expression to a BOOLEAN one: '1' and 'H' are considered TRUE and everything else FALSE . (It also converts BIT to BOOLEAN ) So you can now write this:
or, even better ...
In certain circumstances, ?? is applied implicitly. The condition expression of an if statement is one of those. So you can indeed now write:
You use string literals as literal values of type STD_LOGIC_VECTOR or similar. For example,
In VHDL-1987, string literals provided, in effect, a way of expressing a vector as a binary number. VHDL-1993 introduced binary, octal and hexadecimal bit string literals :
One limitation in VHDL-1993 is that hexadecimal bit-string literals always contain a multiple of 4 bits, and octal ones a multiple of 3 bits. You can’t have a 10-bit hexadecimal bit-string literal, or one containing values other than 0, 1 or _, for example.
In VHDL-2008, bit string literals are enhanced:
Here are some examples:
Note that within bit string literals it is allowed to use either upper or lower case letters, i.e. F or f.
Some of the new features in VHDL-2008 are intended for verification only, not for design. Verification engineers often want to write self-checking test environments. In VHDL this can be difficult as there is no easy way to access a signal or variable buried inside the design hierarchy from the top level of the verification environment.
VHDL-2008 addresses this by introducing external names . An external name may refer to a (shared) variable, signal, or constant which is in another part of the design hierarchy. External names are embedded in double angle brackets << >>
Special characters may be used to move up the hierarchy ^ and to root the path in a package @ . Some examples:
Other uses for external names include injecting errors from a test environment, and forcing and releasing values (see later).
VHDL aggregates allow a value to be made up from a collection individual array or record elements. For arrays, VHDL up to 1076-2002 allows syntax like this:
VHDL-2008 allows the use of a slice in an array aggregate. So for instance the examples above could be written:
It is also possible to use aggregates as the target of an assignment, like this:
Historically there have been two styles of writing "decision" statements in VHDL - concurrent and sequential. And you had to get them correct - you could not use a conditional signal assignment such as...
in a process. VHDL-2008 relaxes this and allows a flip-flop to be modelled like this:
It is also permitted to use the selected signal assignment in a process:
VHDL-2008 makes the generate statement much more flexible. It is now allowed to use else and elsif . Also there is a case version of generate .
This makes generate easier to use. Instead of writing
you can write case...generate :
or you can write if...elsif...else generate :
Note that within each branch, you can still declare local names which will not clash with names in the other branches (such as label c1 above). It is still possible to declare local objects within the branch using begin-end .
The when others and else generate branches can be empty (do nothing) or may contain statements like the other branches.
The keyword all may be used in the context of a sensitivity list, to mean that all signals read in the process should be added to the sensitivity list, for example:
This avoids a common problem where the author modifies a combination process and then forgets to update the sensitivity list, leading to a simulation/synthesis mis-match.
VHDL has a well-designed package IEEE.Numeric_Std which creates two new data types unsigned and signed . However it would sometimes be convenient to do arithmetic on std_logic_vector directly - treating it as either two's complement or unsigned.
In the past this has mainly been achieved by using the non-standard std_logic_unsigned and std_logic_signed packages . VHDL-2008 addresses this issue by adding two new standard arithmetic packages, IEEE.Numeric_Std_Unsigned and IEEE.Numeric_Std_Signed .
Previous Next
Go back to the main VHDL-2008 page.
Lockheed martin.
View more references
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Welcome to our site edaboard.com is an international electronics discussion forum focused on eda software, circuits, schematics, books, theory, papers, asic, pld, 8051, dsp, network, rf, analog design, pcb, service manuals... and a whole lot more to participate you need to register. registration is free. click here to register now..
When using std_logic_vectors(xx down to yy) I can use either binary numbers in the form "11010101" or hex numbers in the form x"00". How can I use decimal numbers?
I guess you can use real datatype. SIGNAL A: real := 0.0; -- double
ravics said: I guess you can use real datatype. SIGNAL A: real := 0.0; -- double Click to expand...
what about the function , conv_std_logic_vector?
sometimes you can use as --***************************** entity decimal is Port ( clk : in STD_LOGIC; res : in STD_LOGIC; cnt : out STD_LOGIC_vector(0 to 5)); end decimal; architecture Behavioral of decimal is signal cnt_s : std_logic_vector(0 to 5); begin SM : process (Clk) begin if (Clk'event and Clk = '1') then if (Res = '1') then cnt_s <= "000000"; elsif cnt_s = 16 then --***************************** cnt_s <= "000000"; else cnt_s <= cnt_s + 4; --***************************** end if; end if; end process SM; cnt <= cnt_s; end Behavioral;
well in define of constant constant8: std_logic_vector(15 downto 0) := 20000; the vhdl take error.:sad: what should i do????
Advanced member level 7.
maia31 said: well in define of constant constant8: std_logic_vector(15 downto 0) := 20000; the vhdl take error.:sad: what should i do???? Click to expand...
signal a : integer range 0 to 1023 --limit to 10 bits signal a : integer range 1024 to 2047 --in some situations limit to 10 bits I hate Integer
treqer said: signal a : integer range 0 to 1023 --limit to 10 bits signal a : integer range 1024 to 2047 --in some situations limit to 10 bits I hate Integer Click to expand...
I do not use Integer if you want to then convert it into a bit signal, and work with individual bits.
treqer said: I do not use Integer if you want to then convert it into a bit signal, and work with individual bits. Click to expand...
I think you're getting very confused. Like I said, an integer is not a bit type. VHDL is strongly typed so has some strong rules, hence why you cannot assign an integer directly to a std_logic_vector. Also comparing VHDL to C# is not valid, as VHDL is not a programming language.
Welcome to edaboard.com.
VHDL assignments are used to assign values from one object to another. In VHDL there are two assignment symbols:
Either of these assignment statements can be said out loud as the word “gets”. So for example in the assignment: test <= input_1; You could say out loud, “The signal test gets (assigned the value from) input_1.”
Note that there is an additional symbol used for component instantiations (=>) this is separate from an assignment.
Also note that <= is also a relational operator (less than or equal to). This is syntax dependent. If <= is used in any conditional statement (if, when, until) then it is a relational operator , otherwise it’s an assignment.
One other note about signal initialization: Signal initialization is allowed in most FPGA fabrics using the := VHDL assignment operator. It is good practice to assign all signals in an FPGA to a known-value when the FPGA is initialized. You should avoid using a reset signal to initialize your FPGA , instead use the := signal assignment.
Learn Verilog
Save my name, email, and website in this browser for the next time I comment.
Find centralized, trusted content and collaborate around the technologies you use most.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Get early access and see previews of new features.
In verilog, I can assign a string to a vector like:
In VHDL, I'm having difficulty finding a method like this:
I've been using:
which is unclear and time consuming for large strings.
I've looked at the textio package and the txt_util package , but neither seem to be very clear on how to interpret a string and convert it to std_logic.
Is there a simple method of assigning ascii codes to std_logic in VHDL?
Here's a minimal example:
This one varies little for Morten's answer - it only uses one multiply, it copies the string instead of creating an alias, it uses an additional variable and it returns a standard logic vector with an ascending index range.
From a package called string_utils:
You could add an argument with a default specifying ascending/descending index range for the return value. You'd only need to provided the argument for the non default.
A small general function is one way to do it, with a suggestion below:
To return an ascii value of a character, use this code:
In your example you are trying to assign a string type to a std_logic_vector type. That is simply not allowed. VHDL is strongly typed.
SIGNAL hello : OUT std_logic_vector (39 DOWNTO 0); ... hello <= "hello";
If your goal is to convert from hexa to ascii for printing simulation result you can simply do that:
character'val(to_integer(unsigned(my_std_logic_vector)))
Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more
Post as a guest.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy .
Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
I'm getting a syntax error near data0_sim in the following code - New to vhdl and confused as I think this should work:
We can only guess since you haven't told us the syntax error, but from the code posted it MIGHT be:
Now there are two things you might be trying to do here:
1) declare a signal and give it an initial value. The correct syntax for that is:
Note that the initialiser uses the variable assignment syntax,to indicate that signal assignment semantics (postponed assignment, event generation) don't apply.
2) declare a signal and later, assign it a value.
The correct syntax for that requires more context : declarations and statements occupy two different spaces in a VHDL unit. This follows programming languages such as Ada, but it is rather different from C.
In VHDL, the context may be an entity/architecture such as:
Why don't you define a component, i.e.
Sign up or log in, post as a guest.
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy .
IMAGES
COMMENTS
The hex value is a bit string literal with a string value equivalent assignable to single dimensional arrays. An integer requires an abstract literal, a based literal of the form 16#38#. Where 16 is the base, the '#' is a delimiter. See IEEE Std 1076-2008 15. Lexical elements, 15.5.3 Based literals.
VHDL supports arithmetic values but it has to know if they are signed or unsigned. I recommend using the numeric_std library to support these types. VHDL is strongly typed. For your first question, I would use: EDIT: I have erased a prior more complex form and used this simpler one after Brian Dummond's comment
x"11A" is a "hexadecimal bit string literal". Prior to VHDL-2008, these had to be a multiple of 4 bits, hence the problem you're seeing. VHDL-2008 removed this restriction, so you can now write 10x"11A". I don't know how much tool support there is for 2008, though. I need to read more about VHDL 2008. Seems to be full of useful features :)
assign hex value I want to creat a code in which there is variabe name let Data_Byte and want assign hex value 0x41, initially to this variable. ... assign hex values in vhdl If I understood: vhdl: signal Data_Byte : std_logic_vector( 7 downto 0) := x"41"; ... hex value assignment.. Data_Byte <= x"BC"; For your problem first initialize..
In VHDL, hex is a much easier way to set bigger vectors instead of regular binary. This is because one symbol of hex is 4-bits of binary. For example: signal my_slv : std_logic_vector(15 downto 0);-- binary method my_slv <= "0000000000000001";-- hex method my_slv <= x"0001"; You can also mix binary and hex for uneven 4-bit vectors:
The most basic of complete VHDL statements, a signal assignment is likely also one of the most common. Syntax: < signal_name > <= < expression >; -- the expression must be of a form whose result matches. the type of the assigned signal. Examples: std_logic_signal_1 <= not std_logic_signal_2; std_logic_signal <= signal_a and signal_b;
This example shows how to convert a hexadecimal value to a std_logic_vector. It is shown in both VHDL '87 (IEEE Std 1076-1987) and VHDL '93 (IEEE Std 1076-1993). For more information on using this example in your project, refer to the how to use VHDL examples section on the VHDL web page. hex.vhd. PORT(. D : OUT STD_LOGIC_VECTOR(7 DOWNTO 0));
Numeric literasls may be expressed in any base from 2 to 16. They may also be broken up using underscore, for clarity. A_INT <= 16#FF#; B_INT <= 2#1010_1010#; MONEY := 1_000_000.0; Real numbers may be expressed in exponential form: FACTOR := 2.2E-6; Literals of type time (and other physical types) must have units.
Keep these a binary strings - unfortunately until vhdl-2008, the case statements are intolerant of "&" and expressions. Not a good thing if the rest of your project team is using VHDL. Probably ought to invest in training instead. VHDL is quite simple once you get past the initial learning stuff. Cheers, Jim--
I'm not sure if this is a VHDL 2008 only operation, but in ieee.numeric_std there is a function called "to_hex_string" that takes in a std_logic_vector and outputs the hex representation as a string.
I have an 8-bit std_logic_vector and I would like to print its value (during simulation) as a 2-digit hexadecimal value. Current code: report "Entity: data_in=" & integer'image(to_integer(unsigned(data_in))); Current output: Entity: data_in=16 Desired output: Entity: data_in=10h What would be the most painless way to achieve this?
VHDL dataflow modeling follows the way the data flows through the design, from input to output. This abstraction level specifies the functionality of a design without specifying its structure. This involves the description at a gate level of the appropriate logic gates (AND, OR, XOR, etc.). The dataflow uses one or more signal assignment ...
One limitation in VHDL-1993 is that hexadecimal bit-string literals always contain a multiple of 4 bits, and octal ones a multiple of 3 bits. You can't have a 10-bit hexadecimal bit-string literal, or one containing values other than 0, 1 or _, for example. ... It is also permitted to use the selected signal assignment in a process: process ...
VHDL 2008 allows for enhanced bit literal assignments that were previously restricted to multiples of 4 for hex values and multiples of 3 for octal values. Now, it is possible to assign explicit widths that are either sign or unsigned and can even contain meta values, such as undefined or high impedance as shown in these examples
VHDL allows you to specify integer literals with a base by using the # wrapper. The number before the first # sign indicates the base. The base must be between 2 and 16, so each of these are valid 123 -- Base 10 implied 10#123# -- Base 10 explicitly specified 16#ABCD# -- Hexadecimal 8#777# -- Octal 5#1234321# -- Base 5
write(output, "0x" & to_hstring(to_signed(i, 32)) & LF); -- Hexadecimal representation. You need to have use std.textio.all; for this to work. Change the 32 to reduce the length of the hex value. I chose 32 so that it can represent any integer value in most simulators. These will also work for report statements, e.g.
There is also nothing wrong with using an integer. If you want an integer, use an integer. signal a : integer range 0 to 1023 --limit to 10 bits. a <= 10; then, if you really really have to convert it to a std_logic_vector. slv <= std_logic_vector ( to_unsigned ( a, 10) );
In VHDL there are two assignment symbols: <= Assignment of Signals. := Assignment of Variables and Signal Initialization. Either of these assignment statements can be said out loud as the word "gets". So for example in the assignment: test <= input_1; You could say out loud, "The signal test gets (assigned the value from) input_1.".
If you want to go to the next level, you could get a copy of the free version of Visual Studio and write a small console application that converts a data file into a VHDL file. I had to do something similar once to convert an intel HEX file into a constant array that was included in a C++ project.
0. In your example you are trying to assign a string type to a std_logic_vector type. That is simply not allowed. VHDL is strongly typed. SIGNAL hello : OUT std_logic_vector (39 DOWNTO 0); ... hello <= "hello"; If your goal is to convert from hexa to ascii for printing simulation result you can simply do that:
Note that the initialiser uses the variable assignment syntax,to indicate that signal assignment semantics (postponed assignment, event generation) don't apply. 2) declare a signal and later, assign it a value. The correct syntax for that requires more context : declarations and statements occupy two different spaces in a VHDL unit.