summaryrefslogtreecommitdiffstatshomepage
path: root/src/emu/cpu/mcs96/mcs96make.py
blob: 8381e63dc2f6d3037e0128c2f4dbea5360d3420a (plain) (blame)
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/python

USAGE = """
Usage:
%s mcs96ops.lst mcs96.inc
"""
import sys

def save_full_one(f, t, name, source):
    print >>f, "void %s_device::%s_full()" % (t, name)
    print >>f, "{"
    for line in source:
        print >>f, line
    print >>f, "}"
    print >>f

class Opcode:
    def __init__(self, rng, name, amode, is_196, ea):
        rng1 = rng.split("-")
        self.rng_start = int(rng1[0], 16)
        if len(rng1) == 2:
            self.rng_end = int(rng1[1], 16)
        else:
            self.rng_end = self.rng_start
        self.name = name
        self.amode = amode
        self.source = []
        self.is_196 = is_196
        if amode in ea:
            for line in ea[amode].source:
                self.source.append(line)

    def add_source_line(self, line):
        self.source.append(line)

class Special:
    def __init__(self, name):
        self.name = name
        self.source = []

    def add_source_line(self, line):
        self.source.append(line)

class Macro:
    def __init__(self, tokens):
        self.name = tokens[1]
        self.params = []
        for i in range(2, len(tokens)):
            self.params.append(tokens[i])
        self.source = []

    def add_source_line(self, line):
        self.source.append(line)

    def apply(self, target, tokens):
        values = []
        for i in range(1, len(tokens)):
            values.append(tokens[i])
        for i in range(0, len(self.source)):
            line = self.source[i]
            for j in range(0, len(self.params)):
                line = line.replace(self.params[j], values[j])
            target.add_source_line(line)

class OpcodeList:
    def __init__(self, fname, is_196):
        self.opcode_info = []
        self.opcode_per_id = {}
        self.ea = {}
        self.macros = {}
        try:
            f = open(fname, "rU")
        except Exception:
            err = sys.exc_info()[1]
            sys.stderr.write("Cannot read opcodes file %s [%s]\n" % (fname, err))
            sys.exit(1)

        inf = None
        for line in f:
            if line.startswith("#"):
                continue
            line = line.rstrip()
            if not line:
                continue
            if line.startswith(" ") or line.startswith("\t"):
                # append instruction to last opcode, maybe expand a macro
                tokens = line.split()
                if tokens[0] in self.macros:
                    self.macros[tokens[0]].apply(inf, tokens)
                else:
                    inf.add_source_line(line)
            else:
                # New something
                tokens = line.split()
                #   Addressing mode header
                if tokens[0] == "eadr":
                    inf = Special(tokens[1])
                    self.ea[inf.name] = inf
                elif tokens[0] == "fetch":
                    inf = Special(tokens[0])
                    self.fetch = inf
                elif tokens[0] == "fetch_noirq":
                    inf = Special(tokens[0])
                    self.fetch_noirq = inf
                elif tokens[0] == "macro":
                    inf = Macro(tokens)
                    self.macros[inf.name] = inf
                else:
                    inf = Opcode(tokens[0], tokens[1], tokens[2], len(tokens) >= 4 and tokens[3] == "196", self.ea)
                    self.opcode_info.append(inf)
                    if is_196 or not inf.is_196:
                        for i in range(inf.rng_start, inf.rng_end+1):
                            self.opcode_per_id[i] = inf

    def save_dasm(self, f, t):
        print >>f, "const %s_device::disasm_entry %s_device::disasm_entries[0x100] = {" % (t, t)
        for i in range(0, 0x100):
            if i in self.opcode_per_id:
                opc = self.opcode_per_id[i]
                alt = "NULL"
                if i + 0xfe00 in self.opcode_per_id:
                    alt = "\"" + self.opcode_per_id[i+0xfe00].name + "\""
                if opc.name == "scall" or opc.name == "lcall":
                    flags = "DASMFLAG_STEP_OVER"
                elif opc.name == "rts":
                    flags = "DASMFLAG_STEP_OUT"
                else:
                    flags = "0"
                print >>f, "\t{ \"%s\", %s, DASM_%s, %s }," % (opc.name, alt, opc.amode, flags)
            else:
                print >>f, "\t{ \"???\", NULL, DASM_none, 0 },"
        print >>f, "};"
        print >>f
    
    def save_opcodes(self, f, t):
        pf = ""
        is_196 = False
        if t == "i8xc196":
            pf = "_196"
            is_196 = True
        for opc in self.opcode_info:
            if opc.is_196 == is_196:
                save_full_one(f, t, opc.name + "_" + opc.amode + pf, opc.source)
        if not is_196:
            save_full_one(f, t, "fetch", self.fetch.source)
            save_full_one(f, t, "fetch_noirq", self.fetch_noirq.source)
    
    def save_exec(self, f, t):
        print >>f, "void %s_device::do_exec_full()" % t
        print >>f, "{"
        print >>f, "\tswitch(inst_state) {"
        for i in range(0x000, 0x200):
            opc = None
            if i >= 0x100 and i-0x100+0xfe00 in self.opcode_per_id:
                opc = self.opcode_per_id[i-0x100+0xfe00]
            if opc is None and (i & 0xff) in self.opcode_per_id:
                opc = self.opcode_per_id[i & 0xff]
            if opc is not None:
                nm = opc.name + "_" + opc.amode
                if opc.is_196:
                    nm += "_196"
                print >>f, "\tcase 0x%03x: %s_full(); break;" % (i, nm)
        print >>f, "\tcase 0x200: fetch_full(); break;"
        print >>f, "\tcase 0x201: fetch_noirq_full(); break;"
        print >>f, "\t}"
        print >>f, "}"

def main(argv):
    if len(argv) != 4:
        print USAGE % argv[0]
        return 1
    
    t = argv[1]
    opcodes = OpcodeList(argv[2], t == "i8xc196")
    
    try:
        f = open(argv[3], "w")
    except Exception:
        err = sys.exc_info()[1]
        sys.stderr.write("cannot write file %s [%s]\n" % (argv[3], err))
        sys.exit(1)
    
    if t != "mcs96":
        opcodes.save_dasm(f, t)
    if t != "i8x9x":
        opcodes.save_opcodes(f, t)
    if t != "mcs96":
        opcodes.save_exec(f, t)
    f.close()

# ======================================================================
if __name__ == "__main__":
    sys.exit(main(sys.argv))