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