// an example illustrating how to count the zeros in a byte.
module zero_count_task (data, out);
input [7:0] data;
output reg [3:0] out; // output declared as register
always @(data)
count_0s_in_byte(data, out);
// task declaration from here.
task count_0s_in_byte(input [7:0] data,
output reg [3:0] count);
integer i;
begin // task body
count = 0;
for (i = 0; i <= 7; i = i + 1)
// The following statement can be replaced by
// : count = count + ~data[i]. Why?
if (data[i] == 0) count= count + 1;
end
endtask
endmodule
// an example illustrating how to count the zeros in a byte.
module zero_count_function (data, out);
input [7:0] data;
output reg [3:0] out; // output declared as register
always @(data)
out = count_0s_in_byte(data);
// function declaration from here.
function [3:0] count_0s_in_byte(input [7:0] data);
integer i;
begin
count_0s_in_byte = 0;
for (i = 0; i <= 7; i = i + 1)
// the following statement can be replaced by:
// count_0s_in_byte = count_0s_in_byte + ~data[i]. Why?
if (data[i] == 0)
count_0s_in_byte = count_0s_in_byte + 1;
end
endfunction
endmodule
沒有留言:
張貼留言