Verilog Coding Style
Verilog Coding Style
ENG-85857
B Jane Smith
Reviewers
Reviewer ASIC Manager Name and Title Bob Parker, Mgr, Hardware Engineering
Modification History
Rev A B Date 10/30/00 11/2/00 Originator Jane Smith Jane Smith Comments Added test bench information on file name, reporting results Added document number and sign-off page
ENG-85857
B Jane Smith
Table of Contents
1 2 Overview........................................................................................................................................... 3 Alignment/Space .............................................................................................................................. 4
2.1 2.2 2.3 2.4 2.5 Tabs ............................................................................................................................................ 4 Text file width ............................................................................................................................. 4 White space around operators .................................................................................................... 4 Nested indentation levels ............................................................................................................. 4 Alignment ................................................................................................................................... 6 Modules/Variables ...................................................................................................................... 8 Special Case Variables................................................................................................................ 8
Clocks ................................................................................................................................................................................. 8 Flip-Flops & Latches ....................................................................................................................................................... 8 Using Signal Source in Name......................................................................................................................................... 9 Active Low signals ........................................................................................................................................................... 9 Active level globally defined........................................................................................................................................ 10 Resets ............................................................................................................................................................................... 10 Summary of prefixes & suffixes for naming signals ................................................................................................ 10
3.3
Signal widths ............................................................................................................................. 11 Module declaration................................................................................................................... 12 Module instantiations ............................................................................................................... 13 Sequential Logic ....................................................................................................................... 13 Combinatorial Logic ................................................................................................................. 14 Defines ...................................................................................................................................... 14 Assignment statements .............................................................................................................. 14 End of line comments ................................................................................................................ 15 File Name .................................................................................................................................. 17 Self-Checking Test Bench Results ............................................................................................. 17
Module Coding............................................................................................................................... 12
4.1 4.2 4.3 4.4 4.5 4.6
5 6
Comments....................................................................................................................................... 15
5.1 6.1 6.2
ENG-85857
B Jane Smith
1 Overview
In the course of projects, we are all constantly reviewing, maintaining, updating, or inheriting each others code. To make these painful tasks a bit easier, the following coding guidelines have been identified. These conventions also allow us to create tools to automate mundane parts of the coding process such as exploding module declarations or creating hierarchical modules. One of the most important issues in reading code is style. Given this, the following style guidelines should be adhered to religiously. There is a template for modules: $AIRO/asic/verilog/templates/module.v. The header by itself is in: $AIRO/asic/verilog/templates/header.v. All of the parts are labeled. Code should be entered into the appropriate section. Comment headers are to be left in the code, even if there is no code entered in that section. It makes it easier for someone reading it to know there are no assign statements (as an example) if the header exists but has no code.
ENG-85857
B Jane Smith
2 Alignment/Space
2.1 Tabs
All alignment related white space should be 4 position tab characters. All popular UNIX text editors can be made to use four position tabs instead of eight position tabs. The following UNIX command can be used to create an alias that will print Verilog using enscript.
alias printcode /usr/5bin/pr -t -e4 \!$ | enscript -b\!$
Correct Example:
if ((my_signal == 1b0) && (my_bus[3:0] == 4d5)) begin
ENG-85857
B Jane Smith
Correct Example:
if ( this ) begin for ( i == 0; i < 10; i = i + 1 ) begin statement1; statement2; end statement3; statement4; end else begin statement5; end
Case statements are a little more complex. The begin/end structure should always be used in a case definition, indentation levels should be used to offset the statements that are encapsulated, but the use of blank lines can be used or omitted to best show the statement groupings. Incorrect Example:
case ( myBus[3:0] ) 4b0000 : 4b0001 : my_signal1 = TRUE; my_signal1 = FALSE;
4b0010 : begin my_signal1 = TRUE; my_signal2 = FALSE; end 4b0100 : my_signal2 = FALSE; default : my_signal1 = TRUE; endcase
ENG-85857
B Jane Smith
Correct Example:
case ( myBus[3:0] ) 4b0000 : begin my_signal1 = TRUE; end 4b0001 : begin my_signal1 = FALSE; end 4b0010 : begin my_signal1 = TRUE; my_signal2 = FALSE; end 4b0100 : begin my_signal2 = FALSE; end default : begin my_signal1 = TRUE; end endcase
2.5 Alignment
Aligning code statements can seem like a menial task, but it significantly adds to the understanding of code. Alignment should be used in declarations, assignments, multi-line statements, and end of line comments. Incorrect Example:
reg[3:0] my_signal1; reg[31:0] myDecodedSignal1; reg[4:0] my_signal2, my_signal3, my_signal4; wire[2:0] mySelect;
Correct Example:
//---------------------------------------------------------// Signal Declarations: reg //---------------------------------------------------------reg [3:0] my_signal1; //description reg [31:0] my_decoded_signal1; //description reg [4:0] my_signal2, //description reg my_signal3, //description reg my_signal4; //description //---------------------------------------------------------// Signal Declarations: wire //---------------------------------------------------------wire [2:0] mySelect; //description
ENG-85857
B Jane Smith
Incorrect Example:
if ( myBoolean ) begin my_signal1 = TRUE; my_delayed_signal1 = !your_signal; end
Correct Example:
if ( myBoolean ) begin my_signal1 my_delayed_signal1 end
= TRUE; = !your_signal;
Some complex Boolean expressions make much more sense if they are expressed as multiline aligned statements. The unaligned example is... Incorrect Example:
if ( ( my_signal1 && your_signal1 ) || ( my_signal2 && your_signal2 ) ||( my_signal3 && your_signal3 ) ) begin
Correct Example:
if ( ( my_signal1 && your_signal1 ) || ( my_signal2 && your_signal2 ) || ( my_signal3 && your_signal3 ) ) begin
ENG-85857
B Jane Smith
3 Naming conventions
3.1 Modules/Variables
All modules and signal names will be lower case, delimited by underscores _. Correct Example:
module my_module ( ena_fft, ena_mdi, fft_in, mdi_out, my_signal1, my_signal2 );
All constant names should be upper case with underscore delimiters (MY_TRUE), with underscore delimiters every 4 characters (7b00X_10XZ).
Incorrect Example:
module FftBlock clk_Fft, clkFft, fftClk_drv, my_Signal_src
ENG-85857
B Jane Smith
There are no prefixes, only the four suffixes: _ff, _nxt, (normal ffs), _meta, and _sync. (for special synchronizing ffs) The output of a flip-flop is named: name_ff, the value the flipflop will be loaded with on the next clock is name_nxt. The signal name_ff becomes the Q output of the flip-flop while name_nxt is the D input. It helps readability of the combinatorial block to know that a signal is a flip-flop output or is to be loaded into a flip-flop. The flip-flop instance is named name_ff_reg by the synthesis tool. (Add figure to show flip-flops) The special suffixes, _meta, and _sync are used when two flip-flops are put next to each other to synchronize a signal to a new clock. The first is named reg_meta, and its output can be metastable, the second flip-flop has reg_meta as an input and reg_sync as an output. Latches use _lat for the output, and _nxt for the input. Correct Example:
always @ ( posedge clkFft_drv or `RESET_EDGE reset_x ) if ( reset_x = `RESET_ON ) begin my_signal0_ff my_signal1_ff my_signal1_dup_ff my_signal2_meta my_signal2_sync end else begin my_signal0_ff my_signal1_ff my_signal1_dup_ff my_signal2_meta my_signal2_sync end end <= <= <= <= <= my_signal0_nxt; my_signal1_nxt; my_signal1_nxt; async_input; my_signal2_meta;
ENG-85857
B Jane Smith
3.2.6 Resets
Resets are named special to allow for libraries with different senses (active high or active low). It is desirable to buffer the reset centrally rather than have it reset in every block. Otherwise, since the reset runs all over the chip, it could require a lot of buffering and therefore there could be a lot of skew on release of the reset from some flip-flops to others. This is controlled by buffering it in much the same way as a clock, in a centralized block. The problem is that different vendors have all of their flips-flops with either active high or active low reset (not both). We have had to use the same netlist for both FPGAs and ASICs and had to use different active levels for each. We handle this by naming the asynchronous reset reset_x and always comparing to a globally defined constant, RESET_ON. The active sense can then be changed for the whole design. Another define, called RESET_EDGE must be declared globally that is used in the sequential always block to indicate posedge or negedge. If an active low reset is used, then a global file for the ASIC will have the following:
`define RESET_EDGE `define RESET_ON `define RESET_EDGE `define RESET_ON negedge 1b0 posedge 1b1
ENG-85857
B Jane Smith
Configuration bit, written by the MAC and static during operation of the PHY Used only with clk prefix. Runs directly to flip-flop clock inputs only. Used only with clk prefix. It runs only to a clock fan-out tree whose output has suffix _drv. Output , Q, of a flip-flop Input, D, of a flip-flop Active low signal. Used when sense is library dependent and is defined globally for the design (as in resets or I/O pad enables)
my_signal = your_signal;.
Correct Example:
reg reg [3:0] [2:0] my_signal your_signal;
my_signal[2:0] = your_signal;
Along these lines, always completely specify the width of constants. For instance Incorrect Example:
reg [3:0] my_signal
This will work, but a more specific declaration would be Correct Example:
reg [3:0] my_signal
ENG-85857
B Jane Smith
4 Module Coding
Each module has sections for parameters, ports, signal declarations, assign statements, assign statements, instantiations, sequential logic, and combinatorial logic. They are organized as in the template file $AIRO/asic/verilog/templates/module.v. Each module will have separate sections for sequential and combinatorial logic. Sequential and combinatorial logic will not be combined in the same always block.
Correct Example:
module my_module ( //----------------------my_input1, my_input2 [2:0], my_input3, my_output1 my_output2, my_inOut ); [31:0],
The module input/output declaration is a simple cut and paste of the IO list in the module declaration. These declarations should also be one per line in alphabetical order. Notice the alignment in the following example. Make sure the parameter list is also alphabetical, especially since they can be re-declared in the instantiation by position only. Correct Example:
//----------------------// Parameters //----------------------parameter CONST1 = 1b0; parameter CONST2 = 3b010; //----------------------// Input Ports //----------------------input my_input1; input [2:0] my_input2; input my_input3; //----------------------// Output Ports
ENG-85857
B Jane Smith
//----------------------output [31:0] my_output1; output my_output2; //----------------------// Bidirectional Ports //----------------------inpout my_inout;
ENG-85857
B Jane Smith
4.5 Defines
Defines are not used in normal module coding. They may be used occasionally to set up some global variables in a top-level file. They should not be in a normal module.
Correct Example:
assign outputA = reg2_ff;
Incorrect Example:
Assign signal1 = signal2 ^ signal3;
ENG-85857
B Jane Smith
5 Comments
Liberal use of comments is strongly encouraged. Adding obvious comments is discouraged. Basically, extensive comments that proceed blocks of code, coupled with sparse back references, guides the reader through the code. Clever methods of implementation should ALWAYS be highlighted with comments, especially if the reader might question the abilities of synthesis tools. Incorrect Example:
// assert myReq1 my_req1 = TRUE; // wait for the acknowledge if ( my_ack ) begin { my_select[27:1], temp } = 1 << my_channel[4:0]; end
With a little different commenting strategy, more important information can be imparted to the reader. Correct Example:
// // // // // // // // // Request that my_module grant access to the RAM. When the acknowledge comes, decode the channel into access selects. The decoder is coded as a left shift operator with a dummy initial position. Believe it or not, this syntax actually synthesizes correctly.
my_req1 = TRUE; if ( my_ack ) begin // shift based decoder {my_select[27:1], temp} = 1 << my_channel[4:0]; end
ENG-85857
B Jane Smith
If the comments are aligned, they make much more sense (like reading a story). Correct Example:
my_signal1 = your_signal1; if ( your_signal2 && !your_signal3 ) begin my_signal2 = TRUE; end // Advance the signals, // check 2 and 3, // then assert request
ENG-85857
B Jane Smith
6 Test Benches
6.1 File Name
The file name for a test bench will be module_tb.v, where module matches the hdl top-level name to be tested.
This will make it easier to post-process files and put together summaries of testing. It will be helpful to add the module/test case to error messages.