root / host / lib / ic_reg_maps / common.py @ 154e4492
History | View | Annotate | Download (2.47 KB)
| 1 |
#
|
|---|---|
| 2 |
# Copyright 2008,2009 Free Software Foundation, Inc.
|
| 3 |
#
|
| 4 |
# This file is part of GNU Radio
|
| 5 |
#
|
| 6 |
# GNU Radio is free software; you can redistribute it and/or modify
|
| 7 |
# it under the terms of the GNU General Public License as published by
|
| 8 |
# the Free Software Foundation; either asversion 3, or (at your option)
|
| 9 |
# any later version.
|
| 10 |
#
|
| 11 |
# GNU Radio is distributed in the hope that it will be useful,
|
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 14 |
# GNU General Public License for more details.
|
| 15 |
#
|
| 16 |
# You should have received a copy of the GNU General Public License
|
| 17 |
# along with GNU Radio; see the file COPYING. If not, write to
|
| 18 |
# the Free Software Foundation, Inc., 51 Franklin Street,
|
| 19 |
# Boston, MA 02110-1301, USA.
|
| 20 |
|
| 21 |
import re |
| 22 |
import os |
| 23 |
import math |
| 24 |
from Cheetah.Template import Template |
| 25 |
|
| 26 |
def parse_tmpl(_tmpl_text, **kwargs): |
| 27 |
return str(Template(_tmpl_text, kwargs)) |
| 28 |
|
| 29 |
def safe_makedirs(path): |
| 30 |
not os.path.isdir(path) and os.makedirs(path) |
| 31 |
|
| 32 |
class reg: |
| 33 |
def __init__(self, reg_des): |
| 34 |
x = re.match('^(\w*)\s*(\w*)\[(.*)\]\s*(\w*)\s*(.*)$', reg_des)
|
| 35 |
name, addr, bit_range, default, enums = x.groups() |
| 36 |
|
| 37 |
#store variables
|
| 38 |
self._name = name
|
| 39 |
self._addr = int(addr, 16) |
| 40 |
if ':' in bit_range: self._addr_spec = sorted(map(int, bit_range.split(':'))) |
| 41 |
else: self._addr_spec = int(bit_range), int(bit_range) |
| 42 |
self._default = int(default, 16) |
| 43 |
|
| 44 |
#extract enum
|
| 45 |
self._enums = list() |
| 46 |
if enums:
|
| 47 |
enum_val = 0
|
| 48 |
for enum_str in map(str.strip, enums.split(',')): |
| 49 |
if '=' in enum_str: |
| 50 |
enum_name, enum_val = enum_str.split('=')
|
| 51 |
enum_val = int(enum_val)
|
| 52 |
else: enum_name = enum_str
|
| 53 |
self._enums.append((enum_name, enum_val))
|
| 54 |
enum_val += 1
|
| 55 |
|
| 56 |
def get_addr(self): return self._addr |
| 57 |
def get_enums(self): return self._enums |
| 58 |
def get_name(self): return self._name |
| 59 |
def get_default(self): |
| 60 |
for key, val in self.get_enums(): |
| 61 |
if val == self._default: return str.upper('%s_%s'%(self.get_name(), key)) |
| 62 |
return self._default |
| 63 |
def get_stdint_type(self):\ |
| 64 |
return 'uint%d_t'%max(2**math.ceil(math.log(self.get_bit_width(), 2)), 8) |
| 65 |
def get_shift(self): return self._addr_spec[0] |
| 66 |
def get_mask(self): return hex(int('1'*self.get_bit_width(), 2)) |
| 67 |
def get_bit_width(self): return self._addr_spec[1] - self._addr_spec[0] + 1 |