ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/rsrc_patches.cpp
Revision: 1.16
Committed: 2008-01-01T09:40:31Z (16 years, 10 months ago) by gbeauche
Branch: MAIN
CVS Tags: HEAD
Changes since 1.15: +1 -1 lines
Log Message:
Happy New Year!

File Contents

# Content
1 /*
2 * rsrc_patches.cpp - Resource patches
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 <string.h>
22
23 #include "sysdeps.h"
24 #include "cpu_emulation.h"
25 #include "macos_util.h"
26 #include "main.h"
27 #include "prefs.h"
28 #include "emul_op.h"
29 #include "audio.h"
30 #include "audio_defs.h"
31 #include "rsrc_patches.h"
32
33 #if ENABLE_MON
34 #include "mon.h"
35 #endif
36
37 #define DEBUG 0
38 #include "debug.h"
39
40
41 /*
42 * Search resource for byte string, return offset (or 0)
43 */
44
45 static uint32 find_rsrc_data(const uint8 *rsrc, uint32 max, const uint8 *search, uint32 search_len, uint32 ofs = 0)
46 {
47 while (ofs < max - search_len) {
48 if (!memcmp(rsrc + ofs, search, search_len))
49 return ofs;
50 ofs++;
51 }
52 return 0;
53 }
54
55
56 /*
57 * Install SynchIdleTime() patch
58 */
59
60 static void patch_idle_time(uint8 *p, uint32 size, int n = 1)
61 {
62 if (!PrefsFindBool("idlewait"))
63 return;
64
65 static const uint8 dat[] = {0x70, 0x03, 0xa0, 0x9f};
66 uint32 base = find_rsrc_data(p, size, dat, sizeof(dat));
67 if (base) {
68 uint8 *pbase = p + base - 0x80;
69 static const uint8 dat2[] = {0x20, 0x78, 0x02, 0xb6, 0x41, 0xe8, 0x00, 0x80};
70 base = find_rsrc_data(pbase, 0x80, dat2, sizeof(dat2));
71 if (base) {
72 uint16 *p16 = (uint16 *)(pbase + base);
73 *p16++ = htons(M68K_EMUL_OP_IDLE_TIME);
74 *p16 = htons(M68K_NOP);
75 FlushCodeCache(pbase + base, 4);
76 D(bug(" patch %d applied\n", n));
77 }
78 }
79 }
80
81
82 /*
83 * Resource patches via vCheckLoad
84 */
85
86 void CheckLoad(uint32 type, int16 id, uint8 *p, uint32 size)
87 {
88 uint16 *p16;
89 uint32 base;
90 D(bug("vCheckLoad %c%c%c%c (%08x) ID %d, data %p, size %d\n", (char)(type >> 24), (char)((type >> 16) & 0xff), (char )((type >> 8) & 0xff), (char )(type & 0xff), type, id, p, size));
91
92 if (type == FOURCC('b','o','o','t') && id == 3) {
93 D(bug(" boot 3 found\n"));
94
95 // Set boot stack pointer (7.5, 7.6, 7.6.1, 8.0)
96 static const uint8 dat[] = {0x22, 0x00, 0xe4, 0x89, 0x90, 0x81, 0x22, 0x40};
97 base = find_rsrc_data(p, size, dat, sizeof(dat));
98 if (base) {
99 p16 = (uint16 *)(p + base + 6);
100 *p16 = htons(M68K_EMUL_OP_FIX_BOOTSTACK);
101 FlushCodeCache(p + base + 6, 2);
102 D(bug(" patch 1 applied\n"));
103 }
104
105 #if !ROM_IS_WRITE_PROTECTED
106 // Set fake handle at 0x0000 to some safe place (so broken Mac programs won't write into Mac ROM) (7.1, 7.5, 8.0)
107 static const uint8 dat2[] = {0x20, 0x78, 0x02, 0xae, 0xd1, 0xfc, 0x00, 0x01, 0x00, 0x00, 0x21, 0xc8, 0x00, 0x00};
108 base = find_rsrc_data(p, size, dat2, sizeof(dat2));
109 if (base) {
110 p16 = (uint16 *)(p + base);
111
112 #if defined(USE_SCRATCHMEM_SUBTERFUGE)
113 // Set 0x0000 to scratch memory area
114 extern uint8 *ScratchMem;
115 const uint32 ScratchMemBase = Host2MacAddr(ScratchMem);
116 *p16++ = htons(0x207c); // move.l #ScratchMem,a0
117 *p16++ = htons(ScratchMemBase >> 16);
118 *p16++ = htons(ScratchMemBase);
119 *p16++ = htons(M68K_NOP);
120 *p16 = htons(M68K_NOP);
121 #else
122 #error System specific handling for writable ROM is required here
123 #endif
124 FlushCodeCache(p + base, 14);
125 D(bug(" patch 2 applied\n"));
126 }
127
128 } else if (type == FOURCC('b','o','o','t') && id == 2) {
129 D(bug(" boot 2 found\n"));
130
131 // Set fake handle at 0x0000 to some safe place (so broken Mac programs won't write into Mac ROM) (7.1, 7.5, 8.0)
132 static const uint8 dat[] = {0x20, 0x78, 0x02, 0xae, 0xd1, 0xfc, 0x00, 0x01, 0x00, 0x00, 0x21, 0xc8, 0x00, 0x00};
133 base = find_rsrc_data(p, size, dat, sizeof(dat));
134 if (base) {
135 p16 = (uint16 *)(p + base);
136
137 #if defined(USE_SCRATCHMEM_SUBTERFUGE)
138 // Set 0x0000 to scratch memory area
139 extern uint8 *ScratchMem;
140 const uint32 ScratchMemBase = Host2MacAddr(ScratchMem);
141 *p16++ = htons(0x207c); // move.l #ScratchMem,a0
142 *p16++ = htons(ScratchMemBase >> 16);
143 *p16++ = htons(ScratchMemBase);
144 *p16++ = htons(M68K_NOP);
145 *p16 = htons(M68K_NOP);
146 #else
147 #error System specific handling for writable ROM is required here
148 #endif
149 FlushCodeCache(p + base, 14);
150 D(bug(" patch 1 applied\n"));
151 }
152 #endif
153
154 } else if (type == FOURCC('P','T','C','H') && id == 630) {
155 D(bug("PTCH 630 found\n"));
156
157 // Don't replace Time Manager (Classic ROM, 6.0.3)
158 static const uint8 dat[] = {0x30, 0x3c, 0x00, 0x58, 0xa2, 0x47};
159 base = find_rsrc_data(p, size, dat, sizeof(dat));
160 if (base) {
161 p16 = (uint16 *)(p + base);
162 p16[2] = htons(M68K_NOP);
163 p16[7] = htons(M68K_NOP);
164 p16[12] = htons(M68K_NOP);
165 FlushCodeCache(p + base, 26);
166 D(bug(" patch 1 applied\n"));
167 }
168
169 // Don't replace Time Manager (Classic ROM, 6.0.8)
170 static const uint8 dat2[] = {0x70, 0x58, 0xa2, 0x47};
171 base = find_rsrc_data(p, size, dat2, sizeof(dat2));
172 if (base) {
173 p16 = (uint16 *)(p + base);
174 p16[1] = htons(M68K_NOP);
175 p16[5] = htons(M68K_NOP);
176 p16[9] = htons(M68K_NOP);
177 FlushCodeCache(p + base, 20);
178 D(bug(" patch 1 applied\n"));
179 }
180
181 } else if (type == FOURCC('p','t','c','h') && id == 26) {
182 D(bug(" ptch 26 found\n"));
183
184 // Trap ABC4 is initialized with absolute ROM address (7.1, 7.5, 7.6, 7.6.1, 8.0)
185 static const uint8 dat[] = {0x40, 0x83, 0x36, 0x10};
186 base = find_rsrc_data(p, size, dat, sizeof(dat));
187 if (base) {
188 p16 = (uint16 *)(p + base);
189 *p16++ = htons((ROMBaseMac + 0x33610) >> 16);
190 *p16 = htons((ROMBaseMac + 0x33610) & 0xffff);
191 FlushCodeCache(p + base, 4);
192 D(bug(" patch 1 applied\n"));
193 }
194
195 } else if (type == FOURCC('p','t','c','h') && id == 34) {
196 D(bug(" ptch 34 found\n"));
197
198 // Don't wait for VIA (Classic ROM, 6.0.8)
199 static const uint8 dat[] = {0x22, 0x78, 0x01, 0xd4, 0x10, 0x11, 0x02, 0x00, 0x00, 0x30};
200 base = find_rsrc_data(p, size, dat, sizeof(dat));
201 if (base) {
202 p16 = (uint16 *)(p + base + 14);
203 *p16 = htons(M68K_NOP);
204 FlushCodeCache(p + base + 14, 2);
205 D(bug(" patch 1 applied\n"));
206 }
207
208 // Don't replace ADBOp() (Classic ROM, 6.0.8)
209 static const uint8 dat2[] = {0x21, 0xc0, 0x05, 0xf0};
210 base = find_rsrc_data(p, size, dat2, sizeof(dat2));
211 if (base) {
212 p16 = (uint16 *)(p + base);
213 *p16++ = htons(M68K_NOP);
214 *p16 = htons(M68K_NOP);
215 FlushCodeCache(p + base, 4);
216 D(bug(" patch 2 applied\n"));
217 }
218
219 } else if (type == FOURCC('g','p','c','h') && id == 750) {
220 D(bug(" gpch 750 found\n"));
221
222 // Don't use PTEST instruction in BlockMove() (7.5, 7.6, 7.6.1, 8.0)
223 static const uint8 dat[] = {0x20, 0x5f, 0x22, 0x5f, 0x0c, 0x38, 0x00, 0x04, 0x01, 0x2f};
224 base = find_rsrc_data(p, size, dat, sizeof(dat));
225 if (base) {
226 p16 = (uint16 *)(p + base + 4);
227 *p16++ = htons(M68K_EMUL_OP_BLOCK_MOVE);
228 *p16++ = htons(0x7000);
229 *p16 = htons(M68K_RTS);
230 FlushCodeCache(p + base + 4, 6);
231 D(bug(" patch 1 applied\n"));
232 }
233
234 // Patch SynchIdleTime()
235 patch_idle_time(p, size, 2);
236
237 } else if (type == FOURCC('l','p','c','h') && id == 24) {
238 D(bug(" lpch 24 found\n"));
239
240 // Don't replace Time Manager (7.0.1, 7.1, 7.5, 7.6, 7.6.1, 8.0)
241 static const uint8 dat[] = {0x70, 0x59, 0xa2, 0x47};
242 base = find_rsrc_data(p, size, dat, sizeof(dat));
243 if (base) {
244 p16 = (uint16 *)(p + base + 2);
245 *p16++ = htons(M68K_NOP);
246 p16 += 3;
247 *p16++ = htons(M68K_NOP);
248 p16 += 7;
249 *p16 = htons(M68K_NOP);
250 FlushCodeCache(p + base + 2, 28);
251 D(bug(" patch 1 applied\n"));
252 }
253
254 } else if (type == FOURCC('l','p','c','h') && id == 31) {
255 D(bug(" lpch 31 found\n"));
256
257 // Don't write to VIA in vSoundDead() (7.0.1, 7.1, 7.5, 7.6, 7.6.1, 8.0)
258 static const uint8 dat[] = {0x20, 0x78, 0x01, 0xd4, 0x08, 0xd0, 0x00, 0x07, 0x4e, 0x75};
259 base = find_rsrc_data(p, size, dat, sizeof(dat));
260 if (base) {
261 p16 = (uint16 *)(p + base);
262 *p16 = htons(M68K_RTS);
263 FlushCodeCache(p + base, 2);
264 D(bug(" patch 1 applied\n"));
265 }
266
267 // Don't replace SCSI manager (7.1, 7.5, 7.6.1, 8.0)
268 static const uint8 dat2[] = {0x0c, 0x6f, 0x00, 0x0e, 0x00, 0x04, 0x66, 0x0c};
269 base = find_rsrc_data(p, size, dat2, sizeof(dat2));
270 if (base) {
271 p16 = (uint16 *)(p + base);
272 *p16++ = htons(M68K_EMUL_OP_SCSI_DISPATCH);
273 *p16++ = htons(0x2e49); // move.l a1,a7
274 *p16 = htons(M68K_JMP_A0);
275 FlushCodeCache(p + base, 6);
276 D(bug(" patch 2 applied\n"));
277 }
278
279 // Patch SynchIdleTime()
280 patch_idle_time(p, size, 3);
281
282 } else if (type == FOURCC('t','h','n','g') && id == -16563) {
283 D(bug(" thng -16563 found\n"));
284
285 // Set audio component flags (7.5, 7.6, 7.6.1, 8.0)
286 *(uint32 *)(p + componentFlags) = htonl(audio_component_flags);
287 D(bug(" patch 1 applied\n"));
288
289 } else if (type == FOURCC('s','i','f','t') && id == -16563) {
290 D(bug(" sift -16563 found\n"));
291
292 // Replace audio component (7.5, 7.6, 7.6.1, 8.0)
293 p16 = (uint16 *)p;
294 *p16++ = htons(0x4e56); *p16++ = htons(0x0000); // link a6,#0
295 *p16++ = htons(0x48e7); *p16++ = htons(0x8018); // movem.l d0/a3-a4,-(sp)
296 *p16++ = htons(0x266e); *p16++ = htons(0x000c); // movea.l 12(a6),a3
297 *p16++ = htons(0x286e); *p16++ = htons(0x0008); // movea.l 8(a6),a4
298 *p16++ = htons(M68K_EMUL_OP_AUDIO);
299 *p16++ = htons(0x2d40); *p16++ = htons(0x0010); // move.l d0,16(a6)
300 *p16++ = htons(0x4cdf); *p16++ = htons(0x1801); // movem.l (sp)+,d0/a3-a4
301 *p16++ = htons(0x4e5e); // unlk a6
302 *p16++ = htons(0x4e74); *p16++ = htons(0x0008); // rtd #8
303 FlushCodeCache(p, 32);
304 D(bug(" patch 1 applied\n"));
305
306 } else if (type == FOURCC('i','n','s','t') && id == -19069) {
307 D(bug(" inst -19069 found\n"));
308
309 // Don't replace Microseconds (QuickTime 2.0)
310 static const uint8 dat[] = {0x30, 0x3c, 0xa1, 0x93, 0xa2, 0x47};
311 base = find_rsrc_data(p, size, dat, sizeof(dat));
312 if (base) {
313 p16 = (uint16 *)(p + base + 4);
314 *p16 = htons(M68K_NOP);
315 FlushCodeCache(p + base + 4, 2);
316 D(bug(" patch 1 applied\n"));
317 }
318
319 } else if (type == FOURCC('D','R','V','R') && id == -20066) {
320 D(bug("DRVR -20066 found\n"));
321
322 // Don't access SCC in .Infra driver
323 static const uint8 dat[] = {0x28, 0x78, 0x01, 0xd8, 0x48, 0xc7, 0x20, 0x0c, 0xd0, 0x87, 0x20, 0x40, 0x1c, 0x10};
324 base = find_rsrc_data(p, size, dat, sizeof(dat));
325 if (base) {
326 p16 = (uint16 *)(p + base + 12);
327 *p16 = htons(0x7a00); // moveq #0,d6
328 FlushCodeCache(p + base + 12, 2);
329 D(bug(" patch 1 applied\n"));
330 }
331
332 } else if (type == FOURCC('l','t','l','k') && id == 0) {
333 D(bug(" ltlk 0 found\n"));
334
335 // Disable LocalTalk (7.0.1, 7.5, 7.6, 7.6.1, 8.0)
336 p16 = (uint16 *)p;
337 *p16++ = htons(M68K_JMP_A0);
338 *p16++ = htons(0x7000);
339 *p16 = htons(M68K_RTS);
340 FlushCodeCache(p, 6);
341 D(bug(" patch 1 applied\n"));
342
343 } else if (type == FOURCC('D','R','V','R') && id == 41) {
344 D(bug(" DRVR 41 found\n"));
345
346 // Don't access ROM85 as it it was a pointer to a ROM version number (8.0, 8.1)
347 static const uint8 dat[] = {0x3a, 0x2e, 0x00, 0x0a, 0x55, 0x4f, 0x3e, 0xb8, 0x02, 0x8e, 0x30, 0x1f, 0x48, 0xc0, 0x24, 0x40, 0x20, 0x40};
348 base = find_rsrc_data(p, size, dat, sizeof(dat));
349 if (base) {
350 p16 = (uint16 *)(p + base + 4);
351 *p16++ = htons(0x303c); // move.l #ROM85,%d0
352 *p16++ = htons(0x028e);
353 *p16++ = htons(M68K_NOP);
354 *p16++ = htons(M68K_NOP);
355 FlushCodeCache(p + base + 4, 8);
356 D(bug(" patch 1 applied\n"));
357 }
358 }
359 }