2014-05-20 05:36:10 -04:00
|
|
|
import csv
|
2014-05-20 07:16:24 -04:00
|
|
|
import time
|
|
|
|
import sys
|
2014-06-19 07:01:18 -04:00
|
|
|
from miscope.host.dump import *
|
2014-05-20 07:16:24 -04:00
|
|
|
from miscope.host.truthtable import *
|
2014-04-18 04:33:05 -04:00
|
|
|
|
|
|
|
class MiIoDriver():
|
|
|
|
def __init__(self, regs, name):
|
|
|
|
self.regs = regs
|
|
|
|
self.name = name
|
|
|
|
self.build_miio()
|
|
|
|
|
|
|
|
def build_miio(self):
|
|
|
|
for key, value in self.regs.d.items():
|
|
|
|
if self.name in key:
|
|
|
|
key.replace(self.name, "miio")
|
|
|
|
setattr(self, key, value)
|
|
|
|
|
|
|
|
def write(self, value):
|
|
|
|
self.miio_o.write(value)
|
|
|
|
|
|
|
|
def read(self):
|
|
|
|
return self.miio_i.read()
|
|
|
|
|
|
|
|
class MiLaDriver():
|
2014-10-06 06:30:06 -04:00
|
|
|
def __init__(self, regs, name, config_csv=None, use_rle=False):
|
2014-04-18 04:33:05 -04:00
|
|
|
self.regs = regs
|
|
|
|
self.name = name
|
2014-05-20 05:36:10 -04:00
|
|
|
self.use_rle = use_rle
|
2014-06-17 05:25:10 -04:00
|
|
|
if config_csv is None:
|
|
|
|
self.config_csv = name + ".csv"
|
2014-05-20 07:16:24 -04:00
|
|
|
self.get_config()
|
|
|
|
self.get_layout()
|
|
|
|
self.build_mila()
|
2014-06-17 05:25:10 -04:00
|
|
|
self.dat = Dat(self.width)
|
2014-10-06 06:30:06 -04:00
|
|
|
|
2014-05-20 07:16:24 -04:00
|
|
|
def get_config(self):
|
2014-06-17 05:25:10 -04:00
|
|
|
csv_reader = csv.reader(open(self.config_csv), delimiter=',', quotechar='#')
|
2014-05-20 07:16:24 -04:00
|
|
|
for item in csv_reader:
|
|
|
|
t, n, v = item
|
|
|
|
if t == "config":
|
|
|
|
setattr(self, n, int(v))
|
|
|
|
|
|
|
|
def get_layout(self):
|
|
|
|
self.layout = []
|
2014-06-17 05:25:10 -04:00
|
|
|
csv_reader = csv.reader(open(self.config_csv), delimiter=',', quotechar='#')
|
2014-05-20 07:16:24 -04:00
|
|
|
for item in csv_reader:
|
|
|
|
t, n, v = item
|
|
|
|
if t == "layout":
|
|
|
|
self.layout.append((n, int(v)))
|
|
|
|
|
2014-04-18 04:33:05 -04:00
|
|
|
def build_mila(self):
|
|
|
|
for key, value in self.regs.d.items():
|
2014-05-22 12:14:03 -04:00
|
|
|
if self.name == key[:len(self.name)]:
|
2014-04-18 04:33:05 -04:00
|
|
|
key.replace(self.name, "mila")
|
2014-05-20 05:36:10 -04:00
|
|
|
setattr(self, key, value)
|
2014-05-20 07:16:24 -04:00
|
|
|
value = 1
|
|
|
|
for name, length in self.layout:
|
|
|
|
setattr(self, name+"_o", value)
|
|
|
|
value = value*(2**length)
|
|
|
|
value = 0
|
|
|
|
for name, length in self.layout:
|
|
|
|
setattr(self, name+"_m", (2**length-1) << value)
|
|
|
|
value += length
|
|
|
|
|
2014-06-17 05:25:10 -04:00
|
|
|
def show_state(self, s):
|
|
|
|
print(s, end="|")
|
2014-05-20 07:16:24 -04:00
|
|
|
sys.stdout.flush()
|
2014-04-18 04:33:05 -04:00
|
|
|
|
2014-12-23 14:53:05 -05:00
|
|
|
def prog_term(self, port, trigger=0, mask=0, cond=None):
|
|
|
|
if cond is not None:
|
|
|
|
for k, v in cond.items():
|
|
|
|
trigger |= getattr(self, k+"_o")*v
|
|
|
|
mask |= getattr(self, k+"_m")*v
|
2014-04-20 18:17:23 -04:00
|
|
|
t = getattr(self, "mila_trigger_port{d}_trig".format(d=int(port)))
|
|
|
|
m = getattr(self, "mila_trigger_port{d}_mask".format(d=int(port)))
|
|
|
|
t.write(trigger)
|
|
|
|
m.write(mask)
|
2014-04-18 04:33:05 -04:00
|
|
|
|
2014-04-20 18:17:23 -04:00
|
|
|
def prog_range_detector(self, port, low, high):
|
|
|
|
l = getattr(self, "mila_trigger_port{d}_low".format(d=int(port)))
|
|
|
|
h = getattr(self, "mila_trigger_port{d}_high".format(d=int(port)))
|
|
|
|
l.write(low)
|
|
|
|
h.write(high)
|
|
|
|
|
|
|
|
def prog_edge_detector(self, port, rising_mask, falling_mask, both_mask):
|
|
|
|
rm = getattr(self, "mila_trigger_port{d}_rising_mask".format(d=int(port)))
|
|
|
|
fm = getattr(self, "mila_trigger_port{d}_falling_mask".format(d=int(port)))
|
|
|
|
bm = getattr(self, "mila_trigger_port{d}_both_mask".format(d=int(port)))
|
|
|
|
rm.write(rising_mask)
|
|
|
|
fm.write(falling_mask)
|
|
|
|
bm.write(both_mask)
|
2014-10-06 06:30:06 -04:00
|
|
|
|
2014-05-20 07:16:24 -04:00
|
|
|
def prog_sum(self, equation):
|
|
|
|
datas = gen_truth_table(equation)
|
2014-04-18 04:33:05 -04:00
|
|
|
for adr, dat in enumerate(datas):
|
|
|
|
self.mila_trigger_sum_prog_adr.write(adr)
|
|
|
|
self.mila_trigger_sum_prog_dat.write(dat)
|
|
|
|
self.mila_trigger_sum_prog_we.write(1)
|
2014-10-06 06:30:06 -04:00
|
|
|
|
2014-05-22 12:14:03 -04:00
|
|
|
def config_rle(self, v):
|
|
|
|
self.mila_rle_enable.write(v)
|
2014-04-18 04:33:05 -04:00
|
|
|
|
|
|
|
def is_done(self):
|
|
|
|
return self.mila_recorder_done.read()
|
|
|
|
|
2014-05-20 07:16:24 -04:00
|
|
|
def wait_done(self):
|
2014-05-22 12:14:03 -04:00
|
|
|
self.show_state("WAIT HIT")
|
2014-05-20 07:16:24 -04:00
|
|
|
while(not self.is_done()):
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
2014-04-18 04:33:05 -04:00
|
|
|
def trigger(self, offset, length):
|
2014-05-20 07:16:24 -04:00
|
|
|
self.show_state("TRIG")
|
2014-05-22 12:14:03 -04:00
|
|
|
if self.with_rle:
|
|
|
|
self.config_rle(self.use_rle)
|
2014-04-18 04:33:05 -04:00
|
|
|
self.mila_recorder_offset.write(offset)
|
|
|
|
self.mila_recorder_length.write(length)
|
|
|
|
self.mila_recorder_trigger.write(1)
|
|
|
|
|
2014-06-17 05:25:10 -04:00
|
|
|
def read(self):
|
|
|
|
self.show_state("READ")
|
2014-04-18 04:33:05 -04:00
|
|
|
empty = self.mila_recorder_read_empty.read()
|
|
|
|
while(not empty):
|
2014-05-20 05:36:10 -04:00
|
|
|
self.dat.append(self.mila_recorder_read_dat.read())
|
2014-04-18 04:33:05 -04:00
|
|
|
empty = self.mila_recorder_read_empty.read()
|
|
|
|
self.mila_recorder_read_en.write(1)
|
2014-10-06 06:30:06 -04:00
|
|
|
if self.with_rle:
|
|
|
|
if self.use_rle:
|
|
|
|
self.dat = self.dat.decode_rle()
|
2014-06-17 05:25:10 -04:00
|
|
|
return self.dat
|
|
|
|
|
|
|
|
def export(self, export_fn=None):
|
|
|
|
self.show_state("EXPORT")
|
2014-06-19 07:01:18 -04:00
|
|
|
dump = Dump()
|
|
|
|
dump.add_from_layout(self.layout, self.dat)
|
2014-06-17 05:25:10 -04:00
|
|
|
if ".vcd" in export_fn:
|
2014-06-19 07:01:18 -04:00
|
|
|
VCDExport(dump).write(export_fn)
|
2014-06-17 05:25:10 -04:00
|
|
|
elif ".csv" in export_fn:
|
2014-06-19 07:01:18 -04:00
|
|
|
CSVExport(dump).write(export_fn)
|
|
|
|
elif ".py" in export_fn:
|
|
|
|
PYExport(dump).write(export_fn)
|
2014-06-17 05:25:10 -04:00
|
|
|
else:
|
2014-06-19 07:01:18 -04:00
|
|
|
raise NotImplementedError
|