2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 package org.altusmetrum.altoslib_1;
21 import java.util.LinkedList;
22 import java.util.Arrays;
24 class HexFileInputStream extends PushbackInputStream {
27 public HexFileInputStream(FileInputStream o) {
28 super(new BufferedInputStream(o));
32 public int read() throws IOException {
39 public void unread(int c) throws IOException {
47 class HexRecord implements Comparable<Object> {
53 static final int NORMAL = 0;
54 static final int EOF = 1;
55 static final int EXTENDED_ADDRESS = 2;
69 boolean ishex(int c) {
70 if ('0' <= c && c <= '9')
72 if ('a' <= c && c <= 'f')
74 if ('A' <= c && c <= 'F')
79 boolean isspace(int c) {
89 if ('0' <= c && c <= '9')
91 if ('a' <= c && c <= 'f')
93 if ('A' <= c && c <= 'F')
98 public byte checksum() {
102 got += (address >> 8) & 0xff;
103 got += (address ) & 0xff;
105 for (int i = 0; i < data.length; i++)
107 return (byte) (-got);
110 public int compareTo(Object other) {
111 HexRecord o = (HexRecord) other;
112 return address - o.address;
115 public String toString() {
116 return String.format("%04x: %02x (%d)", address, type, data.length);
119 public HexRecord(HexFileInputStream input) throws IOException {
120 read_state state = read_state.marker;
126 while (state != read_state.done) {
127 int c = input.read();
128 if (c < 0 && state != read_state.white)
129 throw new IOException(String.format("%d: Unexpected EOF", input.line));
135 throw new IOException("Missing ':'");
136 state = read_state.length;
146 throw new IOException(String.format("Non-hex char '%c'", c));
147 hex = hex << 4 | fromhex(c);
154 data = new byte[hex];
155 state = read_state.address;
160 state = read_state.type;
166 state = read_state.data;
168 state = read_state.checksum;
173 data[ndata] = (byte) hex;
176 if (ndata == data.length)
177 state = read_state.checksum;
180 checksum = (byte) hex;
181 state = read_state.newline;
189 if (c != '\n' && c != '\r')
190 throw new IOException("Missing newline");
191 state = read_state.white;
196 state = read_state.done;
203 got_checksum = checksum();
204 if (got_checksum != checksum)
205 throw new IOException(String.format("Invalid checksum (read 0x%02x computed 0x%02x)\n",
206 checksum, got_checksum));
210 public class AltosHexfile {
214 public byte get_byte(int a) {
215 return data[a - address];
218 public AltosHexfile(FileInputStream file) throws IOException {
219 HexFileInputStream input = new HexFileInputStream(file);
220 LinkedList<HexRecord> record_list = new LinkedList<HexRecord>();
221 boolean done = false;
224 HexRecord record = new HexRecord(input);
226 if (record.type == HexRecord.EOF)
229 record_list.add(record);
232 long extended_addr = 0;
233 long base = 0xffffffff;
234 long bound = 0x00000000;
235 for (HexRecord record : record_list) {
236 switch (record.type) {
238 long addr = extended_addr + record.address;
239 long r_bound = addr + record.data.length;
248 if (record.data.length != 2)
249 throw new IOException("invalid extended segment address record");
250 extended_addr = ((record.data[0] << 8) + (record.data[1])) << 4;
253 if (record.data.length != 2)
254 throw new IOException("invalid extended segment address record");
255 extended_addr = ((record.data[0] << 8) + (record.data[1])) << 16;
258 throw new IOException ("invalid hex record type");
263 throw new IOException("invalid hex file");
265 if (bound - base > 4 * 1024 * 1024)
266 throw new IOException("hex file too large");
268 data = new byte[(int) (bound - base)];
269 address = (int) base;
270 Arrays.fill(data, (byte) 0xff);
272 /* Paint the records into the new array */
273 for (HexRecord record : record_list) {
274 switch (record.type) {
276 long addr = extended_addr + record.address;
277 long r_bound = addr + record.data.length;
278 for (int j = 0; j < record.data.length; j++)
279 data[(int) (addr - base) + j] = record.data[j];
284 if (record.data.length != 2)
285 throw new IOException("invalid extended segment address record");
286 extended_addr = ((record.data[0] << 8) + (record.data[1])) << 4;
289 if (record.data.length != 2)
290 throw new IOException("invalid extended segment address record");
291 extended_addr = ((record.data[0] << 8) + (record.data[1])) << 16;
294 throw new IOException ("invalid hex record type");