Calling function from another module in Verilog - function

I am trying to instantiate and use a function that has been defined in another module.
module simple_function();
function myfunction;
input a, b, c, d;
begin
myfunction = ((a+b) + (c-d));
end
endfunction
endmodule
module function_calling(a, b, c, d, e, f);
input a, b, c, d, e ;
output f;
wire f;
`include "myfunction.v"
assign f = (myfunction (a,b,c,d)) ? e :0;
endmodule
I got this code from http://www.asic-world.com/verilog/task_func1.html
However, when I execute the same in ModelSim Altera Starter Edition 10.0d, I get this error:
Cannot open `include file "myfunction.v".
Where am I going wrong?

An include directive has the same effect as copying and pasting that code in the same spot. From your code it looks like you are trying to define a module (simple_function) inside of another module (function_calling), which is not allowed.
Your function does not need to be contained inside a module. You should change myfunction.v to just contain the myfunction definition and remove the simple_function module entirely. That way when myfunction.v is included it has the same effect as the function being declared in function_calling.

Another way for calling a function of one module into another module is through interface. You can pass interface ports into first module due to which interface will get the all functions and tasks into it, and then interface can be pass into second module, in this way second module can get tasks and functions of first module.

Related

QTCreator function placement - error: reference to non-static member function must be called-

I am new on the QT world. And I am confused to where to put my small bool function:
bool Widget::compareBy(const dist_obj &a, const dist_obj &b)
{
return a.distance < b.distance;
}
and I want to call it from `void Widget::update_window()
like:
`
std::sort(found_obj.begin() , found_obj.end() , compareBy);
it gave:
error: reference to non-static member function must be called
I am using this function in my normal c++11 Clion environment. What am I doing wroin in this QT world :O
thanks
I found an answer which is worked for me from sorting a vector of classes based on a variable in the class
worked like a charm without defining any function or class.

How can I use SWIG to handle a JAVA to C++ call with a pointer-to-pointer argout argument?

The problem involved a JAVA call to a C-function (API) which returned a pointer-to-pointer as an argout argument. I was trying to call the C API from JAVA and I had no way to modify the API.
Using SWIG typemap to pass pointer-to-pointer:
Here is another approach using typemaps. It is targetting Perl, not Java, but the concepts are the same. And I finally managed to get it working using typemaps and no helper functions:
For this function:
typedef void * MyType;
int getblock( int a, int b, MyType *block );
I have 2 typemaps:
%typemap(perl5, in, numinputs=0) void ** data( void * scrap )
{
$1 = &scrap;
}
%typemap(perl5, argout) void ** data
{
SV* tempsv = sv_newmortal();
if ( argvi >= items ) EXTEND(sp,1);
SWIG_MakePtr( tempsv, (void *)*$1, $descriptor(void *), 0);
$result = tempsv;
argvi++;
}
And the function is defined as:
int getblock( int a, int b, void ** data );
In my swig .i file. Now, this passes back an opaque pointer in the argout typemap, becaust that's what useful for this particular situation, however, you could replace the SWIG_MakePtr line with stuff to actually do stuff with the data in the pointer if you wanted to. Also, when I want to pass the pointer into a function, I have a typemap that looks like this:
%typemap(perl5, in) void * data
{
if ( !(SvROK($input)) croak( "Not a reference...\n" );
if ( SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor, 0 ) == -1 )
croak( "Couldn't convert $1 to $1_descriptor\n");
}
And the function is defined as:
int useblock( void * data );
In my swig .i file.
Obviously, this is all perl, but should map pretty directly to Java as far as the typemap architecture goes. Hope it helps...
[Swig] Java: Using C helper function to pass pointer-to-pointer
The problem involved a JAVA call to a C-function (API) which returned a pointer-to-pointer as an argout argument. I was trying to call the C API from JAVA and I had no way to modify the API.
The API.h header file contained:
extern int ReadMessage(HEADER **hdr);
The original C-call looked like:
HEADER *hdr;
int status;
status = ReadMessage(&hdr);
The function of the API was to store data at the memory location specified by the pointer-to-pointer.
I tried to use SWIG to create the appropriate interface file. SWIG.i created the file SWIGTYPE_p_p_header.java from API.h. The problem is the SWIGTYPE_p_p_header constructor initialized swigCPtr to 0.
The JAVA call looked like:
SWIGTYPE_p_p_header hdr = new SWIGTYPE_p_p_header();
status = SWIG.ReadMessage(hdr);
But when I called the API from JAVA the ptr was always 0.
I finally gave up passing the pointer-to-pointer as an input argument. Instead I defined another C-function in SWIG.i to return the pointer-to-pointer in a return value. I thought it was a Kludge ... but it worked!
You may want to try this:
SWIG.i looks like:
// return pointer-to-pointer
%inline %{
HEADER *ReadMessageHelper() {
HEADER *hdr;
int returnValue;
returnValue = ReadMessage(&hdr);
if (returnValue!= 1) hdr = NULL;
return hdr;
}%}
The inline function above could leak memory as Java won't take ownership of the memory created by ReadMessageHelper, since the HEADER instance iscreated on the heap.
The fix for the memory leak is to define ReadMessageHelper as a newobject in order for Java to take control of the memory.
%newobject ReadMessageHelper();
JAVA call now would look like:
HEADER hdr;
hdr = SWIG.ReadMessageHelper();
If you are lucky, as I was, you may have another API available to release the message buffer. In which case, you wouldn’t have to do the previous step.
William Fulton, the SWIG guru, had this to say about the approach above:
“I wouldn't see the helper function as a kludge, more the simplest solution to a tricky problem. Consider what the equivalent pure 100% Java code would be for ReadMessage(). I don't think there is an equivalent as Java classes are passed by reference and there is no such thing as a reference to a reference, or pointer to a pointer in Java. In the C function you have, a HEADER instances is created by ReadMessage and passed back to the caller. I don't see how one can do the equivalent in Java without providing some wrapper class around HEADER and passing the wrapper to the ReadMessage function. At the end of the day, ReadMessage returns a newly created HEADER and the Java way of returning newly created objects is to return it in the return value, not via a parameter.”

setting function prototype in Lua

A simple question: How do I set the prototype for a function that has not been implemented yet?
I just want to do this, cause I'm referring to a function that does not exist(yet).
In C, we would do something like this:
int foo(int bar);
int myint = foo(1);
int foo(int bar)
{
return bar;
}
How do I do this in Lua (with corona)?
You can't. Amber's comment is correct.
Lua doesn't have a concept of type signatures or function prototypes.
The type of foo is that of the object it contains, which is dynamic, changing at runtime. It could be function in one instant, and string or integer or something else in the next.
Conceptually Lua doesn't have a compilation step like C. When you say "run this code" it starts starts executing instructions at the top and works it's way down. In practice, Lua first compiles your code into bytecode before executing it, but the compiler won't balk at something like this:
greet()
function greet()
print('Hello.')
end
Because the value contained in greet is determined at runtime. It's only when you actually try to call (i.e. invoke like a function) the value in greet, at runtime, that Lua will discover that it doesn't contain a callable value (a function or a table/userdata with a metatable containing a __call member) and you'll get a runtime error: "attempt to call global 'greet' (a nil value)". Where "nil value" is whatever value greet contained at the time the call was attempted. In our case, it was nil.
So you will have to make sure that that the code that creates a function and assigns it to foo is called before you attempt to call foo.
It might help if you recognize that this:
local myint = foo(1)
function foo(bar)
return bar
end
Is syntax sugar for this:
local myint = foo(1)
foo = function(bar)
return bar
end
foo is being assigned a function value. That has to happen before you attempt to call that function.
The most common solution to this problem is to treat the file's function as the "compilation time", that is: declare all of your constant data and functions when the file is executed, ready to be used during "execution time". Then, call a main function to begin the "execution time".
For example:
function main()
greet()
end
function greet()
print('Hello.')
end
main()
As greet has been declared in _G, main can access it.

Why is all data that is passed to a function "explicitly passed"?

Data is passed to a function "explicitly" whereas a method is "implicitly passed" to the object for which it was called.
Please could you explain the difference between these two ways of passing data? An example in java or c# would help.
The language Java and Python are good examples in illustrating this. In Python, the object is passed explicitly whenever a method of a class is defined:
class Example(object):
def method(self, a, b):
print a, b
# The variable self can be used to access the current object
Here, the object self is passed explicitly as the first argument. This means that
e = Example()
e.method(3, 4)
is effectively the same as calling method(e, 3, 4) if method were a function.
However, in Java the first argument is not explicitly mentioned:
public class Example {
public void method(int a, int b) {
System.out.println(a + " " + b);
// The variable this can be used to access the current object
}
}
In Java, it would be:
Example e = Example();
e.method(3, 4);
The instance e is passed to method as well but the special variable this can be used to access it.
Of course, for functions each argument is passed explicitly because each argument is mentioned in both the function definition and where the function is called. If we define
def func(a, b, c):
print a, b, c
then we can call it with func(1, 2, 3) which means all arguments are explicitly passed.
In this context a method can be considered to be a function that has access to the object it's bound to. Any properties of this object can be accessed from the method, even though they didn't appear in the signature of the function. You didn't specify a language, but let me give an example in PHP as it's pretty prevalent and easy to read even if you didn't use it.
Edit: the languages were added after I wrote this; maybe someone can translate this to one of those languages if needed.
<?php
/* Explicit passing to a function */
function f($a, b)
{
return $a + b;
}
// f(1, 2) == 3
class C
{
public $a, $b;
/* $a and $b are not in the parameter list. They're accessed via the special $this variable that points to the current object. */
public function m()
{
return $this->a + $this->b;
}
}
$o = new C();
$o->a = 1;
$o->b = 2;
//$o->m() == 3

How to load-string to current namespace (not core)?

I can load arbitrary Clojure source using:
(load-string source)
However, if namespace wasn't provided, it loads code to clojure.core namespace.
For example, following code:
(load-string "(defn add [a b] (+ a b))")
defines a function:
#'clojure.core/add
Now, is there a way to load that code to some other namespace, preferably the same one in which load-string function is called?
(Other than prepending a namespace declaration to source string before evaluation. I know that it would solve the problem - I'd like to know is there a preferred way)
when def needs to decide what namspace a new function should go in it looks at the the current value of the ns var and adds the new function to that namespace. because ns is a var you can dynamically bind it before you call load-string
user> (binding [*ns* (find-ns 'foo)] (load-string "(defn f [] 4)"))
#'foo/f
user> (foo/f)
4