Skip to content

Instantly share code, notes, and snippets.

@DurandA
Created August 30, 2021 21:42
Show Gist options
  • Save DurandA/4ddd7ef826e6c65276aa8a3cdc8376cf to your computer and use it in GitHub Desktop.
Save DurandA/4ddd7ef826e6c65276aa8a3cdc8376cf to your computer and use it in GitHub Desktop.
D-Flipflop VCD generator
import asyncio
import sys
import time
from aiostream import stream
from vcd import VCDWriter
start = time.monotonic()
async def oscillator(writer, variable, freq):
loop = asyncio.get_event_loop()
while True:
await asyncio.sleep(4/freq)
writer.change(variable, (loop.time()-start)*1000, 1)
yield (variable, 1)
await asyncio.sleep(4/freq)
writer.change(variable, (loop.time()-start)*1000, 0)
yield (variable, 0)
async def main():
loop = asyncio.get_event_loop()
with VCDWriter(sys.stdout, timescale='1 ms') as writer:
d_var = writer.register_var('DFF', 'd', 'integer', size=1, init=0)
clk_var = writer.register_var('DFF', 'clk', 'integer', size=1, init=0)
q_var = writer.register_var('DFF', 'q', 'integer', size=1, init=0)
transitions = stream.merge(oscillator(writer, d_var, 100), oscillator(writer, clk_var, 90.01))
async with transitions.stream() as streamer:
async for signal, transition in streamer:
if signal is clk_var and transition == 1:
if q_var.value != d_var.value:
writer.change(q_var, (loop.time()-start)*1000, d_var.value)
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment