Can somebody suggest me how to write a function in verilog which takes a vector as input and produces a vector as output, and the function iteself is independent of the length or size of the vector. I need the syntax of the function.
In vhdl I can do it very eaisly
FUNCTION myabs(s1:std_logic_vector) return std_logic_vector is
--this function returns absolute of the input std_logic_vector
VARIABLE V : std_logic_vector(s1'high downto s1'low) ;
BEGIN
V := s1;
for i in V'low to V'high loop
V(i) := V(i) XOR V(V'HIGH);
end loop;
V:=V+V(V'HIGH);
return V;
end myabs; -- end function
Note that the function does not mention anywhere the 'size of' or the 'width of' the input or output vectors, instead, 'high and 'low vhdl attributes are used.
Can somebody write this in Verilog?
In verilog, You can use alsto the function or the task to finish it .
It is very similar to the VHDL about the function.
I suggest you find a verilog language to refer, then you can get it soon
Well echo47, thanks very much for your reply. But there are built in functions say for example '&&' which do not know the 'length' or 'size' or 'width' of vectors passed to them, yet the do the job. I wonder how they work?
kr,
Aviral Mittal
The compiler knows the value internally, but the language authors simply didn't give us an operator to access it. Big oversight in my opinion. Imagine the C language without the sizeof operator. I think SystemVerilog provides the $bits function.