123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- import csv
- import re
- import sys
- tab_to_col = 3
- class RegField:
- def __init__(self, cols=None):
- if cols:
- self.bits, self.attr, self.reset_val, self.desc = (
- [x.strip() for x in cols])
- self.desc = [self.desc]
- else:
- self.bits = ''
- self.attr = ''
- self.reset_val = ''
- self.desc = []
- def Setup(self, cols):
- self.bits, self.attr, self.reset_val = cols[0:3]
- if len(cols) > 3:
- self.desc.append(cols[3])
- def AddDesc(self, desc):
- self.desc.append(desc)
- def Show(self):
- print self
- print
- self.__init__()
- def __str__(self):
- return '%s,%s,%s,%s' % (self.bits, self.attr, self.reset_val,
- '\n'.join(self.desc))
- class Printer:
- def __init__(self, name):
- self.first = True
- self.name = name
- self.re_sel = re.compile("[1-9]'b([01]+): (.*)")
- def __enter__(self):
- return self
- def __exit__(self, type, value, traceback):
- if not self.first:
- self.output_footer()
- def output_header(self):
- print '/* %s */' % self.name
- print 'enum {'
- def output_footer(self):
- print '};';
- def output_regfield(self, regfield):
- lines = regfield.desc
- field = lines[0]
-
- if field in ['reserved', 'reserve', 'write_enable', 'write_mask']:
- return
- if field.endswith('_sel') or field.endswith('_con'):
- field = field[:-4]
- elif field.endswith(' iomux'):
- field = field[:-6]
- elif field.endswith('_mode') or field.endswith('_mask'):
- field = field[:-5]
-
-
-
- field = field.upper()
- if ':' in regfield.bits:
- bit_high, bit_low = [int(x) for x in regfield.bits.split(':')]
- else:
- bit_high = bit_low = int(regfield.bits)
- bit_width = bit_high - bit_low + 1
- mask = (1 << bit_width) - 1
- if self.first:
- self.first = False
- self.output_header()
- else:
- print
- out_enum(field, 'shift', bit_low)
- out_enum(field, 'mask', mask)
- next_val = -1
-
- for line in lines:
- m = self.re_sel.match(line)
- if m:
- val, enum = int(m.group(1), 2), m.group(2)
- if enum not in ['reserved', 'reserve']:
- out_enum(field, enum, val, val == next_val)
- next_val = val + 1
- def process_file(name, fd):
- field = RegField()
- reg = ''
- fields = []
- def add_it(field):
- if field.bits:
- if reg == name:
- fields.append(field)
- field = RegField()
- return field
- def is_field_start(line):
- if '=' in line or '+' in line:
- return False
- if (line.startswith('gpio') or line.startswith('peri_') or
- line.endswith('_sel') or line.endswith('_con')):
- return True
- if not ' ' in line:
- return True
- return False
- for line in fd:
- line = line.rstrip()
- if line[:4] in ['GRF_', 'PMU_', 'CRU_']:
- field = add_it(field)
- reg = line
- do_this = name == reg
- elif not line or not line.startswith(' '):
- continue
- line = line.replace('\xe2\x80\x99', "'")
- leading = len(line) - len(line.lstrip())
- line = line.lstrip()
- cols = re.split(' *', line, 3)
- if leading > 15 or (len(cols) > 3 and is_field_start(cols[3])):
- if is_field_start(line):
- field = add_it(field)
- field.AddDesc(line)
- else:
- if cols[0] == 'Bit' or len(cols) < 3:
- continue
-
-
- field = add_it(field)
- field.Setup(cols)
- field = add_it(field)
- with Printer(name) as printer:
- for field in fields:
-
- printer.output_regfield(field)
-
- def out_enum(field, suffix, value, skip_val=False):
- str = '%s_%s' % (field.upper(), suffix.upper())
- if not skip_val:
- tabs = tab_to_col - len(str) / 8
- if value > 9:
- val_str = '%#x' % value
- else:
- val_str = '%d' % value
- str += '%s= %s' % ('\t' * tabs, val_str)
- print '\t%s,' % str
- def process_csv(name, fd):
- reader = csv.reader(fd)
- rows = []
- field = RegField()
- for row in reader:
-
- if not row[0]:
- field.desc.append(row[3])
- continue
- if field.bits:
- if field.bits != 'Bit':
- rows.append(field)
-
- field = RegField(row)
- with Printer(name) as printer:
- for row in rows:
-
- printer.output_regfield(row)
-
- fname = sys.argv[1]
- name = sys.argv[2]
- if 1:
- with open(fname, 'r') as fd:
- process_file(name, fd)
- if 0:
- with open(fname, 'r') as fd:
- process_csv(name, fd)
|