summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/machine/i7220.cpp
blob: 0e86dc081bb2027b57e07ba68114742f628969c8 (plain) (blame)
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
// license:BSD-3-Clause
// copyright-holders:Sergey Svishchev
/**********************************************************************

    Intel 7220 Bubble Memory Controller emulation

    References:
    - http://www.decadecounter.com/vta/pdf/BPK72UM.pdf
    - http://www.decadecounter.com/vta/pdf/7220-1.pdf
    - http://www.decadecounter.com/vta/pdf/7242.pdf
    - http://www.decadecounter.com/vta/articleview.php?item=359

    Implemented at least partially
    - commands
        Read Bubble Data, Write Bubble Data
        Initialize, Reset FIFO, Abort, Software Reset
        Read FSA Status, MBM Purge

    Not implemented
    - interrupts
    - DMA
    - commands
        Read Seek, Write Seek, Read Corrected Data, all Bootloop-related
    - access to other than 2 FSA channels at once

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

#include "emu.h"
#include "i7220.h"


//#define LOG_GENERAL (1U <<  0) //defined in logmacro.h already
#define LOG_REGISTER  (1U <<  1)
#define LOG_DEBUG     (1U <<  2)

//#define VERBOSE (LOG_DEBUG)
//#define LOG_OUTPUT_FUNC printf
#include "logmacro.h"

#define LOGREG(...) LOGMASKED(LOG_REGISTER, __VA_ARGS__)
#define LOGDBG(...) LOGMASKED(LOG_DEBUG, __VA_ARGS__)


// device type definition
DEFINE_DEVICE_TYPE(I7220, i7220_device, "i7220", "Intel 7220 BMC")



//**************************************************************************
//  HELPERS
//**************************************************************************



//**************************************************************************
//  LIVE DEVICE
//**************************************************************************

//-------------------------------------------------
//  i7220_device - constructor
//-------------------------------------------------

i7220_device::i7220_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, I7220, tag, owner, clock)
	, device_image_interface(mconfig, *this)
	, intrq_cb(*this)
	, drq_cb(*this)
{
}


//-------------------------------------------------
//  device_start - device-specific startup
//-------------------------------------------------

void i7220_device::device_start()
{
	// resolve callbacks
	intrq_cb.resolve_safe();
	drq_cb.resolve_safe();

	bi.tm = timer_alloc(0);

	// register for state saving
	save_item(NAME(m_regs));
	save_item(NAME(m_rac));
	save_item(NAME(m_cmdr));
	save_item(NAME(m_str));
	save_item(NAME(m_blr));
	save_item(NAME(m_ar));
}

//-------------------------------------------------
//  device_reset - device-specific startup
//-------------------------------------------------

void i7220_device::device_reset()
{
	main_phase = PHASE_IDLE;
	bi.main_state = IDLE;
	bi.sub_state = IDLE;

	set_drq(false);
	set_irq(false);
	fifo_clear();

	m_rac = m_cmdr = m_str = m_blr = m_ar = 0;
	memset(&m_regs, 0, sizeof(m_regs));
}

void i7220_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
{
	general_continue(bi);
}

image_init_result i7220_device::call_load()
{
	if (length() != (m_data_size * I7110_MBM_SIZE))
	{
		return image_init_result::FAIL;
	}
	return image_init_result::PASS;
}


void i7220_device::set_drq(bool state)
{
	if (state != drq)
	{
		drq = state;
		drq_cb(drq);
	}
}

void i7220_device::set_irq(bool state)
{
	if (state != irq)
	{
		irq = state;
		intrq_cb(irq);
	}
}

void i7220_device::update_drq()
{
	switch (bi.main_state)
	{
	case READ_DATA:
		set_drq(m_fifo_size < 22 ? false : true);
		break;

	case WRITE_DATA:
		set_drq(m_fifo_size < (40 - 22) ? true : false);
		break;
	}
}


void i7220_device::fifo_clear()
{
	m_fifo.clear();
	m_fifo_size = 0;
	set_drq(false);
}

uint8_t i7220_device::fifo_pop()
{
	uint8_t val;

	if (m_fifo.empty())
	{
		return m_fifo.dequeue();
	}
	else
	{
		val = m_fifo.dequeue();
		m_fifo_size--;
		if (main_phase == PHASE_EXEC)
		{
			update_drq();
		}
		return val;
	}
}

void i7220_device::fifo_push(uint8_t val)
{
	if (m_fifo.full())
	{
		return;
	}
	else
	{
		m_fifo.enqueue(val);
		m_fifo_size++;
		if (main_phase == PHASE_EXEC)
		{
			update_drq();
		}
	}
}

void i7220_device::update_regs()
{
	m_blr = (m_regs[R_BLR_M] << 8) + m_regs[R_BLR_L];
	m_ar = (m_regs[R_AR_M] << 8) + m_regs[R_AR_L];
	blr_count = m_blr & 0x7ff;
	blr_nfc = (m_blr >> 12) ? ((m_blr >> 12) << 1) : 1;
	ar_addr = m_ar & 0x7ff;
	ar_mbm = (m_ar >> 11) & 15;
}


void i7220_device::start_command(int cmd)
{
	main_phase = PHASE_EXEC;
	m_str &= ~SR_CLEAR;

	switch (cmd)
	{
	// determines how many FSAs are present, reads and decodes bootloop from each MBM and stores result in FSA bootloop regs.
	// all parametric registers must be properly set up before issuing Initialize command.
	// NFC bits in BLR MSB must be set to 0001 before issuing this command.
	// MBM GROUP SELECT bits in the AR must select the last MBM in the system.
	case C_INIT:
		LOG("BMC INIT: BLR %04x (NFC %d pages %d) AR %04x (MBM %d addr %03x) ER %02d\n",
			m_blr, blr_nfc, blr_count, m_ar, ar_mbm, ar_addr, m_regs[R_ER]);
		if (blr_nfc != 2)
		{
			command_fail_start(bi);
		}
		else
		{
			init_start(bi);
		}
		break;

	case C_READ_FSA_STATUS:
		read_fsa_start(bi);
		break;

	// all parametric registers must be properly set up before issuing Read Bubble Data command
	case C_READ:
		LOG("BMC RBD: BLR %04x (NFC %d pages %d) AR %04x (MBM %d addr %03x) ER %02d\n",
			m_blr, blr_nfc, blr_count, m_ar, ar_mbm, ar_addr, m_regs[R_ER]);
		if (ar_mbm >= m_data_size || blr_nfc != 2)
		{
			command_fail_start(bi);
		}
		else
		{
			read_data_start(bi);
		}
		break;

	case C_WRITE:
		LOG("BMC WBD: BLR %04x (NFC %d pages %d) AR %04x (MBM %d addr %03x) ER %02d\n",
			m_blr, blr_nfc, blr_count, m_ar, ar_mbm, ar_addr, m_regs[R_ER]);
		if (ar_mbm >= m_data_size || blr_nfc != 2)
		{
			command_fail_start(bi);
		}
		else
		{
			write_data_start(bi);
		}
		break;

	// XXX clears all BMC registers (except BLR &c), counters and the MBM address RAM.  used by Initialize as subroutine.
	case C_MBM_PURGE:
		m_regs[R_AR_L] = 0;
		m_regs[R_AR_M] &= ~7;
		update_regs();
		main_phase = PHASE_RESULT;
		m_str |= SR_DONE;
		break;

	// controlled termination of currently executing command.  command accepted in BUSY state.
	// if not BUSY, clears FIFO.
	case C_ABORT:
		if (main_phase == PHASE_IDLE)
		{
			fifo_clear();
		}
		else
		{
			main_phase = PHASE_RESULT;
			bi.main_state = bi.sub_state = IDLE;
		}
		m_str |= SR_DONE;
		break;

	case C_RESET_FIFO:
		fifo_clear();
		main_phase = PHASE_RESULT;
		m_str |= SR_DONE;
		break;

	// clears BMC FIFO and all registers except those containing init parameters.  sends Reset to every FSA.
	case C_RESET:
		m_regs[R_UR] = 0;
		fifo_clear();
		main_phase = PHASE_RESULT;
		m_str |= SR_DONE;
		break;

	default:
		command_fail_start(bi);
		break;
	}
}

void i7220_device::general_continue(bubble_info &bi)
{
	switch (bi.main_state)
	{
	case IDLE:
		break;

	case INIT:
		init_continue(bi);
		break;

	case FAIL:
		command_fail_continue(bi);
		break;

	case READ_FSA:
		read_fsa_continue(bi);
		break;

	case READ_DATA:
		read_data_continue(bi);
		break;

	case WRITE_DATA:
		write_data_continue(bi);
		break;

	default:
		LOG("BMC general_continue on unknown main-state %d\n", bi.main_state);
		break;
	}
}

void i7220_device::delay_cycles(emu_timer *tm, int cycles)
{
	tm->adjust(attotime::from_double(double(cycles) / clock()));
}

void i7220_device::command_end(bubble_info &bi, bool success)
{
	logerror("command done (%s) - %02x\n", success ? "success" : "fail", m_str);
	main_phase = PHASE_RESULT;
	bi.main_state = bi.sub_state = IDLE;
	if (success)
	{
		m_str |= SR_DONE;
	}
	else
	{
		m_str |= SR_FAIL | SR_TE;
	}
}


void i7220_device::command_fail_start(bubble_info &bi)
{
	bi.main_state = FAIL;
	bi.sub_state = INITIALIZE;

	command_fail_continue(bi);
}

void i7220_device::command_fail_continue(bubble_info &bi)
{
	for (;;)
	{
		switch (bi.sub_state)
		{
		case INITIALIZE:
			bi.sub_state = COMMAND_DONE;
			delay_cycles(bi.tm, 1200); // XXX
			return;

		case COMMAND_DONE:
			command_end(bi, false);
			return;

		default:
			LOG("BMC fail unknown sub-state %d\n", bi.sub_state);
			return;
		}
	}
}

void i7220_device::init_start(bubble_info &bi)
{
	bi.main_state = INIT;
	bi.sub_state = INITIALIZE;

	init_continue(bi);
}

void i7220_device::init_continue(bubble_info &bi)
{
	for (;;)
	{
		switch (bi.sub_state)
		{
		case INITIALIZE:
			bi.sub_state = WAIT_FSA_REPLY;
			delay_cycles(bi.tm, m_data_size * 60); // p. 4-16 of BPK72UM
			return;

		case WAIT_FSA_REPLY:
			bi.sub_state = COMMAND_DONE;
			break;

		case COMMAND_DONE:
			command_end(bi, true);
			return;

		default:
			LOG("BMC init unknown sub-state %d\n", bi.sub_state);
			return;
		}
	}
}

void i7220_device::read_fsa_start(bubble_info &bi)
{
	bi.main_state = READ_FSA;
	bi.sub_state = INITIALIZE;

	read_fsa_continue(bi);
}

void i7220_device::read_fsa_continue(bubble_info &bi)
{
	for (;;)
	{
		switch (bi.sub_state)
		{
		case INITIALIZE:
			bi.sub_state = WAIT_FSA_REPLY;
			delay_cycles(bi.tm, m_data_size * 60); // p. 4-16 of BPK72UM
			return;

		case WAIT_FSA_REPLY:
			bi.sub_state = COMMAND_DONE;
			break;

		case COMMAND_DONE:
			// each bubble memory module has two FSA channels
			for (int a = 0; a < m_data_size; a++)
			{
				fifo_push(0x28); // FIFOMT | ECF/F
				fifo_push(0x28);
			}
			command_end(bi, true);
			return;

		default:
			LOG("BMC read fsa unknown sub-state %d\n", bi.sub_state);
			return;
		}
	}
}

void i7220_device::read_data_start(bubble_info &bi)
{
	bi.main_state = READ_DATA;
	bi.sub_state = INITIALIZE;

	read_data_continue(bi);
}

void i7220_device::read_data_continue(bubble_info &bi)
{
	for (;;)
	{
		switch (bi.sub_state)
		{
		case INITIALIZE:
			bi.sub_state = SECTOR_READ;
			bi.counter = 0; // 256-bit pages
			bi.limit = blr_count * blr_nfc;
			fseek((ar_addr * 32 * blr_nfc) + (ar_mbm * I7110_MBM_SIZE) + (bi.counter * 32), SEEK_SET);
			break;

		case SECTOR_READ:
			fread(buf, 32);
			bi.sub_state = WAIT_FSA_REPLY;
			delay_cycles(bi.tm, 270 * 20); // p. 4-14 of BPK72UM
			break;

		case WAIT_FSA_REPLY:
			LOGDBG("BMC read data: ct %02d limit %02d\n", bi.counter, bi.limit);
			if (bi.counter < bi.limit)
			{
				for (int a = 0; a < 32; a++)
					fifo_push(buf[a]);
				bi.sub_state = SECTOR_READ;
				bi.counter++;
				delay_cycles(bi.tm, 270 * 20); // p. 4-14 of BPK72UM
				return;
			}
			bi.sub_state = COMMAND_DONE;
			break;

		case COMMAND_DONE:
			command_end(bi, true);
			return;

		default:
			LOG("BMC read data unknown sub-state %d\n", bi.sub_state);
			return;
		}
	}
}

void i7220_device::write_data_start(bubble_info &bi)
{
	bi.main_state = WRITE_DATA;
	bi.sub_state = INITIALIZE;

	write_data_continue(bi);
}

void i7220_device::write_data_continue(bubble_info &bi)
{
	for (;;)
	{
		switch (bi.sub_state)
		{
		case INITIALIZE:
			bi.sub_state = WAIT_FIFO;
			bi.counter = 0;
			bi.limit = blr_count * blr_nfc * 32;
			delay_cycles(bi.tm, 270 * 20); // p. 4-14 of BPK72UM
			return;

		case WAIT_FIFO:
			LOGDBG("BMC write data: fifo %02d ct %02d limit %02d\n", m_fifo_size, bi.counter, bi.limit);
			if (m_fifo_size >= 32)
			{
				for (int a = 0; a < 32; a++)
					buf[a] = fifo_pop();
				fseek((ar_addr * 32 * blr_nfc) + (ar_mbm * I7110_MBM_SIZE) + bi.counter, SEEK_SET);
				fwrite(buf, 32);
				bi.counter += 32;
			}
			if (bi.counter < bi.limit)
			{
				delay_cycles(bi.tm, 270 * 20); // p. 4-14 of BPK72UM
				return;
			}
			bi.sub_state = COMMAND_DONE;
			break;

		case COMMAND_DONE:
			command_end(bi, true);
			return;

		default:
			LOG("BMC write data unknown sub-state %d\n", bi.sub_state);
			return;
		}
	}
}


//-------------------------------------------------
//  read -
//-------------------------------------------------

READ8_MEMBER(i7220_device::read)
{
	uint8_t data = 0;

	switch (offset & 1)
	{
	case 0:
		if (m_rac)
		{
			data = m_regs[m_rac];
			LOGREG("BMC R reg @ %02x == %02x\n", m_rac, data);
			m_rac++;
			m_rac &= 15;
		}
		else
		{
			data = fifo_pop();
			LOGREG("BMC R fifo == %02x\n", data);
		}
		break;

	case 1:
		data = m_str;
		if (main_phase == PHASE_EXEC)
		{
			data |= SR_BUSY;
			switch (bi.main_state)
			{
			case READ_DATA:
				if (!m_fifo.empty()) // XXX
				{
					data |= SR_FIFO;
				}
				break;

			case WRITE_DATA:
				if (!m_fifo.full()) // XXX
				{
					data |= SR_FIFO;
				}
				break;
			}
		}
		else
		{
			if (!m_fifo.empty())
			{
				data |= SR_FIFO;
			}
		}
		LOGREG("BMC R status == %02x (phase %d state %d:%d fifo %d drq %d)\n",
			data, main_phase, bi.main_state, bi.sub_state, m_fifo_size, drq);
		if (main_phase == PHASE_RESULT)
		{
			main_phase = PHASE_IDLE;
		}
		break;
	}

	LOGDBG("BMC R @ %d == %02x\n", offset, data);

	return data;
}

//-------------------------------------------------
//  write -
//-------------------------------------------------

WRITE8_MEMBER( i7220_device::write )
{
	static const char *commands[] = {
		"Write Bootloop Register Masked",
		"Initialize",
		"Read Bubble Data",
		"Write Bubble Data",
		"Read Seek",
		"Read Bootloop Register",
		"Write Bootloop Register",
		"Write Bootloop",
		"Read FSA Status",
		"Abort",
		"Write Seek",
		"Read Bootloop",
		"Read Corrected Data",
		"Reset FIFO",
		"MBM Purge",
		"Software Reset"
	};

	LOGDBG("BMC W @ %d <- %02x\n", offset, data);

	switch (offset & 1)
	{
	case 0:
		if (m_rac)
		{
			LOGREG("BMC W reg @ %02x <- %02x\n", m_rac, data);
			m_regs[m_rac] = data;
			update_regs();
			m_rac++;
			m_rac &= 15;
		}
		else
		{
			LOGREG("BMC W fifo <- %02x\n", data);
			fifo_push(data);
		}
		break;

	case 1:
		if (BIT(data, 5))
		{
			intrq_cb(CLEAR_LINE);
		}
		else if (BIT(data, 4))
		{
			m_cmdr = data & 15;
			if (main_phase == PHASE_IDLE)
			{
				LOG("BMC command %02x '%s'\n", data, commands[m_cmdr]);
				main_phase = PHASE_CMD;
				start_command(m_cmdr);
			}
		}
		else
		{
			m_rac = data & 15;
		}
		break;
	}
	return;
}