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