]> git.sur5r.net Git - cc65/blob - src/da65/code.c
Moved verbose output to a shared module in the common/ directory.
[cc65] / src / da65 / code.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  code.c                                   */
4 /*                                                                           */
5 /*                          Binary code management                           */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000      Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
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 "error.h"
45 #include "code.h"
46
47
48
49 /*****************************************************************************/
50 /*                                   Data                                    */
51 /*****************************************************************************/
52
53
54
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 */
59
60
61
62 /*****************************************************************************/
63 /*                                   Code                                    */
64 /*****************************************************************************/
65
66
67
68 void LoadCode (const char* Name, unsigned long StartAddress)
69 /* Load the code from the given file */
70 {
71     unsigned Count, MaxCount;
72     long Size;
73     FILE* F;
74
75
76     PRECONDITION (StartAddress < 0x10000);
77
78     /* Calculate the maximum code size */
79     MaxCount = 0x10000 - StartAddress;
80
81     /* Open the file */
82     F = fopen (Name, "rb");
83     if (F == 0) {
84         Error ("Cannot open `%s': %s", Name, strerror (errno));
85     }
86
87     /* Seek to the end to get the size of the file */
88     if (fseek (F, 0, SEEK_END) != 0) {
89         Error ("Cannot seek on file `%s': %s", Name, strerror (errno));
90     }
91     Size = ftell (F);
92     rewind (F);
93
94     /* Check if the size is larger than what we can read */
95     if (Size == 0) {
96         Error ("File `%s' contains no data", Name);
97     }
98     if (Size > MaxCount) {
99         Warning ("File `%s' is too large, ignoring %ld bytes",
100                  Name, Size - MaxCount);
101     } else if (MaxCount > Size) {
102         MaxCount = (unsigned) Size;
103     }
104
105     /* Read from the file and remember the number of bytes read */
106     Count = fread (CodeBuf + StartAddress, 1, MaxCount, F);
107     if (ferror (F) || Count != MaxCount) {
108         Error ("Error reading from `%s': %s", Name, strerror (errno));
109     }
110
111     /* Close the file */
112     fclose (F);
113
114     /* Set the buffer variables */
115     CodeStart = PC = StartAddress;
116     CodeEnd = CodeStart + Count - 1;    /* CodeEnd is inclusive */
117 }
118
119
120
121 unsigned char GetCodeByte (unsigned Addr)
122 /* Get a byte from the given address */
123 {
124     PRECONDITION (Addr <= CodeEnd);
125     return CodeBuf [Addr];
126 }
127
128
129
130 unsigned GetCodeWord (unsigned Addr)
131 /* Get a word from the given address */
132 {
133     unsigned Lo = GetCodeByte (Addr);
134     unsigned Hi = GetCodeByte (Addr+1);
135     return Lo | (Hi << 8);
136 }
137
138
139
140 unsigned long GetCodeDWord (unsigned Addr)
141 /* Get a dword from the given address */
142 {
143     unsigned long Lo = GetCodeWord (Addr);
144     unsigned long Hi = GetCodeWord (Addr+2);
145     return Lo | (Hi << 16);
146 }
147
148
149
150 unsigned GetRemainingBytes (void)
151 /* Return the number of remaining code bytes */
152 {
153     if (CodeEnd >= PC) {
154         return (CodeEnd - PC + 1);
155     } else {
156         return 0;
157     }
158 }
159
160
161
162 int CodeLeft (void)
163 /* Return true if there are code bytes left */
164 {
165     return (PC <= CodeEnd);
166 }
167
168
169
170 void ResetCode (void)
171 /* Reset the code input to start over for the next pass */
172 {
173     PC = CodeStart;
174 }
175
176
177