[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: help decoding address fields



jonnosan <jonnosan@gmail.com> wrote:

> Howdy,
> 
> I am looking to extend my dsk parsing code to deal with .nib images.
> I'm having some problems trying to understand how the nibble encoding
> works.
> 
> I've been readin both Beneath Apple DOS and Understanding the Apple ]
> [ but still a bit confused.
> 
> I kind find the address field in a .nib file, and I can decode the Vol
> and track components, but I'm getting bogus values for the sector
> fields. As far as I can tell, this is a normal dos 3.3 image, i.e. no
> copy-protection to deal with.
> 
> This is how I'm interpreting the following address field:
> 
> D5 AA 96 FF FE AA AA AA AA FF FE DE AA
> 
> D5 AA 96  <- start of address field
> FF FE         <- vol # in 4-4 encoding = F E (254)
> AA AA         <- track # in 4-4 encoding=0 0 (00)
> AA AA         <- sector # in 4-4 encoding=0 0 (00)
> FF FE   <- checksum (ignoring for now)
> DE AA   <- end of address field
> 
> 
> So that is Track 0, sector 0.
> 
> But then I see this :
> D5 AA 96 FF FE AA AA AF AF FA FB DE AA
> 
> Which I decode as:
> D5 AA 96  <- start of address field
> FF FE         <- vol # in 4-4 encoding = F E (254)
> AA AA         <- track # in 4-4 encoding=0 0 (00)
> AF AF         <- sector # in 4-4 encoding=3 3 (51)
> FF FE   <- checksum (ignoring for now)
> DE AA   <- end of address field
> 
> i.e. this seems to be claiming it is Track 0, sector 51.
> 
> 
> Have I got a messed up .nib image, or am I missing something obvious?

Congratulations. Your message piqued my curiosity enough to venture into
my basement, open up a few cardboard boxes and pull out some key Apple
II reference manuals for the first time in about five years.

The detail you have missed (and which I almost remembered - it was
bugging me) is that you have the bits in the wrong order.

You also made a mistake: the AF AF bytes should be read as 5 5 when you
do the 4-and-4 decode, not as 3 3.

Within each two byte field in the address field, the bits are arranged
as follows:

Byte 0: 1 d7 1 d5 1 d3 1 d1
Byte 1: 1 d6 1 d4 1 d2 1 d0

To combine them into a single byte, you take the first byte, shift it
left once (with the low order bit being 1 rather than 0) then do a
logical AND with the second byte.

The assembly code for this would be something like:

LDA first_byte
SEC
ROL
AND second_byte

Your second address field is actually the header for sector 15. I also
remembered that DOS 3.3 tracks start with sector 0 then 15.

The AF AF bytes are actually:

 0 . 0 . 1 . 1 .  (rotated left)
 . 0 . 0 . 1 . 1

Combined:

 0 0 0 0 1 1 1 1

which is 0F hex, or 15.

Another way to think of it is that the bits alternate between the two
bytes.

If you look at the data bytes in their hex form (AF AF) then ignore the
AA AA constant bits, you are left with 05 05. Double the first byte (to
get 0A) and add them (to get 0F).

-- 
David Empson
dempson@actrix.gen.nz