Initial creation of sourceware repository
[external/binutils.git] / sim / common / hw-instances.c
1 /*  This file is part of the program psim.
2
3     Copyright (C) 1994-1998, Andrew Cagney <cagney@highland.com.au>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14  
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  
19     */
20
21
22 #include "hw-main.h"
23 #include "hw-base.h"
24
25 #include "sim-assert.h"
26
27 struct hw_instance_data {
28   hw_finish_instance_method *to_finish;
29   struct hw_instance *instances;
30 };
31
32 static hw_finish_instance_method abort_hw_finish_instance;
33
34 void
35 create_hw_instance_data (struct hw *me)
36 {
37   me->instances_of_hw = HW_ZALLOC (me, struct hw_instance_data);
38   set_hw_finish_instance (me, abort_hw_finish_instance);
39 }
40
41 void
42 delete_hw_instance_data (struct hw *me)
43 {
44   /* NOP */
45 }
46
47
48 static void
49 abort_hw_finish_instance (struct hw *hw,
50                           struct hw_instance *instance)
51 {
52   hw_abort (hw, "no instance finish method");
53 }
54
55 void
56 set_hw_finish_instance (struct hw *me,
57                         hw_finish_instance_method *finish)
58 {
59   me->instances_of_hw->to_finish = finish;
60 }
61
62
63 #if 0
64 void
65 clean_hw_instances (struct hw *me)
66 {
67   struct hw_instance **instance = &me->instances;
68   while (*instance != NULL)
69     {
70       struct hw_instance *old_instance = *instance;
71       hw_instance_delete (old_instance);
72       instance = &me->instances;
73     }
74 }
75 #endif
76
77
78 void
79 hw_instance_delete (struct hw_instance *instance)
80 {
81 #if 1
82   hw_abort (hw_instance_hw (instance), "not implemented");
83 #else
84   struct hw *me = hw_instance_hw (instance);
85   if (instance->to_instance_delete == NULL)
86     hw_abort (me, "no delete method");
87   instance->method->delete(instance);
88   if (instance->args != NULL)
89     zfree (instance->args);
90   if (instance->path != NULL)
91     zfree (instance->path);
92   if (instance->child == NULL)
93     {
94       /* only remove leaf nodes */
95       struct hw_instance **curr = &me->instances;
96       while (*curr != instance)
97         {
98           ASSERT (*curr != NULL);
99           curr = &(*curr)->next;
100         }
101       *curr = instance->next;
102     }
103   else
104     {
105       /* check it isn't in the instance list */
106       struct hw_instance *curr = me->instances;
107       while (curr != NULL)
108         {
109           ASSERT(curr != instance);
110           curr = curr->next;
111         }
112       /* unlink the child */
113       ASSERT (instance->child->parent == instance);
114       instance->child->parent = NULL;
115     }
116   cap_remove (me->ihandles, instance);
117   zfree (instance);
118 #endif
119 }
120
121
122 static int
123 panic_hw_instance_read (struct hw_instance *instance,
124                         void *addr,
125                         unsigned_word len)
126 {
127   hw_abort (hw_instance_hw (instance), "no read method");
128   return -1;
129 }
130
131
132
133 static int
134 panic_hw_instance_write (struct hw_instance *instance,
135                          const void *addr,
136                          unsigned_word len)
137 {
138   hw_abort (hw_instance_hw (instance), "no write method");
139   return -1;
140 }
141
142
143 static int
144 panic_hw_instance_seek (struct hw_instance *instance,
145                         unsigned_word pos_hi,
146                         unsigned_word pos_lo)
147 {
148   hw_abort (hw_instance_hw (instance), "no seek method");
149   return -1;
150 }
151
152
153 int
154 hw_instance_call_method (struct hw_instance *instance,
155                          const char *method_name,
156                          int n_stack_args,
157                          unsigned_cell stack_args[/*n_stack_args*/],    
158                          int n_stack_returns,
159                          unsigned_cell stack_returns[/*n_stack_args*/])
160 {
161 #if 1
162   hw_abort (hw_instance_hw (instance), "not implemented");
163   return -1;
164 #else
165   struct hw *me = instance->owner;
166   const hw_instance_methods *method = instance->method->methods;
167   if (method == NULL)
168     {
169       hw_abort (me, "no methods (want %s)", method_name);
170     }
171   while (method->name != NULL)
172     {
173       if (strcmp(method->name, method_name) == 0)
174         {
175           return method->method (instance,
176                                  n_stack_args, stack_args,
177                                  n_stack_returns, stack_returns);
178         }
179       method++;
180     }
181   hw_abort (me, "no %s method", method_name);
182   return 0;
183 #endif
184 }
185
186
187 #define set_hw_instance_read(instance, method)\
188 ((instance)->to_instance_read = (method))
189
190 #define set_hw_instance_write(instance, method)\
191 ((instance)->to_instance_write = (method))
192
193 #define set_hw_instance_seek(instance, method)\
194 ((instance)->to_instance_seek = (method))
195
196
197 #if 0
198 static void
199 set_hw_instance_finish (struct hw *me,
200                         hw_instance_finish_method *method)
201 {
202   if (me->instances_of_hw == NULL)
203     me->instances_of_hw = HW_ZALLOC (me, struct hw_instance_data);
204   me->instances_of_hw->to_finish = method;
205 }
206 #endif
207
208
209 struct hw_instance *
210 hw_instance_create (struct hw *me,
211                     struct hw_instance *parent,
212                     const char *path,
213                     const char *args)
214 {
215   struct hw_instance *instance = ZALLOC (struct hw_instance);
216   /*instance->unit*/
217   /* link this instance into the devices list */
218   instance->hw_of_instance = me;
219   instance->parent_of_instance = NULL;
220   /* link this instance into the front of the devices instance list */
221   instance->sibling_of_instance = me->instances_of_hw->instances;
222   me->instances_of_hw->instances = instance;
223   if (parent != NULL)
224     {
225       ASSERT (parent->child_of_instance == NULL);
226       parent->child_of_instance = instance;
227       instance->parent_of_instance = parent;
228     }
229   instance->args_of_instance = hw_strdup (me, args);
230   instance->path_of_instance = hw_strdup (me, path);
231   set_hw_instance_read (instance, panic_hw_instance_read);
232   set_hw_instance_write (instance, panic_hw_instance_write);
233   set_hw_instance_seek (instance, panic_hw_instance_seek);
234   hw_handle_add_ihandle (me, instance);
235   me->instances_of_hw->to_finish (me, instance);
236   return instance;
237 }
238
239
240 struct hw_instance *
241 hw_instance_interceed (struct hw_instance *parent,
242                        const char *path,
243                        const char *args)
244 {
245 #if 1
246   return NULL;
247 #else
248   struct hw_instance *instance = ZALLOC (struct hw_instance);
249   /*instance->unit*/
250   /* link this instance into the devices list */
251   if (me != NULL)
252     {
253       ASSERT (parent == NULL);
254       instance->hw_of_instance = me;
255       instance->parent_of_instance = NULL;
256       /* link this instance into the front of the devices instance list */
257       instance->sibling_of_instance = me->instances_of_hw->instances;
258       me->instances_of_hw->instances = instance;
259     }
260   if (parent != NULL)
261     {
262       struct hw_instance **previous;
263       ASSERT (parent->child_of_instance == NULL);
264       parent->child_of_instance = instance;
265       instance->owner = parent->owner;
266       instance->parent_of_instance = parent;
267       /* in the devices instance list replace the parent instance with
268          this one */
269       instance->next = parent->next;
270       /* replace parent with this new node */
271       previous = &instance->owner->instances;
272       while (*previous != parent)
273         {
274           ASSERT (*previous != NULL);
275           previous = &(*previous)->next;
276         }
277       *previous = instance;
278     }
279   instance->data = data;
280   instance->args = (args == NULL ? NULL : (char *) strdup(args));
281   instance->path = (path == NULL ? NULL : (char *) strdup(path));
282   cap_add (instance->owner->ihandles, instance);
283   return instance;
284 #endif
285 }