]> cloudbase.mooo.com Git - z180-stamp.git/blame - avr/cmd_boot.c
connect command 'esc \' enhancement, new 'esc :' subcommand
[z180-stamp.git] / avr / cmd_boot.c
CommitLineData
35edb766
L
1/*
2 * (C) Copyright 2014 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 */
534e1dfc
L
9
10/*
11 * Misc boot support
12 */
13#include "common.h"
14#include <stdlib.h>
8a7decea 15#include <ctype.h>
89adce76 16#include <util/atomic.h>
534e1dfc
L
17
18#include "command.h"
b08e079d
L
19#include "cli_readline.h"
20#include "cli.h"
612a6965 21#include "env.h"
89adce76 22#include "con-utils.h"
534e1dfc 23#include "z80-if.h"
89adce76 24#include "z180-serv.h"
8a7decea 25#include "debug.h"
534e1dfc
L
26
27/* ugly hack to get Z180 loadfile into flash memory */
28#define const const FLASH
29#include "../z180/hdrom.h"
30#undef const
31
32
33
34static void z80_load_mem(void)
35{
36 unsigned sec = 0;
37 uint32_t sec_base = hdrom_start;
38
39 printf_P(PSTR("Loading Z180 memory... \n"));
40
41 while (sec < hdrom_sections) {
42 printf_P(PSTR(" From: 0x%.5lX to: 0x%.5lX (%5li bytes)\n"),
43 hdrom_address[sec],
44 hdrom_address[sec]+hdrom_length_of_sections[sec] - 1,
45 hdrom_length_of_sections[sec]);
46
ea6971b8 47 z80_write_block_P((const FLASH unsigned char *) &hdrom[sec_base], /* src */
41d36f28 48 hdrom_address[sec], /* dest */
534e1dfc 49 hdrom_length_of_sections[sec]); /* len */
534e1dfc
L
50 sec_base+=hdrom_length_of_sections[sec];
51 sec++;
52 }
53}
54
d0581f88 55command_ret_t do_loadf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
534e1dfc
L
56{
57 (void) cmdtp; (void) flag; (void) argc; (void) argv;
58
6035a17b 59 if (z80_bus_state() & ZST_RUNNING) {
612a6965 60 my_puts_P(PSTR("Can't load while CPU is running!\n"));
6035a17b 61 return CMD_RET_FAILURE;
534e1dfc 62 }
612a6965
L
63 if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
64 my_puts_P(PSTR("Bus timeout\n"));
65 return CMD_RET_FAILURE;
66 }
534e1dfc 67 z80_load_mem();
612a6965 68 z80_bus_cmd(Release);
6035a17b 69
d0581f88 70 return CMD_RET_SUCCESS;
534e1dfc
L
71}
72
73
d0581f88 74command_ret_t do_busreq_pulse(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
534e1dfc 75{
f338df2a 76 uint16_t count=1;
534e1dfc 77
f338df2a 78 (void) cmdtp; (void) flag;
534e1dfc 79
6035a17b 80 if (!(z80_bus_state() & ZST_RUNNING)) {
f338df2a 81 printf_P(PSTR("## CPU is not running!\n"));
d0581f88 82 return CMD_RET_FAILURE;
534e1dfc
L
83 }
84
f338df2a
L
85 if (argc > 1)
86 count = (uint16_t) strtoul(argv[2], NULL, 16);
87
62f624d3 88 z80_bus_cmd(Request);
f338df2a 89 while (count--)
62f624d3 90 z80_bus_cmd(M_Cycle);
534e1dfc 91
d0581f88 92 return CMD_RET_SUCCESS;
f338df2a
L
93}
94
95
d0581f88 96command_ret_t do_go(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
f338df2a
L
97{
98 uint32_t addr;
99
100 (void) cmdtp; (void) flag;
6035a17b 101
f338df2a
L
102 if (argc < 2)
103 return CMD_RET_USAGE;
104 addr = strtoul(argv[1], NULL, 16);
105 if (addr >= (1UL<<16)) {
534e1dfc
L
106 printf_P(PSTR("## Startaddress 0x%05lx too high.\n"
107 " (Out of logical address space (0x00000-0x0ffff))\n"),
108 addr);
d0581f88 109 return CMD_RET_FAILURE;
6035a17b 110 }
f338df2a 111
6035a17b 112 if (z80_bus_state() & ZST_RUNNING) {
f338df2a 113 printf_P(PSTR("## CPU allready running!\n"));
d0581f88 114 return CMD_RET_FAILURE;
534e1dfc
L
115 }
116
f338df2a
L
117 printf_P(PSTR("## Starting application at 0x%04lx ...\n"), addr);
118
119 if (addr != 0) {
120 uint8_t tmp[3];
121 uint_fast8_t i;
6035a17b 122
62f624d3 123 z80_bus_cmd(Request);
f338df2a
L
124 for (i = 0; i < 3; i++)
125 tmp[i] = z80_read(i);
126 z80_write(0, 0xc3);
127 z80_write(1, addr);
128 z80_write(2, (addr >> 8));
129
62f624d3
L
130 z80_bus_cmd(Run);
131 z80_bus_cmd(M_Cycle);
132 z80_bus_cmd(M_Cycle);
f338df2a
L
133 for (i = 0; i < 3; i++)
134 z80_write(i, tmp[i]);
135 } else
62f624d3 136 z80_bus_cmd(Run);
6035a17b 137
62f624d3 138 z80_bus_cmd(Release);
f338df2a 139
d0581f88 140 return CMD_RET_SUCCESS;
534e1dfc
L
141}
142
8a7decea
L
143static
144void reset_cpu(bus_cmd_t mode)
145{
146 restart_z180_serv();
147 z80_bus_cmd(mode);
148}
149
150
d0581f88 151command_ret_t do_reset(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
534e1dfc
L
152{
153 (void) cmdtp; (void) flag; (void) argc; (void) argv;
154
612a6965 155 printf_P(PSTR("CPU now in reset state.\n"));
534e1dfc 156
8a7decea 157 reset_cpu(Reset);
d0581f88 158 return CMD_RET_SUCCESS;
534e1dfc
L
159}
160
d0581f88 161command_ret_t do_restart(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
534e1dfc
L
162{
163 (void) cmdtp; (void) flag; (void) argc; (void) argv;
164
8a7decea 165 reset_cpu(Restart);
534e1dfc 166
d0581f88 167 return CMD_RET_SUCCESS;
534e1dfc
L
168}
169
8a7decea
L
170static
171void print_con_usage(char esc)
172{ printf_P(PSTR("\n"
173 "------------------------------------------------\n"
174 " ?,H - This Help\n"
8a7decea 175 " Q,X - Return to command line\n"
b08e079d
L
176 " R - Reset (Restart) CPU\n"
177 " : - Execute monitor command\n"
612a6965
L
178 " \\ - code input:\n"
179 " \\nnn 3 decimal digits character code\n"
180 " \\Xhh 2 hexadecimal digits character code\n"
181 " ^%c - (Escape char) Type again to send itself\n"
8a7decea
L
182 "key>"
183 ), esc + 0x40);
184}
89adce76
L
185
186command_ret_t do_console(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
187{
188 int ch;
8a7decea
L
189 uint8_t pending;
190// uint8_t help_prompt = 0;
191 uint8_t code = 0;
192 uint8_t state = 0;
612a6965 193 char esc_char = (char) getenv_ulong(PSTR(ENV_ESC_CHAR), 16, CONFIG_ESC_CHAR);
ea6971b8 194
89adce76
L
195 (void) cmdtp; (void) flag; (void) argc; (void) argv;
196
b08e079d
L
197 printf_P(PSTR("Connecting to CPU. Escape character is '^%c'.\n"),
198 esc_char + 0x40);
89adce76
L
199
200 while (1) {
201
8a7decea 202 ATOMIC_BLOCK(ATOMIC_FORCEON) {
89adce76
L
203 pending = (Stat & S_CON_PENDING) != 0;
204 Stat &= ~S_CON_PENDING;
205 }
3ad4143b
L
206 if (pending) {
207 uint8_t count = 100;
208 while ((ch = z80_memfifo_getc(fifo_conout)) >= 0 && --count)
89adce76 209 putchar(ch);
3ad4143b 210 }
89adce76
L
211
212 if ((ch = my_getchar(0)) >= 0) {
213 switch (state) {
214 case 0:
612a6965 215 if (ch == esc_char) {
89adce76
L
216 state = 1;
217 /* TODO: Timer starten */
218 } else {
219 z80_memfifo_putc(fifo_conin, ch);
ea6971b8 220 }
89adce76 221 break;
8a7decea
L
222 case 2:
223 printf_P(PSTR("\n"
224 "------------------------------------------------\n"));
89adce76 225 case 1:
8a7decea
L
226 state = 0;
227 switch (toupper(ch)) {
89adce76 228
8a7decea
L
229 case '?':
230 case 'H':
612a6965 231 print_con_usage(esc_char);
8a7decea 232 state = 2;
89adce76
L
233 break;
234
8a7decea
L
235 case 'R':
236 reset_cpu(Restart);
89adce76
L
237 break;
238
8a7decea 239 case 'X':
89adce76 240 case 'Q':
889202c4 241 printf_P(PSTR("\n"));
89adce76
L
242 goto quit;
243 break;
244
b08e079d
L
245 case ':':
246 putchar('\n');
247 int cmdlen = cli_readline(PSTR(": "));
248 if (cmdlen > 0)
249 run_command(console_buffer, 0);
250 break;
251
8a7decea
L
252 case '\\':
253 code = 0;
254 state = 3;
255 break;
256
8a7decea 257 default:
612a6965
L
258 if (ch == esc_char)
259 z80_memfifo_putc(fifo_conin, ch);
8a7decea
L
260 break;
261 }
262 break;
263 case 3:
264 if (toupper(ch) == 'X') {
265 state = 6;
266 break;
267 }
268 /* fall thru */
269 case 4:
270 case 5:
271 if (isdigit(ch)) {
272 code = code * 10 + ch - '0';
273 state++;
b08e079d
L
274 } else {
275 if (state > 3)
276 z80_memfifo_putc(fifo_conin, code);
277 z80_memfifo_putc(fifo_conin, ch);
278 state = 0;
8a7decea
L
279 }
280 if (state > 5) {
281 z80_memfifo_putc(fifo_conin, code);
282 state = 0;
283 }
284 break;
285 case 6:
286 case 7:
287 if (isxdigit(ch)) {
288 ch = toupper(ch);
289 if (ch >= 'A')
290 ch -= 'A' - 10;
291 code = code * 16 + ch - '0';
292 state++;
b08e079d
L
293 }else {
294 if (state > 6)
295 z80_memfifo_putc(fifo_conin, code);
296 z80_memfifo_putc(fifo_conin, ch);
297 state = 0;
8a7decea
L
298 }
299 if (state > 7) {
300 z80_memfifo_putc(fifo_conin, code);
301 state = 0;
89adce76 302 }
89adce76
L
303 break;
304 }
305 }
89adce76
L
306 }
307quit:
89adce76
L
308 return CMD_RET_SUCCESS;
309}