summaryrefslogtreecommitdiff
path: root/src/ao_dbg.c
blob: d4c9567fe519039ec28713dd97282c126c919b42 (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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
 * Copyright © 2009 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.
 */

#include "ao.h"
#include "ao_pins.h"

static void
ao_dbg_send_bits(uint8_t msk, uint8_t val) __reentrant
{
	DBG_PORT = (DBG_PORT & ~msk) | (val & msk);
	_asm
		nop
		nop
	_endasm;
}

void
ao_dbg_send_byte(uint8_t byte)
{
	__pdata uint8_t	b, d;

	DBG_PORT |= DBG_DATA;
	DBG_PORT_DIR |= DBG_DATA;
	for (b = 0; b < 8; b++) {
		d = 0;
		if (byte & 0x80)
			d = DBG_DATA;
		byte <<= 1;
		ao_dbg_send_bits(DBG_CLOCK|DBG_DATA, DBG_CLOCK|d);
		ao_dbg_send_bits(DBG_CLOCK|DBG_DATA,     0    |d);
	}
	DBG_PORT_DIR &= ~DBG_DATA;
}

uint8_t
ao_dbg_recv_byte(void)
{
	__pdata uint8_t	byte, b;

	byte = 0;
	for (b = 0; b < 8; b++) {
		byte = byte << 1;
		ao_dbg_send_bits(DBG_CLOCK, DBG_CLOCK);
		if (DBG_DATA_PIN)
			byte |= 1;
		ao_dbg_send_bits(DBG_CLOCK, 0);
	}
	return byte;
}

/* 8051 instructions
 */
#define NOP			0x00
#define MOV_direct_data		0x75
#define LJMP			0x02
#define MOV_Rn_data(n)		(0x78 | (n))
#define DJNZ_Rn_rel(n)		(0xd8 | (n))
#define MOV_A_direct		0xe5
#define MOV_direct1_direct2	0x85
#define MOV_direct_A		0xf5
#define MOV_DPTR_data16		0x90
#define MOV_A_data		0x74
#define MOVX_atDPTR_A		0xf0
#define MOVX_A_atDPTR		0xe0
#define INC_DPTR		0xa3
#define TRAP			0xa5
#define SJMP			0x80
#define JB			0x20

#define DEBUG_INSTR(l)		(0x54 | (l))

#define SFR_PSW			0xD0
#define SFR_DPL0		0x82
#define SFR_DPH0		0x83
#define SFR_DPL1		0x84
#define SFR_DPH1		0x85

__pdata uint8_t	save_acc;
__pdata uint8_t save_psw;
__pdata uint8_t save_dpl0;
__pdata uint8_t save_dph0;
__pdata uint8_t save_dpl1;
__pdata uint8_t save_dph1;

static uint8_t
ao_dbg_inst1(uint8_t a) __reentrant
{
	ao_dbg_send_byte(DEBUG_INSTR(1));
	ao_dbg_send_byte(a);
	return ao_dbg_recv_byte();
}

static uint8_t
ao_dbg_inst2(uint8_t a, uint8_t b) __reentrant
{
	ao_dbg_send_byte(DEBUG_INSTR(2));
	ao_dbg_send_byte(a);
	ao_dbg_send_byte(b);
	return ao_dbg_recv_byte();
}

static uint8_t
ao_dbg_inst3(uint8_t a, uint8_t b, uint8_t c) __reentrant
{
	ao_dbg_send_byte(DEBUG_INSTR(3));
	ao_dbg_send_byte(a);
	ao_dbg_send_byte(b);
	ao_dbg_send_byte(c);
	return ao_dbg_recv_byte();
}

void
ao_dbg_start_transfer(uint16_t addr)
{
	save_acc  = ao_dbg_inst1(NOP);
	save_psw  = ao_dbg_inst2(MOV_A_direct, SFR_PSW);
	save_dpl0 = ao_dbg_inst2(MOV_A_direct, SFR_DPL0);
	save_dph0 = ao_dbg_inst2(MOV_A_direct, SFR_DPH0);
	save_dpl1 = ao_dbg_inst2(MOV_A_direct, SFR_DPL1);
	save_dph1 = ao_dbg_inst2(MOV_A_direct, SFR_DPH1);
	ao_dbg_inst3(MOV_DPTR_data16, addr >> 8, addr);
}

void
ao_dbg_end_transfer(void)
{
	ao_dbg_inst3(MOV_direct_data, SFR_DPL0, save_dpl0);
	ao_dbg_inst3(MOV_direct_data, SFR_DPH0, save_dph0);
	ao_dbg_inst3(MOV_direct_data, SFR_DPL1, save_dpl1);
	ao_dbg_inst3(MOV_direct_data, SFR_DPH1, save_dph1);
	ao_dbg_inst3(MOV_direct_data, SFR_PSW, save_psw);
	ao_dbg_inst2(MOV_A_data, save_acc);
}

void
ao_dbg_write_byte(uint8_t byte)
{
	ao_dbg_inst2(MOV_A_data, byte);
	ao_dbg_inst1(MOVX_atDPTR_A);
	ao_dbg_inst1(INC_DPTR);
}

uint8_t
ao_dbg_read_byte(void)
{
	ao_dbg_inst1(MOVX_A_atDPTR);
	return ao_dbg_inst1(INC_DPTR);
}

static void
ao_dbg_set_pins(void)
{
	/* Make the DBG pins GPIOs. On TeleMetrum, this will
	 * disable the SPI link, so don't expect SPI to work after
	 * using the debugger.
	 */
	DBG_PORT_SEL &= ~(DBG_CLOCK|DBG_DATA|DBG_RESET_N);

	/* make DBG_DATA tri-state */
	DBG_PORT_INP |= DBG_DATA;

	/* Raise RESET_N and CLOCK */
	DBG_PORT |= DBG_RESET_N | DBG_CLOCK;

	/* RESET_N and CLOCK are outputs now */
	DBG_PORT_DIR |= DBG_RESET_N | DBG_CLOCK;
	DBG_PORT_DIR &= ~DBG_DATA;
}

static void
ao_dbg_long_delay(void)
{
	uint8_t	n;

	for (n = 0; n < 20; n++)
		_asm nop _endasm;
}

#define AO_RESET_LOW_DELAY	AO_MS_TO_TICKS(100)
#define AO_RESET_HIGH_DELAY	AO_MS_TO_TICKS(100)

void
ao_dbg_debug_mode(void)
{
	ao_dbg_set_pins();
	ao_dbg_long_delay();
	ao_dbg_send_bits(DBG_CLOCK|DBG_DATA|DBG_RESET_N, DBG_CLOCK|DBG_DATA|DBG_RESET_N);
	ao_dbg_long_delay();
	ao_dbg_send_bits(DBG_CLOCK|DBG_DATA|DBG_RESET_N,     0    |DBG_DATA|    0    );
	ao_delay(AO_RESET_LOW_DELAY);
	ao_dbg_send_bits(DBG_CLOCK|DBG_DATA|DBG_RESET_N, DBG_CLOCK|DBG_DATA|    0    );
	ao_dbg_long_delay();
	ao_dbg_send_bits(DBG_CLOCK|DBG_DATA|DBG_RESET_N,     0    |DBG_DATA|    0    );
	ao_dbg_long_delay();
	ao_dbg_send_bits(DBG_CLOCK|DBG_DATA|DBG_RESET_N, DBG_CLOCK|DBG_DATA|    0    );
	ao_dbg_long_delay();
	ao_dbg_send_bits(DBG_CLOCK|DBG_DATA|DBG_RESET_N,     0    |DBG_DATA|DBG_RESET_N);
	ao_delay(AO_RESET_HIGH_DELAY);
}

void
ao_dbg_reset(void)
{
	ao_dbg_set_pins();
	ao_dbg_long_delay();
	ao_dbg_send_bits(DBG_CLOCK|DBG_DATA|DBG_RESET_N, DBG_CLOCK|DBG_DATA|DBG_RESET_N);
	ao_dbg_long_delay();
	ao_dbg_send_bits(DBG_CLOCK|DBG_DATA|DBG_RESET_N, DBG_CLOCK|DBG_DATA|    0    );
	ao_delay(AO_RESET_LOW_DELAY);
	ao_dbg_send_bits(DBG_CLOCK|DBG_DATA|DBG_RESET_N, DBG_CLOCK|DBG_DATA|    0    );
	ao_dbg_long_delay();
	ao_dbg_send_bits(DBG_CLOCK|DBG_DATA|DBG_RESET_N, DBG_CLOCK|DBG_DATA|    0    );
	ao_dbg_long_delay();
	ao_dbg_send_bits(DBG_CLOCK|DBG_DATA|DBG_RESET_N, DBG_CLOCK|DBG_DATA|    0    );
	ao_dbg_long_delay();
	ao_dbg_send_bits(DBG_CLOCK|DBG_DATA|DBG_RESET_N, DBG_CLOCK|DBG_DATA|DBG_RESET_N);
	ao_delay(AO_RESET_HIGH_DELAY);
}

static void
debug_enable(void)
{
	ao_dbg_debug_mode();
}

static void
debug_reset(void)
{
	ao_dbg_reset();
}

static void
debug_put(void)
{
	for (;;) {
		ao_cmd_white ();
		if (ao_cmd_lex_c == '\n')
			break;
		ao_cmd_hex();
		if (ao_cmd_status != ao_cmd_success)
			break;
		ao_dbg_send_byte(ao_cmd_lex_i);
	}
}

static void
debug_get(void)
{
	__pdata uint16_t count;
	__pdata uint16_t i;
	__pdata uint8_t byte;
	ao_cmd_hex();
	if (ao_cmd_status != ao_cmd_success)
		return;
	count = ao_cmd_lex_i;
	if (count > 256) {
		ao_cmd_status = ao_cmd_syntax_error;
		return;
	}
	for (i = 0; i < count; i++) {
		if (i && (i & 7) == 0)
			putchar('\n');
		byte = ao_dbg_recv_byte();
		ao_cmd_put8(byte);
		putchar(' ');
	}
	putchar('\n');
}

static uint8_t
getnibble(void)
{
	__pdata char	c;

	c = getchar();
	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);
	ao_cmd_status = ao_cmd_lex_error;
	return 0;
}

static void
debug_input(void)
{
	__pdata uint16_t count;
	__pdata uint16_t addr;
	__pdata uint8_t b;
	__pdata uint8_t	i;

	ao_cmd_hex();
	count = ao_cmd_lex_i;
	ao_cmd_hex();
	addr = ao_cmd_lex_i;
	if (ao_cmd_status != ao_cmd_success)
		return;
	ao_dbg_start_transfer(addr);
	i = 0;
	while (count--) {
		if (!(i++ & 7))
			putchar('\n');
		b = ao_dbg_read_byte();
		ao_cmd_put8(b);
	}
	ao_dbg_end_transfer();
	putchar('\n');
}

static void
debug_output(void)
{
	__pdata uint16_t count;
	__pdata uint16_t addr;
	__pdata uint8_t b;

	ao_cmd_hex();
	count = ao_cmd_lex_i;
	ao_cmd_hex();
	addr = ao_cmd_lex_i;
	if (ao_cmd_status != ao_cmd_success)
		return;
	ao_dbg_start_transfer(addr);
	while (count--) {
		b = getnibble() << 4;
		b |= getnibble();
		if (ao_cmd_status != ao_cmd_success)
			return;
		ao_dbg_write_byte(b);
	}
	ao_dbg_end_transfer();
}

__code struct ao_cmds ao_dbg_cmds[7] = {
	{ debug_enable,	"D\0Enable debug" },
	{ debug_get,	"G <count>\0Get data" },
	{ debug_input,	"I <count> <addr>\0Input <count> at <addr>" },
	{ debug_output,	"O <count> <addr>\0Output <count> at <addr>" },
	{ debug_put,	"P <byte> ...\0Put data" },
	{ debug_reset,	"R\0Reset" },
	{ 0, NULL },
};

void
ao_dbg_init(void)
{
	ao_cmd_register(&ao_dbg_cmds[0]);
}