1 /* sim_fio.c: simulator file I/O library
3 Copyright (c) 1993-2006, Robert M Supnik
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:
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
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.
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.
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
36 This library includes:
38 sim_finit - initialize package
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
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).
52 static unsigned char sim_flip
[FLIP_SIZE
];
53 int32 sim_end
= 1; /* 1 = little */
55 /* OS-independent, endian independent binary I/O package
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.
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.
72 int32
sim_finit (void)
74 union {int32 i
; char c
[sizeof (int32
)]; } end_test
;
76 end_test
.i
= 1; /* test endian-ness */
77 sim_end
= end_test
.c
[0];
81 size_t sim_fread (void *bptr
, size_t size
, size_t count
, FILE *fptr
)
85 unsigned char by
, *sptr
, *dptr
;
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? */
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
);
97 sptr
= dptr
= dptr
+ size
; /* next item */
102 size_t sim_fwrite (void *bptr
, size_t size
, size_t count
, FILE *fptr
)
104 size_t c
, j
, nelem
, nbuf
, lcnt
, total
;
106 unsigned char *sptr
, *dptr
;
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;
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
++;
124 c
= fwrite (sim_flip
, size
, c
, fptr
);
125 if (c
== 0) return total
;
133 uint32
sim_fsize_name (char *fname
)
138 if ((fp
= sim_fopen (fname
, "rb")) == NULL
) return 0;
144 uint32
sim_fsize (FILE *fp
)
148 if (fp
== NULL
) return 0;
150 fseek (fp
, 0, SEEK_END
);
152 fseek (fp
, pos
, SEEK_SET
);
156 /* OS-dependent routines */
158 /* Optimized file open */
160 FILE *sim_fopen (const char *file
, const char *mode
)
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
);
168 return fopen (file
, mode
);
174 #if defined (USE_INT64) && defined (USE_ADDR64)
178 #if (defined (__ALPHA) || defined (__ia64)) && defined (VMS) /* 64b VMS */
179 #define _SIM_IO_FSEEK_EXT_ 1
181 static t_int64
fpos_t_to_int64 (fpos_t *pos
)
183 unsigned short *w
= (unsigned short *) pos
; /* endian dep! */
194 static void int64_to_fpos_t (t_int64 ipos
, fpos_t *pos
, size_t mbc
)
196 unsigned short *w
= (unsigned short *) pos
;
197 int bufsize
= mbc
<< 9;
200 w
[2] = (unsigned short) (ipos
% bufsize
);
203 w
[0] = (unsigned short) ipos
;
205 w
[1] = (unsigned short) ipos
;
206 if ((w
[2] == 0) && (w
[0] || w
[1])) {
213 int sim_fseek (FILE *st
, t_addr offset
, int whence
)
225 if (fgetpos (st
, &filepos
)) return (-1);
226 fileaddr
= fpos_t_to_int64 (&filepos
);
227 fileaddr
= fileaddr
+ offset
;
235 int64_to_fpos_t (fileaddr
, &filepos
, 127);
236 return fsetpos (st
, &filepos
);
241 /* Alpha UNIX - natively 64b */
243 #if defined (__ALPHA) && defined (__unix__) /* Alpha UNIX */
244 #define _SIM_IO_FSEEK_EXT_ 1
246 int sim_fseek (FILE *st
, t_addr offset
, int whence
)
248 return fseek (st
, offset
, whence
);
256 #define _SIM_IO_FSEEK_EXT_ 1
258 int sim_fseek (FILE *st
, t_addr offset
, int whence
)
269 if (fgetpos (st
, &fileaddr
)) return (-1);
270 fileaddr
= fileaddr
+ offset
;
278 return fsetpos (st
, &fileaddr
);
281 #endif /* end Windows */
285 #if defined (__linux)
286 #define _SIM_IO_FSEEK_EXT_ 1
288 int sim_fseek (FILE *st
, t_addr xpos
, int origin
)
290 return fseeko64 (st
, xpos
, origin
);
293 #endif /* end Linux with LFS */
297 #if defined (__APPLE__) || defined (__FreeBSD__)
298 #define _SIM_IO_FSEEK_EXT_ 1
300 int sim_fseek (FILE *st
, t_addr xpos
, int origin
)
302 return fseeko (st
, xpos
, origin
);
305 #endif /* end Apple OS/X */
307 #endif /* end 64b seek defs */
309 /* Default: no OS-specific routine has been defined */
311 #if !defined (_SIM_IO_FSEEK_EXT_)
312 #define _SIM_IO_FSEEK_EXT_ 0
314 int sim_fseek (FILE *st
, t_addr xpos
, int origin
)
316 return fseek (st
, (int32
) xpos
, origin
);
321 uint32 sim_taddr_64
= _SIM_IO_FSEEK_EXT_
;