summaryrefslogtreecommitdiff
path: root/src/hc11debug.c
blob: 5206198bd960f8322baf8f6ddeb15ad007c665a0 (plain)
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/* --------------------------------------------------------------------

   hc11debug - Debugger for the Motorola 68HC11

   Copyright (C) 2004-2020 Dominic Radermacher <blip@mockmoon-cybernetics.ch>

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
         
   http://www.apache.org/licenses/LICENSE-2.0
                
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
   -------------------------------------------------------------------- */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>		/* exit() */
#include <string.h>		/* strcmp() */
#include <ctype.h>		/* isdigit() */
#include <unistd.h>		/* read(), write(), sleep(), tcgetattr() */
#include <termios.h>		/* tcgetattr() */
#include <fcntl.h>		/* open() */
#include <sys/types.h>		/* open() */
#include <sys/stat.h>		/* open() */

#include "hc11tools.h"
#include "s19stuff.h"
#include "serial.h"
#include "talker.h"
#include "tty.h"
#include "ui.h"

#define DEFAULT_SERDEV	"/dev/ttyUSB0"

/* --------------------------------------------------------------------
   Function prototypes
   -------------------------------------------------------------------- */

int f_readline(FILE *f, char *buf, int size);
void usage(char *progname);
int is_chardev(char *file);
int parse_args(int argc, char **argv);
int load_talker(struct obj *bo);

int parse_cmd(int fd, char *cmd);

boolean any(char *str, char c);
int arg_val(char *arg);

/* --------------------------------------------------------------------
   Global variables
   -------------------------------------------------------------------- */

char *device;			/* device where the board is connected to */
boolean skipload, extmem;

int is_chardev(char *file)
{
	struct stat stbuf;

	if(stat(file, &stbuf) == -1) {
		return -1;
	}
	if((stbuf.st_mode & S_IFMT) != S_IFCHR) {
		return -1;
	}
	return 0;
}

int parse_args(int argc, char **argv)
{
	int i;
	char *p;

	for(i=1; i<argc; i++) {
		p=argv[i];
		if(*argv[i] != '-') {
			break;
		}
		p++;
		if(strcmp(p, "d") == 0) {		/* Listing file */
			device=argv[++i];
			if(is_chardev(device) != 0) {
				printf("'%s' is no character device\n", device);
				usage(argv[0]);
			}
		} else if(strcmp(p, "-skipload") == 0) {
			i++;
			skipload=true;
		} else if(strcmp(p, "-extmem") == 0) {
			extmem=true;
		} else {
			usage(argv[0]);
		}
	}
	return i;
}

char old_talker[] = 
"S12300008e01ffce10006f2ccc300ca72be72d8610067e0012b6102e842027f9b6102f43cb\n"
"S12300208d462a518d338f8d30178d2d8f81fe260da6008d33178d2116085a26f43b81be19\n"
"S12300402616178d14e70018ce0001180926fce600f7102f084a26eb3bf6102ec50a26a014\n"
"S1230060c42027f5f6102f39188fb6102e2afb188fb7102f39817e260c308f8deb178de884\n"
"S123008030c60920ac813e26128dce178dcb8f35860920af864a8dd00f20fd814a26b9307b\n"
"S11500a0c6093a35ec078dc0178dbdcc0098ed0720c72c\n";

char old_irqvec[] =
"S12100c47e00157e00587e00587e00587e00587e00587e00587e00587e00587e005801\n"
"S12100e27e00587e00587e00587e00587e00587e00587e00587e00007e00587e0058f8\n";

char xx_talk[] = 
"S12300008E01FFCC300CB7102BF7102D86900620FE8D38438D442A19368D3016328F8D2B48\n"
"S1230020168D288F81BE264E8D21A700085A26F83B813E270C308F8D21178D1E30C609205F\n"
"S1230040358D08168D058FC60920DDB6102E850A26AE842027F5B6102F397D102E2AFBB7F3\n"
"S1220060102F39864A8DF330EC078DEE178DEBCC000FED07C607A6008DE0085A26F83B1E\n";

char talk_irqvec[] =
"S11200F17E00117E00637E00007E00007E000012\n";

int load_talker(struct obj *bo)
{
	int i, k;

	bo->start_addr=0xffff;
	bo->end_addr=0x0000;
	for(i=0; i<(int)sizeof(bo->code); bo->code[i++]=0);
	printf("Loading talker code...\n");
	for(i=0; xx_talk[i] != 0; ) {
		for(k=i; xx_talk[k] != '\n'; k++);
		xx_talk[k++]=0;
		parse_s19_line(bo, &xx_talk[i]);
		i=k;
	}
	printf("Loading IRQ vectors...\n");
	for(i=0; talk_irqvec[i] != 0; ) {
		for(k=i; talk_irqvec[k] != '\n'; k++);
		talk_irqvec[k++]=0;
		parse_s19_line(bo, &talk_irqvec[i]);
		i=k;
	}
	return 0;
}

void usage(char *progname)
{
	printf("usage: %s [options] <s19-file>\n", progname);
	printf("\toptions:\n");
	printf("\t-d <device>\tserial device to use (default: %s)\n", DEFAULT_SERDEV);
	printf("\t--skipload\tskip bootload process (talker already running)\n");
	printf("\t--extmem\tenable external bus\n");
	exit(5);
}

/*
 *	any --- does str contain c?
 */
boolean any(char *str, char c)
{
	while(*str != 0) {
		if(*str++ == c) {
			return true;
		}
	}
	return false;
}

/* --------------------------------------------------------------------
	evaluate the decimal value of the argument.
	input can be binary %01010011, hexadecimal $0f or decimal 45
	values
   -------------------------------------------------------------------- */

int arg_val(char *arg)
{
	int val=0;

	if(*arg == '%') {		/* binary constant */
		arg++;
		while(any("01", *arg) == true) {
			printf("%s -> %i\n", arg, val);
			val = (val * 2) + ((*arg++)-'0');
		}
	} else if(*arg == '@') {	/* octal constant */
		arg++;
		while(any("01234567", *arg) == true) {
 			val = (val * 8) + ((*arg++)-'0');
		}
	} else if(*arg == '$') {	/* hex constant */
		arg++;
		while(any("0123456789abcdefABCDEF", *arg) == true) {
			if(*arg > '9') {
				val = (val * 16) + 10 + (tolower(*arg++)-'a');
			} else {
				val = (val * 16) + ((*arg++)-'0');
			}
		}
	} else if(any("0123456789", *arg) == true) {	/* decimal constant */
		while(*arg >= '0' && *arg <= '9') {
			val = (val * 10) + ((*arg++)-'0');
		}
	} else {
		printf("error while parsing argument '%s'\n", arg);
		return 0;
	}
	return val;
}

int parse_cmd(int fd, char *cmd)
{
	char *p;
	uint8_t buf[256];
	int addr=0, len=0, val=0;
	struct obj code;
	struct mcu_regs	myregs;

	if(strncmp(cmd, "md ", 3) == 0) {
		if((p=strchr(cmd+3, ' ')) != NULL) { /* we've got 2 args */
			*p++=0;
			len=arg_val(p);
			if(len > 256) {
				printf("I would not do that if I where you (hint: md <startaddr> [len])\n");
				return 0;
			}
		} else {
			len=16;
		}
		addr=arg_val(cmd+3);
		printf("memory dump from addr $%04x (len = %i)\n", addr, len);
		read_mcu_mem(fd, addr, buf, len);
		ui_show_mem(addr, buf, len);
	} else if(strncmp(cmd, "ms ", 3) == 0) {
		if((p=strchr(cmd+3, ' ')) != NULL) {
			*p++=0;
			val=arg_val(p);
			if(val > 255) {
				printf("value must be 0 .. 255\n");
				return 0;
			}
		} else {
			printf("hint: ms <addr> <value>\n");
			return 0;
		}
		addr=arg_val(cmd+3);
		printf("setting value at addr $%04x to $%02x\n", addr, val);
		buf[0]=val;
		write_mcu_mem(fd, addr, buf, 1);
	} else if(strncmp(cmd, "go ", 3) == 0) {
		addr=arg_val(cmd+3);
		printf("Starting program at 0x%04x\n", addr);
		read_mcu_regs(fd, &myregs);
		myregs.pc=addr;
		write_mcu_regs(fd, &myregs);
		ui_show_mcu_regs(&myregs);
	} else if(strncmp(cmd, "load ", 5) == 0) {
		load_s19file(&code, cmd+5);	/* load s19 source file */
		addr=code.start_addr;
		len=code.end_addr - code.start_addr;
		if(len > 255) {
			printf("sorry, currently we can load a maximum of 256 bytes\n");
			return -1;
		}
		printf("Writing %i byte starting from addr $%04x (this may take a while)\n", len, addr);
		write_mcu_mem(fd, addr, &code.code[addr], len);
		printf("Done\n");
	}
	return 0;
}

int main(int argc, char **argv)
{
	int fd, i, k;
	struct obj bootcode;
	struct mcu mymcu;
	struct mcu_regs	myregs;
	char *cmd;
	uint8_t b;
	uint8_t buf[16];
	
	if(argc < 2) {
		usage(argv[0]);
	}
	device=DEFAULT_SERDEV;
	skipload=false;
	extmem=false;		/* we do not use external bus per default */
	i=parse_args(argc, argv);
	if((fd=serial_open(device, B1200, CS8+CLOCAL)) < 0) {
		fprintf(stderr, "error: can not open '%s'\n", device);
		return 5;
	}
	if(skipload == false) {
		if(i < argc) {
			load_s19file(&bootcode, argv[i]);	/* load s19 source file */
		} else {
			load_talker(&bootcode);
		}
		if(bootcode.start_addr != 0) {
			fprintf(stderr, "error: program must start at address $0000\n");
			return 1;
		}
		if(upload_firmware(fd, &bootcode) != 0) {
			return 5;	/* makes no sense to continue then */
		}
		sleep(1);
	}
/*	if(serial_setspeed(fd, B9600) != 0) {
		printf("failed to switch switching baudrate\n");
		exit(5);
	} */
	mymcu.devid=0;
	mymcu.maskid=0;
	/* try to detect mcu type */
	read_mcu_mem(fd, 0x103c, buf, 1);
	/* either 0x0c or 0x0d seems bootstrap mode */
	b=(buf[0]>>4);
	if((b & 4) == 4) {		/* SMOD=1 - special mode */
		if((b & 8) == 8) {
			read_mcu_mem(fd, 0xbfd4, buf, 2);
			mymcu.devid=(buf[0]<<8)+buf[1];
			read_mcu_mem(fd, 0xbfd2, buf, 2);
			mymcu.maskid=(buf[0]<<8)+buf[1];
		}
		if((b & 2) == 2) {
			printf("=> Special Test mode\n");
		} else {
			// printf("=> Special Bootstrap mode\n");
			if(extmem == true) {
				// printf("enabling external bus\n");
				buf[0] |= 0x20;
				// write_mcu_mem(fd, 0x103c, buf, 1);
			}
		}
	}
	tcflush(fd, TCIOFLUSH);
	i=0;
	tty_init();
	tty_setscrollregion(1, tty_height()-4);
	tty_gotoxy(1, 1);
	tty_cls();
	while((k=tty_getkey()) != KEY_ESC) {
		ui_show_mcu_type(&mymcu);
		if(serial_rx_empty(fd) != 0) {		/* when NOT empty */
			read_byte(fd, &b);
			if(b == 0x4a) {		/* SWI */
				swi_handler(fd, &myregs);
			} else {
				printf("trap code %02x\n", b);
			}
		}
		if(k == 's') {	/* 's' */
			printf("Stopping MCU (setting to idle loop)\n");
			read_mcu_regs(fd, &myregs);
			myregs.pc=0x000f;
			write_mcu_regs(fd, &myregs);
			ui_show_mcu_regs(&myregs);
			i=0;
		} else if(k == 'r') {	/* r */
			if(read_mcu_regs(fd, &myregs) == 0) {
				ui_show_mcu_regs(&myregs);
			}
		} else if(k == KEY_ENTER) {
			cmd=ui_enter_cmd(NULL);
			parse_cmd(fd, cmd);
		} else if(k == KEY_TIMEOUT) {
			if(read_mcu_regs(fd, &myregs) == 0) {
				ui_show_mcu_regs(&myregs);
			}
		} else {
			printf("keycode %04x counter %i\n", k, i);
		}
		i++;
	}
	tty_reset();
	close(fd);
	return 0;
}