summaryrefslogtreecommitdiff
path: root/stm32/z180-stamp-stm32.c
blob: ce161d815f1769d8cb015f6f4dc98f82d5ab05a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
/*
 */

#include <errno.h>
#include <stdio.h>
#include <unistd.h>

#include <libopencmsis/core_cm3.h>
#include <libopencm3/cm3/nvic.h>
#include <libopencm3/cm3/systick.h>
#include <libopencm3/stm32/rtc.h>
#include <libopencm3/stm32/usart.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/timer.h>

#define ODR	0x0c
#define IDR	0x08



#include "z80-if.h"
#include "hdrom.h"

#define USART_CONSOLE USART1

int _write(int fd, char *ptr, int len)  __attribute__((used));

#define S_10MS_TO	(1<<0)

/*
 * LED Connections
 */

#define LED_PORT	GPIOC
#define LED_BLUE_PIN	GPIO8
#define BLUE		8
#define LED_GREEN_PIN	GPIO9
#define GREEN		9


#define LED_BLUE_ON()     BBIO_PERIPH(LED_PORT+ODR, BLUE) = 1
#define LED_BLUE_OFF()    BBIO_PERIPH(LED_PORT+ODR, BLUE) = 0
#define LED_BLUE_TOGGLE() BBIO_PERIPH(LED_PORT+ODR, BLUE) = !BBIO_PERIPH(LED_PORT+ODR, BLUE)

#define LED_GREEN_ON()     BBIO_PERIPH(LED_PORT+ODR, GREEN) = 1
#define LED_GREEN_OFF()    BBIO_PERIPH(LED_PORT+ODR, GREEN) = 0
#define LED_GREEN_TOGGLE() BBIO_PERIPH(LED_PORT+ODR, GREEN) = !BBIO_PERIPH(LED_PORT+ODR, GREEN)


/*
 * Button connections
 */

//BBIO_PERIPH(GPIOA+IDR, 0);

#define KEY_PORT	GPIOA_IDR
#define KEY0		GPIO0
//#define KEY1		GPIO1
//#define KEY2		GPIO2

#define REPEAT_MASK	KEY0		// repeat: key0
#define REPEAT_START	100		// after 1s
#define REPEAT_NEXT	20		// every 200ms


typedef enum {
	NOTHING, PULSE, BLINK1, BLINK2
} LED_MODE;

typedef struct {
	uint8_t mode;
	uint8_t ontime, offtime;
} led_stat_t;

volatile uint8_t led_timer[2];
led_stat_t led_stat[2];

volatile int timeout_1s;
volatile uint32_t Stat;


/*--------------------------------------------------------------------------*/


static void clock_setup(void)
{
	//rcc_clock_setup_in_hse_8mhz_out_24mhz();
	rcc_clock_setup_in_hsi_out_24mhz();

	/* Enable clocks for:
		GPIO port A (for GPIO_USART1_TX and Button)
		GPIO port C (LEDs) 
		USART1
		TIM16 (RST-Pin) 
		TIM1  (IOCS1)
	   TODO: USART1 --> USART_CONSOLE
	*/
	rcc_peripheral_enable_clock(&RCC_APB2ENR, 
			  RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPBEN 
			| RCC_APB2ENR_IOPCEN | RCC_APB2ENR_IOPDEN 
			| RCC_APB2ENR_USART1EN | RCC_APB2ENR_AFIOEN
			| RCC_APB2ENR_TIM1EN | RCC_APB2ENR_TIM16EN);
	/* Enable clocks for:
		TIM3
	*/
	rcc_peripheral_enable_clock(&RCC_APB1ENR, 
			RCC_APB1ENR_TIM3EN);

	/* Enable clocks for:
		DMA1
	*/
	rcc_peripheral_enable_clock(&RCC_AHBENR, 
			RCC_AHBENR_DMA1EN);
}

static void systick_setup(void)
{
	/* SysTick interrupt every N clock pulses: set reload to N-1 */
	STK_RVR = 24000000/1000 - 1;

	/* Set source to core clock, enable int and start counting. */
	STK_CSR = STK_CSR_CLKSOURCE_AHB | STK_CSR_TICKINT | STK_CSR_ENABLE;
}

#if 0
static void nvic_setup(void)
{
//	nvic_enable_irq(NVIC_RTC_IRQ);
//	nvic_set_priority(NVIC_RTC_IRQ, 1);
}
#endif

static void tim3_setup(void)
{
	TIM3_CR1 = TIM_CR1_CMS_EDGE | TIM_CR1_DIR_UP;
		
	TIM3_CCMR2 = 0
		| TIM_CCMR2_OC4M_FORCE_LOW	
	/*	| TIM_CCMR2_OC4M_FORCE_HIGH	*/
	/*	| TIM_CCMR2_OC4M_PWM2		*/
		
	/*	| TIM_CCMR2_OC4PE		*/
	/*	| TIM_CCMR2_OC4FE		*/
		| TIM_CCMR2_CC4S_OUT;
		
	TIM3_CCER = TIM_CCER_CC4E
		| TIM_CCER_CC4P;
	
	TIM3_ARR = 48;	/* default */
	TIM3_CCR4 = 1;	/*  */
}

static void gpio_setup(void)
{

	/* Disable JTAG-DP, but leave SW-DP Enabled. (free PA15, PB3, PB4)
	   Remap SPI1 to PB3, PB4, PB5 and PA15.
	   Remap TIM3 (CH1/PC6, CH2/PC7, CH3/PC8, CH4/PC9)
	   Port D0/Port D1 mapping on OSC_IN/OSC_OUT
	*/
	gpio_primary_remap(AFIO_MAPR_SWJ_CFG_JTAG_OFF_SW_ON, 
			AFIO_MAPR_SPI1_REMAP
			| AFIO_MAPR_TIM3_REMAP_FULL_REMAP
			| AFIO_MAPR_PD01_REMAP);

	/* LEDs and User Button. */
	gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_2_MHZ,
		      GPIO_CNF_OUTPUT_PUSHPULL, LED_BLUE_PIN);
	gpio_set_mode(LED_PORT, GPIO_MODE_OUTPUT_10_MHZ,
		      GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, LED_GREEN_PIN);
	gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
		      GPIO_CNF_INPUT_FLOAT, GPIO0);
}


static void usart_setup(void)
{
	/* Setup GPIO pin GPIO_USART1_TX/LED_GREEN_PIN on GPIO port A for transmit. */
	/* TODO: USART1 --> USART_CONSOLE */

	gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
		      GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);

	/* Setup UART parameters. */
//	usart_set_baudrate(USART_CONSOLE, 38400);
	usart_set_baudrate(USART_CONSOLE, 115200);
	usart_set_databits(USART_CONSOLE, 8);
	usart_set_stopbits(USART_CONSOLE, USART_STOPBITS_1);
	usart_set_mode(USART_CONSOLE, USART_MODE_TX_RX);
	usart_set_parity(USART_CONSOLE, USART_PARITY_NONE);
	usart_set_flow_control(USART_CONSOLE, USART_FLOWCONTROL_NONE);

	/* Finally enable the USART. */
	usart_enable(USART_CONSOLE);
}

/*--------------------------------------------------------------------------*/

/**
 * Use USART_CONSOLE as a console.
 * This is a syscall for newlib
 * @param fd
 * @param ptr
 * @param len
 * @return
 */
int _write(int fd, char *ptr, int len)
{
	int i;

	if (fd == STDOUT_FILENO || fd == STDERR_FILENO) {
		for (i = 0; i < len; i++) {
			if (ptr[i] == '\n') {
				usart_send_blocking(USART_CONSOLE, '\r');
			}
			usart_send_blocking(USART_CONSOLE, ptr[i]);
		}
		return i;
	}
	errno = EIO;
	return -1;
}


/*--------------------------------------------------------------------------*/

void delay_systicks(int ticks)
{
	int start, stop, now;
	
	start = STK_CVR;
	stop = start - ticks;
	if (stop < 0) {
		stop += STK_RVR;
		do {
			now = STK_CVR;
		} while ((now > stop) || (now <= start));
	} else {
		do {
			now = STK_CVR;
		} while ((now > stop) && (now <= start));
	}
}


/*--------------------------------------------------------------------------*/

static void led_toggle(uint8_t lednr) {
	if (lednr == 0)
		LED_BLUE_TOGGLE();
	else if (lednr == 1)
		LED_GREEN_TOGGLE();
}

static void led_on(uint8_t lednr) {
	if (lednr == 0)
		LED_BLUE_ON();
	else if (lednr == 1)
		LED_GREEN_ON();
}

static void led_off(uint8_t lednr) {
	if (lednr == 0)
		LED_BLUE_OFF();
	else if (lednr == 1)
		LED_GREEN_OFF();
}

static uint8_t led_is_on(uint8_t lednr) {
	if (lednr == 0)
		return BBIO_PERIPH(LED_PORT+ODR, BLUE);
	else if (lednr == 1)
		return BBIO_PERIPH(LED_PORT+ODR, GREEN);
	else
		return 0;
}

static void ledset(uint8_t lednr, uint8_t what, uint8_t len) {

	led_stat[lednr].mode = what;
	switch (what) {
	case PULSE:
		led_stat[lednr].ontime = len;
		led_stat[lednr].offtime = 0;
		led_timer[lednr] = len;
		led_on(lednr);
		break;
	case BLINK1:
	case BLINK2:
		if (what == BLINK1)
			led_stat[lednr].offtime = 100 - len;
		else
			led_stat[lednr].offtime = 200 - len;
		led_stat[lednr].ontime = len;
		led_timer[lednr] = len;
		led_on(lednr);
		break;
	default:
		break;
	}
}

/*--------------------------------------------------------------------------*/

static volatile uint16_t key_state,
		key_press, // key press detect
		key_rpt; // key long press and repeat


static uint16_t get_key_press(uint16_t key_mask) {
	__disable_irq();
	// read and clear atomic !
	key_mask &= key_press; // read key(s)
	key_press ^= key_mask; // clear key(s)
	__enable_irq();
	return key_mask;
}

static uint16_t get_key_rpt(uint16_t key_mask) {
	__disable_irq();
	// read and clear atomic !
	key_mask &= key_rpt; // read key(s)
	key_rpt ^= key_mask; // clear key(s)
	__enable_irq();
	return key_mask;
}

static uint16_t get_key_short(uint16_t key_mask) {
	__disable_irq();
	// read key state and key press atomic !
	return get_key_press(key_state & key_mask);
}

/*
static uint16_t get_key_long(uint16_t key_mask) {
	return get_key_press(get_key_rpt(key_mask));
}
*/

static void key_timerproc() {
	static uint16_t key_in_last, rpt;
	uint16_t key_in, c;

	key_in = KEY_PORT;

	c = key_in_last & key_in & ~key_state;

//	key_state = key_state  & key_in_last | (key_state  | key_in_last) & key_in;
//	key_state = key_state  & key_in      | (key_state  | key_in)      & key_in_last;

	key_state = c | ((key_in_last | key_in) & key_state);
		
//	key_state = (key_state&key_in_last) | (key_state&key_in) | (key_in_last&key_in);

	key_press |= c;
	
	key_in_last = key_in;
	

	if ((key_state & REPEAT_MASK) == 0) // check repeat function
		rpt = REPEAT_START;
	if (--rpt == 0) {
		rpt = REPEAT_NEXT; // repeat delay
		key_rpt |= key_state & REPEAT_MASK;
	}


#if 0

static char ds[30];
int dsi = 0;

ds[dsi++] = key_state & 1   ? '1' : '0';
ds[dsi++] = key_in_last & 1 ? '1' : '0';
ds[dsi++] = key_in & 1      ? '1' : '0';
ds[dsi++] = ' ';

//ds[dsi++] = key_state & 1   ? '1' : '0';
//ds[dsi++] = key_in_last & 1 ? '1' : '0';

//ds[dsi++] = ' ';
//ds[dsi++] = ' ';
ds[dsi++] = 0;
puts(ds);	
#endif

}

/*--------------------------------------------------------------------------*/

void sys_tick_handler(void)
{
	static int tick_10ms = 0;
	static int count_ms = 0;

	int i;

	++tick_10ms;
	if (tick_10ms == 10)
	{
		Stat |= S_10MS_TO;

		tick_10ms = 0;
		
		i = led_timer[0];
		if (i)
			led_timer[0] = i - 1;
		i = led_timer[1];
		if (i)
			led_timer[1] = i - 1;

		key_timerproc();
		
		/* Drive timer procedure of low level disk I/O module */
		//disk_timerproc();
	}
	
	count_ms++;
	if (count_ms == 1000) {
		count_ms = 0;

		i = timeout_1s;
		if (i)
			timeout_1s = i - 1;
	}
}

void rtc_isr(void)
{
	/* The interrupt flag isn't cleared by hardware, we have to do it. */
	rtc_clear_flag(RTC_SEC);

}

/*--------------------------------------------------------------------------*/

void tim3_set(int mode)
{
	uint16_t cc_mode;
	
	cc_mode = TIM_CCMR2_CC4S_OUT;

	TIM3_CR1 = TIM_CR1_CMS_EDGE | TIM_CR1_DIR_UP /*| TIM_CR1_OPM */ ;

	if (mode < 0)
		cc_mode |= TIM_CCMR2_OC4M_FORCE_LOW;
	else if (mode == 0)
		cc_mode |= TIM_CCMR2_OC4M_FORCE_HIGH;
	else {
		TIM3_ARR = mode;
		TIM3_CCR4 = mode/2;
		cc_mode |= TIM_CCMR2_OC4M_PWM2;
	}
			
	TIM3_CCMR2 = cc_mode;
		
	if (mode > 0)
		TIM3_CR1 |= TIM_CR1_CEN;
}

/*--------------------------------------------------------------------------*/

static uint32_t z80_sram_cmp(uint32_t addr, int length, uint8_t wval, int inc)
{
	uint8_t rval;
	int errors = 0;
	
	printf("SRAM: Check %#.5x byte... ", length); //fflush(stdout);
	while (length--) {
		if ((rval = z80_read(addr)) != wval) {
			if (errors == 0) { 
				printf("\nSRAM: Address  W  R\n" \
				       "      -------------\n");
//					       12345  00 11
			}
			printf("       %.5lx  %.2x %.2x\n", addr, wval, rval);
			
			if (++errors > 16 )
				break;
		}
		addr++;
		wval += inc;
	}
	printf("Done.\n");

	return addr;
}

#if 0
static void z80_sram_fill(uint32_t addr, int length, uint8_t startval, int inc)
{
	printf("SRAM: Write %#.5x byte... ", length); //fflush(stdout);
	while (length--) {
		z80_write(addr, startval);
		++addr; 
		startval += inc;
	}
	printf("Done.\n");
}


void z80_sram_fill_string(uint32_t addr, int length, const char *text)
{
	char c;
	const char *p = text;

	while (length--) {
		z80_write(addr++, c = *p++);
		if (c == 0)
			p = text;
	}
}


uint32_t z80_sram_cmp_string(uint32_t addr, int length, const char *text)
{
	char c;
	const char *p = text;

	while (length--) {
		c = *p++;
		if (z80_read(addr) != c)
			break;
		++addr;
		if (c == 0)
			p = text;
	}
	return addr;
}

const char * const qbfox = "Zhe quick brown fox jumps over the lazy dog!";
const char * const qbcat = "Zhe quick brown fox jumps over the lazy cat!";

#endif

uint8_t z80_get_byte(uint32_t adr)
{
	uint8_t data;
	
	z80_request_bus();
	data = z80_read(adr),
	z80_release_bus();
	
	return data;
}


/*--------------------------------------------------------------------------*/

static void do_10ms(void) 
{
	for (uint_fast8_t i = 0; i < 2; i++) {
		switch (led_stat[i].mode) {
		case PULSE:
			if (led_timer[i] == 0) {
				led_off(i);
				led_stat[i].mode = NOTHING;
			}
			break;
		case BLINK1:
		case BLINK2:
			if (led_timer[i] == 0) {
				if (led_is_on(i))
					led_timer[i] = led_stat[i].offtime;
				else
					led_timer[i] = led_stat[i].ontime;
				led_toggle(i);
			}
			break;
		default:
			break;
		}
	}
}

void wait_for_z80_init_done(void)
{
	uint8_t buf, out_i, in_i, mask;
	int to;

	timeout_1s = 10;
	to = 0;
	while (timeout_1s) {
		if (to != timeout_1s) {
			buf = z80_get_byte(tx_fifo - 0);
			out_i = z80_get_byte(tx_fifo - 1);
			in_i = z80_get_byte(tx_fifo - 2);
			mask = z80_get_byte(tx_fifo - 3);
			printf(" %.2x  %.2x  %.2x  %.2x\n", buf, out_i, in_i, mask);
			to = timeout_1s;

			if ((out_i == 0) && (mask == 0x7f))
				timeout_1s = 0;
		}
	}
}

/*--------------------------------------------------------------------------*/

int main(void)
{
	//uint32_t led_state = LED_BLUE_PIN;
	//uint32_t rc;
	//uint8_t startval = 0;
	//int count;
	int stat, ch;
	uint8_t c;

	clock_setup();
	gpio_setup();
	tim3_setup();
	setvbuf(stdout, NULL, _IONBF, 0);
	usart_setup();
	printf("\n(STM32F100+HD64180)_stamp Tester\n");

	z80_setup_io_infifo();
	z80_setup_bus();
	printf("z80_setup_bus done.\n");

	/*
	 * If the RTC is pre-configured just allow access, don't reconfigure.
	 * Otherwise enable it with the LSE as clock source and 0x7fff as
	 * prescale value.
	 */
	rtc_auto_awake(LSE, 0x7fff);

	systick_setup();
	///* Setup the RTC interrupt. */
	//nvic_setup();

	/* Enable the RTC interrupt to occur off the SEC flag. */
	//rtc_interrupt_enable(RTC_SEC);

	printf("get bus...");
	z80_busreq(LOW);
	z80_reset(HIGH);
	z80_request_bus();
	printf(" got it!\n");
	
	z80_memset(0, 0x76, 0x80000);
	//z80_sram_fill(0, 512 * 1024, 0x76, 0);
	z80_sram_cmp(0, 512 * 1024, 0x76, 0);
	
	z80_write_block((unsigned char *) hdrom, 0, hdrom_length);	
	z80_reset(LOW);
	printf("bus released!\n");
	z80_release_bus();
	z80_reset(HIGH);
	printf(" reset released!\n");
	
	wait_for_z80_init_done();
	z80_memfifo_init();
	
	ledset(0, BLINK1, 50);

	while (1) {
//		static int tickstat = 0;
		
		if (Stat & S_10MS_TO) {
			Stat &= ~S_10MS_TO;
			do_10ms();
		}


//		if (get_key_long(KEY0))
//			ledset(1, PULSE, 100);
			
		if (get_key_short(KEY0)) {
			z80_reset_pulse();
			wait_for_z80_init_done();
			z80_memfifo_init();
		}


/*
		switch (tickstat) {
		
			case 0:
				if (BBIO_PERIPH(GPIOA+IDR, 0))
				{
					tickstat = 1;

					LED_GREEN_ON();
					LED_GREEN_OFF();
					LED_GREEN_ON();
					delay_systicks(12);
					LED_GREEN_OFF();
				}
				break;
			default:
				if (!BBIO_PERIPH(GPIOA+IDR, 0))
					tickstat = 0;
		}
*/

		//BBIO_PERIPH(LED_PORT+0x0C, 9) = BBIO_PERIPH(GPIOA+0x08, 0);
		
		//BBIO_PERIPH(LED_PORT+0x0C, 9) = !z80_stat_halt();
		
		//BBIO_PERIPH(LED_PORT+0x0C, 9) = (~key_state & KEY0) != 0;
		

/*
		stat = z80_fifo_is_not_full(rx_fifo);
		if(stat) {
			z80_fifo_putc(rx_fifo, 'y');
			if (++count == 154) {
				putchar('\n');
				putchar('\r');
				count = 0;
			}

		}
*/

		stat = 	usart_get_flag(USART_CONSOLE, USART_SR_RXNE);
		if (stat) {
			c = usart_recv(USART_CONSOLE) & 0xff;
			switch (c) {
				case 'H':
					tim3_set(-1);
					break;
				case 'L':
					tim3_set(0);
					break;
				case 'P':
					tim3_set(24000000/1000000 * 5); /* 5 us */
					break;
				default:
					z80_memfifo_putc(fifo_out, c);
			}
		}

		if (timeout_1s == 0) {
		
			while (!z80_memfifo_is_empty(fifo_in)) {
//				LED_GREEN_ON();
				c = z80_memfifo_getc(fifo_in);
				putchar(c);
//				LED_GREEN_OFF();
			}
			
			timeout_1s = 1;
		}
		
		while ((ch = z80_io_infifo_getc()) >= 0) {
			static int linepos;

			if (linepos == 0)
				printf("\n");
			printf(" 0x%.2X ", ch);
			linepos = (linepos + 1) % 16;
		}
	}

	return 0;
}

#if 0

static char ds[30];
int dsi = 0;

ds[dsi++] = key_state1 & 1  ? '1' : '0';
ds[dsi++] = key_in_last & 1 ? '1' : '0';
ds[dsi++] = key_in & 1      ? '1' : '0';
ds[dsi++] = ' ';
ds[dsi++] = key_state1 & 1  ? '1' : '0';
ds[dsi++] = key_in_last & 1 ? '1' : '0';

ds[dsi++] = ' ';
ds[dsi++] = ' ';
ds[dsi++] = key_state & 1  ? '1' : '0';
ds[dsi++] = ct1 & 1  ? '0' : '1';
ds[dsi++] = ct0 & 1  ? '0' : '1';
ds[dsi++] = ' ';
ds[dsi++] = key_state & 1  ? '1' : '0';
//ds[dsi++] = '\r';
//ds[dsi++] = '\n';
ds[dsi++] = 0;
puts(ds);	
#endif