Mixed mode simulation including C code and verilog

I attach a basic simulation combining standard spice models, Xspice models, verilog using yosys and c-code using the d_process method. It’s my way of giving back as I’ve been using Kicad professionally and privately since version 4.

The example is based heavily on The Easiest Way of Simulating C/C++ Code Together with an Analog & Digital Spice Simulation — ISOTEL. Hopefully between the two examples there should be enough for most people to understand what’s happening.

I only use Linux so the instructions will need modifying for Windows and possibly MacOS. Apologies for not being able to help with that. If anyone can get it running on those two, it would be appreciated.

The basic circuit is a simplified buck converter with a current controller. Switches are just switches with anti parallel diodes. No input/output capacitors, just voltage sources.

Target current is set in the buck_controller_msp430.c file and P and I gains are in the buck_fw_msp430.lib file.

You need Kicad 8.0.1, Yosys (tested with 0.23-6 on ubuntu), git and gcc.

To run, extract the files into a folder. Change to the folder and run “git clone GitHub - Isotel/mixedsim: Hardware Design Tool - Mixed Signal Simulation with Verilog” to get the Xspice models for yosys. Then run ./compile_all.sh. This will compile the c and verilog code. d_process.h has been copied from the ngspice source code.

Having done this, you can open the schematic, modify the control line “cd ~/Electronics/holger_sims/buck_msp430” with the folder that you put the files in and run the simulation as normal.

If you open Kicad from a terminal then you can monitor the stderr output as simulation runs.


A big thank you to all of the Kicad team! @holger I am always impressed and grateful for the help you give for how to use the simulation features on these forums. The improvements in ngspice over the last few years have also been very impressive. You’re welcome to take this sim and integrate it into your list of examples. You can also modify it as much as you want.

buck_example.zip (548.3 KB)


Thanks for posting.

I’ll look at it, but it may take some time.


@uros I believe that you’re the person behind Isotel.eu?

Thank you for your example c and verilog model. I hope you’re happy how I’ve used it to produce the example model in the first post above. Feel free to take it and use it to update your original example if you want a kicad example to go with your eagle one.

1 Like

Welcome and thank you for your effort for making a kicad example. I just recently started with kicad/ngspice integration and I am impressed by the progress.

As first I would create a hyper link to this use case.

I am looking at current directory, which is not the project folder but kicad start-up folder. So you had to use an absolute path, which does not make a real sense.

I am not sure if anybody needs ngspice to start like this, whenever I’ve used I’ve always started it from the project folder, so it would make sense that kicad would also change default directory to the project directory. Also python console starts in the same way, but probably we can get the KIPRJMOD variable there and change it, which is not possible in ngspice.

Not sure what is others opinion but I would change default directory to project root not “system default”.

Yes that’s correct. It needs an absolute path at the moment. Interestingly, you only need to change the working directory once per session. After the first run it remembers until you restart kicad.

I agree that the default working path should be the project directory. I haven’t got a GitHub account so haven’t started filing bugs.

I think that there’s also.a memory leak. After a few long runs kicad locks up and then closes.

Yes as it changes kicad default/working directory.

So I’ve changed the directory in the python console, but there is another bug, that the KIPRJMOD is only set once PCB is open, added comment to past issue (environment variable KIPRJMOD fails to refresh (at least in python console) (#5071) · Issues · KiCad / KiCad Source Code / kicad · GitLab); so after opening the pcb and changing it in python the ngspice works

import os
cd( os.getenv("KIPRJMOD") )

Paths and project specific directives should be in .spiceinit file in the project folder, so changing the folder would benefit also in reading this file. There additional source paths may be added to find modules from mixedsim etc.

Anyway I see running ticket, on which I’ve commented: 7.99 Send project directory name to ngspice (#16527) · Issues · KiCad / KiCad Source Code / kicad · GitLab

On Linux an easy work-around is to start the kicad <project> from a command line directly from the project directory; but that’s not the way users would use kicad on win*.