-
-
Save cfelton/e143ba5d302338560754 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import randint | |
from myhdl import * | |
def switchchannels(mem2d, q, clk): | |
@always(clk.posedge) | |
def switch(): | |
#print('switch') | |
#print(mem2d) | |
x = mem2d[0] | |
mem2d[0] = mem2d[1] | |
mem2d[1] = x | |
#print(mem2d) | |
@always(clk.posedge) | |
def logic(): | |
#print('logic') | |
#print(q, mem2d) | |
mem2d[0][0].next = mem2d[0][1] | |
mem2d[0][1].next = q | |
#print(mem2d) | |
return switch, logic | |
from random import randrange | |
def test_switchchannels(): | |
clk = Signal(bool(0)) | |
q = Signal(intbv(0)[4:]) | |
mem2d = [[Signal(intbv(0)[4:]), Signal(intbv(0)[4:])] , | |
[Signal(intbv(1)[4:]), Signal(intbv(1)[4:])]] | |
print('init') | |
print(q) | |
print(clk) | |
print(mem2d) | |
switch_inst = switchchannels(mem2d, q, clk) | |
@always(delay(10)) | |
def clkgen(): | |
clk.next = not clk | |
@instance | |
def stimulus(): | |
for ii in range(10): | |
q.next = randint(0,15) | |
print("time {:<8d}: q = {}".format(now(), int(q))) | |
for ii,row in enumerate(mem2d): | |
print(" {}: {:4d} {:4d}".format(ii, *map(int, row))) | |
yield clk.posedge | |
raise StopSimulation | |
return switch_inst, clkgen, stimulus | |
def simulate(timesteps): | |
traceSignals.timescale = "1ps" | |
tb = traceSignals(test_switchchannels) | |
sim = Simulation(tb) | |
sim.run() | |
simulate(40) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output