



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




















Patrick Schaumont
module
abc(...);
map into a single function
or task
module
abc(...);
call Patrick Schaumont
endmodule
endmodule
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
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
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
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
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
Patrick Schaumont
w
a
myadd = w + c; end endfunctionassign
q =
myadd(c);
// will return c + a + c
endmodule
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
3-bit gray code
Patrick Schaumont
Patrick Schaumont
bit3 = gray3 = 1bit2 = gray3 xor gray2 = 1 xor 1 = 0
Patrick Schaumont
bit3 = gray3 = 1bit2 = gray3 xor gray2 = 1 xor 1 = 0bit1 = gray3 xor gray2 xor gray
xor
xor
Patrick Schaumont
xor
xor
function
[W:0] gray_to_bin;
input
[W:0] a;
reg
[W:0] q;
begin
Patrick Schaumont
begin end endfunction
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