]>
Commit | Line | Data |
---|---|---|
e64eba00 L |
1 | /* |
2 | */ | |
3 | ||
e64eba00 | 4 | #include <stdio.h> |
e64eba00 L |
5 | |
6 | #include <libopencmsis/core_cm3.h> | |
7 | #include <libopencm3/cm3/nvic.h> | |
8 | #include <libopencm3/cm3/systick.h> | |
9 | #include <libopencm3/stm32/rtc.h> | |
e64eba00 L |
10 | #include <libopencm3/stm32/rcc.h> |
11 | #include <libopencm3/stm32/gpio.h> | |
12 | #include <libopencm3/stm32/timer.h> | |
13 | ||
14 | #define ODR 0x0c | |
15 | #define IDR 0x08 | |
16 | ||
17 | ||
6b81b39f L |
18 | #include "debug.h" |
19 | #include "serial.h" | |
e64eba00 | 20 | #include "z80-if.h" |
d9c2b1b6 | 21 | #include "../Z180/hdrom.h" |
e64eba00 | 22 | |
e64eba00 | 23 | |
6b81b39f | 24 | #define ESCCHAR ('^'-0x40) |
e64eba00 L |
25 | |
26 | #define S_10MS_TO (1<<0) | |
27 | ||
28 | /* | |
29 | * LED Connections | |
30 | */ | |
31 | ||
32 | #define LED_PORT GPIOC | |
33 | #define LED_BLUE_PIN GPIO8 | |
34 | #define BLUE 8 | |
35 | #define LED_GREEN_PIN GPIO9 | |
36 | #define GREEN 9 | |
37 | ||
38 | ||
39 | #define LED_BLUE_ON() BBIO_PERIPH(LED_PORT+ODR, BLUE) = 1 | |
40 | #define LED_BLUE_OFF() BBIO_PERIPH(LED_PORT+ODR, BLUE) = 0 | |
41 | #define LED_BLUE_TOGGLE() BBIO_PERIPH(LED_PORT+ODR, BLUE) = !BBIO_PERIPH(LED_PORT+ODR, BLUE) | |
42 | ||
43 | #define LED_GREEN_ON() BBIO_PERIPH(LED_PORT+ODR, GREEN) = 1 | |
44 | #define LED_GREEN_OFF() BBIO_PERIPH(LED_PORT+ODR, GREEN) = 0 | |
45 | #define LED_GREEN_TOGGLE() BBIO_PERIPH(LED_PORT+ODR, GREEN) = !BBIO_PERIPH(LED_PORT+ODR, GREEN) | |
46 | ||
47 | ||
48 | /* | |
49 | * Button connections | |
50 | */ | |
51 | ||
52 | //BBIO_PERIPH(GPIOA+IDR, 0); | |
53 | ||
54 | #define KEY_PORT GPIOA_IDR | |
55 | #define KEY0 GPIO0 | |
56 | //#define KEY1 GPIO1 | |
57 | //#define KEY2 GPIO2 | |
58 | ||
59 | #define REPEAT_MASK KEY0 // repeat: key0 | |
60 | #define REPEAT_START 100 // after 1s | |
61 | #define REPEAT_NEXT 20 // every 200ms | |
62 | ||
63 | ||
64 | typedef enum { | |
65 | NOTHING, PULSE, BLINK1, BLINK2 | |
66 | } LED_MODE; | |
67 | ||
68 | typedef struct { | |
69 | uint8_t mode; | |
70 | uint8_t ontime, offtime; | |
71 | } led_stat_t; | |
72 | ||
73 | volatile uint8_t led_timer[2]; | |
74 | led_stat_t led_stat[2]; | |
75 | ||
76 | volatile int timeout_1s; | |
77 | volatile uint32_t Stat; | |
78 | ||
79 | ||
80 | /*--------------------------------------------------------------------------*/ | |
81 | ||
82 | ||
83 | static void clock_setup(void) | |
84 | { | |
0d318092 L |
85 | //rcc_clock_setup_in_hse_8mhz_out_24mhz(); |
86 | rcc_clock_setup_in_hsi_out_24mhz(); | |
e64eba00 L |
87 | |
88 | /* Enable clocks for: | |
89 | GPIO port A (for GPIO_USART1_TX and Button) | |
90 | GPIO port C (LEDs) | |
91 | USART1 | |
92 | TIM16 (RST-Pin) | |
0d318092 | 93 | TIM1 (IOCS1) |
e64eba00 L |
94 | */ |
95 | rcc_peripheral_enable_clock(&RCC_APB2ENR, | |
96 | RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN | |
97 | | RCC_APB2ENR_IOPCEN | RCC_APB2ENR_IOPDEN | |
98 | | RCC_APB2ENR_USART1EN | RCC_APB2ENR_AFIOEN | |
99 | | RCC_APB2ENR_TIM1EN | RCC_APB2ENR_TIM16EN); | |
100 | /* Enable clocks for: | |
101 | TIM3 | |
102 | */ | |
103 | rcc_peripheral_enable_clock(&RCC_APB1ENR, | |
104 | RCC_APB1ENR_TIM3EN); | |
105 | ||
106 | /* Enable clocks for: | |
0d318092 | 107 | DMA1 |
e64eba00 L |
108 | */ |
109 | rcc_peripheral_enable_clock(&RCC_AHBENR, | |
110 | RCC_AHBENR_DMA1EN); | |
111 | } | |
112 | ||
113 | static void systick_setup(void) | |
114 | { | |
115 | /* SysTick interrupt every N clock pulses: set reload to N-1 */ | |
116 | STK_RVR = 24000000/1000 - 1; | |
117 | ||
118 | /* Set source to core clock, enable int and start counting. */ | |
119 | STK_CSR = STK_CSR_CLKSOURCE_AHB | STK_CSR_TICKINT | STK_CSR_ENABLE; | |
120 | } | |
121 | ||
122 | #if 0 | |
123 | static void nvic_setup(void) | |
124 | { | |
125 | // nvic_enable_irq(NVIC_RTC_IRQ); | |
126 | // nvic_set_priority(NVIC_RTC_IRQ, 1); | |
127 | } | |
128 | #endif | |
129 | ||
130 | static void tim3_setup(void) | |
131 | { | |
132 | TIM3_CR1 = TIM_CR1_CMS_EDGE | TIM_CR1_DIR_UP; | |
133 | ||
134 | TIM3_CCMR2 = 0 | |
135 | | TIM_CCMR2_OC4M_FORCE_LOW | |
136 | /* | TIM_CCMR2_OC4M_FORCE_HIGH */ | |
137 | /* | TIM_CCMR2_OC4M_PWM2 */ | |
138 | ||
139 | /* | TIM_CCMR2_OC4PE */ | |
140 | /* | TIM_CCMR2_OC4FE */ | |
141 | | TIM_CCMR2_CC4S_OUT; | |
142 | ||
143 | TIM3_CCER = TIM_CCER_CC4E | |
144 | | TIM_CCER_CC4P; | |
145 | ||
146 | TIM3_ARR = 48; /* default */ | |
147 | TIM3_CCR4 = 1; /* */ | |
148 | } | |
149 | ||
150 | static void gpio_setup(void) | |
151 | { | |
152 | ||
153 | /* Disable JTAG-DP, but leave SW-DP Enabled. (free PA15, PB3, PB4) | |
154 | Remap SPI1 to PB3, PB4, PB5 and PA15. | |
155 | Remap TIM3 (CH1/PC6, CH2/PC7, CH3/PC8, CH4/PC9) | |
0d318092 | 156 | Port D0/Port D1 mapping on OSC_IN/OSC_OUT |
e64eba00 L |
157 | */ |
158 | gpio_primary_remap(AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON, | |
0d318092 L |
159 | AFIO_MAPR_SPI1_REMAP |
160 | | AFIO_MAPR_TIM3_REMAP_FULL_REMAP | |
161 | | AFIO_MAPR_PD01_REMAP); | |
e64eba00 L |
162 | |
163 | /* LEDs and User Button. */ | |
164 | gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ, | |
165 | GPIO_CNF_OUTPUT_PUSHPULL, LED_BLUE_PIN); | |
166 | gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_10_MHZ, | |
167 | GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, LED_GREEN_PIN); | |
168 | gpio_set_mode(GPIOA, GPIO_MODE_INPUT, | |
169 | GPIO_CNF_INPUT_FLOAT, GPIO0); | |
170 | } | |
171 | ||
172 | ||
e64eba00 L |
173 | /*--------------------------------------------------------------------------*/ |
174 | ||
175 | void delay_systicks(int ticks) | |
176 | { | |
177 | int start, stop, now; | |
178 | ||
179 | start = STK_CVR; | |
180 | stop = start - ticks; | |
181 | if (stop < 0) { | |
182 | stop += STK_RVR; | |
183 | do { | |
184 | now = STK_CVR; | |
185 | } while ((now > stop) || (now <= start)); | |
186 | } else { | |
187 | do { | |
188 | now = STK_CVR; | |
189 | } while ((now > stop) && (now <= start)); | |
190 | } | |
191 | } | |
192 | ||
193 | ||
194 | /*--------------------------------------------------------------------------*/ | |
195 | ||
196 | static void led_toggle(uint8_t lednr) { | |
197 | if (lednr == 0) | |
198 | LED_BLUE_TOGGLE(); | |
199 | else if (lednr == 1) | |
200 | LED_GREEN_TOGGLE(); | |
201 | } | |
202 | ||
203 | static void led_on(uint8_t lednr) { | |
204 | if (lednr == 0) | |
205 | LED_BLUE_ON(); | |
206 | else if (lednr == 1) | |
207 | LED_GREEN_ON(); | |
208 | } | |
209 | ||
210 | static void led_off(uint8_t lednr) { | |
211 | if (lednr == 0) | |
212 | LED_BLUE_OFF(); | |
213 | else if (lednr == 1) | |
214 | LED_GREEN_OFF(); | |
215 | } | |
216 | ||
217 | static uint8_t led_is_on(uint8_t lednr) { | |
218 | if (lednr == 0) | |
219 | return BBIO_PERIPH(LED_PORT+ODR, BLUE); | |
220 | else if (lednr == 1) | |
221 | return BBIO_PERIPH(LED_PORT+ODR, GREEN); | |
222 | else | |
223 | return 0; | |
224 | } | |
225 | ||
226 | static void ledset(uint8_t lednr, uint8_t what, uint8_t len) { | |
227 | ||
228 | led_stat[lednr].mode = what; | |
229 | switch (what) { | |
230 | case PULSE: | |
231 | led_stat[lednr].ontime = len; | |
232 | led_stat[lednr].offtime = 0; | |
233 | led_timer[lednr] = len; | |
234 | led_on(lednr); | |
235 | break; | |
236 | case BLINK1: | |
237 | case BLINK2: | |
238 | if (what == BLINK1) | |
239 | led_stat[lednr].offtime = 100 - len; | |
240 | else | |
241 | led_stat[lednr].offtime = 200 - len; | |
242 | led_stat[lednr].ontime = len; | |
243 | led_timer[lednr] = len; | |
244 | led_on(lednr); | |
245 | break; | |
246 | default: | |
247 | break; | |
248 | } | |
249 | } | |
250 | ||
251 | /*--------------------------------------------------------------------------*/ | |
252 | ||
253 | static volatile uint16_t key_state, | |
254 | key_press, // key press detect | |
255 | key_rpt; // key long press and repeat | |
256 | ||
257 | ||
258 | static uint16_t get_key_press(uint16_t key_mask) { | |
259 | __disable_irq(); | |
260 | // read and clear atomic ! | |
261 | key_mask &= key_press; // read key(s) | |
262 | key_press ^= key_mask; // clear key(s) | |
263 | __enable_irq(); | |
264 | return key_mask; | |
265 | } | |
266 | ||
6b81b39f | 267 | /* |
e64eba00 L |
268 | static uint16_t get_key_rpt(uint16_t key_mask) { |
269 | __disable_irq(); | |
270 | // read and clear atomic ! | |
271 | key_mask &= key_rpt; // read key(s) | |
272 | key_rpt ^= key_mask; // clear key(s) | |
273 | __enable_irq(); | |
274 | return key_mask; | |
275 | } | |
6b81b39f | 276 | */ |
e64eba00 L |
277 | |
278 | static uint16_t get_key_short(uint16_t key_mask) { | |
279 | __disable_irq(); | |
280 | // read key state and key press atomic ! | |
281 | return get_key_press(key_state & key_mask); | |
282 | } | |
283 | ||
284 | /* | |
285 | static uint16_t get_key_long(uint16_t key_mask) { | |
286 | return get_key_press(get_key_rpt(key_mask)); | |
287 | } | |
288 | */ | |
289 | ||
290 | static void key_timerproc() { | |
291 | static uint16_t key_in_last, rpt; | |
292 | uint16_t key_in, c; | |
293 | ||
294 | key_in = KEY_PORT; | |
295 | ||
296 | c = key_in_last & key_in & ~key_state; | |
297 | ||
298 | // key_state = key_state & key_in_last | (key_state | key_in_last) & key_in; | |
299 | // key_state = key_state & key_in | (key_state | key_in) & key_in_last; | |
300 | ||
301 | key_state = c | ((key_in_last | key_in) & key_state); | |
302 | ||
303 | // key_state = (key_state&key_in_last) | (key_state&key_in) | (key_in_last&key_in); | |
304 | ||
305 | key_press |= c; | |
306 | ||
307 | key_in_last = key_in; | |
308 | ||
309 | ||
310 | if ((key_state & REPEAT_MASK) == 0) // check repeat function | |
311 | rpt = REPEAT_START; | |
312 | if (--rpt == 0) { | |
313 | rpt = REPEAT_NEXT; // repeat delay | |
314 | key_rpt |= key_state & REPEAT_MASK; | |
315 | } | |
316 | ||
e64eba00 L |
317 | } |
318 | ||
319 | /*--------------------------------------------------------------------------*/ | |
320 | ||
321 | void sys_tick_handler(void) | |
322 | { | |
323 | static int tick_10ms = 0; | |
324 | static int count_ms = 0; | |
325 | ||
326 | int i; | |
327 | ||
328 | ++tick_10ms; | |
329 | if (tick_10ms == 10) | |
330 | { | |
331 | Stat |= S_10MS_TO; | |
332 | ||
333 | tick_10ms = 0; | |
334 | ||
335 | i = led_timer[0]; | |
336 | if (i) | |
337 | led_timer[0] = i - 1; | |
338 | i = led_timer[1]; | |
339 | if (i) | |
340 | led_timer[1] = i - 1; | |
341 | ||
342 | key_timerproc(); | |
343 | ||
344 | /* Drive timer procedure of low level disk I/O module */ | |
345 | //disk_timerproc(); | |
346 | } | |
347 | ||
348 | count_ms++; | |
349 | if (count_ms == 1000) { | |
350 | count_ms = 0; | |
351 | ||
0d318092 L |
352 | i = timeout_1s; |
353 | if (i) | |
354 | timeout_1s = i - 1; | |
e64eba00 L |
355 | } |
356 | } | |
357 | ||
358 | void rtc_isr(void) | |
359 | { | |
360 | /* The interrupt flag isn't cleared by hardware, we have to do it. */ | |
361 | rtc_clear_flag(RTC_SEC); | |
362 | ||
363 | } | |
364 | ||
365 | /*--------------------------------------------------------------------------*/ | |
366 | ||
367 | void tim3_set(int mode) | |
368 | { | |
369 | uint16_t cc_mode; | |
370 | ||
371 | cc_mode = TIM_CCMR2_CC4S_OUT; | |
372 | ||
0d318092 | 373 | TIM3_CR1 = TIM_CR1_CMS_EDGE | TIM_CR1_DIR_UP /*| TIM_CR1_OPM */ ; |
e64eba00 L |
374 | |
375 | if (mode < 0) | |
376 | cc_mode |= TIM_CCMR2_OC4M_FORCE_LOW; | |
377 | else if (mode == 0) | |
378 | cc_mode |= TIM_CCMR2_OC4M_FORCE_HIGH; | |
379 | else { | |
380 | TIM3_ARR = mode; | |
0d318092 | 381 | TIM3_CCR4 = mode/2; |
e64eba00 L |
382 | cc_mode |= TIM_CCMR2_OC4M_PWM2; |
383 | } | |
384 | ||
385 | TIM3_CCMR2 = cc_mode; | |
386 | ||
387 | if (mode > 0) | |
388 | TIM3_CR1 |= TIM_CR1_CEN; | |
389 | } | |
390 | ||
391 | /*--------------------------------------------------------------------------*/ | |
392 | ||
393 | static uint32_t z80_sram_cmp(uint32_t addr, int length, uint8_t wval, int inc) | |
394 | { | |
395 | uint8_t rval; | |
396 | int errors = 0; | |
397 | ||
6b81b39f | 398 | DBG_P(1, "SRAM: Check %#.5x byte... ", length); |
e64eba00 L |
399 | while (length--) { |
400 | if ((rval = z80_read(addr)) != wval) { | |
401 | if (errors == 0) { | |
402 | printf("\nSRAM: Address W R\n" \ | |
403 | " -------------\n"); | |
404 | // 12345 00 11 | |
405 | } | |
406 | printf(" %.5lx %.2x %.2x\n", addr, wval, rval); | |
407 | ||
408 | if (++errors > 16 ) | |
409 | break; | |
410 | } | |
411 | addr++; | |
412 | wval += inc; | |
413 | } | |
6b81b39f | 414 | DBG_P(1, "Done.\n"); |
e64eba00 L |
415 | |
416 | return addr; | |
417 | } | |
418 | ||
419 | #if 0 | |
420 | static void z80_sram_fill(uint32_t addr, int length, uint8_t startval, int inc) | |
421 | { | |
422 | printf("SRAM: Write %#.5x byte... ", length); //fflush(stdout); | |
423 | while (length--) { | |
424 | z80_write(addr, startval); | |
425 | ++addr; | |
426 | startval += inc; | |
427 | } | |
428 | printf("Done.\n"); | |
429 | } | |
430 | ||
431 | ||
432 | void z80_sram_fill_string(uint32_t addr, int length, const char *text) | |
433 | { | |
434 | char c; | |
435 | const char *p = text; | |
436 | ||
437 | while (length--) { | |
438 | z80_write(addr++, c = *p++); | |
439 | if (c == 0) | |
440 | p = text; | |
441 | } | |
442 | } | |
443 | ||
444 | ||
445 | uint32_t z80_sram_cmp_string(uint32_t addr, int length, const char *text) | |
446 | { | |
447 | char c; | |
448 | const char *p = text; | |
449 | ||
450 | while (length--) { | |
451 | c = *p++; | |
452 | if (z80_read(addr) != c) | |
453 | break; | |
454 | ++addr; | |
455 | if (c == 0) | |
456 | p = text; | |
457 | } | |
458 | return addr; | |
459 | } | |
460 | ||
461 | const char * const qbfox = "Zhe quick brown fox jumps over the lazy dog!"; | |
462 | const char * const qbcat = "Zhe quick brown fox jumps over the lazy cat!"; | |
463 | ||
464 | #endif | |
465 | ||
466 | uint8_t z80_get_byte(uint32_t adr) | |
467 | { | |
468 | uint8_t data; | |
469 | ||
0d318092 | 470 | z80_request_bus(); |
e64eba00 L |
471 | data = z80_read(adr), |
472 | z80_release_bus(); | |
473 | ||
474 | return data; | |
475 | } | |
476 | ||
477 | ||
478 | /*--------------------------------------------------------------------------*/ | |
479 | ||
480 | static void do_10ms(void) | |
481 | { | |
0d318092 | 482 | for (uint_fast8_t i = 0; i < 2; i++) { |
e64eba00 L |
483 | switch (led_stat[i].mode) { |
484 | case PULSE: | |
485 | if (led_timer[i] == 0) { | |
486 | led_off(i); | |
487 | led_stat[i].mode = NOTHING; | |
488 | } | |
489 | break; | |
490 | case BLINK1: | |
491 | case BLINK2: | |
492 | if (led_timer[i] == 0) { | |
493 | if (led_is_on(i)) | |
494 | led_timer[i] = led_stat[i].offtime; | |
495 | else | |
496 | led_timer[i] = led_stat[i].ontime; | |
497 | led_toggle(i); | |
498 | } | |
499 | break; | |
500 | default: | |
501 | break; | |
502 | } | |
503 | } | |
504 | } | |
505 | ||
d9c2b1b6 L |
506 | struct msg_item { |
507 | uint8_t fct; | |
508 | uint8_t sub_min, sub_max; | |
509 | void (*func)(uint8_t, int, uint8_t *); | |
510 | }; | |
511 | ||
512 | uint32_t msg_to_addr(uint8_t *msg) | |
6b81b39f | 513 | { |
d9c2b1b6 L |
514 | uint32_t addr = msg[0] + (msg[1] << 8) + (msg[2] << 16); |
515 | ||
516 | return addr; | |
517 | ||
6b81b39f L |
518 | } |
519 | ||
d9c2b1b6 | 520 | void do_msg_ini_msgfifo(uint8_t subf, int len, uint8_t * msg) |
6b81b39f | 521 | { |
d9c2b1b6 L |
522 | (void)subf; (void)len; |
523 | ||
524 | z80_init_msg_fifo(msg_to_addr(msg)); | |
525 | } | |
526 | ||
527 | ||
528 | void do_msg_ini_memfifo(uint8_t subf, int len, uint8_t * msg) | |
529 | { | |
530 | (void)len; | |
531 | ||
532 | z80_memfifo_init(subf - 1, msg_to_addr(msg)); | |
6b81b39f L |
533 | } |
534 | ||
d9c2b1b6 L |
535 | |
536 | void do_msg_char_out(uint8_t subf, int len, uint8_t * msg) | |
537 | { | |
538 | (void)subf; | |
539 | ||
540 | while (len--) | |
541 | putchar(*msg++); | |
542 | } | |
543 | ||
544 | ||
545 | const struct msg_item z80_messages[] = | |
546 | { | |
547 | { 0, | |
548 | 0, 0, | |
549 | &do_msg_ini_msgfifo}, | |
550 | { 0, | |
551 | 1, 2, | |
552 | &do_msg_ini_memfifo}, | |
553 | { 1, | |
554 | 1, 1, | |
555 | &do_msg_char_out}, | |
556 | { 0xff, /* end mark */ | |
557 | 0, 0, | |
558 | 0}, | |
559 | ||
560 | }; | |
561 | ||
562 | ||
563 | ||
564 | ||
6b81b39f L |
565 | void do_message(int len, uint8_t *msg) |
566 | { | |
d9c2b1b6 L |
567 | uint8_t fct, sub_fct; |
568 | int i = 0; | |
6b81b39f | 569 | |
d9c2b1b6 L |
570 | if (len >= 2) { |
571 | fct = *msg++; | |
572 | sub_fct = *msg++; | |
573 | len -= 2; | |
574 | ||
575 | while (fct != z80_messages[i].fct) | |
576 | ++i; | |
577 | ||
578 | if (z80_messages[i].fct == 0xff) { | |
579 | DBG_P(1, "do_message: Unknown function: %i, %i\n", | |
580 | fct, sub_fct); | |
581 | return; /* TODO: unknown message # */ | |
582 | } | |
583 | ||
584 | while (fct == z80_messages[i].fct) { | |
585 | if (sub_fct >= z80_messages[i].sub_min && sub_fct <= z80_messages[i].sub_max ) | |
586 | break; | |
587 | ++i; | |
6b81b39f | 588 | } |
d9c2b1b6 L |
589 | |
590 | if (z80_messages[i].fct != fct) { | |
591 | DBG_P(1, "do_message: Unknown sub function: %i, %i\n", | |
592 | fct, sub_fct); | |
593 | return; /* TODO: unknown message sub# */ | |
594 | } | |
595 | ||
596 | (z80_messages[i].func)(sub_fct, len, msg); | |
597 | ||
598 | ||
6b81b39f | 599 | } else { |
d9c2b1b6 L |
600 | /* TODO: error */ |
601 | DBG_P(1, "do_message: to few arguments (%i); this shouldn't happen!\n", len); | |
6b81b39f L |
602 | } |
603 | } | |
604 | ||
605 | ||
6b81b39f | 606 | |
d9c2b1b6 L |
607 | #define CTRBUF_LEN 256 |
608 | ||
609 | void check_msg_fifo(void) | |
0d318092 | 610 | { |
6b81b39f L |
611 | int ch; |
612 | static int state; | |
613 | static int msglen,idx; | |
614 | static uint8_t buffer[CTRBUF_LEN]; | |
615 | ||
616 | while (state != 3 && (ch = z80_msg_fifo_getc()) >= 0) { | |
617 | switch (state) { | |
618 | case 0: /* wait for start of message */ | |
619 | if (ch == 0x81) { | |
620 | msglen = 0; | |
621 | idx = 0; | |
622 | state = 1; | |
623 | } | |
624 | break; | |
625 | case 1: /* get msg len */ | |
626 | if (ch > 0 && ch <= CTRBUF_LEN) { | |
627 | msglen = ch; | |
628 | state = 2; | |
629 | } else | |
630 | state = 0; | |
631 | break; | |
632 | case 2: /* get message */ | |
633 | buffer[idx++] = ch; | |
634 | if (idx == msglen) | |
635 | state = 3; | |
636 | break; | |
0d318092 L |
637 | } |
638 | } | |
6b81b39f L |
639 | |
640 | if (state == 3) { | |
641 | do_message(msglen, buffer); | |
642 | state = 0; | |
643 | } | |
0d318092 L |
644 | } |
645 | ||
6b81b39f L |
646 | |
647 | void z80_load_mem(void) | |
648 | { | |
649 | ||
650 | DBG_P(1, "Loading z80 memory... \n"); | |
651 | ||
652 | unsigned sec = 0; | |
653 | uint32_t sec_base = hdrom_start; | |
654 | ||
655 | while (sec < hdrom_sections) { | |
656 | DBG_P(2, " From: 0x%.5lX to: 0x%.5lX (%5li bytes)\n", | |
657 | hdrom_address[sec], | |
658 | hdrom_address[sec]+hdrom_length_of_sections[sec] - 1, | |
659 | hdrom_length_of_sections[sec]); | |
660 | ||
661 | z80_write_block((unsigned char *) &hdrom[sec_base], /* src */ | |
662 | hdrom_address[sec], /* dest */ | |
663 | hdrom_length_of_sections[sec]); /* len */ | |
664 | sec_base+=hdrom_length_of_sections[sec]; | |
665 | sec++; | |
666 | } | |
667 | } | |
e64eba00 L |
668 | /*--------------------------------------------------------------------------*/ |
669 | ||
670 | int main(void) | |
671 | { | |
672 | //uint32_t led_state = LED_BLUE_PIN; | |
673 | //uint32_t rc; | |
674 | //uint8_t startval = 0; | |
675 | //int count; | |
6b81b39f L |
676 | int state = 0; |
677 | int ch; | |
e64eba00 L |
678 | |
679 | clock_setup(); | |
680 | gpio_setup(); | |
681 | tim3_setup(); | |
682 | setvbuf(stdout, NULL, _IONBF, 0); | |
6b81b39f | 683 | serial_setup(); |
0d318092 | 684 | printf("\n(STM32F100+HD64180)_stamp Tester\n"); |
e64eba00 | 685 | |
6b81b39f L |
686 | DBG_P(1, "z80_setup_bus... "); |
687 | z80_setup_msg_fifo(); | |
e64eba00 | 688 | z80_setup_bus(); |
6b81b39f | 689 | DBG_P(1, "done.\n"); |
e64eba00 L |
690 | |
691 | /* | |
692 | * If the RTC is pre-configured just allow access, don't reconfigure. | |
693 | * Otherwise enable it with the LSE as clock source and 0x7fff as | |
694 | * prescale value. | |
695 | */ | |
696 | rtc_auto_awake(LSE, 0x7fff); | |
697 | ||
698 | systick_setup(); | |
e64eba00 | 699 | |
6b81b39f | 700 | DBG_P(1, "Get bus... "); |
e64eba00 L |
701 | z80_busreq(LOW); |
702 | z80_reset(HIGH); | |
0d318092 | 703 | z80_request_bus(); |
6b81b39f | 704 | DBG_P(1, "got it!\n"); |
e64eba00 L |
705 | |
706 | z80_memset(0, 0x76, 0x80000); | |
707 | //z80_sram_fill(0, 512 * 1024, 0x76, 0); | |
708 | z80_sram_cmp(0, 512 * 1024, 0x76, 0); | |
709 | ||
6b81b39f | 710 | z80_load_mem(); |
e64eba00 | 711 | z80_reset(LOW); |
6b81b39f | 712 | DBG_P(1, "Bus released!\n"); |
e64eba00 L |
713 | z80_release_bus(); |
714 | z80_reset(HIGH); | |
6b81b39f | 715 | DBG_P(1, "Reset released!\n"); |
e64eba00 | 716 | |
e64eba00 L |
717 | |
718 | ledset(0, BLINK1, 50); | |
719 | ||
720 | while (1) { | |
6b81b39f | 721 | |
e64eba00 L |
722 | if (Stat & S_10MS_TO) { |
723 | Stat &= ~S_10MS_TO; | |
724 | do_10ms(); | |
725 | } | |
726 | ||
0d318092 | 727 | if (get_key_short(KEY0)) { |
e64eba00 | 728 | z80_reset_pulse(); |
0d318092 | 729 | } |
e64eba00 | 730 | |
6b81b39f L |
731 | if ((ch = serial_getc()) >= 0) { |
732 | switch (state) { | |
e64eba00 | 733 | case 0: |
6b81b39f L |
734 | if (ch == ESCCHAR) { |
735 | state = 1; | |
736 | /* TODO: Timer starten */ | |
737 | } else | |
738 | z80_memfifo_putc(fifo_out, ch); | |
e64eba00 | 739 | break; |
6b81b39f L |
740 | case 1: |
741 | switch (ch) { | |
e64eba00 | 742 | |
6b81b39f | 743 | case 'h': /* test: green led on */ |
e64eba00 L |
744 | tim3_set(-1); |
745 | break; | |
6b81b39f | 746 | case 'l': /* test: green led off */ |
e64eba00 L |
747 | tim3_set(0); |
748 | break; | |
6b81b39f L |
749 | case 'p': /* test: pulse on led pin */ |
750 | tim3_set(24000000 / 1000000 * 5); /* 5 us */ | |
751 | break; | |
752 | case 'r': | |
753 | z80_reset_pulse(); | |
e64eba00 | 754 | break; |
e64eba00 | 755 | |
6b81b39f L |
756 | case ESCCHAR: |
757 | default: | |
758 | z80_memfifo_putc(fifo_out, ch); | |
759 | } | |
760 | state = 0; | |
761 | break; | |
e64eba00 | 762 | } |
e64eba00 | 763 | } |
0d318092 | 764 | |
6b81b39f | 765 | check_msg_fifo(); |
e64eba00 L |
766 | } |
767 | ||
768 | return 0; | |
769 | } |