]> cloudbase.mooo.com Git - z180-stamp.git/blob - avr/cmd_boot.c
command go: new option -h
[z180-stamp.git] / avr / cmd_boot.c
1 /*
2 * (C) Copyright 2014-2016 Leo C. <erbl259-lmu@yahoo.de>
3 *
4 * (C) Copyright 2000-2003
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
7 * SPDX-License-Identifier: GPL-2.0
8 */
9
10 /*
11 * Misc boot support
12 */
13 #include "cmd_boot.h"
14 #include <ctype.h>
15 #include <util/atomic.h>
16
17 #include "cli_readline.h" /* console_buffer[] */
18 #include "cli.h" /* run_command() */
19 #include "env.h"
20 #include "eval_arg.h"
21 #include "con-utils.h"
22 #include "getopt-min.h"
23 #include "z80-if.h"
24 #include "z180-serv.h" /* restart_z180_serv() */
25 #include "debug.h"
26
27 /* ugly hack to get Z180 loadfile into flash memory */
28 #define const const FLASH
29 #include "../z180/hdrom.h"
30 #include "../z180/cfboot.h"
31 #undef const
32
33
34 command_ret_t do_loadf(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED)
35 {
36 if (z80_bus_state() & ZST_RUNNING)
37 cmd_error(CMD_RET_FAILURE, ERUNNING, NULL);
38 z80_bus_request_or_exit();
39 z80_load_mem(2, hdrom,
40 &hdrom_sections,
41 hdrom_address,
42 hdrom_length_of_sections);
43
44 z80_bus_cmd(Release);
45
46 return CMD_RET_SUCCESS;
47 }
48
49
50 void print_vars(char *title)
51 {
52 uint8_t buf[5];
53 zstate_t state = z80_bus_state();
54
55 if((state & ZST_ACQUIRED) == 0)
56 z80_bus_cmd(Request);
57
58 z80_read_block(buf, 9, sizeof buf);
59
60 if((state & ZST_ACQUIRED) == 0)
61 z80_bus_cmd(Release);
62
63 printf_P(PSTR("%s: stage: %d, flag: 0x%.02x, result: %d, IDE stat/error: 0x%.02x/0x%.02x\n"),
64 title, buf[0], buf[1], buf[2], buf[3], buf[4]);
65 }
66
67
68 /*
69 * bootcf [options]
70 *
71 * -a address (100h)
72 * -s start sector (0)
73 * -c sector count (7)
74 * -i Partition id (52)
75 * -n load only
76 * -t timeout (10000)
77 * -v verbose
78 */
79
80 command_ret_t do_bootcf(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
81 {
82 struct {
83 uint8_t jr[2];
84 uint16_t loadaddr;
85 uint8_t sec_start;
86 uint8_t sec_cnt;
87 uint8_t part_id;
88 uint16_t timeout;
89 uint8_t stages;
90 } boot_param;
91
92 struct {
93 uint8_t stages;
94 uint8_t done;
95 uint8_t result;
96 uint8_t ide_stat;
97 uint8_t ide_error;
98 } boot_res;
99
100 int_fast8_t verbosity = 0;
101 uint8_t default_stages;
102 uint32_t val;
103 ERRNUM res = ESUCCESS;
104
105 /* get default values */
106 memcpy_P(&boot_param, cfboot, sizeof boot_param);
107 default_stages = boot_param.stages;
108
109 /* reset getopt() */
110 optind = 0;
111
112 int opt;
113 while ((opt = getopt(argc, argv, PSTR("vna:s:c:t:i:"))) != -1) {
114 switch (opt) {
115 case 'v':
116 verbosity++;
117 break;
118 case 'n':
119 if (boot_param.stages > 0)
120 boot_param.stages--;
121 break;
122 case 'a':
123 val = eval_arg(optarg, NULL);
124 if (val < 0x100 || val > 0xFE00) {
125 printf_P(PSTR("Address out of range: 0x%.4lX\n"), val);
126 return CMD_RET_FAILURE;
127 }
128 boot_param.loadaddr = val;
129 break;
130 case 's':
131 val = eval_arg(optarg, NULL);
132 if (val > 255) {
133 printf_P(PSTR("Start sector out of range: 0x%lX\n"), val);
134 return CMD_RET_FAILURE;
135 }
136 boot_param.sec_start = val;
137 break;
138 case 'c':
139 val = eval_arg(optarg, NULL);
140 if (val > 127) {
141 printf_P(PSTR("Sector count out of range: 0x%lX\n"), val);
142 return CMD_RET_FAILURE;
143 }
144 boot_param.sec_cnt = val;
145 break;
146 case 't':
147 val = eval_arg(optarg, NULL);
148 if (val < 0x1 || val > 0xFFFF) {
149 printf_P(PSTR("Timeout value out of range: 0x%lX\n"), val);
150 return CMD_RET_FAILURE;
151 }
152 boot_param.timeout = val;
153 break;
154 case 'i':
155 val = eval_arg(optarg, NULL);
156 if (val < 0x01 || val > 0xFF) {
157 printf_P(PSTR("Partition id out of range: 0x%lX\n"), val);
158 return CMD_RET_FAILURE;
159 }
160 boot_param.part_id = val;
161 break;
162 default: /* '?' */
163 return CMD_RET_USAGE;
164 }
165 }
166
167 /* remaining arguments */
168 argc -= optind;
169 if (argc) {
170 my_puts_P(PSTR("Argument error!\n"));
171 return CMD_RET_USAGE;
172 }
173
174 if ((val = (uint32_t) boot_param.loadaddr + boot_param.sec_cnt * 512) >= 0xFF00) {
175 printf_P(PSTR("Top address out of range: 0x%.4lX\n"), val);
176 return CMD_RET_FAILURE;
177 }
178
179 if (z80_bus_state() & ZST_RUNNING)
180 cmd_error(CMD_RET_FAILURE, ERUNNING, NULL);
181 z80_bus_request_or_exit();
182 z80_load_mem(verbosity, cfboot,
183 &cfboot_sections,
184 cfboot_address,
185 cfboot_length_of_sections);
186
187 z80_write_block((const uint8_t *) &boot_param,
188 cfboot_address[0], sizeof boot_param);
189 z80_bus_cmd(Release);
190
191 if (boot_param.stages == 0) {
192 printf_P(PSTR("Bootloader loaded at: 0x%.4X\n"), (uint16_t) cfboot_address[0]);
193 } else {
194 printf_P(PSTR("Executing %d of %d Bootloader stages...\n"),
195 boot_param.stages, default_stages);
196
197 z80_bus_cmd(Run);
198 z80_bus_cmd(Release);
199
200 clear_ctrlc(); /* forget any previous Control C */
201 for (boot_res.done = 0; boot_res.done != 0xFF;) {
202 _delay_ms(8);
203 /* check for ctrl-c to abort... */
204 if (had_ctrlc() || ctrlc()) {
205 break;
206 }
207 z80_bus_cmd(Request);
208 z80_read_block((uint8_t *) &boot_res,
209 cfboot_address[0]+sizeof boot_param - 1, sizeof boot_res);
210 z80_bus_cmd(Release);
211 }
212
213 if (boot_res.done != 0xFF) {
214 z80_bus_cmd(Reset);
215 my_puts_P(PSTR("Abort\n"));
216 } else {
217 if (boot_param.stages == default_stages &&
218 boot_res.stages == 0 &&
219 boot_res.result == 0) {
220 my_puts_P(PSTR("Booting...\n"));
221 } else {
222 z80_bus_cmd(Reset);
223 boot_res.stages++;
224 printf_P(PSTR("Bootloader stopped at stage %d, result: %d, IDE stat/error: 0x%.02x/0x%.02x\n"),
225 boot_param.stages - boot_res.stages,
226 boot_res.result, boot_res.ide_stat, boot_res.ide_error);
227 }
228 }
229 }
230
231 return CMD_RET_SUCCESS;
232 }
233
234 command_ret_t do_busreq_pulse(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
235 {
236 uint16_t count=1;
237
238 if (!(z80_bus_state() & ZST_RUNNING)) {
239 printf_P(PSTR("## CPU is not running!\n"));
240 return CMD_RET_FAILURE;
241 }
242
243 if (argc > 1)
244 count = (uint16_t) eval_arg(argv[1], NULL);
245
246 z80_bus_cmd(Request);
247 while (count--)
248 z80_bus_cmd(M_Cycle);
249
250 return CMD_RET_SUCCESS;
251 }
252
253
254 command_ret_t do_go(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
255 {
256 uint32_t addr;
257 bool hold = 0;
258
259 /* reset getopt() */
260 optind = 0;
261
262 int opt;
263 while ((opt = getopt(argc, argv, PSTR("h"))) != -1) {
264 switch (opt) {
265 case 'h':
266 hold = 1;
267 break;
268 default: /* '?' */
269 return CMD_RET_USAGE;
270 }
271 }
272 argc -= optind; /* remaining arguments */
273
274 if (argc != 1)
275 return CMD_RET_USAGE;
276 addr = eval_arg(argv[optind], NULL);
277 if (addr >= (1UL<<16)) {
278 printf_P(PSTR("Invalid startaddress: 0x%05lx\n"
279 " (Out of logical address space (0x00000-0x0ffff))\n"),
280 addr);
281 return CMD_RET_FAILURE;
282 }
283
284 if (z80_bus_state() & ZST_RUNNING) {
285 printf_P(PSTR("CPU already running!\n"));
286 return CMD_RET_FAILURE;
287 }
288
289 printf_P(PSTR("Starting application at 0x%04lx ...\n"), addr);
290
291 if (addr != 0) {
292 uint8_t tmp[3];
293
294 z80_bus_cmd(Request);
295 z80_read_block (tmp, 0, 3);
296 z80_write(0, 0xc3);
297 z80_write(1, addr);
298 z80_write(2, (addr >> 8));
299
300 z80_bus_cmd(Run);
301 _delay_us(10);
302 z80_bus_cmd(M_Cycle);
303 _delay_us(10);
304 z80_bus_cmd(M_Cycle);
305 _delay_us(10);
306 z80_write_block(tmp, 0, 3);
307 } else {
308 if (!hold)
309 z80_bus_cmd(Request);
310 z80_bus_cmd(Run);
311 }
312 if (!hold)
313 z80_bus_cmd(Release);
314
315 return CMD_RET_SUCCESS;
316 }
317
318 static
319 void reset_cpu(bus_cmd_t mode)
320 {
321 restart_z180_serv();
322 z80_bus_cmd(mode);
323 }
324
325
326 command_ret_t do_reset(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED)
327 {
328 printf_P(PSTR("CPU now in reset state.\n"));
329
330 reset_cpu(Reset);
331 return CMD_RET_SUCCESS;
332 }
333
334 command_ret_t do_restart(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED)
335 {
336 reset_cpu(Restart);
337
338 return CMD_RET_SUCCESS;
339 }
340
341 static
342 void print_con_usage(char esc)
343 { printf_P(PSTR("\n"
344 "------------------------------------------------\n"
345 " ?,H - This Help\n"
346 " Q,X - Return to command line\n"
347 " R - Reset (Restart) CPU\n"
348 " : - Execute monitor command\n"
349 " \\ - code input:\n"
350 " \\nnn 3 decimal digits character code\n"
351 " \\Xhh 2 hexadecimal digits character code\n"
352 " ^%c - (Escape char) Type again to send itself\n"
353 "key>"
354 ), esc + 0x40);
355 }
356
357 command_ret_t do_console(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED)
358 {
359 int ch;
360 uint8_t pending;
361 uint8_t code = 0;
362 uint8_t state = 0;
363 char esc_char = (char) getenv_ulong(PSTR(ENV_ESC_CHAR), 16, CONFIG_ESC_CHAR);
364
365 printf_P(PSTR("Connecting to CPU. Escape character is '^%c'.\n"),
366 esc_char + 0x40);
367
368 while (1) {
369
370 ATOMIC_BLOCK(ATOMIC_FORCEON) {
371 pending = (Stat & S_CON_PENDING) != 0;
372 Stat &= ~S_CON_PENDING;
373 }
374 if (pending) {
375 uint8_t count = 100;
376 while ((ch = z80_memfifo_getc(fifo_conout)) >= 0 && --count)
377 putchar(ch);
378 }
379
380 if ((ch = my_getchar(0)) >= 0) {
381 switch (state) {
382 case 0:
383 if (ch == esc_char) {
384 state = 1;
385 /* TODO: Timer starten */
386 } else {
387 z80_memfifo_putc(fifo_conin, ch);
388 }
389 break;
390 case 2:
391 my_puts_P(PSTR("\n"
392 "------------------------------------------------\n"));
393 case 1:
394 state = 0;
395 switch (toupper(ch)) {
396
397 case '?':
398 case 'H':
399 print_con_usage(esc_char);
400 state = 2;
401 break;
402
403 case 'R':
404 reset_cpu(Restart);
405 break;
406
407 case 'X':
408 case 'Q':
409 putchar('\n');
410 goto quit;
411 break;
412
413 case ':':
414 putchar('\n');
415 int cmdlen = cli_readline(PSTR(": "), 1);
416 if (cmdlen > 0)
417 run_command(console_buffer, 0);
418 break;
419
420 case '\\':
421 code = 0;
422 state = 3;
423 break;
424
425 default:
426 if (ch == esc_char)
427 z80_memfifo_putc(fifo_conin, ch);
428 break;
429 }
430 break;
431 case 3:
432 if (toupper(ch) == 'X') {
433 state = 6;
434 break;
435 }
436 /* fall thru */
437 case 4:
438 case 5:
439 if (isdigit(ch)) {
440 code = code * 10 + ch - '0';
441 state++;
442 } else {
443 if (state > 3)
444 z80_memfifo_putc(fifo_conin, code);
445 z80_memfifo_putc(fifo_conin, ch);
446 state = 0;
447 }
448 if (state > 5) {
449 z80_memfifo_putc(fifo_conin, code);
450 state = 0;
451 }
452 break;
453 case 6:
454 case 7:
455 if (isxdigit(ch)) {
456 ch = toupper(ch);
457 if (ch >= 'A')
458 ch -= 'A' - 10;
459 code = code * 16 + ch - '0';
460 state++;
461 }else {
462 if (state > 6)
463 z80_memfifo_putc(fifo_conin, code);
464 z80_memfifo_putc(fifo_conin, ch);
465 state = 0;
466 }
467 if (state > 7) {
468 z80_memfifo_putc(fifo_conin, code);
469 state = 0;
470 }
471 break;
472 }
473 }
474 }
475 quit:
476 return CMD_RET_SUCCESS;
477 }