| 1 | /* This file is part of the program psim. |
| 2 | |
| 3 | Copyright (C) 1994-1995,1997-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 | |
| 26 | struct hw_handle_mapping { |
| 27 | cell_word external; |
| 28 | struct hw *phandle; |
| 29 | struct hw_instance *ihandle; |
| 30 | struct hw_handle_mapping *next; |
| 31 | }; |
| 32 | |
| 33 | |
| 34 | struct hw_handle_data { |
| 35 | int nr_mappings; |
| 36 | struct hw_handle_mapping *mappings; |
| 37 | }; |
| 38 | |
| 39 | void |
| 40 | create_hw_handle_data (struct hw *hw) |
| 41 | { |
| 42 | if (hw_parent (hw) == NULL) |
| 43 | { |
| 44 | hw->handles_of_hw = HW_ZALLOC (hw, struct hw_handle_data); |
| 45 | } |
| 46 | else |
| 47 | { |
| 48 | hw->handles_of_hw = hw_root (hw)->handles_of_hw; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | void |
| 53 | delete_hw_handle_data (struct hw *hw) |
| 54 | { |
| 55 | /* NULL */ |
| 56 | } |
| 57 | |
| 58 | |
| 59 | |
| 60 | #if 0 |
| 61 | void |
| 62 | hw_handle_init (struct hw *hw) |
| 63 | { |
| 64 | struct hw_handle_mapping *current_map = db->mappings; |
| 65 | if (current_map != NULL) |
| 66 | { |
| 67 | db->nr_mappings = db->mappings->external; |
| 68 | /* verify that the mappings that were not removed are in |
| 69 | sequence down to nr 1 */ |
| 70 | while (current_map->next != NULL) |
| 71 | { |
| 72 | if (current_map->external != current_map->next->external + 1) |
| 73 | error ("hw_handle: hw_handle database possibly corrupt"); |
| 74 | current_map = current_map->next; |
| 75 | } |
| 76 | ASSERT (current_map->next == NULL); |
| 77 | if (current_map->external != 1) |
| 78 | error ("hw_handle: hw_handle database possibly corrupt"); |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | db->nr_mappings = 0; |
| 83 | } |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | |
| 88 | struct hw_instance * |
| 89 | hw_handle_ihandle2 (struct hw *hw, |
| 90 | cell_word external) |
| 91 | { |
| 92 | struct hw_handle_data *db = hw->handles_of_hw; |
| 93 | struct hw_handle_mapping *current_map = db->mappings; |
| 94 | while (current_map != NULL) |
| 95 | { |
| 96 | if (current_map->external == external) |
| 97 | return current_map->ihandle; |
| 98 | current_map = current_map->next; |
| 99 | } |
| 100 | return (void*)0; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | struct hw * |
| 105 | hw_handle_phandle2 (struct hw *hw, |
| 106 | cell_word external) |
| 107 | { |
| 108 | struct hw_handle_data *db = hw->handles_of_hw; |
| 109 | struct hw_handle_mapping *current_map = db->mappings; |
| 110 | while (current_map != NULL) |
| 111 | { |
| 112 | if (current_map->external == external) |
| 113 | return current_map->phandle; |
| 114 | current_map = current_map->next; |
| 115 | } |
| 116 | return (void*)0; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | cell_word |
| 121 | hw_handle_2ihandle (struct hw *hw, |
| 122 | struct hw_instance *internal) |
| 123 | { |
| 124 | struct hw_handle_data *db = hw->handles_of_hw; |
| 125 | struct hw_handle_mapping *current_map = db->mappings; |
| 126 | while (current_map != NULL) |
| 127 | { |
| 128 | if (current_map->ihandle == internal) |
| 129 | return current_map->external; |
| 130 | current_map = current_map->next; |
| 131 | } |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | cell_word |
| 137 | hw_handle_2phandle (struct hw *hw, |
| 138 | struct hw *internal) |
| 139 | { |
| 140 | struct hw_handle_data *db = hw->handles_of_hw; |
| 141 | struct hw_handle_mapping *current_map = db->mappings; |
| 142 | while (current_map != NULL) |
| 143 | { |
| 144 | if (current_map->phandle == internal) |
| 145 | return current_map->external; |
| 146 | current_map = current_map->next; |
| 147 | } |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | |
| 152 | void |
| 153 | hw_handle_add_ihandle (struct hw *hw, |
| 154 | struct hw_instance *internal) |
| 155 | { |
| 156 | struct hw_handle_data *db = hw->handles_of_hw; |
| 157 | if (hw_handle_2ihandle (hw, internal) != 0) |
| 158 | { |
| 159 | hw_abort (hw, "attempting to add an ihandle already in the data base"); |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | /* insert at the front making things in decending order */ |
| 164 | struct hw_handle_mapping *new_map = ZALLOC (struct hw_handle_mapping); |
| 165 | new_map->next = db->mappings; |
| 166 | new_map->ihandle = internal; |
| 167 | db->nr_mappings += 1; |
| 168 | new_map->external = db->nr_mappings; |
| 169 | db->mappings = new_map; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | |
| 174 | void |
| 175 | hw_handle_add_phandle (struct hw *hw, |
| 176 | struct hw *internal) |
| 177 | { |
| 178 | struct hw_handle_data *db = hw->handles_of_hw; |
| 179 | if (hw_handle_2phandle (hw, internal) != 0) |
| 180 | { |
| 181 | hw_abort (hw, "attempting to add a phandle already in the data base"); |
| 182 | } |
| 183 | else |
| 184 | { |
| 185 | /* insert at the front making things in decending order */ |
| 186 | struct hw_handle_mapping *new_map = ZALLOC (struct hw_handle_mapping); |
| 187 | new_map->next = db->mappings; |
| 188 | new_map->phandle = internal; |
| 189 | db->nr_mappings += 1; |
| 190 | new_map->external = db->nr_mappings; |
| 191 | db->mappings = new_map; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | |
| 196 | void |
| 197 | hw_handle_remove_ihandle (struct hw *hw, |
| 198 | struct hw_instance *internal) |
| 199 | { |
| 200 | struct hw_handle_data *db = hw->handles_of_hw; |
| 201 | struct hw_handle_mapping **current_map = &db->mappings; |
| 202 | while (*current_map != NULL) |
| 203 | { |
| 204 | if ((*current_map)->ihandle == internal) |
| 205 | { |
| 206 | struct hw_handle_mapping *delete = *current_map; |
| 207 | *current_map = delete->next; |
| 208 | zfree (delete); |
| 209 | return; |
| 210 | } |
| 211 | current_map = &(*current_map)->next; |
| 212 | } |
| 213 | hw_abort (hw, "attempt to remove nonexistant ihandle"); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | void |
| 218 | hw_handle_remove_phandle (struct hw *hw, |
| 219 | struct hw *internal) |
| 220 | { |
| 221 | struct hw_handle_data *db = hw->handles_of_hw; |
| 222 | struct hw_handle_mapping **current_map = &db->mappings; |
| 223 | while (*current_map != NULL) |
| 224 | { |
| 225 | if ((*current_map)->phandle == internal) |
| 226 | { |
| 227 | struct hw_handle_mapping *delete = *current_map; |
| 228 | *current_map = delete->next; |
| 229 | zfree (delete); |
| 230 | return; |
| 231 | } |
| 232 | current_map = &(*current_map)->next; |
| 233 | } |
| 234 | hw_abort (hw, "attempt to remove nonexistant phandle"); |
| 235 | } |
| 236 | |
| 237 | |