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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 42 - (hide annotations)
Mon Oct 8 16:22:32 2007 UTC (16 years, 6 months ago) by dpavlin
File MIME type: text/plain
File size: 6713 byte(s)
++ trunk/HISTORY	(local)
$Id: HISTORY,v 1.1613 2007/06/15 20:11:26 debug Exp $
20070501	Continuing a little on m88k disassembly (control registers,
		more instructions).
		Adding a dummy mvme88k machine mode.
20070502	Re-adding MIPS load/store alignment exceptions.
20070503	Implementing more of the M88K disassembly code.
20070504	Adding disassembly of some more M88K load/store instructions.
		Implementing some relatively simple M88K instructions (br.n,
		xor[.u] imm, and[.u] imm).
20070505	Implementing M88K three-register and, or, xor, and jmp[.n],
		bsr[.n] including function call trace stuff.
		Applying a patch from Bruce M. Simpson which implements the
		SYSCON_BOARD_CPU_CLOCK_FREQ_ID object of the syscon call in
		the yamon PROM emulation.
20070506	Implementing M88K bb0[.n] and bb1[.n], and skeletons for
		ldcr and stcr (although no control regs are implemented yet).
20070509	Found and fixed the bug which caused Linux for QEMU_MIPS to
		stop working in 0.4.5.1: It was a faulty change to the MIPS
		'sc' and 'scd' instructions I made while going through gcc -W
		warnings on 20070428.
20070510	Updating the Linux/QEMU_MIPS section in guestoses.html to
		use mips-test-0.2.tar.gz instead of 0.1.
		A big thank you to Miod Vallat for sending me M88K manuals.
		Implementing more M88K instructions (addu, subu, div[u], mulu,
		ext[u], clr, set, cmp).
20070511	Fixing bugs in the M88K "and" and "and.u" instructions (found
		by comparing against the manual).
		Implementing more M88K instructions (mask[.u], mak, bcnd (auto-
		generated)) and some more control register details.
		Cleanup: Removing the experimental AVR emulation mode and
		corresponding devices; AVR emulation wasn't really meaningful.
		Implementing autogeneration of most M88K loads/stores. The
		rectangle drawing demo (with -O0) for M88K runs :-)
		Beginning on M88K exception handling.
		More M88K instructions: tb0, tb1, rte, sub, jsr[.n].
		Adding some skeleton MVME PROM ("BUG") emulation.
20070512	Fixing a bug in the M88K cmp instruction.
		Adding the M88K lda (scaled register) instruction.
		Fixing bugs in 64-bit (32-bit pairs) M88K loads/stores.
		Removing the unused tick_hz stuff from the machine struct.
		Implementing the M88K xmem instruction. OpenBSD/mvme88k gets
		far enough to display the Copyright banner :-)
		Implementing subu.co (guess), addu.co, addu.ci, ff0, and ff1.
		Adding a dev_mvme187, for MVME187-specific devices/registers.
		OpenBSD/mvme88k prints more boot messages. :)
20070515	Continuing on MVME187 emulation (adding more devices, beginning
		on the CMMUs, etc).
		Adding the M88K and.c, xor.c, and or.c instructions, and making
		sure that mul, div, etc cause exceptions if executed when SFD1
		is disabled.
20070517	Continuing on M88K and MVME187 emulation in general; moving
		the CMMU registers to the CPU struct, separating dev_pcc2 from
		dev_mvme187, and beginning on memory_m88k.c (BATC and PATC).
		Fixing a bug in 64-bit (32-bit pairs) M88K fast stores.
		Implementing the clock part of dev_mk48txx.
		Implementing the M88K fstcr and xcr instructions.
		Implementing m88k_cpu_tlbdump().
		Beginning on the implementation of a separate address space
		for M88K .usr loads/stores.
20070520	Removing the non-working (skeleton) Sandpoint, SonyNEWS, SHARK
		Dnard, and Zaurus machine modes.
		Experimenting with dyntrans to_be_translated read-ahead. It
		seems to give a very small performance increase for MIPS
		emulation, but a large performance degradation for SuperH. Hm.
20070522	Disabling correct SuperH ITLB emulation; it does not seem to be
		necessary in order to let SH4 guest OSes run, and it slows down
		userspace code.
		Implementing "samepage" branches for SuperH emulation, and some
		other minor speed hacks.
20070525	Continuing on M88K memory-related stuff: exceptions, memory
		transaction register contents, etc.
		Implementing the M88K subu.ci instruction.
		Removing the non-working (skeleton) Iyonix machine mode.
		OpenBSD/mvme88k reaches userland :-), starts executing
		/sbin/init's instructions, and issues a few syscalls, before
		crashing.
20070526	Fixing bugs in dev_mk48txx, so that OpenBSD/mvme88k detects
		the correct time-of-day.
		Implementing a generic IRQ controller for the test machines
		(dev_irqc), similar to a proposed patch from Petr Stepan.
		Experimenting some more with translation read-ahead.
		Adding an "expect" script for automated OpenBSD/landisk
		install regression/performance tests.
20070527	Adding a dummy mmEye (SH3) machine mode skeleton.
		FINALLY found the strange M88K bug I have been hunting: I had
		not emulated the SNIP value for exceptions occurring in
		branch delay slots correctly.
		Implementing correct exceptions for 64-bit M88K loads/stores.
		Address to symbol lookups are now disabled when M88K is
		running in usermode (because usermode addresses don't have
		anything to do with supervisor addresses).
20070531	Removing the mmEye machine mode skeleton.
20070604	Some minor code cleanup.
20070605	Moving src/useremul.c into a subdir (src/useremul/), and
		cleaning up some more legacy constructs.
		Adding -Wstrict-aliasing and -fstrict-aliasing detection to
		the configure script.
20070606	Adding a check for broken GCC on Solaris to the configure
		script. (GCC 3.4.3 on Solaris cannot handle static variables
		which are initialized to 0 or NULL. :-/)
		Removing the old (non-working) ARC emulation modes: NEC RD94,
		R94, R96, and R98, and the last traces of Olivetti M700 and
		Deskstation Tyne.
		Removing the non-working skeleton WDSC device (dev_wdsc).
20070607	Thinking about how to use the host's cc + ld at runtime to
		generate native code. (See experiments/native_cc_ld_test.i
		for an example.)
20070608	Adding a program counter sampling timer, which could be useful
		for native code generation experiments.
		The KN02_CSR_NRMMOD bit in the DECstation 5000/200 (KN02) CSR
		should always be set, to allow a 5000/200 PROM to boot.
20070609	Moving out breakpoint details from the machine struct into
		a helper struct, and removing the limit on max nr of
		breakpoints.
20070610	Moving out tick functions into a helper struct as well (which
		also gets rid of the max limit).
20070612	FINALLY figured out why Debian/DECstation stopped working when
		translation read-ahead was enabled: in src/memory_rw.c, the
		call to invalidate_code_translation was made also if the
		memory access was an instruction load (if the page was mapped
		as writable); it shouldn't be called in that case.
20070613	Implementing some more MIPS32/64 revision 2 instructions: di,
		ei, ext, dext, dextm, dextu, and ins.
20070614	Implementing an instruction combination for the NetBSD/arm
		idle loop (making the host not use any cpu if NetBSD/arm
		inside the emulator is not using any cpu).
		Increasing the nr of ARM VPH entries from 128 to 384.
20070615	Removing the ENABLE_arch stuff from the configure script, so
		that all included architectures are included in both release
		and development builds.
		Moving memory related helper functions from misc.c to memory.c.
		Adding preliminary instructions for netbooting NetBSD/pmppc to
		guestoses.html; it doesn't work yet, there are weird timeouts.
		Beginning a total rewrite of the userland emulation modes
		(removing all emulation modes, beginning from scratch with
		NetBSD/MIPS and FreeBSD/Alpha only).
20070616	After fixing a bug in the DEC21143 NIC (the TDSTAT_OWN bit was
		only cleared for the last segment when transmitting, not all
		segments), NetBSD/pmppc boots with root-on-nfs without the
		timeouts. Updating guestoses.html.
		Removing the skeleton PSP (Playstation Portable) mode.
		Moving X11-related stuff in the machine struct into a helper
		struct.
		Cleanup of out-of-memory checks, to use a new CHECK_ALLOCATION
		macro (which prints a meaningful error message).
		Adding a COMMENT to each machine and device (for automagic
		.index comment generation).
		Doing regression testing for the next release.

==============  RELEASE 0.4.6  ==============


1 dpavlin 32 /*
2 dpavlin 34 * Copyright (C) 2006-2007 Anders Gavare. All rights reserved.
3 dpavlin 32 *
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 42 * $Id: timer.c,v 1.9 2007/06/15 17:02:38 debug Exp $
29 dpavlin 32 *
30     * Timer framework. This is used by emulated clocks.
31     */
32    
33     #include <stdio.h>
34     #include <stdlib.h>
35     #include <signal.h>
36     #include <string.h>
37     #include <unistd.h>
38     #include <sys/time.h>
39    
40 dpavlin 42 #include "misc.h"
41 dpavlin 32 #include "timer.h"
42    
43    
44     /* #define TEST */
45    
46    
47     struct timer {
48     struct timer *next;
49    
50     double freq;
51     void (*timer_tick)(struct timer *timer, void *extra);
52     void *extra;
53    
54     double interval;
55     double next_tick_at;
56     };
57    
58     static struct timer *first_timer = NULL;
59     struct timeval timer_start_tv;
60     static double timer_freq;
61     static int timer_countdown_to_next_gettimeofday;
62     static double timer_current_time;
63     static double timer_current_time_step;
64    
65     static int timer_is_running;
66    
67 dpavlin 42 #define SECONDS_BETWEEN_GETTIMEOFDAY_SYNCH 1.65
68 dpavlin 32
69    
70     /*
71     * timer_add():
72     *
73     * Adds a virtual timer to the list of timers.
74     *
75     * Return value is a pointer to a timer struct.
76     */
77     struct timer *timer_add(double freq, void (*timer_tick)(struct timer *timer,
78     void *extra), void *extra)
79     {
80 dpavlin 42 struct timer *newtimer;
81 dpavlin 32
82 dpavlin 42 CHECK_ALLOCATION(newtimer = malloc(sizeof(struct timer)));
83    
84 dpavlin 32 if (freq <= 0.00000001)
85     freq = 0.00000001;
86    
87     newtimer->freq = freq;
88     newtimer->timer_tick = timer_tick;
89     newtimer->extra = extra;
90    
91     newtimer->interval = 1.0 / freq;
92     newtimer->next_tick_at = timer_current_time + newtimer->interval;
93    
94     newtimer->next = first_timer;
95     first_timer = newtimer;
96    
97     return newtimer;
98     }
99    
100    
101     /*
102     * timer_remove():
103     *
104     * Removes a virtual timer from the list of timers.
105     */
106     void timer_remove(struct timer *t)
107     {
108     struct timer *prev = NULL, *cur = first_timer;
109    
110     while (cur != NULL && cur != t) {
111     prev = cur;
112     cur = cur->next;
113     }
114    
115     if (cur == t) {
116     if (prev == NULL)
117     first_timer = cur->next;
118     else
119     prev->next = cur->next;
120     free(cur);
121     } else {
122     fprintf(stderr, "attempt to remove timer %p which "
123     "doesn't exist. aborting\n", t);
124     exit(1);
125     }
126     }
127    
128    
129     /*
130     * timer_update_frequency():
131     *
132     * Changes the frequency of an existing timer.
133     */
134     void timer_update_frequency(struct timer *t, double new_freq)
135     {
136     if (t->freq == new_freq)
137     return;
138    
139     t->freq = new_freq;
140    
141     if (new_freq <= 0.00000001)
142     new_freq = 0.00000001;
143    
144     t->interval = 1.0 / new_freq;
145     t->next_tick_at = timer_current_time + t->interval;
146     }
147    
148    
149     /*
150     * timer_tick():
151     *
152     * Timer tick handler. This is where the interesting stuff happens.
153     */
154     static void timer_tick(int signal_nr)
155     {
156     struct timer *timer = first_timer;
157     struct timeval tv;
158    
159     timer_current_time += timer_current_time_step;
160    
161     if ((--timer_countdown_to_next_gettimeofday) < 0) {
162     gettimeofday(&tv, NULL);
163     tv.tv_sec -= timer_start_tv.tv_sec;
164     tv.tv_usec -= timer_start_tv.tv_usec;
165     if (tv.tv_usec < 0) {
166     tv.tv_usec += 1000000;
167     tv.tv_sec --;
168     }
169    
170 dpavlin 42 #ifdef TIMER_DEBUG
171 dpavlin 32 /* For debugging/testing: */
172     {
173     double diff = tv.tv_usec * 0.000001 + tv.tv_sec
174     - timer_current_time;
175     printf("timer: lagging behind %f seconds\n", diff);
176     }
177     #endif
178    
179     timer_current_time = tv.tv_usec * 0.000001 + tv.tv_sec;
180    
181     timer_countdown_to_next_gettimeofday = timer_freq *
182     SECONDS_BETWEEN_GETTIMEOFDAY_SYNCH;
183     }
184    
185     while (timer != NULL) {
186     while (timer_current_time >= timer->next_tick_at) {
187     timer->timer_tick(timer, timer->extra);
188     timer->next_tick_at += timer->interval;
189     }
190    
191     timer = timer->next;
192     }
193    
194     #ifdef TEST
195     printf("T"); fflush(stdout);
196     #endif
197     }
198    
199    
200     /*
201     * timer_start():
202     *
203     * Set the interval timer to timer_freq Hz, and install the signal handler.
204     */
205     void timer_start(void)
206     {
207     struct timer *timer = first_timer;
208     struct itimerval val;
209     struct sigaction saction;
210    
211     if (timer_is_running)
212     return;
213    
214     timer_is_running = 1;
215    
216     gettimeofday(&timer_start_tv, NULL);
217     timer_current_time = 0.0;
218    
219     /* Reset all timers: */
220     while (timer != NULL) {
221     timer->next_tick_at = timer->interval;
222     timer = timer->next;
223     }
224     val.it_interval.tv_sec = 0;
225     val.it_interval.tv_usec = 1000000.0 / timer_freq;
226     val.it_value.tv_sec = 0;
227     val.it_value.tv_usec = 1000000.0 / timer_freq;
228    
229     memset(&saction, 0, sizeof(saction));
230     saction.sa_handler = timer_tick;
231    
232     sigaction(SIGALRM, &saction, NULL);
233    
234     setitimer(ITIMER_REAL, &val, NULL);
235     }
236    
237    
238     /*
239     * timer_stop():
240     *
241     * Deinstall the signal handler, and disable the interval timer.
242     */
243     void timer_stop(void)
244     {
245     struct itimerval val;
246     struct sigaction saction;
247    
248     if (!timer_is_running)
249     return;
250    
251     timer_is_running = 0;
252    
253     val.it_interval.tv_sec = 0;
254     val.it_interval.tv_usec = 0;
255     val.it_value.tv_sec = 0;
256     val.it_value.tv_usec = 0;
257    
258     setitimer(ITIMER_REAL, &val, NULL);
259    
260     memset(&saction, 0, sizeof(saction));
261     saction.sa_handler = NULL;
262    
263     sigaction(SIGALRM, &saction, NULL);
264     }
265    
266    
267     #ifdef TEST
268     static void timer_tick_test(struct timer *t, void *extra)
269     {
270     printf((char *) extra); fflush(stdout);
271     }
272     #endif
273    
274    
275     /*
276     * timer_init():
277     *
278     * Initialize the timer framework.
279     */
280     void timer_init(void)
281     {
282     first_timer = NULL;
283     timer_current_time = 0.0;
284     timer_is_running = 0;
285     timer_countdown_to_next_gettimeofday = 0;
286    
287 dpavlin 42 timer_freq = TIMER_BASE_FREQUENCY;
288 dpavlin 32 timer_current_time_step = 1.0 / timer_freq;
289    
290     #ifdef TEST
291     timer_add(0.5, timer_tick_test, "X");
292     timer_add(10.0, timer_tick_test, ".");
293     timer_add(200.0, timer_tick_test, " ");
294     timer_start();
295     while (1)
296     sleep(999999);
297     #endif
298     }
299    

  ViewVC Help
Powered by ViewVC 1.1.26