src/lib/formats/ap2_dsk.c
| r15106 | r15107 | |
| 1229 | 1229 | //~332 samples per cell, times 3+8+3 (14) for address mark, 24 for sync, 3+343+3 (349) for data mark, 24 for sync is around 743, near 776 expected |
| 1230 | 1230 | generate_bitstream_from_track(track, head, 200000000/((3004*nsect*6)/2), buf, ts, image); // 3104 needs tweaking |
| 1231 | 1231 | //fprintf(stderr,"done.\n"); |
| 1232 | int oldpos = 0; // DEBUG |
| 1232 | 1233 | int pos = 0; |
| 1233 | 1234 | int wrap = 0; |
| 1234 | 1235 | int hb = 0; |
| 1235 | 1236 | for(;;) { |
| 1236 | 1237 | UINT8 v = gb(buf, ts, pos, wrap); |
| 1237 | | if(v == 0xff) |
| 1238 | if((v == 0xff) || (v == 0x9a)) // note 0x9a varies per title! this is an LFSR? generated value intended to throw off copiers, and only appears after the track splice (before sector 5) |
| 1238 | 1239 | hb = 1; |
| 1239 | 1240 | else if(hb == 1 && v == 0xd5) |
| 1240 | 1241 | hb = 2; |
| r15106 | r15107 | |
| 1244 | 1245 | hb = 0; |
| 1245 | 1246 | |
| 1246 | 1247 | if(hb == 3) { |
| 1247 | | UINT8 h[11]; |
| 1248 | printf("AM at offset: %d, relative: %d\n", pos, pos-oldpos); |
| 1249 | oldpos=pos; |
| 1250 | UINT8 h[7]; |
| 1251 | // grab exactly 7 bytes: should be Track, Sector, Checksum, AA, FF and FF and the Br0derbund Title ID |
| 1248 | 1252 | for(int i=0; i<7; i++) |
| 1249 | 1253 | h[i] = gb(buf, ts, pos, wrap); |
| 1250 | 1254 | UINT8 tr = gcr6bw_tb[h[0]]; |
| 1251 | 1255 | UINT8 se = gcr6bw_tb[h[1]]; |
| 1252 | 1256 | UINT8 chk = gcr6bw_tb[h[2]]; |
| 1253 | 1257 | UINT32 post = (h[3]<<16)|(h[4]<<8)|h[5]; |
| 1254 | | printf("RWTS18 AM:\t Track %d, Sector %2d, Checksum %02X: %s, Postamble %03X: %s\n", tr, se, chk, (chk ^ tr ^ se)==0?"OK":"BAD", post, post==0xAAFFFF?"OK":"BAD"); |
| 1258 | UINT8 bbundid = h[6]; |
| 1259 | printf("RWTS18 AM:\t Track %d, Sector %2d, Checksum %02X: %s, Postamble %03X: %s, BBUNDID %02x\n", tr, se, chk, (chk ^ tr ^ se)==0?"OK":"BAD", post, post==0xAAFFFF?"OK":"BAD", bbundid); |
| 1255 | 1260 | // sanity check |
| 1256 | 1261 | if (tr == track/2 && se < nsect) { |
| 1257 | 1262 | visualgrid[se][track/2] |= ADDRFOUND; |
| r15106 | r15107 | |
| 1264 | 1269 | //int opos = pos; |
| 1265 | 1270 | //int owrap = wrap; |
| 1266 | 1271 | // RWTS18 doesn't have a true data mark, its part of the address header |
| 1267 | | // The next byte however is unique per title |
| 1268 | | v = gb(buf, ts, pos, wrap); |
| 1269 | | printf("Data mark header per-title byte = 0x%2x\n", v); |
| 1270 | | |
| 1271 | 1272 | visualgrid[se][track/2] |= DATAFOUND; |
| 1272 | | //UINT8 *dest = sectdata+(768)*se; |
| 1273 | UINT8 *dest = sectdata+(256)*se; |
| 1273 | 1274 | UINT8 data[0x401]; |
| 1274 | 1275 | UINT32 dpost = 0; |
| 1275 | 1276 | UINT8 c = 0; |
| 1276 | | // first read in the ENTIRE sector and decode to 6bit form |
| 1277 | //dest = sectdata+(768)*se; |
| 1278 | // now read in the sector and decode to 6bit form |
| 1277 | 1279 | for(int i=0; i<0x400; i++) { |
| 1278 | | data[i] = gcr6bw_tb[gb(buf, ts, pos, wrap)] ^ c; |
| 1279 | | c = data[i]; |
| 1280 | | printf("%02x ", c); |
| 1281 | | if (((i&0xf)+1)==0x10) printf("\n"); |
| 1280 | data[i] = gcr6bw_tb[gb(buf, ts, pos, wrap)] ;//^ c; |
| 1281 | c ^= data[i]; |
| 1282 | /*if (((i&0x3)+1)==0x04) { |
| 1283 | printf("%c", ((((data[i-3]&0x30)<<2)|((data[i-2]&0x3F)>>0))&0x3F)+0x40); |
| 1284 | printf("%c", ((((data[i-3]&0x0C)<<4)|((data[i-1]&0x3F)>>0))&0x3F)+0x40); |
| 1285 | printf("%c", ((((data[i-3]&0x03)<<6)|((data[i-0]&0x3F)>>0))&0x3F)+0x40); |
| 1286 | *dest++ = ((data[i-3]&0x30)<<2)|((data[i-2]&0x3F)>>0); |
| 1287 | *dest++ = ((data[i-3]&0x0C)<<4)|((data[i-1]&0x3F)>>0); |
| 1288 | *dest++ = ((data[i-3]&0x03)<<6)|((data[i-0]&0x3F)>>0); |
| 1289 | }*/ |
| 1290 | // printf("%02x ", data[i]); |
| 1291 | // if (((i&0xf)+1)==0x10) printf("\n"); |
| 1282 | 1292 | } |
| 1283 | | // read the checksum byte |
| 1293 | // read the checksum byte (checksum is calced by xoring all data together) |
| 1284 | 1294 | data[0x400] = gcr6bw_tb[gb(buf,ts,pos,wrap)]; |
| 1285 | | // now read the postamble byte |
| 1286 | | for(int i=0; i<1; i++) { |
| 1295 | // now read the postamble bytes |
| 1296 | for(int i=0; i<4; i++) { |
| 1287 | 1297 | dpost <<= 8; |
| 1288 | 1298 | dpost |= gb(buf, ts, pos, wrap); |
| 1289 | 1299 | } |
| 1290 | | printf("Data Postamble was 0x%02x\n", dpost); |
| 1291 | | /* |
| 1292 | | // next combine in the upper 2 bits of each byte |
| 1293 | | UINT8 bit_swap[4] = { 0, 2, 1, 3 }; |
| 1294 | | for(int i=0; i<0x56; i++) |
| 1295 | | data[i+0x056] = data[i+0x056]<<2 | bit_swap[data[i]&3]; |
| 1296 | | for(int i=0; i<0x56; i++) |
| 1297 | | data[i+0x0ac] = data[i+0x0ac]<<2 | bit_swap[(data[i]>>2)&3]; |
| 1298 | | for(int i=0; i<0x54; i++) |
| 1299 | | data[i+0x102] = data[i+0x102]<<2 | bit_swap[(data[i]>>4)&3]; |
| 1300 | | // now decode it into 256 bytes |
| 1301 | | // but only write it if the bitfield of the track shows datagood is NOT set. |
| 1300 | |
| 1301 | /*if (se == 0) // dump some debug data to help find the lfsr before sector 5 |
| 1302 | { |
| 1303 | printf("Data Postamble was 0x%08x\n", dpost); |
| 1304 | for(int i=0; i<0x400; i++) { |
| 1305 | data[i] = gcr6bw_tb[gb(buf, ts, pos, wrap)] ;//^ c; |
| 1306 | c ^= data[i]; |
| 1307 | printf("%02x ", data[i]); |
| 1308 | if (((i&0xf)+1)==0x10) printf("\n"); |
| 1309 | } |
| 1310 | }*/ |
| 1311 | |
| 1312 | // only write it if the bitfield of the track shows datagood is NOT set. |
| 1302 | 1313 | // if it is set we don't want to overwrite a guaranteed good read with a bad one |
| 1303 | 1314 | // if past read had a bad checksum or bad postamble... |
| 1304 | 1315 | #ifndef USE_OLD_BEST_SECTOR_PRIORITY |
| r15106 | r15107 | |
| 1308 | 1319 | // if the current read is bad and the current postamble is good and the previous read had neither good, write it in |
| 1309 | 1320 | // if the current read isn't good and neither is the postamble but nothing better |
| 1310 | 1321 | // has been written before, write it anyway. |
| 1311 | | if ( ((data[0x156] == c) && (dpost&0xFFFF00)==0xDEAA00) || |
| 1312 | | (((data[0x156] == c) && (dpost&0xFFFF00)!=0xDEAA00) && ((visualgrid[se][track/2]&DATAPOST)==0)) || |
| 1313 | | (((data[0x156] != c) && (dpost&0xFFFF00)==0xDEAA00) && (((visualgrid[se][track/2]&DATAGOOD)==0)&&(visualgrid[se][track/2]&DATAPOST)==0)) || |
| 1314 | | (((data[0x156] != c) && (dpost&0xFFFF00)!=0xDEAA00) && (((visualgrid[se][track/2]&DATAGOOD)==0)&&(visualgrid[se][track/2]&DATAPOST)==0)) |
| 1322 | if ( ((data[0x400] == c) && (dpost&0xFF000000)==0xD4000000) || |
| 1323 | (((data[0x400] == c) && (dpost&0xFF000000)!=0xD4000000) && ((visualgrid[se][track/2]&DATAPOST)==0)) || |
| 1324 | (((data[0x400] != c) && (dpost&0xFF000000)==0xD4000000) && (((visualgrid[se][track/2]&DATAGOOD)==0)&&(visualgrid[se][track/2]&DATAPOST)==0)) || |
| 1325 | (((data[0x400] != c) && (dpost&0xFF000000)!=0xD4000000) && (((visualgrid[se][track/2]&DATAGOOD)==0)&&(visualgrid[se][track/2]&DATAPOST)==0)) |
| 1315 | 1326 | ) { |
| 1316 | | for(int i=0x56; i<0x156; i++) { |
| 1317 | | UINT8 dv = data[i]; |
| 1318 | | *dest++ = dv; |
| 1319 | | } |
| 1320 | | } |
| 1327 | // next combine adjacent data bytes to form the 3 constituent sectors |
| 1328 | // format is 0x00AaBbCc 0x00aaaaaa 0x00bbbbbb 0x00cccccc 0x00AaBbCc ... etc |
| 1329 | // aa is sector 0, bb is sector 6, cc is sector 12 |
| 1330 | // first sector: |
| 1331 | dest = sectdata+(256)*se; |
| 1332 | for(int i=0; i<0x100; i++) { |
| 1333 | data[(4*i)+1] |= (data[4*i]&0x30)<<2; |
| 1334 | //printf("%c", (data[4*i]&0x3F)+0x40); |
| 1335 | //if (((i&0xf)+1)==0x10) printf("\n"); |
| 1336 | UINT8 dv = data[(4*i)+1]; |
| 1337 | *dest++ = dv; |
| 1338 | } |
| 1339 | // second sector: |
| 1340 | dest = sectdata+(256)*(se+6); |
| 1341 | for(int i=0; i<0x100; i++) { |
| 1342 | data[(4*i)+2] |= (data[4*i]&0x0c)<<4; |
| 1343 | UINT8 dv = data[(4*i)+2]; |
| 1344 | *dest++ = dv; |
| 1345 | } |
| 1346 | // third sector: |
| 1347 | dest = sectdata+(256)*(se+12); |
| 1348 | for(int i=0; i<0x100; i++) { |
| 1349 | data[(4*i)+3] |= (data[4*i]&0x03)<<6; |
| 1350 | UINT8 dv = data[(4*i)+3]; |
| 1351 | *dest++ = dv; |
| 1352 | } |
| 1353 | } |
| 1321 | 1354 | } |
| 1322 | 1355 | #else |
| 1323 | 1356 | if ((visualgrid[se][track/2]&DATAGOOD)==0) { |
| 1324 | | for(int i=0x56; i<0x156; i++) { |
| 1325 | | UINT8 dv = data[i]; |
| 1357 | // next combine adjacent data bytes to form the 3 constituent sectors |
| 1358 | // format is 0x00AaBbCc 0x00aaaaaa 0x00bbbbbb 0x00cccccc 0x00AaBbCc ... etc |
| 1359 | // aa is sector 0, bb is sector 6, cc is sector 12 |
| 1360 | // first sector: |
| 1361 | dest = sectdata+(256)*se; |
| 1362 | for(int i=0; i<0x100; i++) { |
| 1363 | data[(4*i)+1] |= (data[4*i]&0x30)<<2; |
| 1364 | //printf("%c", (data[4*i]&0x3F)+0x40); |
| 1365 | //if (((i&0xf)+1)==0x10) printf("\n"); |
| 1366 | UINT8 dv = data[(4*i)+1]; |
| 1326 | 1367 | *dest++ = dv; |
| 1327 | | } |
| 1368 | } |
| 1369 | // second sector: |
| 1370 | dest = sectdata+(256)*(se+6); |
| 1371 | for(int i=0; i<0x100; i++) { |
| 1372 | data[(4*i)+2] |= (data[4*i]&0x0c)<<4; |
| 1373 | UINT8 dv = data[(4*i)+2]; |
| 1374 | *dest++ = dv; |
| 1375 | } |
| 1376 | // third sector: |
| 1377 | dest = sectdata+(256)*(se+12); |
| 1378 | for(int i=0; i<0x100; i++) { |
| 1379 | data[(4*i)+3] |= (data[4*i]&0x03)<<6; |
| 1380 | UINT8 dv = data[(4*i)+3]; |
| 1381 | *dest++ = dv; |
| 1382 | } |
| 1328 | 1383 | } |
| 1329 | 1384 | #endif |
| 1330 | 1385 | // do some checking |
| 1331 | | if ((data[0x156] != c) || (dpost&0xFFFF00)!=0xDEAA00) |
| 1332 | | fprintf(stderr,"Data Mark:\tChecksum xpctd %d found %d: %s, Postamble %03X: %s\n", data[0x156], c, (data[0x156]==c)?"OK":"BAD", dpost, (dpost&0xFFFF00)==0xDEAA00?"OK":"BAD"); |
| 1333 | | if (data[0x156] == c) visualgrid[se][track/2] |= DATAGOOD; |
| 1334 | | if ((dpost&0xFFFF00)==0xDEAA00) visualgrid[se][track/2] |= DATAPOST; |
| 1335 | | } else if ((hb == 4)&&(dosver == 1)) { |
| 1336 | | fprintf(stderr,"ERROR: We don't handle dos sectors below 3.3 yet!\n"); |
| 1337 | | } else { |
| 1338 | | pos = opos; |
| 1339 | | wrap = owrap; |
| 1340 | | }*/ |
| 1341 | | } |
| 1386 | if ((data[0x400] != c) || (dpost&0xFF000000)!=0xD4000000) |
| 1387 | fprintf(stderr,"Data Mark:\tChecksum xpctd %d found %d: %s, Postamble %03X: %s\n", data[0x400], c, (data[0x400]==c)?"OK":"BAD", dpost, (dpost&0xFF000000)==0xD4000000?"OK":"BAD"); |
| 1388 | if (data[0x400] == c) visualgrid[se][track/2] |= DATAGOOD; |
| 1389 | if ((dpost&0xFF000000)==0xD4000000) visualgrid[se][track/2] |= DATAPOST; |
| 1390 | } |
| 1342 | 1391 | } |
| 1343 | 1392 | hb = 0; |
| 1344 | 1393 | } |
| r15106 | r15107 | |
| 1357 | 1406 | int total_good = 0; |
| 1358 | 1407 | for (int j = 0; j < 35; j++) { |
| 1359 | 1408 | printf("T%2d: ",j); |
| 1360 | | for (int i = 0; i < 16; i++) { |
| 1409 | for (int i = 0; i < (j==0?16:6); i++) { |
| 1361 | 1410 | if (visualgrid[i][j] == NOTFOUND) printf("-NF- "); |
| 1362 | 1411 | else { |
| 1363 | 1412 | if (visualgrid[i][j] & ADDRFOUND) printf("a"); else printf(" "); |