altosui: flush replies from serial link when entering debug mode
[fw/altos] / ao-tools / altosui / AltosDebug.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package altosui;
19
20 import java.lang.*;
21 import java.io.*;
22 import java.util.concurrent.LinkedBlockingQueue;
23 import java.util.LinkedList;
24 import java.util.Iterator;
25 import altosui.AltosSerial;
26 import altosui.AltosRomconfig;
27
28 public class AltosDebug extends AltosSerial {
29
30         public static final byte WR_CONFIG =            0x1d;
31         public static final byte RD_CONFIG =            0x24;
32         public static final byte CONFIG_TIMERS_OFF =            (1 << 3);
33         public static final byte CONFIG_DMA_PAUSE =             (1 << 2);
34         public static final byte CONFIG_TIMER_SUSPEND =         (1 << 1);
35         public static final byte SET_FLASH_INFO_PAGE =          (1 << 0);
36
37         public static final byte GET_PC =               0x28;
38         public static final byte READ_STATUS =          0x34;
39         public static final byte STATUS_CHIP_ERASE_DONE =       (byte) (1 << 7);
40         public static final byte STATUS_PCON_IDLE =             (1 << 6);
41         public static final byte STATUS_CPU_HALTED =            (1 << 5);
42         public static final byte STATUS_POWER_MODE_0 =          (1 << 4);
43         public static final byte STATUS_HALT_STATUS =           (1 << 3);
44         public static final byte STATUS_DEBUG_LOCKED =          (1 << 2);
45         public static final byte STATUS_OSCILLATOR_STABLE =     (1 << 1);
46         public static final byte STATUS_STACK_OVERFLOW =        (1 << 0);
47
48         public static final byte SET_HW_BRKPNT =        0x3b;
49         public static byte       HW_BRKPNT_N(byte n)    { return (byte) ((n) << 3); }
50         public static final byte HW_BRKPNT_N_MASK =             (0x3 << 3);
51         public static final byte HW_BRKPNT_ENABLE =             (1 << 2);
52
53         public static final byte HALT =                 0x44;
54         public static final byte RESUME =               0x4c;
55         public static       byte DEBUG_INSTR(byte n)    { return (byte) (0x54|(n)); }
56         public static final byte STEP_INSTR =           0x5c;
57         public static        byte STEP_REPLACE(byte n)  { return  (byte) (0x64|(n)); }
58         public static final byte GET_CHIP_ID =          0x68;
59
60
61         static boolean ishex(int c) {
62                 if ('0' <= c && c <= '9')
63                         return true;
64                 if ('a' <= c && c <= 'f')
65                         return true;
66                 if ('A' <= c && c <= 'F')
67                         return true;
68                 return false;
69         }
70
71         static boolean ishex(String s) {
72                 for (int i = 0; i < s.length(); i++)
73                         if (!ishex(s.charAt(i)))
74                                 return false;
75                 return true;
76         }
77         static boolean isspace(int c) {
78                 switch (c) {
79                 case ' ':
80                 case '\t':
81                         return true;
82                 }
83                 return false;
84         }
85
86         static int fromhex(int c) {
87                 if ('0' <= c && c <= '9')
88                         return c - '0';
89                 if ('a' <= c && c <= 'f')
90                         return c - 'a' + 10;
91                 if ('A' <= c && c <= 'F')
92                         return c - 'A' + 10;
93                 return -1;
94         }
95
96         boolean debug_mode;
97
98         void ensure_debug_mode() {
99                 if (!debug_mode) {
100                         printf("m 0\nD\n");
101                         flush_reply();
102                         debug_mode = true;
103                 }
104         }
105
106         void dump_memory(String header, int address, byte[] bytes, int start, int len) {
107                 System.out.printf("%s\n", header);
108                 for (int j = 0; j < len; j++) {
109                         if ((j & 15) == 0) {
110                                 if (j != 0)
111                                         System.out.printf("\n");
112                                 System.out.printf ("%04x:", address + j);
113                         }
114                         System.out.printf(" %02x", bytes[start + j]);
115                 }
116                 System.out.printf("\n");
117         }
118
119         /*
120          * Write target memory
121          */
122         public void write_memory(int address, byte[] bytes, int start, int len) {
123                 ensure_debug_mode();
124 //              dump_memory("write_memory", address, bytes, start, len);
125                 printf("O %x %x\n", len, address);
126                 for (int i = 0; i < len; i++)
127                         printf("%02x", bytes[start + i]);
128         }
129
130         public void write_memory(int address, byte[] bytes) {
131                 write_memory(address, bytes, 0, bytes.length);
132         }
133
134         /*
135          * Read target memory
136          */
137         public byte[] read_memory(int address, int length)
138                 throws IOException, InterruptedException {
139                 byte[]  data = new byte[length];
140
141                 flush_reply();
142                 ensure_debug_mode();
143                 printf("I %x %x\n", length, address);
144                 int i = 0;
145                 int start = 0;
146                 while (i < length) {
147                         String  line = get_reply().trim();
148                         if (!ishex(line) || line.length() % 2 != 0)
149                                 throw new IOException(
150                                         String.format
151                                         ("Invalid reply \"%s\"", line));
152                         int this_time = line.length() / 2;
153                         for (int j = 0; j < this_time; j++)
154                                 data[start + j] = (byte) ((fromhex(line.charAt(j*2)) << 4) +
155                                                   fromhex(line.charAt(j*2+1)));
156                         start += this_time;
157                         i += this_time;
158                 }
159 //              dump_memory("read_memory", address, data, 0, length);
160
161                 return data;
162         }
163
164         /*
165          * Write raw bytes to the debug link using the 'P' command
166          */
167         public void write_bytes(byte[] bytes) throws IOException {
168                 int i = 0;
169                 ensure_debug_mode();
170                 while (i < bytes.length) {
171                         int this_time = bytes.length - i;
172                         if (this_time > 8)
173                                 this_time = 0;
174                         printf("P");
175                         for (int j = 0; j < this_time; j++)
176                                 printf(" %02x", bytes[i+j]);
177                         printf("\n");
178                         i += this_time;
179                 }
180         }
181
182         public void write_byte(byte b) throws IOException {
183                 byte[] bytes = { b };
184                 write_bytes(bytes);
185         }
186
187         /*
188          * Read raw bytes from the debug link using the 'G' command
189          */
190         public byte[] read_bytes(int length)
191                 throws IOException, InterruptedException {
192
193                 flush_reply();
194                 ensure_debug_mode();
195                 printf("G %x\n", length);
196                 int i = 0;
197                 byte[] data = new byte[length];
198                 while (i < length) {
199                         String line = get_reply().trim();
200                         String tokens[] = line.split("\\s+");
201                         for (int j = 0; j < tokens.length; j++) {
202                                 if (!ishex(tokens[j]) ||
203                                     tokens[j].length() != 2)
204                                         throw new IOException(
205                                                 String.format
206                                                 ("Invalid read_bytes reply \"%s\"", line));
207                                 try {
208                                         data[i + j] = (byte) Integer.parseInt(tokens[j], 16);
209                                 } catch (NumberFormatException ne) {
210                                         throw new IOException(
211                                                 String.format
212                                                 ("Invalid read_bytes reply \"%s\"", line));
213                                 }
214                         }
215                         i += tokens.length;
216                 }
217                 return data;
218         }
219
220         public byte read_byte() throws IOException, InterruptedException {
221                 return read_bytes(1)[0];
222         }
223
224         public byte debug_instr(byte[] instruction) throws IOException, InterruptedException {
225                 byte[] command = new byte[1 + instruction.length];
226                 command[0] = DEBUG_INSTR((byte) instruction.length);
227                 for (int i = 0; i < instruction.length; i++)
228                         command[i+1] = instruction[i];
229                 write_bytes(command);
230                 return read_byte();
231         }
232
233         public byte resume() throws IOException, InterruptedException {
234                 write_byte(RESUME);
235                 return read_byte();
236         }
237
238         public int read_uint16() throws IOException, InterruptedException {
239                 byte[] d = read_bytes(2);
240                 return ((int) (d[0] & 0xff) << 8) | (d[1] & 0xff);
241         }
242
243         public int read_uint8()  throws IOException, InterruptedException {
244                 byte[] d = read_bytes(1);
245                 return (int) (d[0] & 0xff);
246         }
247
248         public int get_chip_id() throws IOException, InterruptedException {
249                 write_byte(GET_CHIP_ID);
250                 return read_uint16();
251         }
252
253         public int get_pc() throws IOException, InterruptedException {
254                 write_byte(GET_PC);
255                 return read_uint16();
256         }
257
258         public byte read_status() throws IOException, InterruptedException {
259                 write_byte(READ_STATUS);
260                 return read_byte();
261         }
262
263         static final byte LJMP                  = 0x02;
264
265         public void set_pc(int pc) throws IOException, InterruptedException {
266                 byte high = (byte) (pc >> 8);
267                 byte low = (byte) pc;
268                 byte[] jump_mem = { LJMP, high, low };
269                 debug_instr(jump_mem);
270         }
271
272         public boolean check_connection() throws IOException, InterruptedException {
273                 byte reply = read_status();
274                 if ((reply & STATUS_CHIP_ERASE_DONE) == 0)
275                         return false;
276                 if ((reply & STATUS_PCON_IDLE) != 0)
277                         return false;
278                 if ((reply & STATUS_POWER_MODE_0) == 0)
279                         return false;
280                 return true;
281         }
282
283         public AltosRomconfig romconfig() {
284                 try {
285                         byte[] bytes = read_memory(0xa0, 10);
286                         return new AltosRomconfig(bytes, 0);
287                 } catch (IOException ie) {
288                 } catch (InterruptedException ie) {
289                 }
290                 return new AltosRomconfig();
291         }
292
293         /*
294          * Reset target
295          */
296         public void reset() {
297                 printf ("R\n");
298         }
299 }