|
2 | 2 | from .util import split_tokens
|
3 | 3 |
|
4 | 4 |
|
| 5 | +class RTC_Macros: |
| 6 | + @staticmethod |
| 7 | + def READ_RTC_REG(rtc_reg, low_bit, bit_width): |
| 8 | + return '\treg_rd ' + ', '.join(( |
| 9 | + rtc_reg, |
| 10 | + '%s + %s - 1' % (low_bit, bit_width), |
| 11 | + low_bit |
| 12 | + )) |
| 13 | + |
| 14 | + @staticmethod |
| 15 | + def WRITE_RTC_REG(rtc_reg, low_bit, bit_width, value): |
| 16 | + args = ( |
| 17 | + rtc_reg, |
| 18 | + '%s + %s - 1' % (low_bit, bit_width), |
| 19 | + low_bit, |
| 20 | + value |
| 21 | + ) |
| 22 | + return '\treg_wr ' + ', '.join(args) |
| 23 | + |
| 24 | + @staticmethod |
| 25 | + def READ_RTC_FIELD(rtc_reg, low_bit): |
| 26 | + return RTC_Macros.READ_RTC_REG(rtc_reg, low_bit, 1) |
| 27 | + |
| 28 | + @staticmethod |
| 29 | + def WRITE_RTC_FIELD(rtc_reg, low_bit, value): |
| 30 | + return RTC_Macros.WRITE_RTC_REG(rtc_reg, low_bit, 1, value + ' & 1') |
| 31 | + |
| 32 | + |
5 | 33 | class Preprocessor:
|
6 | 34 | def __init__(self):
|
7 | 35 | self._defines = {}
|
@@ -42,12 +70,34 @@ def expand_defines(self, line):
|
42 | 70 |
|
43 | 71 | return line
|
44 | 72 |
|
| 73 | + def expand_rtc_macros(self, line): |
| 74 | + clean_line = line.strip() |
| 75 | + if not clean_line: |
| 76 | + return line |
| 77 | + |
| 78 | + macro = clean_line.split('(', 1) |
| 79 | + if len(macro) != 2: |
| 80 | + return line |
| 81 | + |
| 82 | + macro_name, macro_args = macro |
| 83 | + |
| 84 | + macro_fn = getattr(RTC_Macros, macro_name, None) |
| 85 | + if macro_fn is None: |
| 86 | + return line |
| 87 | + |
| 88 | + macro_args, _ = macro_args.rsplit(')', 1) # trim away right bracket. safe as comments already stripped |
| 89 | + macro_args = macro_args.split(',') # not safe when args contain ',' but we should not have those |
| 90 | + macro_args = [x.strip() for x in macro_args] |
| 91 | + |
| 92 | + return macro_fn(*macro_args) |
| 93 | + |
45 | 94 | def preprocess(self, content):
|
46 | 95 | self.parse_defines(content)
|
47 | 96 | lines = nocomment.remove_comments(content)
|
48 | 97 | result = []
|
49 | 98 | for line in lines:
|
50 | 99 | line = self.expand_defines(line)
|
| 100 | + line = self.expand_rtc_macros(line) |
51 | 101 | result.append(line)
|
52 | 102 | result = "\n".join(result)
|
53 | 103 | return result
|
|
0 commit comments