]>
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; |
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 L |
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': | |
16af58ea L |
119 | if (boot_param.stages > 0) |
120 | boot_param.stages--; | |
a2907f2e L |
121 | break; |
122 | case 'a': | |
2d914b45 | 123 | val = eval_arg(optarg, NULL); |
a2907f2e L |
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': | |
2d914b45 | 131 | val = eval_arg(optarg, NULL); |
a2907f2e L |
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': | |
2d914b45 | 139 | val = eval_arg(optarg, NULL); |
a2907f2e L |
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': | |
2d914b45 | 147 | val = eval_arg(optarg, NULL); |
a2907f2e L |
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 | } | |
16af58ea | 152 | boot_param.timeout = val; |
a2907f2e L |
153 | break; |
154 | case 'i': | |
2d914b45 | 155 | val = eval_arg(optarg, NULL); |
a2907f2e L |
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 | ||
e9d96859 L |
179 | if (z80_bus_state() & ZST_RUNNING) |
180 | cmd_error(CMD_RET_FAILURE, ERUNNING, NULL); | |
181 | z80_bus_request_or_exit(); | |
a2907f2e L |
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 | ||
16af58ea | 191 | if (boot_param.stages == 0) { |
a2907f2e L |
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"), | |
16af58ea | 195 | boot_param.stages, default_stages); |
a2907f2e L |
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 { | |
16af58ea L |
217 | if (boot_param.stages == default_stages && |
218 | boot_res.stages == 0 && | |
219 | boot_res.result == 0) { | |
a2907f2e L |
220 | my_puts_P(PSTR("Booting...\n")); |
221 | } else { | |
222 | z80_bus_cmd(Reset); | |
16af58ea | 223 | boot_res.stages++; |
a2907f2e | 224 | printf_P(PSTR("Bootloader stopped at stage %d, result: %d, IDE stat/error: 0x%.02x/0x%.02x\n"), |
16af58ea | 225 | boot_param.stages - boot_res.stages, |
a2907f2e L |
226 | boot_res.result, boot_res.ide_stat, boot_res.ide_error); |
227 | } | |
228 | } | |
229 | } | |
230 | ||
231 | return CMD_RET_SUCCESS; | |
232 | } | |
233 | ||
5e8ac5e0 | 234 | command_ret_t do_busreq_pulse(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[]) |
534e1dfc | 235 | { |
f338df2a | 236 | uint16_t count=1; |
534e1dfc | 237 | |
6035a17b | 238 | if (!(z80_bus_state() & ZST_RUNNING)) { |
f338df2a | 239 | printf_P(PSTR("## CPU is not running!\n")); |
d0581f88 | 240 | return CMD_RET_FAILURE; |
534e1dfc L |
241 | } |
242 | ||
f338df2a | 243 | if (argc > 1) |
2d914b45 | 244 | count = (uint16_t) eval_arg(argv[1], NULL); |
f338df2a | 245 | |
62f624d3 | 246 | z80_bus_cmd(Request); |
f338df2a | 247 | while (count--) |
62f624d3 | 248 | z80_bus_cmd(M_Cycle); |
534e1dfc | 249 | |
d0581f88 | 250 | return CMD_RET_SUCCESS; |
f338df2a L |
251 | } |
252 | ||
253 | ||
5e8ac5e0 | 254 | command_ret_t do_go(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[]) |
f338df2a L |
255 | { |
256 | uint32_t addr; | |
257 | ||
f338df2a L |
258 | if (argc < 2) |
259 | return CMD_RET_USAGE; | |
2d914b45 | 260 | addr = eval_arg(argv[1], NULL); |
f338df2a | 261 | if (addr >= (1UL<<16)) { |
51dd0948 | 262 | printf_P(PSTR("Invalid startaddress: 0x%05lx\n" |
534e1dfc L |
263 | " (Out of logical address space (0x00000-0x0ffff))\n"), |
264 | addr); | |
d0581f88 | 265 | return CMD_RET_FAILURE; |
6035a17b | 266 | } |
f338df2a | 267 | |
6035a17b | 268 | if (z80_bus_state() & ZST_RUNNING) { |
51dd0948 | 269 | printf_P(PSTR("CPU already running!\n")); |
d0581f88 | 270 | return CMD_RET_FAILURE; |
534e1dfc L |
271 | } |
272 | ||
51dd0948 | 273 | printf_P(PSTR("Starting application at 0x%04lx ...\n"), addr); |
f338df2a L |
274 | |
275 | if (addr != 0) { | |
276 | uint8_t tmp[3]; | |
6035a17b | 277 | |
62f624d3 | 278 | z80_bus_cmd(Request); |
19a463f4 | 279 | z80_read_block (tmp, 0, 3); |
f338df2a L |
280 | z80_write(0, 0xc3); |
281 | z80_write(1, addr); | |
282 | z80_write(2, (addr >> 8)); | |
283 | ||
62f624d3 L |
284 | z80_bus_cmd(Run); |
285 | z80_bus_cmd(M_Cycle); | |
286 | z80_bus_cmd(M_Cycle); | |
19a463f4 | 287 | z80_write_block(tmp, 0, 3); |
f338df2a | 288 | } else |
62f624d3 | 289 | z80_bus_cmd(Run); |
6035a17b | 290 | |
62f624d3 | 291 | z80_bus_cmd(Release); |
f338df2a | 292 | |
d0581f88 | 293 | return CMD_RET_SUCCESS; |
534e1dfc L |
294 | } |
295 | ||
8a7decea L |
296 | static |
297 | void reset_cpu(bus_cmd_t mode) | |
298 | { | |
299 | restart_z180_serv(); | |
300 | z80_bus_cmd(mode); | |
301 | } | |
302 | ||
303 | ||
5e8ac5e0 | 304 | command_ret_t do_reset(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED) |
534e1dfc | 305 | { |
612a6965 | 306 | printf_P(PSTR("CPU now in reset state.\n")); |
534e1dfc | 307 | |
8a7decea | 308 | reset_cpu(Reset); |
d0581f88 | 309 | return CMD_RET_SUCCESS; |
534e1dfc L |
310 | } |
311 | ||
5e8ac5e0 | 312 | command_ret_t do_restart(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED) |
534e1dfc | 313 | { |
8a7decea | 314 | reset_cpu(Restart); |
534e1dfc | 315 | |
d0581f88 | 316 | return CMD_RET_SUCCESS; |
534e1dfc L |
317 | } |
318 | ||
8a7decea L |
319 | static |
320 | void print_con_usage(char esc) | |
321 | { printf_P(PSTR("\n" | |
322 | "------------------------------------------------\n" | |
323 | " ?,H - This Help\n" | |
8a7decea | 324 | " Q,X - Return to command line\n" |
b08e079d L |
325 | " R - Reset (Restart) CPU\n" |
326 | " : - Execute monitor command\n" | |
612a6965 L |
327 | " \\ - code input:\n" |
328 | " \\nnn 3 decimal digits character code\n" | |
329 | " \\Xhh 2 hexadecimal digits character code\n" | |
330 | " ^%c - (Escape char) Type again to send itself\n" | |
8a7decea L |
331 | "key>" |
332 | ), esc + 0x40); | |
333 | } | |
89adce76 | 334 | |
5e8ac5e0 | 335 | command_ret_t do_console(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED) |
89adce76 L |
336 | { |
337 | int ch; | |
8a7decea | 338 | uint8_t pending; |
8a7decea L |
339 | uint8_t code = 0; |
340 | uint8_t state = 0; | |
612a6965 | 341 | char esc_char = (char) getenv_ulong(PSTR(ENV_ESC_CHAR), 16, CONFIG_ESC_CHAR); |
ea6971b8 | 342 | |
b08e079d L |
343 | printf_P(PSTR("Connecting to CPU. Escape character is '^%c'.\n"), |
344 | esc_char + 0x40); | |
89adce76 L |
345 | |
346 | while (1) { | |
347 | ||
8a7decea | 348 | ATOMIC_BLOCK(ATOMIC_FORCEON) { |
89adce76 L |
349 | pending = (Stat & S_CON_PENDING) != 0; |
350 | Stat &= ~S_CON_PENDING; | |
351 | } | |
3ad4143b L |
352 | if (pending) { |
353 | uint8_t count = 100; | |
354 | while ((ch = z80_memfifo_getc(fifo_conout)) >= 0 && --count) | |
89adce76 | 355 | putchar(ch); |
3ad4143b | 356 | } |
89adce76 L |
357 | |
358 | if ((ch = my_getchar(0)) >= 0) { | |
359 | switch (state) { | |
360 | case 0: | |
612a6965 | 361 | if (ch == esc_char) { |
89adce76 L |
362 | state = 1; |
363 | /* TODO: Timer starten */ | |
364 | } else { | |
365 | z80_memfifo_putc(fifo_conin, ch); | |
ea6971b8 | 366 | } |
89adce76 | 367 | break; |
8a7decea | 368 | case 2: |
5e8ac5e0 | 369 | my_puts_P(PSTR("\n" |
8a7decea | 370 | "------------------------------------------------\n")); |
89adce76 | 371 | case 1: |
8a7decea L |
372 | state = 0; |
373 | switch (toupper(ch)) { | |
89adce76 | 374 | |
8a7decea L |
375 | case '?': |
376 | case 'H': | |
612a6965 | 377 | print_con_usage(esc_char); |
8a7decea | 378 | state = 2; |
89adce76 L |
379 | break; |
380 | ||
8a7decea L |
381 | case 'R': |
382 | reset_cpu(Restart); | |
89adce76 L |
383 | break; |
384 | ||
8a7decea | 385 | case 'X': |
89adce76 | 386 | case 'Q': |
5e8ac5e0 | 387 | putchar('\n'); |
89adce76 L |
388 | goto quit; |
389 | break; | |
390 | ||
b08e079d L |
391 | case ':': |
392 | putchar('\n'); | |
8ed66016 | 393 | int cmdlen = cli_readline(PSTR(": "), 1); |
b08e079d L |
394 | if (cmdlen > 0) |
395 | run_command(console_buffer, 0); | |
396 | break; | |
397 | ||
8a7decea L |
398 | case '\\': |
399 | code = 0; | |
400 | state = 3; | |
401 | break; | |
402 | ||
8a7decea | 403 | default: |
612a6965 L |
404 | if (ch == esc_char) |
405 | z80_memfifo_putc(fifo_conin, ch); | |
8a7decea L |
406 | break; |
407 | } | |
408 | break; | |
409 | case 3: | |
410 | if (toupper(ch) == 'X') { | |
411 | state = 6; | |
412 | break; | |
413 | } | |
414 | /* fall thru */ | |
415 | case 4: | |
416 | case 5: | |
417 | if (isdigit(ch)) { | |
418 | code = code * 10 + ch - '0'; | |
419 | state++; | |
b08e079d L |
420 | } else { |
421 | if (state > 3) | |
422 | z80_memfifo_putc(fifo_conin, code); | |
423 | z80_memfifo_putc(fifo_conin, ch); | |
424 | state = 0; | |
8a7decea L |
425 | } |
426 | if (state > 5) { | |
427 | z80_memfifo_putc(fifo_conin, code); | |
428 | state = 0; | |
429 | } | |
430 | break; | |
431 | case 6: | |
432 | case 7: | |
433 | if (isxdigit(ch)) { | |
434 | ch = toupper(ch); | |
435 | if (ch >= 'A') | |
436 | ch -= 'A' - 10; | |
437 | code = code * 16 + ch - '0'; | |
438 | state++; | |
b08e079d L |
439 | }else { |
440 | if (state > 6) | |
441 | z80_memfifo_putc(fifo_conin, code); | |
442 | z80_memfifo_putc(fifo_conin, ch); | |
443 | state = 0; | |
8a7decea L |
444 | } |
445 | if (state > 7) { | |
446 | z80_memfifo_putc(fifo_conin, code); | |
447 | state = 0; | |
89adce76 | 448 | } |
89adce76 L |
449 | break; |
450 | } | |
451 | } | |
89adce76 L |
452 | } |
453 | quit: | |
89adce76 L |
454 | return CMD_RET_SUCCESS; |
455 | } |