/[gxemul]/trunk/src/cpu.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

Annotation of /trunk/src/cpu.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 34 - (hide annotations)
Mon Oct 8 16:21:17 2007 UTC (16 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 15470 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.1480 2007/02/19 01:34:42 debug Exp $
20061029	Changing usleep(1) calls in the debugger to usleep(10000)
20061107	Adding a new disk image option (-d o...) which sets the ISO9660
		filesystem base offset; also making some other hacks to allow
		NetBSD/dreamcast and homebrew demos/games to boot directly
		from a filesystem image.
		Moving Dreamcast-specific stuff in the documentation to its
		own page (dreamcast.html).
		Adding a border to the Dreamcast PVR framebuffer.
20061108	Adding a -T command line option (again?), for halting the
		emulator on unimplemented memory accesses.
20061109	Continuing on various SH4 and Dreamcast related things.
		The emulator should now halt on more unimplemented device
		accesses, instead of just printing a warning, forcing me to
		actually implement missing stuff :)
20061111	Continuing on SH4 and Dreamcast stuff.
		Adding a bogus Landisk (SH4) machine mode.
20061112	Implementing some parts of the Dreamcast GDROM device. With
		some ugly hacks, NetBSD can (barely) mount an ISO image.
20061113	NetBSD/dreamcast now starts booting from the Live CD image,
		but crashes randomly quite early on in the boot process.
20061122	Beginning on a skeleton interrupt.h and interrupt.c for the
		new interrupt subsystem.
20061124	Continuing on the new interrupt system; taking the first steps
		to attempt to connect CPUs (SuperH and MIPS) and devices
		(dev_cons and SH4 timer interrupts) to it. Many things will
		probably break from now on.
20061125	Converting dev_ns16550, dev_8253 to the new interrupt system.
		Attempting to begin to convert the ISA bus.
20061130	Incorporating a patch from Brian Foley for the configure
		script, which checks for X11 libs in /usr/X11R6/lib64 (which
		is used on some Linux systems).
20061227	Adding a note in the man page about booting from Dreamcast
		CDROM images (i.e. that no external kernel is needed).
20061229	Continuing on the interrupt system rewrite: beginning to
		convert more devices, adding abort() calls for legacy interrupt
		system calls so that everything now _has_ to be rewritten!
		Almost all machine modes are now completely broken.
20061230	More progress on removing old interrupt code, mostly related
		to the ISA bus + devices, the LCA bus (on AlphaBook1), and
		the Footbridge bus (for CATS). And some minor PCI stuff.
		Connecting the ARM cpu to the new interrupt system.
		The CATS, NetWinder, and QEMU_MIPS machine modes now work with
		the new interrupt system :)
20061231	Connecting PowerPC CPUs to the new interrupt system.
		Making PReP machines (IBM 6050) work again.
		Beginning to convert the GT PCI controller (for e.g. Malta
		and Cobalt emulation). Some things work, but not everything.
		Updating Copyright notices for 2007.
20070101	Converting dev_kn02 from legacy style to devinit; the 3max
		machine mode now works with the new interrupt system :-]
20070105	Beginning to convert the SGI O2 machine to the new interrupt
		system; finally converting O2 (IP32) devices to devinit, etc.
20070106	Continuing on the interrupt system redesign/rewrite; KN01
		(PMAX), KN230, and Dreamcast ASIC interrupts should work again,
		moving out stuff from machine.h and devices.h into the
		corresponding devices, beginning the rewrite of i80321
		interrupts, etc.
20070107	Beginning on the rewrite of Eagle interrupt stuff (PReP, etc).
20070117	Beginning the rewrite of Algor (V3) interrupts (finally
		changing dev_v3 into devinit style).
20070118	Removing the "bus" registry concept from machine.h, because
		it was practically meaningless.
		Continuing on the rewrite of Algor V3 ISA interrupts.
20070121	More work on Algor interrupts; they are now working again,
		well enough to run NetBSD/algor. :-)
20070122	Converting VR41xx (HPCmips) interrupts. NetBSD/hpcmips
		can be installed using the new interrupt system :-)
20070123	Making the testmips mode work with the new interrupt system.
20070127	Beginning to convert DEC5800 devices to devinit, and to the
		new interrupt system.
		Converting Playstation 2 devices to devinit, and converting
		the interrupt system. Also fixing a severe bug: the interrupt
		mask register on Playstation 2 is bitwise _toggled_ on writes.
20070128	Removing the dummy NetGear machine mode and the 8250 device
		(which was only used by the NetGear machine).
		Beginning to convert the MacPPC GC (Grand Central) interrupt
		controller to the new interrupt system.
		Converting Jazz interrupts (PICA61 etc.) to the new interrupt
		system. NetBSD/arc can be installed again :-)
		Fixing the JAZZ timer (hardcoding it at 100 Hz, works with
		NetBSD and it is better than a completely dummy timer as it
		was before).
		Converting dev_mp to the new interrupt system, although I
		haven't had time to actually test it yet.
		Completely removing src/machines/interrupts.c, cpu_interrupt
		and cpu_interrupt_ack in src/cpu.c, and
		src/include/machine_interrupts.h! Adding fatal error messages
		+ abort() in the few places that are left to fix.
		Converting dev_z8530 to the new interrupt system.
		FINALLY removing the md_int struct completely from the
		machine struct.
		SH4 fixes (adding a PADDR invalidation in the ITLB replacement
		code in memory_sh.c); the NetBSD/dreamcast LiveCD now runs
		all the way to the login prompt, and can be interacted with :-)
		Converting the CPC700 controller (PCI and interrupt controller
		for PM/PPC) to the new interrupt system.
20070129	Fixing MACE ISA interrupts (SGI IP32 emulation). Both NetBSD/
		sgimips' and OpenBSD/sgi's ramdisk kernels can now be
		interacted with again.
20070130	Moving out the MIPS multi_lw and _sw instruction combinations
		so that they are auto-generated at compile time instead.
20070131	Adding detection of amd64/x86_64 hosts in the configure script,
		for doing initial experiments (again :-) with native code
		generation.
		Adding a -k command line option to set the size of the dyntrans
		cache, and a -B command line option to disable native code
		generation, even if GXemul was compiled with support for
		native code generation for the specific host CPU architecture.
20070201	Experimenting with a skeleton for native code generation.
		Changing the default behaviour, so that native code generation
		is now disabled by default, and has to be enabled by using
		-b on the command line.
20070202	Continuing the native code generation experiments.
		Making PCI interrupts work for Footbridge again.
20070203	More native code generation experiments.
		Removing most of the native code generation experimental code,
		it does not make sense to include any quick hacks like this.
		Minor cleanup/removal of some more legacy MIPS interrupt code.
20070204	Making i80321 interrupts work again (for NetBSD/evbarm etc.),
		and fixing the timer at 100 Hz.
20070206	Experimenting with removing the wdc interrupt slowness hack.
20070207	Lowering the number of dyntrans TLB entries for MIPS from
		192 to 128, resulting in a minor speed improvement.
		Minor optimization to the code invalidation routine in
		cpu_dyntrans.c.
20070208	Increasing (experimentally) the nr of dyntrans instructions per
		loop from 60 to 120.
20070210	Commenting out (experimentally) the dyntrans_device_danger
		detection in memory_rw.c.
		Changing the testmips and baremips machines to use a revision 2
		MIPS64 CPU by default, instead of revision 1.
		Removing the dummy i960, IA64, x86, AVR32, and HP PA-RISC
		files, the PC bios emulation, and the Olivetti M700 (ARC) and
		db64360 emulation modes.
20070211	Adding an "mp" demo to the demos directory, which tests the
		SMP functionality of the testmips machine.
		Fixing PReP interrupts some more. NetBSD/prep now boots again.
20070216	Adding a "nop workaround" for booting Mach/PMAX to the
		documentation; thanks to Artur Bujdoso for the values.
		Converting more of the MacPPC interrupt stuff to the new
		system.
		Beginning to convert BeBox interrupts to the new system.
		PPC603e should NOT have the PPC_NO_DEC flag! Removing it.
		Correcting BeBox clock speed (it was set to 100 in the NetBSD
		bootinfo block, but should be 33000000/4), allowing NetBSD
		to start without using the (incorrect) PPC_NO_DEC hack.
20070217	Implementing (slow) AltiVec vector loads and stores, allowing
		NetBSD/macppc to finally boot using the GENERIC kernel :-)
		Updating the documentation with install instructions for
		NetBSD/macppc.
20070218-19	Regression testing for the release.

==============  RELEASE 0.4.4  ==============


1 dpavlin 2 /*
2 dpavlin 34 * Copyright (C) 2005-2007 Anders Gavare. All rights reserved.
3 dpavlin 2 *
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 dpavlin 34 * $Id: cpu.c,v 1.370 2007/02/10 14:29:54 debug Exp $
29 dpavlin 2 *
30     * Common routines for CPU emulation. (Not specific to any CPU type.)
31     */
32    
33     #include <stdio.h>
34     #include <stdlib.h>
35     #include <sys/types.h>
36 dpavlin 22 #include <sys/mman.h>
37 dpavlin 2 #include <string.h>
38    
39     #include "cpu.h"
40     #include "machine.h"
41 dpavlin 12 #include "memory.h"
42 dpavlin 2 #include "misc.h"
43 dpavlin 32 #include "settings.h"
44 dpavlin 2
45    
46 dpavlin 34 extern size_t dyntrans_cache_size;
47     extern int native_code_translation_enabled;
48    
49 dpavlin 2 static struct cpu_family *first_cpu_family = NULL;
50    
51    
52     /*
53     * cpu_new():
54     *
55     * Create a new cpu object. Each family is tried in sequence until a
56     * CPU family recognizes the cpu_type_name.
57 dpavlin 32 *
58     * If there was no match, NULL is returned. Otherwise, a pointer to an
59     * initialized cpu struct is returned.
60 dpavlin 2 */
61     struct cpu *cpu_new(struct memory *mem, struct machine *machine,
62     int cpu_id, char *name)
63     {
64 dpavlin 10 struct cpu *cpu;
65 dpavlin 2 struct cpu_family *fp;
66     char *cpu_type_name;
67 dpavlin 32 char tmpstr[30];
68 dpavlin 2
69     if (name == NULL) {
70     fprintf(stderr, "cpu_new(): cpu name = NULL?\n");
71     exit(1);
72     }
73    
74     cpu_type_name = strdup(name);
75     if (cpu_type_name == NULL) {
76     fprintf(stderr, "cpu_new(): out of memory\n");
77     exit(1);
78     }
79    
80 dpavlin 12 cpu = zeroed_alloc(sizeof(struct cpu));
81 dpavlin 10
82 dpavlin 34 cpu->path = malloc(strlen(machine->path) + 15);
83     if (cpu->path == NULL) {
84     fprintf(stderr, "cpu_new(): out of memory\n");
85     exit(1);
86     }
87     snprintf(cpu->path, strlen(machine->path) + 15,
88     "%s.cpu[%i]", machine->path, cpu_id);
89    
90 dpavlin 32 cpu->memory_rw = NULL;
91     cpu->name = cpu_type_name;
92     cpu->mem = mem;
93     cpu->machine = machine;
94     cpu->cpu_id = cpu_id;
95     cpu->byte_order = EMUL_UNDEFINED_ENDIAN;
96     cpu->running = 0;
97 dpavlin 10
98 dpavlin 32 /* Create settings, and attach to the machine: */
99     cpu->settings = settings_new();
100     snprintf(tmpstr, sizeof(tmpstr), "cpu[%i]", cpu_id);
101     settings_add(machine->settings, tmpstr, 1,
102     SETTINGS_TYPE_SUBSETTINGS, 0, cpu->settings);
103    
104     settings_add(cpu->settings, "name", 0, SETTINGS_TYPE_STRING,
105     SETTINGS_FORMAT_STRING, (void *) &cpu->name);
106     settings_add(cpu->settings, "running", 0, SETTINGS_TYPE_INT,
107     SETTINGS_FORMAT_YESNO, (void *) &cpu->running);
108    
109 dpavlin 12 cpu_create_or_reset_tc(cpu);
110    
111 dpavlin 2 fp = first_cpu_family;
112    
113     while (fp != NULL) {
114     if (fp->cpu_new != NULL) {
115 dpavlin 10 if (fp->cpu_new(cpu, mem, machine, cpu_id,
116     cpu_type_name)) {
117     /* Sanity check: */
118     if (cpu->memory_rw == NULL) {
119     fatal("\ncpu_new(): memory_rw == "
120     "NULL\n");
121 dpavlin 2 exit(1);
122     }
123 dpavlin 26 break;
124 dpavlin 2 }
125     }
126    
127     fp = fp->next;
128     }
129    
130 dpavlin 26 if (fp == NULL) {
131     fatal("\ncpu_new(): unknown cpu type '%s'\n", cpu_type_name);
132     return NULL;
133     }
134    
135     fp->init_tables(cpu);
136    
137 dpavlin 32 if (cpu->byte_order == EMUL_UNDEFINED_ENDIAN) {
138     fatal("\ncpu_new(): Internal bug: Endianness not set.\n");
139     exit(1);
140     }
141    
142 dpavlin 26 return cpu;
143 dpavlin 2 }
144    
145    
146     /*
147 dpavlin 32 * cpu_destroy():
148     *
149     * Destroy a cpu object.
150     */
151     void cpu_destroy(struct cpu *cpu)
152     {
153     settings_remove(cpu->settings, "name");
154     settings_remove(cpu->settings, "running");
155    
156     /* Remove any remaining level-1 settings: */
157     settings_remove_all(cpu->settings);
158    
159     settings_destroy(cpu->settings);
160    
161 dpavlin 34 if (cpu->path != NULL)
162     free(cpu->path);
163    
164 dpavlin 32 /* TODO: This assumes that zeroed_alloc() actually succeeded
165     with using mmap(), and not malloc()! */
166     munmap((void *)cpu, sizeof(struct cpu));
167     }
168    
169    
170     /*
171 dpavlin 2 * cpu_tlbdump():
172     *
173     * Called from the debugger to dump the TLB in a readable format.
174     * x is the cpu number to dump, or -1 to dump all CPUs.
175     *
176     * If rawflag is nonzero, then the TLB contents isn't formated nicely,
177     * just dumped.
178     */
179     void cpu_tlbdump(struct machine *m, int x, int rawflag)
180     {
181     if (m->cpu_family == NULL || m->cpu_family->tlbdump == NULL)
182     fatal("cpu_tlbdump(): NULL\n");
183     else
184     m->cpu_family->tlbdump(m, x, rawflag);
185     }
186    
187    
188     /*
189     * cpu_disassemble_instr():
190     *
191     * Convert an instruction word into human readable format, for instruction
192     * tracing.
193     */
194     int cpu_disassemble_instr(struct machine *m, struct cpu *cpu,
195 dpavlin 24 unsigned char *instr, int running, uint64_t addr)
196 dpavlin 2 {
197     if (m->cpu_family == NULL || m->cpu_family->disassemble_instr == NULL) {
198     fatal("cpu_disassemble_instr(): NULL\n");
199     return 0;
200     } else
201     return m->cpu_family->disassemble_instr(cpu, instr,
202 dpavlin 24 running, addr);
203 dpavlin 2 }
204    
205    
206     /*
207     * cpu_register_dump():
208     *
209     * Dump cpu registers in a relatively readable format.
210     *
211 dpavlin 18 * gprs: set to non-zero to dump GPRs. (CPU dependent.)
212     * coprocs: set bit 0..x to dump registers in coproc 0..x. (CPU dependent.)
213 dpavlin 2 */
214     void cpu_register_dump(struct machine *m, struct cpu *cpu,
215     int gprs, int coprocs)
216     {
217     if (m->cpu_family == NULL || m->cpu_family->register_dump == NULL)
218     fatal("cpu_register_dump(): NULL\n");
219     else
220     m->cpu_family->register_dump(cpu, gprs, coprocs);
221     }
222    
223    
224     /*
225 dpavlin 24 * cpu_gdb_stub():
226     *
227     * Execute a "remote GDB" command. Return value is a pointer to a newly
228     * allocated response string, if the command was successfully executed. If
229     * there was an error, NULL is returned.
230     */
231     char *cpu_gdb_stub(struct cpu *cpu, char *cmd)
232     {
233     if (cpu->machine->cpu_family == NULL ||
234     cpu->machine->cpu_family->gdb_stub == NULL) {
235     fatal("cpu_gdb_stub(): NULL\n");
236     return NULL;
237     } else
238     return cpu->machine->cpu_family->gdb_stub(cpu, cmd);
239     }
240    
241    
242     /*
243 dpavlin 12 * cpu_functioncall_trace():
244     *
245     * This function should be called if machine->show_trace_tree is enabled, and
246     * a function call is being made. f contains the address of the function.
247     */
248     void cpu_functioncall_trace(struct cpu *cpu, uint64_t f)
249     {
250     int i, n_args = -1;
251     char *symbol;
252     uint64_t offset;
253    
254     if (cpu->machine->ncpus > 1)
255     fatal("cpu%i:\t", cpu->cpu_id);
256    
257     cpu->trace_tree_depth ++;
258 dpavlin 14 if (cpu->trace_tree_depth > 100)
259     cpu->trace_tree_depth = 100;
260 dpavlin 12 for (i=0; i<cpu->trace_tree_depth; i++)
261     fatal(" ");
262    
263     fatal("<");
264     symbol = get_symbol_name_and_n_args(&cpu->machine->symbol_context,
265     f, &offset, &n_args);
266     if (symbol != NULL)
267     fatal("%s", symbol);
268     else {
269     if (cpu->is_32bit)
270 dpavlin 24 fatal("0x%"PRIx32, (uint32_t) f);
271 dpavlin 12 else
272 dpavlin 24 fatal("0x%"PRIx64, (uint64_t) f);
273 dpavlin 12 }
274     fatal("(");
275    
276     if (cpu->machine->cpu_family->functioncall_trace != NULL)
277     cpu->machine->cpu_family->functioncall_trace(cpu, f, n_args);
278    
279     fatal(")>\n");
280 dpavlin 24
281     #ifdef PRINT_MEMORY_CHECKSUM
282     /* Temporary hack for finding bugs: */
283     fatal("call chksum=%016"PRIx64"\n", memory_checksum(cpu->mem));
284     #endif
285 dpavlin 12 }
286    
287    
288     /*
289     * cpu_functioncall_trace_return():
290     *
291     * This function should be called if machine->show_trace_tree is enabled, and
292     * a function is being returned from.
293     *
294     * TODO: Print return value? This could be implemented similar to the
295     * cpu->functioncall_trace function call above.
296     */
297     void cpu_functioncall_trace_return(struct cpu *cpu)
298     {
299     cpu->trace_tree_depth --;
300     if (cpu->trace_tree_depth < 0)
301     cpu->trace_tree_depth = 0;
302     }
303    
304    
305     /*
306     * cpu_create_or_reset_tc():
307     *
308     * Create the translation cache in memory (ie allocate memory for it), if
309     * necessary, and then reset it to an initial state.
310     */
311     void cpu_create_or_reset_tc(struct cpu *cpu)
312     {
313 dpavlin 34 size_t s = dyntrans_cache_size + DYNTRANS_CACHE_MARGIN;
314 dpavlin 12
315 dpavlin 34 if (cpu->translation_cache == NULL) {
316 dpavlin 24 cpu->translation_cache = zeroed_alloc(s);
317 dpavlin 22
318 dpavlin 34 #ifdef NATIVE_CODE_GENERATION
319     if (native_code_translation_enabled) {
320     mprotect(cpu->translation_cache, s,
321     PROT_READ | PROT_WRITE | PROT_EXEC);
322     }
323     #endif
324     }
325    
326     cpu->currently_translating_to_native = 0;
327    
328 dpavlin 12 /* Create an empty table at the beginning of the translation cache: */
329     memset(cpu->translation_cache, 0, sizeof(uint32_t)
330     * N_BASE_TABLE_ENTRIES);
331    
332     cpu->translation_cache_cur_ofs =
333     N_BASE_TABLE_ENTRIES * sizeof(uint32_t);
334    
335     /*
336     * There might be other translation pointers that still point to
337     * within the translation_cache region. Let's invalidate those too:
338     */
339 dpavlin 14 if (cpu->invalidate_code_translation != NULL)
340     cpu->invalidate_code_translation(cpu, 0, INVALIDATE_ALL);
341 dpavlin 12 }
342    
343    
344     /*
345 dpavlin 2 * cpu_dumpinfo():
346     *
347     * Dumps info about a CPU using debug(). "cpu0: CPUNAME, running" (or similar)
348 dpavlin 18 * is outputed, and it is up to CPU dependent code to complete the line.
349 dpavlin 2 */
350     void cpu_dumpinfo(struct machine *m, struct cpu *cpu)
351     {
352     debug("cpu%i: %s, %s", cpu->cpu_id, cpu->name,
353     cpu->running? "running" : "stopped");
354    
355     if (m->cpu_family == NULL || m->cpu_family->dumpinfo == NULL)
356     fatal("cpu_dumpinfo(): NULL\n");
357     else
358     m->cpu_family->dumpinfo(cpu);
359     }
360    
361    
362     /*
363     * cpu_list_available_types():
364     *
365     * Print a list of available CPU types for each cpu family.
366     */
367     void cpu_list_available_types(void)
368     {
369     struct cpu_family *fp;
370 dpavlin 22 int iadd = DEBUG_INDENTATION;
371 dpavlin 2
372     fp = first_cpu_family;
373    
374     if (fp == NULL) {
375     debug("No CPUs defined!\n");
376     return;
377     }
378    
379     while (fp != NULL) {
380     debug("%s:\n", fp->name);
381     debug_indentation(iadd);
382     if (fp->list_available_types != NULL)
383     fp->list_available_types();
384     else
385     debug("(internal error: list_available_types"
386     " = NULL)\n");
387     debug_indentation(-iadd);
388    
389     fp = fp->next;
390     }
391     }
392    
393    
394     /*
395     * cpu_run_deinit():
396     *
397     * Shuts down all CPUs in a machine when ending a simulation. (This function
398     * should only need to be called once for each machine.)
399     */
400 dpavlin 12 void cpu_run_deinit(struct machine *machine)
401 dpavlin 2 {
402     int te;
403    
404     /*
405 dpavlin 28 * Two last ticks of every hardware device. This will allow e.g.
406     * framebuffers to draw the last updates to the screen before halting.
407     *
408     * TODO: This should be refactored when redesigning the mainbus
409     * concepts!
410 dpavlin 2 */
411     for (te=0; te<machine->n_tick_entries; te++) {
412     machine->tick_func[te](machine->cpus[0],
413     machine->tick_extra[te]);
414     machine->tick_func[te](machine->cpus[0],
415     machine->tick_extra[te]);
416     }
417    
418 dpavlin 28 if (machine->show_nr_of_instructions)
419 dpavlin 10 cpu_show_cycles(machine, 1);
420 dpavlin 2
421     fflush(stdout);
422     }
423    
424    
425     /*
426     * cpu_show_cycles():
427     *
428 dpavlin 32 * If show_nr_of_instructions is on, then print a line to stdout about how
429     * many instructions/cycles have been executed so far.
430 dpavlin 2 */
431 dpavlin 10 void cpu_show_cycles(struct machine *machine, int forced)
432 dpavlin 2 {
433     uint64_t offset, pc;
434     char *symbol;
435 dpavlin 12 int64_t mseconds, ninstrs, is, avg;
436 dpavlin 2 struct timeval tv;
437 dpavlin 32 struct cpu *cpu = machine->cpus[machine->bootstrap_cpu];
438 dpavlin 2
439     static int64_t mseconds_last = 0;
440     static int64_t ninstrs_last = -1;
441    
442 dpavlin 32 pc = cpu->pc;
443 dpavlin 2
444     gettimeofday(&tv, NULL);
445 dpavlin 10 mseconds = (tv.tv_sec - machine->starttime.tv_sec) * 1000
446     + (tv.tv_usec - machine->starttime.tv_usec) / 1000;
447 dpavlin 2
448     if (mseconds == 0)
449     mseconds = 1;
450    
451     if (mseconds - mseconds_last == 0)
452     mseconds ++;
453    
454 dpavlin 28 ninstrs = machine->ninstrs_since_gettimeofday;
455 dpavlin 2
456     /* RETURN here, unless show_nr_of_instructions (-N) is turned on: */
457     if (!machine->show_nr_of_instructions && !forced)
458     goto do_return;
459    
460 dpavlin 28 printf("[ %"PRIi64" instrs", (int64_t)machine->ninstrs);
461 dpavlin 2
462     /* Instructions per second, and average so far: */
463 dpavlin 12 is = 1000 * (ninstrs-ninstrs_last) / (mseconds-mseconds_last);
464     avg = (long long)1000 * ninstrs / mseconds;
465     if (is < 0)
466     is = 0;
467     if (avg < 0)
468     avg = 0;
469 dpavlin 2
470 dpavlin 32 if (cpu->has_been_idling) {
471     printf("; idling");
472     cpu->has_been_idling = 0;
473     } else
474     printf("; i/s=%"PRIi64" avg=%"PRIi64, is, avg);
475    
476 dpavlin 2 symbol = get_symbol_name(&machine->symbol_context, pc, &offset);
477    
478 dpavlin 12 if (machine->ncpus == 1) {
479 dpavlin 32 if (cpu->is_32bit)
480 dpavlin 24 printf("; pc=0x%08"PRIx32, (uint32_t) pc);
481 dpavlin 12 else
482 dpavlin 24 printf("; pc=0x%016"PRIx64, (uint64_t) pc);
483 dpavlin 12 }
484 dpavlin 2
485 dpavlin 10 if (symbol != NULL)
486     printf(" <%s>", symbol);
487     printf(" ]\n");
488 dpavlin 2
489     do_return:
490     ninstrs_last = ninstrs;
491     mseconds_last = mseconds;
492     }
493    
494    
495     /*
496     * cpu_run_init():
497     *
498     * Prepare to run instructions on all CPUs in this machine. (This function
499     * should only need to be called once for each machine.)
500     */
501 dpavlin 12 void cpu_run_init(struct machine *machine)
502 dpavlin 2 {
503 dpavlin 28 machine->ninstrs_flush = 0;
504     machine->ninstrs = 0;
505     machine->ninstrs_show = 0;
506 dpavlin 2
507     /* For performance measurement: */
508     gettimeofday(&machine->starttime, NULL);
509 dpavlin 28 machine->ninstrs_since_gettimeofday = 0;
510 dpavlin 2 }
511    
512    
513     /*
514     * add_cpu_family():
515     *
516     * Allocates a cpu_family struct and calls an init function for the
517     * family to fill in reasonable data and pointers.
518     */
519     static void add_cpu_family(int (*family_init)(struct cpu_family *), int arch)
520     {
521     struct cpu_family *fp, *tmp;
522     int res;
523    
524     fp = malloc(sizeof(struct cpu_family));
525     if (fp == NULL) {
526     fprintf(stderr, "add_cpu_family(): out of memory\n");
527     exit(1);
528     }
529     memset(fp, 0, sizeof(struct cpu_family));
530    
531     /*
532     * family_init() returns 1 if the struct has been filled with
533     * valid data, 0 if suppor for the cpu family isn't compiled
534     * into the emulator.
535     */
536     res = family_init(fp);
537     if (!res) {
538     free(fp);
539     return;
540     }
541     fp->arch = arch;
542     fp->next = NULL;
543    
544     /* Add last in family chain: */
545     tmp = first_cpu_family;
546     if (tmp == NULL) {
547     first_cpu_family = fp;
548     } else {
549     while (tmp->next != NULL)
550     tmp = tmp->next;
551     tmp->next = fp;
552     }
553     }
554    
555    
556     /*
557     * cpu_family_ptr_by_number():
558     *
559     * Returns a pointer to a CPU family based on the ARCH_* integers.
560     */
561     struct cpu_family *cpu_family_ptr_by_number(int arch)
562     {
563     struct cpu_family *fp;
564     fp = first_cpu_family;
565    
566     /* YUCK! This is too hardcoded! TODO */
567    
568     while (fp != NULL) {
569     if (arch == fp->arch)
570     return fp;
571     fp = fp->next;
572     }
573    
574     return NULL;
575     }
576    
577    
578     /*
579     * cpu_init():
580     *
581     * Should be called before any other cpu_*() function.
582 dpavlin 32 *
583     * TODO: Make this nicer by moving out the conditional stuff to
584     * an automagically generated file? Or a define in config.h?
585 dpavlin 2 */
586     void cpu_init(void)
587     {
588     /* Note: These are registered in alphabetic order. */
589 dpavlin 12
590     #ifdef ENABLE_ALPHA
591     add_cpu_family(alpha_cpu_family_init, ARCH_ALPHA);
592     #endif
593    
594     #ifdef ENABLE_ARM
595 dpavlin 6 add_cpu_family(arm_cpu_family_init, ARCH_ARM);
596 dpavlin 12 #endif
597    
598 dpavlin 14 #ifdef ENABLE_AVR
599     add_cpu_family(avr_cpu_family_init, ARCH_AVR);
600     #endif
601    
602 dpavlin 32 #ifdef ENABLE_RCA180X
603     add_cpu_family(rca180x_cpu_family_init, ARCH_RCA180X);
604     #endif
605    
606 dpavlin 12 #ifdef ENABLE_M68K
607     add_cpu_family(m68k_cpu_family_init, ARCH_M68K);
608     #endif
609    
610     #ifdef ENABLE_MIPS
611 dpavlin 2 add_cpu_family(mips_cpu_family_init, ARCH_MIPS);
612 dpavlin 12 #endif
613    
614     #ifdef ENABLE_PPC
615 dpavlin 2 add_cpu_family(ppc_cpu_family_init, ARCH_PPC);
616 dpavlin 12 #endif
617    
618 dpavlin 14 #ifdef ENABLE_SH
619     add_cpu_family(sh_cpu_family_init, ARCH_SH);
620     #endif
621    
622 dpavlin 12 #ifdef ENABLE_SPARC
623     add_cpu_family(sparc_cpu_family_init, ARCH_SPARC);
624     #endif
625    
626 dpavlin 28 #ifdef ENABLE_TRANSPUTER
627     add_cpu_family(transputer_cpu_family_init, ARCH_TRANSPUTER);
628     #endif
629 dpavlin 2 }
630    

  ViewVC Help
Powered by ViewVC 1.1.26