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

Subversion Repositories ssbcc

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

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
import re
11
 
12
################################################################################
13
#
14
# Classes
15
#
16
################################################################################
17
 
18
class SSBCCException(Exception):
19
  """
20
  Exception class for ssbcc.
21
  """
22
  def __init__(self,message):
23
    self.message = message;
24
  def __str__(self):
25
    return self.message;
26
 
27
################################################################################
28
#
29
# Methods
30
#
31
################################################################################
32
 
33
def CeilLog2(v):
34
  """
35
  Return the smallest integer that has a power of 2 greater than or equal to
36
  the argument.
37
  """
38
  tmp = int(math.log(v,2));
39
  while 2**tmp < v:
40
    tmp = tmp + 1;
41
  return tmp;
42
 
43
def CeilPow2(v):
44
  """
45
  Return the smallest power of 2 greater than or equal to the argument.
46
  """
47
  return 2**CeilLog2(v);
48
 
49
def ExtractBits(v,bits):
50
  """
51
  Extract the bits specified by bits from v.
52
  bits must have a Verilog-type format.  I.e., [7:0], [0+:8], etc.
53
  """
54
  if type(v) != int:
55
    raise SSBCCException('%s must be an int' % v);
56
  if re.match(r'[[]\d+:\d+]$',bits):
57
    cmd = re.findall(r'[[](\d+):(\d+)]$',bits)[0];
58
    b0 = int(cmd[1]);
59
    bL = int(cmd[0]) - b0 + 1;
60
  elif re.match(r'[[]\d+\+:\d+]$',bits):
61
    cmd = re.findall(r'[[](\d+)\+:(\d+)]$',bits)[0];
62
    b0 = int(cmd[0]);
63
    bL = int(cmd[1]);
64
  else:
65
    raise SSBCCException('Unrecognized bit slice format:  %s' % bits);
66
  if not 1 <= bL <= 8:
67
    raise SSBCCException('Malformed range "%s" doesn\'t provide 1 to 8 bits' % bits)
68
  v /= 2**b0;
69
  v %= 2**bL;
70
  return v;
71
 
72
def IntValue(v):
73
  """
74
  Convert a Verilog format integer into an integer value.
75
  """
76
  ov = 0;
77
  for vv in [v[i] for i in range(len(v)) if '0' <= v[i] <= '9']:
78
    ov *= 10;
79
    ov += ord(vv)-ord('0');
80
  return ov;
81
 
82
def IsPowerOf2(v):
83
  """
84
  Indicate whether or not the argument is a power of 2.
85
  """
86
  return v == 2**int(math.log(v,2)+0.5);
87
 
88
def LoadFile(filename):
89
  """
90
  Load the file into a list with the line contents and line numbers.\n
91
  filename is either the name of the file or a file object.\n
92
  Note:  The file object is closed in either case.
93
  """
94
  if type(filename) == str:
95
    try:
96
      filename = file(filename);
97
    except:
98
      raise SSBCCException('Error opening "%s"' % filename);
99
  elif type(filename) == file:
100
    pass;
101
  else:
102
    raise Exception('Unexpected argument type:  %s' % type(filename))
103
  v = list();
104
  ixLine = 0;
105
  for tmpLine in filename:
106
    ixLine += 1;
107
    while tmpLine and tmpLine[-1] in ('\n','\r',):
108
      tmpLine = tmpLine[0:-1];
109
    v.append((tmpLine,ixLine,));
110
  filename.close();
111
  return v;
112
 
113
################################################################################
114
#
115
# Unit test.
116
#
117
################################################################################
118
 
119
if __name__ == "__main__":
120
 
121
  def Test_ExtractBits(v,bits,vExpect):
122
    vGot = ExtractBits(v,bits);
123
    if vGot != vExpect:
124
      raise Exception('ExtractBits failed: 0x%04X %s ==> 0x%02X instead of 0x%02X' % (v,bits,ExtractBits(v,bits),vExpect,));
125
 
126
  for v in (256,257,510,):
127
    Test_ExtractBits(v,'[0+:8]',v%256);
128
    Test_ExtractBits(v,'[7:0]',v%256);
129
    Test_ExtractBits(v,'[4+:6]',(v/16)%64);
130
    Test_ExtractBits(v,'[9:4]',(v/16)%64);
131
 
132
  print 'Unit test passed';

powered by: WebSVN 2.1.0

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