Wednesday, April 23, 2008

Reusable Channel using Define Macro

Now, same code can be implemented using define macro as follows.

`define channel(A) class A``_channel ; // Class of type 'A_channel' \
    A queue[$]; \
    semaphore sem; \
    task put_data(A t); \
        sem.get(1); \
        queue.push_back(t); \
        sem.put(1); \
    endtask \
    task get_data(ref A t); \
        sem.get(1); \
        t = queue.pop_back(t); \
        sem.put(1); \
    endtask \

Using above mentioned code, you can create any type of customized channel between two components.
Channel passing integer data between two components using,
integer_channel int_channel = new();
Channel passing class object of type 'data_class' between two component using,
data_class_channel data_class_channel = new();
As you noted here, `channel(integer) or `channel(data_class) macro calls, creates customized channel class declarations for you and then you can instantiate those class objects.

Another thing to be noticed is the use of `` to create customized data type. In the example above, I have used 'A_``channel'. This expands to 'A_channel'.

Next: Advanced use of define macro in RVM and VMM of Synopsys


  1. Hi Sandeep,

    Thanks for information on macros in SV. I have a question, in the example stated above instead of calling the macro as `channel(integer), can a macro be used as an argument to the `channel macro?

    If so how can that be done? LRM of SV doesn't talk about this aspect neither do i find it anywhere in the web.

    Thanks for the help.

    Thanks & Regards

  2. Hi Ram,

    Yes. You can pass macro as argument to `channel(integer).
    For example, consider that you have generic data class.
    `define get_data_class_name(A) A``_data
    `define define_data_class(A) class A``_data; \
    A variable; \
    function fnc1(A a); \
    endfunction \
    task tsk1(A a); \
    endtask \

    Now, you can create channel as follows.
    `define_data_class(integer) // Create declaration for class named integer_data.
    `channel(`get_data_class_name(integer)) // This will create channel dealing with data class named integer_data.

    `define_data_class(string) // Create declaration for class named string_data
    `channel(`get_data_class_name(string)) // This will create channel dealing with data class named string_data.

    If above one is difficult to understand, then see this simple one.
    `define DATA_TYPE integer


  3. Wow,,,
    I never paid this much attention to SV macros and so I was unaware of these usages.
    Thanks a lot :)