02_CodingStyle
-
Upload
mohsin-muzawar -
Category
Documents
-
view
214 -
download
0
Transcript of 02_CodingStyle
-
8/3/2019 02_CodingStyle
1/36
Hsi-Pin Ma
http://larc.ee.nthu.edu.tw/~hp/EE4292/
Department of Electrical EngineeringNational Tsing Hua University
-
8/3/2019 02_CodingStyle
2/36
Hsi-Pin Ma
Thanks for Verilog slides from Prof. Chih-TsunHuang.
2
-
8/3/2019 02_CodingStyle
3/36
Hsi-Pin Ma
Michael Keating and Pierre Bricaud,Reuse Methodology Manual,Kluwer Academic Publishers, Third Ed., 2002
M. Morris Mano,Digital Design, 3rd edition, 2002.
Guide to HDL Coding Styles for Synthesis, SOLD.
Preparing Design Files for Synthesis(Chapter 3 in DesignCompiler User Guide), SOLD.
3
-
8/3/2019 02_CodingStyle
4/36
Hsi-Pin Ma
Simple and regular
Use simple constructs and simple clocking schemes
Consistent coding style, consistent naming and state
machines Regular partitioning scheme
Easy to understand by comments and meaningfulnames. No hard coded number
4
-
8/3/2019 02_CodingStyle
5/36
Hsi-Pin Ma
Focus
Keep critical path within a module
All module outputs are registered
Module Well-selected the gate count number within a module
(250 - 5000)
Prepare to reuse hierarchical modules
5
-
8/3/2019 02_CodingStyle
6/36
Hsi-Pin Ma 6
Guideline
For each subblock of a hierarchical macro design,register all output signals from the subblock.
Subblock 1Subblock 2
Block A
-
8/3/2019 02_CodingStyle
7/36
Hsi-Pin Ma 7
Guideline
Keep related combinational logic together in the samemodule
Design Compiler must preserve port definitions.
Logic optimization cannot cross block boundaries
Path from Reg A to Reg C may be larger andslower than necessary.
-
8/3/2019 02_CodingStyle
8/36
Hsi-Pin Ma 8
Related combinational logic is grouped into oneblock.
Sequential optimization may absorb some of thecombinational logic into a more complex flip-flop.(Best) (JK, T, Muxed, Clock-enabled,)
Better Best
-
8/3/2019 02_CodingStyle
9/36
Hsi-Pin Ma 9
Module A
Critical Path
LogicRegA
Module B
Non-critical
Path LogicRegB
Speed Optimization
Area Optimization
-
8/3/2019 02_CodingStyle
10/36
-
8/3/2019 02_CodingStyle
11/36
Hsi-Pin Ma
Focus
Explicit specify the separate assignment
Make use of the parentheses
Modeling Build the design as your knowledge
Dont rely on CAD tools
Separated combinational and sequential logic blocks
11
-
8/3/2019 02_CodingStyle
12/36
Hsi-Pin Ma 12
Using general naming conventions consistently
Document the naming convention
Use lowercase letters for all signal names, variablenames, and port names.
Use uppercase letters for names of constants and user-defined types.
Use meaningful names for signals, ports, andparameters.
Do not use ra for a RAM address bus, but use ram_addr.
Use a consistent name for the clock signal, such as clk. If there is more than one clock, use clkas the prefix for all
clock signals. (clk1, clk2, clk_interface).
-
8/3/2019 02_CodingStyle
13/36
Hsi-Pin Ma
Using general naming conventions consistently
Use the same name for all clock signals that are drivenfrom the same source.
For active-low signals, end the signal with an
underscore followed by a lowercase character (_b, _n)
Use a consistent name for reset signals, such as rst. Ifthe reset signal is active-low, use a name like, rst_n.
When describing multibit buses, use a consistent
ordering bits. Use [x:0] for Verilog, (x downto 0) forVHDL.
When possible, use the same name or similar namesfor ports and signals that are connected.
13
-
8/3/2019 02_CodingStyle
14/36
Hsi-Pin Ma 14
Using Named and Positional Association
Incorrect
BUFGS CLK_1 (.I(CLOCK_IN), CLOCK_OUT);
Correct BUFGS CLK_1 (.I(CLOCK_IN), .O(CLOCK_OUT));
-
8/3/2019 02_CodingStyle
15/36
Hsi-Pin Ma 15
Port maps and generic maps Rule
Always use explicit mapping for ports and generics, using namedassociation rather than positional association.
DW_ram_r_w_s_dff
#((`ram_data_width+`ram_be_data_width),
(`fifo_depth),1)U_int_txf_ram (
.clk (refclk),
.rst_n (txfifo_ram_reset_n),
.cs_n (1b0),
.wr_n (txfifo_wr_en_n),
.rd_addr (txfifo_rd_addr),
.wd_addr (txfifo_wr_addr),
.data_in(txfifo_wr_data),
.data_out (txf_ram_data_out)
)
-
8/3/2019 02_CodingStyle
16/36
Hsi-Pin Ma 16
Guideline
Do not use hard-codednumeric values.
Advantages using constants
Constants are more intelligibleas they associate a designintension with the value.
Constant values can bechanged in one place.
Compilers can spot typos inconstants but not in hard-
coded values.
wire [7:0] my_in_bus;
reg [7:0] my_out_bus;
`define MY_BUS_SIZE 8
wire [`MY_BUS_SIZE-1:0] my_in_bus;
reg [`MY_BUS_SIZE-1:0] my_out_bus;
Poor coding style
Recommended coding style
-
8/3/2019 02_CodingStyle
17/36
Hsi-Pin Ma 17
Guideline
Keep constant and parameters definitions in one or asmall number of files with names such as
DesignName_constants.v orDesignName_parameters.v
-
8/3/2019 02_CodingStyle
18/36
Hsi-Pin Ma 18
Use constants in your design to substitute numbers tomore meaningful names.
The use of constants helps make a design more readableand portable.
-
8/3/2019 02_CodingStyle
19/36
Hsi-Pin Ma 19
Rule
Include a commented, informational header at the top of
every source file, including scripts.
Legal statement: confidentiality, copyright, restrictions on
reproduction
Filename
Author
Description of function and list of key features of the module.
Date the file was created
Modification history, including date, name of modifier, and descriptionof the change
-
8/3/2019 02_CodingStyle
20/36
Hsi-Pin Ma 20
Rule
Use comments appropriately to explain processes,functions, and declarations of types and subtypes.
Guideline Use comments to explain ports, signals, and variables,
or group of signals or variables.
-
8/3/2019 02_CodingStyle
21/36
Hsi-Pin Ma 21
Rule
Use a separate line for each HDL statement.
More readable and maintainable.
-
8/3/2019 02_CodingStyle
22/36
Hsi-Pin Ma 22
Line length guideline Keep the line length to 72 characters or less.
Indentation guidelines
Use indentation of 2 spaces. Larger indentationrestricts line length when there are several levels ofnesting.
Avoid of using tabs. Differences in editors and usersetups make the positioning of tabs unpredictable and
corrupt the intended indentation.
-
8/3/2019 02_CodingStyle
23/36
Hsi-Pin Ma 23
Rule Declare ports in a logical order, and keeps this order consistent throughout
the design
Guidelines
Declare one port per line, with a comment following it (preferably on thesame line)
For each instance, declare the ports in the following order
Inputs Clocks
Resets
Enables
Other control signals
Data and address lines
Outputs Clocks
Resets Enables
Other control signals
Data
Use comments to describe groups of ports.
-
8/3/2019 02_CodingStyle
24/36
Hsi-Pin Ma 24
Omit the Wait for XX ns Statement
Do not use #XX;
Omit the ...After XX ns or Delay Statement
Do not use assign #XX Q=0; Omit Initial Values
Do not use initial sum = 1b0;
Order and Group Arithmetic Functions
ADD = A1 + A2 + A3 + A4; ADD = (A1 + A2) + (A3 + A4); Not equivalent!
Ignore by synthesizer
-
8/3/2019 02_CodingStyle
25/36
Hsi-Pin Ma 25
If statement vs. Case statement
If statement
Priority-encoded logic
For speed critical path Case statement
Balanced logic
For complex decoding
-
8/3/2019 02_CodingStyle
26/36
Hsi-Pin Ma 26
Consideration when writing If statement Make sure that all outputs are defined in all branches
of an if statement. If not, it can create latches or long equations on the CE signal.
A good way to prevent this is to have default values for alloutputs before the if statements.
Limiting the number of input signals into an ifstatement can reduce the number of logic levels. If there are a large number of input signals, see if some of
them can be pre-decoded and registered before the ifstatement.
Avoid bringing the dataflow into a complex ifstatement. Only control signals should be generated in complex if-else
statements.
-
8/3/2019 02_CodingStyle
27/36
Hsi-Pin Ma 27
Default value
-
8/3/2019 02_CodingStyle
28/36
Hsi-Pin Ma 28
REG BREG A
COMB
COMB COMB
REG BREG A
COMB
COMB
COMB
Bad : Combinational processes are looped.
Good : Combinational processes are not looped.
-
8/3/2019 02_CodingStyle
29/36
Hsi-Pin Ma 29
Rule
Include a complete sensitivity list in each of youralways blocks.
always @(a)
c
-
8/3/2019 02_CodingStyle
30/36
Hsi-Pin Ma
Avoid using both positive- and negative-edgeclocks
Post problem for timing analysis
Problems for scan Separate pos and neg FFs in different modules
Avoid clock buffers in synthesis
Avoid gated clock
Avoid internally generated clocks
30
-
8/3/2019 02_CodingStyle
31/36
Hsi-Pin Ma
Syntax error for Verilog Simulator
Mixed edge-triggered and level-sensitivecontrol in an always block
31
-
8/3/2019 02_CodingStyle
32/36
Hsi-Pin Ma
Either non-synthesizable or incorrect after synthesis initial block is forbidden (non-synthesizable)
Multiple assignments (multiple driving sources)
(non-synthesizable)
Mixed blocking and non-blocking assignment
32
-
8/3/2019 02_CodingStyle
33/36
Hsi-Pin Ma
Explicit FSM design
33
-
8/3/2019 02_CodingStyle
34/36
Hsi-Pin Ma
Asynchronous reset Entering the reset state asynchronously, but leaving
synchronous
Synchronous reset
Similar to an enable signal
entering and leaving the reset state synchronously
34
-
8/3/2019 02_CodingStyle
35/36
Hsi-Pin Ma 35
-
8/3/2019 02_CodingStyle
36/36
Hsi-Pin Ma
No initial in the RTL code
FFs are preferred
Avoid unnecessary latches
Avoid combinational feedback
For sequential blocks, use no-blocking statements
For combinational blocks, use blocking statements
Coding state machines
Two procedure blocks: one for the sequential and one for the
combinational Keep FSM logic and non-FSM logic in separate modules
Assign a default state
36