Channels ▼

Al Williams

Dr. Dobb's Bloggers

Pretzel Logic

November 12, 2011

There's nothing special about the order of the parameters, any more than a C function call's parameters matter to the compiler. You simply have to match their order when you use them. But don't think of a module as a subroutine. Think of it as an electronic black box.

Here's the function I want my black box to perform written in C (note that inputs and outputs — at least the way I wrote them above — are one-bit wide):

y=(in1 & in2) & in3;

The truth is, I could write one line of Verilog to capture this:

assign y=(in1 & in2) & in3;  // parenthesis not necessary here

The assign statement effectively says: Every time in1, in2, or in3 changes, compute this expression and assign it to y. That's different than in C. In C, the compiler will generate code that computes y one time with the current values of the inputs. In Verilog, a synthesizer creates an AND gate that constantly monitors the inputs and produces an output.

However, I said I wanted to use two 2-input AND gates, so let's use another method to generate an AND gate (you'd rarely use both methods in one module, other than in an example like this):

module and_test(output y, input in1, input in2, input in3);
wire temp;
assign y=temp & in3;

The assign statement is the same as before, but this time it uses a "wire" (which is just a one-bit variable, sort of). In C, this would be a problem because we didn't set temp to anything yet. But this isn't C! What sets temp is the and gate on the line below.

The and() module is built into Verilog and, like a C printf, can take any number of arguments. So the whole module could be: and(y,in1,in2,in3). Well, you still need the module statement and the endmodule statement, but you know what I mean.

The logical operators are what you expect from C: & (AND), | (OR), ~ (NOT), and ^, (XOR). You can also use and, or, nor, xor, nana, xnor, and not as primitive gates.

The and_test module is ready to synthesize or use in another module. Next time I'll show you how you might write a test bench to check out the module and examine the signals during simulation.

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.