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"
35 static void z80_load_mem(int_fast8_t verbosity
,
36 const FLASH
unsigned char data
[],
37 const FLASH
unsigned long *sections
,
38 const FLASH
unsigned long address
[],
39 const FLASH
unsigned long length_of_sections
[])
41 uint32_t sec_base
= 0;
44 printf_P(PSTR("Loading Z180 memory... \n"));
46 for (unsigned sec
= 0; sec
< *sections
; sec
++) {
48 printf_P(PSTR(" From: 0x%.5lX to: 0x%.5lX (%5li bytes)\n"),
50 address
[sec
]+length_of_sections
[sec
] - 1,
51 length_of_sections
[sec
]);
54 z80_write_block_P((const FLASH
unsigned char *) &data
[sec_base
], /* src */
55 address
[sec
], /* dest */
56 length_of_sections
[sec
]); /* len */
57 sec_base
+= length_of_sections
[sec
];
61 command_ret_t
do_loadf(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
63 (void) cmdtp
; (void) flag
; (void) argc
; (void) argv
;
65 if (z80_bus_state() & ZST_RUNNING
) {
66 my_puts_P(PSTR("Can't load while CPU is running!\n"));
67 return CMD_RET_FAILURE
;
69 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
70 my_puts_P(PSTR("Bus timeout\n"));
71 return CMD_RET_FAILURE
;
73 z80_load_mem(2, hdrom
,
76 hdrom_length_of_sections
);
80 return CMD_RET_SUCCESS
;
84 void print_vars(char *title
)
87 zstate_t state
= z80_bus_state();
89 if((state
& ZST_ACQUIRED
) == 0)
92 z80_read_block(buf
, 9, sizeof buf
);
94 if((state
& ZST_ACQUIRED
) == 0)
97 printf_P(PSTR("%s: stage: %d, flag: 0x%.02x, result: %d, IDE stat/error: 0x%.02x/0x%.02x\n"),
98 title
, buf
[0], buf
[1], buf
[2], buf
[3], buf
[4]);
106 * -s start sector (0)
107 * -c sector count (7)
108 * -i Partition id (52)
114 command_ret_t
do_bootcf(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
134 int_fast8_t verbosity
= 0;
135 uint8_t default_stages
;
138 (void) cmdtp
; (void) flag
;
140 /* get default values */
141 memcpy_P(&boot_param
, cfboot
, sizeof boot_param
);
142 default_stages
= boot_param
.stages
;
148 while ((opt
= getopt(argc
, argv
, PSTR("vna:s:c:t:i:"))) != -1) {
154 if (boot_param
.stages
> 0)
158 val
= eval_arg(optarg
, NULL
);
159 if (val
< 0x100 || val
> 0xFE00) {
160 printf_P(PSTR("Address out of range: 0x%.4lX\n"), val
);
161 return CMD_RET_FAILURE
;
163 boot_param
.loadaddr
= val
;
166 val
= eval_arg(optarg
, NULL
);
168 printf_P(PSTR("Start sector out of range: 0x%lX\n"), val
);
169 return CMD_RET_FAILURE
;
171 boot_param
.sec_start
= val
;
174 val
= eval_arg(optarg
, NULL
);
176 printf_P(PSTR("Sector count out of range: 0x%lX\n"), val
);
177 return CMD_RET_FAILURE
;
179 boot_param
.sec_cnt
= val
;
182 val
= eval_arg(optarg
, NULL
);
183 if (val
< 0x1 || val
> 0xFFFF) {
184 printf_P(PSTR("Timeout value out of range: 0x%lX\n"), val
);
185 return CMD_RET_FAILURE
;
187 boot_param
.timeout
= val
;
190 val
= eval_arg(optarg
, NULL
);
191 if (val
< 0x01 || val
> 0xFF) {
192 printf_P(PSTR("Partition id out of range: 0x%lX\n"), val
);
193 return CMD_RET_FAILURE
;
195 boot_param
.part_id
= val
;
198 return CMD_RET_USAGE
;
202 /* remaining arguments */
205 my_puts_P(PSTR("Argument error!\n"));
206 return CMD_RET_USAGE
;
209 if ((val
= (uint32_t) boot_param
.loadaddr
+ boot_param
.sec_cnt
* 512) >= 0xFF00) {
210 printf_P(PSTR("Top address out of range: 0x%.4lX\n"), val
);
211 return CMD_RET_FAILURE
;
216 if (z80_bus_state() & ZST_RUNNING
) {
217 my_puts_P(PSTR("CPU is allready running!\n"));
218 return CMD_RET_FAILURE
;
220 if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
221 my_puts_P(PSTR("Bus timeout\n"));
222 return CMD_RET_FAILURE
;
224 z80_load_mem(verbosity
, cfboot
,
227 cfboot_length_of_sections
);
229 z80_write_block((const uint8_t *) &boot_param
,
230 cfboot_address
[0], sizeof boot_param
);
231 z80_bus_cmd(Release
);
233 if (boot_param
.stages
== 0) {
234 printf_P(PSTR("Bootloader loaded at: 0x%.4X\n"), (uint16_t) cfboot_address
[0]);
236 printf_P(PSTR("Executing %d of %d Bootloader stages...\n"),
237 boot_param
.stages
, default_stages
);
240 z80_bus_cmd(Release
);
242 clear_ctrlc(); /* forget any previous Control C */
243 for (boot_res
.done
= 0; boot_res
.done
!= 0xFF;) {
245 /* check for ctrl-c to abort... */
246 if (had_ctrlc() || ctrlc()) {
249 z80_bus_cmd(Request
);
250 z80_read_block((uint8_t *) &boot_res
,
251 cfboot_address
[0]+sizeof boot_param
- 1, sizeof boot_res
);
252 z80_bus_cmd(Release
);
255 if (boot_res
.done
!= 0xFF) {
257 my_puts_P(PSTR("Abort\n"));
259 if (boot_param
.stages
== default_stages
&&
260 boot_res
.stages
== 0 &&
261 boot_res
.result
== 0) {
262 my_puts_P(PSTR("Booting...\n"));
266 printf_P(PSTR("Bootloader stopped at stage %d, result: %d, IDE stat/error: 0x%.02x/0x%.02x\n"),
267 boot_param
.stages
- boot_res
.stages
,
268 boot_res
.result
, boot_res
.ide_stat
, boot_res
.ide_error
);
273 return CMD_RET_SUCCESS
;
276 command_ret_t
do_busreq_pulse(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
280 (void) cmdtp
; (void) flag
;
282 if (!(z80_bus_state() & ZST_RUNNING
)) {
283 printf_P(PSTR("## CPU is not running!\n"));
284 return CMD_RET_FAILURE
;
288 count
= (uint16_t) eval_arg(argv
[1], NULL
);
290 z80_bus_cmd(Request
);
292 z80_bus_cmd(M_Cycle
);
294 return CMD_RET_SUCCESS
;
298 command_ret_t
do_go(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
302 (void) cmdtp
; (void) flag
;
305 return CMD_RET_USAGE
;
306 addr
= eval_arg(argv
[1], NULL
);
307 if (addr
>= (1UL<<16)) {
308 printf_P(PSTR("## Startaddress 0x%05lx too high.\n"
309 " (Out of logical address space (0x00000-0x0ffff))\n"),
311 return CMD_RET_FAILURE
;
314 if (z80_bus_state() & ZST_RUNNING
) {
315 printf_P(PSTR("## CPU allready running!\n"));
316 return CMD_RET_FAILURE
;
319 printf_P(PSTR("## Starting application at 0x%04lx ...\n"), addr
);
324 z80_bus_cmd(Request
);
325 z80_read_block (tmp
, 0, 3);
328 z80_write(2, (addr
>> 8));
331 z80_bus_cmd(M_Cycle
);
332 z80_bus_cmd(M_Cycle
);
333 z80_write_block(tmp
, 0, 3);
337 z80_bus_cmd(Release
);
339 return CMD_RET_SUCCESS
;
343 void reset_cpu(bus_cmd_t mode
)
350 command_ret_t
do_reset(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
352 (void) cmdtp
; (void) flag
; (void) argc
; (void) argv
;
354 printf_P(PSTR("CPU now in reset state.\n"));
357 return CMD_RET_SUCCESS
;
360 command_ret_t
do_restart(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
362 (void) cmdtp
; (void) flag
; (void) argc
; (void) argv
;
366 return CMD_RET_SUCCESS
;
370 void print_con_usage(char esc
)
372 "------------------------------------------------\n"
374 " Q,X - Return to command line\n"
375 " R - Reset (Restart) CPU\n"
376 " : - Execute monitor command\n"
377 " \\ - code input:\n"
378 " \\nnn 3 decimal digits character code\n"
379 " \\Xhh 2 hexadecimal digits character code\n"
380 " ^%c - (Escape char) Type again to send itself\n"
385 command_ret_t
do_console(cmd_tbl_t
*cmdtp
, uint_fast8_t flag
, int argc
, char * const argv
[])
389 // uint8_t help_prompt = 0;
392 char esc_char
= (char) getenv_ulong(PSTR(ENV_ESC_CHAR
), 16, CONFIG_ESC_CHAR
);
394 (void) cmdtp
; (void) flag
; (void) argc
; (void) argv
;
396 printf_P(PSTR("Connecting to CPU. Escape character is '^%c'.\n"),
401 ATOMIC_BLOCK(ATOMIC_FORCEON
) {
402 pending
= (Stat
& S_CON_PENDING
) != 0;
403 Stat
&= ~S_CON_PENDING
;
407 while ((ch
= z80_memfifo_getc(fifo_conout
)) >= 0 && --count
)
411 if ((ch
= my_getchar(0)) >= 0) {
414 if (ch
== esc_char
) {
416 /* TODO: Timer starten */
418 z80_memfifo_putc(fifo_conin
, ch
);
423 "------------------------------------------------\n"));
426 switch (toupper(ch
)) {
430 print_con_usage(esc_char
);
440 printf_P(PSTR("\n"));
446 int cmdlen
= cli_readline(PSTR(": "), 1);
448 run_command(console_buffer
, 0);
458 z80_memfifo_putc(fifo_conin
, ch
);
463 if (toupper(ch
) == 'X') {
471 code
= code
* 10 + ch
- '0';
475 z80_memfifo_putc(fifo_conin
, code
);
476 z80_memfifo_putc(fifo_conin
, ch
);
480 z80_memfifo_putc(fifo_conin
, code
);
490 code
= code
* 16 + ch
- '0';
494 z80_memfifo_putc(fifo_conin
, code
);
495 z80_memfifo_putc(fifo_conin
, ch
);
499 z80_memfifo_putc(fifo_conin
, code
);
507 return CMD_RET_SUCCESS
;