This project implements a rule-based transformation of a gate-level Verilog netlist into an equivalent form using a reduced primitive gate basis:
NAND2X1NOR2X1
It includes two implementations:
- Original version (handwritten logic)
- AI-refactored version
Both are executed on the same input netlist and compared for structural equivalence.
The goal of this project is to:
- Convert complex logic gates into NAND/NOR-only representations
- Automatically generate intermediate wires
- Normalize gate-level netlists
- Compare two independent implementations
- Validate structural equivalence in real-world transformation
The following gate types are decomposed:
NAND3XLNOR3XLCLKINVX1INVX2OAI22XLOAI221X1OAI21XLOAI211XLOAI33X1OAI2BB1X1OAI2BB2X1AOI21X1AOI31X1AOI32X1AOI2BB1X1XNOR2XLOR2X2MXI2XL
All are converted into networks of NAND2X1 and NOR2X1.
python-verilog-parser/
├── inputs/
│ └── block32.v
├── outputs/
│ ├── original/
│ │ └── block32_equivalent.v
│ └── ai/
│ └── block32_equivalent.v
├── scripts/
│ ├── original_main.py
│ ├── ai_main.py
│ ├── run_original.sh
│ ├── run_ai.sh
│ └── compare_outputs.sh
├── docs/
└── README.md
./scripts/run_original.sh./scripts/run_ai.sh./scripts/compare_outputs.shBoth implementations were executed on:
inputs/block32.v
| Metric | Original | AI Refactored |
|---|---|---|
| Source lines | 338 | 335 |
| New wires added | 698 | 697 |
| Output lines | 1053 | 1054 |
| Conversion time | ~0.039s | ~0.013s |
| Avg lines/sec | 3787 | 5341 |
Only minor differences were found:
Example:
// Original
NOR2X1 U39403 ( .A(n789), .B(n398), .Y(n389) );
NAND2X1 U39404 ( .A(n788), .B(n788), .Y(n789) );
// AI
NAND2X1 U39404 ( .A(n788), .B(n788), .Y(n789) );
NOR2X1 U39403 ( .A(n789), .B(n398), .Y(n389) );This is a producer-consumer ordering difference and does not affect functionality.
-endmodule
\ No newline at end of file
+endmoduleIn structural Verilog:
Instance order does NOT define circuit behavior. Connectivity does.
Therefore:
- Reordering of gate instances does not change functionality
- Both outputs are functionally and structurally equivalent
The original and AI-refactored implementations produce:
- Equivalent NAND/NOR netlists
- Same logical decomposition
- Nearly identical outputs
Differences are limited to:
- Local ordering of generated gates
- Minor formatting variations
- Gate-level logic normalization
- Decomposition into primitive gates
- Automated intermediate wire generation
- Structural equivalence reasoning
- Performance comparison between implementations
- Add formal equivalence checking (e.g., Yosys)
- Add simulation-based validation
- Use a proper Verilog parser (AST-based)
- Ensure deterministic output ordering
- Expand gate support coverage
See LICENSE file.