|
Forum Index : Microcontroller and PC projects : Bubble universe on steroids
| Author | Message | ||||
| Bleep Guru Joined: 09/01/2022 Location: United KingdomPosts: 808 |
Hi Peter, this sounds very interesting to me, I've often thought, a CSub here, would speed this up dramatically, but when I've looked at how it's all done, eventually gave up. :-( So being able to write a CSub in C, and get it all correctly formatted etc. to use as a CSub would be great. :-) I'll look forward to trying it out. Regards Kevin. PS. I have tested it and sure enought it runa at over 42Hz screen refreshes! or nearly 44Hz with Trace Cache, which now makes very little difference compaired to the massive gain of using assembled C in the tight loop. Edited 2026-06-21 04:37 by Bleep |
||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 11516 |
Try this version in mode 2 (or mode 5) with OPTION RESOLUTION 378 on rc22 just posted ' bubble.bas - "bubble universe" demo driven by the bubblerow CSUB (bubble.c). ' The CSUB does the sin/cos recurrence + scale + offset for one row (66 points), ' returning integer screen coordinates in c() and d(). ' ' Regenerate the CSUB block below with: ' python armcfgen.py bubble.c --compile -n bubblerow -e bubblerow -O s -I <firmware dir> Option tracecache on Font 7 FRAMEBUFFER create FRAMEBUFFER write f Dim Float t,pf(15) ' pf: 0=i 1=b 2=v 3=x 4=pi/2 5=xs 6=ys 7=xc 8=yc Dim Integer c(65),d(65),n(65),m(32,195),nn ' m: 196 palette rows (one per i step) Dim Integer a,g,i,j,xc,yc,xs,ys Const r=(2*Pi)/235,k=255,s=50 CLS RGB(black) t=Rnd*10 nn=Peek(varaddr n()) ' centre and scale factor xc=MM.HRES\2:yc=MM.VRES\2 xs=MM.HRES/4.2:ys=MM.VRES/4.0 ' Oval pf(2)=0:pf(3)=0:pf(4)=Pi/2 ' v=0, x=0, half-pi pf(5)=xs:pf(6)=ys:pf(7)=xc:pf(8)=yc ' build the colour palette, packed 32-bit into m() For a=0 To 195 For g=0 To 65 If a<18 And g<18 Then n(g)=RGB(0,255,0) Else n(g)=RGB(a*1.3,g*3.93,128*(a+g<65)) EndIf Next 'g Memory pack nn,Peek(varaddr m(0,a)),66,32 Next 'a Do CLS Inc t,0.035:g=0:Print Timer:Timer =0 For i=60 To 255 Step 1 ' Step 1 = densest; raise to 2/3 for fewer points pf(0)=i:pf(1)=r*i+t bubblerow c(),d(),pf() ' recurrence + scale + offset (in C) Memory unpack Peek(varaddr m(0,g)),nn,66,32 Pixel c(),d(),n() Inc g Next 'i FRAMEBUFFER copy f,n Loop CSub bubblerow INTEGER, INTEGER, FLOAT 00000000 0014B5F0 68126853 920CB09B 68A2930D 920E68E3 6922930F 92026963 69A29303 920469E3 6A229305 92066A63 6AA29307 92106AE3 6B229311 92126B63 6BA29313 92146BE3 6C229315 92166C63 23009317 91199018 4D519301 990D980C 69DB682B 681E33A4 9B039A02 682B47B0 69DB0006 3390000F 4798681B 9008682B 69DB9109 001A0030 681B33A4 469C3290 46666812 92020039 9B079A06 9B0247B0 682B4798 91039002 99059804 339069DB 4798681B 900A682B 9804910B 69DB9905 33A4001A 68163290 9A06681F 47B89B07 682B47B0 91059004 99099808 33A469DB 9A0A681E 47B09B0B 9008682B 98029109 69DB9903 681E33A4 9B059A04 682B47B0 91039002 99099808 33A469DB 9A0E681E 47B09B0F 9004682B 98089105 69DB9909 3288001A 920A6812 33A0001A 681632A4 9A10681F 47B89B11 9B159A14 9B0A47B0 9A014798 189B9B18 60596018 99039802 69DB682B 33A0001A 68153288 681F69D6 9B139A12 9A1647B8 47B09B17 9A0147A8 189B9B19 60596018 22840013 93013308 42930092 E768D000 9A022000 21009B03 61636122 9B059A04 61E361A2 BDF0B01B E000ED08 End CSub /* bubble.c - "bubble universe" inner loop as a PicoMite CSUB. * * One frame row (66 points): the sin/cos recurrence + scale + offset, producing * integer screen coordinates in c() and d(). Moving this out of the BASIC * interpreter took the demo from 233 ms/frame to ~23 ms. * * Build (compute-heavy -> ask for -Os; -I points at PicoCFunctions.h): * python armcfgen.py bubble.c --compile -n bubblerow -e bubblerow -O s -I <firmware dir> * * Call from BASIC: * CSUB bubblerow INTEGER, INTEGER, FLOAT ' c(), d(), pf() * ... * pf(0)=i : pf(1)=r*i+t * bubblerow c(), d(), pf() * * pf(): 0=iang(i) 1=b(r*i+t) 2=v(state) 3=x(state) 4=pi/2 * 5=xs 6=ys 7=xc 8=yc (v,x persist in pf(2)/pf(3) across calls) * * Uses PicoCFunctions.h: all double maths goes through the firmware CallTable * (Sine/FAdd/FMul/FloatToInt), which the header locates at runtime via VTOR - so * no CallTable argument and no chip-/build-specific address, one blob for every * variant and both chips. (The per-call base re-read the wrappers do is lost in * the noise here - the software Sine calls dominate.) */ #include "PicoCFunctions.h" long long bubblerow(long long *c, long long *d, double *pf) { double iang = pf[0], b = pf[1], v = pf[2], x = pf[3], hp = pf[4]; double xs = pf[5], ys = pf[6], xc = pf[7], yc = pf[8]; int j; for (j = 0; j < 66; j++) { double A = FAdd(iang, v); double siv = Sine(A); double civ = Sine(FAdd(A, hp)); /* cos(i+v) = sin(i+v + pi/2) */ double sx = Sine(x); double cx = Sine(FAdd(x, hp)); /* cos(x) */ double uu = FAdd(siv, sx); v = FAdd(civ, cx); x = FAdd(uu, b); c[j] = FloatToInt(FAdd(FMul(uu, xs), xc)); d[j] = FloatToInt(FAdd(FMul(v, ys), yc)); } pf[2] = v; pf[3] = x; return 0; } |
||||
| Bleep Guru Joined: 09/01/2022 Location: United KingdomPosts: 808 |
That's a huge number of Pixies in 70mS nearly 13000, or 186400 per second!!! Thats not just plotting them, but doing 4 Sin calculations, & a few other 'simple' adds. Not bad for a £3 micro controler. |
||||
| toml_12953 Guru Joined: 13/02/2015 Location: United StatesPosts: 659 |
Here's a video of Bubble Universe running on PicoCalc. I, too, had to comment out the memory pack statements. PicoCalc Bubble Universe Edited 2026-06-21 06:49 by toml_12953 |
||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 11516 |
What firmware, what release, your build or mine? This is really bad as it suggests the linker is ignoring the alignment instruction |
||||
| phil99 Guru Joined: 11/02/2018 Location: AustraliaPosts: 3293 |
The first version runs well on a RP2040. > run "bubble universe 06-2026 v1.bas" 529175.835 179.787 179.632 179.611 Another data point for v2. Saved 2591 bytes > RUN [14] Dim Integer c(65),d(65),n(65),m(32,195),nn ' m: 196 palette rows (one per i step) Error : Not enough Heap memory > option list PicoMite MMBasic RP2040 V6.03.00RC22 Edited 2026-06-21 18:20 by phil99 |
||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 11516 |
The V2 result is just a simple lack of ram on the rp2040. Not related to the alignment issue |
||||
| toml_12953 Guru Joined: 13/02/2015 Location: United StatesPosts: 659 |
I used WebMite RP2350B V6.03.00RC22 (tried on both your build and mine) on a PicoCalc with a Pimoroni Pico2W Plus. |
||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 11516 |
Please list the exact full option list you are using and the exact version of the bubble program and try this WebMiteRP2350V6.03.00RC23.zip Edited 2026-06-21 21:12 by matherp |
||||
| toml_12953 Guru Joined: 13/02/2015 Location: United StatesPosts: 659 |
WebMiteRP2350V6.03.00RC23.zip Option List: WebMite MMBasic RP2350B Edition V6.03.00RC22 OPTION SERIAL CONSOLE COM1,GP0,GP1,BOTH OPTION SYSTEM SPI GP10,GP11,GP12 OPTION SYSTEM I2C GP6,GP7, SLOW OPTION BAUDRATE 19200 OPTION FLASH SIZE 16777216 OPTION LIBRARY_FLASH_SIZE 40000 OPTION COLOURCODE ON OPTION CASE UPPER OPTION TAB 8 OPTION DEFAULT COLOURS GREEN, BLACK OPTION KEYBOARD PICOCALC OPTION HEARTBEAT OFF OPTION PICO OFF OPTION CPUSPEED (KHz) 384000 OPTION LCDPANEL CONSOLE ,, FF00 OPTION DISPLAY 26, 40 OPTION LCDPANEL ST7365P, PORTRAIT,GP14,GP15,GP13 OPTION BACKLIGHT LCD 96 OPTION WIFI MySpectrumWiFie0-2G, **************, PicoCalc-1 OPTION TELNET CONSOLE ON OPTION GUI CONTROLS 50 OPTION SDCARD GP17, GP18, GP19, GP16 OPTION AUDIO GP26,GP27', ON PWM CHANNEL 5 OPTION RTC AUTO ENABLE OPTION MODBUFF ENABLE 192 OPTION PLATFORM PicoCalc OPTION PSRAM PIN GP47 Libraries: OPTION console none ON ERROR SKIP 2 CONST TRUE = -1 CONST FALSE = 0 SETPIN GP4,GP5,COM2 OPTION console screen SUB MM.STARTUP OPTION console none DRIVE "B:" OPTION console screen END SUB SUB MM.END PRINT "END PROGRAM" OPTION console screen END SUB SUB mm.prompt SETPIN GP4,GP5,COM2 PRINT ">"; END SUB SUB LLIST(fname$) FONT 8 OPTION console serial IF fname$="" THEN LIST all ELSE LIST all fname$ END IF PRINT :PRINT :PRINT :PRINT :PRINT PRINT :PRINT FONT 1 OPTION console screen END SUB SUB BEEP() PLAY tone 1000,1000:PAUSE 175 PLAY stop END SUB FUNCTION DET(n()) DET=MATH(m_determinant n()) END FUNCTION Edited 2026-06-21 23:24 by toml_12953 |
||||
| matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 11516 |
Please delete the library and OPTION RESET PICOCALC. Does the problem then go away? If it does slowly add changes back to identify what causes the problem. |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2026 |