GenArch: gen_lc3b_arch.py

File gen_lc3b_arch.py, 6.1 kB (added by peter@tortall.net, 1 year ago)

Example LC-3b generator (uses gen_arch.py)

Line 
1 #! /usr/bin/env python2.4
2 # Yasm LC-3b Architecture Generator
3 # $Id$
4 #
5 #  Copyright (C) 2006  Peter Johnson
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
10 # 1. Redistributions of source code must retain the above copyright
11 #    notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 #    notice, this list of conditions and the following disclaimer in the
14 #    documentation and/or other materials provided with the distribution.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
17 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
20 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 # POSSIBILITY OF SUCH DAMAGE.
27
28 from gen_arch import *
29
30 config_arch(lsb_bit = 0,
31             insn_bit_width = 16,
32             machines = ["lc3b"],
33             parsers = ["nasm"])
34
35 #
36 # Registers
37 #
38
39 for x in range(8):
40     add_register("r%d" % x, None, x)
41
42 #
43 # Fields
44 #
45
46 add_field("opcode", width=4, lsb=12)
47 add_field("jsrop", width=1, lsb=11)
48 add_field("arithop", width=1, lsb=5)
49 add_field("DR", width=3, lsb=9)
50 add_field("SR1", width=3, lsb=6)
51 add_field("SR2", width=3, lsb=0)
52 add_field("imm5", width=5, lsb=0, signed=True)
53 add_field("PCoffset9", width=9, lsb=0, rshift=1, pc_rel=True, save="imm")
54 add_field("PCoffset11", width=11, lsb=0, rshift=1, pc_rel=True, save="imm")
55 add_field("offset6", width=6, lsb=0, rshift=1, signed=True, save="imm")
56 add_field("boffset6", width=6, lsb=0, signed=True, save="imm")
57 add_field("shiftArith", width=1, lsb=5)
58 add_field("shiftDir", width=1, lsb=4)
59 add_field("imm4", width=4, lsb=0)
60 add_field("trapvect8", width=8, lsb=0, rshift=1)
61
62 #
63 # Instructions
64 #
65
66 add_insn("add", "Addition (register)",
67          ["DR", "SR1", "SR2"],
68          ["reg", "reg", "reg"],
69          dict(opcode=0x1, arithop=0))
70
71 add_insn("add", "Addition (immediate)",
72          ["DR", "SR1", "imm5"],
73          ["reg", "reg", "imm"],
74          dict(opcode=0x1, arithop=1))
75
76 add_insn("and", "Bitwise logical AND (register)",
77          ["DR", "SR1", "SR2"],
78          ["reg", "reg", "reg"],
79          dict(opcode=0x5, arithop=0))
80
81 add_insn("and", "Bitwise logical AND (immediate)",
82          ["DR", "SR1", "imm5"],
83          ["reg", "reg", "imm"],
84          dict(opcode=0x5, arithop=1))
85
86 add_insn("br", "Conditional branch",
87          ["PCoffset9"],
88          ["imm"],
89          dict(opcode=0, DR=0x7))
90
91 add_derived_insn("brp", "br", fields_fixed = dict(opcode=0, DR=0x1))
92 add_derived_insn("brz", "br", fields_fixed = dict(opcode=0, DR=0x2))
93 add_derived_insn("brzp", "br", fields_fixed = dict(opcode=0, DR=0x3))
94 add_derived_insn("brn", "br", fields_fixed = dict(opcode=0, DR=0x4))
95 add_derived_insn("brnp", "br", fields_fixed = dict(opcode=0, DR=0x5))
96 add_derived_insn("brnz", "br", fields_fixed = dict(opcode=0, DR=0x6))
97 add_derived_insn("brnzp", "br", fields_fixed = dict(opcode=0, DR=0x7))
98
99 add_insn("jmp", "Jump",
100          ["BaseR"],
101          ["reg"],
102          dict(opcode=0xC),
103          dict(SR1="BaseR"))
104
105 add_insn("jsr", "Jump to subroutine",
106          ["PCoffset11"],
107          ["imm"],
108          dict(opcode=0x4, jsrop=1))
109
110 add_insn("jsrr", "Jump to subroutine",
111          ["BaseR"],
112          ["reg"],
113          dict(opcode=0x4, jsrop=0),
114          dict(SR1="BaseR"))
115
116 add_insn("ldb", "Load byte",
117          ["DR", "BaseR", "boffset6"],
118          ["reg", "reg", "imm"],
119          dict(opcode=0x2),
120          dict(DR="DR", SR1="BaseR", boffset6="boffset6"))
121
122 add_insn("ldi", "Load word indirect",
123          ["DR", "BaseR", "offset6"],
124          ["reg", "reg", "imm"],
125          dict(opcode=0xA),
126          dict(DR="DR", SR1="BaseR", offset6="offset6"))
127
128 add_insn("ldr", "Load word",
129          ["DR", "BaseR", "offset6"],
130          ["reg", "reg", "imm"],
131          dict(opcode=0x6),
132          dict(DR="DR", SR1="BaseR", offset6="offset6"))
133
134 add_insn("lea", "Load effective address",
135          ["DR", "PCoffset9"],
136          ["reg", "imm"],
137          dict(opcode=0xE))
138
139 add_insn("not", "Bitwise complement",
140          ["DR", "SR"],
141          ["reg", "reg"],
142          dict(opcode=0x9, offset6=-1),
143          dict(DR="DR", SR1="SR"))
144
145 add_insn("ret", "Return from subroutine", [], [],
146          dict(opcode=0xC, DR=0, SR1=7, offset6=0))
147
148 add_insn("rti", "Return from interrupt", [], [], dict(opcode=0x8))
149
150 add_insn("lshf", "Left shift",
151          ["DR", "SR", "imm4"],
152          ["reg", "reg", "imm"],
153          dict(opcode=0xD, shiftArith=0, shiftDir=0),
154          dict(DR="DR", SR1="SR", imm4="imm4"))
155
156 add_insn("rshf", "Right shift logical",
157          ["DR", "SR", "imm4"],
158          ["reg", "reg", "imm"],
159          dict(opcode=0xD, shiftArith=0, shiftDir=1),
160          dict(DR="DR", SR1="SR", imm4="imm4"))
161
162 add_insn("rshfa", "Right shift arithmetic",
163          ["DR", "SR", "imm4"],
164          ["reg", "reg", "imm"],
165          dict(opcode=0xD, shiftArith=1, shiftDir=1),
166          dict(DR="DR", SR1="SR", imm4="imm4"))
167
168 add_insn("stb", "Store byte",
169          ["SR", "BaseR", "boffset6"],
170          ["reg", "reg", "imm"],
171          dict(opcode=0x3),
172          dict(DR="SR", SR1="BaseR", boffset6="boffset6"))
173
174 add_insn("sti", "Store word indirect",
175          ["SR", "BaseR", "offset6"],
176          ["reg", "reg", "imm"],
177          dict(opcode=0xB),
178          dict(DR="SR", SR1="BaseR", offset6="offset6"))
179
180 add_insn("str", "Store word",
181          ["SR", "BaseR", "offset6"],
182          ["reg", "reg", "imm"],
183          dict(opcode=0x7),
184          dict(DR="SR", SR1="BaseR", offset6="offset6"))
185
186 add_insn("trap", "System call", ["trapvect8"], ["imm"], dict(opcode=0xF))
187
188 if __name__ == "__main__":
189     gen_code("lc3b")