1 // license:BSD-3-Clause
2 // copyright-holders:R. Belmont, Olivier Galibert
3 /*********************************************************************
4 
5     formats/esq8_dsk.c
6 
7     Formats for 8-bit Ensoniq synthesizers and samplers
8 
9     Disk is PC MFM, 40 tracks, single (Mirage) or double (SQ-80) sided,
10     with 6 sectors per track.
11     Sectors 0-4 are 1024 bytes, sector 5 is 512 bytes
12 
13 *********************************************************************/
14 
15 #include <cassert>
16 
17 #include "flopimg.h"
18 #include "formats/esq8_dsk.h"
19 
20 const floppy_image_format_t::desc_e esq8img_format::esq_6_desc[] = {
21 	{ MFM, 0x4e, 80 },
22 	{ MFM, 0x00, 12 },
23 	{ RAW, 0x5224, 3 },
24 	{ MFM, 0xfc, 1 },
25 	{ MFM, 0x4e, 50 },
26 	{ MFM, 0x00, 12 },
27 	{ SECTOR_LOOP_START, 0, 5 },
28 	{   CRC_CCITT_START, 1 },
29 	{     RAW, 0x4489, 3 },
30 	{     MFM, 0xfe, 1 },
31 	{     TRACK_ID },
32 	{     HEAD_ID },
33 	{     SECTOR_ID },
34 	{     SIZE_ID },
35 	{   CRC_END, 1 },
36 	{   CRC, 1 },
37 	{   MFM, 0x4e, 22 },
38 	{   MFM, 0x00, 12 },
39 	{   CRC_CCITT_START, 2 },
40 	{     RAW, 0x4489, 3 },
41 	{     MFM, 0xfb, 1 },
42 	{     SECTOR_DATA, -1 },
43 	{   CRC_END, 2 },
44 	{   CRC, 2 },
45 	{   MFM, 0x4e, 84 },
46 	{   MFM, 0x00, 12 },
47 	{ SECTOR_LOOP_END },
48 	{ MFM, 0x4e, 170 },
49 	{ END }
50 };
51 
esq8img_format()52 esq8img_format::esq8img_format()
53 {
54 }
55 
name() const56 const char *esq8img_format::name() const
57 {
58 	return "esq8";
59 }
60 
description() const61 const char *esq8img_format::description() const
62 {
63 	return "Ensoniq Mirage/SQ-80 floppy disk image";
64 }
65 
extensions() const66 const char *esq8img_format::extensions() const
67 {
68 	return "img";
69 }
70 
supports_save() const71 bool esq8img_format::supports_save() const
72 {
73 	return true;
74 }
75 
find_size(io_generic * io,int & track_count,int & head_count,int & sector_count)76 void esq8img_format::find_size(io_generic *io, int &track_count, int &head_count, int &sector_count)
77 {
78 	uint64_t size = io_generic_size(io);
79 	track_count = 80;
80 	head_count = 1;
81 	sector_count = 6;
82 
83 	if (size == 5632 * 80)
84 	{
85 		return;
86 	}
87 
88 	track_count = head_count = sector_count = 0;
89 }
90 
identify(io_generic * io,uint32_t form_factor)91 int esq8img_format::identify(io_generic *io, uint32_t form_factor)
92 {
93 	int track_count, head_count, sector_count;
94 	find_size(io, track_count, head_count, sector_count);
95 
96 	if(track_count)
97 		return 50;
98 	return 0;
99 }
100 
load(io_generic * io,uint32_t form_factor,floppy_image * image)101 bool esq8img_format::load(io_generic *io, uint32_t form_factor, floppy_image *image)
102 {
103 	int track_count, head_count, sector_count;
104 	find_size(io, track_count, head_count, sector_count);
105 
106 	uint8_t sectdata[(5*1024) + 512];
107 	desc_s sectors[6];
108 	for(int i=0; i<sector_count; i++) {
109 		if (i < 5)
110 		{
111 			sectors[i].data = sectdata + (1024*i);  // 5 1024 byte sectors
112 			sectors[i].size = 1024;
113 			sectors[i].sector_id = i;
114 		}
115 		else
116 		{
117 			sectors[i].data = sectdata + (5*1024);  // 1 512 byte sector
118 			sectors[i].size = 512;
119 			sectors[i].sector_id = i;
120 		}
121 	}
122 
123 	int track_size = (5*1024) + 512;
124 
125 	for(int track=0; track < track_count; track++)
126 	{
127 		for(int head=0; head < head_count; head++)
128 		{
129 			io_generic_read(io, sectdata, (track*head_count + head)*track_size, track_size);
130 			generate_track(esq_6_desc, track, head, sectors, sector_count, 109376, image);
131 		}
132 	}
133 
134 	image->set_variant(floppy_image::DSDD);
135 
136 	return true;
137 }
138 
save(io_generic * io,floppy_image * image)139 bool esq8img_format::save(io_generic *io, floppy_image *image)
140 {
141 	int track_count, head_count, sector_count;
142 	get_geometry_mfm_pc(image, 2000, track_count, head_count, sector_count);
143 
144 	if(track_count != 80)
145 		track_count = 80;
146 
147 	// Happens for a fully unformatted floppy
148 	if(!head_count)
149 		head_count = 1;
150 
151 	if(sector_count != 6)
152 		sector_count = 6;
153 
154 	uint8_t sectdata[10*512];
155 	int track_size = (5*1024) + 512;
156 
157 	for(int track=0; track < track_count; track++) {
158 		for(int head=0; head < head_count; head++) {
159 			get_track_data_mfm_pc(track, head, image, 2000, 512, sector_count, sectdata);
160 			io_generic_write(io, sectdata, (track*head_count + head)*track_size, track_size);
161 		}
162 	}
163 
164 	return true;
165 }
166 
167 const floppy_format_type FLOPPY_ESQ8IMG_FORMAT = &floppy_image_format_creator<esq8img_format>;
168