Skip to content

Instantly share code, notes, and snippets.

@hgomersall
Created April 7, 2015 16:31
Show Gist options
  • Save hgomersall/6f9268023593ca5fd9d0 to your computer and use it in GitHub Desktop.
Save hgomersall/6f9268023593ca5fd9d0 to your computer and use it in GitHub Desktop.
from myhdl import *
from multiprocessing import Process, current_process, Value, Event, Semaphore
process_count = Value('i', 0)
sync_semaphore = Semaphore()
all_synced = Event()
def synchronise():
with sync_semaphore:
process_count.value += 1
if process_count.value == 2:
process_count.value = 0
all_synced.set()
all_synced.wait()
def metaclock():
@instance
def metaclock_driver():
while True:
yield delay(1)
synchronise()
return metaclock_driver
def clock_driver(clock, period):
first_delay = period//2
@instance
def clk_driver_inst():
while True:
clock.next = not clock
yield delay(first_delay)
clock.next = not clock
yield delay(period - first_delay)
return clk_driver_inst
def simple_counter(clock, reset):
counter_state = modbv(0, min=0, max=20)
@always_seq(clock.posedge, reset=reset)
def counter():
print('process %d, counter %d' %
(current_process().pid, counter_state))
counter_state[:] = counter_state + 1
return counter
reset1 = ResetSignal(0, active=True, async=False)
reset2 = ResetSignal(0, active=True, async=False)
clock1 = Signal(bool(0))
clock2 = Signal(bool(0))
sim1 = Simulation(
[clock_driver(clock1, 9), simple_counter(clock1, reset1), metaclock()])
sim2 = Simulation(
[clock_driver(clock2, 3), simple_counter(clock2, reset2), metaclock()])
process1 = Process(target=sim1.run, args=(50,))
process2 = Process(target=sim2.run, args=(50,))
process1.start()
process2.start()
process1.join()
process2.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment