Diferență între revizuiri ale paginii „Verilog EN”

De la WikiLabs
Jump to navigationJump to search
Linia 6: Linia 6:
  
 
=== Verilog Module Interface ===
 
=== Verilog Module Interface ===
The '' Adder '' module interface is shown below. '''''' Note: '''''' As in the decimal system, where the sum of two numbers '''n''' digits needs '''n + 1''' digits (9 + 9 = 18), and in the binary system, the sum of two ''' n '''bits will be on '''n + 1''' bits.
+
The '' Adder '' module interface is shown below. '''''' Note: '''''' As in the decimal system, where the sum of two numbers '''n''' digits needs '''n + 1''' digits (9 + 9=18), and in the binary system, the sum of two ''' n '''bits will be on '''n + 1''' bits.
 
[[File: Adder_interface.svg | thumb | Representation of the Adder module (black box)]]
 
[[File: Adder_interface.svg | thumb | Representation of the Adder module (black box)]]
<syntaxhighlight lang = "verilog">
+
<syntaxhighlight lang="verilog">
 
module Adder (
 
module Adder (
 
     output [4: 0] out,
 
     output [4: 0] out,
Linia 21: Linia 21:
 
The keywords''''' module''''' and ''''' endmodule '''''are used to start and end defining a module. Immediately after the keyword '''''module''''' follows the module name.
 
The keywords''''' module''''' and ''''' endmodule '''''are used to start and end defining a module. Immediately after the keyword '''''module''''' follows the module name.
  
<div class = "convention">
+
<div class="convention">
<font color = "#0000AA"> '''''Convention:''''' </font> The name of a module will begin with a large letter.
+
<font color="#0000AA"> '''''Convention:''''' </font> The name of a module will begin with a large letter.
</Div>
+
</div>
  
 
After defining the module name, the list of ports, placed between '''round brackets''' and separated by''' comma ''' follows. Accepted keywords are '''''output''''' (representing an output port), '''''input''''' (representing an input port) and '''''inout'''''(representing a bidirectional port).
 
After defining the module name, the list of ports, placed between '''round brackets''' and separated by''' comma ''' follows. Accepted keywords are '''''output''''' (representing an output port), '''''input''''' (representing an input port) and '''''inout'''''(representing a bidirectional port).
  
<div class = "tips">
+
<div class="tips">
<font color = "# 00AA00"> '''''Tip:''''' </font> ''. They introduce [http://en.wikipedia.org/wiki/Tri-state_buffer#Tri-state_Buffer Tri-state Buffer] elements that are ineffective. A more efficient alternative is to define two ports, one input and one output, with similar names (ex: '' data_in '' and '' data_out '').
+
<font color="#00AA00"> '''''Tip:''''' </font> ''. They introduce [http://en.wikipedia.org/wiki/Tri-state_buffer#Tri-state_Buffer Tri-state Buffer] elements that are ineffective. A more efficient alternative is to define two ports, one input and one output, with similar names (ex: '' data_in '' and '' data_out '').
</Div>
+
</div>
  
<div class = "convention">
+
<div class="convention">
<font color = "# 0000AA"> '''''Convention:''''' </font> First the outputs, then the inputs of a module are defined.
+
<font color="#0000AA"> '''''Convention:''''' </font> First the outputs, then the inputs of a module are defined.
</Div>
+
</div>
  
<div class = "convention">
+
<div class="convention">
<font color = "# 0000AA"> '''''Convention:''''' </font> ''''' module '''''.
+
<font color="#0000AA"> '''''Convention:''''' </font> ''''' module '''''.
</Div>
+
</div>
  
 
According to the port type, it follows its size, specified in the bit indexes, where the least significant bit has the index 0. For example, a 4 bit signal will have the following specification: '''[3:0]''' significantly has index 3, least significant 0, total 4 bits). '''''Note:''''' One-bit signals lack the size specification:
 
According to the port type, it follows its size, specified in the bit indexes, where the least significant bit has the index 0. For example, a 4 bit signal will have the following specification: '''[3:0]''' significantly has index 3, least significant 0, total 4 bits). '''''Note:''''' One-bit signals lack the size specification:
  
<syntaxhighlight lang = "verilog">
+
<syntaxhighlight lang="verilog">
 
input signal_de_un_bit,
 
input signal_de_un_bit,
 
</syntaxhighlight>
 
</syntaxhighlight>
Linia 48: Linia 48:
  
 
<! -
 
<! -
# '''''Remark:''''' There are non-synthesizable modules (which do not have logical gaps), which are used as test programs in simulation environments and are called test modules. These modules have no input and output ports and are used only to give the input port values of the module to be tested, and to check the values on the output ports of the module. For these modules, the list of ports and their associated parentheses are missing:
+
#'''''Remark:''''' There are non-synthesizable modules (which do not have logical gaps), which are used as test programs in simulation environments and are called test modules. These modules have no input and output ports and are used only to give the input port values of the module to be tested, and to check the values on the output ports of the module. For these modules, the list of ports and their associated parentheses are missing:
<syntaxhighlight lang = "verilog">
+
<syntaxhighlight lang="verilog">
 
Module TestModule;
 
Module TestModule;
  
Linia 69: Linia 69:
  
 
Blocks that are always unintelligible and are used exclusively for simulation:
 
Blocks that are always unintelligible and are used exclusively for simulation:
* blocks '' 'initial' ''
+
* blocks '''initial'''
  
 
'''''' Remark: '''''' Not any block '''assign''' or '''always''' is synthesizable. There are syntactic constructs that do not have a correspondent in the circuit. These blocks can be simulated but can not be used to program an [[FPGA]] board.
 
'''''' Remark: '''''' Not any block '''assign''' or '''always''' is synthesizable. There are syntactic constructs that do not have a correspondent in the circuit. These blocks can be simulated but can not be used to program an [[FPGA]] board.
Linia 79: Linia 79:
 
The threads are used for linking modules and for assigning partial results to combinational circuits, therefore:
 
The threads are used for linking modules and for assigning partial results to combinational circuits, therefore:
  
<div class = "rule">
+
<div class="rule">
<font color = "red"> ''''' Rule: '''''</font> '''or''' as the output of a module (never both simultaneously).
+
<font color="red"> ''''' Rule: '''''</font> '''or''' as the output of a module (never both simultaneously).
 
</div>
 
</div>
  
 
The threads in a Verilog module are defined as follows:
 
The threads in a Verilog module are defined as follows:
<syntaxhighlight lang = "Verilog">
+
<syntaxhighlight lang="Verilog">
  
 
wire [3:0] wire;
 
wire [3:0] wire;
Linia 95: Linia 95:
  
 
A register is defined as follows:
 
A register is defined as follows:
<syntaxhighlight lang = "Verilog">
+
<syntaxhighlight lang="Verilog">
  
 
reg [3:0] register;
 
reg [3:0] register;
Linia 101: Linia 101:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
<div class = "rule">
+
<div class="rule">
<font color = "red"> ''''' Rule: '''''</font> or '''initial'''.
+
<font color="red"> ''''' Rule: '''''</font> or '''initial'''.
 
</div>
 
</div>
  
<div class = "rule">
+
<div class="rule">
<font color = "red"> '''''Rule:''''' </font> No element can change its value in more than one block. That is, for a wire ('' wire ''), there can not be two blocks '' 'assign' '' in which it takes values, and for the elements '''reg''' '''always''' or '''initial''' in which it changes its value.
+
<font color="red"> '''''Rule:''''' </font> No element can change its value in more than one block. That is, for a wire ('' wire ''), there can not be two blocks '''assign''' in which it takes values, and for the elements '''reg''''''always''' or '''initial''' in which it changes its value.
 
</div>
 
</div>
  
 
'''''Note:''''' An entry of a module is always ''wire''. Thus, the statement
 
'''''Note:''''' An entry of a module is always ''wire''. Thus, the statement
<syntaxhighlight lang = "Verilog">
+
<syntaxhighlight lang="Verilog">
  
 
     input [3:0] in0,
 
     input [3:0] in0,
Linia 118: Linia 118:
 
is equivalent to
 
is equivalent to
  
<syntaxhighlight lang = "Verilog">
+
<syntaxhighlight lang="Verilog">
  
 
     input wire [3:0] in0,
 
     input wire [3:0] in0,
Linia 126: Linia 126:
  
 
'''''Remark:''''' An output of a module may be '''wire''' or '''reg'''. If not specified, it is a ''' wire'''type. Thus, the statement
 
'''''Remark:''''' An output of a module may be '''wire''' or '''reg'''. If not specified, it is a ''' wire'''type. Thus, the statement
<syntaxhighlight lang = "Verilog">
+
<syntaxhighlight lang="Verilog">
  
 
     output [4:0] out,
 
     output [4:0] out,
Linia 134: Linia 134:
 
is equivalent to
 
is equivalent to
  
<syntaxhighlight lang = "Verilog">
+
<syntaxhighlight lang="Verilog">
  
 
     output wire [4:0] out,
 
     output wire [4:0] out,
Linia 140: Linia 140:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
==== Blocks '' 'assign' '' ====
+
==== Blocks '''assign''' ====
  
 
[[File: Adder.png | thumb | Adder module implemented]]
 
[[File: Adder.png | thumb | Adder module implemented]]
  
Assign '' 'is a key word that generates [[combinational circuits]]. As the summator is a [[combinational circuits | combinational circuit]], and output is implicitly wire type, we can implement it with a block '' 'assign' '':
+
Assign '''is a key word that generates [[combinational circuits]]. As the summator is a [[combinational circuits | combinational circuit]], and output is implicitly wire type, we can implement it with a block '''assign''':
<syntaxhighlight lang = "Verilog">
+
<syntaxhighlight lang="Verilog">
 
module Adder (
 
module Adder (
 
     output [4: 0] out,
 
     output [4: 0] out,
Linia 152: Linia 152:
 
)
 
)
  
assign out = in0 + in1;
+
assign out=in0 + in1;
  
 
endmodule
 
endmodule
</ Syntaxhighlight>
+
<syntaxhighlight>
  
'' '' 'Note:' '' '' Generally, a block '' 'assign' '' will generate a synthesizable circuit. There are also exceptions when the desired operation is too complex to be implemented through a combinational circuit effectively. E.g:
+
'''' 'Note:''''' Generally, a block '''assign''' will generate a synthesizable circuit. There are also exceptions when the desired operation is too complex to be implemented through a combinational circuit effectively. E.g:
  
<syntaxhighlight lang = "Verilog">
+
<syntaxhighlight lang="Verilog">
assign out = in0 / in1;
+
assign out=in0 /in1;
</ Syntaxhighlight>
+
<syntaxhighlight>
  
 
is not a synthesizable code for most synthesis tools, but works fine in a simulation.
 
is not a synthesizable code for most synthesis tools, but works fine in a simulation.
  
==== Combined '' always' '' blocks ====
+
==== Combined '' always''' blocks ====
  
Always, a block '' 'always' '' is used to give values ​​of '' 'reg' '' signals. A block '' 'always' '' may translate into [combinational circuits]] or [[sequential circuits]], depending on its list of sensitivities. The general format for a '' 'always' '' block is the following:
+
Always, a block '''always''' is used to give values ​​of '''reg''' signals. A block '''always''' may translate into [combinational circuits]] or [[sequential circuits]], depending on its list of sensitivities. The general format for a '''always''' block is the following:
  
<syntaxhighlight lang = "Verilog">
+
<syntaxhighlight lang="Verilog">
  
 
always @ (<sensitivity list>) begin
 
always @ (<sensitivity list>) begin
     // ...
+
     //...
 
end
 
end
  
</ Syntaxhighlight>
+
<syntaxhighlight>
  
As the name calls it, the list of sensitivities is the list of sensitive signals, that is, the registers described by the block '' 'always' '' depend on. If only the name of a signal is passed in the list of sensitivities without any additional specifiers, then the block is sensitive to any change of this signal. If a block '' 'always' '' has more signals to which it is sensitive, they split into the sensitivity list using the keyword '' 'or' ''.
+
As the name calls it, the list of sensitivities is the list of sensitive signals, that is, the registers described by the block '''always''' depend on. If only the name of a signal is passed in the list of sensitivities without any additional specifiers, then the block is sensitive to any change of this signal. If a block '''always''' has more signals to which it is sensitive, they split into the sensitivity list using the keyword '''or'''.
  
'' '' 'Note:' '' '' If the list of sensitivities contains only signals without other specifiers, then the result of the block synthesis will be a combinational circuit.
+
'''' 'Note:''''' If the list of sensitivities contains only signals without other specifiers, then the result of the block synthesis will be a combinational circuit.
  
In this case, we can redo the implementation of the summator using a block '' 'always' '' as follows:
+
In this case, we can redo the implementation of the summator using a block '''always''' as follows:
  
<syntaxhighlight lang = "Verilog">
+
<syntaxhighlight lang="Verilog">
 
module Adder (
 
module Adder (
 
     output reg [4: 0] out,
 
     output reg [4: 0] out,
Linia 191: Linia 191:
  
 
always @ (in0 or in1) begin
 
always @ (in0 or in1) begin
     out = in0 + in1;
+
     out=in0 + in1;
 
end
 
end
  
 
endmodule
 
endmodule
</ Syntaxhighlight>
+
<syntaxhighlight>
  
==== Blocks '' 'always' '' sequential. Non-blocking assignments ====
+
==== Blocks '''always''' sequential. Non-blocking assignments ====
  
[[Sequential Circuits | Sequential Circuits]] are circuits that are synchronized by [Sequence Circuits # Clock Signal | Clock Signal]. This signal is usually produced by a clock generator and is defined as input for each sequential module (in which there is at least one register). We can modify the previous example so that the output of the summation module is synchronous (that is, change only on the positive clock front). Thus, in
+
[[Sequential Circuits | Sequential Circuits]] are circuits that are synchronized by [Sequence Circuits #Clock Signal | Clock Signal]. This signal is usually produced by a clock generator and is defined as input for each sequential module (in which there is at least one register). We can modify the previous example so that the output of the summation module is synchronous (that is, change only on the positive clock front). Thus, in

Versiunea de la data 11 aprilie 2018 23:37

Modules (synthesizable)

The Verilog language is structured on modules. Each module represents a circuit that implements a certain function. For example, a module may be a summation, ie a circuit that has two entries specifying the two operands and an output representing the result of the assembly. The content of the module is the (structural or behavioral) description of the gates that computes the sum of the two entries. Therefore, the definition of a Verilog module has two parts:

  • interface - the list of all the input and output ports of the circuit, specified by name and size;
  • implementation - actual circuit description using input values to calculate output values;

Verilog Module Interface

The Adder module interface is shown below. ' Note: ' As in the decimal system, where the sum of two numbers n digits needs n + 1 digits (9 + 9=18), and in the binary system, the sum of two n bits will be on n + 1 bits.

Representation of the Adder module (black box)
module Adder (
    output [4: 0] out,
    input [3: 0] in0,
    input [3: 0] in1
)

//implementation

endmodule

The keywords module and endmodule are used to start and end defining a module. Immediately after the keyword module follows the module name.

Convention: The name of a module will begin with a large letter.

After defining the module name, the list of ports, placed between round brackets and separated by comma follows. Accepted keywords are output (representing an output port), input (representing an input port) and inout(representing a bidirectional port).

Tip: . They introduce Tri-state Buffer elements that are ineffective. A more efficient alternative is to define two ports, one input and one output, with similar names (ex: data_in and data_out ).

Convention: First the outputs, then the inputs of a module are defined.

Convention: module .

According to the port type, it follows its size, specified in the bit indexes, where the least significant bit has the index 0. For example, a 4 bit signal will have the following specification: [3:0] significantly has index 3, least significant 0, total 4 bits). Note: One-bit signals lack the size specification:

input signal_de_un_bit,

After the list of ports in brackets, the interface definition ends with the ; character.

<! -

  1. Remark: There are non-synthesizable modules (which do not have logical gaps), which are used as test programs in simulation environments and are called test modules. These modules have no input and output ports and are used only to give the input port values of the module to be tested, and to check the values on the output ports of the module. For these modules, the list of ports and their associated parentheses are missing:
Module TestModule;

//implementation

endmodule

->

Implementing Verilog Modules

The implementation of Verilog modules is done through blocks. These blocks may or may not correspond to a physical scent. If all blocks of a module have a correspondent in a physical circuit, then the module is synthesizable and can be transformed into a physical circuit. Blocks that can generate synthesizable constructions are of four types:

  • blocks assign
  • blocks always
  • Instance blocks
  • blocks generated

In addition, Verilog can define threads (wire) and registers (reg).

Blocks that are always unintelligible and are used exclusively for simulation:

  • blocks initial

' Remark: ' Not any block assign or always is synthesizable. There are syntactic constructs that do not have a correspondent in the circuit. These blocks can be simulated but can not be used to program an FPGA board.

Note: The order of blocks in a module does not matter.

Wires (wire) and registers (reg)

The threads are used for linking modules and for assigning partial results to combinational circuits, therefore:

Rule: or as the output of a module (never both simultaneously).

The threads in a Verilog module are defined as follows:

wire [3:0] wire;

Registers are commonly used to implement sequential stumps, and then they define physical registers, but:

Remark: does not necessarily translate into a physical register. Translating it depends on how it is used.

A register is defined as follows:

reg [3:0] register;

Rule: or initial.

Rule: No element can change its value in more than one block. That is, for a wire ( wire ), there can not be two blocks assign' in which it takes values, and for the elements reg'always or initial in which it changes its value.

Note: An entry of a module is always wire. Thus, the statement

    input [3:0] in0,

is equivalent to

    input wire [3:0] in0,


Remark: An output of a module may be wire or reg. If not specified, it is a wiretype. Thus, the statement

    output [4:0] out,

is equivalent to

    output wire [4:0] out,

Blocks assign

Adder module implemented

Assign is a key word that generates combinational circuits. As the summator is a combinational circuit, and output is implicitly wire type, we can implement it with a block assign: <syntaxhighlight lang="Verilog"> module Adder (

   output [4: 0] out,
   input [3: 0] in0,
   input [3: 0] in1

)

assign out=in0 + in1;

endmodule <syntaxhighlight>

' 'Note: Generally, a block assign will generate a synthesizable circuit. There are also exceptions when the desired operation is too complex to be implemented through a combinational circuit effectively. E.g:

<syntaxhighlight lang="Verilog"> assign out=in0 /in1; <syntaxhighlight>

is not a synthesizable code for most synthesis tools, but works fine in a simulation.

Combined always' blocks

Always, a block always is used to give values ​​of reg signals. A block always may translate into [combinational circuits]] or sequential circuits, depending on its list of sensitivities. The general format for a always block is the following:

<syntaxhighlight lang="Verilog">

always @ (<sensitivity list>) begin

   //...

end

<syntaxhighlight>

As the name calls it, the list of sensitivities is the list of sensitive signals, that is, the registers described by the block always depend on. If only the name of a signal is passed in the list of sensitivities without any additional specifiers, then the block is sensitive to any change of this signal. If a block always has more signals to which it is sensitive, they split into the sensitivity list using the keyword or.

' 'Note: If the list of sensitivities contains only signals without other specifiers, then the result of the block synthesis will be a combinational circuit.

In this case, we can redo the implementation of the summator using a block always as follows:

<syntaxhighlight lang="Verilog"> module Adder (

   output reg [4: 0] out,
   input [3: 0] in0,
   input [3: 0] in1

)

always @ (in0 or in1) begin

   out=in0 + in1;

end

endmodule <syntaxhighlight>

Blocks always sequential. Non-blocking assignments

Sequential Circuits are circuits that are synchronized by [Sequence Circuits #Clock Signal | Clock Signal]. This signal is usually produced by a clock generator and is defined as input for each sequential module (in which there is at least one register). We can modify the previous example so that the output of the summation module is synchronous (that is, change only on the positive clock front). Thus, in