summaryrefslogtreecommitdiff
path: root/scripts/createheader.py
blob: fa8b2088564f9400a406db69e18a97e304a418a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/python

# Author: Andreas Herrmann <andreas.herrmann3@amd.com>
#
# Copyright (C) 2008 Advanced Micro Devices, Inc.
#
# Licensed under the terms of the GNU GENERAL PUBLIC LICENSE version 2.
# See file COPYING for details.


# This script creates C header files from some register
# description files.
#
# The general format for those register descriptions is
#
#	{<name>=<address>;[<description>]
#	[<field name>]:<bit width>[;<value>=<string>|]
#	[<field name>]:<bit width>[;<value>=<string>|]
#	...
#	[<field name>]:<bit width>[;<value>=<string>|]
#       }
#
# If no field name is provided the field is reserved and might
# not be displayed with the msr viewing tool. The optional <value>=<string>
# data is for bit field decoding but it's currently not yet supported.

# Todos/Nice-to-have:
# - comments in this script (?)
# - create information for bit field decoding
# - warn if names contain whitespace characters
# - add include/insert statement to avoid dup definitions of common register
# - allow annotations for registers (e.g. write-only)
# - improve command line handling

import re
import sys
import os

progname=os.path.basename(sys.argv[0])

if len(sys.argv) < 3:
    print(progname + ": invalid number of argumenst")
    print("\nUSAGE:")
    print(progname + " <input file> <struct name>")
    print("\nEXAMPLE:")
    print(progname + " k8regs.txt k8 >k8.h")
    sys.exit(1)

f=open(sys.argv[1])
family=sys.argv[2]

print(\
"/*\n"\
" * Licensed under the terms of the GNU GENERAL PUBLIC LICENSE version 2.\n"\
" * See file COPYING for details.\n"\
" */\n")
print("#ifndef " + family + "_h")
print("#define " + family + "_h\n")
if (re.search("/", sys.argv[1])):
    print("#include \"../msr.h\"\n")
else:
    print("#include \"msr.h\"\n")
struct=[]
for line in f:
    if (re.search("^#",line)):
        continue
    if (re.search("^{",line)):
        start=1; end=0
        tmp=line.split("{")[1]
        tmp=tmp.split("=")
        reg_name=tmp[0].strip()
        tmp=tmp[1].split(";")
        reg_addr=tmp[0].strip()
        if len(tmp) > 1:
            reg_desc=tmp[1].strip()
        else:
            reg_desc=""
        spec=[]
    if (re.search("^}",line)):
        end=1
        str1="_RANGE(" + family + "_" + reg_name
        str2="_NAMES(" + family + "_" + reg_name
        for e in spec:
            if e[0]=="":
                str2 += ",0"
            else:
                str2 += ",\"" + e[0] +"\""
            str1 += "," + e[1]
        str1 += ",0);"
        str2 += ");"
        print(str1)
        print(str2)
        str3 = "_SPEC(" + reg_addr + ", " + reg_name + ", " 
        if reg_desc != "":
            str3 += "\"" + reg_desc + "\", "
        else:
             str3 += "NULL, "
        str3 +=  family + "_)"
        struct.append(str3)
    if (re.search(":", line)):
        tmp=line.split(";")
        field=tmp[0].split(":")
        fname=field[0].strip()
        fwidth=field[1].strip()
        spec.append([fname, fwidth])

print("\nstruct reg_spec " + family + "_spec [] = {")
for i in struct:
    print("\t" + i + ",")
print("	{0, NULL, NULL, NULL, NULL},")
print("};\n")
print("#endif /* " + family + "_h */")
f.close()