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.
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.
@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)
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.
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 “==”
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.
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: