1 /*****************************************************************************/
5 /* Binary code management */
9 /* (C) 2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 /*****************************************************************************/
49 /*****************************************************************************/
51 /*****************************************************************************/
55 unsigned char CodeBuf [0x10000]; /* Code buffer */
56 unsigned long CodeStart; /* Start address */
57 unsigned long CodeEnd; /* End address */
58 unsigned long PC; /* Current PC */
62 /*****************************************************************************/
64 /*****************************************************************************/
68 void LoadCode (const char* Name, unsigned long StartAddress)
69 /* Load the code from the given file */
71 long Count, MaxCount, Size;
75 PRECONDITION (StartAddress < 0x10000);
77 /* Calculate the maximum code size */
78 MaxCount = 0x10000 - StartAddress;
81 F = fopen (Name, "rb");
83 Error ("Cannot open `%s': %s", Name, strerror (errno));
86 /* Seek to the end to get the size of the file */
87 if (fseek (F, 0, SEEK_END) != 0) {
88 Error ("Cannot seek on file `%s': %s", Name, strerror (errno));
93 /* Check if the size is larger than what we can read */
95 Error ("File `%s' contains no data", Name);
97 if (Size > MaxCount) {
98 Warning ("File `%s' is too large, ignoring %ld bytes",
99 Name, Size - MaxCount);
100 } else if (MaxCount > Size) {
101 MaxCount = (unsigned) Size;
104 /* Read from the file and remember the number of bytes read */
105 Count = fread (CodeBuf + StartAddress, 1, MaxCount, F);
106 if (ferror (F) || Count != MaxCount) {
107 Error ("Error reading from `%s': %s", Name, strerror (errno));
113 /* Set the buffer variables */
114 CodeStart = PC = StartAddress;
115 CodeEnd = CodeStart + Count - 1; /* CodeEnd is inclusive */
120 unsigned char GetCodeByte (unsigned Addr)
121 /* Get a byte from the given address */
123 PRECONDITION (Addr <= CodeEnd);
124 return CodeBuf [Addr];
129 unsigned GetCodeWord (unsigned Addr)
130 /* Get a word from the given address */
132 unsigned Lo = GetCodeByte (Addr);
133 unsigned Hi = GetCodeByte (Addr+1);
134 return Lo | (Hi << 8);
139 unsigned long GetCodeDWord (unsigned Addr)
140 /* Get a dword from the given address */
142 unsigned long Lo = GetCodeWord (Addr);
143 unsigned long Hi = GetCodeWord (Addr+2);
144 return Lo | (Hi << 16);
149 unsigned GetRemainingBytes (void)
150 /* Return the number of remaining code bytes */
153 return (CodeEnd - PC + 1);
162 /* Return true if there are code bytes left */
164 return (PC <= CodeEnd);
169 void ResetCode (void)
170 /* Reset the code input to start over for the next pass */