/[gxemul]/trunk/src/cpus/generate_arm_multi.c
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/src/cpus/generate_arm_multi.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 20 - (show annotations)
Mon Oct 8 16:19:23 2007 UTC (16 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 10644 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.1055 2005/11/25 22:48:36 debug Exp $
20051031	Adding disassembly support for more ARM instructions (clz,
		smul* etc), and adding a hack to support "new tiny" pages
		for StrongARM.
20051101	Minor documentation updates (NetBSD 2.0.2 -> 2.1, and OpenBSD
		3.7 -> 3.8, and lots of testing).
		Changing from 1-sector PIO mode 0 transfers to 128-sector PIO
		mode 3 (in dev_wdc).
		Various minor ARM dyntrans updates (pc-relative loads from
		within the same page as the instruction are now treated as
		constant "mov").
20051102	Re-enabling instruction combinations (they were accidentally
		disabled).
		Dyntrans TLB entries are now overwritten using a round-robin
		scheme instead of randomly. This increases performance.
		Fixing a typo in file.c (thanks to Chuan-Hua Chang for
		noticing it).
		Experimenting with adding ATAPI support to dev_wdc (to make
		emulated *BSD detect cdroms as cdroms, not harddisks).
20051104	Various minor updates.
20051105	Continuing on the ATAPI emulation. Seems to work well enough
		for a NetBSD/cats installation, but not OpenBSD/cats.
		Various other updates.
20051106	Modifying the -Y command line option to allow scaleup with
		certain graphic controllers (only dev_vga so far), not just
		scaledown.
		Some minor dyntrans cleanups.
20051107	Beginning a cleanup up the PCI subsystem (removing the
		read_register hack, etc).
20051108	Continuing the cleanup; splitting up some pci devices into a
		normal autodev device and some separate pci glue code.
20051109	Continuing on the PCI bus stuff; all old pci_*.c have been
		incorporated into normal devices and/or rewritten as glue code
		only, adding a dummy Intel 82371AB PIIX4 for Malta (not really
		tested yet).
		Minor pckbc fix so that Linux doesn't complain.
		Working on the DEC 21143 NIC (ethernet mac rom stuff mostly).
		Various other minor fixes.
20051110	Some more ARM dyntrans fine-tuning (e.g. some instruction
		combinations (cmps followed by conditional branch within the
		same page) and special cases for DPIs with regform when the
		shifter isn't used).
20051111	ARM dyntrans updates: O(n)->O(1) for just-mark-as-non-
		writable in the generic pc_to_pointers function, and some other
		minor hacks.
		Merging Cobalt and evbmips (Malta) ISA interrupt handling,
		and some minor fixes to allow Linux to accept harddisk irqs.
20051112	Minor device updates (pckbc, dec21143, lpt, ...), most
		importantly fixing the ALI M1543/M5229 so that harddisk irqs
		work with Linux/CATS.
20051113	Some more generalizations of the PCI subsystem.
		Finally took the time to add a hack for SCSI CDROM TOCs; this
		enables OpenBSD to use partition 'a' (as needed by the OpenBSD
		installer), and Windows NT's installer to get a bit further.
		Also fixing dev_wdc to allow Linux to detect ATAPI CDROMs.
		Continuing on the DEC 21143.
20051114	Minor ARM dyntrans tweaks; ARM cmps+branch optimization when
		comparing with 0, and generalizing the xchg instr. comb.
		Adding disassembly of ARM mrrc/mcrr and q{,d}{add,sub}.
20051115	Continuing on various PPC things (BATs, other address trans-
		lation things, various loads/stores, BeBox emulation, etc.).
		Beginning to work on PPC interrupt/exception support.
20051116	Factoring out some code which initializes legacy ISA devices
		from those machines that use them (bus_isa).
		Continuing on PPC interrupt/exception support.
20051117	Minor Malta fixes: RTC year offset = 80, disabling a speed hack
		which caused NetBSD to detect a too fast cpu, and adding a new
		hack to make Linux detect a faster cpu.
		Continuing on the Artesyn PM/PPC emulation mode.
		Adding an Algor emulation skeleton (P4032 and P5064);
		implementing some of the basics.
		Continuing on PPC emulation in general; usage of unimplemented
		SPRs is now easier to track, continuing on memory/exception
		related issues, etc.
20051118	More work on PPC emulation (tgpr0..3, exception handling,
		memory stuff, syscalls, etc.).
20051119	Changing the ARM dyntrans code to mostly use cpu->pc, and not
		necessarily use arm reg 15. Seems to work.
		Various PPC updates; continuing on the PReP emulation mode.
20051120	Adding a workaround/hack to dev_mc146818 to allow NetBSD/prep
		to detect the clock.
20051121	More cleanup of the PCI bus (memory and I/O bases, etc).
		Continuing on various PPC things (decrementer and timebase,
		WDCs on obio (on PReP) use irq 13, not 14/15).
20051122	Continuing on the CPC700 controller (interrupts etc) for PMPPC,
		and on PPC stuff in general.
		Finally! After some bug fixes to the virtual to physical addr
		translation, NetBSD/{prep,pmppc} 2.1 reach userland and are
		stable enough to be interacted with.
		More PCI updates; reverse-endian device access for PowerPC etc.
20051123	Generalizing the IEEE floating point subsystem (moving it out
		from src/cpus/cpu_mips_coproc.c into a new src/float_emul.c).
		Input via slave xterms was sometimes not really working; fixing
		this for ns16550, and a warning message is now displayed if
		multiple non-xterm consoles are active.
		Adding some PPC floating point support, etc.
		Various interrupt related updates (dev_wdc, _ns16550, _8259,
		and the isa32 common code in machine.c).
		NetBSD/prep can now be installed! :-) (Well, with some manual
		commands necessary before running sysinst.) Updating the
		documentation and various other things to reflect this.
20051124	Various minor documentation updates.
		Continuing the work on the DEC 21143 NIC.
20051125	LOTS of work on the 21143. Both OpenBSD and NetBSD work fine
		with it now, except that OpenBSD sometimes gives a time-out
		warning.
		Minor documentation updates.

==============  RELEASE 0.3.7  ==============


1 /*
2 * Copyright (C) 2005 Anders Gavare. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 *
28 * $Id: generate_arm_multi.c,v 1.11 2005/11/19 18:53:07 debug Exp $
29 *
30 * Generation of commonly used ARM load/store multiple instructions.
31 *
32 * The main idea is to first check whether a load/store would be possible
33 * without going outside a page, and if so, use the host_load or _store
34 * arrays for quick access to emulated RAM. Otherwise, fall back to using
35 * the generic bdt_load() or bdt_store().
36 */
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include "misc.h"
41
42
43 /*
44 * generate_opcode():
45 *
46 * Given an ARM load/store multiple opcode, produce equivalent "hardcoded"
47 * C code which emulates the opcode.
48 *
49 * TODO:
50 *
51 * o) On 64-bit hosts, load/store two registers at a time. This
52 * feature depends both on the alignment of the base register,
53 * and the specific set of registers being loaded/stored.
54 *
55 * o) Alignment checks. (Optional?)
56 *
57 * o) For accesses that cross page boundaries, use two pages using
58 * the fast method instead of calling the generic function?
59 */
60 void generate_opcode(uint32_t opcode)
61 {
62 int p, u, s, w, load, r, n_regs, i, x;
63
64 if ((opcode & 0x0e000000) != 0x08000000) {
65 fprintf(stderr, "opcode 0x%08x is not an ldm/stm\n", opcode);
66 exit(1);
67 }
68
69 r = (opcode >> 16) & 15;
70 p = opcode & 0x01000000? 1 : 0;
71 u = opcode & 0x00800000? 1 : 0;
72 s = opcode & 0x00400000? 1 : 0;
73 w = opcode & 0x00200000? 1 : 0;
74 load = opcode & 0x00100000? 1 : 0;
75 n_regs = 0;
76 for (i=0; i<16; i++)
77 if (opcode & (1 << i))
78 n_regs ++;
79
80 /* TODO: Check for register pairs, for 64-bit load/stores */
81
82 if (n_regs == 0) {
83 fprintf(stderr, "opcode 0x%08x has no registers set\n", opcode);
84 exit(1);
85 }
86
87 if (s) {
88 fprintf(stderr, "opcode 0x%08x has s-bit set\n", opcode);
89 exit(1);
90 }
91
92 if (r == 15) {
93 fprintf(stderr, "opcode 0x%08x has r=15\n", opcode);
94 exit(1);
95 }
96
97 printf("\nvoid arm_instr_multi_0x%08x(struct cpu *cpu,"
98 " struct arm_instr_call *ic) {\n", opcode);
99
100 printf("\tunsigned char *page;\n");
101 printf("\tuint32_t addr = cpu->cd.arm.r[%i];\n", r);
102
103 if (!load && opcode & 0x8000) {
104 printf("\tuint32_t tmp_pc = ((size_t)ic - (size_t)\n\t"
105 " cpu->cd.arm.cur_ic_page) / sizeof(struct "
106 "arm_instr_call);\n"
107 "\ttmp_pc = ((cpu->pc & ~((ARM_IC_ENTRIES_PER_PAGE-1)"
108 "\n\t << ARM_INSTR_ALIGNMENT_SHIFT)))\n"
109 "\t + (tmp_pc << ARM_INSTR_ALIGNMENT_SHIFT) + 12;\n");
110 }
111
112 if (p)
113 printf("\taddr %s 4;\n", u? "+=" : "-=");
114
115 printf("\tpage = cpu->cd.arm.host_%s[addr >> 12];\n",
116 load? "load" : "store");
117
118 printf("\taddr &= 0xffc;\n");
119
120 printf("\tif (");
121 switch (p*2 + u) {
122 case 0: /* post-decrement */
123 if (n_regs > 1)
124 printf("addr >= 0x%x && ", 4*(n_regs-1));
125 break;
126 case 1: /* post-increment */
127 if (n_regs > 1)
128 printf("addr <= 0x%x && ", 0x1000 - 4*n_regs);
129 break;
130 case 2: /* pre-decrement */
131 if (n_regs > 1)
132 printf("addr >= 0x%x && ", 4*(n_regs-1));
133 break;
134 case 3: /* pre-increment */
135 if (n_regs > 1)
136 printf("addr <= 0x%x && ", 0x1000 - 4*n_regs);
137 break;
138 }
139 printf("page != NULL) {\n");
140
141 printf("\t\tuint32_t *p = (uint32_t *) (page + addr);\n");
142
143 if (u) {
144 x = 0;
145 for (i=0; i<=15; i++) {
146 if (!(opcode & (1 << i)))
147 continue;
148
149 if (load && w && i == r) {
150 /* Skip the load if we're using writeback. */
151 } else if (load) {
152 if (i == 15)
153 printf("\t\tcpu->pc = p[%i];\n", x);
154 else
155 printf("\t\tcpu->cd.arm.r[%i] = "
156 "p[%i];\n", i, x);
157 } else {
158 if (i == 15)
159 printf("\t\tp[%i] = tmp_pc;\n", x);
160 else
161 printf("\t\tp[%i] = cpu->cd.arm.r"
162 "[%i];\n", x, i);
163 }
164
165 x ++;
166 }
167 } else {
168 /* Decrementing, but do it incrementing anyway: */
169 x = -n_regs;
170 for (i=0; i<=15; i++) {
171 if (!(opcode & (1 << i)))
172 continue;
173
174 x ++;
175
176 if (load && w && i == r) {
177 /* Skip the load if we're using writeback. */
178 } else if (load) {
179 if (i == 15)
180 printf("\t\tcpu->pc = p[%i];\n", x);
181 else
182 printf("\t\tcpu->cd.arm.r[%i] = "
183 "p[%i];\n", i, x);
184 } else {
185 if (i == 15)
186 printf("\t\tp[%i] = tmp_pc;\n", x);
187 else
188 printf("\t\tp[%i] = "
189 "cpu->cd.arm.r[%i];\n", x, i);
190 }
191 }
192 }
193
194 if (w)
195 printf("\t\tcpu->cd.arm.r[%i] %s %i;\n",
196 r, u? "+=" : "-=", 4*n_regs);
197
198 if (load && opcode & 0x8000) {
199 printf("\t\tquick_pc_to_pointers(cpu);\n");
200 }
201
202 printf("\t} else\n");
203 printf("\t\tinstr(bdt_%s)(cpu, ic);\n", load? "load" : "store");
204
205 printf("}\nY(multi_0x%08x)\n", opcode);
206 }
207
208
209 /*
210 * main():
211 *
212 * Normal ARM code seems to only use about a few hundred of the 1^24 possible
213 * load/store multiple instructions. (I'm not counting the s-bit now.)
214 * Instead of having a linear array of 100s of entries, we can select a list
215 * to scan based on a few bits (*), and those lists will be shorter.
216 *
217 * (*) By running experiment_arm_multi.c on statistics gathered from running
218 * NetBSD/cats, it seems that choosing the following 8 bits results in
219 * the shortest linear lists:
220 *
221 * xxxx100P USWLnnnn llllllll llllllll
222 * ^ ^ ^ ^ ^ ^ ^ ^ (0x00950154)
223 */
224 int main(int argc, char *argv[])
225 {
226 int i, j;
227 int n_used[256];
228
229 if (argc < 2) {
230 fprintf(stderr, "usage: %s opcode [..]\n", argv[0]);
231 exit(1);
232 }
233
234 printf("\n/* AUTOMATICALLY GENERATED! Do not edit. */\n\n"
235 "#include <stdio.h>\n"
236 "#include <stdlib.h>\n"
237 "#include \"cpu.h\"\n"
238 "#include \"misc.h\"\n"
239 "#include \"arm_quick_pc_to_pointers.h\"\n"
240 "#include \"arm_tmphead_1.h\"\n"
241 "\n#define instr(x) arm_instr_ ## x\n");
242 printf("extern void arm_instr_nop(struct cpu *, "
243 "struct arm_instr_call *);\n");
244 printf("extern void arm_instr_bdt_load(struct cpu *, "
245 "struct arm_instr_call *);\n");
246 printf("extern void arm_instr_bdt_store(struct cpu *, "
247 "struct arm_instr_call *);\n");
248 printf("\n\n");
249
250 /* Generate the opcode functions: */
251 for (i=1; i<argc; i++)
252 generate_opcode(strtol(argv[i], NULL, 0));
253
254 /* Generate 256 small lookup tables: */
255 for (j=0; j<256; j++) {
256 int n = 0, zz, zz0;
257 for (i=1; i<argc; i++) {
258 zz = strtol(argv[i], NULL, 0);
259 zz = ((zz & 0x00800000) >> 16)
260 |((zz & 0x00100000) >> 14)
261 |((zz & 0x00040000) >> 13)
262 |((zz & 0x00010000) >> 12)
263 |((zz & 0x00000100) >> 5)
264 |((zz & 0x00000040) >> 4)
265 |((zz & 0x00000010) >> 3)
266 |((zz & 0x00000004) >> 2);
267 if (zz == j)
268 n++;
269 }
270 printf("\nuint32_t multi_opcode_%i[%i] = {\n", j, n+1);
271 for (i=1; i<argc; i++) {
272 zz = zz0 = strtol(argv[i], NULL, 0);
273 zz = ((zz & 0x00800000) >> 16)
274 |((zz & 0x00100000) >> 14)
275 |((zz & 0x00040000) >> 13)
276 |((zz & 0x00010000) >> 12)
277 |((zz & 0x00000100) >> 5)
278 |((zz & 0x00000040) >> 4)
279 |((zz & 0x00000010) >> 3)
280 |((zz & 0x00000004) >> 2);
281 if (zz == j)
282 printf("\t0x%08x,\n", zz0);
283 }
284 printf("0 };\n");
285 }
286
287 /* Generate 256 tables with function pointers: */
288 for (j=0; j<256; j++) {
289 int n = 0, zz, zz0;
290 for (i=1; i<argc; i++) {
291 zz = strtol(argv[i], NULL, 0);
292 zz = ((zz & 0x00800000) >> 16)
293 |((zz & 0x00100000) >> 14)
294 |((zz & 0x00040000) >> 13)
295 |((zz & 0x00010000) >> 12)
296 |((zz & 0x00000100) >> 5)
297 |((zz & 0x00000040) >> 4)
298 |((zz & 0x00000010) >> 3)
299 |((zz & 0x00000004) >> 2);
300 if (zz == j)
301 n++;
302 }
303 n_used[j] = n;
304 if (n == 0)
305 continue;
306 printf("void (*multi_opcode_f_%i[%i])(struct cpu *,"
307 " struct arm_instr_call *) = {\n", j, n*16);
308 for (i=1; i<argc; i++) {
309 zz = zz0 = strtol(argv[i], NULL, 0);
310 zz = ((zz & 0x00800000) >> 16)
311 |((zz & 0x00100000) >> 14)
312 |((zz & 0x00040000) >> 13)
313 |((zz & 0x00010000) >> 12)
314 |((zz & 0x00000100) >> 5)
315 |((zz & 0x00000040) >> 4)
316 |((zz & 0x00000010) >> 3)
317 |((zz & 0x00000004) >> 2);
318 if (zz == j) {
319 printf("\tarm_instr_multi_0x%08x__eq,\n", zz0);
320 printf("\tarm_instr_multi_0x%08x__ne,\n", zz0);
321 printf("\tarm_instr_multi_0x%08x__cs,\n", zz0);
322 printf("\tarm_instr_multi_0x%08x__cc,\n", zz0);
323 printf("\tarm_instr_multi_0x%08x__mi,\n", zz0);
324 printf("\tarm_instr_multi_0x%08x__pl,\n", zz0);
325 printf("\tarm_instr_multi_0x%08x__vs,\n", zz0);
326 printf("\tarm_instr_multi_0x%08x__vc,\n", zz0);
327 printf("\tarm_instr_multi_0x%08x__hi,\n", zz0);
328 printf("\tarm_instr_multi_0x%08x__ls,\n", zz0);
329 printf("\tarm_instr_multi_0x%08x__ge,\n", zz0);
330 printf("\tarm_instr_multi_0x%08x__lt,\n", zz0);
331 printf("\tarm_instr_multi_0x%08x__gt,\n", zz0);
332 printf("\tarm_instr_multi_0x%08x__le,\n", zz0);
333 printf("\tarm_instr_multi_0x%08x,\n", zz0);
334 printf("\tarm_instr_nop,\n");
335 }
336 }
337 printf("};\n");
338 }
339
340
341 printf("\nuint32_t *multi_opcode[256] = {\n");
342 for (i=0; i<256; i++) {
343 printf(" multi_opcode_%i,", i);
344 if ((i % 4) == 0)
345 printf("\n");
346 }
347 printf("};\n");
348
349 printf("\nvoid (**multi_opcode_f[256])(struct cpu *,"
350 " struct arm_instr_call *) = {\n");
351 for (i=0; i<256; i++) {
352 if (n_used[i] > 0)
353 printf(" multi_opcode_f_%i,", i);
354 else
355 printf(" NULL,");
356 if ((i % 4) == 0)
357 printf("\n");
358 }
359 printf("};\n");
360
361 return 0;
362 }
363

  ViewVC Help
Powered by ViewVC 1.1.26