2014-04-18 04:33:05 -04:00
|
|
|
import csv
|
|
|
|
|
|
|
|
class MappedReg:
|
2014-06-26 05:09:59 -04:00
|
|
|
def __init__(self, readfn, writefn, name, addr, length, busword, mode):
|
2014-04-18 04:33:05 -04:00
|
|
|
self.readfn = readfn
|
|
|
|
self.writefn = writefn
|
|
|
|
self.addr = addr
|
|
|
|
self.length = length
|
2014-06-26 05:09:59 -04:00
|
|
|
self.busword = busword
|
2014-04-18 04:33:05 -04:00
|
|
|
self.mode = mode
|
|
|
|
|
2015-02-23 11:04:23 -05:00
|
|
|
def read(self, repeats=None):
|
2014-04-18 04:33:05 -04:00
|
|
|
if self.mode not in ["rw", "ro"]:
|
|
|
|
raise KeyError(name + "register not readable")
|
2015-02-23 11:04:23 -05:00
|
|
|
|
|
|
|
def to_int(v):
|
|
|
|
return 1 if v is None else v
|
|
|
|
read_datas = self.readfn(self.addr, burst_length=self.length, repeats=repeats)
|
|
|
|
datas = []
|
|
|
|
for i in range(to_int(repeats)):
|
2015-02-22 18:09:31 -05:00
|
|
|
data = 0
|
2015-02-23 11:04:23 -05:00
|
|
|
for j in range(self.length):
|
|
|
|
data = data << self.busword
|
|
|
|
data |= read_datas[i*self.length+j]
|
|
|
|
datas.append(data)
|
|
|
|
if repeats is None:
|
|
|
|
return datas[0]
|
|
|
|
else:
|
|
|
|
return datas
|
2014-04-18 04:33:05 -04:00
|
|
|
|
|
|
|
def write(self, value):
|
|
|
|
if self.mode not in ["rw", "wo"]:
|
|
|
|
raise KeyError(name + "register not writable")
|
2015-02-22 18:09:31 -05:00
|
|
|
datas = []
|
2014-04-18 04:33:05 -04:00
|
|
|
for i in range(self.length):
|
2015-02-22 18:09:31 -05:00
|
|
|
datas.append((value >> ((self.length-1-i)*self.busword)) & (2**self.busword-1))
|
|
|
|
self.writefn(self.addr, datas)
|
2014-04-18 04:33:05 -04:00
|
|
|
|
|
|
|
class MappedRegs:
|
|
|
|
def __init__(self, d):
|
|
|
|
self.d = d
|
|
|
|
|
|
|
|
def __getattr__(self, attr):
|
|
|
|
try:
|
|
|
|
return self.__dict__['d'][attr]
|
|
|
|
except KeyError:
|
|
|
|
pass
|
|
|
|
raise KeyError("No such register " + attr)
|
|
|
|
|
2014-06-26 05:09:59 -04:00
|
|
|
def build_map(addrmap, busword, readfn, writefn):
|
2014-04-18 04:33:05 -04:00
|
|
|
csv_reader = csv.reader(open(addrmap), delimiter=',', quotechar='#')
|
|
|
|
d = {}
|
|
|
|
for item in csv_reader:
|
|
|
|
name, addr, length, mode = item
|
|
|
|
addr = int(addr.replace("0x", ""), 16)
|
|
|
|
length = int(length)
|
2014-06-26 05:09:59 -04:00
|
|
|
d[name] = MappedReg(readfn, writefn, name, addr, length, busword, mode)
|
2014-04-18 04:33:05 -04:00
|
|
|
return MappedRegs(d)
|