2 Copyright (C) 1998, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Andrew Cagney and Cygnus Solutions.
6 This file is part of GDB, the GNU debugger.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
45 struct hw_port_edge
*next
;
46 object_disposition disposition
;
51 hw_port_event_method
*to_port_event
;
52 const struct hw_port_descriptor
*ports
;
53 struct hw_port_edge
*edges
;
56 const struct hw_port_descriptor empty_hw_ports
[] =
62 panic_hw_port_event (struct hw
*me
,
68 hw_abort (me
, "no port method");
72 create_hw_port_data (struct hw
*me
)
74 me
->ports_of_hw
= HW_ZALLOC (me
, struct hw_port_data
);
75 set_hw_port_event (me
, panic_hw_port_event
);
76 set_hw_ports (me
, empty_hw_ports
);
80 delete_hw_port_data (struct hw
*me
)
82 hw_free (me
, me
->ports_of_hw
);
83 me
->ports_of_hw
= NULL
;
87 set_hw_ports (struct hw
*me
,
88 const struct hw_port_descriptor ports
[])
90 me
->ports_of_hw
->ports
= ports
;
94 set_hw_port_event (struct hw
*me
,
95 hw_port_event_method
*port_event
)
97 me
->ports_of_hw
->to_port_event
= port_event
;
102 attach_hw_port_edge (struct hw
*me
,
103 struct hw_port_edge
**list
,
107 object_disposition disposition
)
109 struct hw_port_edge
*new_edge
= HW_ZALLOC (me
, struct hw_port_edge
);
110 new_edge
->my_port
= my_port
;
111 new_edge
->dest
= dest
;
112 new_edge
->dest_port
= dest_port
;
113 new_edge
->next
= *list
;
114 new_edge
->disposition
= disposition
;
120 detach_hw_port_edge (struct hw
*me
,
121 struct hw_port_edge
**list
,
126 while (*list
!= NULL
)
128 struct hw_port_edge
*old_edge
= *list
;
129 if (old_edge
->dest
== dest
130 && old_edge
->dest_port
== dest_port
131 && old_edge
->my_port
== my_port
)
133 if (old_edge
->disposition
== permenant_object
)
134 hw_abort (me
, "attempt to delete permenant port edge");
135 *list
= old_edge
->next
;
136 hw_free (me
, old_edge
);
140 hw_abort (me
, "attempt to delete unattached port");
146 clean_hw_port_edges (struct hw_port_edge
**list
)
148 while (*list
!= NULL
)
150 struct hw_port_edge
*old_edge
= *list
;
151 switch (old_edge
->disposition
)
153 case permenant_object
:
154 list
= &old_edge
->next
;
156 case temporary_object
:
157 *list
= old_edge
->next
;
158 hw_free (me
, old_edge
);
169 hw_port_event (struct hw
*me
,
173 int found_an_edge
= 0;
174 struct hw_port_edge
*edge
;
175 /* device's lines directly connected */
176 for (edge
= me
->ports_of_hw
->edges
;
180 if (edge
->my_port
== my_port
)
182 edge
->dest
->ports_of_hw
->to_port_event (edge
->dest
,
191 hw_abort (me
, "No edge for port %d", my_port
);
196 hw_port_attach (struct hw
*me
,
200 object_disposition disposition
)
202 attach_hw_port_edge (me
,
203 &me
->ports_of_hw
->edges
,
212 hw_port_detach (struct hw
*me
,
217 detach_hw_port_edge (me
,
218 &me
->ports_of_hw
->edges
,
226 hw_port_traverse (struct hw
*me
,
227 hw_port_traverse_function
*handler
,
230 struct hw_port_edge
*port_edge
;
231 for (port_edge
= me
->ports_of_hw
->edges
;
233 port_edge
= port_edge
->next
)
235 handler (me
, port_edge
->my_port
,
236 port_edge
->dest
, port_edge
->dest_port
,
243 hw_port_decode (struct hw
*me
,
244 const char *port_name
,
245 port_direction direction
)
247 if (port_name
== NULL
|| port_name
[0] == '\0')
249 if (isdigit (port_name
[0]))
251 return strtoul (port_name
, NULL
, 0);
255 const struct hw_port_descriptor
*ports
=
256 me
->ports_of_hw
->ports
;
259 while (ports
->name
!= NULL
)
261 if (ports
->direction
== bidirect_port
262 || ports
->direction
== direction
)
264 if (ports
->nr_ports
> 0)
266 int len
= strlen (ports
->name
);
267 if (strncmp (port_name
, ports
->name
, len
) == 0)
269 if (port_name
[len
] == '\0')
270 return ports
->number
;
271 else if (isdigit (port_name
[len
]))
273 int port
= (ports
->number
274 + strtoul (&port_name
[len
], NULL
, 0));
275 if (port
>= ports
->number
+ ports
->nr_ports
)
277 "Port %s out of range",
283 else if (strcmp (port_name
, ports
->name
) == 0)
284 return ports
->number
;
290 hw_abort (me
, "Unreconized port %s", port_name
);
296 hw_port_encode (struct hw
*me
,
300 port_direction direction
)
302 const struct hw_port_descriptor
*ports
= NULL
;
303 ports
= me
->ports_of_hw
->ports
;
305 while (ports
->name
!= NULL
)
307 if (ports
->direction
== bidirect_port
308 || ports
->direction
== direction
)
310 if (ports
->nr_ports
> 0)
312 if (port_number
>= ports
->number
313 && port_number
< ports
->number
+ ports
->nr_ports
)
315 strcpy (buf
, ports
->name
);
316 sprintf (buf
+ strlen (buf
), "%d", port_number
- ports
->number
);
317 if (strlen (buf
) >= sizeof_buf
)
318 hw_abort (me
, "hw_port_encode: buffer overflow");
324 if (ports
->number
== port_number
)
326 if (strlen (ports
->name
) >= sizeof_buf
)
327 hw_abort (me
, "hw_port_encode: buffer overflow");
328 strcpy (buf
, ports
->name
);
336 sprintf (buf
, "%d", port_number
);
337 if (strlen (buf
) >= sizeof_buf
)
338 hw_abort (me
, "hw_port_encode: buffer overflow");