System Verilog1
System Verilog1
AIM:
APPARATUS REQUIRED:
❖ Personal computer
❖ Modelsim Software
PROCEDURE:
⮚ Simulate the program after creating the test bench waveform and verify the functional design.
Steps to simulate.
1. Create a new project: Start by opening ModelSim and creating a new project. Click on the "File"
menu, then "New", and then select "Project". Give your project a name and select a working directory
for the project.
2. Add design files: Add your design files to the project by clicking on the "Add" button in the "Design"
tab of the "Project" window. Select your design files and click "OK".
3. Compile design: After adding your design files to the project, compile your design by clicking on the
"Compile" button in the "Design" tab of the "Project" window. This will check the syntax of your
design and generate a compiled version of the design.
4. Create a testbench: Create a testbench to verify the functionality of your design. To create a testbench,
click on the "File" menu, then "New", and select "Text Editor". Create a new file and write your
testbench code in this file.
5. Simulate the design: Once you have created your testbench, simulate your design by clicking on the
"Simulate" button in the "Design" tab of the "Project" window. This will open the Modelsim
simulation window.
6. Analyze simulation results: Analyze the simulation results to verify the functionality of your design.
You can view waveforms and other simulation data by using the various tools available in the
Modelsim simulation window.
7. Debug errors: If any errors or warnings are generated during the simulation, debug them by analyzing
the simulation results and modifying your design or testbench as necessary.
8. Save and export results: Once you are satisfied with the simulation results, save your work and export
any relevant results, such as waveforms or log files.
9. Close the project: Finally, close your project by clicking on the "File" menu and selecting "Close
Project". This will save your project and any changes you have made to it.
PROGRAM:
2:1 MUX
module mux_2x1_gate (
input a, b, sel,
output out
);
wire not_sel;
wire and1, and2;
wire or1;
endmodule
module tb_mux_2x1_gate;
initial begin
// Test case 1: sel = 0, a = 0, b = 0
sel = 0;
a = 0;
b = 0;
#10;
if (out !== 0) $display("Test case 1 failed");
endmodule
Program 2:
module mux_2to1(input logic a, b, select, output logic out);
always @(*) begin
if (select == 1'b0) begin
out = a;
end else begin
out = b;
end
end
endmodule
module tb_mux_2to1_gated_input;
// Inputs
logic a, b, select;
// Outputs
logic out;
// Stimulus
initial begin
// Test case 1: select input 0
a = 1'b1;
b = 1'b0;
select = 1'b0;
#10;
if (out !== 1'b1) $error("Test case 1 failed. Expected output to be 1.");
In the stimulus section, we define four test cases where we set the input values and simulate for some time
using #10. After the simulation, we check the output out against the expected value using $error and display a
message using $display.
Truth Table:
APPARATUS REQUIRED:
❖ Personal computer
PROCEDURE:
⮚ Simulate the program after creating the test bench waveform and verify the functional design.
Steps to simulate.
1. Create a new project: Start by opening ModelSim and creating a new project. Click on the "File"
menu, then "New", and then select "Project". Give your project a name and select a working directory
for the project.
2. Add design files: Add your design files to the project by clicking on the "Add" button in the "Design"
tab of the "Project" window. Select your design files and click "OK".
3. Compile design: After adding your design files to the project, compile your design by clicking on the
"Compile" button in the "Design" tab of the "Project" window. This will check the syntax of your
design and generate a compiled version of the design.
4. Create a testbench: Create a testbench to verify the functionality of your design. To create a testbench,
click on the "File" menu, then "New", and select "Text Editor". Create a new file and write your
testbench code in this file.
5. Simulate the design: Once you have created your testbench, simulate your design by clicking on the
"Simulate" button in the "Design" tab of the "Project" window. This will open the ModelSim
simulation window.
6. Analyze simulation results: Analyze the simulation results to verify the functionality of your design.
You can view waveforms and other simulation data by using the various tools available in the
ModelSim simulation window.
7. Debug errors: If any errors or warnings are generated during the simulation, debug them by analyzing
the simulation results and modifying your design or testbench as necessary.
8. Save and export results: Once you are satisfied with the simulation results, save your work and export
any relevant results, such as waveforms or log files.
9. Close the project: Finally, close your project by clicking on the "File" menu and selecting "Close
Project". This will save your project and any changes you have made to it.
PROGRAM 1:
endmodule
endmodule
// Inputs
input logic [7:0] DUT_output;
input logic [7:0] reference_output;
// Output
output logic score;
// Scoreboard logic
always_comb begin
if (DUT_output == reference_output) begin
score_register = score_register + 1;
end
else begin
score_register = score_register - 1;
end
end
endmodule
System verilog Test Bench:
Simulator Output:
Here, we have a scoreboard module that takes in the DUT output and the expected output, and compares
them using a comparator module. If there is a data mismatch, the scoreboard sets its own data_mismatch
output to 1.
The comparator module is a simple module that checks if two inputs are equal, and sets the output eq to 1 if
they are equal, otherwise to 0.
Program 2:
// Scoreboard module
module scoreboard();
logic [31:0] in_a_score;
logic [31:0] in_b_score;
logic [31:0] out_score;
// DUT module
module dut(
input logic [31:0] in_a,
input logic [31:0] in_b,
output logic [31:0] out
);
endmodule
// Testbench module
module testbench();
scoreboard sb_inst();
endmodule
Simulator Output:
Here, the scoreboard module monitors the inputs and output of the DUT and compares the output with the
expected result (the sum of the inputs). If the output does not match the expected result, an error message is
displayed. The dut module performs a simple addition operation on the inputs, and the testbench module
generates the input stimuli for the DUT.
RESULT:
AIM:
To implement and testing of Semaphore for a Simple DUT.
APPARATUS REQUIRED:
❖ Personal computer
⮚ Simulate the program after creating the test bench waveform and verify the functional design.
Steps to simulate.
1. Create a new project: Start by opening ModelSim and creating a new project. Click on the "File"
menu, then "New", and then select "Project". Give your project a name and select a working directory
for the project.
2. Add design files: Add your design files to the project by clicking on the "Add" button in the "Design"
tab of the "Project" window. Select your design files and click "OK".
3. Compile design: After adding your design files to the project, compile your design by clicking on the
"Compile" button in the "Design" tab of the "Project" window. This will check the syntax of your
design and generate a compiled version of the design.
4. Create a testbench: Create a testbench to verify the functionality of your design. To create a testbench,
click on the "File" menu, then "New", and select "Text Editor". Create a new file and write your
testbench code in this file.
5. Simulate the design: Once you have created your testbench, simulate your design by clicking on the
"Simulate" button in the "Design" tab of the "Project" window. This will open the ModelSim
simulation window.
6. Analyze simulation results: Analyze the simulation results to verify the functionality of your design.
You can view waveforms and other simulation data by using the various tools available in the
ModelSim simulation window.
7. Debug errors: If any errors or warnings are generated during the simulation, debug them by analyzing
the simulation results and modifying your design or testbench as necessary.
8. Save and export results: Once you are satisfied with the simulation results, save your work and export
any relevant results, such as waveforms or log files.
9. Close the project: Finally, close your project by clicking on the "File" menu and selecting "Close
Project". This will save your project and any changes you have made to it.
PROGRAM 1:
module semaphore_ex;
initial begin
fork
display(); //process-1
display(); //process-2
join
end
//display method
#30;
endtask
endmodule
Simulator Output:
module semaphore_ex;
semaphore sema; //declaring semaphore sema
initial begin
sema=new(1); //creating sema with '1' keys
fork
display(1); //process-1
display(2); //process-2
display(3); //process-3
join
end
//display method
task automatic display(int key);
sema.get(key); //getting 'key' number of keys from sema
$display($time,"\tCurrent Simulation Time, Got %0d keys",key);
#30;
sema.put(key+1); //putting 'key' number of keys to sema
endtask
endmodule
Simulator Output:
AIM:
To implement a Mailbox using System Verilog.
APPARATUS REQUIRED:
❖ Personal computer
PROCEDURE:
⮚ Write the System Verilog code for the logic design.
⮚ Simulate the program after creating the test bench waveform and verify the functional design.
Steps to simulate.
1. Create a new project: Start by opening ModelSim and creating a new project. Click on the "File"
menu, then "New", and then select "Project". Give your project a name and select a working directory
for the project.
2. Add design files: Add your design files to the project by clicking on the "Add" button in the "Design"
tab of the "Project" window. Select your design files and click "OK".
3. Compile design: After adding your design files to the project, compile your design by clicking on the
"Compile" button in the "Design" tab of the "Project" window. This will check the syntax of your
design and generate a compiled version of the design.
4. Create a testbench: Create a testbench to verify the functionality of your design. To create a testbench,
click on the "File" menu, then "New", and select "Text Editor". Create a new file and write your
testbench code in this file.
5. Simulate the design: Once you have created your testbench, simulate your design by clicking on the
"Simulate" button in the "Design" tab of the "Project" window. This will open the ModelSim
simulation window.
6. Analyze simulation results: Analyze the simulation results to verify the functionality of your design.
You can view waveforms and other simulation data by using the various tools available in the
ModelSim simulation window.
7. Debug errors: If any errors or warnings are generated during the simulation, debug them by analyzing
the simulation results and modifying your design or testbench as necessary.
8. Save and export results: Once you are satisfied with the simulation results, save your work and export
any relevant results, such as waveforms or log files.
9. Close the project: Finally, close your project by clicking on the "File" menu and selecting "Close
Project". This will save your project and any changes you have made to it.
PROGRAM 1:
Process-1(Generator class) will generate (created and randomize) the packet and put into the mailbox
mb_box
Process-2(Driver class) gets the generated packet from the mailbox and display the fields
//-------------------------------------------------------------------------
// Packet
//-------------------------------------------------------------------------
class packet;
rand bit [7:0] addr;
rand bit [7:0] data;
//-------------------------------------------------------------------------
//Generator - Generates the transaction packet and send to driver
//-------------------------------------------------------------------------
class generator;
packet pkt;
mailbox m_box;
//constructor, getting mailbox handle
function new(mailbox m_box);
this.m_box = m_box;
endfunction
task run;
repeat(2) begin
pkt = new();
pkt.randomize(); //generating packet
m_box.put(pkt); //putting packet into mailbox
$display("Generator::Packet Put into Mailbox");
#5;
end
endtask
endclass
//-------------------------------------------------------------------------
// Driver - Gets the packet from generator and display's the packet items
//-------------------------------------------------------------------------
class driver;
packet pkt;
mailbox m_box;
task run;
repeat(2) begin
m_box.get(pkt); //getting packet from mailbox
$display("Driver::Packet Recived");
$display("Driver::Addr=%0d,Data=%0d\n",pkt.addr,pkt.data);
end
endtask
endclass
//-------------------------------------------------------------------------
// tbench_top
//-------------------------------------------------------------------------
module mailbox_ex;
generator gen;
driver dri;
mailbox m_box; //declaring mailbox m_box
initial begin
//Creating the mailbox, Passing the same handle to generator and driver,
//because same mailbox should be shared in-order to communicate.
m_box = new(); //creating mailbox
Simulator Output:
------------------------------------------
Packet::Packet Generated
Packet::Addr=3,Data=38
Generator::Packet Put into Mailbox
Driver::Packet Recived
Driver::Addr=3,Data=38
Packet::Packet Generated
Packet::Addr=118,Data=92
Generator::Packet Put into Mailbox
Driver::Packet Recived
Driver::Addr=118,Data=92
------------------------------------------
RESULT: