# Porting a BBC Micro galaxy program to the CoCo

Over in the Facebook CoCo group, Carlos Comacho shared a link to a cool online emulator for the BBC Micro. The link contains a BASIC program listing that drew a very cool galaxy graphic.

Here is the page:

https://bbcmic.ro/?t=9db8C

Here is the BBC Micro program:

`10 MODE120 VDU530 FORG=RND(-64)TO40040 t=RND(99):q=RND(99)50 u=RND(1280):v=RND(1024)60 A=RND(1)*370 R=90/(1+RND(200))80 Q=1+R*(.5+RND(1)/2)90 a=1+3*RND(1)^2100 M=1110 IFRND(9)<4Q=R:t=0:q=0:A=0:M=PI/3:a=1120 C=(1+3*RND(1)^2)*R*R130 FORi=0TOC140 S=-LNRND(1)150 T=i*M160 U=S*R*SINT170 V=S*Q*COST180 T=S*A190 X=U*COST+V*SINT200 Y=V*COST-U*SINT210 D=(X*X+Y*Y)/(R*R+Q*Q)220 Z=99*((2.7^-D)+.1)230 Z=Z*(RND(1)-.5)^3240 y=Y*COSt+Z*SINt250 Z=Z*COSt-Y*SINt260 x=u+X*COSq+y*SINq270 y=v-X*SINq+y*COSq280 P=POINT(x,y)+a290 IFP>3P=3300 GCOL0,P310 PLOT69,x,y320 NEXT,330 REM`

I decided to try converting it to the CoCo. This led me to looking up some BBC Micro Commands to understand what they did. I found a few significant differences:

• All variables need to be uppercase to work on the CoCo.
• PI is not a constant on the CoCo, so it needs to be defined.
• Line 10 – MODE1 sets a 320×256 4 color screen. On the CoCo 1/2, the 4-color screen has a resolution of 128×191.
• Line 20 – VDU5 – Not used on the CoCo. It outputs a screen character of 5 for some reason.
• Line 30 – RND(-64) would seed the random number generator, but on the BBC Micro it returns the -64. On the CoCo, RND(-64) looks like it does the same as RND(0) on the CoCo, returning a value from 0.0 to .9999. The FOR/NEXT will just have to do -64 for the start number.
• Line 80, 90, 120, 140 and 230 – Their RND(1) returns a number from 0.0 to .99999. This will be changed to RND(0) on the CoCo.
• Line 110 and 290 – The CoCo needs a THEN between IF X>4 THEN DO SOMETHING.
• Line 160,170, 190, 200, 240, 250, 260 and 270 – SIN and COS need parens on the CoCo.
• Line 280 – POINT looks like PPOINT on the CoCo.
• Line 290 – If the program was written for a 4-color “MODE1” display, I assume their colors are 0-3. On the CoCo, they are 1-4, so that is a minor change.
• Line 300 – GCOL0,P sets a color, so probably COLOR P.
• Line 310 – PLOT69,X,Y becomes PSET(X,Y)
• Line 320 – Their BASIC has an interesting shortcut for NEXT.â€‚After a FOR A you need a NEXT A, but you can also just say “NEXT” and it uses the most recent FOR. If you have nested loops, like FOR A :FOR B … you can end with “NEXT:NEXT” or “NEXT B,A”. It looks like their BASIC allows leaving out the variables, but using the comma. I tried this on CoCo, and it didn’t work. That would have been a fun discovery. That just means “NEXT,” becomes “NEXT:NEXT”.

When I did a quick port, I saw it passing negative values to PPOINT and PSET, so I had to add an extra check that skips plotting anything that would be off the screen (X <0 or >255, and Y<0 or >191).

My attempt at a CoCo 1/2 version looks like this:

`0 'BBC MICRO - 320X256	4 COLOUR DISPLAY1 POKE 65395,05 PI=3.14159265358979323810 'MODE111 PMODE 4,1:PCLS:SCREEN 1,1:PMODE 3,120 'VDU530 'FORG=RND(-64)TO40031 FORG=-64TO40040 T=RND(99):Q=RND(99)50 U=RND(1280):V=RND(1024)60 A=RND(1)*370 R=90/(1+RND(200))80 'Q=1+R*(.5+RND(1)/2)81 Q=1+R*(.5+RND(0)/2)90 'A=1+3*RND(1)^291 A=1+3*RND(0)^2100 M=1110 'IFRND(9)<4Q=R:T=0:Q=0:A=0:M=PI/3:A=1111 IFRND(9)<4THENQ=R:T=0:Q=0:A=0:M=PI/3:A=1120 'C=(1+3*RND(1)^2)*R*R121 C=(1+3*RND(0)^2)*R*R130 FORI=0TOC140 'S=-LNRND(1)141 S=-(LOG(RND(0)) / 0.4342944819)150 T=I*M160 'U=S*R*SINT161 U=S*R*SIN(T)170 'V=S*Q*COST171 V=S*Q*COS(T)180 T=S*A190 'X=U*COST+V*SINT191 X=U*COS(T)+V*SIN(T)200 'Y=V*COST-U*SINT201 Y=V*COS(T)-U*SIN(T)210 D=(X*X+Y*Y)/(R*R+Q*Q)220 Z=99*((2.7^-D)+.1)230 'Z=Z*(RND(1)-.5)^3231 Z=Z*(RND(0)-.5)^3240 'Y=Y*COST+Z*SINT241 Y=Y*COS(T)+Z*SIN(T)250 'Z=Z*COST-Y*SINT251 Z=Z*COS(T)-Y*SIN(T)260 'X=U+X*COSQ+Y*SINQ261 X=U+X*COS(Q)+Y*SIN(Q)270 'Y=V-X*SINQ+Y*COSQ271 Y=V-X*SIN(Q)+Y*COS(Q)275 IF X<0 OR X>255 THEN 321276 IF Y<0 OR Y>191 THEN 321280 'P=POINT(X,Y)+A281 P=PPOINT(X,Y)+A290 'IFP>3P=3291 IFP>3THENP=3300 'GCOL0,P301 COLORP310 'PLOT69,X,Y311 PSET(X,Y)320 'NEXT,321 NEXT:NEXT330 REM340 GOTO 340`

Unfortunately, these changes were not enough.

Can you help?