GithubHelp home page GithubHelp logo

maehw / wokwi-lookup-table-generator Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 4.0 63 KB

Generator for wokwi schematics that implement lookup tables in conjunctive normal form (CNF), i.e. with AND and OR gates

License: GNU General Public License v3.0

Python 100.00%
wokwi wokwi-generator binary-logic circuit-design combinatory-logic digital-design hardware-description-language lookup-table lookup-tables quine-mccluskey

wokwi-lookup-table-generator's Issues

Add support for ABEL (Advanced Boolean Equation Language) truth table design description

"ABEL (Advanced Boolean Equation Language) allows you to enter behavior-like descriptions of a logic circuit. ABEL is an industry-standard hardware description language (HDL) that was developed by Data I/O Corporation for programmable logic devices (PLD). There are other hardware description languages such as VHDL and Verilog. ABEL is a simpler language than VHDL which is capable of describing systems of larger complexity.

ABEL can be used to describe the behavior of a system in a variety of forms, including logic equations, truth tables, and state diagrams using C-like statements. The ABEL compiler allows designs to be simulated and implemented into PLDs such as PALs, CPLDs and FPGAs."

https://web.archive.org/web/20071012234417/http://www.seas.upenn.edu/~ese201/abel/abel_primer.html#Truth

As mentioned on the tinytypeout Discord, it would be nice to support ABEL truth table syntax for inputs.

I would add a preprocessing stage that converts ABEL truth table syntax to the expected JSON format (see demo design progects).

generate.py failure case

This input file causes generate.py to crash:

{
  "version": 1,
  "description": "Generates patterns.",
  "inputs": ["t", "u"],
  "outputs": {
    "A": [1, 0, 0, 0],
    "G": [0, 0, 0, 1]
  }
}

Here is the output from the failing run:

$ python3 generate.py -v -f logic.json -o logic.wokwi
[    INFO] Log level: INFO
[    INFO] Data is read from input file 'logic.json'
[    INFO] Inputs:     2 ['t', 'u']
[    INFO] Outputs:    2 ['A', 'G']
[    INFO]   Output A: [1, 0, 0, 0]; ones: [0]
[    INFO]   Output G: [0, 0, 0, 1]; ones: [3]
Traceback (most recent call last):
  File "/home/osboxes/src/wokwi-lookup-table-generator/generate.py", line 441, in <module>
    logic_meta[output]["qm_terms_raw"] = list( q.simplify(logic[output]) ) # convert set into list (to allow indexing)
  File "/home/osboxes/venvs/py/lib/python3.9/site-packages/quine_mccluskey/qm.py", line 130, in simplify
    return self.simplify_los(ones, dc)
  File "/home/osboxes/venvs/py/lib/python3.9/site-packages/quine_mccluskey/qm.py", line 193, in simplify_los
    essential_implicants = self.__get_essential_implicants(prime_implicants, set(dc))
  File "/home/osboxes/venvs/py/lib/python3.9/site-packages/quine_mccluskey/qm.py", line 419, in __get_essential_implicants
    perms[t] = set(p for p in self.permutations(t) if p not in dc)
  File "/home/osboxes/venvs/py/lib/python3.9/site-packages/quine_mccluskey/qm.py", line 419, in <genexpr>
    perms[t] = set(p for p in self.permutations(t) if p not in dc)
  File "/home/osboxes/venvs/py/lib/python3.9/site-packages/quine_mccluskey/qm.py", line 532, in permutations
    if value[i] == '0' or value[i] == '1':
IndexError: string index out of range

If I change the line for "A" to:

    "A": [0, 0, 1, 0],

then generate.py succeeds:

$ python3 generate.py -v -f logic.json -o logic.wokwi
[    INFO] Log level: INFO
[    INFO] Data is read from input file 'logic.json'
[    INFO] Inputs:     2 ['t', 'u']
[    INFO] Outputs:    2 ['A', 'G']
[    INFO]   Output A: [0, 0, 1, 0]; ones: [2]
[    INFO]   Output G: [0, 0, 0, 1]; ones: [3]
[    INFO] Calculated CNF for output A: t~u
[    INFO] Calculated CNF for output G: tu
[    INFO] Estimated parts usage:
[    INFO]     *   4 buffers (for the inputs and the outputs)
[    INFO]     *   2 NOT gate(s) (for the negated inputs)
[    INFO]     *   2 two-input AND gate(s), in max. 1 stages
[    INFO]     * number of two-input OR gate(s) not estimated yet
[    INFO] 
[    INFO] Connecting inputs with first stage of AND gates for every output.
[    INFO] Connecting input inside the wokwi design to first AND gate stage (later used for output A)...
[    INFO] All AND gates required in the first AND gate stage for output A: [[0]]
[    INFO] Connecting input inside the wokwi design to first AND gate stage (later used for output G)...
[    INFO] All AND gates required in the first AND gate stage for output G: [[1]]
[    INFO] Connecting inputs with first stage of AND gates for every output completed.
[    INFO] 
[    INFO] Merges all first stage AND gates down to a single 'root' AND gate (for every term of every output)
[    INFO] Single AND gate #0 does not need to be merged.
[    INFO] Remaining AND gates (final stage): [0] for the output A to be connected to OR gates
[    INFO] Single AND gate #1 does not need to be merged.
[    INFO] Remaining AND gates (final stage): [1] for the output G to be connected to OR gates
[    INFO] Merges of all first stage AND gates down to a single 'root' AND gate completed.
[    INFO] 
[    INFO] Combining mini terms: Create first stage of OR gates (in order to OR all terms for every output) 
[    INFO] and connect the AND gate from the previous stage (=the final AND stage) to it.
[ WARNING] Single mini term for output A: AND gate output (AND gate #0)
[ WARNING] Creating a dummy OR gate (which does not need to be then merged down).
[    INFO] First stage of OR gates (connected to AND gates): [0] for output A
[ WARNING] Single mini term for output G: AND gate output (AND gate #1)
[ WARNING] Creating a dummy OR gate (which does not need to be then merged down).
[    INFO] First stage of OR gates (connected to AND gates): [1] for output G
[    INFO] Done combining mini terms (AND gates) by first stage of OR gates.
[    INFO] 
[    INFO] Merges all first stage OR gates down to a single 'root' OR gate and connect them to the output buffers
[    INFO] Performing the merges of all OR gates for output A
[    INFO] Number of OR gates to be merged: 1, i.e. [0]
[    INFO] Only single OR gate, therefore directly connect it to the output buffer
[    INFO] Identified #0 as final OR gate for the output A to be connected to output buffer
[    INFO] Performing the merges of all OR gates for output G
[    INFO] Number of OR gates to be merged: 1, i.e. [1]
[    INFO] Only single OR gate, therefore directly connect it to the output buffer
[    INFO] Identified #1 as final OR gate for the output G to be connected to output buffer
[    INFO] Max AND gate stages: 1
[    INFO] Max  OR gate stages: 1
[    INFO] Finished the wokwi design!
[    INFO] Writing final wokwi design file 'logic.wokwi'...

(PS: Love the idea of this tool. Thank you for writing it!!)

Tool to generate the JSON input file from the truth table

It would even be easier if nobody would have to write the JSON input files themselves but just add columns and all values in the truth table and then generate the JSON input file from it.

Any ideas & implementations are welcome!

Replace warnings about potential endless loops by timeouts and error messages

The generator may spit out several warning messages like the following:

[ WARNING] Entering potential endless loop (trying to merge AND gates)

This has been added as the generator is known to hang for some designs. The root cause is currently unknown - as it simply has not been analyzed yet.

Design to reproduce: https://github.com/maehw/wokwi-lookup-table-generator/blob/main/demos/4bit-popcount.logic.json

This may be helpful, but is also annoying as the warning messages will appear for every design.

A better way to detect endless loops would be to use timeouts (wait for some time and abort when the loop has not finished yet). Those timeouts may be dynamic as they should grow with the design complexity (or input/output size). But we could also use a static timeout as the machines to run the generator on should not be too low.

Add support for automated in-circuit verification

Wokwi is a great tool. It does not only allow to simulate digital designs and then run some magic to get your own ASIC design out of it. It also allows to write and simulate software on an embedded target such as an Arduino Nano or Arduino Mega.

The designs generated by wokwi-lookup-table-generator are pure combinatorial logic designs. One entry from the truth table (combination of input signals, ones and zeros) directly generates a specific combination of logic levels on the outputs (ones and zeros). This makes testing the designs generated by the generator quite easy:

  • The test stimuli can be used from the truth table which is an input for the generator
  • The expected outputs of the digital design can also be taken from the truth table

The idea for automated verification is currently experimented with here: https://wokwi.com/projects/342092183606657619

Basic concept:

  • Add an Arduino to the wokwi sketch with plenty of I/Os, best choice seems to be the Arduino Mega at the moment
  • In the sketch.ino: Configure input and output pins in the setup() routine using the pinMode() function
  • Also configure a serial interface by calling Serial.begin(); make sure that the RX and TX pins are not used as input or outputs, but stay reserved to control the inputs of your digital design/ read the outputs from it
  • Add support for serialMonitor in your Wokwi design
  • Set the inputs of your digital design using a series of calls to digitalWrite()
  • Read back the outputs of your digital design using a series of calls to digitalRead()
  • Verify that the measured output signals match the logic levels as definged by the output signals in the truth table
  • Generate outputs and a test report using the serial console (several calls to Serial.print())

This can all be automated.

I suggest to add a command line flag -t/--in-circuit-test to switch between the normal design generation and a verification design generation with the additional Arduino and the additional connections required for in-circuit verification.

Add command line switch aka flag to add template parts of Tiny Tapeout

This generator has been born as part of Tiny Tapeout 1.
Currently Tiny Tapeout 2 is going on: https://tinytapeout.com/

TinyTapeout is an educational project that makes it easier and cheaper than ever to get your digital designs manufactured on a real chip!

We ran TinyTapeout-01 in August and received 150 submissions in just a couple of weeks. Now we’re getting ready to open submissions for Tiny Tapeout 2!

The wokwi template for Tiny Tapeout 2 can be found here: https://wokwi.com/projects/339800239192932947

It would be useful if the generator also generated the following default parts (and their interconnection):

"parts": [
   {
     "type": "wokwi-dip-switch-8",
     "id": "sw1",
     "top": -178.4,
     "left": -102.9,
     "rotate": 90,
     "attrs": {}
   },
   {
     "type": "wokwi-clock-generator",
     "id": "clock1",
     "top": -278.4,
     "left": -134.4,
     "attrs": {}
   },
   { "type": "wokwi-gnd", "id": "pwr2", "top": -76.8, "left": 892.2, "attrs": {} },
   { "type": "wokwi-vcc", "id": "pwr1", "top": -229.64, "left": -115.2, "attrs": {} },
   {
     "type": "chip-input-8-pins",
     "id": "chip1",
     "top": -190.98,
     "left": -4.8,
     "attrs": { "verilogRole": "input" }
   },
   {
     "type": "chip-output-8-pins",
     "id": "chip2",
     "top": -190.98,
     "left": 686.4,
     "attrs": { "verilogRole": "output" }
   },
   {
     "type": "wokwi-7segment",
     "id": "sevseg1",
     "top": -183.24,
     "left": 878.68,
     "attrs": { "common": "cathode" }
   },
   {
     "type": "wokwi-slide-switch",
     "id": "sw2",
     "top": -302.8,
     "left": -35.3,
     "attrs": { "value": "1" }
   },
   {
     "type": "wokwi-pushbutton",
     "id": "btn1",
     "top": -354.6,
     "left": -146,
     "attrs": { "color": "grey", "label": "Step", "bounce": "0" }
   }

The generated design should also be auto-connected to the "Digital Input Breakout" and "Digital Output Breakout" boards as this is where the digital design should go.

Improve layout of wokwi schematic diagram

The wokwi layout is generated automatically.

At the moment of writing, ...

  • ... the OR gates of every stage are only aligned horizontally. Their vertical position can be improved by moving them to a them to the half height (field "top") of the two predecessor gates.
  • ... the AND gates cannot even be distinguished by their stage. All AND gates are in the same horizontal position and just laid out one under each other. Both the vertical and horizontal position should be improved.

The goal is to have more tree-like structure in the layout that also helps to visually understand the schematic better. Best: at a first glance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.