Tips To Optimize Your Verilog HDL Code
Tips To Optimize Your Verilog HDL Code
http://www.inno-logic.com/resourcesTips.html
Search
Resources
RTL Coding Techniques General Reusable Coding Practices Guidelines for Multi-Clock Design Common Mistakes Made in RTL Code Design for Area Design for Static Timing Design for Testability Design for Low Power Design for Verification
1 of 9
6/12/2011 9:04 AM
http://www.inno-logic.com/resourcesTips.html
Multiple non-blocking assignments in procedural block: If there are multiple non-blocking assignments inside always block targeting the same register then the last assignment is synthesized. Using non-blocking assignments inside always block: In non-blocking assignments, all registers are updated at the end of the block. In blocking assignments, the registers are updated immediately. Non-blocking assignment example: Synthesize three D-Flip-Flop always @(posedge clk) begin reg1 <= in0; reg2 <= reg1; reg3 <= reg2; end Blocking assignment example: Synthesize One D-Flip-Flop always @(posedge clk) begin reg1 = in0; reg2 = reg1; reg3 = reg2; end Functions: Functions are primarily used for combinatorial logic since they do not have timing construct. If the call to the function are used in different paths then the logic get replicated. The returned value of the function can be synthesized into D-Flip-Flop if it is declared as reg while inferring the function reg [2:0] out1 ; always @(posedge clk) begin Out1 <= logic_func (in1, in2, in3); end
Tasks: You can use timing constructs such as @ inside tasks but only simulation tools
2 of 9
6/12/2011 9:04 AM
http://www.inno-logic.com/resourcesTips.html
support it. The synthesis tool will ignore all the timing constructs inside the task. So, it is advisable not to use timing construct inside the task and use it for the combinatorial logic. If a task is used for common paths then the logic is reused otherwise logic is replicated for different paths.
Sequential logic: The sequential logic is divided into two parts: latch and flip-flops. Latch is level sensitive and flip-flop is edge sensitive. Typically, incomplete multiplexer logic end up synthesizing into a latch. The following are the two examples of D Flip-flop and D-latch: D-latch: always @(d, clk) begin IF (clk == 1b1) q <= d; end In verilog-2001, the above code can be implemented as: always @(*) begin IF (clk == 1b1) q <= d; end
D-Flip-flop: always @(posedge clk) begin q <= d; end Pros and cons of latch and Flip-Flop: Latch takes less area, consume less power, facilitate time borrowing or cycle stealing, not friendly with DFT tools Flip-flop takes more area, consumes more power, allow synchronous logic, friendly with DFT tools
If-else and case: If-else statement generally synthesize priority encoding logic. Although system verilog allow you to control priority encoder logic.
3 of 9
6/12/2011 9:04 AM
http://www.inno-logic.com/resourcesTips.html
Example: unique if (in1) sel = 2b01; Else if (in2) sel = 2b11; If you use priority construct, instead of unique in system verilog then you again get priority encoder synthesized
Case statement is easy to read and can be synthesized into parallel or priority encoder logic. It is always a good practice to specify all the conditions in case statement or use default statement at the end.
Difference between == and === operators: The == are synthesizable while === operators are not synthesizable. If either of the operand in == has x or z the the result is always x while === compare x and z too. The same is true for != and !== operators. Mealy and Moore state machine: In Mealy machine, output depends upon the input as well as the current state. In Moore machine, output only depends upon the current state. Both are frequently used by the designers depending upon the need. Binary, one-hot and gray encoding: Binary encoding require fewer flip flops and usually multiple transitions at the same time. In one-hot encoding require the same number of flip-flops as the number of states. Only one bit change at a time and rest all are zero. In gray coding, only one bit change at a time, but rest of the bits can be one or zero. Gray coding is popularly used when interfacing between two different clock domains. One more the example is that dual clock FIFO uses gray coding to avoid any mismatch between the post-layout simulation and pre-layout simulation
4 of 9
6/12/2011 9:04 AM
http://www.inno-logic.com/resourcesTips.html
domain, it is always a good idea to synchronize it using two Flip-Flops. By having two Flip-flops, the second flip-flop always make sure to capture the stable data. Some vendors provide this module as 2-stage synchronizer. Data transfer across two different clock domains: Use dual clock FIFO to transfer the data from one clock domain to another domain. Make sure the dual clock FIFO uses gray-coding counter. If the width of the data on both sides is not same then use asymmetrical FIFO. For example, using asymmetrical FIFO, you can transfer data from 32-bit wide bus of one one clock domain to 8-bit wide bus of another clock domain. If the data speed is very
5 of 9
6/12/2011 9:04 AM
http://www.inno-logic.com/resourcesTips.html
.. endif Constant Propagation: The synthesis tools optimize the logic when constant value is propagating to the output. Using generate statement: Verilog 2001 generate statement allow to either instantiating multiple modules without typing them so many times or instantiating modules conditionally. You can use if-else to conditionally instantiate the modules. Also, if you want to instantiate the same module multiple times then better use for loop. This will save you lot of time. generate for (i=0; i < width; I = i+1) begin and_or inst1 (out1[i], in1[i], in2[i]); end endgenerate
6 of 9
6/12/2011 9:04 AM
http://www.inno-logic.com/resourcesTips.html
Internally generated clock and reset Reset derived by output of another flip-flop Presence of latches Avoid tri-state bus: The synthesis tools do not like tri-state buses and there are not testable. If you have to use tri-sate buses then to ensure testability, pass the enable of the tri-state bus through AND gate so that scan_enable signal can control the tri-state bus. Derived Reset: To avoid losing any data in scan mode, add OR gate before the signal reach the reset of next Flip-flip. Add scan_enable signal to the other input of the OR gate. In test mode, asserting sacn_enable make sure that the asynchronous reset is disabled. Derived clock: Use multiplexer logic at the output of the derived clock before it fed to the input of anoth flip-flip. Make the other input of mux as the primary clock and the select line as scan_enable. This will make sure that the primary clock is used during testability. Gated clock: It is a very common practice for power sensitive designs. You basically disable the complete block by simply disabling the input clock by using AND gate. To ensure the design is testable, add OR gate after the AND gate and add scan_enable as another input to the OR gate in addition to the output to the AND gate. Presence of latches: Since enable signal to latch is not a regular clock that is fed to the rest of the logic. To ensure testability, you need to use OR gate using enable and scan_enable signals as input and fed the output to the enable port of the latch.
7 of 9
6/12/2011 9:04 AM
http://www.inno-logic.com/resourcesTips.html
Identify the level of message in terms of severity Controllability of the message Timestamp of the message Message Identification Different kind of severity levels INFO: Message is simply an information WARNING: Something unusual happen but no need to stop simulation ERROR: Indicates that something is wrong and you need to debug and find out the root cause and fix it FATAL: Displayed when there is a serious issue. Simulation will immediately terminate after displaying this message What is BFM? Provides visibility into its communication processes at each level of abstraction Visibility into all configurable parameters Commanded to perform specified sequence of commands Consideration for designing BFM Map the hierarchy of protocol or functionality into BFM Provide a built-in self check capability Specify all the key variables in one single file Ensure all configurable variables must have defaults Must provide a provision to filter the messages All the inputs to the tasks/commands within the BFM should be checked for legal ranges. Must provide a provision for message ID Typical flow for designing BFM Specify the abstraction level at which the BFM is planned to be used Specify user level configuration parameters Specify the hierarchy of commands when they are functionally dependent on hierarchical fashion Specify the commands that user will be able to call within the BFM Specify the details of the message that BFM should convey Specify the interface ports that the BFM use to interact with the DFT Make sure to display the value of parameters before the simulation starts Provide a provision to log the BFM messages into a file or standard output Provide a checking mechanism to check the legal range of all the parameters and input value Main functions of a bus monitor Protocol checking Violation of protocol
8 of 9
6/12/2011 9:04 AM
http://www.inno-logic.com/resourcesTips.html
Monitoring for X and Z values on signals Design latencies between critical signals Monitor timing violations such as setup and hold Transaction logging Log the message with severity level, ID, and description Provide the option to display them as standard output or in a file
Feedback
If you have any suggestion/feedback please email it to [email protected]
Home | About Us | Solutions | Resources | Contact Us 2008 Innovative Logic, Inc. All rights reserved.
9 of 9
6/12/2011 9:04 AM