Docsity

Functions and Tasks - Digital Design II | ECE 4514, Study notes of Electrical and Electronics Engineering

Partial preview of the text

Download Functions and Tasks - Digital Design II | ECE 4514 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

ECE 4514

Digital Design II

Spring 2008^ Lecture 21:

Patrick Schaumont

Lecture 21:

Functions and Tasks

A

Language

Lecture

Patrick Schaumont

Tasks and Functions 

Help with the reusability of writing code

module

abc(...);

map into a single function

or task

module

abc(...);

call Patrick Schaumont

endmodule

endmodule

Functions^ module

andorgate(q, a, b);

output

[1:0] q;

input

a, b;

function

mygate;

input

x, y;

input

op;

case

(op)

1'b0:

mygate

= x & y;

Patrick Schaumont

1'b0:

mygate

= x & y;

1'b1: mygate = x | y; default

: mygate = 1'b0;

endcase endfunctionassign

q[0] = mygate(a, b, 0); // and gate

assign

q[1] = mygate(a, b, 1); // or gate

endmodule

Functions^ module

andorgate(q, a, b);

output

[1:0] q;

input

a, b;

function

mygate;

input

x, y;

input

op;

case

(op)

1'b0:

mygate

= x & y;

x^

y^

op

mygate

Patrick Schaumont

1'b0:

mygate

= x & y;

1'b1: mygate = x | y; default

: mygate = 1'b0;

endcase endfunction assign

q[0] = mygate(a, b, 0); // and gate

assign

q[1] = mygate(a, b, 1); // or gate

endmodule

Functions^ module

andorgate(q, a, b);

output

[1:0] q;

input

a, b;

function

mygate;

input

x, y;

input

op;

case

(op)

1'b0:

mygate

= x & y;

The logic is defined by

Patrick Schaumont

1'b0:

mygate

= x & y;

1'b1: mygate = x | y; default

: mygate = 1'b0;

endcase endfunctionassign

q[0] = mygate(a, b, 0); // and gate

assign

q[1] = mygate(a, b, 1); // or gate

endmodule

The logic is defined by the function calls, not bythe function declaration

Functions can have local variables (reg)^ module

addit(q, a, b, c);

output

[1:0] q;

input

[1:0] a, b, c;

function

[1:0] myadd;

input

[1:0] x, y, z;

reg

[1:0] w;

begin w

= x + y;

Two-bit output^ Local variable

Patrick Schaumont

w

= x + y;

myadd = w + z; end endfunctionassign

q =

myadd(a, b, c);

endmodule

Functions can access module (global) variables^ module

addit(q, a, b, c);

output

[1:0] q;

input

[1:0] a, b, c;

function

[1:0] myadd;

input

[1:0] x;

reg

[1:0] w;

begin w

=^

a

  • x;

Patrick Schaumont

w

=^

a

  • x;

myadd = w + c; end endfunctionassign

q =

myadd(c);

// will return c + a + c

endmodule

Automatic Functions^ module

addit(q1, q2, a, b, c);

output

[1:0] q1, q2;

input

[1:0] a, b, c;

function

automatic

[1:0] myadd;

input

[1:0] x, y, z;

reg

[1:0] w;

begin w

= x + y;

automatic ensures that

all local variables

Patrick Schaumont

w

= x + y;

myadd = w + z; end endfunctionassign

q1 =

myadd(a, b, c)

assign

q2 =

myadd(a, a, a)

endmodule

all local variables

are truly local

With automatic, each invocation of myadd will

use a different w

Example: Gray to Bin Conversion Function

3-bit gray code

Remember Gray Code?

^

A gray code is a code where only 1 bitflips per code word

Patrick Schaumont

How do we build a gray-to-bin function?

Example: Gray to Bin Conversion Function

Gray to Bin should convert this

OUT^000001010011100
IN

Patrick Schaumont

It can be shown that

bin-bit i = xor-sum(gray-bit i .. gray-bit N)

Example: Gray to Bin Conversion Function

Gray to Bin should convert this

OUT^000001010011100
IN

bit3 = gray3 = 1bit2 = gray3 xor gray2 = 1 xor 1 = 0

Patrick Schaumont

It can be shown that

bin-bit i = xor-sum(gray-bit i .. gray-bit N)

Example: Gray to Bin Conversion Function

Gray to Bin should convert this

OUT^000001010011100
IN

bit3 = gray3 = 1bit2 = gray3 xor gray2 = 1 xor 1 = 0bit1 = gray3 xor gray2 xor gray

xor

xor

Patrick Schaumont

It can be shown that

bin-bit i = xor-sum(gray-bit i .. gray-bit N)

xor

xor

Example: Gray to Bin Conversion Function

Cool. Now let's use a loop to build the gray to binfunction

function

[W:0] gray_to_bin;

input

[W:0] a;

reg

[W:0] q;

begin

Patrick Schaumont

begin end endfunction

Example: Gray to Bin Conversion Function

Cool. Now let's use a loop to build the gray to binfunction

function

[W:0] gray_to_bin;

input

[W:0] a;

reg

[W:0] q;

integer i; begin

Patrick Schaumont

begin for (i=0; i<=W; i = i + 1)

q[i] = ^(a >> i); gray_to_bin = q; end endfunction