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.*;
27 public class AltosDebug extends AltosSerial {
29 public static final byte WR_CONFIG = 0x1d;
30 public static final byte RD_CONFIG = 0x24;
31 public static final byte CONFIG_TIMERS_OFF = (1 << 3);
32 public static final byte CONFIG_DMA_PAUSE = (1 << 2);
33 public static final byte CONFIG_TIMER_SUSPEND = (1 << 1);
34 public static final byte SET_FLASH_INFO_PAGE = (1 << 0);
36 public static final byte GET_PC = 0x28;
37 public static final byte READ_STATUS = 0x34;
38 public static final byte STATUS_CHIP_ERASE_DONE = (byte) (1 << 7);
39 public static final byte STATUS_PCON_IDLE = (1 << 6);
40 public static final byte STATUS_CPU_HALTED = (1 << 5);
41 public static final byte STATUS_POWER_MODE_0 = (1 << 4);
42 public static final byte STATUS_HALT_STATUS = (1 << 3);
43 public static final byte STATUS_DEBUG_LOCKED = (1 << 2);
44 public static final byte STATUS_OSCILLATOR_STABLE = (1 << 1);
45 public static final byte STATUS_STACK_OVERFLOW = (1 << 0);
47 public static final byte SET_HW_BRKPNT = 0x3b;
48 public static byte HW_BRKPNT_N(byte n) { return (byte) ((n) << 3); }
49 public static final byte HW_BRKPNT_N_MASK = (0x3 << 3);
50 public static final byte HW_BRKPNT_ENABLE = (1 << 2);
52 public static final byte HALT = 0x44;
53 public static final byte RESUME = 0x4c;
54 public static byte DEBUG_INSTR(byte n) { return (byte) (0x54|(n)); }
55 public static final byte STEP_INSTR = 0x5c;
56 public static byte STEP_REPLACE(byte n) { return (byte) (0x64|(n)); }
57 public static final byte GET_CHIP_ID = 0x68;
62 void ensure_debug_mode() {
70 void dump_memory(String header, int address, byte[] bytes, int start, int len) {
71 System.out.printf("%s\n", header);
72 for (int j = 0; j < len; j++) {
75 System.out.printf("\n");
76 System.out.printf ("%04x:", address + j);
78 System.out.printf(" %02x", bytes[start + j]);
80 System.out.printf("\n");
86 public void write_memory(int address, byte[] bytes, int start, int len) {
88 // dump_memory("write_memory", address, bytes, start, len);
89 printf("O %x %x\n", len, address);
90 for (int i = 0; i < len; i++)
91 printf("%02x", bytes[start + i]);
94 public void write_memory(int address, byte[] bytes) {
95 write_memory(address, bytes, 0, bytes.length);
101 public byte[] read_memory(int address, int length)
102 throws IOException, InterruptedException {
103 byte[] data = new byte[length];
107 printf("I %x %x\n", length, address);
111 String line = get_reply().trim();
112 if (!Altos.ishex(line) || line.length() % 2 != 0)
113 throw new IOException(
115 ("Invalid reply \"%s\"", line));
116 int this_time = line.length() / 2;
117 for (int j = 0; j < this_time; j++)
118 data[start + j] = (byte) ((Altos.fromhex(line.charAt(j*2)) << 4) +
119 Altos.fromhex(line.charAt(j*2+1)));
123 // dump_memory("read_memory", address, data, 0, length);
129 * Write raw bytes to the debug link using the 'P' command
131 public void write_bytes(byte[] bytes) throws IOException {
134 while (i < bytes.length) {
135 int this_time = bytes.length - i;
139 for (int j = 0; j < this_time; j++)
140 printf(" %02x", bytes[i+j]);
146 public void write_byte(byte b) throws IOException {
147 byte[] bytes = { b };
152 * Read raw bytes from the debug link using the 'G' command
154 public byte[] read_bytes(int length)
155 throws IOException, InterruptedException {
159 printf("G %x\n", length);
161 byte[] data = new byte[length];
163 String line = get_reply().trim();
164 String tokens[] = line.split("\\s+");
165 for (int j = 0; j < tokens.length; j++) {
166 if (!Altos.ishex(tokens[j]) ||
167 tokens[j].length() != 2)
168 throw new IOException(
170 ("Invalid read_bytes reply \"%s\"", line));
172 data[i + j] = (byte) Integer.parseInt(tokens[j], 16);
173 } catch (NumberFormatException ne) {
174 throw new IOException(
176 ("Invalid read_bytes reply \"%s\"", line));
184 public byte read_byte() throws IOException, InterruptedException {
185 return read_bytes(1)[0];
188 public byte debug_instr(byte[] instruction) throws IOException, InterruptedException {
189 byte[] command = new byte[1 + instruction.length];
190 command[0] = DEBUG_INSTR((byte) instruction.length);
191 for (int i = 0; i < instruction.length; i++)
192 command[i+1] = instruction[i];
193 write_bytes(command);
197 public byte resume() throws IOException, InterruptedException {
202 public int read_uint16() throws IOException, InterruptedException {
203 byte[] d = read_bytes(2);
204 return ((int) (d[0] & 0xff) << 8) | (d[1] & 0xff);
207 public int read_uint8() throws IOException, InterruptedException {
208 byte[] d = read_bytes(1);
209 return (int) (d[0] & 0xff);
212 public int get_chip_id() throws IOException, InterruptedException {
213 write_byte(GET_CHIP_ID);
214 return read_uint16();
217 public int get_pc() throws IOException, InterruptedException {
219 return read_uint16();
222 public byte read_status() throws IOException, InterruptedException {
223 write_byte(READ_STATUS);
227 static final byte LJMP = 0x02;
229 public void set_pc(int pc) throws IOException, InterruptedException {
230 byte high = (byte) (pc >> 8);
231 byte low = (byte) pc;
232 byte[] jump_mem = { LJMP, high, low };
233 debug_instr(jump_mem);
236 public boolean check_connection() throws IOException, InterruptedException {
237 byte reply = read_status();
238 if ((reply & STATUS_CHIP_ERASE_DONE) == 0)
240 if ((reply & STATUS_PCON_IDLE) != 0)
242 if ((reply & STATUS_POWER_MODE_0) == 0)
247 public AltosRomconfig romconfig() {
249 byte[] bytes = read_memory(0xa0, 10);
250 return new AltosRomconfig(bytes, 0);
251 } catch (IOException ie) {
252 } catch (InterruptedException ie) {
254 return new AltosRomconfig();
260 public void reset() {
264 public AltosDebug (AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {