November 25

How to Describe a Digital Circuit for FPGA

0  comments

When dealing with processors or microcontrollers, we can create a routine for them to follow using predefined programming instructions tailored to that specific processor.

So, in the world of processors, the programming language is like a tool that helps you convey your ideas to the processor.

But what happens when we shift our focus to FPGAs?

How do we go about describing the digital circuit we've envisioned or designed on paper for FPGAs?

In this video, we'll delve into the process of describing a digital circuit for an FPGA or more specifically for the implementation software.

More...

As a quick refresher from our previous videos, we discussed the fundamental concept of FPGAs and how circuits are implemented within them.

We mentioned that within an FPGA, circuits are built using the available hardware resources.

But here's the question: how do we initially describe a digital circuit for FPGA?

Methods for Describing Digital Circuits in FPGA Design

There are generally two primary methods for describing circuits for FPGAs.

The first method involves using circuit schematics.

1- Circuit Schematics

In this schematic-based approach, you use a software tool (aka implementation software) along with basic digital components defined in that tool such as gates, flip-flops, multiplexers, encoders, decoders, and so on, to describe the circuit intended for the FPGA.

circuit schematic

Part of a digital circuit schematic.

By utilizing these building blocks, you can sketch out various circuits for the FPGA and describe their functionalities.

Although this method may appear straightforward, the reality is that it's not particularly practical.

Imagine you're tasked with designing a circuit containing thousands of gates and digital components.

Firstly, fitting this extensive design onto your small computer screen can be quite difficult

Now, picture this: you've successfully drawn the circuit, but then you discover an error or need to make a modification. It becomes a complex and nearly impossible task.

For this very reason, when it comes to describing large circuits within FPGAs, there exists another method that involves the use of Hardware Description Languages (HDLs).

2- Hardware Description Languages (HDLs)

An HDL is a specialized language designed with constructs and expressions that enable us to describe how a digital circuit behaves.

Unlike traditional programming languages that deal with commands or instructions, HDL isn't composed of commands because commands are primarily associated with CPUs (Central Processing Units).

As you're aware, FPGAs don't initially come with CPUs or any prebuilt hardware components that represent specific features.

So, an HDL isn't a language made up of commands.

Instead, it contains constructs and expressions that, while they might resemble programming commands at a glance, are in practice used to describe digital hardware.

Let me clarify the concept of hardware language using another example.

The code you're seeing in the figure is a construct called an if-then-else conditional in the programming world, and it's found in nearly all programming languages.

if then else conditional

If-then-else statement.

If you imagine this as a part of code written in the C language, for instance, which is intended to run on a processor, then the if-then-else instruction has already been defined for the CPU.

When the CPU encounters this instruction, it checks the value of C.

Depending on whether C is zero or not, it will copy either the value of A or B to a variable called F.

Now, let's switch gears and consider these statements as descriptions of digital hardware.

What digital hardware does this code describe?

You got it right; these statements are actually describing a 2-to-1 multiplexer.

2to1 MUX

A 2-to-1 multiplexer.

In a 2-to-1 multiplexer, depending on whether the select line C is zero or not, either input A or input B connects to output F.

As you can see, this code precisely represents and describes this digital concept.

So, as you've observed, in this hardware-focused code, there isn't an instruction meant for execution by a CPU.

Instead, we have a description of a digital hardware circuit.

Now, there are various hardware description languages out there, but the two most well-known and widely used ones are VHDL and Verilog.

While these two languages may have differences in their appearance and syntax, both of them allow you to define any type of digital circuit.

A Quick Introduction to VHDL

Let's delve a bit deeper into VHDL, which stands for VHSIC Hardware Description Language.

The 'V' in VHDL stands for "Very High-Speed Integrated Circuit," so VHDL stands for Very High-Speed Integrated Circuit Hardware Description Language.

Interestingly, VHDL was initially developed by the U.S. Department of Defense.

In the beginning, its main purpose was not to implement digital systems but rather to document circuits that had been previously created.

Another goal behind creating this language was to enable digital circuit modeling.

However, as time passed, VHDL found its use in implementing digital systems as well.

Ultimately, the IEEE organization standardized this language in 1987 as part of the IEEE 1076 standard, and the VHDL we use today is the standard language recognized within the IEEE organization.

Well, as I mentioned earlier, VHDL isn't a programming language; rather, it's a language used for describing digital hardware.

Now, let's get to know the structure of the VHDL.

Understanding the Structure of VHDL Code

In a nutshell, every VHDL code can be divided into two main parts. I'll walk you through these two parts using the figure below.

VHDL structure

The structure of a VHDL code.

In this figure, imagine we have an IC or a digital circuit. Typically, we can break down this digital circuit into two key sections:

One part includes the pins or input-output ports, while the other part forms the internal circuit.

In VHDL terms, the pins or input-output ports go by the name 'Entity,' and the internal circuit is referred to as 'Architecture.'

So, in a VHDL code, you'll generally find two main sections:

The first part is known as the 'Entity,' and the second part is labeled as 'Architecture.'

In the 'Entity' section, our primary task is to define the ports.

This involves giving names to the ports, and specifying whether they are inputs, outputs, or bidirectional. We also determine the type of these ports.

However, the meaty part of a VHDL code, which is the 'Architecture' section, is where we start describing the digital circuit itself.

To become even more familiar with VHDL, let's go over an example together.

Illustrating VHDL with a Full Adder Example

In this figure, you can see a full adder circuit along with logic expressions associated with its outputs.

Full adder circuit functions

A full adder circuit along with logic expressions associated with its outputs.

As you might recall from logic circuits, a full adder takes three inputs: A, B, and CarryIn (Cin), and it produces two outputs: Sum and CarryOut (Cout).

The logic expression for Sum is the result of an XOR operation on all three inputs, while the logic expression for the Carryout is: AB + A(Cin) + B(Cin).

To describe this function in VHDL, we need to first define the inputs and outputs using the 'Entity' and then describe the circuit in the 'Architecture' section.

This circuit involves three inputs: A, B, and Cin, and two outputs: Sum and Cout.

We can define these using the 'Entity' part in VHDL.

entity Full_Adder is 
	
	port ( 
			A   	: in	std_logic; 
			B   	: in	std_logic; 		
			Cin 	: in	std_logic; 		
			Sum 	: in	std_logic; 		
			Cout	: out std_logic 	           
		);

end Full_Adder; 

Now, in this concise code, you'll find the 'Entity' section that starts with the keyword 'Entity', followed by the name of the entity 'Full Adder', and then the keyword 'is'.

Within this section, we introduce the ports. For example, to define the first port, 'A', we first specify its name.

Then, after the colon, we determine whether it's an input or an output. In this case, it's an input, so we use the keyword 'in.' 

Finally, we specify the port type. Here, we've used the type 'STD_Logic,' which we'll discuss in more detail in future videos.

Once we've defined the ports, we move on to describing the internal circuit, and we do that in the 'Architecture' section.

architechture Behavioral of Full_Adder is 
begin 

	Sum  <= A xor B xor Cin; 
	Cout <= (A and B) or (A and Cin) or (B and Cin); 

end Behavioral; 

The 'Architecture' section begins with the keyword 'Architecture,' followed by the name of the architecture, then 'of,' the name of the Entity, and 'is.'

After the keyword 'begin', you're ready to describe the internal circuit within this area.

Thankfully, VHDL already includes basic functions like AND, OR, NOT, XOR, XNOR, NAND, and NOR.

With these, we can generate the Sum and Cout outputs.

For example, to compute the Sum output, we've written 'A XOR B XOR Cin' on the first line of architecture and assigned it to the Sum output.

To assign the result of this logic expression to the Sum output, we use the 'less-than-or-equal-to' symbol in this language, which is often referred to as the 'Assignment' operator.

So, this was a straightforward yet complete circuit for implementing a full adder in the VHDL language.

In upcoming videos, we'll delve deeper into VHDL's features and tackle the implementation of more complex circuits.

Did you find the tutorial "How to Describe a Digital Circuit for FPGA" helpful?

If you have any questions about this post, please feel free to ask in the comments section below. And if you enjoyed the content, don't forget to share it with your friends!

About the author 

Ahmad Saghafi

Hi, I’m Ahmad, founder of FPGATEK and creator of the FPGA Design Blueprint training. With over 15 years of hands-on experience and a wealth of knowledge from successfully implementing numerous industrial projects, I am thrilled to share my insights and expertise with you on this website.

Enjoyed this article?


You may also like:

Leave a Comment

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Get Started With FPGA In 20 Minutes

>