Jump to content

Loading one of many arrays via a function?


sneakthief
 Share

Recommended Posts

First of all, I've carefully read through the "Multidimensional Array" thread. But I'm not sure if it applies to my situation....

I have to create a series of ~50 different one-dimensional arrays, each containing about 20 elements (instead of just a multidimensional array, due to the 255 array-element limit)

Based on the code below, what's the best way to load the contents of one of these arrays (into current_array) by passing an argument (array_number) to a function (LoadArray)?


const char array1[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
const char array2[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
...
const char array50[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]


//////////////////////////////////////////////////////////////
// This function loads a sequence based on the 
// argument that's passed to it
//////////////////////////////////////////////////////////////
void LoadArray(unsigned char array_number)

{

   unsigned char i;

   for (i = 0; i < 20; i++) {

     current_array[i] = array1[i];

    }

}

Or is there a totally different way of doing this?

thanks!

Link to comment
Share on other sites

i've read a dozen function pointer tutorials and i can't seem to figure out how to address a different array depending on the variable passed to the function.

...because i'm such a dumbass and don't know how to elegantly access them in C, i had to make a conditional statement (aka switch) with 50 separate cases. so when a song is loaded, it has to manually go through each case to see if it's the right song so it can load the appropriate array... instead of somehow passing a variable from a function to load the right array.

Link to comment
Share on other sites

Well maybe you're not a dumbass after all... SDCC should have converted your switch statement into a jumptable, just like TK does it in ASM ;)

That said, I haven't seen them, TK's implementation may be more optimised (probably, knowing TK) so maybe you could use inline ASM and use his jumptable macros.

50 elements is a lot though, it'll be a long table of course.

Did you actually try the multidimensional arrays? I tell ya what, I'd really like to finish this once and for all. When today's snapshot build of SDCC is released, I'm gonna make a bare app that just boots, populates a 512k multidimensional array, and then dumps it via sysex. Let's find out what's up with this :)

Link to comment
Share on other sites

Yup, works like a charm. Try it :)

I even got the array to start on 0x0FF (bad) and end at 0x300 (bad) and it still works like a charm. I can't seem to break it any more :D

You're going to want to grab sdcc snapshot 4732.

You freaks on Mac on *nix can grab yours from http://sdcc.sourceforge.net/snapshots/  :)

Link to comment
Share on other sites

re. your array experiment - please tell me the results if you get a chance to do this!

re. multidimensional arrays - these are equally frustrating because each array can only have 255 elements - because my arrays have 20 items, this means i can only have 10 dimensions per array before they break.

so if i'm already breaking it up into groups of 10, why not make the retrieval algorithm easier and simply break it up into 50 separate arrays.

i'm eventually going to use a bankstick to hold all my arrays, but for the moment this is working ok.

Link to comment
Share on other sites

re. your array experiment - please tell me the results if you get a chance to do this!

re. multidimensional arrays - these are equally frustrating because each array can only have 255 elements - because my arrays have 20 items, this means i can only have 10 dimensions per array before they break.

Ahh I don't think so bud :)

I just made array[512], filled it with bytes >0 in the init loop, and then dumpd it via sysex to make sure it was all correct, and it was. If you like I'll attach the code but it's just a few for loops, nothing special.

Just tried it with array[8][64] too and it's sweet as.

Give it a go  :D

Link to comment
Share on other sites

Do you have a 4620?

Anyhow here's some code:

edit: Darn I lost some text

OK... just rename the txt file to .lkr. write these files over a skeleton, compile and upload. 

Edit2: Oh yeh. What you'll see is a bunch of sysex messages, with bytes in pairs, incrementing from 1-64 (decimal)+number of the row (the offset is so I could be sure they were all different), separated by 0x32 (just for me to have something to 'search and replace' when I was checking)

You can see where I've remarked out the SECTION in the linker and the #pragma in the main.c. If you unremthose, you can force it to use certain memory banks. All work OK :)

project.txt

main.c

project.txt

main.c

Link to comment
Share on other sites

Hey just thinking... 50x20 chars is mare ram than this thing has...

Do you have a 4620?

i'm using a 4520 for now.

these are program constants so it's not in the ram. anyhow, all of my sequences are loading and the data shows up and is transmitting correctly :)

i'll try out your code! thx for sharing it here :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...