How to pass a parameter to a C++ module


I am trying to learn how to pass parameters defined as .param into a C++ module but I can’t make it work. Please, see the simple example of the image.
Can anyone tell me how to do it or send an example?

I just used my own C++ code for my article writing project, one of the simulations here.

Check the QSPICE help file for the detail but you need to specify “data type” followed by a white-space, like below.
Then, these parameters will be placed in between IN ports and OUT ports when your resulting DLL called from the SPICE.


Did you add the input parameter float gain1=gain after creating C++ template?
When modifying the input, output, or input parameters, it is necessary to recreate the C++ template. However, there is a project by @RDunn that aims to eliminate this step using an external program ( QCodeGen – A C-Block Component Code Generator For QSpice - QSPICE - Qorvo Tech Forum). If you follow Mike’s instructions, you should recreate the template whenever you make changes to the schematic DLL block.

@RDunn Does your Code Generator handle this situation?

Thanks for your responses. Here is my file. When I re-create the C++ template the parameter gain1 does not appear on it. I am doing something wrong for sure.
I am at the basics with this so it is OK for me to re-create the template at this moment.

digital-module-parameter.qsch (3.3 KB)

Delete your 2nd attribute or add datatype to that.

This is what I am doing but it doesn’t work :slightly_frowning_face:
I am just trying to generate V(out)=gain*V(in) as a test
digital-module-parameter.qsch (3.3 KB)
digitalparam.cpp (1.3 KB)

I have also tried putting the datatype declaration below as you showed in the image but it doesn’t work either:

I download your .qsch in post /5, right-click on dll block, C++ Interface > Create C++ Template, and can get input parameter added in data declaration.

The problem of .qsch in post /3 is that, 2nd attribute is ROUT=1, and 3rd attribute is float gain1=gain, and in C++ template generation, it just ignored 3rd attribute and not declare that. Instance parameter ROUT=1 should be after input parameter in attribute order.
Therefore, for post /5 .qsch, order is correct as 2nd attribute is float gain1=gain and 3rd attribute is ROUT=1. This .qsch should create a correct template.

Thank you for your help. Now it is working fine.
I am posting here the correct files just in case anyone has the same issue in the future.
Thanks! :grinning:
digital-module-parameter.qsch (3.9 MB)
digitalparam.cpp (1.3 KB)

1 Like

Depends on what you mean by “handle.” :roll_eyes: It’s a definite improvement.

First, it catches some errors that QSpice doesn’t detect when generating code. For example,
here’s the QCodeGen summary for one of posted *.qsch.

*** Component Summary ***

Schematic Symbol:      X1
Component/DLL Name:    DigitalParam
Component Description: [no description]

*** Attribute Possibly Invalid : @Index= 3, Text=[ROUT=1]

Input Pin Count:       4
Input Attribute Count: 2
Output Pin Count:      4

Input Pin Details:
PinOrder:  0, PinName: in0       , PortType: Input, DataType: double
PinOrder:  1, PinName: in1       , PortType: Input, DataType: double
PinOrder:  2, PinName: in2       , PortType: Input, DataType: double
PinOrder:  3, PinName: in3       , PortType: Input, DataType: double

Output Pin Details:
PinOrder:  4, PinName: out0      , PortType: Output, DataType: double
PinOrder:  5, PinName: out1      , PortType: Output, DataType: double
PinOrder:  6, PinName: out2      , PortType: Output, DataType: double
PinOrder:  7, PinName: out3      , PortType: Output, DataType: double

Input Attribute Details:
TextOrder:  2, Content: float gain1=gain, DataType: double
TextOrder:  3, Content: ROUT=1, DataType: double

As for updating existing input/output/attributes, if the code was generated with QCodeGen, you can: (1) delete the existing generated parameters, (2) uncomment the code pattern that generated the parameters, (3) save the code as a template, and (4) generate code from the *.qsch using the newly saved template. Not fully automatic but not really that hard.

If the code wasn’t generated with QCodeGen, you can simply generate code (which, unlike QSpice, doesn’t overwrite the existing *.cpp) and copy/paste the needed definitions from the QCodeGen code window into your existing code.