Padauk Mini-C fun! How would you iterate through an array? - Page 1
Forum section: electronics / general

author: abnielsen
author_rank: Contributor
timestamp: April 01, 2019, 09:32:17 pm
content: 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.In Mini-C this doesn't work for constant fixed arrays. So new approach!Arrays are basically pointers right? Yup.Code:word scale = {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:const word scale = {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 :(QuoteHow do I iterate a fixed array in ROM in "Mini-C"?When trying this sample code example I get "Syntax error !!!" from *Point$LCode://Table -> BufferWORDPnt2;BYTEbuffer ;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

author: Eturtle
author_rank: Newbie
timestamp: April 02, 2019, 06:22:04 am
content: Code:$ cat -n pdk.c12 #include <stdint.h>34 int main(void)5 {6 uint16_t scale = {1,2,3,4,5,6,7};7 uint16_t point;8 uint8_t output;910 point = scale;1112 output = *point; //output == 1;13 point+=2;14 output = *point; //output == 2;15 }16$ sdcc -mpdk15 pdk.cpdk.c:10: error 47: indirections to different types assignmentfrom type 'unsigned-int  fixed'to type 'unsigned-int fixed'pdk.c:12: error 27: Pointer requiredpdk.c:14: error 27: Pointer requiredNormally you should get a warning. Use a pointer like "word *point"

author: agehall
author_rank: Frequent Contributor
timestamp: April 02, 2019, 08:57:01 am
content: 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 scalewhere i is [0..6]. Do note that you index positions of the declared size, not bytes in this case.

author: abnielsen
author_rank: Contributor
timestamp: April 02, 2019, 09:14:53 am
content: 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://Table -> BufferWORDPnt2; //See this pointer declaration?BYTEbuffer ;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" array look like?

author: abnielsen
author_rank: Contributor
timestamp: April 02, 2019, 10:12:19 pm
content: 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://.CHIP PMS150C#include"extern.h"word junk;const word Table = {101,102,103,104,105};word Table2 = {106,107,108,109,110};WORDPoint1;WORDPoint2;voidFPPA0 (void){.ADJUST_ICSYSCLK=IHRC/2//SYSCLK=IHRC/2//Insert Initial CodeWORD 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 ICEPoint$0++; // hence, it¡¦s not necessary to add High Point// H_Roll = *Point$W; // Syntax error happen !!!H_Roll = *Point; //H_Roll == 0 in ICEJunk = 0x1234; //Adding random data to RAMPoint1 = 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 wordPoint2+=2;test3 = *Point1; //test3 == 17d (Fail)test4 = *Point2; //test4 == 107d (Success)while (1){//...//wdreset;}}Quote

author: abnielsen
author_rank: Contributor
timestamp: April 02, 2019, 11:12:45 pm
content: 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?

author: mikerj
author_rank: Super Contributor
timestamp: April 03, 2019, 12:03:10 pm
content: 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 onthis threadwith a possible code sample.

author: abnielsen
author_rank: Contributor
timestamp: April 04, 2019, 03:27:22 pm
content: 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:.FOR octave, <O4, O5, O6, O7>tm2s = octave;.FOR note, <Cn, Dn, En, Fn, Gn, An, Bn>tm2b = note;.delay(800000);.ENDM.ENDM

author: spth
author_rank: Regular Contributor
timestamp: April 23, 2019, 08:28:52 pm
content: While there might be some challenges in working around the limitations of Mini-C, I'd suggest to just use the free toolchain instead. You get a proper C compiler, and it works well on various host systems.AFAIK, the only real advantage left on the Mini-C side at this point is support for the pdk16 architecture and support for more devices (but I don't expect the latter point tolast for long).

author: IDEngineer
author_rank: Super Contributor
timestamp: April 27, 2019, 01:44:43 pm
content: Quote from: agehall on April 02, 2019, 08:57:01 amSeparately from the ROM-vs-RAM question... while this may "work", it's bad practice at best and dangerous at worst. You really DO need to explicitly declare something as a pointer to use it as one. Just because the compiler is weak enough to allow you to get away with it - this time - doesn't mean you can rely on that behavior going forward. What if an update to a given compiler makes it more compliant with the language standard?You're incrementing the variable "point" by 2 because YOU know you're using it to address words. The compiler doesn't know that. Things could get weird, fast.The inverse of what you're doing here is far more safe: You can treat a pointer like a variable. Declare "word* point;" and do whatever you want with it. Now the compiler knows what you're doing, you don't have to (remember to) adjust its value by 2 instead of 1, if you change the size of the array elements (say you realize a byte is sufficient, or a dword is needed) you don't have to go back through your code and hope you found all the places where you manually accommodated the size of a word, you're immune from compiler updates, etc. And if you need the actual value of the pointer? Just use it without the leading asterisk.If you absolutely must use a non-pointer as a pointer, at least give the compiler (and the next human that reads your code) a hint with an explicit cast: "*((WORD*) point)". Compilers do support what you're trying to do, though it's definitely less desireable than declaring the pointer as a pointer.I'm not being critical, just trying to help you navigate the minefield. Hope this helps!

author: agehall
author_rank: Frequent Contributor
timestamp: April 28, 2019, 12:02:09 pm
content: Quote from: IDEngineer on April 27, 2019, 01:44:43 pmWait, what? I interpreted the code Eturtle posted as "here is the code you had, but somewhat compiling" but the important bit is the last sentence in his post - "Use a pointer like "word *point"".Unless one has very special reasons to do so (and can articulate these reasons) you should of course always use pointers with the correct data type so that the compiler understands what you are doing. That goes without saying. If my previous post is misread as implying anything else, that is a mis-interpretation.

author: IDEngineer
author_rank: Super Contributor
timestamp: April 28, 2019, 03:11:28 pm
content: Quote from: agehall on April 28, 2019, 12:02:09 pmNope, you and I are in complete agreement. I quoted you, quoting eTurtle, to give both of you credit. I was trying to help the OP understand the point you were both making, that it's better to make things clear to the compiler as opposed to exploiting/relying upon some workaround (or perhaps partial/imperfect implementation of the language).

author: abnielsen
author_rank: Contributor
timestamp: May 07, 2019, 08:12:32 am
content: I completely understand the point - in C, declaring pointers correctly is a must and you must be careful working with them.In Padauk Mini-C though - declaring pointers as pointers is a syntax error. I think the compiler know's from the way you reference them though.

author: agehall
author_rank: Frequent Contributor
timestamp: May 08, 2019, 06:25:17 am
content: Well, according tohttps://es.informatik.uni-kl.de/tools/teaching/MiniCRef.pdfwhich I found by a 10s google-session, you should just be able to index your array A by doing A[ x ] where x is the index you want. That is pretty much the same as I would expect using pointers in C.

author: abnielsen
author_rank: Contributor
timestamp: May 08, 2019, 08:56:40 am
content: The MiniCRef has nothing to do with Padauk Mini-C - it's a "mini" C-reference, not at "Mini-C" reference.Code:#include"extern.h"word test;voidFPPA0 (void){.ADJUST_ICSYSCLK=IHRC/2//SYSCLK=IHRC/2//Insert Initial Codebyte testcounter;testcounter = 5;do {test = 0; //Syntax error: C:\Users\Anders\Desktop\ProjectNy\ProjectNy.C(17): Here need a value !!!} while (testcounter--);while (1){//...//wdreset;}}Above produces a syntax error.Code:Compile ...C:\Users\Anders\Desktop\ProjectNy\ProjectNy.PREC:\Users\Anders\Desktop\ProjectNy\ProjectNy.CC:\Users\Anders\Desktop\ProjectNy\ProjectNy.C(17): Here need a value !!!PMS150C : ROM Size : 0x3F0PMS150C : Last use Code : 0x80 , Remain Free-Code : 0x370 [+37]1 error(s), 0 warning(s)What that means is - you can't index it that way.The correct way is using pointers. Which have funny declarations in Padauk Mini-C...

author: agehall
author_rank: Frequent Contributor
timestamp: May 08, 2019, 09:09:28 am
content: Quote from: abnielsen on May 08, 2019, 08:56:40 amNo, it is not a mini C-reference. It is a reference for MiniC. Not sure if that is exactly what Padauk uses but it isn't a C reference.

author: mikerj
author_rank: Super Contributor
timestamp: May 08, 2019, 05:55:26 pm
content: Quote from: agehall on May 08, 2019, 09:09:28 amIt's aneducational compilerused by a universities computer science department, not related to Padauk micros in any way.

author: gaganchd2000
author_rank: Newbie
timestamp: August 03, 2019, 11:32:28 pm
content: Hi Padauk ExpertsI am writing a code which again needs to iterate through constant values. ( array)I checked the assembly mnemonics and could not find any instructions which can pick data from ROM.Basically looking for similar in padauk for MOVC A, @A+DPTR in 8051.best solution till now i see is.FOR lv_i, <0x00,0xAA,0x00,0x55>PA = lv_i;.delay(1000);.endmI need to do this for 50 different arrays. and writing loop for all arrays will eat my ROM.any better solution ?

author: mikerj
author_rank: Super Contributor
timestamp: August 04, 2019, 11:41:00 am
content: Check the thread I alreadylinked toabove.