Is binary counter Spice model available?

Sorry i messed up and did not attached the project!
Here it is.
Presentation.zip (5.3 KB)

Hey, Daniel. I looked at your project and it seems like what @holger stated above is indeed the issue. Not only is this true, but the D-FlipFlop subciruit you found (link here) is using default-parameter BSIM3 MOSFETs to produce the logic gates inside. I can see a bunch of problems with those kinds of NAND gates connected in the criss-cross pattern required to produce a flip-flop. I can get it to run stable in a different simulator, but the edges are very messy so something is definitely wrong here.

I suggest using the behavioral model @holger posted above. Like he said, you need to create a symbol to utilize his .subckt model within your project. Based on your initial project, it seems like you have a good handle on how to navigate the nuances of KiCad simulation so I think this will also be something you can figure out without too much hand-holding. HINT: the first step is to extract his .subckt / .ends section into its own .LIB file to reference and use with your symbol.

Also, what physical IC are you planning on using for the counter and also what is the part# for the MOSFET you want to use? The way your MOSFET is connected is going to depend heavily on its threshold voltage. Therefore, the specific part is important and you would need to find an exact model for it so your simulation output matches closer to the real life output. Alternatively, may I suggest connecting the output like this instead?

The output will switch between 3.3V and 1.65V. If you need it to switch between 0V and 1.65V then you need to do something similar, but with a P-channel MOSFET. This configuration is less dependent on threshold voltage and you won’t get much of your output swing within the MOSFET’s linear region of operation. Please ignore these comments if I’m not correctly assuming what you’re trying to acheive on the output.

Finally, a couple general suggestions.
1.) Define the rise and fall times for your voltage pulse sources. Don’t leave them at 0.
2.) If you’re primarily doing simulation, use “meg” instead of M on your schematic so you don’t have to keep checking the box in the simulator to auto-fix it for you.
3.) Be careful with your voltages. You’re powering your counter with 3.3V, but your clock source is 5V. The simulator won’t show any errors or smoke coming out of your counter, but you will likely smell it in real life!

1 Like

BTW, note that this D to A converter design relies on the outputs of the counter generating 0V for zero, and Vdd (or at least a known fixed voltage) for one. If the outputs are CMOS, this will be the case, but if you have chosen say BJTs for the counter implementation, then this will not be the case.

2 Likes

Hello Ste!
Your advices are really helpful, and i can not thank enough for your suggestions.
I downloaded the file which @holger posted above and i copy pasted the .subckt part to a new file with .lib extension. I managed to make a component that matches to this library, or i tried to.
Firstly i only want to test and make the circuit in Kicad.
I’m really new on this filed of science. And in @holger file there is a bunch of model. (d_pullup, d_pulldown, dac_bridgem adc_bridge). Cerainly i got a bunch of errors in the simulation, and i think because Kicad miss these models .
I know that the name follows the subckt and after that there is the pinouts.
In this subckt the pins are: updown, clock, out_b2, out_b1, out_b0
i searched in the ps reference guide and i found the .model section but i don’t know how to utilize this. I know i miss a lot of knowledge sorry for that, but we didn’t study this type of field at the university and i search a lot to grab the fundamentals.
I know i have to create components which matches with the .models which the file calls. But I’m sure i miss something.
My questions are:
How can i read how the .model component looks like from the file which @holger sent?
What these line does:
Xcount ud c1 ob2 ob1 ob0 3bcounter
.model state 2 d_state(state_file = “state-3bit-count.in”)

Again thank all the help, you already help a lot and suggested me some really handy information THANK YOU!

In real life i would like to use sn74ls93n 4bit binary counter IC and for the MOSFET i do not certainly know at this point but i’ll figure it out!

PS: I attached picture about the component i created for the subckt and the lib i created for it. Do you think it is correct for this type of subckt?
If you can take a look and help a little bit more i would really appreciate that! If you don’t have much time for this no problem ! You already helped a lot!

Best Regards,
Daniel

Schematic


Simulated error1

Simulated error2

Simulated error 3

The subckt .lib file

Note what I wrote above about about output levels. The LS93 is really old and BJT logic. At least put a CMOS buffer in between so that you get the full swing between 0 and Vcc for the levels to the resistor tree.

2 Likes

Hello @kenyapcomau !
You are right!
Do you think CD4026 is suitable for this parameters?
Best regards
Daniel
EDITED: sorry i see this is not a binary counter it is a decade counter. I’ll note what you wrote thank you again!

I see two issues here. There might be more, but these are what stand out.

First, your lib file looks incorrect. You have non-commented lines both before and after the .subckt / .ends commands. It should look exactly like this:

*** counter
.subckt 3bcounter updown clock out_b2 out_b1 out_b0
* Define a simple 3 bit up/down counter that counts clk edges.
* Digital outputs are on b2 b1 b0.
*   inputs  clock   reset     outputs (all digital)
a0  [upd]  clk    n_zero   [b2 b1 b0]   state2

.model state2 d_state(state_file = "state-3bit-count.in")

* Digital "one" and "zero"
a1 n_one pullup1
.model pullup1 d_pullup(load = 1pF)
a2 n_zero pulldown1
.model pulldown1 d_pulldown(load = 1pF)

* Convert the digital outputs to analog
a3 [b2] [out_b2] dac1
a4 [b1] [out_b1] dac1
a5 [b0] [out_b0] dac1
.model dac1 dac_bridge(out_low = 0 out_high = 5 out_undef = 2.5)
* Convert the analog inputs to digital
a6 [clock] [clk] adc1
a7 [updown] [upd] adc1
.model adc1 adc_bridge(in_low = 0.3 in_high = 3.5)
.ends
*** end counter

Second, notice the .subckt line. The order of the node names matters here. So you need to match up the node order with the pin numbers of the symbol. The “Alternate node sequence” option in the SPICE settings for the symbol lets you redefine this too, but since you’re making the symbol…might as well just make the symbol match. Right now it looks like clock and up/down are swapped on your symbol because it shows clock as pin 1 and updown as pin 2.


If you still have issues, please post the project again and I’ll take another look at it. Please include the symbol library this time too.

Lastly, regarding the resistor DAC and CMOS outputs. I suggest using the CD74HCT93E which is drop-in pin and voltage compatible with the SN74LS93N, but the outputs are CMOS. That should mitigate the issue with the DAC, such that your simulation results would more closely match up with the real-life voltage output.

Actually, now’s a good time to mention that these counters are asynchronous, so the stair-step pattern on the output of the DAC is going to have glitches on the transitions. This won’t show up in the simulation because @holger 's model is based on the XSPICE state-machine which is synchronous. Therefore, if this discrepancy is a problem for you, then you need to match the real-life device with a synchronous counter such as the CD74HCT161E or CD74HCT193E, or something similar.

2 Likes

In addition to what Ste said, this looks like an installation problem. An error message like
Unknown model type adc_bridge - ignored
tells me that the XSPICE extension has not been installed. This would be however necessary to run my counter example. Please check for a file digital.cm in your KiCad/ngspice installation.

2 Likes

Hello @Ste
Thank you again for your help and all the others help too. You are all the real hero here. @holger @kenyapcomau .
I changed the pinout like you said and make the .lib file like you posted above.
MOST of the error messages are disappeared! Hurray!
But sadly i could not solved all of them.
It seems it still has some problem.
“Error on line 6 :
mu1 net-r1-pad1 clk b2 b1 b0 3bcounter”
“Warning: singular matrix: check nodes net-r1-pad1 and net-r1-pad1
Maybe still with the pads/pins?
I tried to supply it with VDD directly and through a resistor. I attached the picture the results. I connected to the gnd the outputs through resistors.
Now i only want to make this subckt work and then i try to implement it in the circuit i need.
And in the end of the simulation message it says:
“Warning - approaching max data size: current size = 603,656 MB, limit = 399,324 MB”
Maybe because of the problems i make everytime. But if not, is there a way to reduce the data size ? Is it a real problem? Or it is just a problem that other problem casuses?
I attached the whole project like you asked @Ste (Hope you understand the sym-lib-table when you mentioned the symbol library)
Without resistor on the updown pin.

With resistor on the updown pin.

Shematic

The project Zip
behav_counter.zip (9.0 KB)

Thank you for the answers!
Best regards!

OK, you’re going to feel very stupid about this, but let’s get it out of the way first:

The 2nd problem I found is that you have the “Spice_Primitive” set to M instead of X. This is selected in the Spice Model Editor window where you select between Diode, BJT, JFET, etc. I have a feeling you accidentally scroll-wheeled from Subcircuit to MOSFET, because that’s how you configure M instead of X.

Lastly, there is an additional problem that is not your fault and I think your example here helped discover a KiCad integration bug with ngspice. I’ll ping @holger just in case he’s not aware or if he has a better workaround or solution.

So, after fixing the first two problems I now get this error:

Instance: a.xu1.a0   Message: 
ERROR
D_STATE: failed to open state file.

image

It cannot find the .in file. I did some experimenting and It turns out that when trying to reference nested files within libraries, the software still thinks it’s in the C:\Program Files\KiCad folder. I was able to do two separate workarounds. The first is to put the absolute path name for the .in file within the .lib file as such:

.model state2 d_state(state_file = "C:\Users\Ste\Documents\KiCad\behav_counter\state-3bit-count.in")

Alternatively, I used the original .lib file and copy/pasted the.in file into C:\Program Files\KiCad where it had no trouble finding it. I know you’re on Linux instead of Windows, but if you run into the same problem on Linux, you should still be able to follow similar workarounds with using Linux pathnames instead of Windows.

After fixing these three problems, the simulation runs correctly and I get the expected output.

1 Like

Hello @Ste!
I feel myself really stupid, that was an amateurish mistake. Sorry for that stupid mistake i was in rush and i dont make enough time to double check it.
I corrected the mistakes you mentioned but the last one is a little bit different. I hard “coded” the path folder in the .in file but didn’t work and i tried to copy paste it to the correct folder but did not work as well :frowning: .

The error message:

File path “hardcoded” in the .in file.

Copy-paste

I’m begin to feel it is what @holger mentioned above. Because i get the model error messages.
It looks like Kicad know where is the .in file but can’t understand what’s in it.
I checked the ngspice files and got the digital file. Is it what we are looking for?
Maybe the problem is that I’m using ngspice-31 and if i know correctly the newest version is 32?
Maybe Kicad still can’t reach the file. I’ll try to find more way to correct the file path, but now I’m lack of the time very sorry for this. If anything other comes to your mind i’m very glad to hear that.

Sorry for any mistake!
Best regards!

Haha. Don’t worry about the missing connection mistake. All of us make mistakes like that eventually.

Ehhhh…I am also running ngspice-31…but I agree with you and @holger that the issue is related to ngspice not finding the XSPICE extensions. I notice from the screenshots that I am running KiCad 5.1.5 and you are running 5.1.4…so maybe there’s an issue there? You can try reinstalling the entire KiCad files to the latest version…I believe it’s 5.1.6…maybe that will help??? Other than that, I don’t have any other advice besides trying it on a different computer and see if you get the same problem. Just in case, I repackaged and attached my version of your project which runs perfectly on my computer as long as I put the .in file in the KiCad directory, as we discussed earlier.

behav_counter_ste.zip (7.8 KB)

Hello @Ste
Long time no see!
I have managed to update KiCad from 5.1.4 to 5.1.6!! YESS!
Thanks to @kenyapcomau!

But it seems that i have made a big mistake…
I got excited how easily i can install packages, and i saw an ngspice package and i tought i can install it,
Oh how silly i was.
The package i downloaded and installed was:
https://packages.debian.org/buster-backports/libngspice0

After this installation i have ran the simulation again and got this result.

Circuit: KiCad schematic
Error on line 0 :
a.xu1.a0 [ xu1.upd ] xu1.clk xu1.n_zero [ xu1.b2 xu1.b1 xu1.b0 ] xu1:state2
MIF-ERROR - unable to find definition of model xu1:state2
Note: can’t find init file.


** ngspice-32 shared library
** Creation Date: Sat May 16 05:21:57 UTC 2020


Error: circuit not parsed.

I think i messed up my installed ngpsice. After i typed apt search ngspice in the terminal i got the following result:

pi@raspberrypi:~ $ apt search ngspice
Sorting… Done
Full Text Search… Done
chiark-scripts/stable,stable 6.0.4 all
chiark system administration scripts

easyspice/stable 0.6.8-3 armhf
Graphical frontend to the Spice simulator

gspiceui/stable 1.1.00+dfsg-2+b1 armhf
Graphical user interface for gnucap and ngspice

gwave/stable 20170109-1 armhf
waveform viewer eg for spice simulators

libngspice0/buster-backports,now 32.2+ds-1~bpo10+1 armhf [installed,automatic]
Spice circuit simulator - library

libngspice0-dev/stable,stable 30.2-1 armhf
Spice circuit simulator - development files for libngspice

ngspice/stable 30.2-1 armhf
Spice circuit simulator

ngspice-dev/stable,stable 30.2-1 armhf
Spice circuit simulator - development files for ngspice and tclspice

ngspice-doc/stable,stable 30.2-1 all
Documentation for the NGspice circuit simulator

tclspice/stable 30.2-1 armhf
NGSpice library for Tcl

Should i reinstall ngspice?
All advice will be appreciated.
I dont wan to overwhelm you after this long time!
Best regards,
Daniel

Sorry, I’m not sure I can help here. I don’t know enough about KiCad on Linux. I know on Windows, KiCad installs its own self-contained ngspice library. Does that happen on Linux?? If it doesn’t, then you could be right that you messed something up with the package installation. If it does, then the problem is not because you installed any extra packages, but looks to be just the same problem you were having before.

This ngspice should be compatible with Eeschema:
http://ngspice.sourceforge.net/ngspice-eeschema.html

Did you install the package by downloading the .deb and using dpkg? This is not the recommended way of installing software, despite the fact that you got away with this installing the kicad-5.1.6 package. Using dpkg may not install dependencies. You should use apt install libngspice0 to make sure it pulls in all the required dependencies. Without the dependencies who knows what the behaviour will be, it’s indeterminate.

To check that all the dependencies are present, do:

$ ldd /usr/lib64/libngspice.so.0.0.0

substituting the correct path for where libngspice0 lives on your system. Typical correct output:

linux-vdso.so.1 (0x00007ffe761c5000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f5669124000)
libtinfo.so.6 => /lib64/libtinfo.so.6 (0x00007f5668ef6000)
libm.so.6 => /lib64/libm.so.6 (0x00007f5668bbe000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f566899f000)
libfftw3.so.3 => /usr/lib64/libfftw3.so.3 (0x00007f5668597000)
libreadline.so.7 => /lib64/libreadline.so.7 (0x00007f5668348000)
libgomp.so.1 => /usr/lib64/libgomp.so.1 (0x00007f5668110000)
libc.so.6 => /lib64/libc.so.6 (0x00007f5667d55000)
/lib64/ld-linux-x86-64.so.2 (0x00007f5669c4e000)

If any of the lines are not found or some other error, you don’t have all the dependencies.

1 Like

BIG LIKE! :+1:
I didn’t know what package i looking for but it seems it was libngspice0.
As you said i just downloaded it and with double click (not through command window) i installed the deb file.
It was a mistake, but i deleted and reinstalled kicad and libngspice and now i finally at the start where is was long ago… but hey it is progress :sweat_smile:
I make a lot of mistakes nowadays sorry for asking so much.
Thank you!
This communitiy is really a lifesaver and very responsive!
Best regards,
Daniel

Hello everyone!
Again me!
I’m back to this project after a long pause.
I tried everything you have suggested but still got this type of erre message.
Unknown model type adc_bridge - ignored
Long story short for those (who dont remember because it was a long long time) i tried to simulate a circuit that has a binary ocunter.
We have solved the problem (at least most of it) and for @holger it is working but sadly not for me.
You have said maybe the problem is with the version, but i do not think so.
I’m working on a Raspberry PI 4 and it is not runnign arm64 architecture.
@holger said maybe the problem is that xspice extension has not been installed.
I ran the following command:

sudo find / -name digital

And got the following result:

/usr/share/scratch/Media/Costumes/Letters/digital
/home/pi/ngspice32/ngspice-32/tests/xspice/digital
/home/pi/ngspice32/ngspice-32/src/xspice/icm/digital
/home/pi/ngspice32/ngspice-32/examples/digital
/home/pi/ngspice32/ngspice-32/release/tests/xspice/digital
/home/pi/ngspice31/ngspice-31/tests/xspice/digital
/home/pi/ngspice31/ngspice-31/src/xspice/icm/digital
/home/pi/ngspice31/ngspice-31/examples/digital
/home/pi/ngspice31/ngspice-31/release/tests/xspice/digital

I noticed that i haven’t got any ngspice within Kicad folder.
Then i ran the following command to search for ngspice.

sudo find / -name ngspice

And got the following result:

/usr/local/share/ngspice
/usr/local/include/ngspice
/usr/local/bin/ngspice
/home/pi/ngspice32/ngspice-32/src/include/ngspice
/home/pi/ngspice32/ngspice-32/visualc/src/include/ngspice
/home/pi/ngspice32/ngspice-32/release/src/include/ngspice
/home/pi/ngspice31/ngspice-31/src/include/ngspice
/home/pi/ngspice31/ngspice-31/visualc/src/include/ngspice
/home/pi/ngspice31/ngspice-31/release/src/include/ngspice
/home/pi/ngspice31/ngspice-31/release/src/ngspice

Question 1 : Do i have digital.cm file, because i’m not sure that digital file is what holger mentioned within ngspice.
Question 2 : Can it cause problem, that i haven’t got ngspice within Kicad file?

Thank you for listening and helping.

Best regards,
Daniel

The XSPICE code models are delivered by
analog.cm
digital.cm
spice2poly.cm
table.cm
xtradev.cm
xtraevt.cm

These files are shared libraries (or dll on Windows). You will have to have them, or the code models are not available. The simulator is found in libngspice.so. No other ngspice files are required.

In my Linux installation (SUSE 15.1) these code model files (a 64bit installation) are to be found in /usr/lib64/ngspice/

libngspice.so is in usr/lib64/

kicad, eeschema and others are in /usr/bin/

If one has a 32 bit installation, instead of lib64 one may find lib.

That sounds like you’ve have installed your own version of spice. Official packages usually don’t install to /usr/local. Be sure that whichever executable is invoked is really the version you want. I once wasted hours recompiling a program because I failed to notice that I had a very old version of the program in /usr/local/bin.