#!/usr/bin/env python3
# Copyright 2015 - 2026, GIBIS-UNIFESP and the wiRedPanda contributors
# SPDX-License-Identifier: GPL-3.0-or-later

"""
Create 4-bit Johnson Counter IC

Johnson counter (twisted ring counter) with 8-state sequence:
  Q3=0,Q2=0,Q1=0,Q0=0 → 0,0,0,1 → 0,0,1,1 → 0,1,1,1 →
  1,1,1,1 → 1,1,1,0 → 1,1,0,0 → 1,0,0,0 → (repeat)

Inputs: CLK (Clock), PRESET (active-LOW async preset for FF[0])
Outputs: Q0, Q1, Q2, Q3

Implementation:
- 4 D Flip-Flops (DFlipFlop)
- 1 NOT gate (invert Q[3] for twisted feedback)
- Simple cascade interconnections

Usage:
    python create_johnson_counter.py
"""

import asyncio

from ic_builder_base import run_ic_builder
from ic_builder_helpers import RingJohnsonCounterBuilder


async def build(mcp) -> bool:
    """Entry point for the builder"""
    return await RingJohnsonCounterBuilder(mcp, is_johnson=True).create()


if __name__ == "__main__":
    import sys
    import traceback

    try:
        exit_code = asyncio.run(run_ic_builder(build, "4-bit Johnson Counter IC"))
        sys.exit(exit_code)
    except Exception as e:
        print(f"Error: {e}")
        traceback.print_exc()
        sys.exit(1)
