-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from .target import Target | ||
from magma.testing.function import testvectors | ||
import magma.config as config | ||
import inspect | ||
import os | ||
import subprocess | ||
import magma as m | ||
from magma.simulator.python_simulator import PythonSimulator | ||
|
||
|
||
class PythonSimulatorTarget(Target): | ||
def __init__(self, circuit, test_vectors, clock): | ||
super().__init__(circuit, test_vectors) | ||
self._clock = clock | ||
|
||
def run(self): | ||
ports = self._circuit.interface.ports | ||
simulator = PythonSimulator(self._circuit, self._clock) | ||
test_vector_length = len(self._test_vectors[0]) | ||
assert len(ports.keys()) == test_vector_length | ||
clock_val = None | ||
for tv_index, tv in enumerate(self._test_vectors): | ||
for i, (name, port) in enumerate(ports.items()): | ||
val = tv[i] | ||
if port is self._clock: | ||
if tv_index == 0: | ||
clock_val = val | ||
elif val != clock_val: | ||
print("Advancing clock") | ||
simulator.advance(2) | ||
elif port.isoutput(): | ||
print(f"Setting {self._circuit.name}.{name} to {val}") | ||
simulator.set_value(port, val) | ||
elif port.isinput(): | ||
print(f"Asserting {self._circuit.name}.{name} is {val}") | ||
sim_val = simulator.get_value(port) | ||
assert sim_val == val | ||
else: | ||
raise NotImplementedError() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import tempfile | ||
import magma as m | ||
import fault.python_simulator_target | ||
from bit_vector import BitVector | ||
import mantle | ||
|
||
|
||
def test_python_simulator_target(): | ||
""" | ||
Test basic python simulator workflow with a simple circuit. | ||
""" | ||
|
||
class Foo(m.Circuit): | ||
IO = ["I", m.In(m.Bit), "O", m.Out(m.Bit)] | ||
|
||
@classmethod | ||
def definition(io): | ||
m.wire(io.I, io.O) | ||
|
||
def bit(val): | ||
return BitVector(val, 1) | ||
|
||
test_vectors = [ | ||
[bit(0), bit(0)], | ||
[bit(1), bit(1)], | ||
] | ||
target = fault.python_simulator_target.PythonSimulatorTarget( | ||
Foo, test_vectors, None) | ||
target.run() |