prom / prom.c

  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
/**
 * \file PROM dancer
 *
 * Read up to 40 pin DIP PROMs using a Teensy++ 2.0
 */

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <stdint.h>
#include <string.h>
#include <util/delay.h>
#include "usb_serial.h"
#include "bits.h"

void send_str(const char *s);
uint8_t recv_str(char *buf, uint8_t size);
void parse_and_execute_command(const char *buf, uint8_t num);

static uint8_t
hexdigit(
	uint8_t x
)
{
	x &= 0xF;
	if (x < 0xA)
		return x + '0' - 0x0;
	else
		return x + 'A' - 0xA;
}


static uint8_t
printable(
	uint8_t x
)
{
	if ('A' <= x && x <= 'Z')
		return 1;
	if ('a' <= x && x <= 'z')
		return 1;
	if ('0' <= x && x <= '9')
		return 1;
	if (x == ' ')
		return 1;

	return 0;
}
	

/** Mapping of AVR IO ports to the ZIF socket pins */
static const uint8_t ports[] = {
	[ 1]	= 0xB6,
	[ 2]	= 0xB5,
	[ 3]	= 0xB4,
	[ 4]	= 0xB3,
	[ 5]	= 0xB2,
	[ 6]	= 0xB1,
	[ 7]	= 0xB0,
	[ 8]	= 0xE7,
	[ 9]	= 0xE6,
	[10]	= 0xA2,
	[11]	= 0xA1,
	[12]	= 0xF0,
	[13]	= 0xF1,
	[14]	= 0xF2,
	[15]	= 0xF3,
	[16]	= 0xF4,
	[17]	= 0xF5,
	[18]	= 0xF6,
	[19]	= 0xF7,
	[20]	= 0xA3,

	[21]	= 0xA7,
	[22]	= 0xC7,
	[23]	= 0xC6,
	[24]	= 0xC5,
	[25]	= 0xC4,
	[26]	= 0xC3,
	[27]	= 0xC2,
	[28]	= 0xC1,
	[29]	= 0xC0,
	[30]	= 0xE1,
	[31]	= 0xE0,
	[32]	= 0xD7,
	[33]	= 0xD6,
	[34]	= 0xD5,
	[35]	= 0xD4,
	[36]	= 0xD3,
	[37]	= 0xD2,
	[38]	= 0xD1,
	[39]	= 0xD0,
	[40]	= 0xB7,
};


typedef struct
{
	char name[16];
	uint8_t pins;
	uint8_t addr_width;
	uint8_t data_width;
	uint8_t addr_pins[24];
	uint8_t data_pins[24];
	uint8_t hi_pins[8];
	uint8_t lo_pins[8];
} prom_t;

/** M27C512
 * 28 total pins
 * 16 pins of address,
 * 8 pins of data,
 * some hi, some low
 */
static const prom_t prom_m27c512 = {
	.name		= "M27C512",
	.pins		= 28,

	.addr_width	= 16,
	.addr_pins	= {
		10, 9, 8, 7, 6, 5, 4, 3, 25, 24, 21, 23, 2, 26, 27, 1,
	},

	.data_width	= 8,
	.data_pins	= {
		11, 12, 13, 15, 16, 17, 18, 19,
	},

	.hi_pins	= { 28, },
	.lo_pins	= { 22, 20, 14, },
};


static const prom_t prom_m27c256 = {
	.name		= "M27C256",
	.pins		= 28,
	.addr_width	= 15,
	.addr_pins	= {
		10, 9, 8, 7, 6, 5, 4, 3, 25, 24, 21, 23, 2, 26, 27,
	},

	.data_width	= 8,
	.data_pins	= {
		11, 12, 13, 15, 16, 17, 18, 19,
	},
	.hi_pins	= { 28, 1 },
	.lo_pins	= { 22, 20, 14, },
};


/** 8192x8 UV EEPROM, found in DX synth */
static const prom_t prom_mbm2764= {
	.name		= "MBM2764-30",
	.pins		= 28,
	.addr_width	= 13,
	.addr_pins	= {
		10, 9, 8, 7, 6, 5, 4, 3, 25, 24, 21, 23, 2,
	},

	.data_width	= 8,
	.data_pins	= {
		11, 12, 13, 15, 16, 17, 18, 19,
	},
	.hi_pins	= {
		28, // vdd, disable if external power is used
		27, // pgm
		1,  // vpp
	},
	.lo_pins	= { 22, 20, 14, }, // !oe, !cs, gnd
};

/** Apple Mac SE PROM chips
 * Similar to a M27C512, but with the 17th address line on 22 instead of Vpp
 */
static const prom_t prom_apple = {
	.name		= "APPLE PROM",
	.pins		= 28,
	.addr_width	= 17,
	.addr_pins	= {
		10, 9, 8, 7, 6, 5, 4, 3, 25, 24, 21, 23, 2, 26, 27, 1, 22
	},

	.data_width	= 8,
	.data_pins	= {
		11, 12, 13, 15, 16, 17, 18, 19,
	},
	.hi_pins	= { 28, },
	.lo_pins	= { 20, 14, },
};

/** Select one of the chips */
static const prom_t * prom = &prom_mbm2764;


/** Translate PROM pin numbers into ZIF pin numbers */
static inline uint8_t
prom_pin(
	const uint8_t pin
)
{
	if (pin <= prom->pins / 2)
		return ports[pin];
	else
		return ports[pin + 40 - prom->pins];
}
		

/** Select a 32-bit address for the current PROM */
static void
prom_set_address(
	uint32_t addr
)
{
	for (uint8_t i = 0 ; i < prom->addr_width ; i++)
	{
		out(prom_pin(prom->addr_pins[i]), addr & 1);
		addr >>= 1;
	}
}


static uint8_t
_prom_read(void)
{
	uint8_t b = 0;
	for (uint8_t i = 0 ; i < prom->data_width  ; i++)
	{
		uint8_t bit = in(prom_pin(prom->data_pins[i])) ? 0x80 : 0;
		b = (b >> 1) | bit;
	}

	return b;
}


/** Read a byte from the PROM at the specified address..
 * \todo Update this to handle wider than 8-bit PROM chips.
 */
static uint8_t
prom_read(
	uint32_t addr
)
{
	prom_set_address(addr);
	for(uint8_t i = 0 ; i < 255; i++)
	{
		asm("nop");
		asm("nop");
		asm("nop");
		asm("nop");
	}

	uint8_t old_r = _prom_read();

	// Try reading a few times to be sure,
	// or until the values converge
	for (uint8_t i = 0 ; i < 8 ; i++)
	{
		uint8_t r = _prom_read();
		if (r == old_r)
			break;
		old_r = r;
	}

	return old_r;
}


/** Configure all of the IO pins for the new PROM type */
static void
prom_setup(void)
{
	// Configure all of the address pins as outputs
	for (uint8_t i = 0 ; i < prom->addr_width ; i++)
		ddr(prom_pin(prom->addr_pins[i]), 1);

	// Configure all of the data pins as inputs
	for (uint8_t i = 0 ; i < prom->data_width ; i++)
		ddr(prom_pin(prom->data_pins[i]), 0);

	// Configure all of the hi and low pins as outputs
	for (uint8_t i = 0 ; i < array_count(prom->hi_pins) ; i++)
	{
		uint8_t pin = prom_pin(prom->hi_pins[i]);
		if (pin == 0)
			continue;
		out(pin, 1);
		ddr(pin, 1);
	}

	for (uint8_t i = 0 ; i < array_count(prom->lo_pins) ; i++)
	{
		uint8_t pin = prom_pin(prom->lo_pins[i]);
		if (pin == 0)
			continue;
		out(pin, 0);
		ddr(pin, 1);
	}

	// Let things stabilize for a little while
	_delay_ms(250);
}


/** Switch all of the ZIF pins back to tri-state to make it safe.
 * Doesn't matter what PROM is inserted.
 */
static void
prom_tristate(void)
{
	for (uint8_t i = 1 ; i <= 40 ; i++)
	{
		ddr(ports[i], 0);
		out(ports[i], 0);
	}
}



static uint8_t
usb_serial_getchar_block(void)
{
	while (1)
	{
		while (usb_serial_available() == 0)
			continue;

		uint16_t c = usb_serial_getchar();
		if (c != -1)
			return c;
	}
}


typedef struct
{
	uint8_t soh;
	uint8_t block_num;
	uint8_t block_num_complement;
	uint8_t data[128];
	uint8_t cksum;
} __attribute__((__packed__))
xmodem_block_t;

#define XMODEM_SOH 0x01
#define XMODEM_EOT 0x04
#define XMODEM_ACK 0x06
#define XMODEM_CAN 0x18
#define XMODEM_C 0x43
#define XMODEM_NACK 0x15
#define XMODEM_EOF 0x1a


/** Send a block.
 * Compute the checksum and complement.
 *
 * \return 0 if all is ok, -1 if a cancel is requested or more
 * than 10 retries occur.
 */
static int
xmodem_send_block(
	xmodem_block_t * const block
)
{
	// Compute the checksum and complement
	uint8_t cksum = 0;
	for (uint8_t i = 0 ; i < sizeof(block->data) ; i++)
		cksum += block->data[i];
	block->cksum = cksum;
	block->block_num_complement = 0xFF - block->block_num;

	// Send the block, and wait for an ACK
	uint8_t retry_count = 0;
	goto send_block;

	while (retry_count++ < 10)
	{
		uint8_t c = usb_serial_getchar_block();
		if (c == XMODEM_ACK)
			return 0;
		if (c == XMODEM_CAN)
			return -1;
		if (c != XMODEM_NACK)
			continue;

		send_block:
		usb_serial_write((void*) block, sizeof(*block));
	}

	// Failure or cancel
	return -1;
}


/** Send the entire PROM memory */
static void
xmodem_send(void)
{
	uint8_t c;
	static xmodem_block_t block;

	block.soh = 0x01;
	block.block_num = 0x00;

	// wait for initial nak
	while (1)
	{
		c = usb_serial_getchar_block();
		if (c == XMODEM_NACK)
			break;
		if (c == XMODEM_CAN)
			return;
	}

	// Ending address
	const uint32_t end_addr = (((uint32_t) 1) << prom->addr_width) - 1;

	// Bring the pins up to level
	prom_setup();

	// Start sending!
	uint32_t addr = 0;
	while (1)
	{
		block.block_num++;
		for (uint8_t off = 0 ; off < sizeof(block.data) ; off++)
			block.data[off] = prom_read(addr++);

		if (xmodem_send_block(&block) < 0)
			return;

		// If we have wrapped the address, we are done
		if (addr >= end_addr)
			break;
	}

#if 0
/* Don't send EOF?  rx adds it to the file? */
	block.block_num++;
	memset(block.data, XMODEM_EOF, sizeof(block.data));
	if (xmodem_send_block(&block) < 0)
		return;
#endif

	// File transmission complete.  send an EOT
	while (1)
	{
		usb_serial_putchar(XMODEM_EOT);
		c = usb_serial_getchar_block();
		if (c == XMODEM_ACK
		||  c == XMODEM_CAN)
			break;
	}
}



int main(void)
{
	// set for 16 MHz clock
#define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n))
	CPU_PRESCALE(0);

	// Disable the ADC
	ADMUX = 0;

	// initialize the USB, and then wait for the host
	// to set configuration.  If the Teensy is powered
	// without a PC connected to the USB port, this 
	// will wait forever.
	usb_init();
	while (!usb_configured()) /* wait */ ;
	_delay_ms(1000);

	// wait for the user to run their terminal emulator program
	// which sets DTR to indicate it is ready to receive.
	while (!(usb_serial_get_control() & USB_SERIAL_DTR))
		continue;


	// discard anything that was received prior.  Sometimes the
	// operating system or other software will send a modem
	// "AT command", which can still be buffered.
	usb_serial_flush_input();


#if 0
	uint16_t addr = 0;
	char line[64];
	uint8_t off = 0;

	send_str(PSTR("Looking for strings\r\n"));

	while (1)
	{
		addr++;
		if (addr == 0)
			send_str(PSTR("wrap\r\n"));

		uint8_t byte = read_byte(addr);
		if (byte == 0)
			continue;

		if (off == 0)
		{
			line[off++] = hexdigit(addr >> 12);
			line[off++] = hexdigit(addr >>  8);
			line[off++] = hexdigit(addr >>  4);
			line[off++] = hexdigit(addr >>  0);
			line[off++] = '=';
		}

		if (printable(byte))
		{
			line[off++] = byte;
			if (off < sizeof(line) - 2)
				continue;
		} else {
			line[off++] = hexdigit(byte >> 4);
			line[off++] = hexdigit(byte >> 0);
		}

		line[off++] = '\r';
		line[off++] = '\n';
		usb_serial_write(line, off);
		off = 0;
	}
#else
	while (1)
	{
		send_str(PSTR("Start your xmodem receive\r\n"));

		// And now send it
		xmodem_send();
		prom_tristate();
	}
#endif
}


// Send a string to the USB serial port.  The string must be in
// flash memory, using PSTR
//
void send_str(const char *s)
{
	char c;
	while (1) {
		c = pgm_read_byte(s++);
		if (!c) break;
		usb_serial_putchar(c);
	}
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.