1 /* sim_fio.c: simulator file I/O library
2 
3    Copyright (c) 1993-2008, Robert M Supnik
4 
5    Permission is hereby granted, free of charge, to any person obtaining a
6    copy of this software and associated documentation files (the "Software"),
7    to deal in the Software without restriction, including without limitation
8    the rights to use, copy, modify, merge, publish, distribute, sublicense,
9    and/or sell copies of the Software, and to permit persons to whom the
10    Software is furnished to do so, subject to the following conditions:
11 
12    The above copyright notice and this permission notice shall be included in
13    all copies or substantial portions of the Software.
14 
15    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18    ROBERT M SUPNIK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19    IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20    CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22    Except as contained in this notice, the name of Robert M Supnik shall not be
23    used in advertising or otherwise to promote the sale, use or other dealings
24    in this Software without prior written authorization from Robert M Supnik.
25 
26    28-Jun-07    RMS     Added VMS IA64 support (from Norm Lastovica)
27    10-Jul-06    RMS     Fixed linux conditionalization (from Chaskiel Grundman)
28    15-May-06    RMS     Added sim_fsize_name
29    21-Apr-06    RMS     Added FreeBSD large file support (from Mark Martinec)
30    19-Nov-05    RMS     Added OS/X large file support (from Peter Schorn)
31    16-Aug-05    RMS     Fixed C++ declaration and cast problems
32    17-Jul-04    RMS     Fixed bug in optimized sim_fread (reported by Scott Bailey)
33    26-May-04    RMS     Optimized sim_fread (suggested by John Dundas)
34    02-Jan-04    RMS     Split out from SCP
35 
36    This library includes:
37 
38    sim_finit    -       initialize package
39    sim_fopen    -       open file
40    sim_fread    -       endian independent read (formerly fxread)
41    sim_write    -       endian independent write (formerly fxwrite)
42    sim_fseek    -       extended (>32b) seek (formerly fseek_ext)
43    sim_fsize    -       get file size
44 
45    sim_fopen and sim_fseek are OS-dependent.  The other routines are not.
46    sim_fsize is always a 32b routine (it is used only with small capacity random
47    access devices like fixed head disks and DECtapes).
48 */
49 
50 #include "sim_defs.h"
51 
52 static unsigned char sim_flip[FLIP_SIZE];
53 int32 sim_end = 1;                                      /* 1 = little */
54 
55 /* OS-independent, endian independent binary I/O package
56 
57    For consistency, all binary data read and written by the simulator
58    is stored in little endian data order.  That is, in a multi-byte
59    data item, the bytes are written out right to left, low order byte
60    to high order byte.  On a big endian host, data is read and written
61    from high byte to low byte.  Consequently, data written on a little
62    endian system must be byte reversed to be usable on a big endian
63    system, and vice versa.
64 
65    These routines are analogs of the standard C runtime routines
66    fread and fwrite.  If the host is little endian, or the data items
67    are size char, then the calls are passed directly to fread or
68    fwrite.  Otherwise, these routines perform the necessary byte swaps.
69    Sim_fread swaps in place, sim_fwrite uses an intermediate buffer.
70 */
71 
sim_finit(void)72 int32 sim_finit (void)
73 {
74 union {int32 i; char c[sizeof (int32)]; } end_test;
75 
76 end_test.i = 1;                                         /* test endian-ness */
77 sim_end = end_test.c[0];
78 return sim_end;
79 }
80 
sim_fread(void * bptr,size_t size,size_t count,FILE * fptr)81 size_t sim_fread (void *bptr, size_t size, size_t count, FILE *fptr)
82 {
83 size_t c, j;
84 int32 k;
85 unsigned char by, *sptr, *dptr;
86 
87 if ((size == 0) || (count == 0))                        /* check arguments */
88     return 0;
89 c = fread (bptr, size, count, fptr);                    /* read buffer */
90 if (sim_end || (size == sizeof (char)) || (c == 0))     /* le, byte, or err? */
91     return c;                                           /* done */
92 for (j = 0, dptr = sptr = (unsigned char *) bptr; j < c; j++) { /* loop on items */
93     for (k = size - 1; k >= (((int32) size + 1) / 2); k--) {
94         by = *sptr;                                     /* swap end-for-end */
95         *sptr++ = *(dptr + k);
96         *(dptr + k) = by;
97         }
98     sptr = dptr = dptr + size;                          /* next item */
99     }
100 return c;
101 }
102 
sim_fwrite(void * bptr,size_t size,size_t count,FILE * fptr)103 size_t sim_fwrite (void *bptr, size_t size, size_t count, FILE *fptr)
104 {
105 size_t c, j, nelem, nbuf, lcnt, total;
106 int32 i, k;
107 unsigned char *sptr, *dptr;
108 
109 if ((size == 0) || (count == 0))                        /* check arguments */
110     return 0;
111 if (sim_end || (size == sizeof (char)))                 /* le or byte? */
112     return fwrite (bptr, size, count, fptr);            /* done */
113 nelem = FLIP_SIZE / size;                               /* elements in buffer */
114 nbuf = count / nelem;                                   /* number buffers */
115 lcnt = count % nelem;                                   /* count in last buf */
116 if (lcnt) nbuf = nbuf + 1;
117 else lcnt = nelem;
118 total = 0;
119 sptr = (unsigned char *) bptr;                          /* init input ptr */
120 for (i = nbuf; i > 0; i--) {                            /* loop on buffers */
121     c = (i == 1)? lcnt: nelem;
122     for (j = 0, dptr = sim_flip; j < c; j++) {          /* loop on items */
123         for (k = size - 1; k >= 0; k--)
124             *(dptr + k) = *sptr++;
125         dptr = dptr + size;
126         }
127     c = fwrite (sim_flip, size, c, fptr);
128     if (c == 0)
129         return total;
130     total = total + c;
131     }
132 return total;
133 }
134 
135 /* Get file size */
136 
sim_fsize_name(char * fname)137 uint32 sim_fsize_name (char *fname)
138 {
139 FILE *fp;
140 uint32 sz;
141 
142 if ((fp = sim_fopen (fname, "rb")) == NULL)
143     return 0;
144 sz = sim_fsize (fp);
145 fclose (fp);
146 return sz;
147 }
148 
sim_fsize(FILE * fp)149 uint32 sim_fsize (FILE *fp)
150 {
151 uint32 pos, sz;
152 
153 if (fp == NULL)
154     return 0;
155 pos = ftell (fp);
156 fseek (fp, 0, SEEK_END);
157 sz = ftell (fp);
158 fseek (fp, pos, SEEK_SET);
159 return sz;
160 }
161 
162 /* OS-dependent routines */
163 
164 /* Optimized file open */
165 
sim_fopen(const char * file,const char * mode)166 FILE *sim_fopen (const char *file, const char *mode)
167 {
168 #if defined (VMS)
169 return fopen (file, mode, "ALQ=32", "DEQ=4096",
170         "MBF=6", "MBC=127", "FOP=cbt,tef", "ROP=rah,wbh", "CTX=stm");
171 #elif defined (USE_INT64) && defined (USE_ADDR64) && defined (__linux)
172 return fopen64 (file, mode);
173 #else
174 return fopen (file, mode);
175 #endif
176 }
177 
178 /* Long seek */
179 
180 #if defined (USE_INT64) && defined (USE_ADDR64)
181 
182 /* 64b VMS */
183 
184 #if (defined (__ALPHA) || defined (__ia64)) && defined (VMS) /* 64b VMS */
185 #define _SIM_IO_FSEEK_EXT_      1
186 
fpos_t_to_int64(fpos_t * pos)187 static t_int64 fpos_t_to_int64 (fpos_t *pos)
188 {
189 unsigned short *w = (unsigned short *) pos;             /* endian dep! */
190 t_int64 result;
191 
192 result = w[1];
193 result <<= 16;
194 result += w[0];
195 result <<= 9;
196 result += w[2];
197 return result;
198 }
199 
int64_to_fpos_t(t_int64 ipos,fpos_t * pos,size_t mbc)200 static void int64_to_fpos_t (t_int64 ipos, fpos_t *pos, size_t mbc)
201 {
202 unsigned short *w = (unsigned short *) pos;
203 int bufsize = mbc << 9;
204 
205 w[3] = 0;
206 w[2] = (unsigned short) (ipos % bufsize);
207 ipos -= w[2];
208 ipos >>= 9;
209 w[0] = (unsigned short) ipos;
210 ipos >>= 16;
211 w[1] = (unsigned short) ipos;
212 if ((w[2] == 0) && (w[0] || w[1])) {
213     w[2] = bufsize;
214     w[0] -= mbc;
215     }
216 return;
217 }
218 
sim_fseek(FILE * st,t_addr offset,int whence)219 int sim_fseek (FILE *st, t_addr offset, int whence)
220 {
221 t_addr fileaddr;
222 fpos_t filepos;
223 
224 switch (whence) {
225 
226     case SEEK_SET:
227         fileaddr = offset;
228         break;
229 
230     case SEEK_CUR:
231         if (fgetpos (st, &filepos))
232             return (-1);
233         fileaddr = fpos_t_to_int64 (&filepos);
234         fileaddr = fileaddr + offset;
235         break;
236 
237     default:
238         errno = EINVAL;
239         return (-1);
240         }
241 
242 int64_to_fpos_t (fileaddr, &filepos, 127);
243 return fsetpos (st, &filepos);
244 }
245 
246 #endif
247 
248 /* Alpha UNIX - natively 64b */
249 
250 #if defined (__ALPHA) && defined (__unix__)             /* Alpha UNIX */
251 #define _SIM_IO_FSEEK_EXT_      1
252 
sim_fseek(FILE * st,t_addr offset,int whence)253 int sim_fseek (FILE *st, t_addr offset, int whence)
254 {
255 return fseek (st, offset, whence);
256 }
257 
258 #endif
259 
260 /* Windows */
261 
262 #if defined (_WIN32)
263 #define _SIM_IO_FSEEK_EXT_      1
264 
sim_fseek(FILE * st,t_addr offset,int whence)265 int sim_fseek (FILE *st, t_addr offset, int whence)
266 {
267 fpos_t fileaddr;
268 
269 switch (whence) {
270 
271     case SEEK_SET:
272         fileaddr = offset;
273         break;
274 
275     case SEEK_CUR:
276         if (fgetpos (st, &fileaddr))
277             return (-1);
278         fileaddr = fileaddr + offset;
279         break;
280 
281     default:
282         errno = EINVAL;
283         return (-1);
284         }
285 
286 return fsetpos (st, &fileaddr);
287 }
288 
289 #endif                                                  /* end Windows */
290 
291 /* Linux */
292 
293 #if defined (__linux)
294 #define _SIM_IO_FSEEK_EXT_      1
295 
sim_fseek(FILE * st,t_addr xpos,int origin)296 int sim_fseek (FILE *st, t_addr xpos, int origin)
297 {
298 return fseeko64 (st, xpos, origin);
299 }
300 
301 #endif                                                  /* end Linux with LFS */
302 
303 /* Apple OS/X */
304 
305 #if defined (__APPLE__) || defined (__FreeBSD__)
306 #define _SIM_IO_FSEEK_EXT_      1
307 
sim_fseek(FILE * st,t_addr xpos,int origin)308 int sim_fseek (FILE *st, t_addr xpos, int origin)
309 {
310 return fseeko (st, xpos, origin);
311 }
312 
313 #endif  /* end Apple OS/X */
314 
315 #endif                                                  /* end 64b seek defs */
316 
317 /* Default: no OS-specific routine has been defined */
318 
319 #if !defined (_SIM_IO_FSEEK_EXT_)
320 #define _SIM_IO_FSEEK_EXT_      0
321 
sim_fseek(FILE * st,t_addr xpos,int origin)322 int sim_fseek (FILE *st, t_addr xpos, int origin)
323 {
324 return fseek (st, (int32) xpos, origin);
325 }
326 
327 #endif
328 
329 uint32 sim_taddr_64 = _SIM_IO_FSEEK_EXT_;
330