Thursday, March 31, 2005
Macros
Macros in Handel-C
Macro Expression
Non-parameterized (doesn't take arguments) marco expressions are of two types:
(1) Simple constant equivalent to #define
(2) a constant expression
Simple Expression
Constant Expression
Parameterized macro expressions
Macros with parameters can be used as well
Macro Procedures
Macro procedures are used to replace complete statments to avoid tedius repetition while coding. It generates the hardware for the statement each time it is referenced.
Difference Between Macro Procedures and Pre-processor Macro
Macro procedures differ from preprocessor macros (#define) in that they are not simple text replacements. Macro procedures must be valid Handel-C statments.
This is okay as proprocessor macro:
but it is not a valid macro procedure because it is not a complete statement
Macro Expression
Non-parameterized (doesn't take arguments) marco expressions are of two types:
(1) Simple constant equivalent to #define
(2) a constant expression
Simple Expression
macro expr DATA_WIDTH = 15;This form of the macro expression is similiar to #define. WHenever DATA_WIDTH appears, the compiler replaces it with 15.
Constant Expression
macro expr sum = (x + y) @ (y + z);A real expression can be used as well.
Parameterized macro expressions
Macros with parameters can be used as well
macro expr add3(x) = x + 3;This is equivalent to the following code:
y = add3(z);
y = z + 3;This form of macro is simialr to #define with parameters. Everything the add3() macro is referenced, it is expanded in the manner shown above. In this example, an adder is generated in hardware every time the add3() macro is used
Macro Procedures
Macro procedures are used to replace complete statments to avoid tedius repetition while coding. It generates the hardware for the statement each time it is referenced.
macro proc Name(parameters) StatementsMacro may be prototyped (like functions) which can be declared in one file and use them in another. Macro prototype is like so:
macro proc work(x , y);If we have local or static declarations within the macro procedure, a copy of the variable will be created for each copy of the macro.
Difference Between Macro Procedures and Pre-processor Macro
Macro procedures differ from preprocessor macros (#define) in that they are not simple text replacements. Macro procedures must be valid Handel-C statments.
This is okay as proprocessor macro:
#define test(x,y) if (x!=(y<<2))
but it is not a valid macro procedure because it is not a complete statement