### Author Topic: Padauk Mini-C fun! How would you iterate through an array?  (Read 680 times)

0 Members and 1 Guest are viewing this topic.

#### abnielsen

• Contributor
• Posts: 8
• Country:
##### Padauk Mini-C fun! How would you iterate through an array?
« on: April 02, 2019, 08:32:17 am »
I used to think I knew how to iterate through an array. Thanks to Padauk I have to learn new stuff!

Usually I would do a for loop with a counter - arrayname[count].
In Mini-C this doesn't work for constant fixed arrays. So new approach!

Arrays are basically pointers right? Yup.

Code: [Select]
word scale[7] = {1,2,3,4,5,6,7};word point;byte output;point = scale;output = *point; //output == 1;point+=2;output = *point; //output == 2;
The above works! Yay!

As soon as I try to save in ROM, everything breaks though :/

Code: [Select]
const word scale[7] = {1,2,3,4,5,6,7};word point;byte output;point = scale;output = *point; //output == random garbage :(point+=2;output = *point; //output == random garbage :(
Quote
In "Mini-C" project, the constant data of ROM can be declared as follows:
CONST   WORD    array_name []   =   { 0x1234, 0x5678, 0xABCD };

How do I iterate a fixed array in ROM in "Mini-C"?

When trying this sample code example I get "Syntax error !!!" from *Point$L Code: [Select]  // Table -> Buffer WORD Pnt2; BYTE buffer [0x20]; Point = Table; Pnt2 = buf; while (1) { *Pnt2 = *Point$L; if (A == 0) break; Pnt2$0++; *Pnt2 = *Point$H; if (A == 0) break; Point++; }
Is this a question for Padauk or does anyone here see something obvious I'm missing? EEVBlog forum is the closest to a Padauk-forum I can think of

#### Eturtle

• Contributor
• Posts: 5
• Country:
##### Re: Padauk Mini-C fun! How would you iterate through an array?
« Reply #1 on: April 02, 2019, 05:22:04 pm »
Code: [Select]
$cat -n pdk.c 1 2 #include <stdint.h> 3 4 int main(void) 5 { 6 uint16_t scale[7] = {1,2,3,4,5,6,7}; 7 uint16_t point; 8 uint8_t output; 9 10 point = scale; 11 12 output = *point; //output == 1; 13 point+=2; 14 output = *point; //output == 2; 15 } 16$ sdcc -mpdk15 pdk.c pdk.c:10: error 47: indirections to different types assignment   from type 'unsigned-int [7] fixed'  to type 'unsigned-int fixed'pdk.c:12: error 27: Pointer requiredpdk.c:14: error 27: Pointer required
Normally you should get a warning. Use a pointer like "word *point"

#### agehall

• Regular Contributor
• Posts: 223
• Country:
##### Re: Padauk Mini-C fun! How would you iterate through an array?
« Reply #2 on: April 02, 2019, 07:57:01 pm »
I think your code works by accident when you put the array in RAM. If you were to declare another variable before the scale array, it would probably break.

As Eturtle already pointed out, you need to work with pointers to do what you are trying to do. The other way is to index the array like scale where i is [0..6]. Do note that you index positions of the declared size, not bytes in this case.

#### abnielsen

• Contributor
• Posts: 8
• Country:
##### Re: Padauk Mini-C fun! How would you iterate through an array?
« Reply #3 on: April 02, 2019, 08:14:53 pm »
Thanks for the input!

I don't have the documentation available at the moment, but I'm sure I read "any WORD in Mini-C can be a pointer".. I read that as - they don't have to be declared as pointers to be pointers.

The Padauk IDE is a lot of fun.

At least, that's what I get from the sample code I pasted. In the Help -> "Refer source" menu in Padauk IDE (0.85)

Code: [Select]
 // Table -> Buffer WORD Pnt2; //See this pointer declaration? BYTE buffer [0x20]; Point = Table; //See this pointer declaration? Pnt2 = buf; while (1) { *Pnt2 = *Point$L; if (A == 0) break; Pnt2$0++; *Pnt2 = *Point$H; if (A == 0) break; Point++; } What would the "normal" way of referencing a "const uint16_t[7]" array look like? #### abnielsen • Contributor • Posts: 8 • Country: ##### Re: Padauk Mini-C fun! How would you iterate through an array? « Reply #4 on: April 03, 2019, 09:12:19 am » Here is my complete testing code. Reading from RAM works, reading constant array from ROM doesn't. Maybe it's a bug - the$L, $H and$W byte selectors don't work either(syntax error). I added the example code from the "get rolling code" example, in the beginning.

Code: [Select]
//.CHIP PMS150C#include "extern.h"word junk;const word Table[5] = {101,102,103,104,105};word Table2[5] = {106,107,108,109,110};WORD Point1;WORD Point2;void FPPA0 (void){ .ADJUST_IC SYSCLK=IHRC/2 // SYSCLK=IHRC/2 // Insert Initial Code WORD    Point, L_Roll, H_Roll;        Point   =   _SYS(ADR_ROLL);     // Use the built-in constant  _SYS(ADR_ROLL) to obtain                                        // the address. Generally, the space should be in xxxA//        L_Roll  =   *Point$W; //C:\Users\Anders\ArrayTest\ArrayTest.C(23): Syntax error happen !!! L_Roll = *Point; //L_Roll == 0 in ICE Point$0++;                      // hence, it¡¦s not necessary to add High Point//        H_Roll  =   *Point\$W; // Syntax error happen !!! H_Roll = *Point; //H_Roll == 0 in ICE Junk = 0x1234; //Adding random data to RAM Point1 = Table; Point2 = Table2; WORD test; WORD test2; WORD test3; WORD test4; test = *Point1; //Test == 0 (Fail) test2 = *Point2; //test2 == 106d (Success) Point1+=2; //Increment pointers one word Point2+=2; test3 = *Point1; //test3 == 17d (Fail) test4 = *Point2; //test4 == 107d (Success) while (1) {// ...// wdreset; }}
Quote
Compile ...
C:\Users\Anders\ArrayTest\ArrayTest.PRE
C:\Users\Anders\ArrayTest\ArrayTest.C
memory 0x0 ~ 0x3F are free
Use  30 memory, Remain 34 unused memory
Check Sum is 0x6616CA
PMS150C  : ROM Size : 0x3F0
PMS150C  : Last use Code : 0xBD , Remain Free-Code : 0x333 [+37]

0 error(s), 0 warning(s)

#### abnielsen

• Contributor
• Posts: 8
• Country:
##### Re: Padauk Mini-C fun! How would you iterate through an array?
« Reply #5 on: April 03, 2019, 10:12:45 am »
I think I figured it out. PMS150C which I've been using in my test doesn't have the ldtabl and ldtabh instructions *SIGH*
I guess this means I can store data in ROM, but I can't read it out this way. In fact I don't see any instructions in the datasheet for reading out ROM at all.

Is there a way of getting around this? What's the most efficient and practical way of storing and iterating through a sequence of fixed/constant data?

#### mikerj

• Super Contributor
• Posts: 1778
• Country:
##### Re: Padauk Mini-C fun! How would you iterate through an array?
« Reply #6 on: April 03, 2019, 11:03:10 pm »
This sounds very much like the old 12 and 14 bit PICs where there was no way to directly read program memory.  For const arrays in program memory you had to store a bunch of "retw x" instructions where x was one 8 bit array value, and then perform a call followed by a calculated jump into the array of retw x instructions.

The PIC compilers used to do this automatically, you could use normal array index notation, but perhaps the Padauk compiler is a bit less advanced.  There is some more information on this thread with a possible code sample.
« Last Edit: April 04, 2019, 09:12:23 pm by mikerj »

#### abnielsen

• Contributor
• Posts: 8
• Country:
##### Re: Padauk Mini-C fun! How would you iterate through an array?
« Reply #7 on: April 05, 2019, 02:27:22 am »
Great info! I'm not sure it's the best way, but it looks like it works better than what I'm doing now.

Macros are another way of getting around this - Padauk IDE supports for loops with values like this:

Code: [Select]
.FOR octave, <O4, O5, O6, O7> tm2s = octave; .FOR note, <Cn, Dn, En, Fn, Gn, An, Bn> tm2b = note; .delay(800000); .ENDM.ENDM

Smf