trunk/src/lib/formats/dmk_dsk.c
| r0 | r32586 | |
| 1 | // license:BSD-3-Clause |
| 2 | // copyright-holders:Wilbert Pol |
| 3 | /********************************************************************* |
| 4 | |
| 5 | formats/dmk_dsk.h |
| 6 | |
| 7 | DMK disk images |
| 8 | |
| 9 | TODO: |
| 10 | - Add write/format support. |
| 11 | - Add single density support. |
| 12 | - Check support on other drivers besides msx. |
| 13 | |
| 14 | *********************************************************************/ |
| 15 | |
| 16 | #include "emu.h" |
| 17 | #include "dmk_dsk.h" |
| 18 | |
| 19 | |
| 20 | dmk_format::dmk_format() |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | |
| 25 | const char *dmk_format::name() const |
| 26 | { |
| 27 | return "dmk"; |
| 28 | } |
| 29 | |
| 30 | |
| 31 | const char *dmk_format::description() const |
| 32 | { |
| 33 | return "DMK disk image"; |
| 34 | } |
| 35 | |
| 36 | |
| 37 | const char *dmk_format::extensions() const |
| 38 | { |
| 39 | return "dmk"; |
| 40 | } |
| 41 | |
| 42 | |
| 43 | int dmk_format::identify(io_generic *io, UINT32 form_factor) |
| 44 | { |
| 45 | const int header_size = 16; |
| 46 | UINT8 header[header_size]; |
| 47 | |
| 48 | UINT64 size = io_generic_size(io); |
| 49 | |
| 50 | io_generic_read(io, header, 0, header_size); |
| 51 | |
| 52 | int tracks = header[1]; |
| 53 | int track_size = ( header[3] << 8 ) | header[2]; |
| 54 | int heads = (header[4] & 0x10) ? 1 : 2; |
| 55 | |
| 56 | // The first header byte must be 00 or FF |
| 57 | if (header[0] != 0x00 && header[0] != 0xff) |
| 58 | { |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | // Bytes C-F must be zero |
| 63 | if (header[0x0c] != 0 || header[0xd] != 0 || header[0xe] != 0 || header[0xf] != 0) |
| 64 | { |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | // Check track size within limits |
| 69 | if (track_size < 0x80 || track_size > 0x3FFF ) |
| 70 | { |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | if (size == header_size + heads * tracks * track_size) |
| 75 | { |
| 76 | return 70; |
| 77 | } |
| 78 | |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | bool dmk_format::load(io_generic *io, UINT32 form_factor, floppy_image *image) |
| 84 | { |
| 85 | const int header_size = 16; |
| 86 | UINT8 header[header_size]; |
| 87 | |
| 88 | io_generic_read(io, header, 0, header_size); |
| 89 | |
| 90 | const int tracks = header[1]; |
| 91 | const int track_size = ( header[3] << 8 ) | header[2]; |
| 92 | const int heads = (header[4] & 0x10) ? 1 : 2; |
| 93 | const bool is_sd = (header[4] & 0x40) ? true : false; |
| 94 | const int raw_track_size = 2 * 8 * ( track_size - 0x80 ); |
| 95 | |
| 96 | if (is_sd) |
| 97 | { |
| 98 | if (heads == 2) |
| 99 | { |
| 100 | image->set_variant(floppy_image::DSSD); |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | image->set_variant(floppy_image::SSSD); |
| 105 | } |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | if (heads == 2) |
| 110 | { |
| 111 | image->set_variant(floppy_image::DSDD); |
| 112 | } |
| 113 | else |
| 114 | { |
| 115 | image->set_variant(floppy_image::SSDD); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | for (int track = 0; track < tracks; track++) |
| 120 | { |
| 121 | for (int head = 0; head < heads; head++) |
| 122 | { |
| 123 | dynamic_array<UINT8> track_data(track_size); |
| 124 | dynamic_array<UINT32> raw_track_data(raw_track_size); |
| 125 | int iam_location = -1; |
| 126 | int idam_location[64]; |
| 127 | int dam_location[64]; |
| 128 | int tpos = 0; |
| 129 | |
| 130 | // Read track |
| 131 | io_generic_read(io, track_data, header_size + ( heads * track + head ) * track_size, track_size); |
| 132 | |
| 133 | for (int i = 0; i < 64; i++) |
| 134 | { |
| 135 | idam_location[i] = -1; |
| 136 | dam_location[i] = -1; |
| 137 | } |
| 138 | |
| 139 | // Find IDAM locations |
| 140 | UINT16 track_header_offset = 0; |
| 141 | UINT16 track_offset = ( ( track_data[track_header_offset + 1] << 8 ) | track_data[track_header_offset] ) & 0x3fff; |
| 142 | track_header_offset += 2; |
| 143 | |
| 144 | while ( track_offset != 0 && track_offset >= 0x83 && track_offset < track_size && track_header_offset < 0x80 ) |
| 145 | { |
| 146 | // Assume 3 bytes before IDAM pointers are the start of IDAM indicators |
| 147 | idam_location[(track_header_offset/2) - 1] = track_offset - 3; |
| 148 | |
| 149 | // Scan for DAM location |
| 150 | for (int i = track_offset + 53; i > track_offset + 10; i--) |
| 151 | { |
| 152 | if ((track_data[i] == 0xfb || track_data[i] == 0xf8) && track_data[i-1] == 0xa1 && track_data[i-2] == 0xa1) |
| 153 | { |
| 154 | dam_location[(track_header_offset/2) - 1] = i - 3; |
| 155 | break; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | track_offset = ( ( track_data[track_header_offset + 1] << 8 ) | track_data[track_header_offset] ) & 0x3fff; |
| 160 | track_header_offset += 2; |
| 161 | }; |
| 162 | |
| 163 | // Find IAM location |
| 164 | for(int i = idam_location[0] - 1; i >= 3; i--) |
| 165 | { |
| 166 | // It's usually 3 bytes but several dumped tracks seem to contain only 2 bytes |
| 167 | if (track_data[i] == 0xfc && track_data[i-1] == 0xc2 && track_data[i-2] == 0xc2) |
| 168 | { |
| 169 | iam_location = i - 3; |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | int idam_index = 0; |
| 175 | int dam_index = 0; |
| 176 | for (int offset = 0x80; offset < track_size; offset++) |
| 177 | { |
| 178 | if (offset == iam_location) |
| 179 | { |
| 180 | // Write IAM |
| 181 | raw_w(raw_track_data, tpos, 16, 0x5224); |
| 182 | raw_w(raw_track_data, tpos, 16, 0x5224); |
| 183 | raw_w(raw_track_data, tpos, 16, 0x5224); |
| 184 | offset += 3; |
| 185 | } |
| 186 | |
| 187 | if (offset == idam_location[idam_index]) |
| 188 | { |
| 189 | raw_w(raw_track_data, tpos, 16, 0x4489); |
| 190 | raw_w(raw_track_data, tpos, 16, 0x4489); |
| 191 | raw_w(raw_track_data, tpos, 16, 0x4489); |
| 192 | idam_index += 1; |
| 193 | offset += 3; |
| 194 | } |
| 195 | |
| 196 | if (offset == dam_location[dam_index]) |
| 197 | { |
| 198 | raw_w(raw_track_data, tpos, 16, 0x4489); |
| 199 | raw_w(raw_track_data, tpos, 16, 0x4489); |
| 200 | raw_w(raw_track_data, tpos, 16, 0x4489); |
| 201 | dam_index += 1; |
| 202 | offset += 3; |
| 203 | } |
| 204 | |
| 205 | mfm_w(raw_track_data, tpos, 8, track_data[offset]); |
| 206 | } |
| 207 | |
| 208 | generate_track_from_levels(track, head, raw_track_data, raw_track_size, 0, image); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return true; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | bool dmk_format::save(io_generic *io, floppy_image *image) |
| 217 | { |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | |
| 222 | bool dmk_format::supports_save() const |
| 223 | { |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | |
| 228 | const floppy_format_type FLOPPY_DMK_FORMAT = &floppy_image_format_creator<dmk_format>; |
| 229 | |