Output digital block behaving weirdly

Test_C.qsch (3.3 KB)
my_first_block.cpp (1.4 KB)

Hello,

I’m trying to replicate a C++ block tutorial, to get going with making some mixed mode simulation, however I get stuck on an issue where the output isn’t behaving correctly and nowhere close to the youtube example. https://www.youtube.com/watch?v=feyHJ9AIki0
It’s like Qspice is changing the output between clocks.

I don’t know if I have some hidden settings that need to be set, that isn’t disclosed in the tutorial, but for what I can tell I have the exact same setup.

I have also tried to set maximum timestep to 1n still looks nowhere close and of cource the simulation runs very slowly with that setting.

Do anyone have an idea on how to make it behave as intended? I have included the sim files.

Hi, Anton.

Looks like the output port data type is 64-bit integer in the schematic block and double in the C++ code. Try changing the port type to double in the schematic.

–robert

@RDunn That’s one problem, but it still doesn’t work as shown in the video. Every action should be taken at clock edge only. Well… I am not familiar with the method for the clock edge routine that Gregory implemented in his YouTube video.

Here is my version. Well, in the community, I recommend you read @RDunn Github “CBlock_Doc” basic article (a number of documents), or @physicboy Github projects (quite advance), or my Github (Device guideline, Ø-Device section)

Test_C.qsch (3.2 KB)
my_first_block.cpp (2.2 KB)

In template creation, you need to include struct template. This method is the most reliable way to save and load variable between each timestep iteration.

Thank you all!

Most appreciated to get this fast response. I also saw now that I made a typo, or more exactly I didn’t see an error, the youtube author fixed. I had " CLK != clk_state" instead of “==”

1 Like

I saw static variable is used in that example… @RDunn any concern about using static vs struct?

Well, the “static” keyword doesn’t really matter in this particular case (single compilation unit) so these are just global variables.

As long as there is only one instance of the c-block in the schematic, no problem. Personally, I highly recommend using per-instance data to ensure that multiple instances work properly.

1 Like

@RDunn

I am not sure if there is any particular meaning for that static, since it’s declared as global (not within function).

If it’s declared within function as local, then yes the difference is the value will be retained between function call.

By the way, just adding to what Rob said, keeping the variables with instance struct is also helping a lot when porting the code to different platform (or MCU) since all variables are already well organized

OK, I just remembered another issue with global variables: They are “initialized” only when the first step begins and not when subsequent steps begin. So, from the original posted code:

static bool clk_state = false;
static double Z1 = 0.0;
static double Z2 = 0.0;
static double A0 = -0.5;
static double A1 = 0.9;

Those variables will not be reset to the initialized values between multiple steps. That would probably be a problem, likely subtle and confusing.

1 Like