Unaligned memory access
Harrison.
Posts: 484
What would be the best way to perform unaligned HUB memory access? I have a 1518 byte array that I would like to be able to access via LONG[noparse]/noparse, WORD[noparse]/noparse, and BYTE[noparse]/noparse at even or odd addresses. I am currently doing shifts and independent BYTE[noparse]/noparse accesses, but that is messy and probably slow.
One last question, would the addition (+) or the bitwise OR (|) operator be faster for joining bytes into a long? I am assuming the bitwise OR would be, but I have no idea how SPIN handles this internally.
For example:
EDIT: I found some information here about unaligned access: http://forums.parallax.com/forums/default.aspx?f=25&p=1&m=148004 . I wasn't able to discern any real answers though, other than the fact that there will be corruption if I had longs, words, and bytes I wanted to access all in one long. My issue is I am accessing an ethernet packet that has longs, words, and bytes inside of byte aligned packets.
Harrison
Post Edited (Harrison.) : 11/18/2007 9:30:45 AM GMT
One last question, would the addition (+) or the bitwise OR (|) operator be faster for joining bytes into a long? I am assuming the bitwise OR would be, but I have no idea how SPIN handles this internally.
For example:
VAR byte buffer[noparse][[/noparse]1518] PUB main | i i := LONG[noparse][[/noparse]@buffer + 3] ' may work if aligned? i := WORD[noparse][[/noparse]@buffer + 3] ' another maybe? i := BYTE[noparse][[/noparse]@buffer + 6] << 24 + BYTE[noparse][[/noparse]@buffer + 5] << 16 + BYTE[noparse][[/noparse]@buffer + 4] << 8 + BYTE[noparse][[/noparse]@buffer + 3] ' this is what I have to do now
EDIT: I found some information here about unaligned access: http://forums.parallax.com/forums/default.aspx?f=25&p=1&m=148004 . I wasn't able to discern any real answers though, other than the fact that there will be corruption if I had longs, words, and bytes I wanted to access all in one long. My issue is I am accessing an ethernet packet that has longs, words, and bytes inside of byte aligned packets.
Harrison
Post Edited (Harrison.) : 11/18/2007 9:30:45 AM GMT
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe
IC Layout Engineer
Parallax, Inc.
You have to understand that the MEMORY (not only COG, but HUB also) consists of 32 bit elements.
Accessing something from one part and something from the other is simply not possible.
You can of course write very fancy routines to just do that, i.e. reading here and there and putting it together.
I think there is not much difference with respect to timing how you do that.... All will be VERY SLOW
When you have a larger misaligned array, use Beau's trick (BYTEMOVE).
... and have the Cog run an assembler program which monitors hubAddress and when not negative fills in the return parameters as required.
Set hubAddress, wait for it to go negative and read whichever xxxxValue you want. You can wrap that all up in Spin functions if you like ...
That would probably make for a nice introductory program for venturing into assembler code.
Now, my original thought with the above method was that at least in one direction that the data would be overwritten and corrupted.· I sent an E-mail to Jeff Martin regarding this potential issue, and below is how he replied....
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe
IC Layout Engineer
Parallax, Inc.
Looks like I'll have to analyze the clock cycles taken for each type of operation to find out the best method. But for now, it seems to work well and I should have my new ethernet code ready for release soon.
Harrison