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;
27 class HexFileInputStream extends PushbackInputStream {
30 public HexFileInputStream(FileInputStream o) {
31 super(new BufferedInputStream(o));
35 public int read() throws IOException {
42 public void unread(int c) throws IOException {
50 class HexRecord implements Comparable {
56 static final int NORMAL = 0;
57 static final int EOF = 1;
58 static final int EXTENDED_ADDRESS = 2;
72 boolean ishex(int c) {
73 if ('0' <= c && c <= '9')
75 if ('a' <= c && c <= 'f')
77 if ('A' <= c && c <= 'F')
82 boolean isspace(int c) {
92 if ('0' <= c && c <= '9')
94 if ('a' <= c && c <= 'f')
96 if ('A' <= c && c <= 'F')
101 public byte checksum() {
105 got += (address >> 8) & 0xff;
106 got += (address ) & 0xff;
108 for (int i = 0; i < data.length; i++)
110 return (byte) (-got);
113 public int compareTo(Object other) {
114 HexRecord o = (HexRecord) other;
115 return address - o.address;
118 public String toString() {
119 return String.format("%04x: %02x (%d)", address, type, data.length);
122 public HexRecord(HexFileInputStream input) throws IOException {
123 read_state state = read_state.marker;
129 while (state != read_state.done) {
130 int c = input.read();
131 if (c < 0 && state != read_state.white)
132 throw new IOException(String.format("%d: Unexpected EOF", input.line));
138 throw new IOException("Missing ':'");
139 state = read_state.length;
149 throw new IOException(String.format("Non-hex char '%c'", c));
150 hex = hex << 4 | fromhex(c);
157 data = new byte[hex];
158 state = read_state.address;
163 state = read_state.type;
169 state = read_state.data;
171 state = read_state.checksum;
176 data[ndata] = (byte) hex;
179 if (ndata == data.length)
180 state = read_state.checksum;
183 checksum = (byte) hex;
184 state = read_state.newline;
192 if (c != '\n' && c != '\r')
193 throw new IOException("Missing newline");
194 state = read_state.white;
199 state = read_state.done;
206 got_checksum = checksum();
207 if (got_checksum != checksum)
208 throw new IOException(String.format("Invalid checksum (read 0x%02x computed 0x%02x)\n",
209 checksum, got_checksum));
213 public class AltosHexfile {
217 public byte get_byte(int a) {
218 return data[a - address];
221 public AltosHexfile(FileInputStream file) throws IOException {
222 HexFileInputStream input = new HexFileInputStream(file);
223 LinkedList<HexRecord> record_list = new LinkedList<HexRecord>();
224 boolean done = false;
227 HexRecord record = new HexRecord(input);
229 if (record.type == HexRecord.EOF)
232 record_list.add(record);
234 HexRecord[] records = record_list.toArray(new HexRecord[0]);
235 Arrays.sort(records);
236 if (records.length > 0) {
237 int base = records[0].address;
238 int bound = records[records.length-1].address +
239 records[records.length-1].data.length;
241 data = new byte[bound - base];
243 Arrays.fill(data, (byte) 0xff);
245 /* Paint the records into the new array */
246 for (int i = 0; i < records.length; i++) {
247 for (int j = 0; j < records[i].data.length; j++)
248 data[records[i].address - base + j] = records[i].data[j];