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
;
104 /* get default values */
105 memcpy_P(&boot_param
, cfboot
, sizeof boot_param
);
106 default_stages
= boot_param
.stages
;
109 while ((opt
= getopt(argc
, argv
, PSTR("vna:s:c:t:i:"))) != -1) {
115 if (boot_param
.stages
> 0)
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
;
124 boot_param
.loadaddr
= val
;
127 val
= eval_arg(optarg
, NULL
);
129 printf_P(PSTR("Start sector out of range: 0x%lX\n"), val
);
130 return CMD_RET_FAILURE
;
132 boot_param
.sec_start
= val
;
135 val
= eval_arg(optarg
, NULL
);
137 printf_P(PSTR("Sector count out of range: 0x%lX\n"), val
);
138 return CMD_RET_FAILURE
;
140 boot_param
.sec_cnt
= val
;
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
;
148 boot_param
.timeout
= val
;
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
;
156 boot_param
.part_id
= val
;
159 return CMD_RET_USAGE
;
163 /* remaining arguments */
166 my_puts_P(PSTR("Argument error!\n"));
167 return CMD_RET_USAGE
;
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
;
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
,
181 cfboot_length_of_sections
);
183 z80_write_block((const uint8_t *) &boot_param
,
184 cfboot_address
[0], sizeof boot_param
);
185 z80_bus_cmd(Release
);
187 if (boot_param
.stages
== 0) {
188 printf_P(PSTR("Bootloader loaded at: 0x%.4X\n"), (uint16_t) cfboot_address
[0]);
190 printf_P(PSTR("Executing %d of %d Bootloader stages...\n"),
191 boot_param
.stages
, default_stages
);
194 z80_bus_cmd(Release
);
196 clear_ctrlc(); /* forget any previous Control C */
197 for (boot_res
.done
= 0; boot_res
.done
!= 0xFF;) {
199 /* check for ctrl-c to abort... */
200 if (had_ctrlc() || ctrlc()) {
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
);
209 if (boot_res
.done
!= 0xFF) {
211 my_puts_P(PSTR("Abort\n"));
213 if (boot_param
.stages
== default_stages
&&
214 boot_res
.stages
== 0 &&
215 boot_res
.result
== 0) {
216 my_puts_P(PSTR("Booting...\n"));
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
);
227 return CMD_RET_SUCCESS
;
230 command_ret_t
do_busreq_pulse(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
234 if (!(z80_bus_state() & ZST_RUNNING
)) {
235 printf_P(PSTR("## CPU is not running!\n"));
236 return CMD_RET_FAILURE
;
240 count
= (uint16_t) eval_arg(argv
[1], NULL
);
242 z80_bus_cmd(Request
);
244 z80_bus_cmd(M_Cycle
);
246 return CMD_RET_SUCCESS
;
250 command_ret_t
do_go(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
256 while ((opt
= getopt(argc
, argv
, PSTR("h"))) != -1) {
262 return CMD_RET_USAGE
;
265 argc
-= optind
; /* remaining arguments */
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"),
274 return CMD_RET_FAILURE
;
277 if (z80_bus_state() & ZST_RUNNING
) {
278 printf_P(PSTR("CPU already running!\n"));
279 return CMD_RET_FAILURE
;
282 printf_P(PSTR("Starting application at 0x%04lx ...\n"), addr
);
287 z80_bus_cmd(Request
);
288 z80_read_block (tmp
, 0, 3);
291 z80_write(2, (addr
>> 8));
295 z80_bus_cmd(M_Cycle
);
297 z80_bus_cmd(M_Cycle
);
299 z80_write_block(tmp
, 0, 3);
302 z80_bus_cmd(Request
);
306 z80_bus_cmd(Release
);
308 return CMD_RET_SUCCESS
;
312 void reset_cpu(bus_cmd_t mode
)
319 command_ret_t
do_reset(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
321 printf_P(PSTR("CPU now in reset state.\n"));
324 return CMD_RET_SUCCESS
;
327 command_ret_t
do_restart(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
331 return CMD_RET_SUCCESS
;
335 void print_con_usage(char esc
)
337 "------------------------------------------------\n"
339 " Q,X - Return to command line\n"
340 " R - Reset (Restart) CPU\n"
341 " : - Execute monitor command\n"
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"
350 command_ret_t
do_console(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
356 char esc_char
= (char) getenv_ulong(PSTR(ENV_ESC_CHAR
), 16, CONFIG_ESC_CHAR
);
358 printf_P(PSTR("Connecting to CPU. Escape character is '^%c'.\n"),
363 ATOMIC_BLOCK(ATOMIC_FORCEON
) {
364 pending
= (Stat
& S_CON_PENDING
) != 0;
365 Stat
&= ~S_CON_PENDING
;
369 while ((ch
= z80_memfifo_getc(fifo_conout
)) >= 0 && --count
)
373 if ((ch
= my_getchar(0)) >= 0) {
376 if (ch
== esc_char
) {
378 /* TODO: Timer starten */
380 z80_memfifo_putc(fifo_conin
, ch
);
385 "------------------------------------------------\n"));
388 switch (toupper(ch
)) {
392 print_con_usage(esc_char
);
408 int cmdlen
= cli_readline(PSTR(": "), 1);
410 run_command(console_buffer
, 0);
420 z80_memfifo_putc(fifo_conin
, ch
);
425 if (toupper(ch
) == 'X') {
433 code
= code
* 10 + ch
- '0';
437 z80_memfifo_putc(fifo_conin
, code
);
438 z80_memfifo_putc(fifo_conin
, ch
);
442 z80_memfifo_putc(fifo_conin
, code
);
452 code
= code
* 16 + ch
- '0';
456 z80_memfifo_putc(fifo_conin
, code
);
457 z80_memfifo_putc(fifo_conin
, ch
);
461 z80_memfifo_putc(fifo_conin
, code
);
469 return CMD_RET_SUCCESS
;