ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/macos_util.cpp
Revision: 1.6
Committed: 2004-01-12T15:37:18Z (20 years, 10 months ago) by cebix
Branch: MAIN
Changes since 1.5: +1 -1 lines
Log Message:
Happy New Year! :)

File Contents

# User Rev Content
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     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    
62    
63     /*
64     * Reset MacOS utilities
65     */
66    
67     void MacOSUtilReset(void)
68     {
69     cu_tvect = 0;
70     gsl_tvect = 0;
71     fs_tvect = 0;
72     cc_tvect = 0;
73     }
74    
75    
76     /*
77     * Enqueue QElem to list
78     */
79    
80     void Enqueue(uint32 elem, uint32 list)
81     {
82     WriteMacInt32(elem + qLink, 0);
83     if (!ReadMacInt32(list + qTail)) {
84     WriteMacInt32(list + qHead, elem);
85     WriteMacInt32(list + qTail, elem);
86     } else {
87     WriteMacInt32(ReadMacInt32(list + qTail) + qLink, elem);
88     WriteMacInt32(list + qTail, elem);
89     }
90     }
91    
92    
93     /*
94     * Find first free drive number, starting at num
95     */
96    
97     static bool is_drive_number_free(int num)
98     {
99     uint32 e = ReadMacInt32(0x308 + qHead);
100     while (e) {
101     uint32 d = e - dsQLink;
102     if ((int)ReadMacInt16(d + dsQDrive) == num)
103     return false;
104     e = ReadMacInt32(e + qLink);
105     }
106     return true;
107     }
108    
109     int FindFreeDriveNumber(int num)
110     {
111     while (!is_drive_number_free(num))
112     num++;
113     return num;
114     }
115    
116    
117     /*
118     * Mount volume with given file handle (call this function when you are unable to
119     * do automatic media change detection and the user has to press a special key
120     * or something to mount a volume; this function will check if there's really a
121     * volume in the drive with SysIsDiskInserted(); volumes which are present on startup
122     * are automatically mounted)
123     */
124    
125     void MountVolume(void *fh)
126     {
127     SonyMountVolume(fh) || DiskMountVolume(fh) || CDROMMountVolume(fh);
128     }
129    
130    
131     /*
132     * Calculate disk image file layout given file size and first 256 data bytes
133     */
134    
135     void FileDiskLayout(loff_t size, uint8 *data, loff_t &start_byte, loff_t &real_size)
136     {
137     if (size == 419284 || size == 838484) {
138     // 400K/800K DiskCopy image, 84 byte header
139     start_byte = 84;
140     real_size = (size - 84) & ~0x1ff;
141     } else {
142     // 0..511 byte header
143     start_byte = size & 0x1ff;
144     real_size = size - start_byte;
145     }
146     }
147    
148    
149     /*
150     * Find symbol in shared library (using CFM)
151     * lib and sym must be Pascal strings!
152     */
153    
154 gbeauche 1.4 void *FindLibSymbol(char *lib_str, char *sym_str)
155 cebix 1.1 {
156 gbeauche 1.4 SheepVar32 conn_id = 0;
157     SheepVar32 main_addr = 0;
158     SheepArray<256> err;
159     WriteMacInt8(err.addr(), 0);
160     SheepVar32 sym_addr = 0;
161     SheepVar32 sym_class = 0;
162 cebix 1.1
163 gbeauche 1.4 SheepString lib(lib_str);
164     SheepString sym(sym_str);
165    
166     D(bug("FindLibSymbol %s in %s...\n", sym.value()+1, lib.value()+1));
167 cebix 1.1
168 gbeauche 1.2 if (ReadMacInt32(XLM_RUN_MODE) == MODE_EMUL_OP) {
169 cebix 1.1 M68kRegisters r;
170    
171     // Find shared library
172 gbeauche 1.5 static const uint16 proc1[] = {
173     PW(0x558f), // subq.l #2,a7
174     PW(0x2f08), // move.l a0,-(a7)
175     PW(0x2f3c), PW(0x7077), PW(0x7063), // move.l #'pwpc',-(a7)
176     PW(0x2f3c), PW(0x0000), PW(0x0001), // move.l #kReferenceCFrag,-(a7)
177     PW(0x2f09), // move.l a1,-(a7)
178     PW(0x2f0a), // move.l a2,-(a7)
179     PW(0x2f0b), // move.l a3,-(a7)
180     PW(0x3f3c), PW(0x0001), // (GetSharedLibrary)
181     PW(0xaa5a), // CFMDispatch
182     PW(0x301f), // move.w (a7)+,d0
183     PW(M68K_RTS)
184 cebix 1.1 };
185 gbeauche 1.4 r.a[0] = lib.addr();
186     r.a[1] = conn_id.addr();
187     r.a[2] = main_addr.addr();
188     r.a[3] = err.addr();
189 cebix 1.1 Execute68k((uint32)proc1, &r);
190 gbeauche 1.4 D(bug(" GetSharedLibrary: ret %d, connection ID %ld, main %p\n", (int16)r.d[0], conn_id.value(), main_addr.value()));
191 cebix 1.1 if (r.d[0])
192     return NULL;
193    
194     // Find symbol
195 gbeauche 1.5 static const uint16 proc2[] = {
196     PW(0x558f), // subq.l #2,a7
197     PW(0x2f00), // move.l d0,-(a7)
198     PW(0x2f08), // move.l a0,-(a7)
199     PW(0x2f09), // move.l a1,-(a7)
200     PW(0x2f0a), // move.l a2,-(a7)
201     PW(0x3f3c), PW(0x0005), // (FindSymbol)
202     PW(0xaa5a), // CFMDispatch
203     PW(0x301f), // move.w (a7)+,d0
204     PW(M68K_RTS)
205 cebix 1.1 };
206 gbeauche 1.4 r.d[0] = conn_id.value();
207     r.a[0] = sym.addr();
208     r.a[1] = sym_addr.addr();
209     r.a[2] = sym_class.addr();
210 cebix 1.1 Execute68k((uint32)proc2, &r);
211 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()));
212 cebix 1.1 //!! CloseConnection()?
213     if (r.d[0])
214     return NULL;
215     else
216 gbeauche 1.4 return (void *)sym_addr.value();
217 cebix 1.1
218     } else {
219    
220     if (GetSharedLibrary == NULL || FindSymbol == NULL) {
221     printf("FATAL: FindLibSymbol() called too early\n");
222     return 0;
223     }
224     int16 res;
225 gbeauche 1.4 res = GetSharedLibrary(lib.value(), FOURCC('p','w','p','c'), 1, (uint32 *)conn_id.addr(), (void **)main_addr.addr(), (char *)err.addr());
226     D(bug(" GetSharedLibrary: ret %d, connection ID %ld, main %p\n", res, conn_id.value(), main_addr.value()));
227 cebix 1.1 if (res)
228     return NULL;
229 gbeauche 1.4 res = FindSymbol(conn_id.value(), sym.value(), (void **)sym_addr.addr(), (uint32 *)sym_class.addr());
230     D(bug(" FindSymbol: ret %d, sym_addr %p, sym_class %ld\n", res, sym_addr.value(), sym_class.value()));
231 cebix 1.1 //!!?? CloseConnection(&conn_id);
232     if (res)
233     return NULL;
234     else
235 gbeauche 1.4 return (void *)sym_addr.value();
236 cebix 1.1 }
237     }
238    
239    
240     /*
241     * Find CallUniversalProc() TVector
242     */
243    
244     void InitCallUniversalProc()
245     {
246     cu_tvect = (uint32)FindLibSymbol("\014InterfaceLib", "\021CallUniversalProc");
247     D(bug("CallUniversalProc TVECT at %08lx\n", cu_tvect));
248     if (cu_tvect == 0) {
249     printf("FATAL: Can't find CallUniversalProc()\n");
250     QuitEmulator();
251     }
252    
253     gsl_tvect = (uint32)FindLibSymbol("\014InterfaceLib", "\020GetSharedLibrary");
254     D(bug("GetSharedLibrary TVECT at %08lx\n", gsl_tvect));
255     if (gsl_tvect == 0) {
256     printf("FATAL: Can't find GetSharedLibrary()\n");
257     QuitEmulator();
258     }
259    
260     fs_tvect = (uint32)FindLibSymbol("\014InterfaceLib", "\012FindSymbol");
261     D(bug("FindSymbol TVECT at %08lx\n", fs_tvect));
262     if (fs_tvect == 0) {
263     printf("FATAL: Can't find FindSymbol()\n");
264     QuitEmulator();
265     }
266    
267     cc_tvect = (uint32)FindLibSymbol("\014InterfaceLib", "\017CloseConnection");
268     D(bug("CloseConnection TVECT at %08lx\n", cc_tvect));
269     if (cc_tvect == 0) {
270     printf("FATAL: Can't find CloseConnection()\n");
271     QuitEmulator();
272     }
273     }
274    
275    
276     /*
277     * CallUniversalProc
278     */
279    
280     long CallUniversalProc(void *upp, uint32 info)
281     {
282     if (cu_tvect == 0) {
283     printf("FATAL: CallUniversalProc() called too early\n");
284     return 0;
285     }
286     return CallUniversal(upp, info);
287     }
288    
289    
290     /*
291     * Convert time_t value to MacOS time (seconds since 1.1.1904)
292     */
293    
294     uint32 TimeToMacTime(time_t t)
295     {
296     // This code is taken from glibc 2.2
297    
298     // Convert to number of seconds elapsed since 1-Jan-1904
299     struct tm *local = localtime(&t);
300     const int TM_EPOCH_YEAR = 1900;
301     const int MAC_EPOCH_YEAR = 1904;
302     int a4 = ((local->tm_year + TM_EPOCH_YEAR) >> 2) - !(local->tm_year & 3);
303     int b4 = (MAC_EPOCH_YEAR >> 2) - !(MAC_EPOCH_YEAR & 3);
304     int a100 = a4 / 25 - (a4 % 25 < 0);
305     int b100 = b4 / 25 - (b4 % 25 < 0);
306     int a400 = a100 >> 2;
307     int b400 = b100 >> 2;
308     int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400);
309     uint32 days = local->tm_yday + 365 * (local->tm_year - 4) + intervening_leap_days;
310     return local->tm_sec + 60 * (local->tm_min + 60 * (local->tm_hour + 24 * days));
311     }