OpenCores
URL https://opencores.org/ocsvn/ssbcc/ssbcc/trunk

Subversion Repositories ssbcc

[/] [ssbcc/] [trunk/] [ssbccUtil.py] - Blame information for rev 3

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 sinclairrf
################################################################################
2
#
3
# Copyright 2012, Sinclair R.F., Inc.
4
#
5
# Utilities required by ssbcc
6
#
7
################################################################################
8
 
9
import math
10 3 sinclairrf
import os
11 2 sinclairrf
import re
12
 
13
################################################################################
14
#
15
# Classes
16
#
17
################################################################################
18
 
19
class SSBCCException(Exception):
20
  """
21
  Exception class for ssbcc.
22
  """
23
  def __init__(self,message):
24
    self.message = message;
25
  def __str__(self):
26
    return self.message;
27
 
28
################################################################################
29
#
30
# Methods
31
#
32
################################################################################
33
 
34
def CeilLog2(v):
35
  """
36
  Return the smallest integer that has a power of 2 greater than or equal to
37
  the argument.
38
  """
39
  tmp = int(math.log(v,2));
40
  while 2**tmp < v:
41
    tmp = tmp + 1;
42
  return tmp;
43
 
44
def CeilPow2(v):
45
  """
46
  Return the smallest power of 2 greater than or equal to the argument.
47
  """
48
  return 2**CeilLog2(v);
49
 
50
def ExtractBits(v,bits):
51
  """
52
  Extract the bits specified by bits from v.
53
  bits must have a Verilog-type format.  I.e., [7:0], [0+:8], etc.
54
  """
55
  if type(v) != int:
56
    raise SSBCCException('%s must be an int' % v);
57
  if re.match(r'[[]\d+:\d+]$',bits):
58
    cmd = re.findall(r'[[](\d+):(\d+)]$',bits)[0];
59
    b0 = int(cmd[1]);
60
    bL = int(cmd[0]) - b0 + 1;
61
  elif re.match(r'[[]\d+\+:\d+]$',bits):
62
    cmd = re.findall(r'[[](\d+)\+:(\d+)]$',bits)[0];
63
    b0 = int(cmd[0]);
64
    bL = int(cmd[1]);
65
  else:
66
    raise SSBCCException('Unrecognized bit slice format:  %s' % bits);
67
  if not 1 <= bL <= 8:
68
    raise SSBCCException('Malformed range "%s" doesn\'t provide 1 to 8 bits' % bits)
69
  v /= 2**b0;
70
  v %= 2**bL;
71
  return v;
72
 
73
def IntValue(v):
74
  """
75
  Convert a Verilog format integer into an integer value.
76
  """
77 3 sinclairrf
  save_v = v;
78
  if re.match(r'([1-9]\d*)?\'[bodh]',v):
79
    length = 0;
80
    while v[0] != '\'':
81
      length *= 10;
82
      length += ord(v[0]) - ord('0');
83
      v = v[1:];
84
    v=v[1:];
85
    if v[0] == 'b':
86
      base = 2;
87
    elif v[0] == 'o':
88
      base = 8;
89
    elif v[0] == 'd':
90
      base = 10;
91
    elif v[0] == 'h':
92
      base = 16;
93
    else:
94
      raise Exception('Program bug -- unrecognized base:  "%c"' % v[0]);
95
    v = v[1:];
96
  else:
97
    length = 0;
98
    base = 10;
99 2 sinclairrf
  ov = 0;
100 3 sinclairrf
  for vv in [v[i] for i in range(len(v)) if v[i] != '_']:
101
    ov *= base;
102
    try:
103
      dv = int(vv,base);
104
    except:
105
      raise SSBCCException('Malformed parameter value:  "%s"' % save_v);
106
    ov += dv;
107
  if length > 0 and ov >= 2**length:
108
    raise SSBCCException('Paramter length and value don\'t match:  "%s"' % save_v);
109 2 sinclairrf
  return ov;
110
 
111
def IsPowerOf2(v):
112
  """
113
  Indicate whether or not the argument is a power of 2.
114
  """
115
  return v == 2**int(math.log(v,2)+0.5);
116
 
117 3 sinclairrf
def LoadFile(filename,config):
118 2 sinclairrf
  """
119
  Load the file into a list with the line contents and line numbers.\n
120
  filename is either the name of the file or a file object.\n
121
  Note:  The file object is closed in either case.
122
  """
123
  if type(filename) == str:
124 3 sinclairrf
    for path in config.includepaths:
125
      fullfilename = os.path.join(path,filename);
126
      if os.path.isfile(fullfilename):
127
        try:
128
          fp = file(fullfilename);
129
        except:
130
          raise SSBCCException('Error opening "%s"' % filename);
131
        break;
132
    else:
133
      raise SSBCCException('.INCLUDE file "%s" not found' % filename);
134 2 sinclairrf
  elif type(filename) == file:
135 3 sinclairrf
    fp = filename;
136 2 sinclairrf
  else:
137
    raise Exception('Unexpected argument type:  %s' % type(filename))
138
  v = list();
139
  ixLine = 0;
140 3 sinclairrf
  for tmpLine in fp:
141 2 sinclairrf
    ixLine += 1;
142
    while tmpLine and tmpLine[-1] in ('\n','\r',):
143
      tmpLine = tmpLine[0:-1];
144
    v.append((tmpLine,ixLine,));
145 3 sinclairrf
  fp.close();
146 2 sinclairrf
  return v;
147
 
148
################################################################################
149
#
150
# Unit test.
151
#
152
################################################################################
153
 
154
if __name__ == "__main__":
155
 
156
  def Test_ExtractBits(v,bits,vExpect):
157
    vGot = ExtractBits(v,bits);
158
    if vGot != vExpect:
159
      raise Exception('ExtractBits failed: 0x%04X %s ==> 0x%02X instead of 0x%02X' % (v,bits,ExtractBits(v,bits),vExpect,));
160
 
161
  for v in (256,257,510,):
162
    Test_ExtractBits(v,'[0+:8]',v%256);
163
    Test_ExtractBits(v,'[7:0]',v%256);
164
    Test_ExtractBits(v,'[4+:6]',(v/16)%64);
165
    Test_ExtractBits(v,'[9:4]',(v/16)%64);
166
 
167
  print 'Unit test passed';

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.