Channels ▼

Al Williams

Dr. Dobb's Bloggers

Pretzel Logic

November 12, 2011

The last few weeks I've been musing about how FPGAs and Verilog are working their way into many embedded systems. I mentioned last time that it is much simpler to describe logic in a high-level language like Verilog (or VHDL) instead of drawing huge schematics. The wires required to hook up even a small CPU would quickly look tangle like a pretzel — or maybe a plate of spaghetti (not to mention real life wires; see Steve Chamberlin's Big Mess 'o Wires computer, for example).

I also mentioned that you have to build a crystal radio before you can start working on a multichannel digital stereo system. To that end, this time I want to show you what some very simple logic circuits in Verilog look like.

A Verilog "program" consists of modules. Modules can contain other modules or basic Verilog statements. Typically, a Verilog program has one of two possible purposes. In some cases, an engineer writes Verilog to simulate a system. A simulation often has commands that you use to write data to a file, for example. It might have specific timing delay information. Frequently, the main part of a simulation looks suspiciously like a C program where the test fixture (or test bench) steps through a sequence of commands.

Other Verilog modules are made for synthesis. That is, some tool is going to parse the Verilog and generate an electronic circuit from it and then further process that circuit to create the programming file for an FPGA. This is, of course, highly vendor-specific and not all tools support all the same synthesis constructs. In general, you will have to use a very particular subset of Verilog if you expect your code to be synthesizable. Frequently, a test bench will instantiate synthesizable modules to test. That's ok. The Verilog simulation can do anything. The synthesis is where certain operations won't work.

Keep in mind, though, there's no official way to tell which kind of module you are looking at. If you know what synthesis tool you are using and you know what statements are not allowed for synthesis using that tool, then the presence of forbidden commands means the module is not synthesizable (by that tool). Even then, some synthesizers simply ignore certain constructs, so it can still be hard to tell if the developer intended the module to be dual-purpose or not.

To start, let's consider a simple circuit that uses two AND gates, each with two inputs. In Verilog, there are at least two ways to accomplish this task. First, you need a module statement that defines the inputs and outputs:

module and_test(output y, input in1, input in2, input in3);

This is the "new" style of writing modules (analogous to ANSI C). Older versions used a more K&R C-like style, and you may still see examples of that on the Internet.

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video