1 |
/* |
2 |
* macos_util.cpp - MacOS definitions/utility functions |
3 |
* |
4 |
* Basilisk II (C) 1997-2008 Christian Bauer |
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 "adb.h" |
24 |
#include "main.h" |
25 |
#include "sony.h" |
26 |
#include "disk.h" |
27 |
#include "cdrom.h" |
28 |
#include "macos_util.h" |
29 |
|
30 |
#define DEBUG 0 |
31 |
#include "debug.h" |
32 |
|
33 |
|
34 |
/* |
35 |
* Enqueue QElem to list |
36 |
*/ |
37 |
|
38 |
void EnqueueMac(uint32 elem, uint32 list) |
39 |
{ |
40 |
WriteMacInt32(elem + qLink, 0); |
41 |
if (!ReadMacInt32(list + qTail)) { |
42 |
WriteMacInt32(list + qHead, elem); |
43 |
WriteMacInt32(list + qTail, elem); |
44 |
} else { |
45 |
WriteMacInt32(ReadMacInt32(list + qTail) + qLink, elem); |
46 |
WriteMacInt32(list + qTail, elem); |
47 |
} |
48 |
} |
49 |
|
50 |
|
51 |
/* |
52 |
* Find first free drive number, starting at num |
53 |
*/ |
54 |
|
55 |
static bool is_drive_number_free(int num) |
56 |
{ |
57 |
uint32 e = ReadMacInt32(0x308 + qHead); |
58 |
while (e) { |
59 |
uint32 d = e - dsQLink; |
60 |
if ((int)ReadMacInt16(d + dsQDrive) == num) |
61 |
return false; |
62 |
e = ReadMacInt32(e + qLink); |
63 |
} |
64 |
return true; |
65 |
} |
66 |
|
67 |
int FindFreeDriveNumber(int num) |
68 |
{ |
69 |
while (!is_drive_number_free(num)) |
70 |
num++; |
71 |
return num; |
72 |
} |
73 |
|
74 |
|
75 |
/* |
76 |
* Mount volume with given file handle (call this function when you are unable to |
77 |
* do automatic media change detection and the user has to press a special key |
78 |
* or something to mount a volume; this function will check if there's really a |
79 |
* volume in the drive with SysIsDiskInserted(); volumes which are present on startup |
80 |
* are automatically mounted) |
81 |
*/ |
82 |
|
83 |
void MountVolume(void *fh) |
84 |
{ |
85 |
SonyMountVolume(fh) || DiskMountVolume(fh) || CDROMMountVolume(fh); |
86 |
} |
87 |
|
88 |
|
89 |
/* |
90 |
* Calculate disk image file layout given file size and first 256 data bytes |
91 |
*/ |
92 |
|
93 |
void FileDiskLayout(loff_t size, uint8 *data, loff_t &start_byte, loff_t &real_size) |
94 |
{ |
95 |
if (size == 419284 || size == 838484) { |
96 |
// 400K/800K DiskCopy image, 84 byte header |
97 |
start_byte = 84; |
98 |
real_size = (size - 84) & ~0x1ff; |
99 |
} else { |
100 |
// 0..511 byte header |
101 |
start_byte = size & 0x1ff; |
102 |
real_size = size - start_byte; |
103 |
} |
104 |
} |
105 |
|
106 |
|
107 |
uint32 DebugUtil(uint32 Selector) |
108 |
{ |
109 |
switch (Selector) { |
110 |
case duDebuggerGetMax: |
111 |
return 3; |
112 |
case duDebuggerEnter: |
113 |
return 0; |
114 |
case duDebuggerExit: |
115 |
return 0; |
116 |
case duDebuggerPoll: |
117 |
ADBInterrupt(); |
118 |
return 0; |
119 |
default: |
120 |
return (uint32) paramErr; |
121 |
} |
122 |
} |
123 |
|
124 |
|
125 |
/* |
126 |
* Convert time_t value to MacOS time (seconds since 1.1.1904) |
127 |
*/ |
128 |
|
129 |
uint32 TimeToMacTime(time_t t) |
130 |
{ |
131 |
// This code is taken from glibc 2.2 |
132 |
|
133 |
// Convert to number of seconds elapsed since 1-Jan-1904 |
134 |
struct tm *local = localtime(&t); |
135 |
const int TM_EPOCH_YEAR = 1900; |
136 |
const int MAC_EPOCH_YEAR = 1904; |
137 |
int a4 = ((local->tm_year + TM_EPOCH_YEAR) >> 2) - !(local->tm_year & 3); |
138 |
int b4 = (MAC_EPOCH_YEAR >> 2) - !(MAC_EPOCH_YEAR & 3); |
139 |
int a100 = a4 / 25 - (a4 % 25 < 0); |
140 |
int b100 = b4 / 25 - (b4 % 25 < 0); |
141 |
int a400 = a100 >> 2; |
142 |
int b400 = b100 >> 2; |
143 |
int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400); |
144 |
uint32 days = local->tm_yday + 365 * (local->tm_year - 4) + intervening_leap_days; |
145 |
return local->tm_sec + 60 * (local->tm_min + 60 * (local->tm_hour + 24 * days)); |
146 |
} |