ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/main.cpp
Revision: 1.3
Committed: 2005-02-27T21:52:05Z (19 years, 9 months ago) by gbeauche
Branch: MAIN
Changes since 1.2: +6 -1 lines
Log Message:
Fix native Linux/ppc with recent enough glibc that supports TLS; r2 is used
in that case. Tell me if I broke other arches, e.g. r13 is no longer saved
in Video and Ethernet stubs, though it seems to be OK.

Colateral feature: SheepShaver should now run on Linux/ppc64 with relevant
32-bit runtime. Native Linux/ppc64 support is harder as low mem globals are
32-bit in mind and e.g. the TLS register there is %r13, %r2 is the TOC
(PowerOpen/AIX ABI)

File Contents

# User Rev Content
1 gbeauche 1.1 /*
2     * main.cpp - ROM patches
3     *
4 gbeauche 1.2 * SheepShaver (C) 1997-2005 Christian Bauer and Marc Hellwig
5 gbeauche 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    
23     #include "main.h"
24     #include "version.h"
25     #include "prefs.h"
26     #include "prefs_editor.h"
27     #include "cpu_emulation.h"
28     #include "emul_op.h"
29     #include "xlowmem.h"
30     #include "xpram.h"
31     #include "timer.h"
32     #include "adb.h"
33     #include "sony.h"
34     #include "disk.h"
35     #include "cdrom.h"
36     #include "scsi.h"
37     #include "video.h"
38     #include "audio.h"
39     #include "ether.h"
40     #include "serial.h"
41     #include "clip.h"
42     #include "extfs.h"
43     #include "sys.h"
44     #include "macos_util.h"
45     #include "rom_patches.h"
46     #include "user_strings.h"
47     #include "vm_alloc.h"
48     #include "sigsegv.h"
49     #include "thunks.h"
50    
51     #define DEBUG 0
52     #include "debug.h"
53    
54     #ifdef ENABLE_MON
55     #include "mon.h"
56    
57     static uint32 sheepshaver_read_byte(uintptr adr)
58     {
59     return ReadMacInt8(adr);
60     }
61    
62     static void sheepshaver_write_byte(uintptr adr, uint32 b)
63     {
64     WriteMacInt8(adr, b);
65     }
66     #endif
67    
68    
69     /*
70     * Initialize everything, returns false on error
71     */
72    
73     bool InitAll(void)
74     {
75     // Load NVRAM
76     XPRAMInit();
77    
78     // Load XPRAM default values if signature not found
79     if (XPRAM[0x130c] != 0x4e || XPRAM[0x130d] != 0x75
80     || XPRAM[0x130e] != 0x4d || XPRAM[0x130f] != 0x63) {
81     D(bug("Loading XPRAM default values\n"));
82     memset(XPRAM + 0x1300, 0, 0x100);
83     XPRAM[0x130c] = 0x4e; // "NuMc" signature
84     XPRAM[0x130d] = 0x75;
85     XPRAM[0x130e] = 0x4d;
86     XPRAM[0x130f] = 0x63;
87     XPRAM[0x1301] = 0x80; // InternalWaitFlags = DynWait (don't wait for SCSI devices upon bootup)
88     XPRAM[0x1310] = 0xa8; // Standard PRAM values
89     XPRAM[0x1311] = 0x00;
90     XPRAM[0x1312] = 0x00;
91     XPRAM[0x1313] = 0x22;
92     XPRAM[0x1314] = 0xcc;
93     XPRAM[0x1315] = 0x0a;
94     XPRAM[0x1316] = 0xcc;
95     XPRAM[0x1317] = 0x0a;
96     XPRAM[0x131c] = 0x00;
97     XPRAM[0x131d] = 0x02;
98     XPRAM[0x131e] = 0x63;
99     XPRAM[0x131f] = 0x00;
100     XPRAM[0x1308] = 0x13;
101     XPRAM[0x1309] = 0x88;
102     XPRAM[0x130a] = 0x00;
103     XPRAM[0x130b] = 0xcc;
104     XPRAM[0x1376] = 0x00; // OSDefault = MacOS
105     XPRAM[0x1377] = 0x01;
106     }
107    
108     // Set boot volume
109     int16 i16 = PrefsFindInt32("bootdrive");
110     XPRAM[0x1378] = i16 >> 8;
111     XPRAM[0x1379] = i16 & 0xff;
112     i16 = PrefsFindInt32("bootdriver");
113     XPRAM[0x137a] = i16 >> 8;
114     XPRAM[0x137b] = i16 & 0xff;
115    
116     // Create BootGlobs at top of Mac memory
117     memset(RAMBaseHost + RAMSize - 4096, 0, 4096);
118     BootGlobsAddr = RAMBase + RAMSize - 0x1c;
119     WriteMacInt32(BootGlobsAddr - 5 * 4, RAMBase + RAMSize); // MemTop
120     WriteMacInt32(BootGlobsAddr + 0 * 4, RAMBase); // First RAM bank
121     WriteMacInt32(BootGlobsAddr + 1 * 4, RAMSize);
122     WriteMacInt32(BootGlobsAddr + 2 * 4, (uint32)-1); // End of bank table
123    
124     // Init thunks
125     if (!ThunksInit())
126     return false;
127    
128     // Init drivers
129     SonyInit();
130     DiskInit();
131     CDROMInit();
132     SCSIInit();
133    
134     // Init external file system
135     ExtFSInit();
136    
137     // Init ADB
138     ADBInit();
139    
140     // Init audio
141     AudioInit();
142    
143     // Init network
144     EtherInit();
145    
146     // Init serial ports
147     SerialInit();
148    
149     // Init Time Manager
150     TimerInit();
151    
152     // Init clipboard
153     ClipInit();
154    
155     // Init video
156     if (!VideoInit())
157     return false;
158    
159     // Install ROM patches
160     if (!PatchROM()) {
161     ErrorAlert(GetString(STR_UNSUPPORTED_ROM_TYPE_ERR));
162     return false;
163     }
164    
165     // Initialize Kernel Data
166     KernelData *kernel_data = (KernelData *)Mac2HostAddr(KERNEL_DATA_BASE);
167     memset(kernel_data, 0, sizeof(KernelData));
168     if (ROMType == ROMTYPE_NEWWORLD) {
169     uint32 of_dev_tree = SheepMem::Reserve(4 * sizeof(uint32));
170     Mac_memset(of_dev_tree, 0, 4 * sizeof(uint32));
171     uint32 vector_lookup_tbl = SheepMem::Reserve(128);
172     uint32 vector_mask_tbl = SheepMem::Reserve(64);
173     memset((uint8 *)kernel_data + 0xb80, 0x3d, 0x80);
174     Mac_memset(vector_lookup_tbl, 0, 128);
175     Mac_memset(vector_mask_tbl, 0, 64);
176     kernel_data->v[0xb80 >> 2] = htonl(ROM_BASE);
177     kernel_data->v[0xb84 >> 2] = htonl(of_dev_tree); // OF device tree base
178     kernel_data->v[0xb90 >> 2] = htonl(vector_lookup_tbl);
179     kernel_data->v[0xb94 >> 2] = htonl(vector_mask_tbl);
180     kernel_data->v[0xb98 >> 2] = htonl(ROM_BASE); // OpenPIC base
181     kernel_data->v[0xbb0 >> 2] = htonl(0); // ADB base
182     kernel_data->v[0xc20 >> 2] = htonl(RAMSize);
183     kernel_data->v[0xc24 >> 2] = htonl(RAMSize);
184     kernel_data->v[0xc30 >> 2] = htonl(RAMSize);
185     kernel_data->v[0xc34 >> 2] = htonl(RAMSize);
186     kernel_data->v[0xc38 >> 2] = htonl(0x00010020);
187     kernel_data->v[0xc3c >> 2] = htonl(0x00200001);
188     kernel_data->v[0xc40 >> 2] = htonl(0x00010000);
189     kernel_data->v[0xc50 >> 2] = htonl(RAMBase);
190     kernel_data->v[0xc54 >> 2] = htonl(RAMSize);
191     kernel_data->v[0xf60 >> 2] = htonl(PVR);
192     kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed); // clock-frequency
193     kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed); // bus-frequency
194     kernel_data->v[0xf6c >> 2] = htonl(TimebaseSpeed); // timebase-frequency
195     } else if (ROMType == ROMTYPE_GOSSAMER) {
196     kernel_data->v[0xc80 >> 2] = htonl(RAMSize);
197     kernel_data->v[0xc84 >> 2] = htonl(RAMSize);
198     kernel_data->v[0xc90 >> 2] = htonl(RAMSize);
199     kernel_data->v[0xc94 >> 2] = htonl(RAMSize);
200     kernel_data->v[0xc98 >> 2] = htonl(0x00010020);
201     kernel_data->v[0xc9c >> 2] = htonl(0x00200001);
202     kernel_data->v[0xca0 >> 2] = htonl(0x00010000);
203     kernel_data->v[0xcb0 >> 2] = htonl(RAMBase);
204     kernel_data->v[0xcb4 >> 2] = htonl(RAMSize);
205     kernel_data->v[0xf60 >> 2] = htonl(PVR);
206     kernel_data->v[0xf64 >> 2] = htonl(CPUClockSpeed); // clock-frequency
207     kernel_data->v[0xf68 >> 2] = htonl(BusClockSpeed); // bus-frequency
208     kernel_data->v[0xf6c >> 2] = htonl(TimebaseSpeed); // timebase-frequency
209     } else {
210     kernel_data->v[0xc80 >> 2] = htonl(RAMSize);
211     kernel_data->v[0xc84 >> 2] = htonl(RAMSize);
212     kernel_data->v[0xc90 >> 2] = htonl(RAMSize);
213     kernel_data->v[0xc94 >> 2] = htonl(RAMSize);
214     kernel_data->v[0xc98 >> 2] = htonl(0x00010020);
215     kernel_data->v[0xc9c >> 2] = htonl(0x00200001);
216     kernel_data->v[0xca0 >> 2] = htonl(0x00010000);
217     kernel_data->v[0xcb0 >> 2] = htonl(RAMBase);
218     kernel_data->v[0xcb4 >> 2] = htonl(RAMSize);
219     kernel_data->v[0xf80 >> 2] = htonl(PVR);
220     kernel_data->v[0xf84 >> 2] = htonl(CPUClockSpeed); // clock-frequency
221     kernel_data->v[0xf88 >> 2] = htonl(BusClockSpeed); // bus-frequency
222     kernel_data->v[0xf8c >> 2] = htonl(TimebaseSpeed); // timebase-frequency
223     }
224    
225     // Initialize extra low memory
226     D(bug("Initializing Low Memory...\n"));
227     Mac_memset(0, 0, 0x3000);
228     WriteMacInt32(XLM_SIGNATURE, FOURCC('B','a','a','h')); // Signature to detect SheepShaver
229     WriteMacInt32(XLM_KERNEL_DATA, KernelDataAddr); // For trap replacement routines
230     WriteMacInt32(XLM_PVR, PVR); // Theoretical PVR
231     WriteMacInt32(XLM_BUS_CLOCK, BusClockSpeed); // For DriverServicesLib patch
232     WriteMacInt16(XLM_EXEC_RETURN_OPCODE, M68K_EXEC_RETURN); // For Execute68k() (RTS from the executed 68k code will jump here and end 68k mode)
233     WriteMacInt32(XLM_ZERO_PAGE, SheepMem::ZeroPage()); // Pointer to read-only page with all bits set to 0
234     #if !EMULATED_PPC
235 gbeauche 1.3 #ifdef SYSTEM_CLOBBERS_R2
236     WriteMacInt32(XLM_TOC, (uint32)TOC); // TOC pointer of emulator
237     #endif
238     #ifdef SYSTEM_CLOBBERS_R13
239     WriteMacInt32(XLM_R13, (uint32)R13); // TLS register
240     #endif
241 gbeauche 1.1 #endif
242     WriteMacInt32(XLM_ETHER_INIT, NativeFunction(NATIVE_ETHER_INIT)); // DLPI ethernet driver functions
243     WriteMacInt32(XLM_ETHER_TERM, NativeFunction(NATIVE_ETHER_TERM));
244     WriteMacInt32(XLM_ETHER_OPEN, NativeFunction(NATIVE_ETHER_OPEN));
245     WriteMacInt32(XLM_ETHER_CLOSE, NativeFunction(NATIVE_ETHER_CLOSE));
246     WriteMacInt32(XLM_ETHER_WPUT, NativeFunction(NATIVE_ETHER_WPUT));
247     WriteMacInt32(XLM_ETHER_RSRV, NativeFunction(NATIVE_ETHER_RSRV));
248     WriteMacInt32(XLM_VIDEO_DOIO, NativeFunction(NATIVE_VIDEO_DO_DRIVER_IO));
249     D(bug("Low Memory initialized\n"));
250    
251     #if ENABLE_MON
252     // Initialize mon
253     mon_init();
254     mon_read_byte = sheepshaver_read_byte;
255     mon_write_byte = sheepshaver_write_byte;
256     #endif
257    
258     return true;
259     }
260    
261    
262     /*
263     * Deinitialize everything
264     */
265    
266     void ExitAll(void)
267     {
268     #if ENABLE_MON
269     // Deinitialize mon
270     mon_exit();
271     #endif
272    
273     // Save NVRAM
274     XPRAMExit();
275    
276     // Exit clipboard
277     ClipExit();
278    
279     // Exit Time Manager
280     TimerExit();
281    
282     // Exit serial
283     SerialExit();
284    
285     // Exit network
286     EtherExit();
287    
288     // Exit audio
289     AudioExit();
290    
291     // Exit ADB
292     ADBExit();
293    
294     // Exit video
295     VideoExit();
296    
297     // Exit external file system
298     ExtFSExit();
299    
300     // Exit drivers
301     SCSIExit();
302     CDROMExit();
303     DiskExit();
304     SonyExit();
305    
306     // Delete thunks
307     ThunksExit();
308     }
309    
310    
311     /*
312     * Patch things after system startup (gets called by disk driver accRun routine)
313     */
314    
315     void PatchAfterStartup(void)
316     {
317     ExecuteNative(NATIVE_VIDEO_INSTALL_ACCEL);
318     InstallExtFS();
319     }