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