Professional Documents
Culture Documents
Verilog RTL Coding Style: Hsi-Pin Ma
Verilog RTL Coding Style: Hsi-Pin Ma
Hsi-Pin Ma
http://larc.ee.nthu.edu.tw/~hp/EE4292/ Department of Electrical Engineering National Tsing Hua University
Acknowledgement
Thanks for Verilog slides from Prof. Chih-Tsun Huang.
Hsi-Pin Ma
References
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 Design Compiler User Guide), SOLD.
Hsi-Pin Ma
Hsi-Pin Ma
Module Partition
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
Hsi-Pin Ma
Subblock 1
Subblock 2
Block A
Hsi-Pin Ma 6
Design Compiler must preserve port definitions. Logic optimization cannot cross block boundaries Path from Reg A to Reg C may be larger and slower than necessary.
Hsi-Pin Ma
Better
Hsi-Pin Ma
Best
8
Speed Optimization
Reg A
Module A
Area Optimization
Reg B
Module B
Hsi-Pin Ma
BAD
Hsi-Pin Ma
Good
10
Adding Structure
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
Hsi-Pin Ma
11
Hsi-Pin Ma
13
Correct
BUFGS CLK_1 (.I(CLOCK_IN), .O(CLOCK_OUT));
Hsi-Pin Ma
14
15
Hsi-Pin Ma
16
Hsi-Pin Ma
17
Specifying Constants
Use constants in your design to substitute numbers to more meaningful names. The use of constants helps make a design more readable and portable.
//Using parameters for OPCODE functions parameter ZERO = 2b00; parameter A_AND_B = 2b01; parameter A_OR_B = 2b10; parameter ONE = 2b11; always @ (OPCODE or A or B) begin if (OPCODE == ZERO) OP_OUT = 1b0; else if (OPCODE == A_AND_B) OP_OUT=A&B; else if (OPCODE == A_OR_B) OP_OUT = A|B; else OP_OUT = 1b1; end // Using a parameter for Bus Size parameter BUS_SIZE = 8; output [BUS_SIZE-1:0] OUT; input [BUS_SIZE-1:0] X,Y;
Hsi-Pin Ma
18
Hsi-Pin Ma
19
Use Comments
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.
Hsi-Pin Ma
20
Hsi-Pin Ma
21
Indentation guidelines
Use indentation of 2 spaces. Larger indentation restricts line length when there are several levels of nesting. Avoid of using tabs. Differences in editors and user setups make the positioning of tabs unpredictable and corrupt the intended indentation.
Hsi-Pin Ma
22
Port Ordering
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 the same line) For each instance, declare the ports in the following order
Inputs
Clocks Resets Enables Other control signals Data and address lines Clocks Resets Enables Other control signals Data
Outputs
Ignore by synthesizer
Hsi-Pin Ma
24
Case statement
Balanced logic For complex decoding
Hsi-Pin Ma
25
Limiting the number of input signals into an if statement 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 if statement.
Default value
Hsi-Pin Ma 27
COMB
COMB
COMB
REG A
REG B
COMB
COMB
COMB
Hsi-Pin Ma
28
Hsi-Pin Ma
29
Avoid clock buffers in synthesis Avoid gated clock Avoid internally generated clocks
Hsi-Pin Ma 30
Hsi-Pin Ma
31
Non-Synthesizable Style
Either non-synthesizable or incorrect after synthesis initial block is forbidden (non-synthesizable) Multiple assignments (multiple driving sources) (non-synthesizable) always @(src1 or src2)
result = src1 + src2; always @(inc1 or inc2 or offset) result = inc1 + inc2 + offset;
Hsi-Pin Ma
32
33
Reset
Asynchronous reset
Entering the reset state asynchronously, but leaving synchronous
always @(posedge clk or negedge rst) if (~rst) q <= 0; else q<= d;
Synchronous reset
Similar to an enable signal entering and leaving the reset state synchronously
always @(posedge clk) if (~rst) q <= 0; else q<= d;
Hsi-Pin Ma 34
Hsi-Pin Ma
35