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