Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / sim / common / hw-ports.c
1 /* Hardware ports.
2 Copyright (C) 1998-2022 Free Software Foundation, Inc.
3 Contributed by Andrew Cagney and Cygnus Solutions.
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 3 of the License, or
10 (at your option) 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
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* This must come before any other includes. */
21 #include "defs.h"
22
23 #include "hw-main.h"
24 #include "hw-base.h"
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
29
30
31 struct hw_port_edge
32 {
33 int my_port;
34 struct hw *dest;
35 int dest_port;
36 struct hw_port_edge *next;
37 object_disposition disposition;
38 };
39
40 struct hw_port_data
41 {
42 hw_port_event_method *to_port_event;
43 const struct hw_port_descriptor *ports;
44 struct hw_port_edge *edges;
45 };
46
47 const struct hw_port_descriptor empty_hw_ports[] =
48 {
49 { NULL, 0, 0, 0 },
50 };
51
52 static void
53 panic_hw_port_event (struct hw *me,
54 int my_port,
55 struct hw *source,
56 int source_port,
57 int level)
58 {
59 hw_abort (me, "no port method");
60 }
61
62 void
63 create_hw_port_data (struct hw *me)
64 {
65 me->ports_of_hw = HW_ZALLOC (me, struct hw_port_data);
66 set_hw_port_event (me, panic_hw_port_event);
67 set_hw_ports (me, empty_hw_ports);
68 }
69
70 void
71 delete_hw_port_data (struct hw *me)
72 {
73 hw_free (me, me->ports_of_hw);
74 me->ports_of_hw = NULL;
75 }
76
77 void
78 set_hw_ports (struct hw *me,
79 const struct hw_port_descriptor ports[])
80 {
81 me->ports_of_hw->ports = ports;
82 }
83
84 void
85 set_hw_port_event (struct hw *me,
86 hw_port_event_method *port_event)
87 {
88 me->ports_of_hw->to_port_event = port_event;
89 }
90
91
92 static void
93 attach_hw_port_edge (struct hw *me,
94 struct hw_port_edge **list,
95 int my_port,
96 struct hw *dest,
97 int dest_port,
98 object_disposition disposition)
99 {
100 struct hw_port_edge *new_edge = HW_ZALLOC (me, struct hw_port_edge);
101 new_edge->my_port = my_port;
102 new_edge->dest = dest;
103 new_edge->dest_port = dest_port;
104 new_edge->next = *list;
105 new_edge->disposition = disposition;
106 *list = new_edge;
107 }
108
109
110 static void
111 detach_hw_port_edge (struct hw *me,
112 struct hw_port_edge **list,
113 int my_port,
114 struct hw *dest,
115 int dest_port)
116 {
117 while (*list != NULL)
118 {
119 struct hw_port_edge *old_edge = *list;
120 if (old_edge->dest == dest
121 && old_edge->dest_port == dest_port
122 && old_edge->my_port == my_port)
123 {
124 if (old_edge->disposition == permenant_object)
125 hw_abort (me, "attempt to delete permenant port edge");
126 *list = old_edge->next;
127 hw_free (me, old_edge);
128 return;
129 }
130 }
131 hw_abort (me, "attempt to delete unattached port");
132 }
133
134
135 #if 0
136 static void
137 clean_hw_port_edges (struct hw_port_edge **list)
138 {
139 while (*list != NULL)
140 {
141 struct hw_port_edge *old_edge = *list;
142 switch (old_edge->disposition)
143 {
144 case permenant_object:
145 list = &old_edge->next;
146 break;
147 case temporary_object:
148 *list = old_edge->next;
149 hw_free (me, old_edge);
150 break;
151 }
152 }
153 }
154 #endif
155
156
157 /* Ports: */
158
159 void
160 hw_port_event (struct hw *me,
161 int my_port,
162 int level)
163 {
164 int found_an_edge = 0;
165 struct hw_port_edge *edge;
166 /* device's lines directly connected */
167 for (edge = me->ports_of_hw->edges;
168 edge != NULL;
169 edge = edge->next)
170 {
171 if (edge->my_port == my_port)
172 {
173 edge->dest->ports_of_hw->to_port_event (edge->dest,
174 edge->dest_port,
175 me,
176 my_port,
177 level);
178 found_an_edge = 1;
179 }
180 }
181 if (!found_an_edge)
182 hw_abort (me, "No edge for port %d", my_port);
183 }
184
185
186 void
187 hw_port_attach (struct hw *me,
188 int my_port,
189 struct hw *dest,
190 int dest_port,
191 object_disposition disposition)
192 {
193 attach_hw_port_edge (me,
194 &me->ports_of_hw->edges,
195 my_port,
196 dest,
197 dest_port,
198 disposition);
199 }
200
201
202 void
203 hw_port_detach (struct hw *me,
204 int my_port,
205 struct hw *dest,
206 int dest_port)
207 {
208 detach_hw_port_edge (me,
209 &me->ports_of_hw->edges,
210 my_port,
211 dest,
212 dest_port);
213 }
214
215
216 void
217 hw_port_traverse (struct hw *me,
218 hw_port_traverse_function *handler,
219 void *data)
220 {
221 struct hw_port_edge *port_edge;
222 for (port_edge = me->ports_of_hw->edges;
223 port_edge != NULL;
224 port_edge = port_edge->next)
225 {
226 handler (me, port_edge->my_port,
227 port_edge->dest, port_edge->dest_port,
228 data);
229 }
230 }
231
232
233 int
234 hw_port_decode (struct hw *me,
235 const char *port_name,
236 port_direction direction)
237 {
238 if (port_name == NULL || port_name[0] == '\0')
239 return 0;
240 if (isdigit (port_name[0]))
241 {
242 return strtoul (port_name, NULL, 0);
243 }
244 else
245 {
246 const struct hw_port_descriptor *ports =
247 me->ports_of_hw->ports;
248 if (ports != NULL)
249 {
250 while (ports->name != NULL)
251 {
252 if (ports->direction == bidirect_port
253 || ports->direction == direction)
254 {
255 if (ports->nr_ports > 0)
256 {
257 int len = strlen (ports->name);
258 if (strncmp (port_name, ports->name, len) == 0)
259 {
260 if (port_name[len] == '\0')
261 return ports->number;
262 else if (isdigit (port_name[len]))
263 {
264 int port = (ports->number
265 + strtoul (&port_name[len], NULL, 0));
266 if (port >= ports->number + ports->nr_ports)
267 hw_abort (me,
268 "Port %s out of range",
269 port_name);
270 return port;
271 }
272 }
273 }
274 else if (strcmp (port_name, ports->name) == 0)
275 return ports->number;
276 }
277 ports++;
278 }
279 }
280 }
281 hw_abort (me, "Unrecognized port %s", port_name);
282 return 0;
283 }
284
285
286 int
287 hw_port_encode (struct hw *me,
288 int port_number,
289 char *buf,
290 int sizeof_buf,
291 port_direction direction)
292 {
293 const struct hw_port_descriptor *ports = NULL;
294 ports = me->ports_of_hw->ports;
295 if (ports != NULL) {
296 while (ports->name != NULL)
297 {
298 if (ports->direction == bidirect_port
299 || ports->direction == direction)
300 {
301 if (ports->nr_ports > 0)
302 {
303 if (port_number >= ports->number
304 && port_number < ports->number + ports->nr_ports)
305 {
306 strcpy (buf, ports->name);
307 sprintf (buf + strlen (buf), "%d", port_number - ports->number);
308 if (strlen (buf) >= sizeof_buf)
309 hw_abort (me, "hw_port_encode: buffer overflow");
310 return strlen (buf);
311 }
312 }
313 else
314 {
315 if (ports->number == port_number)
316 {
317 if (strlen (ports->name) >= sizeof_buf)
318 hw_abort (me, "hw_port_encode: buffer overflow");
319 strcpy (buf, ports->name);
320 return strlen (buf);
321 }
322 }
323 }
324 ports++;
325 }
326 }
327 sprintf (buf, "%d", port_number);
328 if (strlen (buf) >= sizeof_buf)
329 hw_abort (me, "hw_port_encode: buffer overflow");
330 return strlen (buf);
331 }
This page took 0.044856 seconds and 4 git commands to generate.