forked from ShovelTime/final-frontier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_host.c
342 lines (307 loc) · 6.75 KB
/
_host.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* This is used by the C output mode of the 68k assembler.
* Loads 68k executable into m68k memory and applies all the relocations.
* Executable is in an atari .prg format.
*/
typedef void (*HOSTCALL) ();
extern HOSTCALL hcalls[];
//#define likely(x) __builtin_expect((x),1)
//#define unlikely(x) __builtin_expect((x),0)
#define M68K_DEBUG
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
typedef unsigned int u32;
typedef unsigned short u16;
typedef unsigned char u8;
typedef signed int s32;
typedef signed short s16;
typedef signed char s8;
#define LOAD_BASE 0x0
# ifdef M68K_DEBUG
int line_no;
# endif /* M68K_DEBUG */
#define MEM_SIZE (0x110000)
union Reg {
u16 word[2];
u32 _u32;
u16 _u16;
u8 _u8;
s32 _s32;
s16 _s16;
s8 _s8;
};
/* m68000 state ----------------------------------------------------- */
s8 m68kram[MEM_SIZE];
union Reg Regs[16];
/* status flags */
/* it is an optimisation that instead of having a Z (zero) flag we have
* an nZ (not zero) flag, because this way we can usually just stick
* the result in nZ. */
s32 N,nZ,V,C,X;
s32 bN,bnZ,bV,bC,bX;
s32 rdest; /* return address from interrupt. zero if none in service */
s32 exceptions_pending;
s32 exceptions_pending_nums[32];
u32 exception_handlers[32];
s8 *STRam;
#ifdef M68K_DEBUG
#define BOUNDS_CHECK
#if 0
static inline void BOUNDS_CHECK (u32 pos, int num)
{
if ((pos+num) > MEM_SIZE) {
printf ("Error. 68K memory access out of bounds (address $%x, line %d).\n", pos, line_no);
abort ();
}
}
#endif
#endif /* M68K_DEBUG */
static inline u32 do_get_mem_long(u32 *a)
{
#ifdef __i386__
u32 val = *a;
__asm__ ("bswap %0\n":"=r"(val):"0"(val));
return val;
#elif LITTLE_ENDIAN
u8 *b = (u8 *)a;
return (*b << 24) | (*(b+1) << 16) | (*(b+2) << 8) | (*(b+3));
#else
return *a;
#endif
}
static inline u16 do_get_mem_word(u16 *a)
{
#ifdef __i386__
u16 val = *a;
__asm__ ("rorw $8,%0" : "=q" (val) : "0" (val));
return val;
#elif LITTLE_ENDIAN
u8 *b = (u8 *)a;
return (*b << 8) | (*(b+1));
#else
return *a;
#endif
}
static inline u8 do_get_mem_byte(u8 *a)
{
return *a;
}
static inline void do_put_mem_long(u32 *a, u32 v)
{
#ifdef __i386__
__asm__ ("bswap %0\n":"=r"(v):"0"(v));
*a = v;
#elif LITTLE_ENDIAN
u8 *b = (u8 *)a;
*b = v >> 24;
*(b+1) = v >> 16;
*(b+2) = v >> 8;
*(b+3) = v;
#else
*a = v;
#endif
}
static inline void do_put_mem_word(u16 *a, u16 v)
{
#ifdef __i386__
__asm__ ("rorw $8,%0" : "=q" (v) : "0" (v));
*a = v;
#elif LITTLE_ENDIAN
u8 *b = (u8 *)a;
*b = v >> 8;
*(b+1) = v;
#else
*a = v;
#endif
}
static inline void do_put_mem_byte(u8 *a, u8 v)
{
*a = v;
}
#ifdef PART2
void SetReg (int reg, int val)
{
Regs[reg]._s32 = val;
}
int GetReg (int reg)
{
return Regs[reg]._s32;
}
s32 MemReadLong (u32 pos)
{
#ifdef M68K_DEBUG
BOUNDS_CHECK (pos,4);
#endif /* M68K_DEBUG */
return do_get_mem_long ((u32 *)(m68kram+pos));
}
s16 MemReadWord (u32 pos)
{
#ifdef M68K_DEBUG
BOUNDS_CHECK (pos,2);
#endif /* M68K_DEBUG */
return do_get_mem_word ((u16 *)(m68kram+pos));
}
s8 MemReadByte (u32 pos)
{
#ifdef M68K_DEBUG
BOUNDS_CHECK (pos,1);
#endif /* M68K_DEBUG */
return do_get_mem_byte ((u8 *)(m68kram+pos));
}
void MemWriteByte (u32 pos, int val)
{
#ifdef M68K_DEBUG
BOUNDS_CHECK (pos,1);
#endif /* M68K_DEBUG */
do_put_mem_byte ((u8 *)(m68kram+pos), (u8)val);
}
void MemWriteWord (u32 pos, int val)
{
#ifdef M68K_DEBUG
BOUNDS_CHECK (pos,2);
#endif /* M68K_DEBUG */
do_put_mem_word ((u16 *)(m68kram+pos), (u16)val);
}
void MemWriteLong (u32 pos, int val)
{
#ifdef M68K_DEBUG
BOUNDS_CHECK (pos,4);
#endif /* M68K_DEBUG */
do_put_mem_long ((u32 *)(m68kram+pos), (u32)val);
}
int GetZFlag () { return !nZ; }
int GetNFlag () { return N; }
int GetCFlag () { return C; }
int GetVFlag () { return V; }
int GetXFlag () { return X; }
void SetZFlag (char val) { nZ = !val; }
#endif /* PART2 */
static inline s32 rdlong (u32 pos)
{
#ifdef M68K_DEBUG
BOUNDS_CHECK (pos,4);
#endif /* M68K_DEBUG */
return do_get_mem_long ((u32 *)(m68kram+pos));
}
static inline s16 rdword (u32 pos)
{
#ifdef M68K_DEBUG
BOUNDS_CHECK (pos,2);
#endif /* M68K_DEBUG */
return do_get_mem_word ((u16 *)(m68kram+pos));
}
static inline s8 rdbyte (u32 pos)
{
#ifdef M68K_DEBUG
BOUNDS_CHECK (pos,1);
#endif /* M68K_DEBUG */
return do_get_mem_byte ((u8 *)(m68kram+pos));
}
static inline void wrbyte (u32 pos, int val)
{
#ifdef M68K_DEBUG
BOUNDS_CHECK (pos,1);
#endif /* M68K_DEBUG */
do_put_mem_byte ((u8 *)(m68kram+pos), (u8)val);
}
static inline void wrword (u32 pos, int val)
{
#ifdef M68K_DEBUG
BOUNDS_CHECK (pos,2);
#endif /* M68K_DEBUG */
do_put_mem_word ((u16 *)(m68kram+pos), (u16)val);
}
static inline void wrlong (u32 pos, int val)
{
#ifdef M68K_DEBUG
BOUNDS_CHECK (pos,4);
#endif /* M68K_DEBUG */
do_put_mem_long ((u32 *)(m68kram+pos), (u32)val);
}
#ifdef PART1
void FlagException (int num)
{
if (exception_handlers[num]) {
exceptions_pending |= 1<<num;
exceptions_pending_nums[num]++;
}
}
/* bin loader (in-place) ------------------------------------------------------- */
static s32 buf_pos;
static s32 get_fixup (s32 reloc, s32 code_end)
{
s32 old_bufpos;
s32 next;
static s32 reloc_pos;
old_bufpos = buf_pos;
if (reloc == 0) {
buf_pos = code_end;
reloc = rdlong (buf_pos);
buf_pos += 4;
reloc_pos = buf_pos;
buf_pos = old_bufpos;
if (reloc == 0) return 0;
else return reloc+0x1c+LOAD_BASE;
} else {
buf_pos = reloc_pos;
again:
next = (u8)rdbyte (buf_pos);
buf_pos++;
if (next == 0) {
buf_pos = old_bufpos;
return 0;
} else if (next == 1) {
reloc += 254;
goto again;
}
else reloc += next;
reloc_pos = buf_pos;
buf_pos = old_bufpos;
return reloc;
}
}
void load_binfile (const char *bin_filename)
{
s32 reloc, next, pos, code_end, len, i = 0;
FILE *f;
if ((f = fopen (bin_filename, "r")) == NULL) {
fprintf (stderr, "Error opening 68k-binary '%s'\n", bin_filename);
//SDL_Quit ();
exit (-2);
}
fseek (f, 0, SEEK_END);
len = ftell (f);
fseek (f, 0, SEEK_SET);
assert (len+LOAD_BASE < MEM_SIZE);
fread (m68kram+LOAD_BASE, 1, len, f);
fclose (f);
buf_pos = LOAD_BASE + 2;
code_end = LOAD_BASE + 0x1c + rdlong (buf_pos);
i=0;
reloc = get_fixup (0, code_end);
while (reloc) {
i++;
pos = buf_pos;
/* address to be modified */
buf_pos = reloc;
next = rdlong (buf_pos);
next += LOAD_BASE;
wrlong (buf_pos, next);
if (next > code_end) {
fprintf (stderr, "Reloc 0x%x (0x%x) out of range..\n", next, reloc+LOAD_BASE);
}
buf_pos = pos;
reloc = get_fixup (reloc, code_end);
}
fprintf (stderr, "%s: 0x%x bytes (code end 0x%x), %d fixups; loaded at 0x%x.\n", bin_filename, len, code_end, i, LOAD_BASE);
}
#ifdef M68K_DEBUG
void m68k_print_line_no ()
{
printf ("Hello. At fe2.s line %d.\n", line_no);
fflush (stdout);
}
#endif /* M68K_DEBUG */
#endif /* PART1 */