|
|
1.1 root 1: /*
2: * Loads fe2.bin into ST memory and applies all the relocations.
3: * fe2.bin (and any crap as68k generates) is just plain atari
4: * prg format, so we could load anything that understood the
5: * stripped down and weirdified hardware of this VM.
6: */
7:
8: #include <SDL.h>
9: #include <SDL_types.h>
10:
11: #include "main.h"
12: #include "decode.h"
13: #include "dialog.h"
14: #include "file.h"
15: #include "m68000.h"
16: #include "memAlloc.h"
17: #include "stMemory.h"
18: #include "loadfe2.h"
19:
20: static void *fe2file;
21: static long len;
22: static long code_end;
23: static int buf_pos;
24:
25: static int rd_int ()
26: {
27: int val = STMemory_Swap68000Long (*(Uint32 *)(fe2file+buf_pos));
28: buf_pos += 4;
29: return val;
30: }
31:
32: static unsigned char rd_byte ()
33: {
34: unsigned char val = *(unsigned char*)(fe2file+buf_pos);
35: buf_pos++;
36: return val;
37: }
38:
39: static int get_fixup (int reloc)
40: {
41: int old_bufpos;
42: int next;
43: static int reloc_pos;
44:
45: old_bufpos = buf_pos;
46: if (reloc == 0) {
47: buf_pos = code_end;
48: reloc = rd_int ();
49: reloc_pos = buf_pos;
50: buf_pos = old_bufpos;
51: if (reloc == 0) return 0;
52: else return reloc + FE2BASE;
53: } else {
54: buf_pos = reloc_pos;
55: again:
56: next = rd_byte ();
57: if (next == 0) {
58: buf_pos = old_bufpos;
59: return 0;
60: } else if (next == 1) {
61: reloc += 254;
62: goto again;
63: }
64: else reloc += next;
65: reloc_pos = buf_pos;
66: buf_pos = old_bufpos;
67: return reloc;
68: }
69: }
70:
71: void LoadFE2 ()
72: {
73: int reloc, next, pos, i = 0;
74: fe2file = File_Read ("fe2.bin", NULL, &len, NULL);
75:
76: if (!len) {
77: printf ("Error loading fe2.bin\n");
78: SDL_Quit ();
79: exit (-2);
80: }
81: buf_pos = 2;
82: code_end = 0x1c + rd_int ();
83:
84: reloc = FE2BASE;
85: for (i=0x1c; i<code_end; i++) {
86: STMemory_WriteByte (reloc, *(char *)(fe2file+i));
87: reloc++;
88: }
89:
90: i=0;
91: reloc = get_fixup (0);
92: while (reloc) {
93: i++;
94: pos = buf_pos;
95: /* address to be modified */
96: buf_pos = reloc;
97: next = STMemory_ReadLong (buf_pos);
98: next += FE2BASE;
99: STMemory_WriteLong (buf_pos, next);
100: if (next > code_end+FE2BASE) {
101: printf ("Reloc 0x%x (0x%x) out of range..\n", next, reloc+FE2BASE);
102: }
103: buf_pos = pos;
104: reloc = get_fixup (reloc);
105: }
106: printf ("fe2.bin 0x%lx bytes (code end 0x%lx), %d fixups; loaded at 0x%x.\n", len, code_end, i, FE2BASE);
107:
108: Memory_Free (fe2file);
109: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.