summaryrefslogtreecommitdiff
path: root/ao-tools/altosui/AltosDebug.java
blob: 7bd3a5cd14a60fd40696cd7f347b00fbd8c12541 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * Copyright © 2010 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

package altosui;

import java.lang.*;
import java.io.*;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.LinkedList;
import java.util.Iterator;
import altosui.AltosSerial;
import altosui.AltosRomconfig;

public class AltosDebug extends AltosSerial {

	static final byte WR_CONFIG =		0x1d;
	static final byte RD_CONFIG =		0x24;
	static final byte CONFIG_TIMERS_OFF =		(1 << 3);
	static final byte CONFIG_DMA_PAUSE =		(1 << 2);
	static final byte CONFIG_TIMER_SUSPEND =		(1 << 1);
	static final byte SET_FLASH_INFO_PAGE =		(1 << 0);

	static final byte GET_PC	=		0x28;
	static final byte READ_STATUS =		0x34;
	static final byte STATUS_CHIP_ERASE_DONE =	(byte) (1 << 7);
	static final byte STATUS_PCON_IDLE =		(1 << 6);
	static final byte STATUS_CPU_HALTED =		(1 << 5);
	static final byte STATUS_POWER_MODE_0 =		(1 << 4);
	static final byte STATUS_HALT_STATUS =		(1 << 3);
	static final byte STATUS_DEBUG_LOCKED =		(1 << 2);
	static final byte STATUS_OSCILLATOR_STABLE =	(1 << 1);
	static final byte STATUS_STACK_OVERFLOW =	(1 << 0);

	static final byte SET_HW_BRKPNT =	0x3b;
	static byte       HW_BRKPNT_N(byte n)	{ return (byte) ((n) << 3); }
	static final byte HW_BRKPNT_N_MASK =		(0x3 << 3);
	static final byte HW_BRKPNT_ENABLE =		(1 << 2);

	static final byte HALT =			0x44;
	static final byte RESUME	=		0x4c;
	static       byte DEBUG_INSTR(byte n)	{ return (byte) (0x54|(n)); }
	static final byte STEP_INSTR =		0x5c;
	static        byte STEP_REPLACE(byte n)	{ return  (byte) (0x64|(n)); }
	static final byte GET_CHIP_ID =		0x68;


	static boolean ishex(int c) {
		if ('0' <= c && c <= '9')
			return true;
		if ('a' <= c && c <= 'f')
			return true;
		if ('A' <= c && c <= 'F')
			return true;
		return false;
	}

	static boolean ishex(String s) {
		for (int i = 0; i < s.length(); i++)
			if (!ishex(s.charAt(i)))
				return false;
		return true;
	}
	static boolean isspace(int c) {
		switch (c) {
		case ' ':
		case '\t':
			return true;
		}
		return false;
	}

	static int fromhex(int c) {
		if ('0' <= c && c <= '9')
			return c - '0';
		if ('a' <= c && c <= 'f')
			return c - 'a' + 10;
		if ('A' <= c && c <= 'F')
			return c - 'A' + 10;
		return -1;
	}

	boolean	debug_mode;

	void ensure_debug_mode() {
		if (!debug_mode) {
			printf("D\n");
			debug_mode = true;
		}
	}

	/*
	 * Write target memory
	 */
	public void write_memory(int address, byte[] bytes) {
		ensure_debug_mode();
		printf("O %x %x\n", bytes.length, address);
		for (int i = 0; i < bytes.length; i++)
			printf("%02x", bytes[i]);
	}

	/*
	 * Read target memory
	 */
	public byte[] read_memory(int address, int length)
		throws IOException, InterruptedException {
		byte[]	data = new byte[length];

		flush_reply();
		ensure_debug_mode();
		printf("I %x %x\n", length, address);
		int i = 0;
		while (i < length) {
			String	line = get_reply().trim();
			if (!ishex(line) || line.length() % 2 != 0)
				throw new IOException(
					String.format
					("Invalid reply \"%s\"", line));
			int this_time = line.length() / 2;
			for (int j = 0; j < this_time; j++)
				data[j] = (byte) ((fromhex(line.charAt(j*2)) << 4) +
						  fromhex(line.charAt(j*2+1)));
			i += this_time;
		}

		return data;
	}

	/*
	 * Write raw bytes to the debug link using the 'P' command
	 */
	public void write_bytes(byte[] bytes) throws IOException {
		int i = 0;
		ensure_debug_mode();
		while (i < bytes.length) {
			int this_time = bytes.length - i;
			if (this_time > 8)
				this_time = 0;
			printf("P");
			for (int j = 0; j < this_time; j++)
				printf(" %02x", bytes[i+j]);
			printf("\n");
			i += this_time;
		}
	}

	public void write_byte(byte b) throws IOException {
		byte[] bytes = { b };
		write_bytes(bytes);
	}

	/*
	 * Read raw bytes from the debug link using the 'G' command
	 */
	public byte[] read_bytes(int length)
		throws IOException, InterruptedException {

		flush_reply();
		ensure_debug_mode();
		printf("G %x\n", length);
		int i = 0;
		byte[] data = new byte[length];
		while (i < length) {
			String line = get_reply().trim();
			String tokens[] = line.split("\\s+");
			for (int j = 0; j < tokens.length; j++) {
				if (!ishex(tokens[j]) ||
				    tokens[j].length() != 2)
					throw new IOException(
						String.format
						("Invalid read_bytes reply \"%s\"", line));
				try {
					data[i + j] = (byte) Integer.parseInt(tokens[j], 16);
				} catch (NumberFormatException ne) {
					throw new IOException(
						String.format
						("Invalid read_bytes reply \"%s\"", line));
				}
			}
			i += tokens.length;
		}
		return data;
	}

	public byte read_status() throws IOException, InterruptedException {
		write_byte(READ_STATUS);
		return read_bytes(2)[0];
	}

	public boolean check_connection() throws IOException, InterruptedException {
		byte reply = read_status();
		System.out.printf("status %x\n", reply);
		if ((reply & STATUS_CHIP_ERASE_DONE) == 0)
			return false;
		if ((reply & STATUS_PCON_IDLE) != 0)
			return false;
		if ((reply & STATUS_POWER_MODE_0) == 0)
			return false;
		return true;
	}

	public AltosRomconfig romconfig() {
		try {
			byte[] bytes = read_memory(0xa0, 10);
			return new AltosRomconfig(bytes, 0);
		} catch (IOException ie) {
		} catch (InterruptedException ie) {
		}
		return new AltosRomconfig();
	}

	/*
	 * Reset target
	 */
	public void reset() {
		printf ("R\n");
	}
}