1/*****************************************************************************/
2/* */
3/* code.c */
4/* */
5/* Binary code management */
6/* */
7/* */
8/* */
9/* (C) 2000-2003 Ullrich von Bassewitz */
10/* Roemerstrasse 52 */
11/* D-70794 Filderstadt */
12/* EMail: uz@cc65.org */
13/* */
14/* */
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. */
18/* */
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: */
22/* */
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 */
30/* distribution. */
31/* */
32/*****************************************************************************/
33
34
35
36#include <stdio.h>
37#include <string.h>
38#include <errno.h>
39
40/* common */
41#include "check.h"
42
43/* da65 */
44#include "code.h"
45#include "error.h"
46#include "global.h"
47
48
49
50/*****************************************************************************/
51/* Data */
52/*****************************************************************************/
53
54
55
56unsigned char CodeBuf [0x10000]; /* Code buffer */
57unsigned long CodeStart; /* Start address */
58unsigned long CodeEnd; /* End address */
59unsigned long PC; /* Current PC */
60
61
62
63/*****************************************************************************/
64/* Code */
65/*****************************************************************************/
66
67
68
69void LoadCode (void)
70/* Load the code from the given file */
71{
72 long Count, MaxCount, Size;
73 FILE* F;
74
75
76 PRECONDITION (StartAddr < 0x10000);
77
78 /* Open the file */
79 F = fopen (InFile, "rb");
80 if (F == 0) {
81 Error ("Cannot open '%s': %s", InFile, strerror (errno));
82 }
83
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));
87 }
88 Size = ftell (F);
89
90 /* The input offset must be smaller than the size */
91 if (InputOffs >= 0) {
92 if (InputOffs >= Size) {
93 Error ("Input offset is greater than file size");
94 }
95 } else {
96 /* Use a zero offset */
97 InputOffs = 0;
98 }
99
100 /* Seek to the input offset and correct size to contain the remainder of
101 ** the file.
102 */
103 if (fseek (F, InputOffs, SEEK_SET) != 0) {
104 Error ("Cannot seek on file '%s': %s", InFile, strerror (errno));
105 }
106 Size -= InputOffs;
107
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");
112 }
113 Size = InputSize;
114 }
115
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.
119 */
120 if (StartAddr < 0) {
121 if (Size > 0x10000) {
122 StartAddr = 0;
123 } else {
124 StartAddr = 0x10000 - Size;
125 }
126 }
127
128 /* Calculate the maximum code size */
129 MaxCount = 0x10000 - StartAddr;
130
131 /* Check if the size is larger than what we can read */
132 if (Size == 0) {
133 Error ("Nothing to read from input file '%s'", InFile);
134 }
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;
140 }
141
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));
146 }
147
148 /* Close the file */
149 fclose (F);
150
151 /* Set the buffer variables */
152 CodeStart = PC = StartAddr;
153 CodeEnd = CodeStart + Count - 1; /* CodeEnd is inclusive */
154}
155
156
157
158unsigned char GetCodeByte (unsigned Addr)
159/* Get a byte from the given address */
160{
161 PRECONDITION (Addr <= CodeEnd);
162 return CodeBuf [Addr];
163}
164
165
166
167unsigned GetCodeDByte (unsigned Addr)
168/* Get a dbyte from the given address */
169{
170 unsigned Lo = GetCodeByte (Addr);
171 unsigned Hi = GetCodeByte (Addr+1);
172 return (Lo <<8) | Hi;
173}
174
175
176
177unsigned GetCodeWord (unsigned Addr)
178/* Get a word from the given address */
179{
180 unsigned Lo = GetCodeByte (Addr);
181 unsigned Hi = GetCodeByte (Addr+1);
182 return Lo | (Hi << 8);
183}
184
185
186
187unsigned long GetCodeDWord (unsigned Addr)
188/* Get a dword from the given address */
189{
190 unsigned long Lo = GetCodeWord (Addr);
191 unsigned long Hi = GetCodeWord (Addr+2);
192 return Lo | (Hi << 16);
193}
194
195
196
197unsigned GetRemainingBytes (void)
198/* Return the number of remaining code bytes */
199{
200 if (CodeEnd >= PC) {
201 return (CodeEnd - PC + 1);
202 } else {
203 return 0;
204 }
205}
206
207
208
209int CodeLeft (void)
210/* Return true if there are code bytes left */
211{
212 return (PC <= CodeEnd);
213}
214
215
216
217void ResetCode (void)
218/* Reset the code input to start over for the next pass */
219{
220 PC = CodeStart;
221}
222