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