| 1 |
/* |
/* |
| 2 |
* macos_util.cpp - MacOS definitions/utility functions |
* macos_util.cpp - MacOS definitions/utility functions |
| 3 |
* |
* |
| 4 |
* SheepShaver (C) 1997-2002 Christian Bauer and Marc Hellwig |
* SheepShaver (C) 1997-2005 Christian Bauer and Marc Hellwig |
| 5 |
* |
* |
| 6 |
* This program is free software; you can redistribute it and/or modify |
* 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 |
* it under the terms of the GNU General Public License as published by |
| 27 |
#include "xlowmem.h" |
#include "xlowmem.h" |
| 28 |
#include "emul_op.h" |
#include "emul_op.h" |
| 29 |
#include "macos_util.h" |
#include "macos_util.h" |
| 30 |
|
#include "thunks.h" |
| 31 |
|
|
| 32 |
#define DEBUG 0 |
#define DEBUG 0 |
| 33 |
#include "debug.h" |
#include "debug.h" |
| 42 |
} |
} |
| 43 |
typedef int16 (*gsl_ptr)(char *, uint32, uint32, uint32 *, void **, char *); |
typedef int16 (*gsl_ptr)(char *, uint32, uint32, uint32 *, void **, char *); |
| 44 |
static uint32 gsl_tvect = 0; |
static uint32 gsl_tvect = 0; |
| 45 |
static inline int16 GetSharedLibrary(char *arg1, uint32 arg2, uint32 arg3, uint32 *arg4, void **arg5, char *arg6) |
static inline int16 GetSharedLibrary(uintptr arg1, uint32 arg2, uint32 arg3, uintptr arg4, uintptr arg5, uintptr arg6) |
| 46 |
{ |
{ |
| 47 |
return (int16)CallMacOS6(gsl_ptr, gsl_tvect, arg1, arg2, arg3, arg4, arg5, arg6); |
return (int16)CallMacOS6(gsl_ptr, gsl_tvect, (char *)arg1, arg2, arg3, (uint32 *)arg4, (void **)arg5, (char *)arg6); |
| 48 |
} |
} |
| 49 |
typedef int16 (*fs_ptr)(uint32, char *, void **, uint32 *); |
typedef int16 (*fs_ptr)(uint32, char *, void **, uint32 *); |
| 50 |
static uint32 fs_tvect = 0; |
static uint32 fs_tvect = 0; |
| 51 |
static inline int16 FindSymbol(uint32 arg1, char *arg2, void **arg3, uint32 *arg4) |
static inline int16 FindSymbol(uint32 arg1, uintptr arg2, uintptr arg3, uintptr arg4) |
| 52 |
{ |
{ |
| 53 |
return (int16)CallMacOS4(fs_ptr, fs_tvect, arg1, arg2, arg3, arg4); |
return (int16)CallMacOS4(fs_ptr, fs_tvect, arg1, (char *)arg2, (void **)arg3, (uint32 **)arg4); |
| 54 |
} |
} |
| 55 |
typedef int16 (*cc_ptr)(uint32 *); |
typedef int16 (*cc_ptr)(uint32 *); |
| 56 |
static uint32 cc_tvect = 0; |
static uint32 cc_tvect = 0; |
| 58 |
{ |
{ |
| 59 |
return (int16)CallMacOS1(cc_ptr, cc_tvect, arg1); |
return (int16)CallMacOS1(cc_ptr, cc_tvect, arg1); |
| 60 |
} |
} |
| 61 |
|
typedef uint32 (*nps_ptr)(uint32); |
| 62 |
|
static uint32 nps_tvect = 0; |
| 63 |
|
static inline uint32 NewPtrSys(uint32 arg1) |
| 64 |
|
{ |
| 65 |
|
return CallMacOS1(nps_ptr, nps_tvect, arg1); |
| 66 |
|
} |
| 67 |
|
typedef void (*d_ptr)(uint32); |
| 68 |
|
static uint32 d_tvect = 0; |
| 69 |
|
static inline void DisposePtr(uint32 arg1) |
| 70 |
|
{ |
| 71 |
|
CallMacOS1(d_ptr, d_tvect, arg1); |
| 72 |
|
} |
| 73 |
|
|
| 74 |
|
|
| 75 |
/* |
/* |
| 163 |
* lib and sym must be Pascal strings! |
* lib and sym must be Pascal strings! |
| 164 |
*/ |
*/ |
| 165 |
|
|
| 166 |
void *FindLibSymbol(char *lib, char *sym) |
uint32 FindLibSymbol(char *lib_str, char *sym_str) |
| 167 |
{ |
{ |
| 168 |
uint32 conn_id = 0; |
SheepVar32 conn_id = 0; |
| 169 |
void *main_addr = NULL; |
SheepVar32 main_addr = 0; |
| 170 |
char err[256] = ""; |
SheepArray<256> err; |
| 171 |
uint32 *sym_addr = NULL; |
WriteMacInt8(err.addr(), 0); |
| 172 |
uint32 sym_class = 0; |
SheepVar32 sym_addr = 0; |
| 173 |
|
SheepVar32 sym_class = 0; |
| 174 |
|
|
| 175 |
D(bug("FindLibSymbol %s in %s...\n", sym+1, lib+1)); |
SheepString lib(lib_str); |
| 176 |
|
SheepString sym(sym_str); |
| 177 |
|
|
| 178 |
if (*(uint32 *)XLM_RUN_MODE == MODE_EMUL_OP) { |
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; |
M68kRegisters r; |
| 182 |
|
|
| 183 |
// Find shared library |
// Find shared library |
| 184 |
static const uint16 proc1[] = { |
static const uint8 proc1_template[] = { |
| 185 |
0x558f, // subq.l #2,a7 |
0x55, 0x8f, // subq.l #2,a7 |
| 186 |
0x2f08, // move.l a0,-(a7) |
0x2f, 0x08, // move.l a0,-(a7) |
| 187 |
0x2f3c, 0x7077, 0x7063, // move.l #'pwpc',-(a7) |
0x2f, 0x3c, 0x70, 0x77, 0x70, 0x63, // move.l #'pwpc',-(a7) |
| 188 |
0x2f3c, 0, 1, // move.l #kReferenceCFrag,-(a7) |
0x2f, 0x3c, 0x00, 0x00, 0x00, 0x01, // move.l #kReferenceCFrag,-(a7) |
| 189 |
0x2f09, // move.l a1,-(a7) |
0x2f, 0x09, // move.l a1,-(a7) |
| 190 |
0x2f0a, // move.l a2,-(a7) |
0x2f, 0x0a, // move.l a2,-(a7) |
| 191 |
0x2f0b, // move.l a3,-(a7) |
0x2f, 0x0b, // move.l a3,-(a7) |
| 192 |
0x3f3c, 1, // (GetSharedLibrary) |
0x3f, 0x3c, 0x00, 0x01, // (GetSharedLibrary) |
| 193 |
0xaa5a, // CFMDispatch |
0xaa, 0x5a, // CFMDispatch |
| 194 |
0x301f, // move.w (a7)+,d0 |
0x30, 0x1f, // move.w (a7)+,d0 |
| 195 |
M68K_RTS |
M68K_RTS >> 8, M68K_RTS |
| 196 |
}; |
}; |
| 197 |
r.a[0] = (uint32)lib; |
BUILD_SHEEPSHAVER_PROCEDURE(proc1); |
| 198 |
r.a[1] = (uint32)&conn_id; |
r.a[0] = lib.addr(); |
| 199 |
r.a[2] = (uint32)&main_addr; |
r.a[1] = conn_id.addr(); |
| 200 |
r.a[3] = (uint32)err; |
r.a[2] = main_addr.addr(); |
| 201 |
Execute68k((uint32)proc1, &r); |
r.a[3] = err.addr(); |
| 202 |
D(bug(" GetSharedLibrary: ret %d, connection ID %ld, main %p\n", (int16)r.d[0], conn_id, main_addr)); |
Execute68k(proc1, &r); |
| 203 |
|
D(bug(" GetSharedLibrary: ret %d, connection ID %ld, main %p\n", (int16)r.d[0], conn_id.value(), main_addr.value())); |
| 204 |
if (r.d[0]) |
if (r.d[0]) |
| 205 |
return NULL; |
return 0; |
| 206 |
|
|
| 207 |
// Find symbol |
// Find symbol |
| 208 |
static const uint16 proc2[] = { |
static const uint8 proc2_template[] = { |
| 209 |
0x558f, // subq.l #2,a7 |
0x55, 0x8f, // subq.l #2,a7 |
| 210 |
0x2f00, // move.l d0,-(a7) |
0x2f, 0x00, // move.l d0,-(a7) |
| 211 |
0x2f08, // move.l a0,-(a7) |
0x2f, 0x08, // move.l a0,-(a7) |
| 212 |
0x2f09, // move.l a1,-(a7) |
0x2f, 0x09, // move.l a1,-(a7) |
| 213 |
0x2f0a, // move.l a2,-(a7) |
0x2f, 0x0a, // move.l a2,-(a7) |
| 214 |
0x3f3c, 5, // (FindSymbol) |
0x3f, 0x3c, 0x00, 0x05, // (FindSymbol) |
| 215 |
0xaa5a, // CFMDispatch |
0xaa, 0x5a, // CFMDispatch |
| 216 |
0x301f, // move.w (a7)+,d0 |
0x30, 0x1f, // move.w (a7)+,d0 |
| 217 |
M68K_RTS |
M68K_RTS >> 8, M68K_RTS |
| 218 |
}; |
}; |
| 219 |
r.d[0] = conn_id; |
BUILD_SHEEPSHAVER_PROCEDURE(proc2); |
| 220 |
r.a[0] = (uint32)sym; |
r.d[0] = conn_id.value(); |
| 221 |
r.a[1] = (uint32)&sym_addr; |
r.a[0] = sym.addr(); |
| 222 |
r.a[2] = (uint32)&sym_class; |
r.a[1] = sym_addr.addr(); |
| 223 |
Execute68k((uint32)proc2, &r); |
r.a[2] = sym_class.addr(); |
| 224 |
D(bug(" FindSymbol: ret %d, sym_addr %p, sym_class %ld\n", (int16)r.d[0], sym_addr, sym_class)); |
Execute68k(proc2, &r); |
| 225 |
|
D(bug(" FindSymbol1: ret %d, sym_addr %p, sym_class %ld\n", (int16)r.d[0], sym_addr.value(), sym_class.value())); |
| 226 |
//!! CloseConnection()? |
//!! CloseConnection()? |
| 227 |
if (r.d[0]) |
if (r.d[0]) |
| 228 |
return NULL; |
return 0; |
| 229 |
else |
else |
| 230 |
return sym_addr; |
return sym_addr.value(); |
| 231 |
|
|
| 232 |
} else { |
} else { |
| 233 |
|
|
| 236 |
return 0; |
return 0; |
| 237 |
} |
} |
| 238 |
int16 res; |
int16 res; |
| 239 |
res = GetSharedLibrary(lib, FOURCC('p','w','p','c'), 1, &conn_id, &main_addr, err); |
res = GetSharedLibrary(lib.addr(), FOURCC('p','w','p','c'), 1, conn_id.addr(), main_addr.addr(), err.addr()); |
| 240 |
D(bug(" GetSharedLibrary: ret %d, connection ID %ld, main %p\n", res, conn_id, main_addr)); |
D(bug(" GetSharedLibrary: ret %d, connection ID %ld, main %p\n", res, conn_id.value(), main_addr.value())); |
| 241 |
if (res) |
if (res) |
| 242 |
return NULL; |
return 0; |
| 243 |
res = FindSymbol(conn_id, sym, (void **)&sym_addr, &sym_class); |
res = FindSymbol(conn_id.value(), sym.addr(), sym_addr.addr(), sym_class.addr()); |
| 244 |
D(bug(" FindSymbol: ret %d, sym_addr %p, sym_class %ld\n", res, sym_addr, sym_class)); |
D(bug(" FindSymbol: ret %d, sym_addr %p, sym_class %ld\n", res, sym_addr.value(), sym_class.value())); |
| 245 |
//!!?? CloseConnection(&conn_id); |
//!!?? CloseConnection(&conn_id); |
| 246 |
if (res) |
if (res) |
| 247 |
return NULL; |
return 0; |
| 248 |
else |
else |
| 249 |
return sym_addr; |
return sym_addr.value(); |
| 250 |
} |
} |
| 251 |
} |
} |
| 252 |
|
|
| 257 |
|
|
| 258 |
void InitCallUniversalProc() |
void InitCallUniversalProc() |
| 259 |
{ |
{ |
| 260 |
cu_tvect = (uint32)FindLibSymbol("\014InterfaceLib", "\021CallUniversalProc"); |
cu_tvect = FindLibSymbol("\014InterfaceLib", "\021CallUniversalProc"); |
| 261 |
D(bug("CallUniversalProc TVECT at %08lx\n", cu_tvect)); |
D(bug("CallUniversalProc TVECT at %08lx\n", cu_tvect)); |
| 262 |
if (cu_tvect == 0) { |
if (cu_tvect == 0) { |
| 263 |
printf("FATAL: Can't find CallUniversalProc()\n"); |
printf("FATAL: Can't find CallUniversalProc()\n"); |
| 264 |
QuitEmulator(); |
QuitEmulator(); |
| 265 |
} |
} |
| 266 |
|
|
| 267 |
gsl_tvect = (uint32)FindLibSymbol("\014InterfaceLib", "\020GetSharedLibrary"); |
gsl_tvect = FindLibSymbol("\014InterfaceLib", "\020GetSharedLibrary"); |
| 268 |
D(bug("GetSharedLibrary TVECT at %08lx\n", gsl_tvect)); |
D(bug("GetSharedLibrary TVECT at %08lx\n", gsl_tvect)); |
| 269 |
if (gsl_tvect == 0) { |
if (gsl_tvect == 0) { |
| 270 |
printf("FATAL: Can't find GetSharedLibrary()\n"); |
printf("FATAL: Can't find GetSharedLibrary()\n"); |
| 271 |
QuitEmulator(); |
QuitEmulator(); |
| 272 |
} |
} |
| 273 |
|
|
| 274 |
fs_tvect = (uint32)FindLibSymbol("\014InterfaceLib", "\012FindSymbol"); |
fs_tvect = FindLibSymbol("\014InterfaceLib", "\012FindSymbol"); |
| 275 |
D(bug("FindSymbol TVECT at %08lx\n", fs_tvect)); |
D(bug("FindSymbol TVECT at %08lx\n", fs_tvect)); |
| 276 |
if (fs_tvect == 0) { |
if (fs_tvect == 0) { |
| 277 |
printf("FATAL: Can't find FindSymbol()\n"); |
printf("FATAL: Can't find FindSymbol()\n"); |
| 278 |
QuitEmulator(); |
QuitEmulator(); |
| 279 |
} |
} |
| 280 |
|
|
| 281 |
cc_tvect = (uint32)FindLibSymbol("\014InterfaceLib", "\017CloseConnection"); |
cc_tvect = FindLibSymbol("\014InterfaceLib", "\017CloseConnection"); |
| 282 |
D(bug("CloseConnection TVECT at %08lx\n", cc_tvect)); |
D(bug("CloseConnection TVECT at %08lx\n", cc_tvect)); |
| 283 |
if (cc_tvect == 0) { |
if (cc_tvect == 0) { |
| 284 |
printf("FATAL: Can't find CloseConnection()\n"); |
printf("FATAL: Can't find CloseConnection()\n"); |
| 285 |
QuitEmulator(); |
QuitEmulator(); |
| 286 |
} |
} |
| 287 |
|
|
| 288 |
|
nps_tvect = FindLibSymbol("\014InterfaceLib", "\011NewPtrSys"); |
| 289 |
|
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 |
|
d_tvect = FindLibSymbol("\014InterfaceLib", "\012DisposePtr"); |
| 296 |
|
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 |
} |
} |
| 302 |
|
|
| 303 |
|
|
| 337 |
uint32 days = local->tm_yday + 365 * (local->tm_year - 4) + intervening_leap_days; |
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)); |
return local->tm_sec + 60 * (local->tm_min + 60 * (local->tm_hour + 24 * days)); |
| 339 |
} |
} |
| 340 |
|
|
| 341 |
|
|
| 342 |
|
/* |
| 343 |
|
* Memory allocators in MacOS system heap zone |
| 344 |
|
*/ |
| 345 |
|
|
| 346 |
|
uint32 Mac_sysalloc(uint32 size) |
| 347 |
|
{ |
| 348 |
|
return NewPtrSys(size); |
| 349 |
|
} |
| 350 |
|
|
| 351 |
|
void Mac_sysfree(uint32 addr) |
| 352 |
|
{ |
| 353 |
|
DisposePtr(addr); |
| 354 |
|
} |