Previous 15994 Revisions Next

r15107 Monday 7th May, 2012 at 06:13:46 UTC by Jonathan Gevaryahu
ap2_dsk.c: Preliminary, working RWTS18 decoding support; Decodes disks and shows ascii text; not sure if sectors are in the correct order yet. [Lord Nightmare, Balrog, Roland Gustafsson]
[src/lib/formats]ap2_dsk.c

src/lib/formats/ap2_dsk.c
r15106r15107
12291229            //~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
12301230            generate_bitstream_from_track(track, head, 200000000/((3004*nsect*6)/2), buf, ts, image); // 3104 needs tweaking
12311231//fprintf(stderr,"done.\n");
1232            int oldpos = 0; // DEBUG
12321233            int pos = 0;
12331234            int wrap = 0;
12341235            int hb = 0;
12351236            for(;;) {
12361237                  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)
12381239                        hb = 1;
12391240                  else if(hb == 1 && v == 0xd5)
12401241                        hb = 2;
r15106r15107
12441245                        hb = 0;
12451246
12461247                  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
12481252                        for(int i=0; i<7; i++)
12491253                              h[i] = gb(buf, ts, pos, wrap);
12501254                        UINT8 tr = gcr6bw_tb[h[0]];
12511255                        UINT8 se = gcr6bw_tb[h[1]];
12521256                        UINT8 chk = gcr6bw_tb[h[2]];
12531257                        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);
12551260                        // sanity check
12561261                        if (tr == track/2 && se < nsect) {
12571262                        visualgrid[se][track/2] |= ADDRFOUND;
r15106r15107
12641269                              //int opos = pos;
12651270                              //int owrap = wrap;
12661271                              // 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                             
12711272                              visualgrid[se][track/2] |= DATAFOUND;
1272                              //UINT8 *dest = sectdata+(768)*se;
1273                              UINT8 *dest = sectdata+(256)*se;
12731274                              UINT8 data[0x401];
12741275                              UINT32 dpost = 0;
12751276                              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
12771279                              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");
12821292                              }
1283                              // read the checksum byte
1293                              // read the checksum byte (checksum is calced by xoring all data together)
12841294                              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++) {
12871297                                    dpost <<= 8;
12881298                                    dpost |= gb(buf, ts, pos, wrap);
12891299                              }
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.
13021313                              // if it is set we don't want to overwrite a guaranteed good read with a bad one
13031314                              // if past read had a bad checksum or bad postamble...
13041315#ifndef USE_OLD_BEST_SECTOR_PRIORITY
r15106r15107
13081319                                 // if the current read is bad and the current postamble is good and the previous read had neither good, write it in
13091320                                 // if the current read isn't good and neither is the postamble but nothing better
13101321                                 // 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))
13151326                                 ) {
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                                       }
13211354                              }
13221355#else
13231356                              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];
13261367                                       *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                                       }
13281383                              }
13291384#endif
13301385                              // 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                           }
13421391                        }
13431392                        hb = 0;
13441393                  }
r15106r15107
13571406      int total_good = 0;
13581407      for (int j = 0; j < 35; j++) {
13591408         printf("T%2d: ",j);
1360         for (int i = 0; i < 16; i++) {
1409         for (int i = 0; i < (j==0?16:6); i++) {
13611410            if (visualgrid[i][j] == NOTFOUND) printf("-NF- ");
13621411            else {
13631412            if (visualgrid[i][j] & ADDRFOUND) printf("a"); else printf(" ");

Previous 15994 Revisions Next


© 1998-2018 The MESS Team