summaryrefslogtreecommitdiff
path: root/avr/z180-stamp-avr.c
blob: 4ea4d472f7ccf3f038f16be628883e9287925e18 (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
/*
 */


#include <avr/io.h>
//#include <avr/power.h>
//#include <avr/pgmspace.h>
//#include <avr/interrupt.h>
//#include <util/atomic.h>
//#include <avr/sleep.h>
//#include <string.h>

#include <stdio.h>


#include "debug.h"
#include "serial.h"
#include "z80-if.h"

#define const const __flash
#include "../z180/hdrom.h"
#undef const

#define FLASH __flash
//#define FLASH 

#define ESCCHAR		('^'-0x40)

#define S_10MS_TO	(1<<0)


volatile int_fast8_t timeout_1s;
volatile uint_least8_t Stat;


/****************************************************************/

#define P_ADL		PORTA
#define P_ADH		PORTC
#define P_ADB		PORTE
#define PIN_ADB		PINE

#define ADB_WIDTH	3
#define ADB_SHIFT	2
//#define ADB_PORT	GPIOE

#define MASK(n)	((1<<(n))-1)
#define SMASK(w,s) (MASK(w) << (s))

typedef union {
	uint32_t l;
	uint16_t w[2];
	uint8_t b[4];
} addr_t;
 


void z80_setaddress(uint32_t addr)
{
	addr_t x;

	x.l = addr;
	P_ADL = x.b[0];
	P_ADH = x.b[1];
	PIN_ADB = ((x.b[2] << ADB_SHIFT) ^ P_ADB) & MASK(ADB_WIDTH) << ADB_SHIFT ;

}

/****************************************************************/

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


void sys_tick_handler(void)
{
	static int_fast8_t tick_10ms = 0;
	static int_fast16_t count_ms = 0;

	int_fast8_t i;

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

		tick_10ms = 0;
		
		
		/* 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;
	}
}


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

static uint32_t z80_sram_cmp(uint32_t addr, uint32_t length, uint8_t wval, int inc)
{
	uint8_t rval;
	int_fast8_t errors = 0;
	
	DBG_P(1, "SRAM: Check %#.5x byte... ", length);
	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;
	}
	DBG_P(1, "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) 
{
}

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

struct msg_item {
	uint8_t fct;
	uint8_t sub_min, sub_max;
	void (*func)(uint8_t, int, uint8_t *);
};

uint32_t msg_to_addr(uint8_t *msg)
{
	union {
		uint32_t as32;
		uint8_t as8[4];
	} addr;

	addr.as8[0] = msg[0];
	addr.as8[1] = msg[1];
	addr.as8[2] = msg[2];
	addr.as8[3] = 0;

	return addr.as32;
}

void do_msg_ini_msgfifo(uint8_t subf, int len, uint8_t * msg)
{
	(void)subf; (void)len;

	z80_init_msg_fifo(msg_to_addr(msg));
}


void do_msg_ini_memfifo(uint8_t subf, int len, uint8_t * msg)
{
	(void)len;

	z80_memfifo_init(subf - 1, msg_to_addr(msg));
}


void do_msg_char_out(uint8_t subf, int len, uint8_t * msg)
{
	(void)subf;

	while (len--)
		putchar(*msg++);
}


const FLASH struct msg_item z80_messages[] =
{
	{ 0,			/* fct nr. */
	  0, 0,			/* sub fct nr. from, to */
	  &do_msg_ini_msgfifo},
	{ 0,
	  1, 2,
	  &do_msg_ini_memfifo},
	{ 1,
	  1, 1,
	  &do_msg_char_out},
	{ 0xff,				/* end mark */
	  0, 0,
	  0},

};




void do_message(int len, uint8_t *msg)
{
	uint8_t fct, sub_fct;
	int_fast8_t i = 0;

	if (len >= 2) {
		fct = *msg++;
		sub_fct = *msg++;
		len -= 2;

		while (fct != z80_messages[i].fct)
			++i;

		if (z80_messages[i].fct == 0xff) {
			DBG_P(1, "do_message: Unknown function: %i, %i\n",
					fct, sub_fct);
			return; /* TODO: unknown message # */
		}

		while (fct == z80_messages[i].fct) {
			if (sub_fct >= z80_messages[i].sub_min && sub_fct <= z80_messages[i].sub_max )
				break;
			++i;
		}

		if (z80_messages[i].fct != fct) {
			DBG_P(1, "do_message: Unknown sub function: %i, %i\n",
					fct, sub_fct);
			return; /* TODO: unknown message sub# */
		}

		(z80_messages[i].func)(sub_fct, len, msg);


	} else {
		/* TODO: error */
		DBG_P(1, "do_message: to few arguments (%i); this shouldn't happen!\n", len);
	}
}



#define CTRBUF_LEN 256

void check_msg_fifo(void)
{
	int ch;
	static int_fast8_t state;
	static int msglen,idx;
	static uint8_t buffer[CTRBUF_LEN];

	while (state != 3 && (ch = z80_msg_fifo_getc()) >= 0) {
		switch (state) {
		case 0:		/* wait for start of message */
			if (ch == 0x81)	{
				msglen = 0;
				idx = 0;
				state = 1;
			}
			break;
		case 1:		/* get msg len */
			if (ch > 0 && ch <= CTRBUF_LEN) {
				msglen = ch;
				state = 2;
			} else
				state = 0;
			break;
		case 2: 	/* get message */
			buffer[idx++] = ch;
			if (idx == msglen)
				state = 3;
			break;
		}
	}

	if (state == 3) {
		do_message(msglen, buffer);
		state = 0;
	}
}


void z80_load_mem(void)
{
	unsigned sec = 0;
	uint32_t sec_base = hdrom_start;

	DBG_P(1, "Loading z80 memory... \n");

	while (sec < hdrom_sections) {
		DBG_P(2, "  From: 0x%.5lX to: 0x%.5lX    (%5li bytes)\n",
				hdrom_address[sec],
				hdrom_address[sec]+hdrom_length_of_sections[sec] - 1,
				hdrom_length_of_sections[sec]);

		z80_write_block((unsigned char *) &hdrom[sec_base],  /* src */
		                hdrom_address[sec],                  /* dest */
				hdrom_length_of_sections[sec]);      /* len */
		sec_base+=hdrom_length_of_sections[sec];
		sec++;
	}
}
/*--------------------------------------------------------------------------*/

int main(void)
{
	int_fast8_t state = 0;
	int ch;

//	setvbuf(stdout, NULL, _IONBF, 0);
	serial_setup();
//	printf("\n(STM32F100+HD64180)_stamp Tester\n");
	printf("\n(ATMEGA1281+HD64180)_stamp Tester\n");

	DBG_P(1, "z80_setup_bus... ");
	z80_setup_msg_fifo();
	z80_setup_bus();
	DBG_P(1, "done.\n");


	DBG_P(1, "Get bus... ");
	z80_busreq(LOW);
	z80_reset(HIGH);
	z80_request_bus();
	DBG_P(1, "got it!\n");
	
	z80_memset(0, 0x76, 0x80000);
	//z80_sram_fill(0, 512 * 1024, 0x76, 0);
	z80_sram_cmp(0, (uint32_t)512 * 1024, 0x76, 0);
	
	z80_load_mem();
	z80_reset(LOW);
	DBG_P(1, "Bus released!\n");
	z80_release_bus();
	z80_reset(HIGH);
	DBG_P(1, "Reset released!\n");
	
	
	while (1) {

		if (Stat & S_10MS_TO) {
			Stat &= ~S_10MS_TO;
			do_10ms();
		}


		if ((ch = serial_getc()) >= 0) {
			switch (state) {
			case 0:
				if (ch == ESCCHAR) {
					state = 1;
					/* TODO: Timer starten */
				} else
					z80_memfifo_putc(fifo_out, ch);
				break;
			case 1:
				switch (ch) {

				case 'r':
					z80_reset_pulse();
					break;

				case ESCCHAR:
				default:
					z80_memfifo_putc(fifo_out, ch);
				}
				state = 0;
				break;
			}
		}

		check_msg_fifo();
	}

	return 0;
}