ca2e5a9046625465130c0f4dd1ec55f9c10cba48
[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         boolean debug_mode;
62
63         void ensure_debug_mode() {
64                 if (!debug_mode) {
65                         printf("m 0\nD\n");
66                         flush_reply();
67                         debug_mode = true;
68                 }
69         }
70
71         void dump_memory(String header, int address, byte[] bytes, int start, int len) {
72                 System.out.printf("%s\n", header);
73                 for (int j = 0; j < len; j++) {
74                         if ((j & 15) == 0) {
75                                 if (j != 0)
76                                         System.out.printf("\n");
77                                 System.out.printf ("%04x:", address + j);
78                         }
79                         System.out.printf(" %02x", bytes[start + j]);
80                 }
81                 System.out.printf("\n");
82         }
83
84         /*
85          * Write target memory
86          */
87         public void write_memory(int address, byte[] bytes, int start, int len) {
88                 ensure_debug_mode();
89 //              dump_memory("write_memory", address, bytes, start, len);
90                 printf("O %x %x\n", len, address);
91                 for (int i = 0; i < len; i++)
92                         printf("%02x", bytes[start + i]);
93         }
94
95         public void write_memory(int address, byte[] bytes) {
96                 write_memory(address, bytes, 0, bytes.length);
97         }
98
99         /*
100          * Read target memory
101          */
102         public byte[] read_memory(int address, int length)
103                 throws IOException, InterruptedException {
104                 byte[]  data = new byte[length];
105
106                 flush_reply();
107                 ensure_debug_mode();
108                 printf("I %x %x\n", length, address);
109                 int i = 0;
110                 int start = 0;
111                 while (i < length) {
112                         String  line = get_reply().trim();
113                         if (!Altos.ishex(line) || line.length() % 2 != 0)
114                                 throw new IOException(
115                                         String.format
116                                         ("Invalid reply \"%s\"", line));
117                         int this_time = line.length() / 2;
118                         for (int j = 0; j < this_time; j++)
119                                 data[start + j] = (byte) ((Altos.fromhex(line.charAt(j*2)) << 4) +
120                                                   Altos.fromhex(line.charAt(j*2+1)));
121                         start += this_time;
122                         i += this_time;
123                 }
124 //              dump_memory("read_memory", address, data, 0, length);
125
126                 return data;
127         }
128
129         /*
130          * Write raw bytes to the debug link using the 'P' command
131          */
132         public void write_bytes(byte[] bytes) throws IOException {
133                 int i = 0;
134                 ensure_debug_mode();
135                 while (i < bytes.length) {
136                         int this_time = bytes.length - i;
137                         if (this_time > 8)
138                                 this_time = 0;
139                         printf("P");
140                         for (int j = 0; j < this_time; j++)
141                                 printf(" %02x", bytes[i+j]);
142                         printf("\n");
143                         i += this_time;
144                 }
145         }
146
147         public void write_byte(byte b) throws IOException {
148                 byte[] bytes = { b };
149                 write_bytes(bytes);
150         }
151
152         /*
153          * Read raw bytes from the debug link using the 'G' command
154          */
155         public byte[] read_bytes(int length)
156                 throws IOException, InterruptedException {
157
158                 flush_reply();
159                 ensure_debug_mode();
160                 printf("G %x\n", length);
161                 int i = 0;
162                 byte[] data = new byte[length];
163                 while (i < length) {
164                         String line = get_reply().trim();
165                         String tokens[] = line.split("\\s+");
166                         for (int j = 0; j < tokens.length; j++) {
167                                 if (!Altos.ishex(tokens[j]) ||
168                                     tokens[j].length() != 2)
169                                         throw new IOException(
170                                                 String.format
171                                                 ("Invalid read_bytes reply \"%s\"", line));
172                                 try {
173                                         data[i + j] = (byte) Integer.parseInt(tokens[j], 16);
174                                 } catch (NumberFormatException ne) {
175                                         throw new IOException(
176                                                 String.format
177                                                 ("Invalid read_bytes reply \"%s\"", line));
178                                 }
179                         }
180                         i += tokens.length;
181                 }
182                 return data;
183         }
184
185         public byte read_byte() throws IOException, InterruptedException {
186                 return read_bytes(1)[0];
187         }
188
189         public byte debug_instr(byte[] instruction) throws IOException, InterruptedException {
190                 byte[] command = new byte[1 + instruction.length];
191                 command[0] = DEBUG_INSTR((byte) instruction.length);
192                 for (int i = 0; i < instruction.length; i++)
193                         command[i+1] = instruction[i];
194                 write_bytes(command);
195                 return read_byte();
196         }
197
198         public byte resume() throws IOException, InterruptedException {
199                 write_byte(RESUME);
200                 return read_byte();
201         }
202
203         public int read_uint16() throws IOException, InterruptedException {
204                 byte[] d = read_bytes(2);
205                 return ((int) (d[0] & 0xff) << 8) | (d[1] & 0xff);
206         }
207
208         public int read_uint8()  throws IOException, InterruptedException {
209                 byte[] d = read_bytes(1);
210                 return (int) (d[0] & 0xff);
211         }
212
213         public int get_chip_id() throws IOException, InterruptedException {
214                 write_byte(GET_CHIP_ID);
215                 return read_uint16();
216         }
217
218         public int get_pc() throws IOException, InterruptedException {
219                 write_byte(GET_PC);
220                 return read_uint16();
221         }
222
223         public byte read_status() throws IOException, InterruptedException {
224                 write_byte(READ_STATUS);
225                 return read_byte();
226         }
227
228         static final byte LJMP                  = 0x02;
229
230         public void set_pc(int pc) throws IOException, InterruptedException {
231                 byte high = (byte) (pc >> 8);
232                 byte low = (byte) pc;
233                 byte[] jump_mem = { LJMP, high, low };
234                 debug_instr(jump_mem);
235         }
236
237         public boolean check_connection() throws IOException, InterruptedException {
238                 byte reply = read_status();
239                 if ((reply & STATUS_CHIP_ERASE_DONE) == 0)
240                         return false;
241                 if ((reply & STATUS_PCON_IDLE) != 0)
242                         return false;
243                 if ((reply & STATUS_POWER_MODE_0) == 0)
244                         return false;
245                 return true;
246         }
247
248         public AltosRomconfig romconfig() {
249                 try {
250                         byte[] bytes = read_memory(0xa0, 10);
251                         return new AltosRomconfig(bytes, 0);
252                 } catch (IOException ie) {
253                 } catch (InterruptedException ie) {
254                 }
255                 return new AltosRomconfig();
256         }
257
258         /*
259          * Reset target
260          */
261         public void reset() {
262                 printf ("R\n");
263         }
264 }