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 UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
63 ERRNUM res
= ESUCCESS
;
65 if (z80_bus_state() & ZST_RUNNING
) {
67 } else if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
71 cmd_error(CMD_RET_FAILURE
, res
, NULL
);
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 UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
134 int_fast8_t verbosity
= 0;
135 uint8_t default_stages
;
137 ERRNUM res
= ESUCCESS
;
139 /* get default values */
140 memcpy_P(&boot_param
, cfboot
, sizeof boot_param
);
141 default_stages
= boot_param
.stages
;
147 while ((opt
= getopt(argc
, argv
, PSTR("vna:s:c:t:i:"))) != -1) {
153 if (boot_param
.stages
> 0)
157 val
= eval_arg(optarg
, NULL
);
158 if (val
< 0x100 || val
> 0xFE00) {
159 printf_P(PSTR("Address out of range: 0x%.4lX\n"), val
);
160 return CMD_RET_FAILURE
;
162 boot_param
.loadaddr
= val
;
165 val
= eval_arg(optarg
, NULL
);
167 printf_P(PSTR("Start sector out of range: 0x%lX\n"), val
);
168 return CMD_RET_FAILURE
;
170 boot_param
.sec_start
= val
;
173 val
= eval_arg(optarg
, NULL
);
175 printf_P(PSTR("Sector count out of range: 0x%lX\n"), val
);
176 return CMD_RET_FAILURE
;
178 boot_param
.sec_cnt
= val
;
181 val
= eval_arg(optarg
, NULL
);
182 if (val
< 0x1 || val
> 0xFFFF) {
183 printf_P(PSTR("Timeout value out of range: 0x%lX\n"), val
);
184 return CMD_RET_FAILURE
;
186 boot_param
.timeout
= val
;
189 val
= eval_arg(optarg
, NULL
);
190 if (val
< 0x01 || val
> 0xFF) {
191 printf_P(PSTR("Partition id out of range: 0x%lX\n"), val
);
192 return CMD_RET_FAILURE
;
194 boot_param
.part_id
= val
;
197 return CMD_RET_USAGE
;
201 /* remaining arguments */
204 my_puts_P(PSTR("Argument error!\n"));
205 return CMD_RET_USAGE
;
208 if ((val
= (uint32_t) boot_param
.loadaddr
+ boot_param
.sec_cnt
* 512) >= 0xFF00) {
209 printf_P(PSTR("Top address out of range: 0x%.4lX\n"), val
);
210 return CMD_RET_FAILURE
;
213 if (z80_bus_state() & ZST_RUNNING
) {
215 } else if (!(z80_bus_cmd(Request
) & ZST_ACQUIRED
)) {
219 cmd_error(CMD_RET_FAILURE
, res
, NULL
);
221 z80_load_mem(verbosity
, cfboot
,
224 cfboot_length_of_sections
);
226 z80_write_block((const uint8_t *) &boot_param
,
227 cfboot_address
[0], sizeof boot_param
);
228 z80_bus_cmd(Release
);
230 if (boot_param
.stages
== 0) {
231 printf_P(PSTR("Bootloader loaded at: 0x%.4X\n"), (uint16_t) cfboot_address
[0]);
233 printf_P(PSTR("Executing %d of %d Bootloader stages...\n"),
234 boot_param
.stages
, default_stages
);
237 z80_bus_cmd(Release
);
239 clear_ctrlc(); /* forget any previous Control C */
240 for (boot_res
.done
= 0; boot_res
.done
!= 0xFF;) {
242 /* check for ctrl-c to abort... */
243 if (had_ctrlc() || ctrlc()) {
246 z80_bus_cmd(Request
);
247 z80_read_block((uint8_t *) &boot_res
,
248 cfboot_address
[0]+sizeof boot_param
- 1, sizeof boot_res
);
249 z80_bus_cmd(Release
);
252 if (boot_res
.done
!= 0xFF) {
254 my_puts_P(PSTR("Abort\n"));
256 if (boot_param
.stages
== default_stages
&&
257 boot_res
.stages
== 0 &&
258 boot_res
.result
== 0) {
259 my_puts_P(PSTR("Booting...\n"));
263 printf_P(PSTR("Bootloader stopped at stage %d, result: %d, IDE stat/error: 0x%.02x/0x%.02x\n"),
264 boot_param
.stages
- boot_res
.stages
,
265 boot_res
.result
, boot_res
.ide_stat
, boot_res
.ide_error
);
270 return CMD_RET_SUCCESS
;
273 command_ret_t
do_busreq_pulse(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
277 if (!(z80_bus_state() & ZST_RUNNING
)) {
278 printf_P(PSTR("## CPU is not running!\n"));
279 return CMD_RET_FAILURE
;
283 count
= (uint16_t) eval_arg(argv
[1], NULL
);
285 z80_bus_cmd(Request
);
287 z80_bus_cmd(M_Cycle
);
289 return CMD_RET_SUCCESS
;
293 command_ret_t
do_go(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc
, char * const argv
[])
298 return CMD_RET_USAGE
;
299 addr
= eval_arg(argv
[1], NULL
);
300 if (addr
>= (1UL<<16)) {
301 printf_P(PSTR("## Startaddress 0x%05lx too high.\n"
302 " (Out of logical address space (0x00000-0x0ffff))\n"),
304 return CMD_RET_FAILURE
;
307 if (z80_bus_state() & ZST_RUNNING
) {
308 printf_P(PSTR("## CPU already running!\n"));
309 return CMD_RET_FAILURE
;
312 printf_P(PSTR("## Starting application at 0x%04lx ...\n"), addr
);
317 z80_bus_cmd(Request
);
318 z80_read_block (tmp
, 0, 3);
321 z80_write(2, (addr
>> 8));
324 z80_bus_cmd(M_Cycle
);
325 z80_bus_cmd(M_Cycle
);
326 z80_write_block(tmp
, 0, 3);
330 z80_bus_cmd(Release
);
332 return CMD_RET_SUCCESS
;
336 void reset_cpu(bus_cmd_t mode
)
343 command_ret_t
do_reset(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
345 printf_P(PSTR("CPU now in reset state.\n"));
348 return CMD_RET_SUCCESS
;
351 command_ret_t
do_restart(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
355 return CMD_RET_SUCCESS
;
359 void print_con_usage(char esc
)
361 "------------------------------------------------\n"
363 " Q,X - Return to command line\n"
364 " R - Reset (Restart) CPU\n"
365 " : - Execute monitor command\n"
366 " \\ - code input:\n"
367 " \\nnn 3 decimal digits character code\n"
368 " \\Xhh 2 hexadecimal digits character code\n"
369 " ^%c - (Escape char) Type again to send itself\n"
374 command_ret_t
do_console(cmd_tbl_t
*cmdtp UNUSED
, uint_fast8_t flag UNUSED
, int argc UNUSED
, char * const argv
[] UNUSED
)
380 char esc_char
= (char) getenv_ulong(PSTR(ENV_ESC_CHAR
), 16, CONFIG_ESC_CHAR
);
382 printf_P(PSTR("Connecting to CPU. Escape character is '^%c'.\n"),
387 ATOMIC_BLOCK(ATOMIC_FORCEON
) {
388 pending
= (Stat
& S_CON_PENDING
) != 0;
389 Stat
&= ~S_CON_PENDING
;
393 while ((ch
= z80_memfifo_getc(fifo_conout
)) >= 0 && --count
)
397 if ((ch
= my_getchar(0)) >= 0) {
400 if (ch
== esc_char
) {
402 /* TODO: Timer starten */
404 z80_memfifo_putc(fifo_conin
, ch
);
409 "------------------------------------------------\n"));
412 switch (toupper(ch
)) {
416 print_con_usage(esc_char
);
432 int cmdlen
= cli_readline(PSTR(": "), 1);
434 run_command(console_buffer
, 0);
444 z80_memfifo_putc(fifo_conin
, ch
);
449 if (toupper(ch
) == 'X') {
457 code
= code
* 10 + ch
- '0';
461 z80_memfifo_putc(fifo_conin
, code
);
462 z80_memfifo_putc(fifo_conin
, ch
);
466 z80_memfifo_putc(fifo_conin
, code
);
476 code
= code
* 16 + ch
- '0';
480 z80_memfifo_putc(fifo_conin
, code
);
481 z80_memfifo_putc(fifo_conin
, ch
);
485 z80_memfifo_putc(fifo_conin
, code
);
493 return CMD_RET_SUCCESS
;