1 |
/* |
2 |
* macos_util.cpp - MacOS definitions/utility functions |
3 |
* |
4 |
* SheepShaver (C) 1997-2004 Christian Bauer and Marc Hellwig |
5 |
* |
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 |
#include "thunks.h" |
31 |
|
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 |
static inline int16 GetSharedLibrary(char *arg1, uint32 arg2, uint32 arg3, uint32 *arg4, void **arg5, char *arg6) |
46 |
{ |
47 |
return (int16)CallMacOS6(gsl_ptr, gsl_tvect, arg1, arg2, arg3, arg4, arg5, arg6); |
48 |
} |
49 |
typedef int16 (*fs_ptr)(uint32, char *, void **, uint32 *); |
50 |
static uint32 fs_tvect = 0; |
51 |
static inline int16 FindSymbol(uint32 arg1, char *arg2, void **arg3, uint32 *arg4) |
52 |
{ |
53 |
return (int16)CallMacOS4(fs_ptr, fs_tvect, arg1, arg2, arg3, arg4); |
54 |
} |
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 |
typedef void *(*nps_ptr)(uint32); |
62 |
static uint32 nps_tvect = 0; |
63 |
static inline void *NewPtrSys(uint32 arg1) |
64 |
{ |
65 |
return (void *)CallMacOS1(nps_ptr, nps_tvect, arg1); |
66 |
} |
67 |
typedef void (*d_ptr)(void *); |
68 |
static uint32 d_tvect = 0; |
69 |
static inline void DisposePtr(void *arg1) |
70 |
{ |
71 |
CallMacOS1(d_ptr, d_tvect, arg1); |
72 |
} |
73 |
|
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 |
void *FindLibSymbol(char *lib_str, char *sym_str) |
167 |
{ |
168 |
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 |
|
175 |
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 |
|
180 |
if (ReadMacInt32(XLM_RUN_MODE) == MODE_EMUL_OP) { |
181 |
M68kRegisters r; |
182 |
|
183 |
// Find shared library |
184 |
static const uint16 proc1[] = { |
185 |
PW(0x558f), // subq.l #2,a7 |
186 |
PW(0x2f08), // move.l a0,-(a7) |
187 |
PW(0x2f3c), PW(0x7077), PW(0x7063), // move.l #'pwpc',-(a7) |
188 |
PW(0x2f3c), PW(0x0000), PW(0x0001), // move.l #kReferenceCFrag,-(a7) |
189 |
PW(0x2f09), // move.l a1,-(a7) |
190 |
PW(0x2f0a), // move.l a2,-(a7) |
191 |
PW(0x2f0b), // move.l a3,-(a7) |
192 |
PW(0x3f3c), PW(0x0001), // (GetSharedLibrary) |
193 |
PW(0xaa5a), // CFMDispatch |
194 |
PW(0x301f), // move.w (a7)+,d0 |
195 |
PW(M68K_RTS) |
196 |
}; |
197 |
r.a[0] = lib.addr(); |
198 |
r.a[1] = conn_id.addr(); |
199 |
r.a[2] = main_addr.addr(); |
200 |
r.a[3] = err.addr(); |
201 |
Execute68k((uint32)proc1, &r); |
202 |
D(bug(" GetSharedLibrary: ret %d, connection ID %ld, main %p\n", (int16)r.d[0], conn_id.value(), main_addr.value())); |
203 |
if (r.d[0]) |
204 |
return NULL; |
205 |
|
206 |
// Find symbol |
207 |
static const uint16 proc2[] = { |
208 |
PW(0x558f), // subq.l #2,a7 |
209 |
PW(0x2f00), // move.l d0,-(a7) |
210 |
PW(0x2f08), // move.l a0,-(a7) |
211 |
PW(0x2f09), // move.l a1,-(a7) |
212 |
PW(0x2f0a), // move.l a2,-(a7) |
213 |
PW(0x3f3c), PW(0x0005), // (FindSymbol) |
214 |
PW(0xaa5a), // CFMDispatch |
215 |
PW(0x301f), // move.w (a7)+,d0 |
216 |
PW(M68K_RTS) |
217 |
}; |
218 |
r.d[0] = conn_id.value(); |
219 |
r.a[0] = sym.addr(); |
220 |
r.a[1] = sym_addr.addr(); |
221 |
r.a[2] = sym_class.addr(); |
222 |
Execute68k((uint32)proc2, &r); |
223 |
D(bug(" FindSymbol1: ret %d, sym_addr %p, sym_class %ld\n", (int16)r.d[0], sym_addr.value(), sym_class.value())); |
224 |
//!! CloseConnection()? |
225 |
if (r.d[0]) |
226 |
return NULL; |
227 |
else |
228 |
return (void *)sym_addr.value(); |
229 |
|
230 |
} else { |
231 |
|
232 |
if (GetSharedLibrary == NULL || FindSymbol == NULL) { |
233 |
printf("FATAL: FindLibSymbol() called too early\n"); |
234 |
return 0; |
235 |
} |
236 |
int16 res; |
237 |
res = GetSharedLibrary(lib.value(), FOURCC('p','w','p','c'), 1, (uint32 *)conn_id.addr(), (void **)main_addr.addr(), (char *)err.addr()); |
238 |
D(bug(" GetSharedLibrary: ret %d, connection ID %ld, main %p\n", res, conn_id.value(), main_addr.value())); |
239 |
if (res) |
240 |
return NULL; |
241 |
res = FindSymbol(conn_id.value(), sym.value(), (void **)sym_addr.addr(), (uint32 *)sym_class.addr()); |
242 |
D(bug(" FindSymbol: ret %d, sym_addr %p, sym_class %ld\n", res, sym_addr.value(), sym_class.value())); |
243 |
//!!?? CloseConnection(&conn_id); |
244 |
if (res) |
245 |
return NULL; |
246 |
else |
247 |
return (void *)sym_addr.value(); |
248 |
} |
249 |
} |
250 |
|
251 |
|
252 |
/* |
253 |
* Find CallUniversalProc() TVector |
254 |
*/ |
255 |
|
256 |
void InitCallUniversalProc() |
257 |
{ |
258 |
cu_tvect = (uint32)FindLibSymbol("\014InterfaceLib", "\021CallUniversalProc"); |
259 |
D(bug("CallUniversalProc TVECT at %08lx\n", cu_tvect)); |
260 |
if (cu_tvect == 0) { |
261 |
printf("FATAL: Can't find CallUniversalProc()\n"); |
262 |
QuitEmulator(); |
263 |
} |
264 |
|
265 |
gsl_tvect = (uint32)FindLibSymbol("\014InterfaceLib", "\020GetSharedLibrary"); |
266 |
D(bug("GetSharedLibrary TVECT at %08lx\n", gsl_tvect)); |
267 |
if (gsl_tvect == 0) { |
268 |
printf("FATAL: Can't find GetSharedLibrary()\n"); |
269 |
QuitEmulator(); |
270 |
} |
271 |
|
272 |
fs_tvect = (uint32)FindLibSymbol("\014InterfaceLib", "\012FindSymbol"); |
273 |
D(bug("FindSymbol TVECT at %08lx\n", fs_tvect)); |
274 |
if (fs_tvect == 0) { |
275 |
printf("FATAL: Can't find FindSymbol()\n"); |
276 |
QuitEmulator(); |
277 |
} |
278 |
|
279 |
cc_tvect = (uint32)FindLibSymbol("\014InterfaceLib", "\017CloseConnection"); |
280 |
D(bug("CloseConnection TVECT at %08lx\n", cc_tvect)); |
281 |
if (cc_tvect == 0) { |
282 |
printf("FATAL: Can't find CloseConnection()\n"); |
283 |
QuitEmulator(); |
284 |
} |
285 |
|
286 |
nps_tvect = (uint32)FindLibSymbol("\014InterfaceLib", "\011NewPtrSys"); |
287 |
D(bug("NewPtrSys TVECT at %08lx\n", nps_tvect)); |
288 |
if (nps_tvect == 0) { |
289 |
printf("FATAL: Can't find NewPtrSys()\n"); |
290 |
QuitEmulator(); |
291 |
} |
292 |
|
293 |
d_tvect = (uint32)FindLibSymbol("\014InterfaceLib", "\012DisposePtr"); |
294 |
D(bug("DisposePtr TVECT at %08lx\n", d_tvect)); |
295 |
if (d_tvect == 0) { |
296 |
printf("FATAL: Can't find DisposePtr()\n"); |
297 |
QuitEmulator(); |
298 |
} |
299 |
} |
300 |
|
301 |
|
302 |
/* |
303 |
* CallUniversalProc |
304 |
*/ |
305 |
|
306 |
long CallUniversalProc(void *upp, uint32 info) |
307 |
{ |
308 |
if (cu_tvect == 0) { |
309 |
printf("FATAL: CallUniversalProc() called too early\n"); |
310 |
return 0; |
311 |
} |
312 |
return CallUniversal(upp, info); |
313 |
} |
314 |
|
315 |
|
316 |
/* |
317 |
* Convert time_t value to MacOS time (seconds since 1.1.1904) |
318 |
*/ |
319 |
|
320 |
uint32 TimeToMacTime(time_t t) |
321 |
{ |
322 |
// This code is taken from glibc 2.2 |
323 |
|
324 |
// Convert to number of seconds elapsed since 1-Jan-1904 |
325 |
struct tm *local = localtime(&t); |
326 |
const int TM_EPOCH_YEAR = 1900; |
327 |
const int MAC_EPOCH_YEAR = 1904; |
328 |
int a4 = ((local->tm_year + TM_EPOCH_YEAR) >> 2) - !(local->tm_year & 3); |
329 |
int b4 = (MAC_EPOCH_YEAR >> 2) - !(MAC_EPOCH_YEAR & 3); |
330 |
int a100 = a4 / 25 - (a4 % 25 < 0); |
331 |
int b100 = b4 / 25 - (b4 % 25 < 0); |
332 |
int a400 = a100 >> 2; |
333 |
int b400 = b100 >> 2; |
334 |
int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400); |
335 |
uint32 days = local->tm_yday + 365 * (local->tm_year - 4) + intervening_leap_days; |
336 |
return local->tm_sec + 60 * (local->tm_min + 60 * (local->tm_hour + 24 * days)); |
337 |
} |
338 |
|
339 |
|
340 |
/* |
341 |
* Memory allocators in MacOS system heap zone |
342 |
*/ |
343 |
|
344 |
void *Mac_sysalloc(uint32 size) |
345 |
{ |
346 |
return NewPtrSys(size); |
347 |
} |
348 |
|
349 |
void Mac_sysfree(void *p) |
350 |
{ |
351 |
DisposePtr(p); |
352 |
} |