Tuesday, December 9, 2025

port generation

 Question:

You have a SystemVerilog class that represents a network packet, with the following properties:

  • src_port: a 16-bit integer representing the source port number
  • dst_port: a 16-bit integer representing the destination port number
  • packet_size: a 32-bit integer representing the size of the packet in bytes

You want to write a constraint to ensure that:

  • The src_port and dst_port fields are within the range of 0 to 65535 (inclusive)
  • The src_port and dst_port fields cannot be equal to each other (i.e., they must be distinct)
  • The packet_size field must be within the range of 1 to 1500 (inclusive)
  • At least one of the src_port or dst_port fields must be an even number
  • If the packet_size is greater than 1000, then the src_port field must be greater than 1024

Write a SystemVerilog constraint that satisfies these conditions.

======================================================

/// src dst 

  

  class packet;

    rand int src_port;

    rand int dst_port;

    rand int packet_size;


    constraint src_dst_port_c {

        src_port inside {[0:65535]}; // 0 to 65535 inclusive

        dst_port inside {[0:65535]}; // 0 to 65535 inclusive

        src_port != dst_port; // src_port and dst_port must be distinct

    }


    constraint packet_size_c {

        packet_size inside {[1:1500]}; // 1 to 1500 inclusive

    }


    constraint even_port_c {

        (src_port % 2 == 0) || (dst_port % 2 == 0); // at least one of src_port or dst_port must be even

    }


    constraint large_packet_c {

        (packet_size > 1000) -> (src_port > 1024); // if packet_size > 1000, then src_port > 1024

    }


endclass



// Testbench module

module tb;

    // Create a packet instance

    packet p;


    initial begin

        // Initialize the random seed

        //$random_seed = 1;


        // Create a new packet instance

        p = new();


        // Repeat the randomization and printing process

        repeat (10) begin

            // Randomize the packet fields

            p.randomize();


          $display("src  = %d , dst = %d , pkt size = %d", p.src_port , p.dst_port , p.packet_size );


            // Wait for 1ns before generating the next packet

            #1ns;

        end


        // Wait for 1ns before finishing the simulation

        #1ns;


        $finish;

    end

endmodule


packet generation question

 Q -> 

You have a SystemVerilog class that represents a network packet, with the following properties:

  • addr: a 32-bit integer representing the destination address
  • data: a 16-byte array of bytes representing the packet payload
  • proto: a 2-bit enumeration representing the protocol type (e.g. TCP, UDP, ICMP, etc.)

You want to write a constraint to ensure that:

  • The addr field is randomly assigned a value between 192.168.0.1 and 192.168.0.100 (inclusive)
  • The data field is randomly assigned a value, but with the following conditions:
    • At least 4 bytes of the payload must be non-zero
    • No more than 8 bytes of the payload can be zero
  • The proto field is randomly assigned one of the following values: TCP, UDP, or ICMP

Write a SystemVerilog constraint that satisfies these conditions.

=================================================================

// Define the enum for protocol type

typedef enum {TCP, UDP, ICMP} proto_e;


// Define the packet class with constraints

class packet;

    rand int addr;

    rand byte data[16];

    rand proto_e proto;

    int non_zero_count ;

    int zero_count ;


    // Constraints for packet fields

    constraint addr_c {

        addr inside {[32'hC0A80001 : 32'hC0A86401]}; // 192.168.0.1 to 192.168.0.100

    }



      constraint data_c {

        foreach (data[i]) {

            data[i] dist {0 := 50, [1:255] := 50}; // Uniform distribution for non-zero and zero bytes

        }

    }

    constraint proto_c {

        proto dist {TCP := 1, UDP := 1, ICMP := 1}; // Uniform distribution

    }


    // Method to print packet information

    function void print();

        $display("Packet Information:");

        $display("  Address: %d", addr);

        $display("  Data: ");

        foreach (data[i]) begin

            $display("    byte[%0d] = %d", i, data[i]);

        end

        case (proto)

            TCP: $display("  Protocol: TCP");

            UDP: $display("  Protocol: UDP");

            ICMP: $display("  Protocol: ICMP");

        endcase

    endfunction


endclass


// Testbench module

module tb;

    // Create a packet instance

    packet p;


    initial begin

        // Initialize the random seed

        //$random_seed = 1;


        // Create a new packet instance

        p = new();


        // Repeat the randomization and printing process

        repeat (10) begin

            // Randomize the packet fields

            if (!p.randomize()) begin

                $error("Failed to randomize packet");

            end


            // Print the packet information

            p.print();


            // Wait for 1ns before generating the next packet

            #1ns;

        end


        // Wait for 1ns before finishing the simulation

        #1ns;


        $finish;

    end

endmodule


Tuesday, December 2, 2025

Write code that will create the four processes. Process_C should begin when Process_A has finished, and then Process_D. However, Process_B ought to wait for Process_D to finish.

 module tb; 

event process_A_done; // Declare events for synchronization 

event process_D_done; 

event process_C_done; 

initial 

begin 

fork: process_fork

begin: process_A 


$display("Process_A completed %0t",$time);

 -> process_A_done; // Trigger process_A_done event 

end 

begin: process_B 

wait(process_D_done.triggered); // Wait for process_B_done event 


 $display("Process_B completed %0t",$time); 

end 

begin: process_C 

wait(process_A_done.triggered);


$display("Process_C completed %0t",$time); 

-> process_C_done; 

end 

begin: process_D 

wait(process_C_done.triggered); 


$display("Process_D completed %0t",$time); 

->process_D_done; 

end 

join

end

endmodule


Key points - events , wait , -> , triggered , 

wait_order Waits for events to be triggered in the given order, and issues an error if any event executes out of order.