OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [branches/] [oc/] [gdb-5.0/] [sim/] [common/] [hw-events.c] - Blame information for rev 1771

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 106 markom
/* Hardware event manager.
2
   Copyright (C) 1998 Free Software Foundation, Inc.
3
   Contributed by Cygnus Support.
4
 
5
This file is part of GDB, the GNU debugger.
6
 
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2, or (at your option)
10
any later version.
11
 
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
 
17
You should have received a copy of the GNU General Public License along
18
with this program; if not, write to the Free Software Foundation, Inc.,
19
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
 
21
 
22
#include "hw-main.h"
23
#include "hw-base.h"
24
 
25
#include "sim-events.h"
26
 
27
 
28
/* The hw-events object is implemented using sim-events */
29
 
30
struct hw_event {
31
  void *data;
32
  struct hw *me;
33
  hw_event_callback *callback;
34
  sim_event *real;
35
  struct hw_event_data *entry;
36
};
37
 
38
struct hw_event_data {
39
  struct hw_event event;
40
  struct hw_event_data *next;
41
};
42
 
43
void
44
create_hw_event_data (struct hw *me)
45
{
46
  if (me->events_of_hw != NULL)
47
    hw_abort (me, "stray events");
48
  /* NOP */
49
}
50
 
51
void
52
delete_hw_event_data (struct hw *me)
53
{
54
  if (me->events_of_hw != NULL)
55
    hw_abort (me, "stray events");
56
}
57
 
58
 
59
/* Pass the H/W event onto the real callback */
60
 
61
static void
62
bounce_hw_event (SIM_DESC sd,
63
                 void *data)
64
{
65
  /* save the data */
66
  struct hw_event_data *entry = (struct hw_event_data *) data;
67
  struct hw *me = entry->event.me;
68
  void *event_data = entry->event.data;
69
  hw_event_callback *callback = entry->event.callback;
70
  struct hw_event_data **prev = &me->events_of_hw;
71
  while ((*prev) != entry)
72
    prev = &(*prev)->next;
73
  (*prev) = entry->next;
74
  hw_free (me, entry);
75
  callback (me, event_data); /* may not return */
76
}
77
 
78
 
79
 
80
/* Map onto the event functions */
81
 
82
struct hw_event *
83
hw_event_queue_schedule (struct hw *me,
84
                         signed64 delta_time,
85
                         hw_event_callback *callback,
86
                         void *data)
87
{
88
  struct hw_event *event;
89
  va_list dummy;
90
  event = hw_event_queue_schedule_vtracef (me, delta_time, callback, data,
91
                                           NULL, dummy);
92
  return event;
93
}
94
 
95
struct hw_event *
96
hw_event_queue_schedule_tracef (struct hw *me,
97
                                signed64 delta_time,
98
                                hw_event_callback *callback,
99
                                void *data,
100
                                const char *fmt,
101
                                ...)
102
{
103
  struct hw_event *event;
104
  va_list ap;
105
  va_start (ap, fmt);
106
  event = hw_event_queue_schedule_vtracef (me, delta_time, callback, data, fmt, ap);
107
  va_end (ap);
108
  return event;
109
}
110
 
111
struct hw_event *
112
hw_event_queue_schedule_vtracef (struct hw *me,
113
                                 signed64 delta_time,
114
                                 hw_event_callback *callback,
115
                                 void *data,
116
                                 const char *fmt,
117
                                 va_list ap)
118
{
119
  struct hw_event_data *entry = HW_ZALLOC (me, struct hw_event_data);
120
  entry->next = me->events_of_hw;
121
  me->events_of_hw = entry;
122
  /* fill it in */
123
  entry->event.entry = entry;
124
  entry->event.data = data;
125
  entry->event.callback = callback;
126
  entry->event.me = me;
127
  entry->event.real = sim_events_schedule_vtracef (hw_system (me),
128
                                                   delta_time,
129
                                                   bounce_hw_event,
130
                                                   entry,
131
                                                   fmt, ap);
132
  return &entry->event;
133
}
134
 
135
 
136
void
137
hw_event_queue_deschedule (struct hw *me,
138
                           struct hw_event *event_to_remove)
139
{
140
/* ZAP the event but only if it is still in the event queue.  Note
141
   that event_to_remove is only de-referenced after its validity has
142
   been confirmed.  */
143
  struct hw_event_data **prev;
144
  for (prev = &me->events_of_hw;
145
       (*prev) != NULL;
146
       prev = &(*prev)->next)
147
    {
148
      struct hw_event_data *entry = (*prev);
149
      if (&entry->event == event_to_remove)
150
        {
151
          sim_events_deschedule (hw_system (me),
152
                                 entry->event.real);
153
          (*prev) = entry->next;
154
          hw_free (me, entry);
155
          return;
156
        }
157
    }
158
}
159
 
160
 
161
signed64
162
hw_event_queue_time (struct hw *me)
163
{
164
  return sim_events_time (hw_system (me));
165
}
166
 
167
 
168
/* Only worry about this compling on ANSI systems.
169
   Build with `make test-hw-events' in sim/<cpu> directory*/
170
 
171
#if defined (MAIN)
172
#include "sim-main.h"
173
#include <string.h>
174
#include <stdio.h>
175
 
176
static void
177
test_handler (struct hw *me,
178
              void *data)
179
{
180
  int *n = data;
181
  if (*n != hw_event_queue_time (me))
182
    abort ();
183
  *n = -(*n);
184
}
185
 
186
int
187
main (int argc,
188
      char **argv)
189
{
190
  host_callback *cb = ZALLOC (host_callback);
191
  struct sim_state *sd = sim_state_alloc (0, cb);
192
  struct hw *me = ZALLOC (struct hw);
193
  sim_pre_argv_init (sd, "test-hw-events");
194
  sim_post_argv_init (sd);
195
  me->system_of_hw = sd;
196
 
197
  printf ("Create hw-event-data\n");
198
  {
199
    create_hw_alloc_data (me);
200
    create_hw_event_data (me);
201
    delete_hw_event_data (me);
202
    delete_hw_alloc_data (me);
203
  }
204
 
205
  printf ("Create hw-events\n");
206
  {
207
    struct hw_event *a;
208
    struct hw_event *b;
209
    struct hw_event *c;
210
    struct hw_event *d;
211
    create_hw_alloc_data (me);
212
    create_hw_event_data (me);
213
    a = hw_event_queue_schedule (me, 0, NULL, NULL);
214
    b = hw_event_queue_schedule (me, 1, NULL, NULL);
215
    c = hw_event_queue_schedule (me, 2, NULL, NULL);
216
    d = hw_event_queue_schedule (me, 1, NULL, NULL);
217
    hw_event_queue_deschedule (me, c);
218
    hw_event_queue_deschedule (me, b);
219
    hw_event_queue_deschedule (me, a);
220
    hw_event_queue_deschedule (me, d);
221
    c = HW_ZALLOC (me, struct hw_event);
222
    hw_event_queue_deschedule (me, b); /* OOPS! */
223
    hw_free (me, c);
224
    delete_hw_event_data (me);
225
    delete_hw_alloc_data (me);
226
  }
227
 
228
  printf ("Schedule hw-events\n");
229
  {
230
    struct hw_event **e;
231
    int *n;
232
    int i;
233
    int nr = 4;
234
    e = HW_NZALLOC (me, struct hw_event *, nr);
235
    n = HW_NZALLOC (me, int, nr);
236
    create_hw_alloc_data (me);
237
    create_hw_event_data (me);
238
    for (i = 0; i < nr; i++)
239
      {
240
        n[i] = i;
241
        e[i] = hw_event_queue_schedule (me, i, test_handler, &n[i]);
242
      }
243
    sim_events_preprocess (sd, 1, 1);
244
    for (i = 0; i < nr; i++)
245
      {
246
        if (sim_events_tick (sd))
247
          sim_events_process (sd);
248
      }
249
    for (i = 0; i < nr; i++)
250
      {
251
        if (n[i] != -i)
252
          abort ();
253
        hw_event_queue_deschedule (me, e[i]);
254
      }
255
    hw_free (me, n);
256
    hw_free (me, e);
257
    delete_hw_event_data (me);
258
    delete_hw_alloc_data (me);
259
  }
260
 
261
  return 0;
262
}
263
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.