Top Banner
Lecture 12: Testbenches and VHDL
39

Lecture 12: Testbenches and VHDL

Feb 07, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture 12: Testbenches and VHDL

Lecture 12: Testbenches and VHDL

Page 2: Lecture 12: Testbenches and VHDL

2

• HDL that tests another module: device under test (dut)

• Not synthesizeable • Types:

– Simple – Self-checking – Self-checking with testvectors

Testbenches

Slide derived from slides by Harris & Harris from their book

Page 3: Lecture 12: Testbenches and VHDL

3

module example(input logic a, b, c,

output logic y);

logic ab, bb, cb, n1, n2, n3;

assign #1 {ab, bb, cb} = ~{a, b, c};

assign #2 n1 = ab & bb & cb;

assign #2 n2 = a & bb & cb;

assign #2 n3 = a & bb & c;

assign #4 y = n1 | n2 | n3;

endmodule

Delays

Slide derived from slides by Harris & Harris from their book

Page 4: Lecture 12: Testbenches and VHDL

4

module example(input logic a, b, c,

output logic y);

logic ab, bb, cb, n1, n2, n3;

assign #1 {ab, bb, cb} =

~{a, b, c};

assign #2 n1 = ab & bb & cb;

assign #2 n2 = a & bb & cb;

assign #2 n3 = a & bb & c;

assign #4 y = n1 | n2 | n3;

endmodule

Delays

Slide derived from slides by Harris & Harris from their book

Page 5: Lecture 12: Testbenches and VHDL

5

• Write SystemVerilog code to implement the following function in hardware:

y = bc + ab

• Name the module sillyfunction

Testbench Example

Slide derived from slides by Harris & Harris from their book

Page 6: Lecture 12: Testbenches and VHDL

6

• Write SystemVerilog code to implement the following function in hardware:

y = bc + ab

module sillyfunction(input logic a, b, c,

output logic y);

assign y = ~b & ~c | a & ~b;

endmodule

Testbench Example

Slide derived from slides by Harris & Harris from their book

Page 7: Lecture 12: Testbenches and VHDL

7

module testbench1(); logic a, b, c; logic y; // instantiate device under test sillyfunction dut(a, b, c, y); // apply inputs one at a time initial begin a = 0; b = 0; c = 0; #10; c = 1; #10; b = 1; c = 0; #10; c = 1; #10; a = 1; b = 0; c = 0; #10; c = 1; #10; b = 1; c = 0; #10; c = 1; #10; end endmodule

Simple Testbench

Slide derived from slides by Harris & Harris from their book

Page 8: Lecture 12: Testbenches and VHDL

8

module testbench2();

logic a, b, c;

logic y;

sillyfunction dut(a, b, c, y); // instantiate dut initial begin // apply inputs, check results one at a time a = 0; b = 0; c = 0; #10;

if (y !== 1) $display("000 failed.");

c = 1; #10;

if (y !== 0) $display("001 failed.");

b = 1; c = 0; #10;

if (y !== 0) $display("010 failed.");

c = 1; #10;

if (y !== 0) $display("011 failed.");

a = 1; b = 0; c = 0; #10;

if (y !== 1) $display("100 failed.");

c = 1; #10;

if (y !== 1) $display("101 failed.");

b = 1; c = 0; #10;

if (y !== 0) $display("110 failed.");

c = 1; #10;

if (y !== 0) $display("111 failed.");

end

endmodule

Self-checking Testbench

Slide derived from slides by Harris & Harris from their book

Page 9: Lecture 12: Testbenches and VHDL

9

• Testvector file: inputs and expected outputs • Testbench:

1. Generate clock for assigning inputs, reading outputs 2. Read testvectors file into array 3. Assign inputs, expected outputs 4. Compare outputs with expected outputs and report

errors

Testbench with Testvectors

Slide derived from slides by Harris & Harris from their book

Page 10: Lecture 12: Testbenches and VHDL

10

• Testbench clock: – assign inputs (on rising edge) – compare outputs with expected outputs (on falling

edge).

• Testbench clock also used as clock for synchronous

sequential circuits

AssignInputs

CompareOutputs toExpected

CLK

Testbench with Testvectors

Slide derived from slides by Harris & Harris from their book

Page 11: Lecture 12: Testbenches and VHDL

11

• File: example.tv • contains vectors of abc_y expected

000_1

001_0

010_0

011_0

100_1

101_1

110_0

111_0

Testvectors File

Slide derived from slides by Harris & Harris from their book

Page 12: Lecture 12: Testbenches and VHDL

12

module testbench3();

logic clk, reset;

logic a, b, c, yexpected;

logic y;

logic [31:0] vectornum, errors; // bookkeeping variables

logic [3:0] testvectors[10000:0]; // array of testvectors

// instantiate device under test sillyfunction dut(a, b, c, y);

// generate clock always // no sensitivity list, so it always executes

begin

clk = 1; #5; clk = 0; #5;

end

1. Generate Clock

Slide derived from slides by Harris & Harris from their book

Page 13: Lecture 12: Testbenches and VHDL

13

// at start of test, load vectors and pulse reset

initial

begin

$readmemb("example.tv", testvectors);

vectornum = 0; errors = 0;

reset = 1; #27; reset = 0;

end

// Note: $readmemh reads testvector files written in // hexadecimal

2. Read Testvectors into Array

Slide derived from slides by Harris & Harris from their book

Page 14: Lecture 12: Testbenches and VHDL

14

// apply test vectors on rising edge of clk always @(posedge clk)

begin

#1; {a, b, c, yexpected} = testvectors[vectornum];

end

3. Assign Inputs & Expected Outputs

Slide derived from slides by Harris & Harris from their book

Page 15: Lecture 12: Testbenches and VHDL

15

// check results on falling edge of clk always @(negedge clk)

if (~reset) begin // skip during reset

if (y !== yexpected) begin

$display("Error: inputs = %b", {a, b, c});

$display(" outputs = %b (%b expected)",y,yexpected);

errors = errors + 1;

end

// Note: to print in hexadecimal, use %h. For example, // $display(“Error: inputs = %h”, {a, b, c});

4. Compare with Expected Outputs

Slide derived from slides by Harris & Harris from their book

Page 16: Lecture 12: Testbenches and VHDL

16

// increment array index and read next testvector vectornum = vectornum + 1;

if (testvectors[vectornum] === 4'bx) begin

$display("%d tests completed with %d errors",

vectornum, errors);

$finish;

end

end

endmodule

// === and !== can compare values that are 1, 0, x, or z.

4. Compare with Expected Outputs

Slide derived from slides by Harris & Harris from their book

Page 17: Lecture 12: Testbenches and VHDL

VHDL

Page 18: Lecture 12: Testbenches and VHDL

18

• We will go over examples of VHDL in comparison to SystemVerilog

• Examples taken from Ch. 4 of the Harris & Harris book 2nd Edition (recommended but not required book for this class)

VHDL

Page 19: Lecture 12: Testbenches and VHDL

19

Modules and Assign Statements

Slide derived from Harris & Harris book

Page 20: Lecture 12: Testbenches and VHDL

20

Conditional Assignment

Slide derived from Harris & Harris book

Page 21: Lecture 12: Testbenches and VHDL

21

More Assign Statements

Slide derived from Harris & Harris book

Page 22: Lecture 12: Testbenches and VHDL

22

Operators

Slide derived from Harris & Harris book

Page 23: Lecture 12: Testbenches and VHDL

23

Numbers

Slide derived from Harris & Harris book

Page 24: Lecture 12: Testbenches and VHDL

24

Bit Manipulations

Slide derived from Harris & Harris book

Page 25: Lecture 12: Testbenches and VHDL

25

Module Instantiations

Slide derived from Harris & Harris book

Page 26: Lecture 12: Testbenches and VHDL

26

Module Instantiations

Slide derived from Harris & Harris book

Page 27: Lecture 12: Testbenches and VHDL

27

Register

Slide derived from Harris & Harris book

Page 28: Lecture 12: Testbenches and VHDL

28

Resettable Register

Slide derived from Harris & Harris book

Page 29: Lecture 12: Testbenches and VHDL

29

Resettable Enabled Register

Slide derived from Harris & Harris book

Page 30: Lecture 12: Testbenches and VHDL

30

Multiple Registers

Slide derived from Harris & Harris book

Page 31: Lecture 12: Testbenches and VHDL

31

Always Comb

Slide derived from Harris & Harris book

Page 32: Lecture 12: Testbenches and VHDL

32

Case Statement

Slide derived from Harris & Harris book

Page 33: Lecture 12: Testbenches and VHDL

33

Case Statement

Slide derived from Harris & Harris book

Page 34: Lecture 12: Testbenches and VHDL

34

More If-Then-Else

Slide derived from Harris & Harris book

Page 35: Lecture 12: Testbenches and VHDL

35

Casez Statement

Slide derived from Harris & Harris book

Page 36: Lecture 12: Testbenches and VHDL

36

Blocking vs. Non-Blocking

Slide derived from Harris & Harris book

Page 37: Lecture 12: Testbenches and VHDL

37

Finite State Machine

Slide derived from Harris & Harris book

Page 38: Lecture 12: Testbenches and VHDL

38

Parameterized Modules

Slide derived from Harris & Harris book

Page 39: Lecture 12: Testbenches and VHDL

39

Parameterized Modules

Slide derived from Harris & Harris book