digital system design verilog-part vce.sharif.edu/courses/83-84/2/111/resources/root/verilog/verilog...

21
Digital System Design Verilog-Part V Amir Masoud Gharehbaghi [email protected]

Upload: others

Post on 17-May-2020

21 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Digital System Design Verilog-Part Vce.sharif.edu/courses/83-84/2/111/resources/root/Verilog/Verilog 5.pdf · Digital System Design Verilog-Part V Amir Masoud Gharehbaghi amgh@mehr.sharif.edu

Digital System DesignVerilog-Part V

Amir Masoud [email protected]

Page 2: Digital System Design Verilog-Part Vce.sharif.edu/courses/83-84/2/111/resources/root/Verilog/Verilog 5.pdf · Digital System Design Verilog-Part V Amir Masoud Gharehbaghi amgh@mehr.sharif.edu

Sharif University of Technology 2

Function Declaration

Functions can be defined in a module.

function <range_or_type> function_name;<input declarations><register declarations><function statements>endfunction

Page 3: Digital System Design Verilog-Part Vce.sharif.edu/courses/83-84/2/111/resources/root/Verilog/Verilog 5.pdf · Digital System Design Verilog-Part V Amir Masoud Gharehbaghi amgh@mehr.sharif.edu

Sharif University of Technology 3

Function Declaration (cont.)

Functions have a return valueReturn value is one bit by defaultReturn value can be a vector (if range specified)Return value can be of different register types

integer…

Page 4: Digital System Design Verilog-Part Vce.sharif.edu/courses/83-84/2/111/resources/root/Verilog/Verilog 5.pdf · Digital System Design Verilog-Part V Amir Masoud Gharehbaghi amgh@mehr.sharif.edu

Sharif University of Technology 4

Function Declaration (cont.)

Function must have at least one inputMay have more than one input

Local variables of Register type can be defined in a function

reginteger…

Page 5: Digital System Design Verilog-Part Vce.sharif.edu/courses/83-84/2/111/resources/root/Verilog/Verilog 5.pdf · Digital System Design Verilog-Part V Amir Masoud Gharehbaghi amgh@mehr.sharif.edu

Sharif University of Technology 5

Function Statements

Any sequential statementDelay or Event control is not supported in function statements

Functions are executed in 0 time

Nested function call is supported

Page 6: Digital System Design Verilog-Part Vce.sharif.edu/courses/83-84/2/111/resources/root/Verilog/Verilog 5.pdf · Digital System Design Verilog-Part V Amir Masoud Gharehbaghi amgh@mehr.sharif.edu

Sharif University of Technology 6

Function Call

Appears in expressionsFormat:function_name (input_list)

Order of inputs is the order of definition in function body

Page 7: Digital System Design Verilog-Part Vce.sharif.edu/courses/83-84/2/111/resources/root/Verilog/Verilog 5.pdf · Digital System Design Verilog-Part V Amir Masoud Gharehbaghi amgh@mehr.sharif.edu

Sharif University of Technology 7

Example: Parity Checkermodule parity (a, p) ;

input [7:0] a; output p;function par ;

input [7:0] data;par = ^ data;

endfunctionassign #10 p = par(a);

endmodule

Page 8: Digital System Design Verilog-Part Vce.sharif.edu/courses/83-84/2/111/resources/root/Verilog/Verilog 5.pdf · Digital System Design Verilog-Part V Amir Masoud Gharehbaghi amgh@mehr.sharif.edu

Sharif University of Technology 8

Example: Shiftermodule shifter (o, in, shift, lr);

input [15:0] in; input [3:0] shift;input lr;output [15:0] o; reg [15:0] o;

always @(in or shift or lr)o <= shift_lr(in, shift, lr);

Page 9: Digital System Design Verilog-Part Vce.sharif.edu/courses/83-84/2/111/resources/root/Verilog/Verilog 5.pdf · Digital System Design Verilog-Part V Amir Masoud Gharehbaghi amgh@mehr.sharif.edu

Sharif University of Technology 9

Example: Shifter (cont.)function [15:0] shift_lr ;

input [15:0] in; input [4:1] num_shift;input left_right;shift_lr = left_right ? in << num_shift :

in >> num_shift ;endfunctionendmodule

Page 10: Digital System Design Verilog-Part Vce.sharif.edu/courses/83-84/2/111/resources/root/Verilog/Verilog 5.pdf · Digital System Design Verilog-Part V Amir Masoud Gharehbaghi amgh@mehr.sharif.edu

Sharif University of Technology 10

Example: Shifter (cont.)function [15:0] shift_lr ;

input [15:0] in; input [4:1] num_shift;input left_right;reg [15:0] shl, shr;shl = in << num_shift;shr = in >> num_shift;if (left_right) shift_lr =shl; else shift_lr =shr;

endfunctionendmodule

Page 11: Digital System Design Verilog-Part Vce.sharif.edu/courses/83-84/2/111/resources/root/Verilog/Verilog 5.pdf · Digital System Design Verilog-Part V Amir Masoud Gharehbaghi amgh@mehr.sharif.edu

Sharif University of Technology 11

Example: Multifunction ALUmodule alu (a, b, s, sel);

input [7:0] a, b; input [1:0] sel;output [7:0] s; reg [7:0] s;

// function declaration comes here -> next slide

always @(a or b or sel) s <= func (a, b, sel);

endmodule

Page 12: Digital System Design Verilog-Part Vce.sharif.edu/courses/83-84/2/111/resources/root/Verilog/Verilog 5.pdf · Digital System Design Verilog-Part V Amir Masoud Gharehbaghi amgh@mehr.sharif.edu

Sharif University of Technology 12

Example: Multifunction ALU (cont.)

function integer func ;input [1:8] in1, in2; input [1:0] sel;case (sel)

0: func <= in1 + in2;1: func <= in1 - in2;2: func <= in1 << in2;3: func <= in1 >> in2;default: func <= 0;

endcaseendfunction

Page 13: Digital System Design Verilog-Part Vce.sharif.edu/courses/83-84/2/111/resources/root/Verilog/Verilog 5.pdf · Digital System Design Verilog-Part V Amir Masoud Gharehbaghi amgh@mehr.sharif.edu

Sharif University of Technology 13

Example: Moore 011 Detectormodule moore_011 (x, z, clk);

input x, clk; output z; reg z; reg [2:1] state = 0;function calc_out ; input [1:0] s;

calc_out = s == 3;endfunctionalways @(posedge clk)

state <= next_st(state,x);always @ (state) z <= calc_out (state);

Page 14: Digital System Design Verilog-Part Vce.sharif.edu/courses/83-84/2/111/resources/root/Verilog/Verilog 5.pdf · Digital System Design Verilog-Part V Amir Masoud Gharehbaghi amgh@mehr.sharif.edu

Sharif University of Technology 14

Moore 011 Detector (cont.)function [2:1] next_st ;

input [2:1] state; input xif (state == 0) next_st <= x ? 0 : 1;else if (state == 1) next_st <= x ? 2 : 1;else if (state == 2) next_st <= x ? 3 : 1;else if (state == 3) next_st <= x ? 0 : 1;else next_st <= 0;

endfunctionendmodule

Page 15: Digital System Design Verilog-Part Vce.sharif.edu/courses/83-84/2/111/resources/root/Verilog/Verilog 5.pdf · Digital System Design Verilog-Part V Amir Masoud Gharehbaghi amgh@mehr.sharif.edu

Sharif University of Technology 15

Task Declaration

Tasks can be defined in a module.

Task task_name;<interface declarations><task declarations><task statements>endtask

Page 16: Digital System Design Verilog-Part Vce.sharif.edu/courses/83-84/2/111/resources/root/Verilog/Verilog 5.pdf · Digital System Design Verilog-Part V Amir Masoud Gharehbaghi amgh@mehr.sharif.edu

Sharif University of Technology 16

Task Declaration (cont.)Interfaces can be

input / output / inoutNumber of interfaces is 0 or moreReturn values through output / inoutinterfacesLocal variables of Register type can be defined in a task

reginteger…

Page 17: Digital System Design Verilog-Part Vce.sharif.edu/courses/83-84/2/111/resources/root/Verilog/Verilog 5.pdf · Digital System Design Verilog-Part V Amir Masoud Gharehbaghi amgh@mehr.sharif.edu

Sharif University of Technology 17

Task Statements

Any sequential statementDelay or Event control is supported in task statements

Tasks may be executed in 0 time or not

Tasks can enable another tasks or have function call

Page 18: Digital System Design Verilog-Part Vce.sharif.edu/courses/83-84/2/111/resources/root/Verilog/Verilog 5.pdf · Digital System Design Verilog-Part V Amir Masoud Gharehbaghi amgh@mehr.sharif.edu

Sharif University of Technology 18

Task Enable

Appear in sequential bodiesFormat:task_name (interface_list) ;

Order of interfaces are the order of definition in task body

Page 19: Digital System Design Verilog-Part Vce.sharif.edu/courses/83-84/2/111/resources/root/Verilog/Verilog 5.pdf · Digital System Design Verilog-Part V Amir Masoud Gharehbaghi amgh@mehr.sharif.edu

Sharif University of Technology 19

Example: 011 Detectormodule mealy_011 (x, z, clk);

input x, clk; output z; reg z;reg [2:1] state = 0, next_st = 0;always @(posedge clk)

state <= next_st;task calc_out;

input [2:1] state; input x; output z;#10if (state == 2 && x) z <= 1; else z <= 0;

endtaskalways @(state or x) calc_out(state, x , z);

Page 20: Digital System Design Verilog-Part Vce.sharif.edu/courses/83-84/2/111/resources/root/Verilog/Verilog 5.pdf · Digital System Design Verilog-Part V Amir Masoud Gharehbaghi amgh@mehr.sharif.edu

Sharif University of Technology 20

Example: 011 Detector (cont.)always @(state or x)

case (state)2'b00 : next_st <= x ? 0 : 1;2'b01 : next_st <= x ? 2 : 1;2'b10 : next_st <= x ? 0 : 1;default: next_st <= 0;

endcaseendmodule

Page 21: Digital System Design Verilog-Part Vce.sharif.edu/courses/83-84/2/111/resources/root/Verilog/Verilog 5.pdf · Digital System Design Verilog-Part V Amir Masoud Gharehbaghi amgh@mehr.sharif.edu

Sharif University of Technology 21

Example: Apply Inputsmodule testbench ;

reg [7:0] a, b;// a module instantiation comes hereinitial apply (a, b);initial a = 0; initial b = 0;task apply;

output [7:0] a, b;#10 a = a + 3; #5 b = b + 1;

endtaskendmodule