Debian Codegraph @

terps/git/memory.c

raw symbols SCIP
1 // $Id: memory.c,v 1.11 2004/01/25 21:04:19 iain Exp $
2
3 #include "git.h"
4 #include <stdlib.h>
5 #include <string.h>
6
7 const git_uint8 * gInitMem;
8 git_uint8 * gMem;
9
10 git_uint32 gRamStart;
11 git_uint32 gExtStart;
12 git_uint32 gEndMem;
13 git_uint32 gOriginalEndMem;
14
15 void initMemory (const git_uint8 * gamefile, git_uint32 size)
16 {
17 // Make sure we have at least enough
18 // data for the standard glulx header.
19
20 if (size < 36)
21 fatalError("This file is too small to be a valid glulx gamefile");
22
23 gInitMem = gamefile;
24
25 // Check the magic number. From the spec:
26 // * Magic number: 47 6C 75 6C, which is to say ASCII 'Glul'.
27
28 if (read32 (gInitMem + 0) != 0x476c756c)
29 fatalError("This is not a glulx game file");
30
31 // Load the correct values for ramstart, extstart and endmem.
32
33 gRamStart = read32 (gInitMem + 8);
34 gExtStart = read32 (gInitMem + 12);
35 gOriginalEndMem = gEndMem = read32 (gInitMem + 16);
36
37 // Make sure the values are sane.
38
39 if (gRamStart < 36)
40 fatalError ("Bad header (RamStart is too low)");
41
42 if (gRamStart > size)
43 fatalError ("Bad header (RamStart is bigger than the entire gamefile)");
44
45 if (gExtStart > size)
46 fatalError ("Bad header (ExtStart is bigger than the entire gamefile)");
47
48 if (gExtStart < gRamStart)
49 fatalError ("Bad header (ExtStart is lower than RamStart)");
50
51 if (gEndMem < gExtStart)
52 fatalError ("Bad header (EndMem is lower than ExtStart)");
53
54 if (gRamStart & 255)
55 fatalError ("Bad header (RamStart is not a multiple of 256)");
56
57 if (gExtStart & 255)
58 fatalError ("Bad header (ExtStart is not a multiple of 256)");
59
60 if (gEndMem & 255)
61 fatalError ("Bad header (EndMem is not a multiple of 256)");
62
63 // Allocate the RAM. We'll duplicate the last few bytes of ROM
64 // here so that reads which cross the ROM/RAM boundary don't fail.
65
66 gMem = malloc (gEndMem);
67 if (gMem == NULL)
68 fatalError ("Failed to allocate game RAM");
69
70 // Copy the initial memory contents.
71 memcpy (gMem, gInitMem, gExtStart);
72
73 // Zero out the extended RAM.
74 memset (gMem + gExtStart, 0, gEndMem - gExtStart);
75 }
76
77 int verifyMemory ()
78 {
79 git_uint32 checksum = 0;
80
81 git_uint32 n;
82 for (n = 0 ; n < gExtStart ; n += 4)
83 checksum += read32 (gInitMem + n);
84
85 checksum -= read32 (gInitMem + 32);
86 return (checksum == read32 (gInitMem + 32)) ? 0 : 1;
87 }
88
89 int resizeMemory (git_uint32 newSize, int isInternal)
90 {
91 git_uint8* newMem;
92
93 if (newSize == gEndMem)
94 return 0; // Size is not changed.
95 if (!isInternal && heap_is_active())
96 fatalError ("Cannot resize Glulx memory space while heap is active.");
97 if (newSize < gOriginalEndMem)
98 fatalError ("Cannot resize Glulx memory space smaller than it started.");
99 if (newSize & 0xFF)
100 fatalError ("Can only resize Glulx memory space to a 256-byte boundary.");
101
102 newMem = realloc(gMem, newSize);
103 if (!newMem)
104 {
105 return 1; // Failed to extend memory.
106 }
107 if (newSize > gEndMem)
108 memset (newMem + gEndMem, 0, newSize - gEndMem);
109
110 gMem = newMem;
111 gEndMem = newSize;
112 return 0;
113 }
114
115 void resetMemory (git_uint32 protectPos, git_uint32 protectSize)
116 {
117 git_uint32 protectEnd = protectPos + protectSize;
118 git_uint32 i;
119
120 // Deactivate the heap (if it was active).
121 heap_clear();
122
123 gEndMem = gOriginalEndMem;
124
125 // Copy the initial contents of RAM.
126 for (i = gRamStart; i < gExtStart; ++i)
127 {
128 if (i >= protectEnd || i < protectPos)
129 gMem [i] = gInitMem [i];
130 }
131
132 // Zero out the extended RAM.
133 for (i = gExtStart; i < gEndMem; ++i)
134 {
135 if (i >= protectEnd || i < protectPos)
136 gMem [i] = 0;
137 }
138 }
139
140 void shutdownMemory ()
141 {
142 // We didn't allocate the ROM, so we
143 // only need to dispose of the RAM.
144
145 free (gMem);
146
147 // Zero out all our globals.
148
149 gRamStart = gExtStart = gEndMem = gOriginalEndMem = 0;
150 gInitMem = gMem = NULL;
151 }
152
153 void memReadError (git_uint32 address)
154 {
155 fatalError ("Out-of-bounds memory access");
156 }
157
158 void memWriteError (git_uint32 address)
159 {
160 fatalError ("Out-of-bounds memory access");
161 }