]> git.sur5r.net Git - cc65/blob - src/da65/code.c
ff031d0aaecdd9a94a1dbe7326bb5e7b01ccae07
[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 static unsigned char CodeBuf [0x10000];         /* Code buffer */
56 static unsigned long CodeStart;                 /* Start address */
57 static unsigned long CodeEnd;                   /* End address */
58 static 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     FILE* F;
73
74
75     PRECONDITION (StartAddress < 0x10000);
76
77     /* Calculate the maximum code size */
78     MaxCount = 0x10000 - StartAddress;
79
80     /* Open the file */
81     F = fopen (Name, "rb");
82     if (F == 0) {
83         Error ("Cannot open `%s': %s", Name, strerror (errno));
84     }
85
86     /* Read from the file and remember the number of bytes read */
87     Count = fread (CodeBuf + StartAddress, 1, MaxCount, F);
88     if (ferror (F)) {
89         Error ("Error reading from `%s': %s", Name, strerror (errno));
90     }
91     if (Count == 0) {
92         Error ("File `%s' contains no data", Name);
93     }
94
95     /* Set the buffer variables */
96     CodeStart = PC = StartAddress;
97     CodeEnd = CodeStart + Count - 1;    /* CodeEnd is inclusive */
98 }
99
100
101
102 unsigned GetPC (void)
103 /* Get the current program counter */
104 {
105     return PC;
106 }
107
108
109
110 unsigned char PeekCodeByte (void)
111 /* Peek at the byte at the current PC */
112 {
113     PRECONDITION (PC <= CodeEnd);
114     return CodeBuf [PC];
115 }
116
117
118
119 unsigned char GetCodeByte (void)
120 /* Get a byte from the PC and increment it */
121 {
122     PRECONDITION (PC <= CodeEnd);
123     return CodeBuf [PC++];
124 }
125
126
127
128 unsigned GetCodeWord (void)
129 /* Get a word from the current PC and increment it */
130 {
131     unsigned Lo = GetCodeByte ();
132     unsigned Hi = GetCodeByte ();
133     return Lo | (Hi << 8);
134 }
135
136
137
138 unsigned GetRemainingBytes (void)
139 /* Return the number of remaining code bytes */
140 {
141     if (CodeEnd >= PC) {
142         return (CodeEnd - PC + 1);
143     } else {
144         return 0;
145     }
146 }
147
148
149
150 void ResetCode (void)
151 /* Reset the code input to start over for the next pass */
152 {
153     PC = CodeStart;
154 }
155
156
157