aboutsummaryrefslogtreecommitdiff
path: root/sim/common/sim-events.c
blob: 3d2d005266d76c80592e5232f86c9d7db9706afc (plain)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
/*  This file is part of the program psim.

    Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
    */


#ifndef _SIM_EVENTS_C_
#define _SIM_EVENTS_C_

#include "sim-main.h"
#include "sim-assert.h"

#ifdef HAVE_STRING_H
#include <string.h>
#else
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#endif

#include <signal.h>


typedef enum {
  watch_invalid,

  /* core - target byte order */
  watch_core_targ_1,
  watch_core_targ_2,
  watch_core_targ_4,
  watch_core_targ_8,
  /* core - big-endian */
  watch_core_be_1,
  watch_core_be_2,
  watch_core_be_4,
  watch_core_be_8,
  /* core - little-endian */
  watch_core_le_1,
  watch_core_le_2,
  watch_core_le_4,
  watch_core_le_8,

  /* sim - host byte order */
  watch_sim_host_1,
  watch_sim_host_2,
  watch_sim_host_4,
  watch_sim_host_8,
  /* sim - big-endian */
  watch_sim_be_1,
  watch_sim_be_2,
  watch_sim_be_4,
  watch_sim_be_8,
  /* sim - little-endian */
  watch_sim_le_1,
  watch_sim_le_2,
  watch_sim_le_4,
  watch_sim_le_8,
  
  /* wallclock */
  watch_clock,

  /* timer */
  watch_timer,
} sim_event_watchpoints;


struct _sim_event {
  sim_event_watchpoints watching;
  void *data;
  sim_event_handler *handler;
  /* timer event */
  signed64 time_of_event;
  /* watch wallclock event */
  unsigned wallclock;
  /* watch core address */
  address_word core_addr;
  sim_core_maps core_map;
  /* watch sim addr */
  void *host_addr;
  /* watch core/sim range */
  int is_within; /* 0/1 */
  unsigned ub;
  unsigned lb;
  unsigned64 ub64;
  unsigned64 lb64;
  /* list */
  sim_event *next;
};


/* The event queue maintains a single absolute time using two
   variables.
   
   TIME_OF_EVENT: this holds the time at which the next event is ment
   to occure.  If no next event it will hold the time of the last
   event.

   TIME_FROM_EVENT: The current distance from TIME_OF_EVENT.  If an
   event is pending, this will be positive.  If no future event is
   pending (eg when poll-event is being processed) this will be
   negative.  This variable is decremented once for each iteration of
   a clock cycle.

   Initially, the clock is started at time one (0) with TIME_OF_EVENT
   == 0 and TIME_FROM_EVENT == 0.

   Clearly there is a bug in that this code assumes that the absolute
   time counter will never become greater than 2^62.

   To avoid the need to use 64bit arithmetic, the event queue always
   contains at least one event scheduled every 16 000 ticks.  This
   limits the time from event counter to values less than
   16 000. */


#if !defined (SIM_EVENTS_POLL_RATE)
#define SIM_EVENTS_POLL_RATE 0x100000
#endif


#define _ETRACE sd

#undef ETRACE
#define ETRACE(ARGS) \
do \
  { \
    if (WITH_TRACE) \
      { \
        if (STATE_EVENTS (sd)->trace) \
          { \
            const char *file; \
            SIM_FILTER_PATH (file, __FILE__); \
            sim_io_printf (sd, "%s:%d: ", file, __LINE__); \
            sim_io_printf  ARGS; \
          } \
      } \
  } \
while (0)


/* event queue iterator - don't iterate over the held queue. */

STATIC_INLINE_SIM_EVENTS\
(sim_event **)
next_event_queue (SIM_DESC sd,
		  sim_event **queue)
{
  if (queue == NULL)
    return &STATE_EVENTS (sd)->queue;
  else if (queue == &STATE_EVENTS (sd)->queue)
    return &STATE_EVENTS (sd)->watchpoints;
  else if (queue == &STATE_EVENTS (sd)->watchpoints)
    return &STATE_EVENTS (sd)->watchedpoints;
  else if (queue == &STATE_EVENTS (sd)->watchedpoints)
    return NULL;
  else
    sim_io_error (sd, "next_event_queue - bad queue");
  return NULL;
}


STATIC_INLINE_SIM_EVENTS\
(void)
sim_events_poll (SIM_DESC sd,
		 void *data)
{
  /* just re-schedule in 1000 million ticks time */
  sim_events_schedule (sd, SIM_EVENTS_POLL_RATE, sim_events_poll, sd);
  sim_io_poll_quit (sd);
}


/* "events" module install handler.
   This is called via sim_module_install to install the "events" subsystem
   into the simulator.  */

EXTERN_SIM_EVENTS\
(SIM_RC)
sim_events_install (SIM_DESC sd)
{
  SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  sim_module_add_uninstall_fn (sd, sim_events_uninstall);
  sim_module_add_init_fn (sd, sim_events_init);
  return SIM_RC_OK;
}


/* Uninstall the "events" subsystem from the simulator.  */

EXTERN_SIM_EVENTS\
(void)
sim_events_uninstall (SIM_DESC sd)
{
  /* FIXME: free buffers, etc. */
}


/* malloc/free */

STATIC_INLINE_SIM_EVENTS\
(sim_event *)
sim_events_zalloc (SIM_DESC sd)
{
  sim_events *events = STATE_EVENTS (sd);
  sim_event *new = events->free_list;
  if (new != NULL)
    {
      events->free_list = new->next;
      memset (new, 0, sizeof (*new));
    }
  else
    {
#if defined (HAVE_SIGPROCMASK) && defined (SIG_SETMASK)
      /*-LOCK-*/
      sigset_t old_mask;
      sigset_t new_mask;
      sigfillset(&new_mask);
      sigprocmask (SIG_SETMASK, &new_mask, &old_mask);
#endif
      new = ZALLOC (sim_event);
#if defined (HAVE_SIGPROCMASK) && defined (SIG_SETMASK)
      /*-UNLOCK-*/
      sigprocmask (SIG_SETMASK, &old_mask, NULL);
#endif
    }
  return new;
}

STATIC_INLINE_SIM_EVENTS\
(void)
sim_events_free (SIM_DESC sd,
		 sim_event *dead)
{
  sim_events *events = STATE_EVENTS (sd);
  dead->next = events->free_list;
  events->free_list = dead;
}


/* Initialize the simulator event manager */

EXTERN_SIM_EVENTS\
(SIM_RC)
sim_events_init (SIM_DESC sd)
{
  sim_events *events = STATE_EVENTS (sd);

  /* drain the interrupt queue */
  events->nr_held = 0;
  if (events->held == NULL)
    events->held = zalloc (sizeof (sim_event) * MAX_NR_SIGNAL_SIM_EVENTS);

  /* drain the normal queues */
  {
    sim_event **queue = NULL;
    while ((queue = next_event_queue (sd, queue)) != NULL)
      {
	if (queue == NULL) break;
	while (*queue != NULL)
	  {
	    sim_event *dead = *queue;
	    *queue = dead->next;
	    sim_events_free (sd, dead);
	  }
	*queue = NULL;
      }
  }

  /* wind time back to zero */
  events->nr_ticks_to_process = 1; /* start by doing queue */
  events->time_of_event = 0;
  events->time_from_event = 0;
  events->initial_wallclock = sim_elapsed_time_get ();

  /* schedule our initial counter event */
  sim_events_schedule (sd, 0, sim_events_poll, sd);

  /* from now on, except when the large-int event is being processed
     the event queue is non empty */
  SIM_ASSERT (events->queue != NULL);

  return SIM_RC_OK;
}


INLINE_SIM_EVENTS\
(signed64)
sim_events_time (SIM_DESC sd)
{
  sim_events *events = STATE_EVENTS (sd);
  return events->time_of_event - events->time_from_event;
}


STATIC_INLINE_SIM_EVENTS\
(void)
update_time_from_event (SIM_DESC sd)
{
  sim_events *events = STATE_EVENTS (sd);
  signed64 current_time = sim_events_time (sd);
  if (events->queue != NULL)
    {
      events->time_from_event = (events->queue->time_of_event - current_time);
      events->time_of_event = events->queue->time_of_event;
    }
  else
    {
      events->time_of_event = current_time - 1;
      events->time_from_event = -1;
    }
  SIM_ASSERT (current_time == sim_events_time (sd));
  SIM_ASSERT ((events->time_from_event >= 0) == (events->queue != NULL));
}


STATIC_INLINE_SIM_EVENTS\
(void)
insert_sim_event (SIM_DESC sd,
		  sim_event *new_event,
		  signed64 delta)
{
  sim_events *events = STATE_EVENTS (sd);
  sim_event *curr;
  sim_event **prev;
  signed64 time_of_event;

  if (delta < 0)
    sim_io_error (sd, "what is past is past!\n");
  
  /* compute when the event should occure */
  time_of_event = sim_events_time (sd) + delta;
  
  /* find the queue insertion point - things are time ordered */
  prev = &events->queue;
  curr = events->queue;
  while (curr != NULL && time_of_event >= curr->time_of_event)
    {
      SIM_ASSERT (curr->next == NULL
		  || curr->time_of_event <= curr->next->time_of_event);
      prev = &curr->next;
      curr = curr->next;
    }
  SIM_ASSERT (curr == NULL || time_of_event < curr->time_of_event);
  
  /* insert it */
  new_event->next = curr;
  *prev = new_event;
  new_event->time_of_event = time_of_event;
  
  /* adjust the time until the first event */
  update_time_from_event (sd);
}


EXTERN_SIM_EVENTS\
(sim_event *)
sim_events_schedule (SIM_DESC sd,
		     signed64 delta_time,
		     sim_event_handler *handler,
		     void *data)
{
  sim_event *new_event = sim_events_zalloc (sd);
  new_event->data = data;
  new_event->handler = handler;
  new_event->watching = watch_timer;
  insert_sim_event(sd, new_event, delta_time);
  ETRACE((_ETRACE,
	  "event scheduled at %ld - tag 0x%lx - time %ld, handler 0x%lx, data 0x%lx\n",
	  (long)sim_events_time(sd),
	  (long)new_event,
	  (long)new_event->time_of_event,
	  (long)new_event->handler,
	  (long)new_event->data));
  return new_event;
}


EXTERN_SIM_EVENTS\
(void)
sim_events_schedule_after_signal (SIM_DESC sd,
				  signed64 delta_time,
				  sim_event_handler *handler,
				  void *data)
{
  sim_events *events = STATE_EVENTS (sd);
  sim_event *new_event;
#if defined (HAVE_SIGPROCMASK) && defined (SIG_SETMASK)
  /*-LOCK-*/
  sigset_t old_mask;
  sigset_t new_mask;
  sigfillset(&new_mask);
  sigprocmask (SIG_SETMASK, &new_mask, &old_mask);
#endif
  
  /* allocate an event entry from the signal buffer */
  new_event = &events->held [events->nr_held];
  events->nr_held ++;
  if (events->nr_held >= MAX_NR_SIGNAL_SIM_EVENTS)
    {
      sim_engine_abort (NULL, NULL, NULL_CIA,
			"sim_events_schedule_after_signal - buffer oveflow");
    }
  
  new_event->data = data;
  new_event->handler = handler;
  new_event->time_of_event = delta_time; /* work it out later */
  new_event->next = NULL;

  events->work_pending = 1; /* notify main process */

#if defined (HAVE_SIGPROCMASK) && defined (SIG_SETMASK)
  /*-UNLOCK-*/
  sigprocmask (SIG_SETMASK, &old_mask, NULL);
#endif
  
  ETRACE ((_ETRACE,
	   "signal scheduled at %ld - tag 0x%lx - time %ld, handler 0x%lx, data 0x%lx\n",
	   (long)sim_events_time(sd),
	   (long)new_event,
	   (long)new_event->time_of_event,
	   (long)new_event->handler,
	   (long)new_event->data));
}


EXTERN_SIM_EVENTS\
(sim_event *)
sim_events_watch_clock (SIM_DESC sd,
			unsigned delta_ms_time,
			sim_event_handler *handler,
			void *data)
{
  sim_events *events = STATE_EVENTS (sd);
  sim_event *new_event = sim_events_zalloc (sd);
  /* type */
  new_event->watching = watch_clock;
  /* handler */
  new_event->data = data;
  new_event->handler = handler;
  /* data */
  new_event->wallclock = (sim_elapsed_time_since (events->initial_wallclock) + delta_ms_time);
  /* insert */
  new_event->next = events->watchpoints;
  events->watchpoints = new_event;
  events->work_pending = 1;
  ETRACE ((_ETRACE,
	  "event watching clock at %ld - tag 0x%lx - wallclock %ld, handler 0x%lx, data 0x%lx\n",
	   (long)sim_events_time (sd),
	   (long)new_event,
	   (long)new_event->wallclock,
	   (long)new_event->handler,
	   (long)new_event->data));
  return new_event;
}


EXTERN_SIM_EVENTS\
(sim_event *)
sim_events_watch_sim (SIM_DESC sd,
		      void *host_addr,
		      int nr_bytes,
		      int byte_order,
		      int is_within,
		      unsigned64 lb,
		      unsigned64 ub,
		      sim_event_handler *handler,
		      void *data)
{
  sim_events *events = STATE_EVENTS (sd);
  sim_event *new_event = sim_events_zalloc (sd);
  /* type */
  switch (byte_order)
    {
    case 0:
      switch (nr_bytes)
	{
	case 1: new_event->watching = watch_sim_host_1; break;
	case 2: new_event->watching = watch_sim_host_2; break;
	case 4: new_event->watching = watch_sim_host_4; break;
	case 8: new_event->watching = watch_sim_host_8; break;
	default: sim_io_error (sd, "sim_events_watch_sim - invalid nr bytes");
	}
      break;
    case BIG_ENDIAN:
      switch (nr_bytes)
	{
	case 1: new_event->watching = watch_sim_be_1; break;
	case 2: new_event->watching = watch_sim_be_2; break;
	case 4: new_event->watching = watch_sim_be_4; break;
	case 8: new_event->watching = watch_sim_be_8; break;
	default: sim_io_error (sd, "sim_events_watch_sim - invalid nr bytes");
	}
      break;
    case LITTLE_ENDIAN:
      switch (nr_bytes)
	{
	case 1: new_event->watching = watch_sim_le_1; break;
	case 2: new_event->watching = watch_sim_le_2; break;
	case 4: new_event->watching = watch_sim_le_4; break;
	case 8: new_event->watching = watch_sim_le_8; break;
	default: sim_io_error (sd, "sim_events_watch_sim - invalid nr bytes");
	}
      break;
    default:
      sim_io_error (sd, "sim_events_watch_sim - invalid byte order");
    }
  /* handler */
  new_event->data = data;
  new_event->handler = handler;
  /* data */
  new_event->host_addr = host_addr;
  new_event->lb = lb;
  new_event->lb64 = lb;
  new_event->ub = ub;
  new_event->ub64 = ub;
  new_event->is_within = (is_within != 0);
  /* insert */
  new_event->next = events->watchpoints;
  events->watchpoints = new_event;
  events->work_pending = 1;
  ETRACE ((_ETRACE,
	   "event watching host at %ld - tag 0x%lx - host-addr 0x%lx, 0x%lx..0x%lx, handler 0x%lx, data 0x%lx\n",
	   (long)sim_events_time (sd),
	   (long)new_event,
	   (long)new_event->host_addr,
	   (long)new_event->lb,
	   (long)new_event->ub,
	   (long)new_event->handler,
	   (long)new_event->data));
  return new_event;
}


EXTERN_SIM_EVENTS\
(sim_event *)
sim_events_watch_core (SIM_DESC sd,
		       address_word core_addr,
		       sim_core_maps core_map,
		       int nr_bytes,
		       int byte_order,
		       int is_within,
		       unsigned64 lb,
		       unsigned64 ub,
		       sim_event_handler *handler,
		       void *data)
{
  sim_events *events = STATE_EVENTS (sd);
  sim_event *new_event = sim_events_zalloc (sd);
  /* type */
  switch (byte_order)
    {
    case 0:
      switch (nr_bytes)
	{
	case 1: new_event->watching = watch_core_targ_1; break;
	case 2: new_event->watching = watch_core_targ_2; break;
	case 4: new_event->watching = watch_core_targ_4; break;
	case 8: new_event->watching = watch_core_targ_8; break;
	default: sim_io_error (sd, "sim_events_watch_core - invalid nr bytes");
	}
      break;
    case BIG_ENDIAN:
      switch (nr_bytes)
	{
	case 1: new_event->watching = watch_core_be_1; break;
	case 2: new_event->watching = watch_core_be_2; break;
	case 4: new_event->watching = watch_core_be_4; break;
	case 8: new_event->watching = watch_core_be_8; break;
	default: sim_io_error (sd, "sim_events_watch_core - invalid nr bytes");
	}
      break;
    case LITTLE_ENDIAN:
      switch (nr_bytes)
	{
	case 1: new_event->watching = watch_core_le_1; break;
	case 2: new_event->watching = watch_core_le_2; break;
	case 4: new_event->watching = watch_core_le_4; break;
	case 8: new_event->watching = watch_core_le_8; break;
	default: sim_io_error (sd, "sim_events_watch_core - invalid nr bytes");
	}
      break;
    default:
      sim_io_error (sd, "sim_events_watch_core - invalid byte order");
    }
  /* handler */
  new_event->data = data;
  new_event->handler = handler;
  /* data */
  new_event->core_addr = core_addr;
  new_event->core_map = core_map;
  new_event->lb = lb;
  new_event->lb64 = lb;
  new_event->ub = ub;
  new_event->ub64 = ub;
  new_event->is_within = (is_within != 0);
  /* insert */
  new_event->next = events->watchpoints;
  events->watchpoints = new_event;
  events->work_pending = 1;
  ETRACE ((_ETRACE,
	   "event watching host at %ld - tag 0x%lx - host-addr 0x%lx, 0x%lx..0x%lx, handler 0x%lx, data 0x%lx\n",
	   (long)sim_events_time (sd),
	   (long)new_event,
	   (long)new_event->host_addr,
	   (long)new_event->lb,
	   (long)new_event->ub,
	   (long)new_event->handler,
	   (long)new_event->data));
  return new_event;
}


EXTERN_SIM_EVENTS\
(void)
sim_events_deschedule (SIM_DESC sd,
		       sim_event *event_to_remove)
{
  sim_events *events = STATE_EVENTS (sd);
  sim_event *to_remove = (sim_event*)event_to_remove;
  SIM_ASSERT ((events->time_from_event >= 0) == (events->queue != NULL));
  if (event_to_remove != NULL)
    {
      sim_event **queue = NULL;
      while ((queue = next_event_queue (sd, queue)) != NULL)
	{
	  sim_event **ptr_to_current;
	  for (ptr_to_current = queue;
	       *ptr_to_current != NULL && *ptr_to_current != to_remove;
	       ptr_to_current = &(*ptr_to_current)->next);
	  if (*ptr_to_current == to_remove)
	    {
	      sim_event *dead = *ptr_to_current;
	      *ptr_to_current = dead->next;
	      ETRACE ((_ETRACE,
		       "event/watch descheduled at %ld - tag 0x%lx - time %ld, handler 0x%lx, data 0x%lx\n",
		       (long) sim_events_time (sd),
		       (long) event_to_remove,
		       (long) dead->time_of_event,
		       (long) dead->handler,
		       (long) dead->data));
	      sim_events_free (sd, dead);
	      update_time_from_event (sd);
	      SIM_ASSERT ((events->time_from_event >= 0) == (events->queue != NULL));
	      return;
	    }
	}
    }
  ETRACE ((_ETRACE,
	   "event/watch descheduled at %ld - tag 0x%lx - not found\n",
	   (long) sim_events_time (sd),
	   (long) event_to_remove));
}


STATIC_INLINE_SIM_EVENTS\
(int)
sim_watch_valid (SIM_DESC sd,
		 sim_event *to_do)
{
  switch (to_do->watching)
    {

#define WATCH_CORE(N,OP,EXT) \
      { \
	unsigned_##N word = 0; \
	int nr_read = sim_core_read_buffer (sd, NULL, to_do->core_map, &word, to_do->core_addr, sizeof (word)); \
	OP (word); \
	return (nr_read == sizeof (unsigned_##N) \
		&& (to_do->is_within \
		    == (word >= to_do->lb##EXT \
			&& word <= to_do->ub##EXT))); \
      }
    case watch_core_targ_1: WATCH_CORE (1, T2H,);
    case watch_core_targ_2: WATCH_CORE (2, T2H,);
    case watch_core_targ_4: WATCH_CORE (4, T2H,);
    case watch_core_targ_8: WATCH_CORE (8, T2H,64);

    case watch_core_be_1: WATCH_CORE (1, BE2H,);
    case watch_core_be_2: WATCH_CORE (2, BE2H,);
    case watch_core_be_4: WATCH_CORE (4, BE2H,);
    case watch_core_be_8: WATCH_CORE (8, BE2H,64);

    case watch_core_le_1: WATCH_CORE (1, LE2H,);
    case watch_core_le_2: WATCH_CORE (2, LE2H,);
    case watch_core_le_4: WATCH_CORE (4, LE2H,);
    case watch_core_le_8: WATCH_CORE (8, LE2H,64);
#undef WATCH_CORE

#define WATCH_SIM(N,OP,EXT) \
      { \
	unsigned_##N word = *(unsigned_##N*)to_do->host_addr; \
        OP (word); \
	return (to_do->is_within \
		== (word >= to_do->lb##EXT \
		    && word <= to_do->ub##EXT)); \
      }

    case watch_sim_host_1: WATCH_SIM (1, word = ,);
    case watch_sim_host_2: WATCH_SIM (2, word = ,);
    case watch_sim_host_4: WATCH_SIM (4, word = ,);
    case watch_sim_host_8: WATCH_SIM (8, word = ,64);

    case watch_sim_be_1: WATCH_SIM (1, BE2H,);
    case watch_sim_be_2: WATCH_SIM (2, BE2H,);
    case watch_sim_be_4: WATCH_SIM (4, BE2H,);
    case watch_sim_be_8: WATCH_SIM (8, BE2H,64);

    case watch_sim_le_1: WATCH_SIM (1, LE2H,);
    case watch_sim_le_2: WATCH_SIM (1, LE2H,);
    case watch_sim_le_4: WATCH_SIM (1, LE2H,);
    case watch_sim_le_8: WATCH_SIM (1, LE2H,64);
#undef WATCH_SIM

    case watch_clock: /* wallclock */
      {
	unsigned long elapsed_time = sim_elapsed_time_since (STATE_EVENTS (sd)->initial_wallclock);
	return (elapsed_time >= to_do->wallclock);
      }

    default:
      sim_io_error (sd, "sim_watch_valid - bad switch");
      break;

    }
  return 1;
}


INLINE_SIM_EVENTS\
(int)
sim_events_tick (SIM_DESC sd)
{
  sim_events *events = STATE_EVENTS (sd);

  /* this should only be called after the previous ticks have been
     fully processed */
  SIM_ASSERT (events->nr_ticks_to_process == 0);

  /* Advance the time but *only* if there is nothing to process */
  if (events->work_pending
      || events->time_from_event == 0)
    {
      events->nr_ticks_to_process = 1;
      return 1;
    }
  else {
    events->time_from_event -= 1;
    return 0;
  }
}


INLINE_SIM_EVENTS\
(int)
sim_events_tickn (SIM_DESC sd,
		  unsigned n)
{
  sim_events *events = STATE_EVENTS (sd);

  /* this should only be called after the previous ticks have been
     fully processed */
  SIM_ASSERT (events->nr_ticks_to_process == 0);
  SIM_ASSERT (n > 0);

  /* Advance the time but *only* if there is nothing to process */
  if (events->work_pending
      || events->time_from_event < n)
    {
      events->nr_ticks_to_process = n;
      return 1;
    }
  else {
    events->time_from_event -= n;
    return 0;
  }
}


INLINE_SIM_EVENTS\
(void)
sim_events_preprocess (SIM_DESC sd,
		       int events_were_last,
		       int events_were_next)
{
  sim_events *events = STATE_EVENTS(sd);
  if (events->nr_ticks_to_process != 0)
    {
      /* Halted midway through event processing */
      ASSERT (events_were_last && events_were_next);
      sim_events_process (sd);
    }
  else if (events_were_next)
    {
      /* Halted by the last processor */
      ASSERT (events->nr_ticks_to_process == 0 && !events_were_last);
      if (sim_events_tick (sd))
	sim_events_process (sd);
    }
}


INLINE_SIM_EVENTS\
(void)
sim_events_process (SIM_DESC sd)
{
  sim_events *events = STATE_EVENTS(sd);
  signed64 event_time = sim_events_time(sd);

  ASSERT (events->nr_ticks_to_process != 0);

  /* move any events that were queued by any signal handlers onto
     the real event queue.  */
  if (events->nr_held > 0)
    {
      int i;
      
#if defined(HAVE_SIGPROCMASK) && defined(SIG_SETMASK)
      /*-LOCK-*/
      sigset_t old_mask;
      sigset_t new_mask;
      sigfillset(&new_mask);
      sigprocmask(SIG_SETMASK, &new_mask, &old_mask);
#endif
      
      for (i = 0; i < events->nr_held; i++)
	{
	  sim_event *entry = &events->held [i];
	  sim_events_schedule (sd,
			       entry->time_of_event,
			       entry->handler,
			       entry->data);
	}
      events->nr_held = 0;
      
#if defined(HAVE_SIGPROCMASK) && defined(SIG_SETMASK)
      /*-UNLOCK-*/
      sigprocmask(SIG_SETMASK, &old_mask, NULL);
#endif
      
    }
  
  /* Process any watchpoints. Be careful to allow a watchpoint to
     appear/disappear under our feet.
     To ensure that watchpoints are processed only once per cycle,
     they are moved onto a watched queue, this returned to the
     watchpoint queue when all queue processing has been
     completed. */
  while (events->watchpoints != NULL)
    {
      sim_event *to_do = events->watchpoints;
      events->watchpoints = to_do->next;
      if (sim_watch_valid (sd, to_do))
	{
	  sim_event_handler *handler = to_do->handler;
	  void *data = to_do->data;
	  events->queue = to_do->next;
	  ETRACE((_ETRACE,
		  "event issued at %ld - tag 0x%lx - handler 0x%lx, data 0x%lx\n",
		  (long) event_time,
		  (long) to_do,
		  (long) handler,
		  (long) data));
	  sim_events_free (sd, to_do);
	  handler (sd, data);
	}
      else
	{
	  to_do->next = events->watchedpoints;
	  events->watchedpoints = to_do;
	}
    }
  
  /* consume all events for this or earlier times.  Be careful to
     allow an event to appear/disappear under our feet */
  while (events->queue->time_of_event <
	 (event_time + events->nr_ticks_to_process))
    {
      sim_event *to_do = events->queue;
      sim_event_handler *handler = to_do->handler;
      void *data = to_do->data;
      events->queue = to_do->next;
      ETRACE((_ETRACE,
	      "event issued at %ld - tag 0x%lx - handler 0x%lx, data 0x%lx\n",
	      (long) event_time,
	      (long) to_do,
	      (long) handler,
	      (long) data));
      sim_events_free (sd, to_do);
      handler (sd, data);
    }
  
  /* put things back where they belong ready for the next iteration */
  events->watchpoints = events->watchedpoints;
  events->watchedpoints = NULL;
  if (events->watchpoints != NULL)
    events->work_pending = 1;
  
  /* re-caculate time for new events then advance the time */
  update_time_from_event(sd);
  SIM_ASSERT (events->time_from_event >= events->nr_ticks_to_process);
  SIM_ASSERT (events->queue != NULL); /* always poll event */
  events->time_from_event -= events->nr_ticks_to_process;

  /* this round of processing complete */
  events->nr_ticks_to_process = 0;
}

#endif