1 /*****************************************************************************/
5 /* Module to handle the Atari XEX binary format */
9 /* (C) 2018 Daniel Serpell */
12 /* This software is provided 'as-is', without any expressed or implied */
13 /* warranty. In no event will the authors be held liable for any damages */
14 /* arising from the use of this software. */
16 /* Permission is granted to anyone to use this software for any purpose, */
17 /* including commercial applications, and to alter it and redistribute it */
18 /* freely, subject to the following restrictions: */
20 /* 1. The origin of this software must not be misrepresented; you must not */
21 /* claim that you wrote the original software. If you use this software */
22 /* in a product, an acknowledgment in the product documentation would be */
23 /* appreciated but is not required. */
24 /* 2. Altered source versions must be plainly marked as such, and must not */
25 /* be misrepresented as being the original software. */
26 /* 3. This notice may not be removed or altered from any source */
29 /*****************************************************************************/
38 #include "alignment.h"
57 /*****************************************************************************/
59 /*****************************************************************************/
64 unsigned Undef; /* Count of undefined externals */
65 FILE* F; /* Output file */
66 const char* Filename; /* Name of output file */
67 Import* RunAd; /* Run Address */
72 /*****************************************************************************/
74 /*****************************************************************************/
78 XexDesc* NewXexDesc (void)
79 /* Create a new XEX format descriptor */
81 /* Allocate memory for a new XexDesc struct */
82 XexDesc* D = xmalloc (sizeof (XexDesc));
84 /* Initialize the fields */
90 /* Return the created struct */
96 void FreeXexDesc (XexDesc* D)
97 /* Free a XEX format descriptor */
104 void XexSetRunAd (XexDesc* D, Import *RunAd)
105 /* Set the RUNAD export */
112 static unsigned XexWriteExpr (ExprNode* E, int Signed, unsigned Size,
113 unsigned long Offs attribute ((unused)),
115 /* Called from SegWrite for an expression. Evaluate the expression, check the
116 ** range and write the expression value to the file.
119 /* There's a predefined function to handle constant expressions */
120 return SegWriteConstExpr (((XexDesc*)Data)->F, E, Signed, Size);
125 static void PrintNumVal (const char* Name, unsigned long V)
126 /* Print a numerical value for debugging */
128 Print (stdout, 2, " %s = 0x%lx\n", Name, V);
133 static void XexWriteMem (XexDesc* D, MemoryArea* M)
134 /* Write the segments of one memory area to a file */
138 /* Get the start address and size of this memory area */
139 unsigned long Addr = M->Start;
141 /* Real size of the memory area, either the FillLevel or the Size */
142 unsigned long Size = M->FillLevel;
143 if ((M->Flags & MF_FILL) != 0 && M->FillLevel < M->Size)
147 if (ftell (D->F) == 0)
148 Write16(D->F, 0xFFFF);
150 Write16(D->F, Addr + Size - 1);
152 /* Walk over all segments in this memory area */
153 for (I = 0; I < CollCount (&M->SegList); ++I) {
157 /* Get the segment */
158 SegDesc* S = CollAtUnchecked (&M->SegList, I);
160 /* Keep the user happy */
161 Print (stdout, 1, " Allocating `%s'\n", GetString (S->Name));
163 /* Writes do only occur in the load area and not for BSS segments */
164 DoWrite = (S->Flags & SF_BSS) == 0 && /* No BSS segment */
165 S->Load == M && /* LOAD segment */
166 S->Seg->Dumped == 0; /* Not already written */
168 /* If this is the run memory area, we must apply run alignment. If
169 ** this is not the run memory area but the load memory area (which
170 ** means that both are different), we must apply load alignment.
171 ** Beware: DoWrite may be true even if this is the run memory area,
172 ** because it may be also the load memory area.
176 /* Handle ALIGN and OFFSET/START */
177 if (S->Flags & SF_ALIGN) {
178 /* Align the address */
179 unsigned long NewAddr = AlignAddr (Addr, S->RunAlignment);
180 if (DoWrite || (M->Flags & MF_FILL) != 0) {
181 WriteMult (D->F, M->FillVal, NewAddr - Addr);
182 PrintNumVal ("SF_ALIGN", NewAddr - Addr);
185 } else if (S->Flags & (SF_OFFSET | SF_START)) {
186 unsigned long NewAddr = S->Addr;
187 if (S->Flags & SF_OFFSET) {
188 /* It's an offset, not a fixed address, make an address */
191 if (DoWrite || (M->Flags & MF_FILL) != 0) {
192 /* "overwrite" segments are not supported */
193 if (S->Flags & SF_OVERWRITE) {
194 Error ("ATARI file format does not support overwrite for segment '%s'.",
195 GetString (S->Name));
197 WriteMult (D->F, M->FillVal, NewAddr-Addr);
198 PrintNumVal ("SF_OFFSET", NewAddr - Addr);
204 } else if (S->Load == M) {
206 /* Handle ALIGN_LOAD */
207 if (S->Flags & SF_ALIGN_LOAD) {
208 /* Align the address */
209 unsigned long NewAddr = AlignAddr (Addr, S->LoadAlignment);
210 if (DoWrite || (M->Flags & MF_FILL) != 0) {
211 WriteMult (D->F, M->FillVal, NewAddr - Addr);
212 PrintNumVal ("SF_ALIGN_LOAD", NewAddr - Addr);
219 /* Now write the segment to disk if it is not a BSS type segment and
220 ** if the memory area is the load area.
223 unsigned long P = ftell (D->F);
224 SegWrite (D->Filename, D->F, S->Seg, XexWriteExpr, D);
225 PrintNumVal ("Wrote", (unsigned long) (ftell (D->F) - P));
226 } else if (M->Flags & MF_FILL) {
227 WriteMult (D->F, S->Seg->FillVal, S->Seg->Size);
228 PrintNumVal ("Filled", (unsigned long) S->Seg->Size);
231 /* If this was the load memory area, mark the segment as dumped */
236 /* Calculate the new address */
237 Addr += S->Seg->Size;
240 /* If a fill was requested, fill the remaining space */
241 if ((M->Flags & MF_FILL) != 0 && M->FillLevel < M->Size) {
242 unsigned long ToFill = M->Size - M->FillLevel;
243 Print (stdout, 2, " Filling 0x%lx bytes with 0x%02x\n",
245 WriteMult (D->F, M->FillVal, ToFill);
246 M->FillLevel = M->Size;
253 static int XexUnresolved (unsigned Name attribute ((unused)), void* D)
254 /* Called if an unresolved symbol is encountered */
256 /* Unresolved symbols are an error in XEX format. Bump the counter
257 ** and return zero telling the caller that the symbol is indeed
260 ((XexDesc*) D)->Undef++;
266 void XexWriteTarget (XexDesc* D, struct File* F)
267 /* Write a XEX output file */
271 /* Place the filename in the control structure */
272 D->Filename = GetString (F->Name);
274 /* Check for unresolved symbols. The function XexUnresolved is called
275 ** if we get an unresolved symbol.
277 D->Undef = 0; /* Reset the counter */
278 CheckUnresolvedImports (XexUnresolved, D);
280 /* We had unresolved symbols, cannot create output file */
281 Error ("%u unresolved external(s) found - cannot create output file", D->Undef);
285 D->F = fopen (D->Filename, "wb");
287 Error ("Cannot open `%s': %s", D->Filename, strerror (errno));
290 /* Keep the user happy */
291 Print (stdout, 1, "Opened `%s'...\n", D->Filename);
293 /* Dump all memory areas */
294 for (I = 0; I < CollCount (&F->MemoryAreas); ++I) {
296 MemoryArea* M = CollAtUnchecked (&F->MemoryAreas, I);
297 Print (stdout, 1, " XEX Dumping `%s'\n", GetString (M->Name));
301 /* Write RUNAD at file end */
303 Write16 (D->F, 0x2E0);
304 Write16 (D->F, 0x2E1);
305 Write16 (D->F, GetExportVal (D->RunAd->Exp));
309 if (fclose (D->F) != 0) {
310 Error ("Cannot write to `%s': %s", D->Filename, strerror (errno));
313 /* Reset the file and filename */