summaryrefslogtreecommitdiff
path: root/convert.rb
blob: 96e0d639905024dcc52bf534ef20856c380b0b58 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/ruby1.9.1

require "pp"

$regname = ARGV[0]

$mode = :def

re = %r<(?x)
    ^
    \s*
    (?:
        (?'start'\d{1,2})
        (?:\s+|\s*:\s*)
        (?'stop'\d{1,2})
    |
        (?'something'\d{1,4})
    )?
    \s+
    (?'acronym'[\w\d_]+?)?
    \s*
    $
>

current = start = stop = nil

def valid_bitnumber(current, new)
    return (new == 31 or new == 15 or new == 7) if not current

    return current == new
end

def warning(current, start, line)
    expected = current
    expected ||= "7, 15 or 31"
    $stderr.puts "WARNING: Expected bit #{expected} but got bit #{start} in line \"#{line.chomp}\""
end

total_width = 0
acronym_begin = nil

# First pass: get lines
lines = []
lines << $_ until STDIN.gets.chomp.empty?

# Second pass: parse lines
lines2 = []
lines.each do |line| 
    m = re.match line

    if not m
        $stderr.puts "WARNING: Invalid line: #{line}"
        next
    end

    start = stop = nil

    if m[:start]
        start = m[:start].to_i
        if not valid_bitnumber(current, start)
            warning current, start, line
        end

        stop = m["stop"].to_i
    elsif m[:something]
        start = m[:something].to_i
        if not valid_bitnumber(current, start)
            start, stop = m[:something].scan(/^(\d{1,2})(\d{1,2})$/).first
            start, stop = start.to_i, stop.to_i
            start, stop = stop, start if start < stop
            if not valid_bitnumber(current, start)
                warning current, start, line
            end
        else
            stop = start
        end
    end
    
    if start and stop
        current = stop - 1
        width = start - stop + 1
        total_width += width
    end

    lines2 << {
        :line => line,
        :acronym => (m[:acronym] or "") ,
        :start => start,
        :stop => stop,
        :width => width
    }

end

# Third pass: assemble cut up field names
lines3 = []
lines2.length.times do |i|
    if lines2[i][:start] and lines2[i][:stop]
        lines3 << lines2[i]
    else
        prev = lines3[-1]
        cur = lines2[i]
        next_ = lines2[i+1]

        if prev and next_
            puts "WARNING: the copy-paste resulte is ambiguous:"
            puts "----"
            print prev[:line]
            print cur[:line]
            print next_[:line]
            puts "----"
            puts "Are the field names"
            print ' (1) "'
            print prev[:acronym] + cur[:acronym]
            print '" and "'
            print next_[:acronym]
            puts '"'
            print ' (2) "'
            print prev[:acronym]
            print '" and "'
            print cur[:acronym] + next_[:acronym]
            puts '"'

            num = nil
            loop do
                print "Choice: "
                num = STDIN.gets.to_i
                break if num == 1 or num == 2
            end

            if num == 1
                prev[:acronym] = prev[:acronym] + cur[:acronym]
            else
                next_[:acronym] = cur[:acronym] + next_[:acronym]
            end
        elsif prev
            prev[:acronym] = prev[:acronym] + cur[:acronym]
        elsif next_
            next_[:acronym] = cur[:acronym] + next_[:acronym]
        else
            puts "WARNING: WTF? line #{i} has no prev nor next line"
            pp lines
            pp lines2
            pp lines3
        end
    end
end

lines3.each do |line|
    acronym, start, stop, width = line[:acronym], line[:start], line[:stop], line[:width]
    name = (acronym =~ /reserved|rsvd/i ? "" : ($regname ? $regname + "_" : "") + acronym)
    
    if $mode == :struct
        tabs = "\t" * [2 - name.length / 8, 0].max
        puts "\t#{name}#{tabs}:#{width},"
    elsif $mode == :def
        if name != ""
            tabs = "\t" * [2 - name.length / 8, 1].max
            puts "#define #{name}#{tabs}#{stop}"
        end
    end

end

if total_width != 8 and total_width != 16 and total_width != 32
    $stderr.puts "WARNING: Total register width is #{total_width} bits"
end