1 /*****************************************************************************/
5 /* Binary code management */
9 /* (C) 2000-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
50 /*****************************************************************************/
52 /*****************************************************************************/
56 unsigned char CodeBuf [0x10000]; /* Code buffer */
57 unsigned long CodeStart; /* Start address */
58 unsigned long CodeEnd; /* End address */
59 unsigned long PC; /* Current PC */
63 /*****************************************************************************/
65 /*****************************************************************************/
70 /* Load the code from the given file */
72 long Count, MaxCount, Size;
76 PRECONDITION (StartAddr < 0x10000);
79 F = fopen (InFile, "rb");
81 Error ("Cannot open `%s': %s", InFile, strerror (errno));
84 /* Seek to the end to get the size of the file */
85 if (fseek (F, 0, SEEK_END) != 0) {
86 Error ("Cannot seek on file `%s': %s", InFile, strerror (errno));
90 /* The input offset must be smaller than the size */
92 if (InputOffs >= Size) {
93 Error ("Input offset is greater than file size");
96 /* Use a zero offset */
100 /* Seek to the input offset and correct size to contain the remainder of
103 if (fseek (F, InputOffs, SEEK_SET) != 0) {
104 Error ("Cannot seek on file `%s': %s", InFile, strerror (errno));
108 /* Limit the size to the maximum input size if one is given */
109 if (InputSize >= 0) {
110 if (InputSize > Size) {
111 Error ("Input size is greater than what is available");
116 /* If the start address was not given, set it so that the code loads to
117 * 0x10000 - Size. This is a reasonable default assuming that the file
118 * is a ROM that contains the hardware vectors at $FFFA.
121 if (Size > 0x10000) {
124 StartAddr = 0x10000 - Size;
128 /* Calculate the maximum code size */
129 MaxCount = 0x10000 - StartAddr;
131 /* Check if the size is larger than what we can read */
133 Error ("Nothing to read from input file `%s'", InFile);
135 if (Size > MaxCount) {
136 Warning ("File `%s' is too large, ignoring %ld bytes",
137 InFile, Size - MaxCount);
138 } else if (MaxCount > Size) {
139 MaxCount = (unsigned) Size;
142 /* Read from the file and remember the number of bytes read */
143 Count = fread (CodeBuf + StartAddr, 1, MaxCount, F);
144 if (ferror (F) || Count != MaxCount) {
145 Error ("Error reading from `%s': %s", InFile, strerror (errno));
151 /* Set the buffer variables */
152 CodeStart = PC = StartAddr;
153 CodeEnd = CodeStart + Count - 1; /* CodeEnd is inclusive */
158 unsigned char GetCodeByte (unsigned Addr)
159 /* Get a byte from the given address */
161 PRECONDITION (Addr <= CodeEnd);
162 return CodeBuf [Addr];
167 unsigned GetCodeDByte (unsigned Addr)
168 /* Get a dbyte from the given address */
170 unsigned Lo = GetCodeByte (Addr);
171 unsigned Hi = GetCodeByte (Addr+1);
172 return (Lo <<8) | Hi;
177 unsigned GetCodeWord (unsigned Addr)
178 /* Get a word from the given address */
180 unsigned Lo = GetCodeByte (Addr);
181 unsigned Hi = GetCodeByte (Addr+1);
182 return Lo | (Hi << 8);
187 unsigned long GetCodeDWord (unsigned Addr)
188 /* Get a dword from the given address */
190 unsigned long Lo = GetCodeWord (Addr);
191 unsigned long Hi = GetCodeWord (Addr+2);
192 return Lo | (Hi << 16);
197 unsigned GetRemainingBytes (void)
198 /* Return the number of remaining code bytes */
201 return (CodeEnd - PC + 1);
210 /* Return true if there are code bytes left */
212 return (PC <= CodeEnd);
217 void ResetCode (void)
218 /* Reset the code input to start over for the next pass */