24-bit/16-bit divide routine for 8-bit processor

24-bit/16-bit divide routine for 8-bit processor

Post by Guy Berthiau » Fri, 22 Jun 2001 09:03:49



Hi All,
        I am using an NEC K/0S microcontroller to access a serial data
flash device.  Part of the spec calls for the serial data flash
interface to accept a 24-bit linear address.  In order to make this
work, I have to divide the 24-bit address by a 9-bit page size in
order to determine which page of the device to access.

        Can anyone point me to a function that might satisfy this
problem?  BTW, we are very limited on space, so it has to be done in
assembly language, and the K/0S family does not have a divide or
multiply instruction.

Thanks in advance for the help.
Guy

--

Guy Berthiaume

"With, without,
 And who'll deny,
 It's what the fighting's all about."
                          - Roger Waters

 
 
 

24-bit/16-bit divide routine for 8-bit processor

Post by Jonathan Kirwa » Fri, 22 Jun 2001 09:10:39




>    I am using an NEC K/0S microcontroller to access a serial data
>flash device.  Part of the spec calls for the serial data flash
>interface to accept a 24-bit linear address.  In order to make this
>work, I have to divide the 24-bit address by a 9-bit page size in
>order to determine which page of the device to access.

>    Can anyone point me to a function that might satisfy this
>problem?  BTW, we are very limited on space, so it has to be done in
>assembly language, and the K/0S family does not have a divide or
>multiply instruction.

I may be misunderstanding your needs, but is what you are asking as
simple as it sounds?  If you have a 24-bit address space that should
be seen as a series of 9-bit addressed pages which are non-overlapping
and adjacent and start at 0, then it should be as simple as a masking
operation -- the page number would be the upper (24-9) or 15 bits and
the page address would be the lower 9 bits.  Just use an AND and a
SHIFT operation.  Even on a PIC this would be fast and short.

I must be missing something.

Jon

 
 
 

24-bit/16-bit divide routine for 8-bit processor

Post by Guy Berthiau » Fri, 22 Jun 2001 09:17:09


Actually, the page size is 264 bytes, so it takes 9 bits to hold the
value.  I am limiting the driver to work only on the devices that have
that page size, so the routine could take advantage of that.

Guy

On Thu, 21 Jun 2001 00:10:39 GMT, Jonathan Kirwan


>I may be misunderstanding your needs, but is what you are asking as
>simple as it sounds?  If you have a 24-bit address space that should
>be seen as a series of 9-bit addressed pages which are non-overlapping
>and adjacent and start at 0, then it should be as simple as a masking
>operation -- the page number would be the upper (24-9) or 15 bits and
>the page address would be the lower 9 bits.  Just use an AND and a
>SHIFT operation.  Even on a PIC this would be fast and short.

>I must be missing something.

>Jon

--

Guy Berthiaume

"With, without,
 And who'll deny,
 It's what the fighting's all about."
                          - Roger Waters

 
 
 

24-bit/16-bit divide routine for 8-bit processor

Post by Ralph Maso » Fri, 22 Jun 2001 13:52:47


If your so really limited how about only storing 256 bytes per page.

Then the page id = address >> 8

Doesn't get much faster on a 8 bit micro than ignoring 8 bits.


> Actually, the page size is 264 bytes, so it takes 9 bits to hold the
> value.  I am limiting the driver to work only on the devices that have
> that page size, so the routine could take advantage of that.

> Guy

> On Thu, 21 Jun 2001 00:10:39 GMT, Jonathan Kirwan

> >I may be misunderstanding your needs, but is what you are asking as
> >simple as it sounds?  If you have a 24-bit address space that should
> >be seen as a series of 9-bit addressed pages which are non-overlapping
> >and adjacent and start at 0, then it should be as simple as a masking
> >operation -- the page number would be the upper (24-9) or 15 bits and
> >the page address would be the lower 9 bits.  Just use an AND and a
> >SHIFT operation.  Even on a PIC this would be fast and short.

> >I must be missing something.

> >Jon

> --

> Guy Berthiaume

> "With, without,
>  And who'll deny,
>  It's what the fighting's all about."
>                           - Roger Waters

 
 
 

24-bit/16-bit divide routine for 8-bit processor

Post by Jonathan Kirwa » Fri, 22 Jun 2001 15:43:28




>Actually, the page size is 264 bytes, so it takes 9 bits to hold the
>value.  I am limiting the driver to work only on the devices that have
>that page size, so the routine could take advantage of that.

Ok.  That's the missing piece.

Well, you can just notice that 1/264 is:

      ----------
  .0000000011111

(That bar above the 10 binary digits indicates a repeating section.)

This means the effect is of adding your 24 bit value in the following
interpretations of the bits:

   15.9+14.10+13.11+12.12+11.13+5.19+4.20+3.21+2.22+1.23

(the rest of the repeating parts are too small to be of concern.)

Assume that "int" is 24 bits in size in the following (use assembly)
and that "short" is small, say 8 bits.  I'd imagine that the following
would work fine:

    short i;
    int a= v;
    for (i= 0; i < 5; ++i)
        a = v + (a >> 1);
    a >>= 4;
    for (i= 0; i < 5; ++i)
        a = v + (a >> 1);
    a >>= 9;

(assuming v is your 24-bit starting address value.)

Or at least that should get you very close.  I haven't tested it and I
suspect that there may be a detail I missed.  But it should get the
idea across and the resulting value in "a" should be very close to the
page number you are looking for.

Jon

 
 
 

24-bit/16-bit divide routine for 8-bit processor

Post by Paul Keinane » Fri, 22 Jun 2001 16:37:52




>Actually, the page size is 264 bytes, so it takes 9 bits to hold the
>value.  

Since 264 is even, you could shift both sides one position to the
right, so you get a 23:8 division, which is easier and faster to
implement than a 24:9 division on an 8 bit processor. If you also need
the remainder,  you have to use 16 bit arithmetic and shift it left
one position and add the least significant bit from the original
dividend.

Paul