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_DISABLE_INTERRUPT, |
55 |
NATIVE_ENABLE_INTERRUPT, |
56 |
NATIVE_MAKE_EXECUTABLE, |
57 |
NATIVE_CHECK_LOAD_INVOC, |
58 |
NATIVE_OP_MAX |
59 |
}; |
60 |
|
61 |
// Initialize the thunks system |
62 |
extern bool ThunksInit(void); |
63 |
|
64 |
// Exit the thunks system |
65 |
extern void ThunksExit(void); |
66 |
|
67 |
// Return the fake PowerPC opcode to handle specified native code |
68 |
#if EMULATED_PPC |
69 |
extern uint32 NativeOpcode(int selector); |
70 |
#endif |
71 |
|
72 |
// Return the native function descriptor (TVECT) |
73 |
extern uint32 NativeTVECT(int selector); |
74 |
|
75 |
// Return the native function address |
76 |
extern uint32 NativeFunction(int selector); |
77 |
|
78 |
// Return the routine descriptor address of the native function |
79 |
extern uint32 NativeRoutineDescriptor(int selector); |
80 |
|
81 |
|
82 |
/* |
83 |
* Helpers to share 32-bit addressable data with MacOS |
84 |
*/ |
85 |
|
86 |
class SheepMem { |
87 |
static uint32 align(uint32 size); |
88 |
protected: |
89 |
static uint32 page_size; |
90 |
static uintptr zero_page; |
91 |
static uintptr base; |
92 |
static uintptr top; |
93 |
static const uint32 size = 0x40000; // 256 KB |
94 |
public: |
95 |
static bool Init(void); |
96 |
static void Exit(void); |
97 |
static uint32 PageSize(); |
98 |
static uintptr ZeroPage(); |
99 |
static uintptr Reserve(uint32 size); |
100 |
static void Release(uint32 size); |
101 |
friend class SheepVar; |
102 |
}; |
103 |
|
104 |
inline uint32 SheepMem::align(uint32 size) |
105 |
{ |
106 |
// Align on 4 bytes boundaries |
107 |
return (size + 3) & -4; |
108 |
} |
109 |
|
110 |
inline uint32 SheepMem::PageSize() |
111 |
{ |
112 |
return page_size; |
113 |
} |
114 |
|
115 |
inline uintptr SheepMem::ZeroPage() |
116 |
{ |
117 |
return zero_page; |
118 |
} |
119 |
|
120 |
inline uintptr SheepMem::Reserve(uint32 size) |
121 |
{ |
122 |
top -= align(size); |
123 |
assert(top >= base); |
124 |
return top; |
125 |
} |
126 |
|
127 |
inline void SheepMem::Release(uint32 size) |
128 |
{ |
129 |
top += align(size); |
130 |
} |
131 |
|
132 |
class SheepVar |
133 |
{ |
134 |
uintptr m_base; |
135 |
uint32 m_size; |
136 |
public: |
137 |
SheepVar(uint32 requested_size); |
138 |
~SheepVar() { SheepMem::Release(m_size); } |
139 |
uintptr addr() const { return m_base; } |
140 |
void *ptr() const { return (void *)addr(); } |
141 |
}; |
142 |
|
143 |
inline SheepVar::SheepVar(uint32 requested_size) |
144 |
{ |
145 |
m_size = SheepMem::align(requested_size); |
146 |
m_base = SheepMem::Reserve(m_size); |
147 |
} |
148 |
|
149 |
// TODO: optimize for 32-bit platforms |
150 |
|
151 |
template< int size > |
152 |
struct SheepArray : public SheepVar |
153 |
{ |
154 |
SheepArray() : SheepVar(size) { } |
155 |
uint8 *ptr() const { return (uint8 *)addr(); } |
156 |
}; |
157 |
|
158 |
struct SheepVar32 : public SheepVar |
159 |
{ |
160 |
SheepVar32() : SheepVar(4) { } |
161 |
SheepVar32(uint32 value) : SheepVar(4) { set_value(value); } |
162 |
uint32 value() const { return ReadMacInt32(addr()); } |
163 |
void set_value(uint32 v) { WriteMacInt32(addr(), v); } |
164 |
uint32 *ptr() const { return (uint32 *)addr(); } |
165 |
}; |
166 |
|
167 |
struct SheepString : public SheepVar |
168 |
{ |
169 |
SheepString(const char *str) : SheepVar(strlen(str) + 1) |
170 |
{ if (str) strcpy((char *)addr(), str); else WriteMacInt8(addr(), 0); } |
171 |
char *value() const |
172 |
{ return (char *)addr(); } |
173 |
char *ptr() const |
174 |
{ return (char *)addr(); } |
175 |
}; |
176 |
|
177 |
#endif |