There is a PWM available in KiCad/ngspice as a code model. It has been used in several of my examples at More simulation examples for KiCad/Eeschema/ngspice, for example in the Regulated boost converter or the Digital PWM amplifier.
ngspice supports direct simulation of digital Verilog blocks, which have been compiled with Verilator (see Ngspice, the open source Spice circuit simulator - Special features). This feature has not yet been used in conjunction with KiCad, but I don’t see any roadblocks.
Holger, thank you for your great work on ngspice. With Kicad as UI it becomes really user friendly.
Is there any chance, that you could do a tutorial on verliog / c integration in kicad?
I would like verify my power supply topologies with ngspice.
The boost converter examples are nice for basic tests. But i would like to simulate later my real control loop, that i would write in the best possible case in c.
By the way, I woun’t mind to write the pwm duty cycle generator in c.
The PWM generator is written in C (code model using XSPICE macros, see ngspice / ngspice / [902a62] /src/xspice/icm/digital/d_pwm). The cmpp processor then creates a plain C file, which is compiled together with ngspice and saved in the shared library digital.cm.
The Verilog interface d_cosim and the C interface d_process support only digital data transfer.
You may write your complete system as code model blocks in ngspice (analog, digital, mixed signal). These may be put into subcircuit models (individually or already connected as a complex model), loaded by KiCad/ngspice to provide system simulation.
The only deficit of this approach that I am aware of is that code models (currently) do not support small signal noise simulation (which is difficult to use in a mixed signal environment anyway).
Generate the new code model and add it to an existing code model library, e.g. xtradev. See ngspice manual chpater 26.3, and Ngspice, the open source Spice circuit simulator - XSPICE HowTo . The existing code models (we have already 70) may serve as examples.
Create a simple test bench (ngspice netlist) to check your development.
Replace the KiCad xtradev.cm with the newly generated extradev.cm which includes the new code model.
Create an Eeschema symbol.
Put the new code model into a ngspice subcircuit and attach it to the symbol.