CCITT CRC

Discuss using Kryoflux to image floppies
Post Reply
JonAbbott
Posts: 1736
Joined: Thu Apr 11, 2013 12:13 pm
Location: Essex

CCITT CRC

Post by JonAbbott » Fri May 17, 2013 6:10 am

To calculate the table:

Code: Select all

DIM table% 256 * 4

FOR loop% = 0 TO 255
 w% = loop% << 8
 FOR loop2% = 0 TO 7
   w% = (w% << 1) EOR ((w%>>15 AND 1) * &1021)
 NEXT
 table%!(loop% * 4) = w% AND &FFFF
NEXT
To update the CRC:

Code: Select all

crc = (crc<<8 EOR table%!((crc>>8 EOR value)<<2)) AND &FFFF
Where "value" is the next FM/MFM word.


The CRC is reset to FFFF at the following points:

FM:
  • On hitting an ID marker (F57E)
    On hitting a sync marker (5554)
MFM:
  • On hitting a sync marker (4489)
To check if a CRC is valid, simply EOR the running CRC with the data or ID CRC, if the result isn't zero there's a CRC error.

If you want to look at the source, the APD decoder is in: adffs.file_types.APD

danielj
Posts: 48
Joined: Sun May 12, 2013 9:08 pm

Re: CCITT CRC

Post by danielj » Fri May 17, 2013 10:09 am

Thanks Jon - always lovely to see something in BBC BASIC :D - that's much more translatable into Java than the C equivalents which I'd been perusing (I'd done one but wasn't convinced it was working). Are the clock bits fed in to the CRC register too?

d.

JonAbbott
Posts: 1736
Joined: Thu Apr 11, 2013 12:13 pm
Location: Essex

Re: CCITT CRC

Post by JonAbbott » Fri May 17, 2013 1:20 pm

Yes, pass the raw FM/MFM into it as 16bit words.

danielj
Posts: 48
Joined: Sun May 12, 2013 9:08 pm

Re: CCITT CRC

Post by danielj » Sun May 19, 2013 10:41 pm

Hmm..

Just having spent a few hours bodging this in and out of java and a few iterations, it seems that using the raw fm/mfm with clock bits intact in words or bytes (it was easier to deal with things a byte at a time in the end) doesn't work, the checksums are based the final bytes as read, clock stripped...

That is right, isn't it? Just working with an FM bbc disc at the moment (it's what I have to hand, and the FM decoder was already written) and that's the only way I can get the checksums to play ball...
d.

JonAbbott
Posts: 1736
Joined: Thu Apr 11, 2013 12:13 pm
Location: Essex

Re: CCITT CRC

Post by JonAbbott » Mon May 20, 2013 7:21 pm

Sorry, it would help if I had checked the ADFFS source code before telling you compete rubbish! The CRC is a word and you feed a byte into it having stripped the timing bits out.

If you search the source for MFM_crc, you'll see all the points it's updated / reset. Where it gets interesting is sector-in-sector, I had to guess that as it's not clear how its dealt with by a 1772. I got it producing the correct results on all the APD's I could get hold of, took a lot of trial on error though.

Post Reply