ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/include/thunks.h
Revision: 1.11
Committed: 2004-11-22T21:50:45Z (19 years, 11 months ago) by gbeauche
Content type: text/plain
Branch: MAIN
Changes since 1.10: +16 -0 lines
Log Message:
Try to explain the SheepShaver globals

File Contents

# User Rev Content
1 gbeauche 1.1 /*
2     * thunks.h - Thunks to share data and code with MacOS
3     *
4     * SheepShaver (C) 1997-2002 Christian Bauer and Marc Hellwig
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     #ifndef THUNKS_H
22     #define THUNKS_H
23    
24     #include "cpu_emulation.h"
25    
26     /*
27     * Native function invocation
28     */
29    
30     enum {
31     NATIVE_PATCH_NAME_REGISTRY,
32     NATIVE_VIDEO_INSTALL_ACCEL,
33     NATIVE_VIDEO_VBL,
34     NATIVE_VIDEO_DO_DRIVER_IO,
35     NATIVE_ETHER_IRQ,
36     NATIVE_ETHER_INIT,
37     NATIVE_ETHER_TERM,
38     NATIVE_ETHER_OPEN,
39     NATIVE_ETHER_CLOSE,
40     NATIVE_ETHER_WPUT,
41     NATIVE_ETHER_RSRV,
42     NATIVE_SERIAL_NOTHING,
43     NATIVE_SERIAL_OPEN,
44     NATIVE_SERIAL_PRIME_IN,
45     NATIVE_SERIAL_PRIME_OUT,
46     NATIVE_SERIAL_CONTROL,
47     NATIVE_SERIAL_STATUS,
48     NATIVE_SERIAL_CLOSE,
49     NATIVE_GET_RESOURCE,
50     NATIVE_GET_1_RESOURCE,
51     NATIVE_GET_IND_RESOURCE,
52     NATIVE_GET_1_IND_RESOURCE,
53     NATIVE_R_GET_RESOURCE,
54     NATIVE_MAKE_EXECUTABLE,
55 gbeauche 1.4 NATIVE_CHECK_LOAD_INVOC,
56 gbeauche 1.6 NATIVE_SYNC_HOOK,
57     NATIVE_BITBLT_HOOK,
58     NATIVE_FILLRECT_HOOK,
59     NATIVE_BITBLT,
60     NATIVE_INVRECT,
61 gbeauche 1.7 NATIVE_FILLRECT,
62 gbeauche 1.1 NATIVE_OP_MAX
63     };
64    
65     // Initialize the thunks system
66     extern bool ThunksInit(void);
67    
68 gbeauche 1.3 // Exit the thunks system
69     extern void ThunksExit(void);
70    
71 gbeauche 1.1 // Return the fake PowerPC opcode to handle specified native code
72     #if EMULATED_PPC
73     extern uint32 NativeOpcode(int selector);
74     #endif
75    
76     // Return the native function descriptor (TVECT)
77     extern uint32 NativeTVECT(int selector);
78    
79     // Return the native function address
80     extern uint32 NativeFunction(int selector);
81    
82 gbeauche 1.3 // Return the routine descriptor address of the native function
83     extern uint32 NativeRoutineDescriptor(int selector);
84    
85 gbeauche 1.1
86     /*
87     * Helpers to share 32-bit addressable data with MacOS
88 gbeauche 1.11 *
89     * There are two distinct allocatable regions:
90     *
91     * - The Data region is used to share data between MacOS and
92     * SheepShaver. This is stack-like allocation since it is
93     * meant to only hold temporary data which dies at the end
94     * of the current function scope.
95     *
96     * - The Procedure region is used to hold permanent M68K or
97     * PowerPC code to assist native routine implementations.
98     *
99     * - The Procedure region grows up whereas the Data region
100     * grows down. They may intersect into the ZeroPage, which
101     * is a read-only page with all bits set to zero. In practise,
102     * the intersection is unlikely since the Procedure region is
103     * static and the Data region is meant to be small (< 256 KB).
104 gbeauche 1.1 */
105    
106     class SheepMem {
107     static uint32 align(uint32 size);
108     protected:
109 gbeauche 1.5 static uint32 page_size;
110 gbeauche 1.2 static uintptr zero_page;
111 gbeauche 1.1 static uintptr base;
112 gbeauche 1.10 static uintptr data;
113     static uintptr proc;
114     static const uint32 size = 0x80000; // 512 KB
115 gbeauche 1.1 public:
116     static bool Init(void);
117     static void Exit(void);
118 gbeauche 1.5 static uint32 PageSize();
119 gbeauche 1.9 static uint32 ZeroPage();
120     static uint32 Reserve(uint32 size);
121 gbeauche 1.1 static void Release(uint32 size);
122 gbeauche 1.10 static uint32 ReserveProc(uint32 size);
123 gbeauche 1.1 friend class SheepVar;
124     };
125    
126     inline uint32 SheepMem::align(uint32 size)
127     {
128     // Align on 4 bytes boundaries
129     return (size + 3) & -4;
130 gbeauche 1.5 }
131    
132     inline uint32 SheepMem::PageSize()
133     {
134     return page_size;
135 gbeauche 1.2 }
136    
137 gbeauche 1.9 inline uint32 SheepMem::ZeroPage()
138 gbeauche 1.2 {
139     return zero_page;
140 gbeauche 1.1 }
141    
142 gbeauche 1.9 inline uint32 SheepMem::Reserve(uint32 size)
143 gbeauche 1.1 {
144 gbeauche 1.10 data -= align(size);
145     assert(data >= proc);
146     return data;
147 gbeauche 1.1 }
148    
149     inline void SheepMem::Release(uint32 size)
150     {
151 gbeauche 1.10 data += align(size);
152     }
153    
154     inline uint32 SheepMem::ReserveProc(uint32 size)
155     {
156     uint32 mproc = proc;
157     proc += align(size);
158     assert(proc < data);
159     return mproc;
160     }
161    
162     static inline uint32 SheepProc(const uint8 *proc, uint32 proc_size)
163     {
164     uint32 mac_proc = SheepMem::ReserveProc(proc_size);
165     Host2Mac_memcpy(mac_proc, proc, proc_size);
166     return mac_proc;
167 gbeauche 1.1 }
168    
169     class SheepVar
170     {
171 gbeauche 1.9 uint32 m_base;
172     uint32 m_size;
173 gbeauche 1.1 public:
174     SheepVar(uint32 requested_size);
175     ~SheepVar() { SheepMem::Release(m_size); }
176 gbeauche 1.9 uint32 addr() const { return m_base; }
177 gbeauche 1.1 };
178    
179     inline SheepVar::SheepVar(uint32 requested_size)
180     {
181     m_size = SheepMem::align(requested_size);
182     m_base = SheepMem::Reserve(m_size);
183     }
184    
185     // TODO: optimize for 32-bit platforms
186    
187 gbeauche 1.9 template< int requested_size >
188 gbeauche 1.1 struct SheepArray : public SheepVar
189     {
190 gbeauche 1.9 SheepArray() : SheepVar(requested_size) { }
191 gbeauche 1.1 };
192    
193     struct SheepVar32 : public SheepVar
194     {
195     SheepVar32() : SheepVar(4) { }
196     SheepVar32(uint32 value) : SheepVar(4) { set_value(value); }
197     uint32 value() const { return ReadMacInt32(addr()); }
198     void set_value(uint32 v) { WriteMacInt32(addr(), v); }
199     };
200    
201     struct SheepString : public SheepVar
202     {
203     SheepString(const char *str) : SheepVar(strlen(str) + 1)
204 gbeauche 1.9 { if (str) strcpy(value(), str); else WriteMacInt8(addr(), 0); }
205 gbeauche 1.1 char *value() const
206 gbeauche 1.9 { return (char *)Mac2HostAddr(addr()); }
207 gbeauche 1.1 };
208    
209     #endif