51 |
|
NATIVE_GET_IND_RESOURCE, |
52 |
|
NATIVE_GET_1_IND_RESOURCE, |
53 |
|
NATIVE_R_GET_RESOURCE, |
54 |
– |
NATIVE_DISABLE_INTERRUPT, |
55 |
– |
NATIVE_ENABLE_INTERRUPT, |
54 |
|
NATIVE_MAKE_EXECUTABLE, |
55 |
+ |
NATIVE_CHECK_LOAD_INVOC, |
56 |
+ |
NATIVE_SYNC_HOOK, |
57 |
+ |
NATIVE_BITBLT_HOOK, |
58 |
+ |
NATIVE_FILLRECT_HOOK, |
59 |
+ |
NATIVE_BITBLT, |
60 |
+ |
NATIVE_INVRECT, |
61 |
+ |
NATIVE_FILLRECT, |
62 |
|
NATIVE_OP_MAX |
63 |
|
}; |
64 |
|
|
65 |
|
// Initialize the thunks system |
66 |
|
extern bool ThunksInit(void); |
67 |
|
|
68 |
+ |
// Exit the thunks system |
69 |
+ |
extern void ThunksExit(void); |
70 |
+ |
|
71 |
|
// Return the fake PowerPC opcode to handle specified native code |
72 |
|
#if EMULATED_PPC |
73 |
|
extern uint32 NativeOpcode(int selector); |
79 |
|
// Return the native function address |
80 |
|
extern uint32 NativeFunction(int selector); |
81 |
|
|
82 |
+ |
// Return the routine descriptor address of the native function |
83 |
+ |
extern uint32 NativeRoutineDescriptor(int selector); |
84 |
+ |
|
85 |
|
|
86 |
|
/* |
87 |
|
* Helpers to share 32-bit addressable data with MacOS |
88 |
+ |
* |
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 |
|
*/ |
105 |
|
|
106 |
|
class SheepMem { |
107 |
|
static uint32 align(uint32 size); |
108 |
|
protected: |
109 |
+ |
static uint32 page_size; |
110 |
+ |
static uintptr zero_page; |
111 |
|
static uintptr base; |
112 |
< |
static uintptr top; |
113 |
< |
static const uint32 size = 0x40000; |
112 |
> |
static uintptr data; |
113 |
> |
static uintptr proc; |
114 |
> |
static const uint32 size = 0x80000; // 512 KB |
115 |
|
public: |
116 |
|
static bool Init(void); |
117 |
|
static void Exit(void); |
118 |
< |
static uintptr Reserve(uint32 size); |
118 |
> |
static uint32 PageSize(); |
119 |
> |
static uint32 ZeroPage(); |
120 |
> |
static uint32 Reserve(uint32 size); |
121 |
|
static void Release(uint32 size); |
122 |
+ |
static uint32 ReserveProc(uint32 size); |
123 |
|
friend class SheepVar; |
124 |
|
}; |
125 |
|
|
129 |
|
return (size + 3) & -4; |
130 |
|
} |
131 |
|
|
132 |
< |
inline uintptr SheepMem::Reserve(uint32 size) |
132 |
> |
inline uint32 SheepMem::PageSize() |
133 |
> |
{ |
134 |
> |
return page_size; |
135 |
> |
} |
136 |
> |
|
137 |
> |
inline uint32 SheepMem::ZeroPage() |
138 |
> |
{ |
139 |
> |
return zero_page; |
140 |
> |
} |
141 |
> |
|
142 |
> |
inline uint32 SheepMem::Reserve(uint32 size) |
143 |
|
{ |
144 |
< |
top -= align(size); |
145 |
< |
assert(top >= base); |
146 |
< |
return top; |
144 |
> |
data -= align(size); |
145 |
> |
assert(data >= proc); |
146 |
> |
return data; |
147 |
|
} |
148 |
|
|
149 |
|
inline void SheepMem::Release(uint32 size) |
150 |
|
{ |
151 |
< |
top += align(size); |
151 |
> |
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 |
+ |
} |
168 |
+ |
|
169 |
+ |
#define BUILD_SHEEPSHAVER_PROCEDURE(PROC) \ |
170 |
+ |
static uint32 PROC = 0; \ |
171 |
+ |
if (PROC == 0) \ |
172 |
+ |
PROC = SheepProc(PROC##_template, sizeof(PROC##_template)) |
173 |
+ |
|
174 |
|
class SheepVar |
175 |
|
{ |
176 |
< |
uintptr m_base; |
177 |
< |
uint32 m_size; |
176 |
> |
uint32 m_base; |
177 |
> |
uint32 m_size; |
178 |
|
public: |
179 |
|
SheepVar(uint32 requested_size); |
180 |
|
~SheepVar() { SheepMem::Release(m_size); } |
181 |
< |
uintptr addr() const { return m_base; } |
119 |
< |
void *ptr() const { return (void *)addr(); } |
181 |
> |
uint32 addr() const { return m_base; } |
182 |
|
}; |
183 |
|
|
184 |
|
inline SheepVar::SheepVar(uint32 requested_size) |
189 |
|
|
190 |
|
// TODO: optimize for 32-bit platforms |
191 |
|
|
192 |
< |
template< int size > |
192 |
> |
template< int requested_size > |
193 |
|
struct SheepArray : public SheepVar |
194 |
|
{ |
195 |
< |
SheepArray() : SheepVar(size) { } |
134 |
< |
uint8 *ptr() const { return (uint8 *)addr(); } |
195 |
> |
SheepArray() : SheepVar(requested_size) { } |
196 |
|
}; |
197 |
|
|
198 |
|
struct SheepVar32 : public SheepVar |
201 |
|
SheepVar32(uint32 value) : SheepVar(4) { set_value(value); } |
202 |
|
uint32 value() const { return ReadMacInt32(addr()); } |
203 |
|
void set_value(uint32 v) { WriteMacInt32(addr(), v); } |
143 |
– |
uint32 *ptr() const { return (uint32 *)addr(); } |
204 |
|
}; |
205 |
|
|
206 |
|
struct SheepString : public SheepVar |
207 |
|
{ |
208 |
|
SheepString(const char *str) : SheepVar(strlen(str) + 1) |
209 |
< |
{ if (str) strcpy((char *)addr(), str); else WriteMacInt8(addr(), 0); } |
209 |
> |
{ if (str) strcpy(value(), str); else WriteMacInt8(addr(), 0); } |
210 |
|
char *value() const |
211 |
< |
{ return (char *)addr(); } |
152 |
< |
char *ptr() const |
153 |
< |
{ return (char *)addr(); } |
211 |
> |
{ return (char *)Mac2HostAddr(addr()); } |
212 |
|
}; |
213 |
|
|
214 |
|
#endif |