System Verilog AND Verification : Department OF Electronics AND Communication B M S College OF Engineering
System Verilog AND Verification : Department OF Electronics AND Communication B M S College OF Engineering
B M S COLLEGE OF ENGINEERING
(AUTONOMOUS COLLEGE UNDER VTU, BELGAUM)
BANGALORE – 560019
2021-22
BY
NAME USN
SANJANA VK 1BM19EC141
Course Instructor
2. Design a Moore FSM to detect the sequence 1001 without overlap. Write a test
bench environment with covergroup and coverpoints to verify the design. Randomize
the inputs appropriately using transactor class, generator class and driver class with
virtual interfaces. (Optional: Use mailboxes to communicate between generator and
driver class).
2|Page
1. Design a 4-bit ALU and randomize the inputs to the design. Write
assertions to test the following:
Design – ALU.v:
module ALU (
input clk, rst,
input [2:0] op,
input [3:0] a, b,
output reg [3:0] q
);
always @(posedge clk, posedge rst)
begin
if(rst)
begin
q <= 4'd0;
end
else
begin
case(op)
3'd0: q <= a + b;
3'd1: q <= a - b;
3'd2: q <= a * b;
3'd3: q <= a / b;
3'd4: q <= a & b;
3'd5: q <= a | b;
3'd6: q <= ~a;
3'd7: q <= (a >> 1);
default: q <= 4'bx;
endcase
end
end
endmodule
Interface – alu_inter.sv
3|Page
endclocking
property p_rst;
@(cb) (rst == 1)|->(q == 0);
endproperty:p_rst
a1:assert property(p_rst) $display("rst held");
else $display("rst fail");
property op0;
@(cb) (op == 0)|->(q == a+b);
endproperty:op0
Testbench – test.sv:
class transactor;
rand bit [3:0] op;
rand bit [4:0] a, b;
//constraint c1 {a inside {[0:10], 12, 15};}
//constraint c2 {b >= 0; b< 14;}
endclass
class driver;
transactor tx;
virtual alu_inter.TB i1;
task drive_data();
begin
repeat (2000) @(i1.cb)
if(tx.randomize())
begin
i1.cb.op <= tx.op;
i1.cb.a <= tx.a;
i1.cb.b <= tx.b;
end
else
$display("[%d] Randomization Failed!", $time);
end
endtask
endclass
//`include "alu_inter.sv"
//`include "driver.sv"
program test(alu_inter.TB i1);
driver drv = new(i1);
initial
4|Page
begin
i1.rst <= 1;
#10 i1.rst <= 0;
#100 i1.rst <= 1;
#20 i1.rst <= 0;
#3000 $finish;
end
initial
begin
drv.drive_data();
end
endprogram
//`include "alu_inter.sv"
//`include "test.sv"
//`include "ALU.v"
module top();
bit clk;
always #5 clk = ~clk;
alu_inter io (clk);
test t1 (io);
ALU DUT (.clk(clk), .rst(io.rst), .op(io.op), .a(io.a), .b(io.b), .q(io.q));
initial
begin
$dumpfile("ALU.vcd");
$dumpvars;
end
endmodule
Simulation in Simvison:
5|Page
Assertions:
a. Add assertion held for opcode = 000:
6|Page
2. Design a Moore FSM to detect the sequence 1001 without overlap. Write
a test bench environment with covergroup and coverpoints to verify the
design. Randomize the inputs appropriately using transactor class,
generator class and driver class with virtual interfaces. (Optional: Use
mailboxes to communicate between generator and driver class).
//Design
module moore101(
input clk, rst, I,
end
// Next state block
always @(posedge clk , I, ps)
begin
case(ps)
S0: begin
if(I)
7|Page
ns <= S1;
else
ns <= S0;
end
S1: begin
if(I)
ns <= S1;
else
ns <= S2;
end
S2: begin
if(I)
ns <= S3;
else
ns <= S0;
end
S3: begin
if(I)
ns <= S1;
else
ns <= S0;
end
S1: Y <= 0;
S2: Y <= 0;
8|Page
S3: Y <= 1;
default: Y <= 0;
endcase
end
endmodule
//interface
interface moore101_io(input bit clk);
logic rst, I, Y;
logic [1:0] ps, ns;
clocking cb @(posedge clk);
default input #1ns output #3ns;
endinterface
test
class transactor;
rand bit I;
// constraint inp {I inside {0,1};}
endclass
//generator
class generator;
transactor tx;
virtual moore101_io.TB i1;
mailbox gen2drv;
endfunction
task gen_data();
9|Page
repeat (1000) @(i1.cb)
begin
if(tx.randomize())
begin
$display("[GEN] I = %d", tx.I);
end
else
begin
$display("[%d] Ramdomization Failed!", $time);
end
gen2drv.put(tx);
end
endtask
endclass
class driver;
transactor tx;
mailbox gen2drv;
virtual moore101_io.TB i1;
covergroup cg;
I: coverpoint i1.cb.I {
bins t0 = {0};
bins t1 = {1};
}
10 | P a g e
}
Y: coverpoint i1.cb.Y;
endgroup
this.cg = new();
endfunction
task drive_data();
begin
endtask
endclass
//testbench
program test(moore101_io.TB i1);
mailbox gen2drv = new();
i1.rst <= 0;
#10 i1.rst <= 1;
11 | P a g e
#50 i1.rst <= 0;
end
initial
begin
genr.gen_data();
end
initial
begin
drv.drive_data();
end
endprogram
//Top
module top;
bit clk;
always #5 clk = ~clk;
moore101_io io (clk);
$dumpfile("moore101.vcd");
$dumpvars;
end
endmodule
12 | P a g e
Simulation in Simvison:
13 | P a g e