summaryrefslogtreecommitdiffstatshomepage
path: root/src/devices/bus/ti99x/990_hd.cpp
blob: c17f63b807e4d8b78c7958ae54577bb7fa78a88c (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
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
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
// license:GPL-2.0+
// copyright-holders:Raphael Nabet
/*
    990_hd.c: emulation of a generic ti990 hard disk controller, for use with
    TILINE-based TI990 systems (TI990/10, /12, /12LR, /10A, Business system 300
    and 300A).

    This core will emulate the common feature set found in every disk controller.
    Most controllers support additional features, but are still compatible with
    the basic feature set.  I have a little documentation on two specific
    disk controllers (WD900 and WD800/WD800A), but I have not tried to emulate
    controller-specific features.


    Long description: see 2234398-9701 and 2306140-9701.


    Raphael Nabet 2002-2003
*/

#include "emu.h"

#include "990_hd.h"

#include "harddisk.h"
#include "imagedev/harddriv.h"

/* Max sector length is bytes.  Generally 256, except for a few older disk
units which use 288-byte-long sectors, and SCSI units which generally use
standard 512-byte-long sectors. */
/* I chose a limit of 512.  No need to use more until someone writes CD-ROMs
for TI990. */
#define MAX_SECTOR_SIZE 512

/* Description of custom format */
/* We can use MAME's harddisk.c image format instead. */

/* machine-independent big-endian 32-bit integer */
struct UINT32BE
{
	uint8_t bytes[4];
};

static inline uint32_t get_UINT32BE(UINT32BE word)
{
	return (word.bytes[0] << 24) | (word.bytes[1] << 16) | (word.bytes[2] << 8) | word.bytes[3];
}

#ifdef UNUSED_FUNCTION
static inline void set_UINT32BE(UINT32BE *word, uint32_t data)
{
	word->bytes[0] = (data >> 24) & 0xff;
	word->bytes[1] = (data >> 16) & 0xff;
	word->bytes[2] = (data >> 8) & 0xff;
	word->bytes[3] = data & 0xff;
}
#endif

/* disk image header */
struct disk_image_header
{
	UINT32BE cylinders;         /* number of cylinders on hard disk (big-endian) */
	UINT32BE heads;             /* number of heads on hard disk (big-endian) */
	UINT32BE sectors_per_track; /* number of sectors per track on hard disk (big-endian) */
	UINT32BE bytes_per_sector;  /* number of bytes of data per sector on hard disk (big-endian) */
};

enum
{
	header_len = sizeof(disk_image_header)
};


/* masks for individual bits controller registers */
enum
{
	w0_offline          = 0x8000,
	w0_not_ready        = 0x4000,
	w0_write_protect    = 0x2000,
	w0_unsafe           = 0x1000,
	w0_end_of_cylinder  = 0x0800,
	w0_seek_incomplete  = 0x0400,
	/*w0_offset_active  = 0x0200,*/
	w0_pack_change      = 0x0100,

	w0_attn_lines       = 0x00f0,
	w0_attn_mask        = 0x000f,

	w1_extended_command = 0xc000,
	/*w1_strobe_early       = 0x2000,
	w1_strobe_late      = 0x1000,*/
	w1_transfer_inhibit = 0x0800,
	w1_command          = 0x0700,
	w1_offset           = 0x0080,
	w1_offset_forward   = 0x0040,
	w1_head_address     = 0x003f,

	w6_unit0_sel        = 0x0800,
	w6_unit1_sel        = 0x0400,
	w6_unit2_sel        = 0x0200,
	w6_unit3_sel        = 0x0100,

	w7_idle             = 0x8000,
	w7_complete         = 0x4000,
	w7_error            = 0x2000,
	w7_int_enable       = 0x1000,
	/*w7_lock_out           = 0x0800,*/
	w7_retry            = 0x0400,
	w7_ecc              = 0x0200,
	w7_abnormal_completion  = 0x0100,
	w7_memory_error         = 0x0080,
	w7_data_error           = 0x0040,
	w7_tiline_timeout_err   = 0x0020,
	w7_header_err           = 0x0010,
	w7_rate_err             = 0x0008,
	w7_command_time_out_err = 0x0004,
	w7_search_err           = 0x0002,
	w7_unit_err             = 0x0001
};

/* masks for computer-controlled bit in each controller register  */
static const uint16_t w_mask[8] =
{
	0x000f,     /* Controllers should prevent overwriting of w0 status bits, and I know
	            that some controllers do so. */
	0xffff,
	0xffff,
	0xffff,
	0xffff,
	0xffff,
	0xffff,
	0xf7ff      /* Don't overwrite reserved bits */
};


int ti990_hdc_device::get_id_from_device( device_t *device )
{
	int id = -1;

	if ( ! strcmp( ":harddisk1", device->tag() ) )
	{
		id = 0;
	}
	else if ( ! strcmp( ":harddisk2", device->tag() ) )
	{
		id = 1;
	}
	else if ( ! strcmp( ":harddisk3", device->tag() ) )
	{
		id = 2;
	}
	else if ( ! strcmp( ":harddisk4", device->tag() ) )
	{
		id = 3;
	}
	assert( id >= 0 );

	return id;
}


/*
    Initialize hard disk unit and open a hard disk image
*/
DEVICE_IMAGE_LOAD_MEMBER( ti990_hdc_device, ti990_hd )
{
	int id = get_id_from_device( &image.device() );
	hd_unit_t *d;
	hard_disk_file  *hd_file;

	d = &m_d[id];
	d->img = &image;

	hd_file = dynamic_cast<harddisk_image_device *>(&image)->get_hard_disk_file();

	if ( hd_file )
	{
		const hard_disk_info *standard_header;

		d->format = format_mame;
		d->hd_handle = hd_file;

		/* use standard hard disk image header. */
		standard_header = hard_disk_get_info(d->hd_handle);

		d->cylinders = standard_header->cylinders;
		d->heads = standard_header->heads;
		d->sectors_per_track = standard_header->sectors;
		d->bytes_per_sector = standard_header->sectorbytes;
	}
	else
	{
		/* older, custom format */
		disk_image_header custom_header;
		int bytes_read;

		/* set file descriptor */
		d->format = format_old;
		d->hd_handle = nullptr;

		/* use custom image header. */
		/* to convert old header-less images to this format, insert a 16-byte
		header as follow: 00 00 03 8f  00 00 00 05  00 00 00 21  00 00 01 00 */
		d->img->fseek(0, SEEK_SET);
		bytes_read = d->img->fread(&custom_header, sizeof(custom_header));
		if (bytes_read != sizeof(custom_header))
		{
			d->format = format_mame;    /* don't care */
			d->wp = 1;
			d->unsafe = 1;
			return image_init_result::FAIL;
		}

		d->cylinders = get_UINT32BE(custom_header.cylinders);
		d->heads = get_UINT32BE(custom_header.heads);
		d->sectors_per_track = get_UINT32BE(custom_header.sectors_per_track);
		d->bytes_per_sector = get_UINT32BE(custom_header.bytes_per_sector);
	}

	if (d->bytes_per_sector > MAX_SECTOR_SIZE)
	{
		d->format = format_mame;
		d->hd_handle = nullptr;
		d->wp = 1;
		d->unsafe = 1;
		return image_init_result::FAIL;
	}

	/* tell whether the image is writable */
	d->wp = image.is_readonly();

	d->unsafe = 1;
	/* set attention line */
	m_w[0] |= (0x80 >> id);

	return image_init_result::PASS;
}

/*
    close a hard disk image
*/
DEVICE_IMAGE_UNLOAD_MEMBER( ti990_hdc_device, ti990_hd )
{
	int id = get_id_from_device(&image.device());
	hd_unit_t *d;

	d = &m_d[id];

	d->format = format_mame;    /* don't care */
	d->hd_handle = nullptr;
	d->wp = 1;
	d->unsafe = 1;

	/* clear attention line */
	m_w[0] &= ~ (0x80 >> id);
}

/*
    Return true if a HD image has been loaded
*/
int ti990_hdc_device::is_unit_loaded(int unit)
{
	int reply = 0;

	switch (m_d[unit].format)
	{
	case format_mame:
		reply = (m_d[unit].hd_handle != nullptr);
		break;

	case format_old:
		reply = (m_d[unit].img->exists() ? 1 : 0);
		break;
	}

	return reply;
}

/*
    Parse the disk select lines, and return the corresponding tape unit.
    (-1 if none)
*/
int ti990_hdc_device::cur_disk_unit()
{
	int reply;


	if (m_w[6] & w6_unit0_sel)
		reply = 0;
	else if (m_w[6] & w6_unit1_sel)
		reply = 1;
	else if (m_w[6] & w6_unit2_sel)
		reply = 2;
	else if (m_w[6] & w6_unit3_sel)
		reply = 3;
	else
		reply = -1;

	if (reply >= MAX_DISK_UNIT)
		reply = -1;

	return reply;
}

/*
    Update interrupt state
*/
void ti990_hdc_device::update_interrupt()
{
	if (!m_interrupt_callback.isnull())
		m_interrupt_callback((m_w[7] & w7_idle)
									&& (((m_w[7] & w7_int_enable) && (m_w[7] & (w7_complete | w7_error)))
										|| ((m_w[0] & (m_w[0] >> 4)) & w0_attn_mask)));
}

/*
    Check that a sector address is valid.

    Terminate current command and return non-zero if the address is invalid.
*/
int ti990_hdc_device::check_sector_address(int unit, unsigned int cylinder, unsigned int head, unsigned int sector)
{
	if ((cylinder > m_d[unit].cylinders) || (head > m_d[unit].heads) || (sector > m_d[unit].sectors_per_track))
	{   /* invalid address */
		if (cylinder > m_d[unit].cylinders)
		{
			m_w[0] |= w0_seek_incomplete;
			m_w[7] |= w7_idle | w7_error | w7_unit_err;
		}
		else if (head > m_d[unit].heads)
		{
			m_w[0] |= w0_end_of_cylinder;
			m_w[7] |= w7_idle | w7_error | w7_unit_err;
		}
		else if (sector > m_d[unit].sectors_per_track)
			m_w[7] |= w7_idle | w7_error | w7_command_time_out_err;
		update_interrupt();
		return 1;
	}

	return 0;
}

/*
    Seek to sector whose address is given
*/
int ti990_hdc_device::sector_to_lba(int unit, unsigned int cylinder, unsigned int head, unsigned int sector, unsigned int *lba)
{
	if (check_sector_address(unit, cylinder, head, sector))
		return 1;

	* lba = (cylinder*m_d[unit].heads + head)*m_d[unit].sectors_per_track + sector;

	return 0;
}

/*
    Read one given sector
*/
int ti990_hdc_device::read_sector(int unit, unsigned int lba, void *buffer, unsigned int bytes_to_read)
{
	unsigned long byte_position;
	unsigned int bytes_read;

	switch (m_d[unit].format)
	{
	case format_mame:
		bytes_read = m_d[unit].bytes_per_sector * hard_disk_read(m_d[unit].hd_handle, lba, buffer);
		if (bytes_read > bytes_to_read)
			bytes_read = bytes_to_read;
		break;

	case format_old:
		byte_position = lba*m_d[unit].bytes_per_sector + header_len;
		m_d[unit].img->fseek(byte_position, SEEK_SET);
		bytes_read = m_d[unit].img->fread(buffer, bytes_to_read);
		break;

	default:
		bytes_read = 0;
		break;
	}

	return bytes_read;
}

/*
    Write one given sector
*/
int ti990_hdc_device::write_sector(int unit, unsigned int lba, const void *buffer, unsigned int bytes_to_write)
{
	unsigned long byte_position;
	unsigned int bytes_written;

	switch (m_d[unit].format)
	{
	case format_mame:
		bytes_written = m_d[unit].bytes_per_sector * hard_disk_write(m_d[unit].hd_handle, lba, buffer);
		if (bytes_written > bytes_to_write)
			bytes_written = bytes_to_write;
		break;

	case format_old:
		byte_position = lba*m_d[unit].bytes_per_sector + header_len;
		m_d[unit].img->fseek(byte_position, SEEK_SET);
		bytes_written = m_d[unit].img->fwrite(buffer, bytes_to_write);
		break;

	default:
		bytes_written = 0;
		break;
	}

	return bytes_written;
}

/*
    Handle the store registers command: read the drive geometry.
*/
void ti990_hdc_device::store_registers()
{
	int dma_address;
	int byte_count;

	uint16_t buffer[3];
	int i, real_word_count;

	int dsk_sel = cur_disk_unit();


	if (dsk_sel == -1)
	{
		/* No idea what to report... */
		m_w[7] |= w7_idle | w7_error | w7_abnormal_completion;
		update_interrupt();
		return;
	}
	else if (! is_unit_loaded(dsk_sel))
	{   /* offline */
		m_w[0] |= w0_offline | w0_not_ready;
		m_w[7] |= w7_idle | w7_error | w7_unit_err;
		update_interrupt();
		return;
	}

	m_d[dsk_sel].unsafe = 0;      /* I think */

	dma_address = ((((int) m_w[6]) << 16) | m_w[5]) & 0x1ffffe;
	byte_count = m_w[4] & 0xfffe;

	/* formatted words per track */
	buffer[0] = (m_d[dsk_sel].sectors_per_track*m_d[dsk_sel].bytes_per_sector) >> 1;
	/* MSByte: sectors per track; LSByte: bytes of overhead per sector */
	buffer[1] = (m_d[dsk_sel].sectors_per_track << 8) | 0;
	/* bits 0-4: heads; bits 5-15: cylinders */
	buffer[2] = (m_d[dsk_sel].heads << 11) | m_d[dsk_sel].cylinders;

	real_word_count = byte_count >> 1;
	if (real_word_count > 3)
		real_word_count = 3;

	/* DMA */
	if (! (m_w[1] & w1_transfer_inhibit))
		for (i=0; i<real_word_count; i++)
		{
			m_memory_space->write_word(dma_address, buffer[i]);
			dma_address = (dma_address + 2) & 0x1ffffe;
		}

	m_w[7] |= w7_idle | w7_complete;
	update_interrupt();
}

/*
    Handle the write format command: format a complete track on disk.

    The emulation just clears the track data in the disk image.
*/
void ti990_hdc_device::write_format()
{
	unsigned int cylinder, head, sector;
	unsigned int lba;

	uint8_t buffer[MAX_SECTOR_SIZE];
	int bytes_written;

	int dsk_sel = cur_disk_unit();


	if (dsk_sel == -1)
	{
		/* No idea what to report... */
		m_w[7] |= w7_idle | w7_error | w7_abnormal_completion;
		update_interrupt();
		return;
	}
	else if (! is_unit_loaded(dsk_sel))
	{   /* offline */
		m_w[0] |= w0_offline | w0_not_ready;
		m_w[7] |= w7_idle | w7_error | w7_unit_err;
		update_interrupt();
		return;
	}
	else if (m_d[dsk_sel].unsafe)
	{   /* disk in unsafe condition */
		m_w[0] |= w0_unsafe | w0_pack_change;
		m_w[7] |= w7_idle | w7_error | w7_unit_err;
		update_interrupt();
		return;
	}
	else if (m_d[dsk_sel].wp)
	{   /* disk write-protected */
		m_w[0] |= w0_write_protect;
		m_w[7] |= w7_idle | w7_error | w7_unit_err;
		update_interrupt();
		return;
	}

	cylinder = m_w[3];
	head = m_w[1] & w1_head_address;

	if (sector_to_lba(dsk_sel, cylinder, head, 0, &lba))
		return;

	memset(buffer, 0, m_d[dsk_sel].bytes_per_sector);

	for (sector=0; sector<m_d[dsk_sel].sectors_per_track; sector++)
	{
		bytes_written = write_sector(dsk_sel, lba, buffer, m_d[dsk_sel].bytes_per_sector);

		if (bytes_written != m_d[dsk_sel].bytes_per_sector)
		{
			m_w[0] |= w0_offline | w0_not_ready;
			m_w[7] |= w7_idle | w7_error | w7_unit_err;
			update_interrupt();
			return;
		}

		lba++;
	}

	m_w[7] |= w7_idle | w7_complete;
	update_interrupt();
}

/*
    Handle the read data command: read a variable number of sectors from disk.
*/
void ti990_hdc_device::read_data()
{
	int dma_address;
	int byte_count;

	unsigned int cylinder, head, sector;
	unsigned int lba;

	uint8_t buffer[MAX_SECTOR_SIZE];
	int bytes_to_read;
	int bytes_read;
	int i;

	int dsk_sel = cur_disk_unit();


	if (dsk_sel == -1)
	{
		/* No idea what to report... */
		m_w[7] |= w7_idle | w7_error | w7_abnormal_completion;
		update_interrupt();
		return;
	}
	else if (! is_unit_loaded(dsk_sel))
	{   /* offline */
		m_w[0] |= w0_offline | w0_not_ready;
		m_w[7] |= w7_idle | w7_error | w7_unit_err;
		update_interrupt();
		return;
	}
	else if (m_d[dsk_sel].unsafe)
	{   /* disk in unsafe condition */
		m_w[0] |= w0_unsafe | w0_pack_change;
		m_w[7] |= w7_idle | w7_error | w7_unit_err;
		update_interrupt();
		return;
	}

	dma_address = ((((int) m_w[6]) << 16) | m_w[5]) & 0x1ffffe;
	byte_count = m_w[4] & 0xfffe;

	cylinder = m_w[3];
	head = m_w[1] & w1_head_address;
	sector = m_w[2] & 0xff;

	if (sector_to_lba(dsk_sel, cylinder, head, sector, &lba))
		return;

	while (byte_count)
	{   /* read data sector per sector */
		if (cylinder > m_d[dsk_sel].cylinders)
		{
			m_w[0] |= w0_seek_incomplete;
			m_w[7] |= w7_idle | w7_error | w7_unit_err;
			update_interrupt();
			return;
		}

		bytes_to_read = (byte_count < m_d[dsk_sel].bytes_per_sector) ? byte_count : m_d[dsk_sel].bytes_per_sector;
		bytes_read = read_sector(dsk_sel, lba, buffer, bytes_to_read);

		if (bytes_read != bytes_to_read)
		{   /* behave as if the controller could not found the sector ID mark */
			m_w[7] |= w7_idle | w7_error | w7_command_time_out_err;
			update_interrupt();
			return;
		}

		/* DMA */
		if (! (m_w[1] & w1_transfer_inhibit))
			for (i=0; i<bytes_read; i+=2)
			{
				m_memory_space->write_word(dma_address, (((int) buffer[i]) << 8) | buffer[i+1]);
				dma_address = (dma_address + 2) & 0x1ffffe;
			}

		byte_count -= bytes_read;

		/* update sector address to point to next sector */
		lba++;
		sector++;
		if (sector == m_d[dsk_sel].sectors_per_track)
		{
			sector = 0;
			head++;
			if (head == m_d[dsk_sel].heads)
			{
				head = 0;
				cylinder++;
			}
		}
	}

	m_w[7] |= w7_idle | w7_complete;
	update_interrupt();
}

/*
    Handle the write data command: write a variable number of sectors from disk.
*/
void ti990_hdc_device::write_data()
{
	int dma_address;
	int byte_count;

	unsigned int cylinder, head, sector;
	unsigned int lba;

	uint8_t buffer[MAX_SECTOR_SIZE];
	uint16_t word;
	int bytes_written;
	int i;

	int dsk_sel = cur_disk_unit();


	if (dsk_sel == -1)
	{
		/* No idea what to report... */
		m_w[7] |= w7_idle | w7_error | w7_abnormal_completion;
		update_interrupt();
		return;
	}
	else if (! is_unit_loaded(dsk_sel))
	{   /* offline */
		m_w[0] |= w0_offline | w0_not_ready;
		m_w[7] |= w7_idle | w7_error | w7_unit_err;
		update_interrupt();
		return;
	}
	else if (m_d[dsk_sel].unsafe)
	{   /* disk in unsafe condition */
		m_w[0] |= w0_unsafe | w0_pack_change;
		m_w[7] |= w7_idle | w7_error | w7_unit_err;
		update_interrupt();
		return;
	}
	else if (m_d[dsk_sel].wp)
	{   /* disk write-protected */
		m_w[0] |= w0_write_protect;
		m_w[7] |= w7_idle | w7_error | w7_unit_err;
		update_interrupt();
		return;
	}

	dma_address = ((((int) m_w[6]) << 16) | m_w[5]) & 0x1ffffe;
	byte_count = m_w[4] & 0xfffe;

	cylinder = m_w[3];
	head = m_w[1] & w1_head_address;
	sector = m_w[2] & 0xff;

	if (sector_to_lba(dsk_sel, cylinder, head, sector, &lba))
		return;

	while (byte_count > 0)
	{   /* write data sector per sector */
		if (cylinder > m_d[dsk_sel].cylinders)
		{
			m_w[0] |= w0_seek_incomplete;
			m_w[7] |= w7_idle | w7_error | w7_unit_err;
			update_interrupt();
			return;
		}

		/* DMA */
		for (i=0; (i<byte_count) && (i<m_d[dsk_sel].bytes_per_sector); i+=2)
		{
			word = m_memory_space->read_word(dma_address);
			buffer[i] = word >> 8;
			buffer[i+1] = word & 0xff;

			dma_address = (dma_address + 2) & 0x1ffffe;
		}
		/* fill with 0s if we did not reach sector end */
		for (; i<m_d[dsk_sel].bytes_per_sector; i+=2)
			buffer[i] = buffer[i+1] = 0;

		bytes_written = write_sector(dsk_sel, lba, buffer, m_d[dsk_sel].bytes_per_sector);

		if (bytes_written != m_d[dsk_sel].bytes_per_sector)
		{
			m_w[0] |= w0_offline | w0_not_ready;
			m_w[7] |= w7_idle | w7_error | w7_unit_err;
			update_interrupt();
			return;
		}

		byte_count -= bytes_written;

		/* update sector address to point to next sector */
		lba++;
		sector++;
		if (sector == m_d[dsk_sel].sectors_per_track)
		{
			sector = 0;
			head++;
			if (head == m_d[dsk_sel].heads)
			{
				head = 0;
				cylinder++;
			}
		}
	}

	m_w[7] |= w7_idle | w7_complete;
	update_interrupt();
}

/*
    Handle the unformatted read command: read drive geometry information.
*/
void ti990_hdc_device::unformatted_read()
{
	int dma_address;
	int byte_count;

	unsigned int cylinder, head, sector;

	uint16_t buffer[3];
	int i, real_word_count;

	int dsk_sel = cur_disk_unit();


	if (dsk_sel == -1)
	{
		/* No idea what to report... */
		m_w[7] |= w7_idle | w7_error | w7_abnormal_completion;
		update_interrupt();
		return;
	}
	else if (! is_unit_loaded(dsk_sel))
	{   /* offline */
		m_w[0] |= w0_offline | w0_not_ready;
		m_w[7] |= w7_idle | w7_error | w7_unit_err;
		update_interrupt();
		return;
	}
	else if (m_d[dsk_sel].unsafe)
	{   /* disk in unsafe condition */
		m_w[0] |= w0_unsafe | w0_pack_change;
		m_w[7] |= w7_idle | w7_error | w7_unit_err;
		update_interrupt();
		return;
	}

	dma_address = ((((int) m_w[6]) << 16) | m_w[5]) & 0x1ffffe;
	byte_count = m_w[4] & 0xfffe;

	cylinder = m_w[3];
	head = m_w[1] & w1_head_address;
	sector = m_w[2] & 0xff;

	if (check_sector_address(dsk_sel, cylinder, head, sector))
		return;

	dma_address = ((((int) m_w[6]) << 16) | m_w[5]) & 0x1ffffe;
	byte_count = m_w[4] & 0xfffe;

	/* bits 0-4: head address; bits 5-15: cylinder address */
	buffer[0] = (head << 11) | cylinder;
	/* MSByte: sectors per record (1); LSByte: sector address */
	buffer[1] = (1 << 8) | sector;
	/* formatted words per record */
	buffer[2] = m_d[dsk_sel].bytes_per_sector >> 1;

	real_word_count = byte_count >> 1;
	if (real_word_count > 3)
		real_word_count = 3;

	/* DMA */
	if (! (m_w[1] & w1_transfer_inhibit))
		for (i=0; i<real_word_count; i++)
		{
			m_memory_space->write_word(dma_address, buffer[i]);
			dma_address = (dma_address + 2) & 0x1ffffe;
		}

	m_w[7] |= w7_idle | w7_complete;
	update_interrupt();
}

/*
    Handle the restore command: return to track 0.
*/
void ti990_hdc_device::restore()
{
	int dsk_sel = cur_disk_unit();


	if (dsk_sel == -1)
	{
		/* No idea what to report... */
		m_w[7] |= w7_idle | w7_error | w7_abnormal_completion;
		update_interrupt();
		return;
	}
	else if (! is_unit_loaded(dsk_sel))
	{   /* offline */
		m_w[0] |= w0_offline | w0_not_ready;
		m_w[7] |= w7_idle | w7_error | w7_unit_err;
		update_interrupt();
		return;
	}

	m_d[dsk_sel].unsafe = 0;      /* I think */

	/*if (seek_to_sector(dsk_sel, 0, 0, 0))
	    return;*/

	m_w[7] |= w7_idle | w7_complete;
	update_interrupt();
}

/*
    Parse command code and execute the command.
*/
void ti990_hdc_device::execute_command()
{
	/* hack */
	m_w[0] &= 0xff;

	if (m_w[1] & w1_extended_command)
		logerror("extended commands not supported\n");

	switch (/*((m_w[1] & w1_extended_command) >> 11) |*/ ((m_w[1] & w1_command) >> 8))
	{
	case 0x00: //0b000:
		/* store registers */
		logerror("store registers\n");
		store_registers();
		break;
	case 0x01: //0b001:
		/* write format */
		logerror("write format\n");
		write_format();
		break;
	case 0x02: //0b010:
		/* read data */
		logerror("read data\n");
		read_data();
		break;
	case 0x03: //0b011:
		/* write data */
		logerror("write data\n");
		write_data();
		break;
	case 0x04: //0b100:
		/* unformatted read */
		logerror("unformatted read\n");
		unformatted_read();
		break;
	case 0x05: //0b101:
		/* unformatted write */
		logerror("unformatted write\n");
		/* ... */
		m_w[7] |= w7_idle | w7_error | w7_abnormal_completion;
		update_interrupt();
		break;
	case 0x06: //0b110:
		/* seek */
		logerror("seek\n");
		/* This command can (almost) safely be ignored */
		m_w[7] |= w7_idle | w7_complete;
		update_interrupt();
		break;
	case 0x07: //0b111:
		/* restore */
		logerror("restore\n");
		restore();
		break;
	}
}

/*
    Read one register in TPCS space
*/
READ16_MEMBER(ti990_hdc_device::read)
{
	if (offset < 8)
		return m_w[offset];
	else
		return 0;
}

/*
    Write one register in TPCS space.  Execute command if w7_idle is cleared.
*/
WRITE16_MEMBER(ti990_hdc_device::write)
{
	if (offset < 8)
	{
		/* write protect if a command is in progress */
		if (m_w[7] & w7_idle)
		{
			uint16_t old_data = m_w[offset];

			/* Only write writable bits AND honor byte accesses (ha!) */
			m_w[offset] = (m_w[offset] & ((~w_mask[offset]) | mem_mask)) | (data & w_mask[offset] & ~mem_mask);

			if ((offset == 0) || (offset == 7))
				update_interrupt();

			if ((offset == 7) && (old_data & w7_idle) && ! (data & w7_idle))
			{   /* idle has been cleared: start command execution */
				execute_command();
			}
		}
	}
}


DEFINE_DEVICE_TYPE(TI990_HDC, ti990_hdc_device, "ti990_hdc", "Generic TI-990 Hard Disk Controller")

ti990_hdc_device::ti990_hdc_device(const machine_config &mconfig, const char *tag, device_t *owner, uint32_t clock)
	: device_t(mconfig, TI990_HDC, tag, owner, clock)
	, m_memory_space(*this, finder_base::DUMMY_TAG, -1)
	, m_interrupt_callback(*this)
{
}

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

void ti990_hdc_device::device_start()
{
	int i;

	/* initialize harddisk information */
	/* attention lines will be set by DEVICE_IMAGE_LOD */
	for (i=0; i<MAX_DISK_UNIT; i++)
	{
		m_d[i].format = format_mame;
		m_d[i].hd_handle = nullptr;
		m_d[i].wp = 1;
		m_d[i].unsafe = 1;
	}
	memset(m_w, 0, sizeof(m_w));
	m_w[7] = w7_idle;

	/* get references to harddisk devices */
	m_d[0].img = dynamic_cast<device_image_interface *>(subdevice("harddisk1"));
	m_d[1].img = dynamic_cast<device_image_interface *>(subdevice("harddisk2"));
	m_d[2].img = dynamic_cast<device_image_interface *>(subdevice("harddisk3"));
	m_d[3].img = dynamic_cast<device_image_interface *>(subdevice("harddisk4"));

	m_interrupt_callback.resolve_safe();

	update_interrupt();
}


//-------------------------------------------------
//  device_add_mconfig - add device configuration
//-------------------------------------------------

MACHINE_CONFIG_START(ti990_hdc_device::device_add_mconfig)
	MCFG_HARDDISK_ADD( "harddisk1" )
	MCFG_HARDDISK_LOAD(ti990_hdc_device, ti990_hd)
	MCFG_HARDDISK_UNLOAD(ti990_hdc_device, ti990_hd)
	MCFG_HARDDISK_ADD( "harddisk2" )
	MCFG_HARDDISK_LOAD(ti990_hdc_device, ti990_hd)
	MCFG_HARDDISK_UNLOAD(ti990_hdc_device, ti990_hd)
	MCFG_HARDDISK_ADD( "harddisk3" )
	MCFG_HARDDISK_LOAD(ti990_hdc_device, ti990_hd)
	MCFG_HARDDISK_UNLOAD(ti990_hdc_device, ti990_hd)
	MCFG_HARDDISK_ADD( "harddisk4" )
	MCFG_HARDDISK_LOAD(ti990_hdc_device, ti990_hd)
	MCFG_HARDDISK_UNLOAD(ti990_hdc_device, ti990_hd)
MACHINE_CONFIG_END