1 |
cebix |
1.1 |
/* |
2 |
|
|
* macos_util.cpp - MacOS definitions/utility functions |
3 |
|
|
* |
4 |
cebix |
1.6 |
* SheepShaver (C) 1997-2004 Christian Bauer and Marc Hellwig |
5 |
cebix |
1.1 |
* |
6 |
|
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#include "sysdeps.h" |
22 |
|
|
#include "cpu_emulation.h" |
23 |
|
|
#include "main.h" |
24 |
|
|
#include "sony.h" |
25 |
|
|
#include "disk.h" |
26 |
|
|
#include "cdrom.h" |
27 |
|
|
#include "xlowmem.h" |
28 |
|
|
#include "emul_op.h" |
29 |
|
|
#include "macos_util.h" |
30 |
gbeauche |
1.4 |
#include "thunks.h" |
31 |
cebix |
1.1 |
|
32 |
|
|
#define DEBUG 0 |
33 |
|
|
#include "debug.h" |
34 |
|
|
|
35 |
|
|
|
36 |
|
|
// Function pointers |
37 |
|
|
typedef long (*cu_ptr)(void *, uint32); |
38 |
|
|
static uint32 cu_tvect = 0; |
39 |
|
|
static inline long CallUniversal(void *arg1, uint32 arg2) |
40 |
|
|
{ |
41 |
|
|
return (long)CallMacOS2(cu_ptr, cu_tvect, arg1, arg2); |
42 |
|
|
} |
43 |
|
|
typedef int16 (*gsl_ptr)(char *, uint32, uint32, uint32 *, void **, char *); |
44 |
|
|
static uint32 gsl_tvect = 0; |
45 |
gbeauche |
1.8 |
static inline int16 GetSharedLibrary(uintptr arg1, uint32 arg2, uint32 arg3, uintptr arg4, uintptr arg5, uintptr arg6) |
46 |
cebix |
1.1 |
{ |
47 |
gbeauche |
1.8 |
return (int16)CallMacOS6(gsl_ptr, gsl_tvect, (char *)arg1, arg2, arg3, (uint32 *)arg4, (void **)arg5, (char *)arg6); |
48 |
cebix |
1.1 |
} |
49 |
|
|
typedef int16 (*fs_ptr)(uint32, char *, void **, uint32 *); |
50 |
|
|
static uint32 fs_tvect = 0; |
51 |
gbeauche |
1.8 |
static inline int16 FindSymbol(uint32 arg1, uintptr arg2, uintptr arg3, uintptr arg4) |
52 |
cebix |
1.1 |
{ |
53 |
gbeauche |
1.8 |
return (int16)CallMacOS4(fs_ptr, fs_tvect, arg1, (char *)arg2, (void **)arg3, (uint32 **)arg4); |
54 |
cebix |
1.1 |
} |
55 |
|
|
typedef int16 (*cc_ptr)(uint32 *); |
56 |
|
|
static uint32 cc_tvect = 0; |
57 |
|
|
static inline int16 CloseConnection(uint32 *arg1) |
58 |
|
|
{ |
59 |
|
|
return (int16)CallMacOS1(cc_ptr, cc_tvect, arg1); |
60 |
|
|
} |
61 |
gbeauche |
1.10 |
typedef uint32 (*nps_ptr)(uint32); |
62 |
gbeauche |
1.7 |
static uint32 nps_tvect = 0; |
63 |
gbeauche |
1.10 |
static inline uint32 NewPtrSys(uint32 arg1) |
64 |
gbeauche |
1.7 |
{ |
65 |
gbeauche |
1.10 |
return CallMacOS1(nps_ptr, nps_tvect, arg1); |
66 |
gbeauche |
1.7 |
} |
67 |
gbeauche |
1.10 |
typedef void (*d_ptr)(uint32); |
68 |
gbeauche |
1.7 |
static uint32 d_tvect = 0; |
69 |
gbeauche |
1.10 |
static inline void DisposePtr(uint32 arg1) |
70 |
gbeauche |
1.7 |
{ |
71 |
|
|
CallMacOS1(d_ptr, d_tvect, arg1); |
72 |
|
|
} |
73 |
cebix |
1.1 |
|
74 |
|
|
|
75 |
|
|
/* |
76 |
|
|
* Reset MacOS utilities |
77 |
|
|
*/ |
78 |
|
|
|
79 |
|
|
void MacOSUtilReset(void) |
80 |
|
|
{ |
81 |
|
|
cu_tvect = 0; |
82 |
|
|
gsl_tvect = 0; |
83 |
|
|
fs_tvect = 0; |
84 |
|
|
cc_tvect = 0; |
85 |
|
|
} |
86 |
|
|
|
87 |
|
|
|
88 |
|
|
/* |
89 |
|
|
* Enqueue QElem to list |
90 |
|
|
*/ |
91 |
|
|
|
92 |
|
|
void Enqueue(uint32 elem, uint32 list) |
93 |
|
|
{ |
94 |
|
|
WriteMacInt32(elem + qLink, 0); |
95 |
|
|
if (!ReadMacInt32(list + qTail)) { |
96 |
|
|
WriteMacInt32(list + qHead, elem); |
97 |
|
|
WriteMacInt32(list + qTail, elem); |
98 |
|
|
} else { |
99 |
|
|
WriteMacInt32(ReadMacInt32(list + qTail) + qLink, elem); |
100 |
|
|
WriteMacInt32(list + qTail, elem); |
101 |
|
|
} |
102 |
|
|
} |
103 |
|
|
|
104 |
|
|
|
105 |
|
|
/* |
106 |
|
|
* Find first free drive number, starting at num |
107 |
|
|
*/ |
108 |
|
|
|
109 |
|
|
static bool is_drive_number_free(int num) |
110 |
|
|
{ |
111 |
|
|
uint32 e = ReadMacInt32(0x308 + qHead); |
112 |
|
|
while (e) { |
113 |
|
|
uint32 d = e - dsQLink; |
114 |
|
|
if ((int)ReadMacInt16(d + dsQDrive) == num) |
115 |
|
|
return false; |
116 |
|
|
e = ReadMacInt32(e + qLink); |
117 |
|
|
} |
118 |
|
|
return true; |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
int FindFreeDriveNumber(int num) |
122 |
|
|
{ |
123 |
|
|
while (!is_drive_number_free(num)) |
124 |
|
|
num++; |
125 |
|
|
return num; |
126 |
|
|
} |
127 |
|
|
|
128 |
|
|
|
129 |
|
|
/* |
130 |
|
|
* Mount volume with given file handle (call this function when you are unable to |
131 |
|
|
* do automatic media change detection and the user has to press a special key |
132 |
|
|
* or something to mount a volume; this function will check if there's really a |
133 |
|
|
* volume in the drive with SysIsDiskInserted(); volumes which are present on startup |
134 |
|
|
* are automatically mounted) |
135 |
|
|
*/ |
136 |
|
|
|
137 |
|
|
void MountVolume(void *fh) |
138 |
|
|
{ |
139 |
|
|
SonyMountVolume(fh) || DiskMountVolume(fh) || CDROMMountVolume(fh); |
140 |
|
|
} |
141 |
|
|
|
142 |
|
|
|
143 |
|
|
/* |
144 |
|
|
* Calculate disk image file layout given file size and first 256 data bytes |
145 |
|
|
*/ |
146 |
|
|
|
147 |
|
|
void FileDiskLayout(loff_t size, uint8 *data, loff_t &start_byte, loff_t &real_size) |
148 |
|
|
{ |
149 |
|
|
if (size == 419284 || size == 838484) { |
150 |
|
|
// 400K/800K DiskCopy image, 84 byte header |
151 |
|
|
start_byte = 84; |
152 |
|
|
real_size = (size - 84) & ~0x1ff; |
153 |
|
|
} else { |
154 |
|
|
// 0..511 byte header |
155 |
|
|
start_byte = size & 0x1ff; |
156 |
|
|
real_size = size - start_byte; |
157 |
|
|
} |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
|
161 |
|
|
/* |
162 |
|
|
* Find symbol in shared library (using CFM) |
163 |
|
|
* lib and sym must be Pascal strings! |
164 |
|
|
*/ |
165 |
|
|
|
166 |
gbeauche |
1.10 |
uint32 FindLibSymbol(char *lib_str, char *sym_str) |
167 |
cebix |
1.1 |
{ |
168 |
gbeauche |
1.4 |
SheepVar32 conn_id = 0; |
169 |
|
|
SheepVar32 main_addr = 0; |
170 |
|
|
SheepArray<256> err; |
171 |
|
|
WriteMacInt8(err.addr(), 0); |
172 |
|
|
SheepVar32 sym_addr = 0; |
173 |
|
|
SheepVar32 sym_class = 0; |
174 |
cebix |
1.1 |
|
175 |
gbeauche |
1.4 |
SheepString lib(lib_str); |
176 |
|
|
SheepString sym(sym_str); |
177 |
|
|
|
178 |
|
|
D(bug("FindLibSymbol %s in %s...\n", sym.value()+1, lib.value()+1)); |
179 |
cebix |
1.1 |
|
180 |
gbeauche |
1.2 |
if (ReadMacInt32(XLM_RUN_MODE) == MODE_EMUL_OP) { |
181 |
cebix |
1.1 |
M68kRegisters r; |
182 |
|
|
|
183 |
|
|
// Find shared library |
184 |
gbeauche |
1.9 |
static const uint8 proc1_template[] = { |
185 |
|
|
0x55, 0x8f, // subq.l #2,a7 |
186 |
|
|
0x2f, 0x08, // move.l a0,-(a7) |
187 |
|
|
0x2f, 0x3c, 0x70, 0x77, 0x70, 0x63, // move.l #'pwpc',-(a7) |
188 |
|
|
0x2f, 0x3c, 0x00, 0x00, 0x00, 0x01, // move.l #kReferenceCFrag,-(a7) |
189 |
|
|
0x2f, 0x09, // move.l a1,-(a7) |
190 |
|
|
0x2f, 0x0a, // move.l a2,-(a7) |
191 |
|
|
0x2f, 0x0b, // move.l a3,-(a7) |
192 |
|
|
0x3f, 0x3c, 0x00, 0x01, // (GetSharedLibrary) |
193 |
|
|
0xaa, 0x5a, // CFMDispatch |
194 |
|
|
0x30, 0x1f, // move.w (a7)+,d0 |
195 |
|
|
M68K_RTS >> 8, M68K_RTS |
196 |
cebix |
1.1 |
}; |
197 |
gbeauche |
1.9 |
BUILD_SHEEPSHAVER_PROCEDURE(proc1); |
198 |
gbeauche |
1.4 |
r.a[0] = lib.addr(); |
199 |
|
|
r.a[1] = conn_id.addr(); |
200 |
|
|
r.a[2] = main_addr.addr(); |
201 |
|
|
r.a[3] = err.addr(); |
202 |
gbeauche |
1.9 |
Execute68k(proc1, &r); |
203 |
gbeauche |
1.4 |
D(bug(" GetSharedLibrary: ret %d, connection ID %ld, main %p\n", (int16)r.d[0], conn_id.value(), main_addr.value())); |
204 |
cebix |
1.1 |
if (r.d[0]) |
205 |
gbeauche |
1.10 |
return 0; |
206 |
cebix |
1.1 |
|
207 |
|
|
// Find symbol |
208 |
gbeauche |
1.9 |
static const uint8 proc2_template[] = { |
209 |
|
|
0x55, 0x8f, // subq.l #2,a7 |
210 |
|
|
0x2f, 0x00, // move.l d0,-(a7) |
211 |
|
|
0x2f, 0x08, // move.l a0,-(a7) |
212 |
|
|
0x2f, 0x09, // move.l a1,-(a7) |
213 |
|
|
0x2f, 0x0a, // move.l a2,-(a7) |
214 |
|
|
0x3f, 0x3c, 0x00, 0x05, // (FindSymbol) |
215 |
|
|
0xaa, 0x5a, // CFMDispatch |
216 |
|
|
0x30, 0x1f, // move.w (a7)+,d0 |
217 |
|
|
M68K_RTS >> 8, M68K_RTS |
218 |
cebix |
1.1 |
}; |
219 |
gbeauche |
1.9 |
BUILD_SHEEPSHAVER_PROCEDURE(proc2); |
220 |
gbeauche |
1.4 |
r.d[0] = conn_id.value(); |
221 |
|
|
r.a[0] = sym.addr(); |
222 |
|
|
r.a[1] = sym_addr.addr(); |
223 |
|
|
r.a[2] = sym_class.addr(); |
224 |
gbeauche |
1.9 |
Execute68k(proc2, &r); |
225 |
gbeauche |
1.4 |
D(bug(" FindSymbol1: ret %d, sym_addr %p, sym_class %ld\n", (int16)r.d[0], sym_addr.value(), sym_class.value())); |
226 |
cebix |
1.1 |
//!! CloseConnection()? |
227 |
|
|
if (r.d[0]) |
228 |
gbeauche |
1.10 |
return 0; |
229 |
cebix |
1.1 |
else |
230 |
gbeauche |
1.10 |
return sym_addr.value(); |
231 |
cebix |
1.1 |
|
232 |
|
|
} else { |
233 |
|
|
|
234 |
|
|
if (GetSharedLibrary == NULL || FindSymbol == NULL) { |
235 |
|
|
printf("FATAL: FindLibSymbol() called too early\n"); |
236 |
|
|
return 0; |
237 |
|
|
} |
238 |
|
|
int16 res; |
239 |
gbeauche |
1.8 |
res = GetSharedLibrary(lib.addr(), FOURCC('p','w','p','c'), 1, conn_id.addr(), main_addr.addr(), err.addr()); |
240 |
gbeauche |
1.4 |
D(bug(" GetSharedLibrary: ret %d, connection ID %ld, main %p\n", res, conn_id.value(), main_addr.value())); |
241 |
cebix |
1.1 |
if (res) |
242 |
gbeauche |
1.10 |
return 0; |
243 |
gbeauche |
1.8 |
res = FindSymbol(conn_id.value(), sym.addr(), sym_addr.addr(), sym_class.addr()); |
244 |
gbeauche |
1.4 |
D(bug(" FindSymbol: ret %d, sym_addr %p, sym_class %ld\n", res, sym_addr.value(), sym_class.value())); |
245 |
cebix |
1.1 |
//!!?? CloseConnection(&conn_id); |
246 |
|
|
if (res) |
247 |
gbeauche |
1.10 |
return 0; |
248 |
cebix |
1.1 |
else |
249 |
gbeauche |
1.10 |
return sym_addr.value(); |
250 |
cebix |
1.1 |
} |
251 |
|
|
} |
252 |
|
|
|
253 |
|
|
|
254 |
|
|
/* |
255 |
|
|
* Find CallUniversalProc() TVector |
256 |
|
|
*/ |
257 |
|
|
|
258 |
|
|
void InitCallUniversalProc() |
259 |
|
|
{ |
260 |
gbeauche |
1.10 |
cu_tvect = FindLibSymbol("\014InterfaceLib", "\021CallUniversalProc"); |
261 |
cebix |
1.1 |
D(bug("CallUniversalProc TVECT at %08lx\n", cu_tvect)); |
262 |
|
|
if (cu_tvect == 0) { |
263 |
|
|
printf("FATAL: Can't find CallUniversalProc()\n"); |
264 |
|
|
QuitEmulator(); |
265 |
|
|
} |
266 |
|
|
|
267 |
gbeauche |
1.10 |
gsl_tvect = FindLibSymbol("\014InterfaceLib", "\020GetSharedLibrary"); |
268 |
cebix |
1.1 |
D(bug("GetSharedLibrary TVECT at %08lx\n", gsl_tvect)); |
269 |
|
|
if (gsl_tvect == 0) { |
270 |
|
|
printf("FATAL: Can't find GetSharedLibrary()\n"); |
271 |
|
|
QuitEmulator(); |
272 |
|
|
} |
273 |
|
|
|
274 |
gbeauche |
1.10 |
fs_tvect = FindLibSymbol("\014InterfaceLib", "\012FindSymbol"); |
275 |
cebix |
1.1 |
D(bug("FindSymbol TVECT at %08lx\n", fs_tvect)); |
276 |
|
|
if (fs_tvect == 0) { |
277 |
|
|
printf("FATAL: Can't find FindSymbol()\n"); |
278 |
|
|
QuitEmulator(); |
279 |
|
|
} |
280 |
|
|
|
281 |
gbeauche |
1.10 |
cc_tvect = FindLibSymbol("\014InterfaceLib", "\017CloseConnection"); |
282 |
cebix |
1.1 |
D(bug("CloseConnection TVECT at %08lx\n", cc_tvect)); |
283 |
|
|
if (cc_tvect == 0) { |
284 |
|
|
printf("FATAL: Can't find CloseConnection()\n"); |
285 |
|
|
QuitEmulator(); |
286 |
|
|
} |
287 |
gbeauche |
1.7 |
|
288 |
gbeauche |
1.10 |
nps_tvect = FindLibSymbol("\014InterfaceLib", "\011NewPtrSys"); |
289 |
gbeauche |
1.7 |
D(bug("NewPtrSys TVECT at %08lx\n", nps_tvect)); |
290 |
|
|
if (nps_tvect == 0) { |
291 |
|
|
printf("FATAL: Can't find NewPtrSys()\n"); |
292 |
|
|
QuitEmulator(); |
293 |
|
|
} |
294 |
|
|
|
295 |
gbeauche |
1.10 |
d_tvect = FindLibSymbol("\014InterfaceLib", "\012DisposePtr"); |
296 |
gbeauche |
1.7 |
D(bug("DisposePtr TVECT at %08lx\n", d_tvect)); |
297 |
|
|
if (d_tvect == 0) { |
298 |
|
|
printf("FATAL: Can't find DisposePtr()\n"); |
299 |
|
|
QuitEmulator(); |
300 |
|
|
} |
301 |
cebix |
1.1 |
} |
302 |
|
|
|
303 |
|
|
|
304 |
|
|
/* |
305 |
|
|
* CallUniversalProc |
306 |
|
|
*/ |
307 |
|
|
|
308 |
|
|
long CallUniversalProc(void *upp, uint32 info) |
309 |
|
|
{ |
310 |
|
|
if (cu_tvect == 0) { |
311 |
|
|
printf("FATAL: CallUniversalProc() called too early\n"); |
312 |
|
|
return 0; |
313 |
|
|
} |
314 |
|
|
return CallUniversal(upp, info); |
315 |
|
|
} |
316 |
|
|
|
317 |
|
|
|
318 |
|
|
/* |
319 |
|
|
* Convert time_t value to MacOS time (seconds since 1.1.1904) |
320 |
|
|
*/ |
321 |
|
|
|
322 |
|
|
uint32 TimeToMacTime(time_t t) |
323 |
|
|
{ |
324 |
|
|
// This code is taken from glibc 2.2 |
325 |
|
|
|
326 |
|
|
// Convert to number of seconds elapsed since 1-Jan-1904 |
327 |
|
|
struct tm *local = localtime(&t); |
328 |
|
|
const int TM_EPOCH_YEAR = 1900; |
329 |
|
|
const int MAC_EPOCH_YEAR = 1904; |
330 |
|
|
int a4 = ((local->tm_year + TM_EPOCH_YEAR) >> 2) - !(local->tm_year & 3); |
331 |
|
|
int b4 = (MAC_EPOCH_YEAR >> 2) - !(MAC_EPOCH_YEAR & 3); |
332 |
|
|
int a100 = a4 / 25 - (a4 % 25 < 0); |
333 |
|
|
int b100 = b4 / 25 - (b4 % 25 < 0); |
334 |
|
|
int a400 = a100 >> 2; |
335 |
|
|
int b400 = b100 >> 2; |
336 |
|
|
int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400); |
337 |
|
|
uint32 days = local->tm_yday + 365 * (local->tm_year - 4) + intervening_leap_days; |
338 |
|
|
return local->tm_sec + 60 * (local->tm_min + 60 * (local->tm_hour + 24 * days)); |
339 |
|
|
} |
340 |
gbeauche |
1.7 |
|
341 |
|
|
|
342 |
|
|
/* |
343 |
|
|
* Memory allocators in MacOS system heap zone |
344 |
|
|
*/ |
345 |
|
|
|
346 |
gbeauche |
1.10 |
uint32 Mac_sysalloc(uint32 size) |
347 |
gbeauche |
1.7 |
{ |
348 |
|
|
return NewPtrSys(size); |
349 |
|
|
} |
350 |
|
|
|
351 |
gbeauche |
1.10 |
void Mac_sysfree(uint32 addr) |
352 |
gbeauche |
1.7 |
{ |
353 |
gbeauche |
1.10 |
DisposePtr(addr); |
354 |
gbeauche |
1.7 |
} |