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.
20 import altosui.AltosHexfile;
22 public class AltosRomconfig {
26 public int serial_number;
27 public int radio_calibration;
29 static int get_int(byte[] bytes, int start, int len) {
33 v = v | ((((int) bytes[start]) & 0xff) << o);
41 static void put_int(int value, byte[] bytes, int start, int len) {
43 bytes[start] = (byte) (value & 0xff);
50 public AltosRomconfig(byte[] bytes, int offset) {
51 version = get_int(bytes, offset + 0, 2);
52 check = get_int(bytes, offset + 2, 2);
53 if (check == (~version & 0xffff)) {
56 serial_number = get_int(bytes, offset + 4, 2);
57 radio_calibration = get_int(bytes, offset + 6, 4);
64 public AltosRomconfig(AltosHexfile hexfile) {
65 this(hexfile.data, 0xa0 - hexfile.address);
68 public void write(byte[] bytes, int offset) throws IOException {
70 throw new IOException("rom configuration invalid");
72 if (offset < 0 || bytes.length < offset + 10)
73 throw new IOException("image cannot contain rom config");
75 AltosRomconfig existing = new AltosRomconfig(bytes, offset);
77 throw new IOException("image does not contain existing rom config");
79 switch (existing.version) {
81 put_int(serial_number, bytes, offset + 4, 2);
82 put_int(radio_calibration, bytes, offset + 6, 4);
87 public void write (AltosHexfile hexfile) throws IOException {
88 write(hexfile.data, 0xa0 - hexfile.address);
89 new AltosRomconfig(hexfile);
92 public AltosRomconfig(int in_serial_number, int in_radio_calibration) {
95 check = (~version & 0xffff);
96 serial_number = in_serial_number;
97 radio_calibration = in_radio_calibration;
100 public boolean valid() {
101 return valid && serial_number != 0;
104 public AltosRomconfig() {