Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : Don't laugh!
Author | Message | ||||
yock1960 Senior Member Joined: 18/08/2020 Location: United StatesPosts: 167 |
At least don't tell me you laughed . Even at my best, my coding skills were well below many of you...and that was decades ago! But, I try...and I need help! I did the best I could speeding up the drawing of the cellular automata, in my recent program and I saw that it wasn't really drawing, but the calculations that were the issue. So, I decided to delve into CSUBS. My C is really rusty and after trying to get this to work using pointers...and failing...I tried the code below (BASIC code below the C code), which at least does not crash the CMM2, but does not work...all black pixels. Perhaps I can't pass the arguments as arrays? Or is double not correct? MMBASIC integers are 64 bits right? I also wasn't able to successfully utilize the 'RunBasicSub' to call the math scale commands either, extra terminating '/0' added. This sub gets called to populate the next row , 299 times. I'm not sure what's happening exactly in the BASIC program, but it finishes the 'plot' really fast now! #include "ARMCFunctions.h" void nextrow(double row[], double newrow[], double colors[], double code[], double rowcode[], double nrcode[]) { int i; int j; j = rowcode[0] + rowcode[1]; nrcode[0] = code[j]; newrow[0] = colors[(int)nrcode[0]]; for (i=1;i<=598;i++) { RoutineChecks(); nrcode[i] = code[j+(int)rowcode[i+1]]; newrow[i] = colors[(int)nrcode[i]]; j = rowcode[i] + rowcode[i+1]; } nrcode[599] = code[j]; newrow[599] = colors[(int)nrcode[599]]; } this is how I call it: nextrow(row%(),newrow%(),colors%(),code%(),rowcode%(),nrcode%()) here is the BASIC code that it's trying to replace: sub nextrow local x%,y% y% = rowcode%(0) + rowcode%(1) nrcode%(0) = code%(y%) newrow%(0) = colors%(nrcode%(0)) for x% = 1 to SIZE_X-&h1 nrcode%(x%) = code%(y%+rowcode%(x%+1)) newrow%(x%) = colors%(nrcode%(x%)) y% = rowcode%(x%) + rowcode%(x%+1) next nrcode%(SIZE_X) = code%(y%) newrow%(SIZE_X) = colors%(nrcode%(SIZE_X)) math scale newrow%(),1,row%() math scale nrcode%(),1,rowcode%() end sub |
||||
LeoNicolas Guru Joined: 07/10/2020 Location: CanadaPosts: 444 |
There are no "bellow skills", only coding for fun. The rule is: Create your CMM2 Basic code, share it with your forum friends and be happy |
||||
LeoNicolas Guru Joined: 07/10/2020 Location: CanadaPosts: 444 |
Integers are 64 bits and it's possible to pass arrays to CSUBs but I guess the IN arrays should be different from the OUT arrays. I could be wrong. I can't give you good advice because I'm also a noob working with CSUBs, but you can try to create a simple function receiving 2 arrays (IN and OUT) and copy the values from the first to the second. This can be a good test to understand how the CSUB arguments work. |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 8576 |
Try nextrow(row%(0),newrow%(0),colors%(0),code%(0),rowcode%(0),nrcode%(0)) If it works I'll explain why |
||||
epsilon Senior Member Joined: 30/07/2020 Location: BelgiumPosts: 255 |
Double is not the correct type here. Doubles are floating point values which use an entirely different 64-bit format than integers do (see https://en.wikipedia.org/wiki/Double-precision_floating-point_format ). I use 'long long' for 64-bit integers in my CSUBs. And I pass in arrays by passing in the array's first element as suggested by matherp. Edited 2020-12-02 19:34 by epsilon Epsilon CMM2 projects |
||||
jirsoft Guru Joined: 18/09/2020 Location: Czech RepublicPosts: 532 |
I'm using array() as parameter into CSUB, but I read it in CSUB as pointers. Somethink like: void nextrow(long long int *row, long long int *newrow, long long int *colors, long long int *code, long long int *rowcode, long long int *nrcode) { int i; int j; j = (int)*rowcode++ + (int) rowcode++; *nrcode = *(code + j); *newrow = *(colors + *nrcode); ... But it 's just snippet and 'm not sitting on CMM2, so take it just as different point of view. Jiri Napoleon Commander and SimplEd for CMM2 (GitHub), CMM2.fun |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 8576 |
Jiri is correct in what he is doing as is epsilon array() passes a pointer to the array array(0) passes the address of the first element of the array You can use either but the CSUB must match the calling mechanism |
||||
yock1960 Senior Member Joined: 18/08/2020 Location: United StatesPosts: 167 |
Thanks for the responses! Progress...but still issues. At this time, changing declarations from double (duh) to long, seems to have been the most help. I now get some plotted pixels (not correctly), but not all the way across...or rather, they turn to all black. Calling the csub using arrayname() or arrayname(0) seems not to matter, or perhaps the remaining issue makes it so. I'm getting unexpected values (full 64bit rather than 24bit color values) in my arrays, which makes me think that the csub is not pointing to the correct memory locations. To Jiri and Peter's point, I'm pretty sure declaring the variables as pointers would be better...but I'm really bad at indirection, was getting tons of errors! I did get it to compile and link once using pointers, but Armcfgenv142.bas choked on it. I will check back later after some other activities this morning and continue to work on this! Steve Edited 2020-12-02 22:00 by yock1960 |
||||
jirsoft Guru Joined: 18/09/2020 Location: Czech RepublicPosts: 532 |
I think array() and array(0) is the same address (pointer to), begin of array = first member. Array(1) will be 8 bytes difference... Try make & 0xFFFFFF ,I had sometimes also problem with some strange 'dust' in the other bytes of the variable... Jiri Napoleon Commander and SimplEd for CMM2 (GitHub), CMM2.fun |
||||
epsilon Senior Member Joined: 30/07/2020 Location: BelgiumPosts: 255 |
'long' is a 32-bit integer type. You should use 'long long', which is a 64-bit integer type. Epsilon CMM2 projects |
||||
yock1960 Senior Member Joined: 18/08/2020 Location: United StatesPosts: 167 |
Thanks Epsilon! After I started this response and when I pasted the 'then' current CSUB source, that worked really well...for 3 rows...I noticed something. I was using a literal constant in my loop...it was wrong...too big by 100! Hopefully I'll remember that lesson for a while. I changed it to a defined constant...using the correct value and it works like a charm! This makes a nice change in plotting time, from about 6.8 seconds to 0.15 seconds! Now maybe I can go back to playing with Mauro's psgmini routines and see if it will play nicer with my code! Thanks again everyone who chipped in! Steve |
||||
Print this page |