Skip to content

Instantly share code, notes, and snippets.

@hgomersall
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hgomersall/59e3d2b151e7931b1965 to your computer and use it in GitHub Desktop.
Save hgomersall/59e3d2b151e7931b1965 to your computer and use it in GitHub Desktop.
An example myhdl sim showing two sims running concurrently in different threads with no problem. In such an example, it is necessary to have separate signals, otherwise they *will* step on each other.
from myhdl import *
import threading
a = Signal(intbv(0)[5:])
b = Signal(intbv(0)[5:])
clock1 = Signal(False)
clock2 = Signal(False)
reset1 = ResetSignal(False, active=True, async=False)
reset2 = ResetSignal(False, active=True, async=False)
lock = threading.Lock()
def clockgen(clock):
@instance
def clkgen():
while True:
yield delay(10)
clock.next = not clock
return clkgen
def stimulus(sig, clock, reset):
data = [0]
@always(clock.posedge, reset)
def stimulus_inst():
sig.next = data[0]
data[0] += 1
return stimulus_inst
def printer(sig, clock, reset):
@always_seq(clock.posedge, reset)
def printer_inst():
with lock:
print threading.current_thread(), now(), sig
return printer_inst
sim1 = Simulation([clockgen(clock1), stimulus(a, clock1, reset1),
printer(a, clock1, reset1)])
sim2 = Simulation([clockgen(clock2), stimulus(b, clock2, reset2),
printer(b, clock2, reset2)])
sim1_thread = threading.Thread(target=sim1.run, args=(200,))
sim2_thread = threading.Thread(target=sim2.run, args=(300,))
sim1_thread.start()
sim2_thread.start()
sim1_thread.join()
sim2_thread.join()
@hgomersall
Copy link
Author

The output from the above is something like:

<Thread(Thread-1, started 139843183535872)> 10 0
<Thread(Thread-1, started 139843183535872)> 30 0
<Thread(Thread-2, started 139843175143168)> 10 0
<Thread(Thread-1, started 139843183535872)> 50 1
<Thread(Thread-2, started 139843175143168)> 30 0
<Thread(Thread-1, started 139843183535872)> 70 2
<Thread(Thread-2, started 139843175143168)> 50 1
<Thread(Thread-1, started 139843183535872)> 90 3
<Thread(Thread-2, started 139843175143168)> 70 2
<Thread(Thread-1, started 139843183535872)> 110 4
<Thread(Thread-2, started 139843175143168)> 90 3
<Thread(Thread-1, started 139843183535872)> 130 5
<Thread(Thread-2, started 139843175143168)> 110 4
<Thread(Thread-1, started 139843183535872)> 150 6
<Thread(Thread-2, started 139843175143168)> 130 5
<Thread(Thread-1, started 139843183535872)> 170 7
<Thread(Thread-2, started 139843175143168)> 150 6
<Thread(Thread-1, started 139843183535872)> 190 8
<class 'myhdl._SuspendSimulation'>: Simulated 200 timesteps
<Thread(Thread-2, started 139843175143168)> 170 7
<Thread(Thread-2, started 139843175143168)> 190 8
<Thread(Thread-2, started 139843175143168)> 210 9
<Thread(Thread-2, started 139843175143168)> 230 10
<Thread(Thread-2, started 139843175143168)> 250 11
<Thread(Thread-2, started 139843175143168)> 270 12
<Thread(Thread-2, started 139843175143168)> 290 13
<class 'myhdl._SuspendSimulation'>: Simulated 300 timesteps

myhdl 0.9 will just run forever.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment