Date : Wed, 25 Oct 2006 01:28:49
From : jgh@... (Jonathan Graham Harston)
Subject: Re: ADFS sector checksums
>Message-ID: <BAY124-W5606DA6AB57280F80FA612B20C0@...>
Steve O'Learey <navalenigma@...> wrote:
> Jonathon Graham Harston's site but I don't know if
^^^^^^^^
Who?
> http://mdfs.net/Docs/Comp/Disk/Format/ADFS
> States it's the sum of adding with carry the 255 bytes of data and then
> adding the sector number.
I wrote it that way because I could not get:
DEFFNsum:LOCAL sum%
FOR A%=0 TO 254:sum%=sum%+mem%?A%:IF sum%>255:sum%=(sum%+1)AND255
NEXT:=sum%
to give the correct checksum. It was always one off for sector
one, so I added one, and arbitarily defined it as adding the
sector number.
For instance:
Sector 0: 07 00 00 00 00 .... 00 00 00 00 20 xx
FNsum returns &27, actual checksum is &27
Sector 1: F9 FF 1F 00 00 .... 00 27 E6 00 03 xx
FNsum returns &2A, but actual checksum is &2B.
> For example the second sector (sector 01) of a blank new ADFS disk image
> created by BeebEm contains the following data (all values in hex)
>
> 100 F9 04 00 00 ..... 00 00 00 00 00 03 xx
F9+04 = FD
FD+lots of zeros = FD
FD+03 = 100
Hmm. I would then add-and-carry 1, giving 02, but you say the actual
checksum is 00.
John Kortink <kortink@...> wrote:
> A 'C' code fragment that calculates the checksum correctly is :
>
> int ix, sum, cflag;
>
> for (ix = 254, sum = 255, cflag = 0; ix >= 0; ix--)
> if ((cflag = ((sum += sector[ix] + cflag) > 255)) != 0)
> sum -= 256;
DEFFNsum(sector)
sum%5
cflag=0
FOR ix%4 TO 0 STEP -1
sum=sum+sector?ix+cflag
cflag=0:IF sum>255:cflag=1:sum=sum-256
NEXT
=sum
Well, that gives me &21 instead of &2B.
Rewriting it as:
DEFFNsum:LOCAL sum%:sum%%5
FOR A%%4 TO 0 STEP -1
sum%=sum%+mem%?A%:IF sum%>255:sum%=(sum%+1)AND255
NEXT:=sum%
still gives me &2A instead of &2B.
The actual code in ADFS (http://mdfs.net/Info/Comp/BBC/ADFS/v150.zip) is:
9070 88 . DEY Reset Y to &FF
9071 98 . TYA Initialise A with -1
9072 18 . CLC Clear carry
9073 79 FF C0 y.@ ADC &C0FF,Y Add sector 1 bytes from &FE to &00
9076 88 . DEY
9077 D0 FA P~ BNE &9073 Loop for all bytes
9079 60 ` RTS
--
J.G.Harston - jgh@... - mdfs.net/User/JGH
In 1939 $50 of groceries would fill three station wagons. Today I
can lift $50 of groceries with one hand. I must have got stonger.