2 * (C) Copyright 2014-2016 Leo C. <erbl259-lmu@yahoo.de>
4 * (C) Copyright 2000-2003
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
7 * SPDX-License-Identifier: GPL-2.0
15 #include <util/atomic.h>
17 #include "cli_readline.h" /* console_buffer[] */
18 #include "cli.h" /* run_command() */
21 #include "con-utils.h"
22 #include "getopt-min.h"
24 #include "z180-serv.h" /* restart_z180_serv() */
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"
34 command_ret_t
do_loadf(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
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
,
42 hdrom_length_of_sections
);
46 return CMD_RET_SUCCESS
;
50 void print_vars(char *title
)
53 zstate_t state
= z80_bus_state();
55 if((state
& ZST_ACQUIRED
) == 0)
58 z80_read_block(buf
, 9, sizeof buf
);
60 if((state
& ZST_ACQUIRED
) == 0)
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]);
74 * -i Partition id (52)
80 command_ret_t
do_bootcf(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
100 int_fast8_t verbosity
= 0;
101 uint8_t default_stages
;
103 ERRNUM res
= ESUCCESS
;
105 /* get default values */
106 memcpy_P(&boot_param
, cfboot
, sizeof boot_param
);
107 default_stages
= boot_param
.stages
;
113 while ((opt
= getopt(argc
, argv
, PSTR("vna:s:c:t:i:"))) != -1) {
119 if (boot_param
.stages
> 0)
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
;
128 boot_param
.loadaddr
= val
;
131 val
= eval_arg(optarg
, NULL
);
133 printf_P(PSTR("Start sector out of range: 0x%lX\n"), val
);
134 return CMD_RET_FAILURE
;
136 boot_param
.sec_start
= val
;
139 val
= eval_arg(optarg
, NULL
);
141 printf_P(PSTR("Sector count out of range: 0x%lX\n"), val
);
142 return CMD_RET_FAILURE
;
144 boot_param
.sec_cnt
= val
;
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
;
152 boot_param
.timeout
= val
;
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
;
160 boot_param
.part_id
= val
;
163 return CMD_RET_USAGE
;
167 /* remaining arguments */
170 my_puts_P(PSTR("Argument error!\n"));
171 return CMD_RET_USAGE
;
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
;
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
,
185 cfboot_length_of_sections
);
187 z80_write_block((const uint8_t *) &boot_param
,
188 cfboot_address
[0], sizeof boot_param
);
189 z80_bus_cmd(Release
);
191 if (boot_param
.stages
== 0) {
192 printf_P(PSTR("Bootloader loaded at: 0x%.4X\n"), (uint16_t) cfboot_address
[0]);
194 printf_P(PSTR("Executing %d of %d Bootloader stages...\n"),
195 boot_param
.stages
, default_stages
);
198 z80_bus_cmd(Release
);
200 clear_ctrlc(); /* forget any previous Control C */
201 for (boot_res
.done
= 0; boot_res
.done
!= 0xFF;) {
203 /* check for ctrl-c to abort... */
204 if (had_ctrlc() || ctrlc()) {
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
);
213 if (boot_res
.done
!= 0xFF) {
215 my_puts_P(PSTR("Abort\n"));
217 if (boot_param
.stages
== default_stages
&&
218 boot_res
.stages
== 0 &&
219 boot_res
.result
== 0) {
220 my_puts_P(PSTR("Booting...\n"));
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
);
231 return CMD_RET_SUCCESS
;
234 command_ret_t
do_busreq_pulse(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
238 if (!(z80_bus_state() & ZST_RUNNING
)) {
239 printf_P(PSTR("## CPU is not running!\n"));
240 return CMD_RET_FAILURE
;
244 count
= (uint16_t) eval_arg(argv
[1], NULL
);
246 z80_bus_cmd(Request
);
248 z80_bus_cmd(M_Cycle
);
250 return CMD_RET_SUCCESS
;
254 command_ret_t
do_go(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
259 return CMD_RET_USAGE
;
260 addr
= eval_arg(argv
[1], NULL
);
261 if (addr
>= (1UL<<16)) {
262 printf_P(PSTR("Invalid startaddress: 0x%05lx\n"
263 " (Out of logical address space (0x00000-0x0ffff))\n"),
265 return CMD_RET_FAILURE
;
268 if (z80_bus_state() & ZST_RUNNING
) {
269 printf_P(PSTR("CPU already running!\n"));
270 return CMD_RET_FAILURE
;
273 printf_P(PSTR("Starting application at 0x%04lx ...\n"), addr
);
278 z80_bus_cmd(Request
);
279 z80_read_block (tmp
, 0, 3);
282 z80_write(2, (addr
>> 8));
285 z80_bus_cmd(M_Cycle
);
286 z80_bus_cmd(M_Cycle
);
287 z80_write_block(tmp
, 0, 3);
291 z80_bus_cmd(Release
);
293 return CMD_RET_SUCCESS
;
297 void reset_cpu(bus_cmd_t mode
)
304 command_ret_t
do_reset(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
306 printf_P(PSTR("CPU now in reset state.\n"));
309 return CMD_RET_SUCCESS
;
312 command_ret_t
do_restart(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
316 return CMD_RET_SUCCESS
;
320 void print_con_usage(char esc
)
322 "------------------------------------------------\n"
324 " Q,X - Return to command line\n"
325 " R - Reset (Restart) CPU\n"
326 " : - Execute monitor command\n"
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"
335 command_ret_t
do_console(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
341 char esc_char
= (char) getenv_ulong(PSTR(ENV_ESC_CHAR
), 16, CONFIG_ESC_CHAR
);
343 printf_P(PSTR("Connecting to CPU. Escape character is '^%c'.\n"),
348 ATOMIC_BLOCK(ATOMIC_FORCEON
) {
349 pending
= (Stat
& S_CON_PENDING
) != 0;
350 Stat
&= ~S_CON_PENDING
;
354 while ((ch
= z80_memfifo_getc(fifo_conout
)) >= 0 && --count
)
358 if ((ch
= my_getchar(0)) >= 0) {
361 if (ch
== esc_char
) {
363 /* TODO: Timer starten */
365 z80_memfifo_putc(fifo_conin
, ch
);
370 "------------------------------------------------\n"));
373 switch (toupper(ch
)) {
377 print_con_usage(esc_char
);
393 int cmdlen
= cli_readline(PSTR(": "), 1);
395 run_command(console_buffer
, 0);
405 z80_memfifo_putc(fifo_conin
, ch
);
410 if (toupper(ch
) == 'X') {
418 code
= code
* 10 + ch
- '0';
422 z80_memfifo_putc(fifo_conin
, code
);
423 z80_memfifo_putc(fifo_conin
, ch
);
427 z80_memfifo_putc(fifo_conin
, code
);
437 code
= code
* 16 + ch
- '0';
441 z80_memfifo_putc(fifo_conin
, code
);
442 z80_memfifo_putc(fifo_conin
, ch
);
446 z80_memfifo_putc(fifo_conin
, code
);
454 return CMD_RET_SUCCESS
;