Start of implementation of a distributed (between processors)
[external/binutils.git] / sim / common / sim-n-core.h
1 /*  This file is part of the program psim.
2
3     Copyright (C) 1994-1997, 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 #ifndef N
23 #error "N must be #defined"
24 #endif
25
26 #include "sim-xcat.h"
27
28 /* NOTE: see end of file for #undef of these macros */
29 #define unsigned_N XCONCAT2(unsigned_,N)
30 #define T2H_N XCONCAT2(T2H_,N)
31 #define H2T_N XCONCAT2(H2T_,N)
32
33 #define sim_core_read_aligned_N XCONCAT2(sim_core_read_aligned_,N)
34 #define sim_core_write_aligned_N XCONCAT2(sim_core_write_aligned_,N)
35 #define sim_core_read_unaligned_N XCONCAT2(sim_core_read_unaligned_,N)
36 #define sim_core_write_unaligned_N XCONCAT2(sim_core_write_unaligned_,N)
37
38
39 INLINE_SIM_CORE(unsigned_N)
40 sim_core_read_aligned_N(sim_cpu *cpu,
41                         sim_cia cia,
42                         sim_core_maps map,
43                         unsigned_word addr)
44 {
45   unsigned_N val;
46   sim_core_mapping *mapping = sim_core_find_mapping (CPU_CORE (cpu), map,
47                                                      addr,
48                                                      sizeof (unsigned_N),
49                                                      1,
50                                                      cpu, cia); /*abort*/
51 #if (WITH_DEVICES)
52   if (WITH_CALLBACK_MEMORY && mapping->device != NULL) {
53     unsigned_N data;
54     if (device_io_read_buffer (mapping->device,
55                                &data,
56                                mapping->space,
57                                addr,
58                                sizeof (unsigned_N)) != sizeof (unsigned_N))
59       device_error (mapping->device, "internal error - sim_core_read_N() - io_read_buffer should not fail");
60     val = T2H_N (data);
61   }
62   else
63 #endif
64     val = T2H_N (*(unsigned_N*) sim_core_translate (mapping, addr));
65   if (TRACE_P (cpu, TRACE_CORE_IDX))
66     trace_printf (CPU_STATE (cpu), cpu,
67                   "sim-n-core.c:%d: read-%d %s:0x%08lx -> 0x%lx\n",
68                   __LINE__,
69                   sizeof (unsigned_N),
70                   sim_core_map_to_str (map),
71                   (unsigned long) addr,
72                   (unsigned long) val);
73   return val;
74 }
75
76
77 INLINE_SIM_CORE(unsigned_N)
78 sim_core_read_unaligned_N(sim_cpu *cpu,
79                           sim_cia cia,
80                           sim_core_maps map,
81                           unsigned_word addr)
82 {
83   int alignment = sizeof (unsigned_N) - 1;
84   /* if hardwired to forced alignment just do it */
85   if (WITH_ALIGNMENT == FORCED_ALIGNMENT)
86     return sim_core_read_aligned_N (cpu, cia, map, addr & ~alignment);
87   else if ((addr & alignment) == 0)
88     return sim_core_read_aligned_N (cpu, cia, map, addr);
89   else
90     switch (CURRENT_ALIGNMENT)
91       {
92       case STRICT_ALIGNMENT:
93         /* FIXME - notify abort handler */
94         sim_io_error (CPU_STATE (cpu), "read-%d - misaligned access to 0x%lx",
95                       sizeof (unsigned_N), (unsigned long) addr);
96         return -1;
97       case NONSTRICT_ALIGNMENT:
98         {
99           unsigned_N val;
100           if (sim_core_read_buffer (CPU_STATE (cpu), map, &val, addr,
101                                     sizeof(unsigned_N))
102               != sizeof(unsigned_N))
103             sim_io_error(CPU_STATE (cpu), "misaligned %d byte read to 0x%lx failed",
104                          sizeof(unsigned_N), (unsigned long) addr);
105           val = T2H_N(val);
106           return val;
107         }
108       case FORCED_ALIGNMENT:
109         return sim_core_read_aligned_N (cpu, cia, map, addr & ~alignment);
110       case MIXED_ALIGNMENT:
111         sim_io_error (CPU_STATE (cpu), "internal error - sim_core_read_unaligned_N - mixed alignment");
112         return 0;
113       default:
114         sim_io_error (CPU_STATE (cpu), "internal error - sim_core_read_unaligned_N - bad switch");
115         return 0;
116       }
117 }
118
119
120 INLINE_SIM_CORE(void)
121 sim_core_write_aligned_N(sim_cpu *cpu,
122                          sim_cia cia,
123                          sim_core_maps map,
124                          unsigned_word addr,
125                          unsigned_N val)
126 {
127   sim_core_mapping *mapping = sim_core_find_mapping(CPU_CORE (cpu), map,
128                                                     addr,
129                                                     sizeof (unsigned_N),
130                                                     1,
131                                                     cpu, cia); /*abort*/
132 #if (WITH_DEVICES)
133   if (WITH_CALLBACK_MEMORY && mapping->device != NULL) {
134     unsigned_N data = H2T_N (val);
135     if (device_io_write_buffer (mapping->device,
136                                 &data,
137                                 mapping->space,
138                                 addr,
139                                 sizeof (unsigned_N), /* nr_bytes */
140                                 cpu,
141                                 cia) != sizeof (unsigned_N))
142       device_error (mapping->device, "internal error - sim_core_write_N() - io_write_buffer should not fail");
143   }
144   else
145 #endif
146     *(unsigned_N*) sim_core_translate (mapping, addr) = H2T_N (val);
147   if (TRACE_P (cpu, TRACE_CORE_IDX))
148     trace_printf (CPU_STATE (cpu), cpu,
149                   "sim-n-core.c:%d: write-%d %s:0x%08lx <- 0x%lx\n",
150                   __LINE__,
151                   sizeof (unsigned_N),
152                   sim_core_map_to_str (map),
153                   (unsigned long) addr,
154                   (unsigned long) val);
155 }
156
157
158 INLINE_SIM_CORE(void)
159 sim_core_write_unaligned_N(sim_cpu *cpu,
160                            sim_cia cia,
161                            sim_core_maps map,
162                            unsigned_word addr,
163                            unsigned_N val)
164 {
165   int alignment = sizeof (unsigned_N) - 1;
166   /* if hardwired to forced alignment just do it */
167   if (WITH_ALIGNMENT == FORCED_ALIGNMENT)
168     sim_core_write_aligned_N (cpu, cia, map, addr & ~alignment, val);
169   else if ((addr & alignment) == 0)
170     sim_core_write_aligned_N (cpu, cia, map, addr, val);
171   else
172     switch (CURRENT_ALIGNMENT)
173       {
174       case STRICT_ALIGNMENT:
175         /* FIXME - notify abort handler */
176         sim_io_error (CPU_STATE (cpu),
177                       "write-%d - misaligned access to 0x%lx",
178                       sizeof (unsigned_N), (unsigned long) addr);
179         break;
180       case NONSTRICT_ALIGNMENT:
181         {
182           val = T2H_N(val);
183           if (sim_core_write_buffer (CPU_STATE (cpu), map, &val, addr,
184                                      sizeof(unsigned_N))
185               != sizeof(unsigned_N))
186             sim_io_error(CPU_STATE (cpu),
187                          "misaligned %d byte read to 0x%lx failed",
188                          sizeof(unsigned_N), (unsigned long) addr);
189           break;
190         }
191       case FORCED_ALIGNMENT:
192         sim_core_write_aligned_N (cpu, cia, map, addr & ~alignment, val);
193       case MIXED_ALIGNMENT:
194         sim_io_error (CPU_STATE (cpu), "internal error - sim_core_write_unaligned_N - mixed alignment");
195         break;
196       default:
197         sim_io_error (CPU_STATE (cpu), "internal error - sim_core_write_unaligned_N - bad switch");
198         break;
199       }
200 }
201
202
203 /* NOTE: see start of file for #define of these macros */
204 #undef unsigned_N
205 #undef T2H_N
206 #undef H2T_N
207 #undef sim_core_read_aligned_N
208 #undef sim_core_write_aligned_N
209 #undef sim_core_read_unaligned_N
210 #undef sim_core_write_unaligned_N