Verilog Cheat Sheet
Verilog Cheat Sheet
1. Lexical Elements
1.1 Number Representations
1 // Format : < size > ’ < base > < number >
2 8 ’ b10101111 // 8 - bit binary
3 8 ’ hAF // 8 - bit hexadecimal ( same as above )
4 8 ’ d175 // 8 - bit decimal ( same as above )
5 8 ’ o257 // 8 - bit octal ( same as above )
6 16 ’ hFACE // 16 - bit hexadecimal
7 ’ h116 // Unsized hexadecimal
8 4 ’ b1010 // 4 - bit binary
9 32 ’ h0000_00FF // Underscores for readability
Variable Types
1
2. Module Structure
2.1 Module Declaration
9 endmodule
10
2
13 counter my_counter ( clk , rst , count ) ;
14 defparam my_counter . WIDTH = 16;
15
4. Operators
4.1 Arithmetic Operators
1 + // Addition
2 - // Subtraction
3 * // Multiplication
4 / // Division
5 % // Modulus
6 ** // Exponentiation ( power )
1 ! // Logical NOT
2 && // Logical AND
3 || // Logical OR
1 ~ // Bitwise NOT
2 & // Bitwise AND
3 | // Bitwise OR
4 ^ // Bitwise XOR
5 ^~ or ~^ // Bitwise XNOR
3
4.7 Shift Operators
1 // Concatenation
2 {A , B , C } // Join bits together
3 {4 ’ b1010 , 4 ’ b0101 } // Results in 8 ’ b10100101
4
5 // Replication
6 {4{1 ’ b1 }} // Replicates 1 ’ b1 four times : 4 ’ b1111
7 {3{2 ’ b10 }} // Replicates 2 ’ b10 three times : 6 ’ b101010
8 {2{ a , b }} // Same as {a , b , a , b }
5. Procedural Blocks
5.1 Initial Block
4
6. Assignments
6.1 Continuous Assignment
7. Conditional Statements
7.1 If-Else Statement
1 if ( condition ) begin
2 // Statements if condition is true
3 end
4 else if ( another_condition ) begin
5 // Statements if another_condition is true
6 end
7 else begin
8 // Statements if all conditions are false
9 end
1 case ( expression )
2 value1 : begin
3 // Statements for value1
4 end
5 value2 , value3 : begin
6 // Statements for value2 or value3
7 end
8 default : begin
9 // Default statements
10 end
11 endcase
12
5
21 casez ( expression )
22 4 ’ b1 ???: statements ; // ? represents don ’t care
23 4 ’b ?1??: statements ;
24 default : statements ;
25 endcasez
4 // Nested conditional
5 assign out = sel1 ? a : ( sel2 ? b : c ) ;
8. Loops
8.1 For Loop
1 forever begin
2 #10 clk = ~ clk ; // Toggle clock every 10 time units
3 end
6
10 // Task with direction specifiers ( Verilog -2001)
11 task automatic process_data (
12 input [7:0] data_in ,
13 output [7:0] data_out
14 );
15 #2 data_out = data_in + 8 ’ h5A ;
16 endtask
1 // Calling a task
2 process_data ( in_data , out_data ) ;
3
4 // Calling a function
5 result = add_offset ( in_data ) ;
7
10.2 Gate Delays
8
1 // Conditional generate
2 generate
3 if ( WIDTH == 8) begin : gen_8bit
4 adder_8bit adder_inst (a , b , sum ) ;
5 end
6 else begin : gen_generic
7 adder_generic #( WIDTH ) adder_inst (a , b , sum ) ;
8 end
9 endgenerate
10
19 // Case generate
20 generate
21 case ( MODE )
22 0: begin : gen_mode0
23 // Mode 0 logic
24 end
25 1: begin : gen_mode1
26 // Mode 1 logic
27 end
28 default : begin : gen_default
29 // Default mode logic
30 end
31 endcase
32 endgenerate
9
1 $finish ; // End simulation
2 $stop ; // Pause simulation
3
6 // Format specifiers
7 // % h - hex , % d - decimal , % b - binary , % o - octal
8 // % c - character , % s - string , % t - time , % m - hierarchical name
9
10 // File operations
11 $fopen ( " filename . txt " ) ; // Open file
12 $fclose ( file_handle ) ; // Close file
13 $fdisplay ( file_handle , " Text % d " , value ) ; // Display to file
14 $fwrite ( file_handle , " Text % d " , value ) ; // Write to file
15 $fstrobe ( file_handle , " Text % d " , value ) ; // Strobe to file
16 $fmonitor ( file_handle , " Text % d " , value ) ; // Monitor to file
15.2 Counter
10
5 else if ( enable )
6 count <= count + 1;
7 end
16 case ( state )
17 IDLE : begin
18 if ( start )
19 next_state = STATE1 ;
20 end
21 STATE1 : begin
22 output_reg = 1;
23 if ( condition )
24 next_state = STATE2 ;
25 end
26 STATE2 : begin
27 output_reg = 2;
28 if ( done )
29 next_state = IDLE ;
30 end
31 default : next_state = IDLE ;
32 endcase
33 end
11
5 always @ (*) begin
6 out = a & b | c ;
7 end
1 module testbench ;
2 // Declare signals
3 reg clk , rst , in ;
4 wire out ;
5
12
6 // Instantiate design under test ( DUT )
7 my_module dut (
8 . clk ( clk ) ,
9 . rst ( rst ) ,
10 . in ( in ) ,
11 . out ( out )
12 );
13
14 // Clock generation
15 initial begin
16 clk = 0;
17 forever #5 clk = ~ clk ;
18 end
19
20 // Test stimulus
21 initial begin
22 // Initialize
23 rst = 1;
24 in = 0;
25
26 // Release reset
27 #20 rst = 0;
28
29 // Apply stimulus
30 #10 in = 1;
31 #10 in = 0;
32
33 // Monitor results
34 $display ( " Time =% t , out =% b " , $time , out ) ;
35
36 // End simulation
37 #100 $finish ;
38 end
39 endmodule
6 // Check result
7 if ( out !== 1 ’ b1 ) begin
8 $display (" ERROR : Expected out =1 , got out =% b " , out ) ;
9 $finish ;
10 end
11 else begin
12 $display (" Test passed !") ;
13 end
14 end
4 // Write port
13
5 always @ ( posedge clk ) begin
6 if ( we )
7 mem [ wr_addr ] <= wr_data ;
8 end
9
10 // Read port
11 always @ ( posedge clk ) begin
12 if ( re )
13 rd_data <= mem [ rd_addr ];
14 end
14