Compile wires "from, to" list to working C code

mentalblood 1c59bdc6db updated readme 3 years ago
lib 25a6c1041d added and tested wrapped_sum_8 3 years ago
.gitignore 8d5af1d077 small refactoring 3 years ago
README.md 1c59bdc6db updated readme 3 years ago
core.py 29f34664bf made tests part of compiled file smaller 3 years ago
interface.py 46b91388f8 finally moved to new compilation technique 3 years ago
make.cmd c82350eaf9 updated readme 3 years ago

README.md

📦 LSC — Logic Scheme to C compiler

Features

  • Written in Python 🐍
  • Takes JSON logic schemes representations 👀
  • Checks JSON representation for errors 🧐
  • Requires only wires "from, to" description ➡️
  • Works with nested schemes (i.e defined scheme can be used in other schemes in file as element) 📁
  • Scheme tests can be provided ✔️
  • High full (JSON - C - executable) compilation speed 🚀

Usage

usage: interface.py [-h] -i INPUT [-t TARGET] -l [LINK ...] -o OUTPUT

Compile logic schemes JSON representations to C file

optional arguments:
  -h, --help            show this help message and exit
  -i INPUT, --input INPUT
                        Path to file with target function
  -t TARGET, --target TARGET
                        Name of function to compile
  -l [LINK ...], --link [LINK ...]
                        Paths to (input files) / (directories containing input files) with required functions descriptions
  -o OUTPUT, --output OUTPUT
                        Path to output file

Editor ❗

You can use this web-based editor to create and export schemes to JSON compatible with this compiler

Examples

XOR

{
	"xor": {
		"wires": [
			{
				"from": "INPUT_2[1]",
				"to": "NOT_2[1]"
			},
			{
				"from": "INPUT_1[1]",
				"to": "NOT_1[1]"
			},
			{
				"from": "INPUT_1[1]",
				"to": "OR_1[1]"
			},
			{
				"from": "INPUT_2[1]",
				"to": "OR_1[2]"
			},
			{
				"from": "NOT_1[1]",
				"to": "OR_2[1]"
			},
			{
				"from": "NOT_2[1]",
				"to": "OR_2[2]"
			},
			{
				"from": "OR_1[1]",
				"to": "AND_1[2]"
			},
			{
				"from": "AND_1[1]",
				"to": "OUTPUT_1[1]"
			},
			{
				"from": "OR_2[1]",
				"to": "AND_1[1]"
			}
		],
		"tests": [
			{
				"inputs": [
					0,
					0
				],
				"outputs": [
					0
				]
			},
			{
				"inputs": [
					1,
					1
				],
				"outputs": [
					0
				]
			},
			{
				"inputs": [
					1,
					0
				],
				"outputs": [
					1
				]
			},
			{
				"inputs": [
					0,
					1
				],
				"outputs": [
					1
				]
			}
		]
	}
}

command for compile:

python interface.py --input lib\xor.json --target xor --link lib --output example.c

output code: godbolt 📜

Full bit adder

{
	"sum": {
		"wires": [
			{
				"from": "INPUT_1[1]",
				"to": "AND_1[1]"
			},
			{
				"from": "INPUT_1[1]",
				"to": "AND_2[1]"
			},
			{
				"from": "INPUT_1[1]",
				"to": "OR_3_1[1]"
			},
			{
				"from": "INPUT_1[1]",
				"to": "AND_3_1[1]"
			},
			{
				"from": "INPUT_2[1]",
				"to": "AND_1[2]"
			},
			{
				"from": "INPUT_2[1]",
				"to": "AND_3[1]"
			},
			{
				"from": "INPUT_2[1]",
				"to": "OR_3_1[2]"
			},
			{
				"from": "INPUT_2[1]",
				"to": "AND_3_1[2]"
			},
			{
				"from": "INPUT_3[1]",
				"to": "AND_2[2]"
			},
			{
				"from": "INPUT_3[1]",
				"to": "AND_3[2]"
			},
			{
				"from": "INPUT_3[1]",
				"to": "OR_3_1[3]"
			},
			{
				"from": "INPUT_3[1]",
				"to": "AND_3_1[3]"
			},
			{
				"from": "AND_1[1]",
				"to": "OR_3_2[1]"
			},
			{
				"from": "AND_2[1]",
				"to": "OR_3_2[2]"
			},
			{
				"from": "AND_3[1]",
				"to": "OR_3_2[3]"
			},
			{
				"from": "OR_3_2[1]",
				"to": "OUTPUT_1[1]"
			},
			{
				"from": "OR_3_2[1]",
				"to": "NOT_1[1]"
			},
			{
				"from": "NOT_1[1]",
				"to": "AND_4[1]"
			},
			{
				"from": "OR_3_1[1]",
				"to": "AND_4[2]"
			},
			{
				"from": "AND_4[1]",
				"to": "OR_1[1]"
			},
			{
				"from": "AND_3_1[1]",
				"to": "OR_1[2]"
			},
			{
				"from": "OR_1[1]",
				"to": "OUTPUT_2[1]"
			}
		],
		"tests": [
			{
				"inputs": [
					0,
					0,
					0
				],
				"outputs": [
					0,
					0
				]
			},
			{
				"inputs": [
					0,
					0,
					1
				],
				"outputs": [
					0,
					1
				]
			},
			{
				"inputs": [
					0,
					1,
					0
				],
				"outputs": [
					0,
					1
				]
			},
			{
				"inputs": [
					1,
					0,
					0
				],
				"outputs": [
					0,
					1
				]
			},
			{
				"inputs": [
					0,
					1,
					1
				],
				"outputs": [
					1,
					0
				]
			},
			{
				"inputs": [
					1,
					1,
					0
				],
				"outputs": [
					1,
					0
				]
			},
			{
				"inputs": [
					1,
					0,
					1
				],
				"outputs": [
					1,
					0
				]
			},
			{
				"inputs": [
					1,
					1,
					1
				],
				"outputs": [
					1,
					1
				]
			}
		]
	}
}

command for compile:

python interface.py --input lib\sum.json --target sum --link lib --output example.c

output code: godbolt 📜

Full 8bit adder (cascade)

{
	"sum_8": {
		"wires": [
			{
				"from": "INPUT_16[1]",
				"to": "sum_1[1]"
			},
			{
				"from": "INPUT_8[1]",
				"to": "sum_1[2]"
			},
			{
				"from": "INPUT_7[1]",
				"to": "sum_2[2]"
			},
			{
				"from": "INPUT_15[1]",
				"to": "sum_2[1]"
			},
			{
				"from": "INPUT_17[1]",
				"to": "sum_1[3]"
			},
			{
				"from": "sum_1[1]",
				"to": "sum_2[3]"
			},
			{
				"from": "INPUT_6[1]",
				"to": "sum_3[2]"
			},
			{
				"from": "INPUT_14[1]",
				"to": "sum_3[1]"
			},
			{
				"from": "sum_2[1]",
				"to": "sum_3[3]"
			},
			{
				"from": "INPUT_5[1]",
				"to": "sum_4[2]"
			},
			{
				"from": "sum_3[1]",
				"to": "sum_4[3]"
			},
			{
				"from": "INPUT_1[1]",
				"to": "sum_8[2]"
			},
			{
				"from": "INPUT_9[1]",
				"to": "sum_8[1]"
			},
			{
				"from": "INPUT_2[1]",
				"to": "sum_7[2]"
			},
			{
				"from": "INPUT_10[1]",
				"to": "sum_7[1]"
			},
			{
				"from": "INPUT_11[1]",
				"to": "sum_6[1]"
			},
			{
				"from": "INPUT_3[1]",
				"to": "sum_6[2]"
			},
			{
				"from": "INPUT_12[1]",
				"to": "sum_5[1]"
			},
			{
				"from": "INPUT_4[1]",
				"to": "sum_5[2]"
			},
			{
				"from": "sum_4[1]",
				"to": "sum_5[3]"
			},
			{
				"from": "sum_5[1]",
				"to": "sum_6[3]"
			},
			{
				"from": "sum_6[1]",
				"to": "sum_7[3]"
			},
			{
				"from": "sum_7[1]",
				"to": "sum_8[3]"
			},
			{
				"from": "INPUT_13[1]",
				"to": "sum_4[1]"
			},
			{
				"from": "sum_8[1]",
				"to": "OUTPUT_1[1]"
			},
			{
				"from": "sum_8[2]",
				"to": "OUTPUT_2[1]"
			},
			{
				"from": "sum_1[2]",
				"to": "OUTPUT_9[1]"
			},
			{
				"from": "sum_2[2]",
				"to": "OUTPUT_8[1]"
			},
			{
				"from": "sum_3[2]",
				"to": "OUTPUT_7[1]"
			},
			{
				"from": "sum_4[2]",
				"to": "OUTPUT_6[1]"
			},
			{
				"from": "sum_5[2]",
				"to": "OUTPUT_5[1]"
			},
			{
				"from": "sum_6[2]",
				"to": "OUTPUT_4[1]"
			},
			{
				"from": "sum_7[2]",
				"to": "OUTPUT_3[1]"
			}
		],
		"tests": [
			{
				"inputs": [
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0
				],
				"outputs": [
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0
				]
			},
			{
				"inputs": [
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					1,
					0
				],
				"outputs": [
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					1
				]
			},
			{
				"inputs": [
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					1,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0
				],
				"outputs": [
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					1
				]
			},
			{
				"inputs": [
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					1,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					1,
					0
				],
				"outputs": [
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					1,
					0
				]
			},
			{
				"inputs": [
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					1,
					0,
					0
				],
				"outputs": [
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					1,
					0
				]
			},
			{
				"inputs": [
					0,
					0,
					0,
					0,
					0,
					0,
					1,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					0
				],
				"outputs": [
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					1,
					0
				]
			},
			{
				"inputs": [
					0,
					0,
					0,
					0,
					0,
					0,
					1,
					0,
					0,
					0,
					0,
					0,
					0,
					0,
					1,
					0,
					0
				],
				"outputs": [
					0,
					0,
					0,
					0,
					0,
					0,
					1,
					0,
					0
				]
			}
		]
	}
}

command for compile:

python interface.py --input lib\sum_8.json --target sum_8 --link lib --output example.c

output code: godbolt 📜

Benefits

  • Super-easy to integrate with any logic schemes editor 🔌
  • Results in logic expressions, so C compiler can easily optimize them 🎯
  • Supports nesting 📁

So

You can

  • Rapidly develop complex schemes 💪
  • While keeping calculation speed high 🚀
  • Without worrying about optimization 😇