src/lib/formats/dfi_dsk.c
| r14999 | r15000 | |
| 31 | 31 | |
| 32 | 32 | ****************************************************************************/ |
| 33 | 33 | |
| 34 | /* TODO: |
| 35 | * Support auto-identification heuristics for determining disk image speed, |
| 36 | capture clock rate, and number of multireads per image. |
| 37 | * Handle 0xFF bytes properly |
| 38 | * Correctly note exact index timing. |
| 39 | */ |
| 40 | |
| 34 | 41 | #include "emu.h" |
| 35 | 42 | #include "dfi_dsk.h" |
| 36 | 43 | #include <zlib.h> |
| 37 | | #define NUMBER_OF_MULTIREADS 1 |
| 44 | #define NUMBER_OF_MULTIREADS 3 |
| 45 | // clockspeed: 100mhz = 4, 50mhz = 2, 25mhz = 1 |
| 46 | #define CLOCKSPEED 4 |
| 47 | // threshholds for brickwall windowing |
| 48 | #define MIN_CLOCKS 65 |
| 49 | #define MAX_CLOCKS 260 |
| 50 | #define MIN_THRESH (MIN_CLOCKS*CLOCKSPEED) |
| 51 | #define MAX_THRESH (MAX_CLOCKS*CLOCKSPEED) |
| 52 | // constants to help guess clockrate and rpm |
| 53 | // constant is 25mhz / 6 revolutions per second (360rpm) = 4166667 +- 2.5% |
| 54 | #define REV25_MIN 4062500 |
| 55 | #define REV25_MAX 4270833 |
| 56 | // define the following to show a histogram per track |
| 57 | #undef TRACK_HISTOGRAM |
| 38 | 58 | |
| 39 | 59 | dfi_format::dfi_format() : floppy_image_format_t() |
| 40 | 60 | { |
| r14999 | r15000 | |
| 64 | 84 | { |
| 65 | 85 | char sign[4]; |
| 66 | 86 | io_generic_read(io, sign, 0, 4); |
| 87 | if (memcmp(sign, "DFER", 4)) |
| 88 | fatalerror("Old type Discferret image detected; the mess Discferret decoder will not handle this properly, bailing out!\n"); |
| 67 | 89 | return memcmp(sign, "DFE2", 4) ? 0 : 100; |
| 68 | 90 | } |
| 69 | 91 | |
| r14999 | r15000 | |
| 73 | 95 | int pos = 4; |
| 74 | 96 | UINT8 *data = 0; |
| 75 | 97 | int data_size = 0; |
| 76 | | |
| 98 | int onerev_time = 0; // time for one revolution, used to guess clock and rpm for DFE2 files |
| 99 | unsigned long clock_rate = 100000000; // sample clock rate in megahertz |
| 100 | int rpm = 360; // drive rpm |
| 77 | 101 | while(pos < size) { |
| 78 | 102 | UINT8 h[10]; |
| 79 | 103 | io_generic_read(io, h, pos, 10); |
| r14999 | r15000 | |
| 99 | 123 | pos += tsize+10; |
| 100 | 124 | tsize--; // Drop the extra 0x00 at the end |
| 101 | 125 | |
| 102 | | int index_time = 0; |
| 103 | | int index_count = 0; |
| 104 | | int total_time = 0; |
| 126 | int index_time = 0; // what point the last index happened |
| 127 | int index_count = 0; // number of index pulses per track |
| 128 | int index_polarity = 0; // current polarity of index |
| 129 | int total_time = 0; // total sampled time per track |
| 105 | 130 | for(int i=0; i<tsize; i++) { |
| 106 | 131 | UINT8 v = data[i]; |
| 107 | | if((v & 0x7f) == 0x7f) |
| 132 | //if((v & 0x7f) == 0x7f) |
| 133 | if (v == 0x7f) |
| 108 | 134 | total_time += 0x7f; |
| 109 | 135 | else { |
| 110 | | total_time += v & 0x7f; |
| 111 | | if((v & 0x80) && (index_count<NUMBER_OF_MULTIREADS)) { |
| 112 | | index_time = total_time;///NUMBER_OF_MULTIREADS; |
| 136 | if((v & 0x80)==0) total_time += v & 0x7f; |
| 137 | if(v & 0x80) { |
| 138 | index_time = total_time; |
| 139 | if (onerev_time == 0) onerev_time = total_time; |
| 140 | //index_polarity ^= 1; |
| 141 | //index_count =+ index_polarity; |
| 113 | 142 | index_count++; |
| 114 | 143 | } |
| 115 | 144 | } |
| r14999 | r15000 | |
| 117 | 146 | |
| 118 | 147 | if(!track && !head) |
| 119 | 148 | fprintf(stderr, "%02d:%d tt=%10d it=%10d\n", track, head, total_time, index_time); |
| 149 | if(!track && !head) { |
| 150 | fprintf(stderr, "index_count: %d, onerev_time: %d\n", index_count, onerev_time); |
| 151 | if ((onerev_time > REV25_MIN) && (onerev_time < REV25_MAX)) { |
| 152 | fprintf(stderr, "Guess: speed: 360rpm, clock 25MHz\n"); |
| 153 | clock_rate = 25000000; rpm = 360; |
| 154 | } else if ((onerev_time > REV25_MIN*1.2) && (onerev_time < REV25_MAX*1.2)) { |
| 155 | fprintf(stderr, "Guess: speed: 300rpm, clock 25MHz\n"); |
| 156 | clock_rate = 25000000; rpm = 300; |
| 157 | } else if ((onerev_time > REV25_MIN*2) && (onerev_time < REV25_MAX*2)) { |
| 158 | fprintf(stderr, "Guess: speed: 360rpm, clock 50MHz\n"); |
| 159 | clock_rate = 50000000; rpm = 360; |
| 160 | } else if ((onerev_time > (REV25_MIN*2)*1.2) && (onerev_time < (REV25_MAX*2)*1.2)) { |
| 161 | fprintf(stderr, "Guess: speed: 300rpm, clock 50MHz\n"); |
| 162 | clock_rate = 50000000; rpm = 300; |
| 163 | } else if ((onerev_time > REV25_MIN*4) && (onerev_time < REV25_MAX*4)) { |
| 164 | fprintf(stderr, "Guess: speed: 360rpm, clock 100MHz\n"); |
| 165 | clock_rate = 100000000; rpm = 360; |
| 166 | } else if ((onerev_time > (REV25_MIN*4)*1.2) && (onerev_time < (REV25_MAX*4)*1.2)) { |
| 167 | fprintf(stderr, "Guess: speed: 300rpm, clock 100MHz\n"); |
| 168 | clock_rate = 100000000; rpm = 300; |
| 169 | } else |
| 170 | fprintf(stderr, "WARNING: Cannot Guess Speed! Assuming 360rpm, 100Mhz clock!\n"); |
| 171 | fprintf(stderr,"Actual rpm based on index: %f\n", ((double)clock_rate/(double)onerev_time)*60); |
| 172 | } |
| 120 | 173 | |
| 121 | 174 | if(!index_time) |
| 122 | 175 | index_time = total_time; |
| r14999 | r15000 | |
| 124 | 177 | image->set_track_size(track, head, tsize); |
| 125 | 178 | |
| 126 | 179 | int cur_time = 0; |
| 180 | int prev_time = 0; |
| 181 | #ifdef TRACK_HISTOGRAM |
| 182 | // histogram |
| 183 | int time_buckets[4096]; |
| 184 | for (int i = 0; i < 4096; i++) |
| 185 | time_buckets[i] = 0; |
| 186 | #endif |
| 127 | 187 | index_count = 0; |
| 128 | | int index_polarity = 0; |
| 188 | index_polarity = 0; |
| 129 | 189 | UINT32 mg = floppy_image::MG_A; |
| 130 | 190 | UINT32 *buf = image->get_buffer(track, head); |
| 131 | 191 | int tpos = 0; |
| 132 | 192 | buf[tpos++] = mg; |
| 133 | 193 | for(int i=0; i<tsize; i++) { |
| 134 | 194 | UINT8 v = data[i]; |
| 135 | | if((v & 0x7f) == 0x7f) // 0x7F or 0xFF: no transition, but a carry |
| 136 | | cur_time += 0x7f; |
| 195 | //if((v & 0x7f) == 0x7f) {// 0x7F or 0xFF: no transition, but a carry |
| 196 | if (v == 0x7f) { |
| 197 | cur_time += 0x7f; // should this be done if v&80 is also set? |
| 198 | if(v & 0x80) { // 0xFF an index, note the index (TODO: actually do this!) and do not add number |
| 199 | index_polarity ^= 1; |
| 200 | index_count += index_polarity; |
| 201 | if (index_count == NUMBER_OF_MULTIREADS) break; |
| 202 | } |
| 203 | } |
| 137 | 204 | else if((v & 0x80) == 0) { // 0x00-0x7E: not an index or carry, add the number and store transition |
| 138 | 205 | cur_time += v & 0x7f; |
| 139 | | mg = mg == floppy_image::MG_A ? floppy_image::MG_B : floppy_image::MG_A; |
| 140 | | buf[tpos++] = mg | UINT32(200000000ULL*cur_time/index_time); |
| 206 | int trans_time = cur_time - prev_time; |
| 207 | #ifdef TRACK_HISTOGRAM |
| 208 | time_buckets[(trans_time<4096)?trans_time:4095]++; |
| 209 | #endif |
| 210 | // cur_time and onerev_time need to be converted to something standard, so we'll stick with 300rpm at the standard 200mhz rate that mfi uses internally |
| 211 | // TODO for 4/22/2012: ACTUALLY DO THIS RESCALING STEP |
| 212 | // filter out spurious crap |
| 213 | //if (trans_time <= MIN_THRESH) fprintf(stderr, "DFI: Throwing out short transition of length %d\n", trans_time); |
| 214 | //if ((prev_time == 0) || ((trans_time > MIN_THRESH))) { |
| 215 | if ((prev_time == 0) || ((trans_time > MIN_THRESH) && (trans_time <= MAX_THRESH))) { |
| 216 | mg = mg == floppy_image::MG_A ? floppy_image::MG_B : floppy_image::MG_A; |
| 217 | buf[tpos++] = mg | UINT32((200000000ULL*cur_time)/index_time); |
| 218 | prev_time = cur_time; |
| 219 | } |
| 220 | if (trans_time > MAX_THRESH) { // we probably missed a transition |
| 221 | mg = mg == floppy_image::MG_A ? floppy_image::MG_B : floppy_image::MG_A; |
| 222 | //fprintf(stderr,"missed transition, total time for transition is %d\n",trans_time); |
| 223 | buf[tpos++] = mg | UINT32((200000000ULL*(cur_time-(trans_time/2)))/index_time); // generate imaginary transition at half period |
| 224 | buf[tpos++] = mg | UINT32(200000000ULL*cur_time/index_time); // generate transition now |
| 225 | prev_time = cur_time; |
| 226 | } |
| 141 | 227 | } else if(v & 0x80) { // 0x80-0xFF an index, note the index (TODO: actually do this!) and do not add number |
| 142 | 228 | index_polarity ^= 1; |
| 143 | 229 | index_count += index_polarity; |
| 144 | 230 | if (index_count == NUMBER_OF_MULTIREADS) break; |
| 145 | 231 | } |
| 146 | 232 | } |
| 233 | #ifdef TRACK_HISTOGRAM |
| 234 | if (((track%2)==0)&&(head==0)) { |
| 235 | for (int i = 0; i < 4096; i++) { |
| 236 | fprintf(stderr,"%4d:%4d ", i, time_buckets[i]); |
| 237 | if (((i+1)%10)==0) fprintf(stderr,"\n"); |
| 238 | } |
| 239 | } |
| 240 | #endif |
| 241 | index_count = 0; |
| 147 | 242 | image->set_track_size(track, head, tpos); |
| 148 | 243 | } |
| 149 | 244 | |