Imported Upstream version 3.0
[debian/gnuradio] / usrp / firmware / include / generate_regs.py
1 #!/usr/bin/env python
2
3 import os, os.path
4 import re
5 import sys
6
7
8 # set srcdir to the directory that contains Makefile.am
9 try:
10     srcdir = os.environ['srcdir']
11 except KeyError, e:
12     srcdir = "."
13 srcdir = srcdir + '/'
14
15 def open_src (name, mode):
16     global srcdir
17     return open (os.path.join (srcdir, name), mode)
18
19
20 def generate_fpga_regs (h_filename, v_filename):
21     const_width = 7                  # bit width of constants
22     
23     h_file = open_src (h_filename, 'r')
24     v_file = open (v_filename, 'w')
25     v_file.write (
26     '''//
27 // This file is machine generated from %s
28 // Do not edit by hand; your edits will be overwritten.
29 //
30 ''' % (h_filename,))
31
32     pat = re.compile (r'^#define\s*(FR_\w*)\s*(\w*)(.*)$')
33     pat_bitno = re.compile (r'^#define\s*(bitno\w*)\s*(\w*)(.*)$')
34     pat_bm = re.compile (r'^#define\s*(bm\w*)\s*(\w*)(.*)$')
35     for line in h_file:
36         if re.match ('//|\s*$', line):  # comment or blank line
37             v_file.write (line)
38         mo = pat.search (line)
39         mo_bitno =pat_bitno.search (line)
40         mo_bm =pat_bm.search (line)
41         if mo:
42             v_file.write ('`define %-25s %d\'d%s%s\n' % (
43                 mo.group (1), const_width, mo.group (2), mo.group (3)))
44         elif mo_bitno:
45             v_file.write ('`define %-25s %s%s\n' % (
46                 mo_bitno.group (1), mo_bitno.group (2), mo_bitno.group (3)))
47         elif mo_bm:
48             v_file.write ('`define %-25s %s%s\n' % (
49                 mo_bm.group (1), mo_bm.group (2), mo_bm.group (3)))
50     
51
52 if __name__ == '__main__':
53     if len (sys.argv) != 3:
54         sys.stderr.write ('usage: %s file.h file.v\n' % (sys.argv[0]))
55         sys.exit (1)
56     generate_fpga_regs (sys.argv[1], sys.argv[2])
57