litex/miscope/host/drivers.py

144 lines
3.9 KiB
Python
Raw Normal View History

import csv
import time
import sys
from miscope.host.dump import *
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
self.use_rle = use_rle
if config_csv is None:
self.config_csv = name + ".csv"
self.get_config()
self.get_layout()
self.build_mila()
self.dat = Dat(self.width)
2014-10-06 06:30:06 -04:00
def get_config(self):
csv_reader = csv.reader(open(self.config_csv), delimiter=',', quotechar='#')
for item in csv_reader:
t, n, v = item
if t == "config":
setattr(self, n, int(v))
def get_layout(self):
self.layout = []
csv_reader = csv.reader(open(self.config_csv), delimiter=',', quotechar='#')
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")
setattr(self, key, value)
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
def show_state(self, s):
print(s, end="|")
sys.stdout.flush()
2014-04-18 04:33:05 -04: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")
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
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
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()
def wait_done(self):
2014-05-22 12:14:03 -04:00
self.show_state("WAIT HIT")
while(not self.is_done()):
time.sleep(0.1)
2014-04-18 04:33:05 -04:00
def trigger(self, offset, length):
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)
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):
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()
return self.dat
def export(self, export_fn=None):
self.show_state("EXPORT")
dump = Dump()
dump.add_from_layout(self.layout, self.dat)
if ".vcd" in export_fn:
VCDExport(dump).write(export_fn)
elif ".csv" in export_fn:
CSVExport(dump).write(export_fn)
elif ".py" in export_fn:
PYExport(dump).write(export_fn)
else:
raise NotImplementedError