![]() |
Forum Index : Microcontroller and PC projects : MM2: string sort using cFunction
Author | Message | ||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10197 |
As requested I've created a Cfunction to sort the strings within a string array. The example program uses some fun code to convert numbers to text i.e. -767 > "negative seven hunded sixty-seven" and loads these into array "s$(200) length 60". The sort is a bubble sort as most of the other sorts rely on the nature of the number being sorted to optimise the approach. However, performance is still very usable. For 101 strings up to 45 characters in length with no duplicates I get 62msec. For 151 strings with 50 duplicates I get 139 msec for 201 strings with 50 exact duplicates and 50 shortened duplicates I get 259 msec The C source is included in the code as comment cpu 48 option explicit option default none dim S$(200) length 60 dim integer i pause 500 ' preparesillystrings(101) 'Create 101 random strings for i=101 to 150 s$(I)=S$(I-75) 'Create 50 duplicates to make things harder NEXT I for i=151 to 200 s$(I)=left$(S$(I-75),20) 'Create 50 duplicates but shorter to make things even harder NEXT I ' timer=0 ' Sort the first 201 strings in the S$ array, the third parameter is mandatory and is the number specified in ' the "LENGTH" parameter in the string defintion, if this is not specified use 255 in the function call i=stringsort(S$(),201,60) print "Time taken ",Timer," msec" ' for i=0 to 150 print s$(i) next i end ' CFunction stringsort 00000000 27bdfff0 afb3000c afb20008 afb10004 afb00000 8cc30000 24630001 8cb90000 2b250002 14a00041 00603021 10000038 00838021 01a09821 918e0000 91af0000 01cf502b 01c01021 01ea100a 10400011 01809021 25850001 25a90001 004c5021 90a80000 91270000 0107102b 00e8382b 0162380b 10aa0005 00e01021 14e00009 24a50001 1000fff6 25290001 14e00005 00000000 01cf282b 14a00014 01601021 01ee102b 54430012 27180001 01ee282b 01e5700a 11cb000d 00608821 25850001 00ae7021 02603821 02402821 90a80000 90e90000 a0a90000 a0e80000 24a50001 14aefffa 24e70001 00608821 27180001 01866021 0319282a 14a0ffce 01a66821 56200005 00806021 10000009 00021fc3 240bffff 00806021 02006821 00008821 24180001 1000ffc3 24030001 00021fc3 8fb3000c 8fb20008 8fb10004 8fb00000 03e00008 27bd0010 End CFunction ' 'long long stringsort(unsigned char *sarray, long long *nstring, long long *maxlength){ 'long ii,offset=*maxlength+1,i, s = 1, n=*nstring; 'long k; 'unsigned char *s1,*s2,*p1,*p2; 'unsigned char temp; ' while (s){ ' s=0; ' for(i=1;i<n;i++){ ' s2=i*offset+sarray; ' s1=(i-1)*offset+sarray; ' ii = *s1 < *s2 ? *s1 : *s2; //get the smaller length ' p1 = s1 + 1; p2 = s2 + 1; ' k=0; //assume the strings match ' while((ii--) && (k==0)) { ' if(*p1 > *p2){ ' k=1; //earlier in the array is bigger ' } ' if(*p1 < *p2){ ' k=-1; //later in the array is bigger ' } ' p1++; p2++; ' } ' // if up to this point the strings match ' // make the decision based on which one is shorter ' if(k==0){ ' if(*s1 > *s2) k=1; ' if(*s1 < *s2) k=-1; ' } ' if (k==1){ // if earlier is bigger swap them round ' ii = *s1 > *s2 ? *s1 : *s2; //get the bigger length ' ii++; ' p1=s1;p2=s2; ' while(ii--){ ' temp=*p1; ' *p1=*p2; ' *p2=temp; ' p1++; p2++; ' } ' s=1; ' } ' } ' } 'return k; '} ' sub preparesillystrings(nstrings as integer) 'small DATA "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" DATA "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" 'tens DATA "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" 'big DATA "thousand", "million", "billion" dim small(19) AS STRING length 10, tens(7) AS STRING length 10, big(2) AS STRING length 10 local i as integer FOR i = 1 TO 19 READ small(i) NEXT FOR i = 0 TO 7 READ tens(i) NEXT FOR i = 0 TO 2 READ big(i) NEXT for i=0 to nstrings s$(i)=int2text(rnd()*10000-1000) next i end sub FUNCTION int2Text(number AS integer) as string local num AS integer, outP AS STRING length 60, unit AS INTEGER local tmpLng1 AS integer IF 0 = number THEN int2Text = "zero" EXIT FUNCTION END IF num = ABS(number) DO tmpLng1 = num MOD 100 SELECT CASE tmpLng1 CASE 1 TO 19 outP = small(tmpLng1) + " " + outP CASE 20 TO 99 SELECT CASE tmpLng1 MOD 10 CASE 0 outP = tens((tmpLng1 \ 10) - 2) + " " + outP CASE ELSE outP = tens((tmpLng1 \ 10) - 2) + "-" + small(tmpLng1 MOD 10) + " " + outP END SELECT END SELECT tmpLng1 = (num MOD 1000) \ 100 IF tmpLng1 THEN outP = small(tmpLng1) + " hundred " + outP END IF num = num \ 1000 IF num < 1 THEN EXIT DO tmpLng1 = num MOD 1000 IF tmpLng1 THEN outP = big(unit) + " " + outP unit = unit + 1 LOOP IF number < 0 THEN outP = "negative " + outP Do WHILE ASC(RIGHT$(outp,1))<=32 outp = LEFT$(outp,len(outp)-1) loop int2Text = outP END FUNCTION |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3282 |
Wonderful, thank you. The two CFunctions (numeric and string sort) make a great general purpose package for people with this particular need. It is marvellous that such specialised functions can be added to the language so easily. If it is OK with you I will add both of them to the next Micromite release. Geoff Geoff Graham - http://geoffg.net |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10197 |
Of course that's OK. I do want to re-request though that, for a future release, you look at only storing cFunctions as binary (with perhaps just a function prototype visible in Basic). It makes no sense to me for them to be available in the editor and takes a huge amount of space. What has focused me on this again is the development I've been doing to integrate the Henning Karlson fonts into the graphics routines. The three basic fonts end up as the cFunction below which takes a huge amount of space in its text representation, but very little as binary. The memory report I get from the same program with and without the fonts even after "crunching" is: With: Program 46K, Cfunctions 10K Without: Program 30K, Cfunctions 6K So they are using 20K of memory for only 4K of "value" ' 'C:\Users\Peter\Dropbox\MPLABXProjects\ILI9341ST7735serfonts \dist\default\production\SmallFont.bas ' 'SmallFont 2015-01-22 16:12:39 CFuncGen Ver 1.0.21 by user=Peter ' CFunction CSmallFont 00000000 5f200c08 00000000 00000000 00000000 20200000 20202020 00002000 50502800 00000000 00000000 28280000 fc5028fc 00005050 a8782000 283060a0 0020f0a8 a8480000 342850b0 00004854 50200000 a8a87850 00006c90 80404000 00000000 00000000 10080400 10101010 00040810 10204000 10101010 00402010 20000000 a87070a8 00000020 20200000 2020f820 00000020 00000000 00000000 80404000 00000000 0000f800 00000000 00000000 00000000 00004000 10100800 40202010 00804040 88700000 88888888 00007088 60200000 20202020 00007020 88700000 40201088 0000f880 88700000 08083008 00007088 30100000 78905050 00001810 80f80000 0808f080 00007088 90700000 8888f080 00007088 90f80000 20202010 00002020 88700000 88887088 00007088 88700000 08788888 00007048 00000000 00000020 00002000 00000000 00002000 00202000 10080400 10204020 00000408 00000000 f80000f8 00000000 10204000 10080408 00004020 88700000 20201088 00002000 88700000 b8a8a898 00007880 20200000 78505030 0000cc48 48f00000 48487048 0000f048 88780000 80808080 00007088 48f00000 48484848 0000f048 48f80000 40507050 0000f848 48f80000 40507050 0000e040 48380000 889c8080 00003048 48cc0000 48487848 0000cc48 20f80000 20202020 0000f820 107c0000 10101010 00e09010 48ec0000 50506050 0000ec48 40e00000 40404040 0000fc44 d8d80000 a8a8d8d8 0000a8a8 48dc0000 58586868 0000e848 88700000 88888888 00007088 48f00000 40407048 0000e040 88700000 e8888888 00187098 48f00000 48507048 0000ec48 88780000 08106080 0000f088 a8f80000 20202020 00007020 48cc0000 48484848 00003048 48cc0000 30505048 00002020 a8a80000 505070a8 00005050 50d80000 50202050 0000d850 50d80000 20202050 00007020 90f80000 40202010 0000f848 20203800 20202020 00382020 40404000 10102020 00000810 10107000 10101010 00701010 00502000 00000000 00000000 00000000 00000000 fc000000 00002000 00000000 00000000 00000000 38483000 00003c48 40c00000 48487040 00007048 00000000 40483800 00003840 08180000 48483808 00003c48 00000000 78483000 00003840 201c0000 20207820 00007820 00000000 30483c00 38447840 40c00000 48487040 0000ec48 00200000 20206000 00007020 00100000 10103000 e0101010 40c00000 70505c40 0000ec48 20e00000 20202020 0000f820 00000000 a8a8f000 0000a8a8 00000000 4848f000 0000ec48 00000000 48483000 00003048 00000000 4848f000 e0407048 00000000 48483800 1c083848 00000000 4060d800 0000e040 00000000 30407800 00007808 20000000 20207020 00001820 00000000 4848d800 00003c48 00000000 5048ec00 00002030 00000000 70a8a800 00005050 00000000 2050d800 0000d850 00000000 5048ec00 c0202030 00000000 20107800 00007820 10101800 10102010 00181010 10101010 10101010 10101010 20206000 20201020 00602020 0018a440 00000000 00000000 End CFunction ' 'C:\Users\Peter\Dropbox\MPLABXProjects\ILI9341ST7735ser.X\di st\default\production\SevenSegNumFontPlus.bas ' 'SevenSegNumFontPlus 2015-01-21 16:48:44 CFuncGen Ver 1.0.21 by user=Peter ' CFunction CSevenSegFont 00000000 0b303220 00000000 00000000 00feff00 00ffff01 80ffff03 60ffff01 f0feff0c f801001e f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f 7800003e 18000038 08000020 00000000 00000020 18000038 7800003e f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f000001e 60feff0c 00ffff01 80ffff03 00ffff01 00feff00 00000000 00000000 00000000 00000000 00000000 00000000 00000000 60000000 f0000000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 78000000 18000000 08000000 00000000 00000000 18000000 78000000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f0000000 60000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00feff00 00ffff01 80ffff03 60ffff01 f0feff00 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 78000000 18feff01 88ffff03 e0ffff0f c0ffff27 00ffff39 0000003e 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000001e 00feff0c 00ffff01 80ffff03 00ffff01 00feff00 00000000 00000000 00000000 00000000 00feff00 00ffff01 80ffff03 60ffff01 f0feff00 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 78000000 18feff01 88ffff03 e0ffff0f c0ffff07 18ffff01 78000000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f0000000 60feff00 00ffff01 80ffff03 00ffff01 00feff00 00000000 00000000 00000000 00000000 00000000 00000000 00000000 60000000 f000000c f801001e f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f 7800003e 18feff39 88ffff23 e0ffff0f c0ffff07 18ffff01 78000000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f0000000 60000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00feff00 00ffff01 80ffff03 00ffff01 00feff0c 0000001e 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003e 00feff39 80ffff23 e0ffff0f c0ffff07 18ffff01 78000000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f0000000 60feff00 00ffff01 80ffff03 00ffff01 00feff00 00000000 00000000 00000000 00000000 00feff00 00ffff01 80ffff03 00ffff01 00feff0c 0000001e 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003f 0000003e 00feff39 80ffff23 e0ffff0f c0ffff27 18ffff39 7800003e f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f000001e 60feff0c 00ffff01 80ffff03 00ffff01 00feff00 00000000 00000000 00000000 00000000 00feff00 00ffff01 80ffff03 60ffff01 f0feff00 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 78000000 18000000 08000000 00000000 00000000 18000000 78000000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f0000000 60000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00feff00 00ffff01 80ffff03 60ffff01 f0feff0c f801001e f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f 7800003e 18feff39 88ffff23 e0ffff0f c0ffff27 18ffff39 7800003e f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f000001e 60feff0c 00ffff01 80ffff03 00ffff01 00feff00 00000000 00000000 00000000 00000000 00feff00 00ffff01 80ffff03 60ffff01 f0feff0c f801001e f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f f801003f 7800003e 18feff39 88ffff23 e0ffff0f c0ffff07 18ffff01 78000000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f8010000 f0000000 60feff00 00ffff01 80ffff03 00ffff01 00feff00 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00e00000 00f00100 00f80300 00f80300 00f80300 00f00100 00e00000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00e00000 00f00100 00f80300 00f80300 00f80300 00f00100 00e00000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 End CFunction ' 'C:\Users\Peter\Dropbox\MPLABXProjects\ILI9341ST7735serfonts \dist\default\production\BigFont.bas ' 'BigFont 2015-01-22 16:16:51 CFuncGen Ver 1.0.21 by user=Peter ' CFunction CBigFont 00000000 5f201010 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 800f0007 800f800f 800f800f 00070007 00000000 00070007 00000007 380e0000 380e380e 3006380e 00000000 00000000 00000000 00000000 00000000 300c0000 300c300c fe7ffe7f 300c300c 300c300c fe7ffe7f 300c300c 0000300c 40020000 f80f4002 401af81f f01f401a 5802f80f f81f5802 4002f01f 00004002 00000000 100e0000 700e300e c001e000 00078003 700c700e 00007008 00000000 00000000 8019000f 80198019 080f000f f819980f e018f018 980ff019 00000000 00000000 00070007 000e0007 00000000 00000000 00000000 00000000 00000000 00000000 c001f000 00078003 000e000e 000e000e 80030007 f000c001 00000000 00000000 8003000f e000c001 70007000 70007000 c001e000 000f8003 00000000 00000000 88118001 e0079009 fc3fe007 e007fc3f 9009e007 80018811 00000000 00000000 00000000 80018001 f00f8001 8001f00f 80018001 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00070000 00070007 0000000e 00000000 00000000 00000000 f81f0000 0000f81f 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00070000 00070007 00000000 00000000 06000200 1c000e00 70003800 c001e000 00078003 001c000e 00000000 00000000 381cf00f f81c781c b81df81c 381fb81d 381e381f f00f381c 00000000 00000000 80018001 801f8003 8003801f 80038003 80038003 f01f8003 00000000 00000000 701ce00f 3800381c e0007000 8003c001 380e0007 f81f381c 00000000 00000000 701ce00f 3800381c c0037000 7000c003 381c3800 e00f701c 00000000 00000000 e001e000 e006e003 e018e00c f81ff81f e000e000 f803e000 00000000 00000000 001cf81f 001c001c e01f001c 7800f01f 381c3800 e00f701c 00000000 00000000 0007e003 001c000e f01f001c 381cf81f 381c381c f00f381c 00000000 00000000 1c1cfc1f 1c1c1c1c 38001c00 e0007000 8003c001 80038003 00000000 00000000 381cf00f 381c381c e007381f f81ce007 381c381c f00f381c 00000000 00000000 381cf00f 381c381c f81f381c 3800f80f 70003800 c007e000 00000000 00000000 00000000 80038003 00008003 80030000 80038003 00000000 00000000 00000000 00000000 80038003 00008003 80030000 80038003 00000007 00000000 70000000 c001e000 00078003 001c000e 000e001c 80030007 e000c001 00007000 00000000 00000000 fc3f0000 0000fc3f fc3f0000 0000fc3f 00000000 00000000 001c0000 0007000e c0018003 7000e000 e0007000 8003c001 000e0007 0000001c c0030000 781ef00f 38003818 e0007000 c001c001 00000000 c001c001 0000c001 f80f0000 1c1c1c1c 1c1c1c1c fc1cfc1c fc1cfc1c 001c001c f01f001c 0000f807 00000000 e007c003 381c700e 381c381c f81f381c 381c381c 381c381c 00000000 00000000 380ef01f 380e380e f00f380e 380ef00f 380e380e f01f380e 00000000 00000000 380ef007 001c381c 001c001c 001c001c 381c001c f007380e 00000000 00000000 700ee01f 380e380e 380e380e 380e380e 380e380e e01f700e 00000000 00000000 180ef81f 000e080e f00f300e 300ef00f 080e000e f81f180e 00000000 00000000 180ef81f 000e080e f00f300e 300ef00f 000e000e 001f000e 00000000 00000000 380ef007 381c381c 001c001c f81c001c 381c381c f807380e 00000000 00000000 701c701c 701c701c f01f701c 701cf01f 701c701c 701c701c 00000000 00000000 8003e00f 80038003 80038003 80038003 80038003 e00f8003 00000000 00000000 7000fc01 70007000 70007000 70387000 70387038 e00f7038 00000000 00000000 380e381e e00e700e 800fc00f c00f800f 700ee00e 381e380e 00000000 00000000 000e001f 000e000e 000e000e 000e000e 180e080e f81f380e 00000000 00000000 3c1e1c1c fc1f7c1f dc1dfc1f 1c1c9c1c 1c1c1c1c 1c1c1c1c 00000000 00000000 1c1c1c1c 1c1f1c1e dc1d9c1f 7c1cfc1c 1c1c3c1c 1c1c1c1c 00000000 00000000 f007e003 1c1c380e 1c1c1c1c 1c1c1c1c 380e1c1c e003f007 00000000 00000000 380ef01f 380e380e f00f380e 000ef00f 000e000e 001f000e 00000000 00000000 780fe003 1c1c380e 1c1c1c1c 7c1c1c1c f80ffc1c 3800f80f 0000fc00 00000000 380ef01f 380e380e f00f380e 700ef00f 380e380e 381e380e 00000000 00000000 381cf00f 381c381c e00f001c 3800f007 381c381c f00f381c 00000000 00000000 cc19fc1f c001c411 c001c001 c001c001 c001c001 f007c001 00000000 00000000 701c701c 701c701c 701c701c 701c701c 701c701c e00f701c 00000000 00000000 701c701c 701c701c 701c701c 701c701c e00e701c 8003c007 00000000 00000000 1c1c1c1c 1c1c1c1c 9c1c1c1c 9c1c9c1c f80ff80f 70077007 00000000 00000000 701c701c e00e701c 8003c007 c0078003 701ce00e 701c701c 00000000 00000000 701c701c 701c701c e00e701c 8003c007 80038003 e00f8003 00000000 00000000 381cf81f 70103818 c001e000 00078003 181c080e f81f381c 00000000 00000000 0007f007 00070007 00070007 00070007 00070007 f0070007 00000000 00000000 00180010 000e001c 80030007 e000c001 38007000 07001c00 00000000 00000000 7000f007 70007000 70007000 70007000 70007000 f0077000 00000000 80010000 e007c003 381c700e 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 ff7fff7f 00000000 001c001c 00070007 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 7000e00f f00f7000 701c701c d80f701c 00000000 00000000 000e001e 000e000e 380ef00f 380e380e 380e380e f01b380e 00000000 00000000 00000000 00000000 701ce00f 001c701c 701c001c e00f701c 00000000 00000000 7000f800 70007000 701cf00f 701c701c 701c701c d80f701c 00000000 00000000 00000000 00000000 701ce00f f01f701c 701c001c e00f701c 00000000 00000000 7007e003 00077007 e01f0007 0007e01f 00070007 c01f0007 00000000 00000000 00000000 00000000 701cd80f 701c701c f00f701c 7000f007 e00f701c 00000000 000e001e 000e000e 380ff00e 380e380f 380e380e 381e380e 00000000 00000000 c001c001 0000c001 c001c00f c001c001 c001c001 f80fc001 00000000 00000000 70007000 00007000 7000f003 70007000 70007000 701c7000 e007f00c 00000000 000e001e 000e000e 700e380e c00fe00e 700ee00e 381e380e 00000000 00000000 c001c00f c001c001 c001c001 c001c001 c001c001 f80fc001 00000000 00000000 00000000 00000000 9c1cf81f 9c1c9c1c 9c1c9c1c 9c1c9c1c 00000000 00000000 00000000 00000000 701ce01f 701c701c 701c701c 701c701c 00000000 00000000 00000000 00000000 701ce00f 701c701c 701c701c e00f701c 00000000 00000000 00000000 00000000 380ef01b 380e380e 380e380e 000ef00f 001f000e 00000000 00000000 00000000 e038b01f e038e038 e038e038 e000e01f f001e000 00000000 00000000 00000000 f80ff01e 000e380f 000e000e 001f000e 00000000 00000000 00000000 00000000 301ce00f 800f301c 7018e003 e00f7018 00000000 00000000 00010000 00070003 0007f01f 00070007 70070007 e0037007 00000000 00000000 00000000 00000000 701c701c 701c701c 701c701c d80f701c 00000000 00000000 00000000 00000000 701c701c 701c701c e00e701c 8003c007 00000000 00000000 00000000 00000000 1c1c1c1c 9c1c1c1c f80f9c1c 70077007 00000000 00000000 00000000 00000000 e01ce01c 8007c00f c00f8007 e01ce01c 00000000 00000000 00000000 00000000 380e380e 380e380e f007380e e000e003 801fc001 00000000 00000000 00000000 e018e01f 8003c011 200e0007 e01f601c 00000000 00000000 8003f801 80038003 001c0007 0007001c 80038003 f8018003 00000000 c0010000 c001c001 c001c001 c001c001 c001c001 c001c001 c001c001 0000c001 00000000 c001801f c001c001 3800e000 e0003800 c001c001 801fc001 00000000 00000000 9c3b1c1f f838dc39 00000000 00000000 00000000 00000000 00000000 End CFunction ' |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3282 |
As it happens, I have just finished a test version of MMBasic which strips out the CFunction data and just leaves stubs marking the CFunctions in the code. The CFunction data is then stored as executable binary in flash (as it is in V4.6). When you list or edit the program the CFunction data is re inserted where the stubs are located so that you are unaware of this bit of trickery. Unfortunately, after all this work, I belatedly realised that this does not help with the amount of flash memory used. The reason is that the full text of the program most be stored in RAM before it can be written to flash. This is because writing to flash disrupts communications including the console serial I/O which is used to transfer the program. So the amount of free RAM (about 53KB) limits the maximum size of the program NOT the amount of free flash. This RAM must be able to hold the full program including the CFunction data in hex as the stripping of the CFunction data can only be practically done after the full program has been buffered in RAM. I suppose that we could have a separate command for transferring the CFunction data (for example, XMODEM CFUN) but that would mean that the program would be in two pieces which would have to be transferred separately - very messy and not nice. As far as I can see the only practical solution is to use a chip with more RAM but that has other issues including the time required to port the code and the limited number of people who would need so much capacity (my apologies, I still have to reply to your email on that subject). Geoff Geoff Graham - http://geoffg.net |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10197 |
Since anyone using Micromite with Cfunctions is likely to be using MMEdit, perhaps we could twist Jim's arm to create a version that could do this behind the scenes transparently to the user. Given the complexity of the things he does with facilities like "crunch" it shouldn't be too difficult to parse out the Cfunctions as one download and then the Basic code as the second. Jim: is this possible? |
||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
As CFUNCTIONS are programmed separately from the rest of the program it might be useful to be able to load cfunctions into flash prior to loading and running a basic program. A possible solution: Use the LIBRARY command for loading cfunctions into flash. Currently only the MM and CMM version have a library command and extending this command to be able to use cfunction would be a nice extension. Some example uses: Adding a cfunction to flash: [code] LIBRARY ADD stringsort 00000000 27bdfff0 afb3000c afb20008 afb10004 afb00000 8cc30000 24630001 8cb90000 2b250002 14a00041 00603021 10000038 00838021 01a09821 918e0000 91af0000 01cf502b 01c01021 01ea100a 10400011 01809021 25850001 25a90001 004c5021 90a80000 91270000 0107102b 00e8382b 0162380b 10aa0005 00e01021 14e00009 24a50001 1000fff6 25290001 14e00005 00000000 01cf282b 14a00014 01601021 01ee102b 54430012 27180001 01ee282b 01e5700a 11cb000d 00608821 25850001 00ae7021 02603821 02402821 90a80000 90e90000 a0a90000 a0e80000 24a50001 14aefffa 24e70001 00608821 27180001 01866021 0319282a 14a0ffce 01a66821 56200005 00806021 10000009 00021fc3 240bffff 00806021 02006821 00008821 24180001 1000ffc3 24030001 00021fc3 8fb3000c 8fb20008 8fb10004 8fb00000 03e00008 27bd0010 End >RUN stringsort succesfully loaded into flash at address &H12345678. > [/code] To see a list of library functions [code] >LIBRARY LIST stringsort &H12345678 > [/code] Deleting a cfunction: [code] >LIBRARY DELETE stringsort stringsort succesfully deleted from flash. > [/code] or [code] >LIBRARY CLEAR All library functions are deleted. > [/code] Repeatedly adding and deleting cfunctions from flash can lead to fragmentation. Which is not really a problem if there is enough flash available for the functions needed. a LIBRARY CLEAR and then adding the cfunctions again would solve fragmentation and would be a good final step before finishing the project. It would be nice to be able to reconstruct a cfunction if the source is lost. [code] >LIBRARY RETRIEVE stringsort CFUNCTION stringsort 00000000 27bdfff0 afb3000c afb20008 afb10004 afb00000 8cc30000 24630001 8cb90000 2b250002 14a00041 00603021 10000038 00838021 01a09821 918e0000 91af0000 01cf502b 01c01021 01ea100a 10400011 01809021 25850001 25a90001 004c5021 90a80000 91270000 0107102b 00e8382b 0162380b 10aa0005 00e01021 14e00009 24a50001 1000fff6 25290001 14e00005 00000000 01cf282b 14a00014 01601021 01ee102b 54430012 27180001 01ee282b 01e5700a 11cb000d 00608821 25850001 00ae7021 02603821 02402821 90a80000 90e90000 a0a90000 a0e80000 24a50001 14aefffa 24e70001 00608821 27180001 01866021 0319282a 14a0ffce 01a66821 56200005 00806021 10000009 00021fc3 240bffff 00806021 02006821 00008821 24180001 1000ffc3 24030001 00021fc3 8fb3000c 8fb20008 8fb10004 8fb00000 03e00008 27bd0010 END CFUNCTION > [/code] By loading cfunctions into flash you are practically extending the functionality of MMBasic with new keywords. If cfunctions are popular and add significant value they can then be migrated to MMBasic pretty easy while maintaining backward compatibility for programs that used those cfunctions. Microblocks. Build with logic. |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3282 |
I still think that loading CFunctions (or LIBRARIES) separately is messy and non intuitive. And it is only a limited fix - the real solution is much more memory. But I will experiment with loading and unloading functions separately and see where that takes us. Geoff Graham - http://geoffg.net |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6268 |
I like having all the required code in the one file. That helps prevent the wrong version of a cfunction being used etc. The disadvantage of having it all in one file is the time it takes to transfer the file to the micromite. This is where having the cfunctions loaded separately would help a lot. Provided cfunctions were loaded using XMODEM, any terminal program could be used to transfer them. MMEdit could be adapted to automate the loading. I could strip the cfunctions out and upload separately using LIBRARY or XMODEM CFUNC etc but I think it is important to allow ANY terminal program to do the job. The biggest advantage that MMBasic has over the competitors is the built-in editor and no reliance on a specific PC compiler. Developing cfunctions is different but it is still important to cater for the users who will not want to mess with creating cfunctions, just use them. Jim VK7JH MMedit |
||||
akashh Senior Member ![]() Joined: 19/01/2014 Location: IndiaPosts: 115 |
If the issue is the inline editor, why not work on a buffering system that loads only the relevant part from flash onto the display window and as you scroll it writes it out to some temporary space if anything is changed. It would allow you to have programs larger than the ram size. |
||||
MicroBlocks![]() Guru ![]() Joined: 12/05/2012 Location: ThailandPosts: 2209 |
In my opinion it will be far less messy and more intuitive when you can load cfunction into flash first. :) Also the chance to accidentally break them by having a single character changed is avoided. Editing a program will also be more easy as the part that can not be edited anyway is not there. Adding memory to a chip is impossible,so the next best thing is to optimize the use of memory. I see cfunction as an extension to MMBasic. If compared to an operating system, cfunctions can be seen as 'drivers'. Very usefull as has been proven, it enables the uMite to do almost anything imaginable. Leveraging that functionality will open up lots of possibilities. The uMite will still work as easy as it is now. More advanced use can be a little more difficult. That is how people learn. Having a library statement also opens the way for loading basic subroutines and functions into flash. [code] LIBRARY ADD SUB MySub(a,b) print a+b END LIBRARY ADD FUNCTION MyFunction$(name) MyFunction$ = "Hello + name END [/code] going with that thought a cfunction would then be loaded into flash with: [code] LIBRARY ADD CFUNCTION 00000000 12345678 12345678 12345678 12345678 END [/code] So basically you take your source add the LIBRARY ADD to your current subroutine, functions and cfunctions and they are transfered to flash. Your editing experience will be better as the final versions of the subroutines and function are made 'read only'. Microblocks. Build with logic. |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |