Emulate ICs In Python

Most people who want to simulate logic ICs will use Verilog, VHDL, or System Verilog. Not [hsoft]. He wanted to use Python, and wrote a simple Python framework for doing just that. You can find the code on GitHub, and there is an ASCII video that won’t embed here at Hackaday, but which you can view at ASCIInema.

Below the break we have an example of “constructing” a circuit in Python using ICemu:

dec = SN74HC138()
sr1 = CD74AC164()
sr2 = CD74AC164()
mcu_pin = OutputPin('PB4')

sr1.pin_CP.wire_to(dec.pin_Y0)
sr2.pin_CP.wire_to(dec.pin_Y1)
sr1.pin_DS1.wire_to(mcu_pin)
sr2.pin_DS1.wire_to(mcu_pin)

print(dec.asciiart())
     _______
   A>|- U +|>Y7
   B>|-   +|>Y6
   C>|-   +|>Y5
 G2A>|-   +|>Y4
 G2B>|-   +|>Y3
  G1>|+   +|>Y2
  Y0<|-___+|>Y1

Note that the + and – signs show the current state of the pin.

Useful? Depends. If you are using Python anyway or trying to integrate with other Python code — or even another language that binds with Python — it might be useful. On the other hand, if you don’t have a reason to use Python, you will probably get more help, examples, and jobs by learning Verilog or another conventional hardware definition language. If you insist, you might consider using Python that can synthesize to an FPGA, at least.

16 thoughts on “Emulate ICs In Python

    1. Vivado!!! I use Xilinx ISE,

      For newbies I always recommend Altera Quartus for a number of reasons –

      The chips FPGA/CPLD are not a lot different in cost
      Xilinx hardware programmer $35 – Altera hardware programmer $5
      Xilinx ISE (code environment) requires a hard to get registration key – Altera Quartus runs from install
      Xilinx ISE is hard to use and generates a lot of confusing warnings/errors – Altera is easier to use.

      Once your on your feet then you will be able to appreciate some of the advanced features of the Xilinx ISE but start with Altera Quartus first.

  1. Why would anyone use this instead of one of many modeling and simulation packages? For example LTSpice, Proteus, Yenka, Electronics Workbench, Crocodile Clips to name the few I used. Why would I write my own component descriptions when there are models ready to use in spice format?

    1. I dunno about this Python thing, but SPICE is a PAIN when it comes to digital – it’s an analog user-land in SPICE. Taming transients in SPICE takes a lot of experience. But it can be done, and often it is the best way to solve problems via simulation (given proven device models of-course). LTSpice has logic libraries you can freely download and install from the LTSpice Yahoo group (yup, it’s still on Yahoo Groups, and IMO that’s OK for now).

  2. Hi, author here.

    I created this mainly because I wanted to explore the problem domain, but also because Verilog-based simulator seem to be used to design circuits. That’s not what I want. I want a tool to help me debug the software I’m flashing on MY MCU. I want to plug that software directly into a simulated circuit. I’ve updated the README to add clarity: https://github.com/hsoft/icemu#comparison-with-verilogvhdl

    Also, I don’t use proprietary software, so that limits my choices a bit. The world of Verilog simulators seems to be dominated by proprietary software. Thanks for the reference to PySpice, I didn’t know about it. I’m looking it up now.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.