First Commit of my working state
[simh.git] / sim_fio.c
1 /* sim_fio.c: simulator file I/O library
2
3 Copyright (c) 1993-2006, 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
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
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)) return 0; /* check arguments */
88 c = fread (bptr, size, count, fptr); /* read buffer */
89 if (sim_end || (size == sizeof (char)) || (c == 0)) /* le, byte, or err? */
90 return c; /* done */
91 for (j = 0, dptr = sptr = (unsigned char *) bptr; j < c; j++) { /* loop on items */
92 for (k = size - 1; k >= (((int32) size + 1) / 2); k--) {
93 by = *sptr; /* swap end-for-end */
94 *sptr++ = *(dptr + k);
95 *(dptr + k) = by;
96 }
97 sptr = dptr = dptr + size; /* next item */
98 }
99 return c;
100 }
101
102 size_t sim_fwrite (void *bptr, size_t size, size_t count, FILE *fptr)
103 {
104 size_t c, j, nelem, nbuf, lcnt, total;
105 int32 i, k;
106 unsigned char *sptr, *dptr;
107
108 if ((size == 0) || (count == 0)) return 0; /* check arguments */
109 if (sim_end || (size == sizeof (char))) /* le or byte? */
110 return fwrite (bptr, size, count, fptr); /* done */
111 nelem = FLIP_SIZE / size; /* elements in buffer */
112 nbuf = count / nelem; /* number buffers */
113 lcnt = count % nelem; /* count in last buf */
114 if (lcnt) nbuf = nbuf + 1;
115 else lcnt = nelem;
116 total = 0;
117 sptr = (unsigned char *) bptr; /* init input ptr */
118 for (i = nbuf; i > 0; i--) { /* loop on buffers */
119 c = (i == 1)? lcnt: nelem;
120 for (j = 0, dptr = sim_flip; j < c; j++) { /* loop on items */
121 for (k = size - 1; k >= 0; k--) *(dptr + k) = *sptr++;
122 dptr = dptr + size;
123 }
124 c = fwrite (sim_flip, size, c, fptr);
125 if (c == 0) return total;
126 total = total + c;
127 }
128 return total;
129 }
130
131 /* Get file size */
132
133 uint32 sim_fsize_name (char *fname)
134 {
135 FILE *fp;
136 uint32 sz;
137
138 if ((fp = sim_fopen (fname, "rb")) == NULL) return 0;
139 sz = sim_fsize (fp);
140 fclose (fp);
141 return sz;
142 }
143
144 uint32 sim_fsize (FILE *fp)
145 {
146 uint32 pos, sz;
147
148 if (fp == NULL) return 0;
149 pos = ftell (fp);
150 fseek (fp, 0, SEEK_END);
151 sz = ftell (fp);
152 fseek (fp, pos, SEEK_SET);
153 return sz;
154 }
155
156 /* OS-dependent routines */
157
158 /* Optimized file open */
159
160 FILE *sim_fopen (const char *file, const char *mode)
161 {
162 #if defined (VMS)
163 return fopen (file, mode, "ALQ=32", "DEQ=4096",
164 "MBF=6", "MBC=127", "FOP=cbt,tef", "ROP=rah,wbh", "CTX=stm");
165 #elif defined (USE_INT64) && defined (USE_ADDR64) && defined (__linux)
166 return fopen64 (file, mode);
167 #else
168 return fopen (file, mode);
169 #endif
170 }
171
172 /* Long seek */
173
174 #if defined (USE_INT64) && defined (USE_ADDR64)
175
176 /* 64b VMS */
177
178 #if (defined (__ALPHA) || defined (__ia64)) && defined (VMS) /* 64b VMS */
179 #define _SIM_IO_FSEEK_EXT_ 1
180
181 static t_int64 fpos_t_to_int64 (fpos_t *pos)
182 {
183 unsigned short *w = (unsigned short *) pos; /* endian dep! */
184 t_int64 result;
185
186 result = w[1];
187 result <<= 16;
188 result += w[0];
189 result <<= 9;
190 result += w[2];
191 return result;
192 }
193
194 static void int64_to_fpos_t (t_int64 ipos, fpos_t *pos, size_t mbc)
195 {
196 unsigned short *w = (unsigned short *) pos;
197 int bufsize = mbc << 9;
198
199 w[3] = 0;
200 w[2] = (unsigned short) (ipos % bufsize);
201 ipos -= w[2];
202 ipos >>= 9;
203 w[0] = (unsigned short) ipos;
204 ipos >>= 16;
205 w[1] = (unsigned short) ipos;
206 if ((w[2] == 0) && (w[0] || w[1])) {
207 w[2] = bufsize;
208 w[0] -= mbc;
209 }
210 return;
211 }
212
213 int sim_fseek (FILE *st, t_addr offset, int whence)
214 {
215 t_addr fileaddr;
216 fpos_t filepos;
217
218 switch (whence) {
219
220 case SEEK_SET:
221 fileaddr = offset;
222 break;
223
224 case SEEK_CUR:
225 if (fgetpos (st, &filepos)) return (-1);
226 fileaddr = fpos_t_to_int64 (&filepos);
227 fileaddr = fileaddr + offset;
228 break;
229
230 default:
231 errno = EINVAL;
232 return (-1);
233 }
234
235 int64_to_fpos_t (fileaddr, &filepos, 127);
236 return fsetpos (st, &filepos);
237 }
238
239 #endif
240
241 /* Alpha UNIX - natively 64b */
242
243 #if defined (__ALPHA) && defined (__unix__) /* Alpha UNIX */
244 #define _SIM_IO_FSEEK_EXT_ 1
245
246 int sim_fseek (FILE *st, t_addr offset, int whence)
247 {
248 return fseek (st, offset, whence);
249 }
250
251 #endif
252
253 /* Windows */
254
255 #if defined (_WIN32)
256 #define _SIM_IO_FSEEK_EXT_ 1
257
258 int sim_fseek (FILE *st, t_addr offset, int whence)
259 {
260 fpos_t fileaddr;
261
262 switch (whence) {
263
264 case SEEK_SET:
265 fileaddr = offset;
266 break;
267
268 case SEEK_CUR:
269 if (fgetpos (st, &fileaddr)) return (-1);
270 fileaddr = fileaddr + offset;
271 break;
272
273 default:
274 errno = EINVAL;
275 return (-1);
276 }
277
278 return fsetpos (st, &fileaddr);
279 }
280
281 #endif /* end Windows */
282
283 /* Linux */
284
285 #if defined (__linux)
286 #define _SIM_IO_FSEEK_EXT_ 1
287
288 int sim_fseek (FILE *st, t_addr xpos, int origin)
289 {
290 return fseeko64 (st, xpos, origin);
291 }
292
293 #endif /* end Linux with LFS */
294
295 /* Apple OS/X */
296
297 #if defined (__APPLE__) || defined (__FreeBSD__)
298 #define _SIM_IO_FSEEK_EXT_ 1
299
300 int sim_fseek (FILE *st, t_addr xpos, int origin)
301 {
302 return fseeko (st, xpos, origin);
303 }
304
305 #endif /* end Apple OS/X */
306
307 #endif /* end 64b seek defs */
308
309 /* Default: no OS-specific routine has been defined */
310
311 #if !defined (_SIM_IO_FSEEK_EXT_)
312 #define _SIM_IO_FSEEK_EXT_ 0
313
314 int sim_fseek (FILE *st, t_addr xpos, int origin)
315 {
316 return fseek (st, (int32) xpos, origin);
317 }
318
319 #endif
320
321 uint32 sim_taddr_64 = _SIM_IO_FSEEK_EXT_;