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