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.
22 import java.util.concurrent.LinkedBlockingQueue;
23 import java.util.LinkedList;
24 import java.util.Iterator;
25 import java.util.Arrays;
26 import org.altusmetrum.AltosLib.*;
28 class HexFileInputStream extends PushbackInputStream {
31 public HexFileInputStream(FileInputStream o) {
32 super(new BufferedInputStream(o));
36 public int read() throws IOException {
43 public void unread(int c) throws IOException {
51 class HexRecord implements Comparable {
57 static final int NORMAL = 0;
58 static final int EOF = 1;
59 static final int EXTENDED_ADDRESS = 2;
73 boolean ishex(int c) {
74 if ('0' <= c && c <= '9')
76 if ('a' <= c && c <= 'f')
78 if ('A' <= c && c <= 'F')
83 boolean isspace(int c) {
93 if ('0' <= c && c <= '9')
95 if ('a' <= c && c <= 'f')
97 if ('A' <= c && c <= 'F')
102 public byte checksum() {
106 got += (address >> 8) & 0xff;
107 got += (address ) & 0xff;
109 for (int i = 0; i < data.length; i++)
111 return (byte) (-got);
114 public int compareTo(Object other) {
115 HexRecord o = (HexRecord) other;
116 return address - o.address;
119 public String toString() {
120 return String.format("%04x: %02x (%d)", address, type, data.length);
123 public HexRecord(HexFileInputStream input) throws IOException {
124 read_state state = read_state.marker;
130 while (state != read_state.done) {
131 int c = input.read();
132 if (c < 0 && state != read_state.white)
133 throw new IOException(String.format("%d: Unexpected EOF", input.line));
139 throw new IOException("Missing ':'");
140 state = read_state.length;
150 throw new IOException(String.format("Non-hex char '%c'", c));
151 hex = hex << 4 | fromhex(c);
158 data = new byte[hex];
159 state = read_state.address;
164 state = read_state.type;
170 state = read_state.data;
172 state = read_state.checksum;
177 data[ndata] = (byte) hex;
180 if (ndata == data.length)
181 state = read_state.checksum;
184 checksum = (byte) hex;
185 state = read_state.newline;
193 if (c != '\n' && c != '\r')
194 throw new IOException("Missing newline");
195 state = read_state.white;
200 state = read_state.done;
207 got_checksum = checksum();
208 if (got_checksum != checksum)
209 throw new IOException(String.format("Invalid checksum (read 0x%02x computed 0x%02x)\n",
210 checksum, got_checksum));
214 public class AltosHexfile {
218 public byte get_byte(int a) {
219 return data[a - address];
222 public AltosHexfile(FileInputStream file) throws IOException {
223 HexFileInputStream input = new HexFileInputStream(file);
224 LinkedList<HexRecord> record_list = new LinkedList<HexRecord>();
225 boolean done = false;
228 HexRecord record = new HexRecord(input);
230 if (record.type == HexRecord.EOF)
233 record_list.add(record);
235 HexRecord[] records = record_list.toArray(new HexRecord[0]);
236 Arrays.sort(records);
237 if (records.length > 0) {
238 int base = records[0].address;
239 int bound = records[records.length-1].address +
240 records[records.length-1].data.length;
242 data = new byte[bound - base];
244 Arrays.fill(data, (byte) 0xff);
246 /* Paint the records into the new array */
247 for (int i = 0; i < records.length; i++) {
248 for (int j = 0; j < records[i].data.length; j++)
249 data[records[i].address - base + j] = records[i].data[j];