Wednesday, May 25, 2022

Lorentz Attractor in BASIC

In case you couldn't tell, I was on a BASIC kick recently.  Another port I posted to facebook.

"This BASIC code represents the Lorentz attractor for Commodore 64."

Here is a port of one I got from a Commodore group. The original C64 code is there but with REM in front of the lines that changed. Delete lines 1-4, lines 1000 on, and change the POKE to 65495 to run this on a CoCo 1/2.



0 POKE65497,0:PCLEAR4:PMODE 4,1:PCLS:SCREEN 1,0
1 AD=VAL("&HFA0C")
2 FORI=0 TO 64:READ B$:A=VAL("&H"+B$)
3 POKE AD+I,A:NEXT
REM $BB00 JMP $FA0C
4 POKE VAL("&HBB02"),VAL("&H7E"):POKE VAL("&HBB03"),VAL("&HFA"):POKE VAL("&HBB04"),VAL("&H0C")
REM 10 rem skydda basic
REM 20 poke 56,32:poke 52,32
REM 30 rem pixelgrafik
REM 40 clr:poke 53272,25
REM 50 poke 53265,peek(53265) or 32
REM 60 print "{clear}"
REM 70 rem rensa befintliga pixlar
REM 80 poke 88,0:poke 89,63
REM 90 poke 113,64:poke 114,31
REM 100 poke 12,1:sys 45760
110 rem lorenz
120 x=5
130 y=5
140 z=5
150 t=0
REM 160 s=1/200
160 s=1/192
170 d=10
180 r=28
190 b=8/3
200 t=t+0.1
210 dx=d*(y-x)
220 x1=x+dx*s
230 dy=(r*x-y)-x*z
240 y1=y+dy*s
250 dz=x*y-b*z
260 z1=z+dz*s
270 x=x1
280 y=y1
290 z=z1
300 rem position
REM 310 xx=150+4*x:yy=20+3*z
310 xx=120+4*x:yy=10+3*z
320 rem rita pixel
REM 330 row=int(yy/8):ch=int(xx/8)
REM 340 line=yy and 7:bit=7-(xx and 7)
REM 350 byte=8192+row*320+ch*8+line
REM 360 poke byte,peek(byte) or 2^bit
330 PSET(XX,YY)
370 rem upprepa
380 if t<1000 goto 200
1000 DATA 32,79,E7,60,96,60,3D,ED,63,E6,60,96,5E,3D,ED,61,E6,60,96,5D
1001 DATA 3D,ED,65,E6,60,96,5F,3D,E3,62,ED,62,EC,65,E9,61,89,00,ED,60
1002 DATA EC,63,D3,15,97,16,D7,63,EC,61,D9,14,99,13,DD,14,A6,60,89,00
1003 DATA 97,13,32,67,39

 

Burning Ship Fractal in BASIC

Another port I recently posted on facebook.
It's a fractal program in BASIC I ported to the Color Computer. 
Stuff like this doesn't take long to port and it's kinda fun to play with.


REM SET COCO3 TO HIGH SPEED MODE, SET UP A PMODE4 SCREEN, AND INSTALL THE MULTIPLY PATCH
0 POKE65497,0:PCLEAR4:PMODE 4,1:PCLS0:SCREEN 1,0:GOSUB10000
1 G=130:A=0:B=1:C=2:D=3:E=4:F=105:P=255:Q=191:S=50:T=27:H=90:M=16:R=0
2 U=28672:V=32:W=28:L=29:N=30:Z=7:Z2=1/1000:G2=8:G3=1.7:G4=.06
3 G5=1/8:G6=63:FORI=R TOP:X=I*Z2-G3
REM FOR I = 0 TO 255
4 FORI=R TOP:X=I*Z2-G3
REM FOR J=0 TO 192
8 FORJ=R TOQ
9 Y=J*Z2-G4:A=U+INT(I*G5)+V*(J AND G6)
REM 12 PRESET(I,J):RE=R:IM=R:K=R
12 RE=R:IM=R:K=R
20 TM=RE*RE-IM*IM+X:IM=ABS(C*RE*IM)+Y:RE=TM:K=K+B
30 IFK<M AND RE*RE+IM*IM<E THEN20
40 IFK>=M THEN50
45 PSET(I,J)
50 NEXTJ,I
60 GOTO60
REM COCO3 HARDWARE MULTIPLY PATCH.
REM JUST ADD A GOSUB 10000 AT THE TOP OF YOUR CODE TO ADD IT
REM ADDRESS WE ARE STORING THE MULTIPLY PATCH IN RAM
10000 AD=VAL("&HFA0C")
REM POKE THE MULTIPLY PATCH INTO RAM
10001 FORI=0 TO 64:READ B$:A=VAL("&H"+B$):POKE AD+I,A:NEXT
REM MAKE BASIC JUMP TO OUR MULTIPY INSTEAD OF USING IT'S CODE. $BB00 JMP $FA0C
10002 POKE VAL("&HBB02"),VAL("&H7E"):POKE VAL("&HBB03"),VAL("&HFA"):POKE VAL("&HBB04"),VAL("&H0C")
10003 RETURN
10004 DATA 32,79,E7,60,96,60,3D,ED,63,E6,60,96,5E,3D,ED,61,E6,60,96,5D
10005 DATA 3D,ED,65,E6,60,96,5F,3D,E3,62,ED,62,EC,65,E9,61,89,00,ED,60
10006 DATA EC,63,D3,15,97,16,D7,63,EC,61,D9,14,99,13,DD,14,A6,60,89,00
10007 DATA 97,13,32,67,39



And with a slightly better configuration.



Change this line:
2 U=28672:V=32:W=28:L=29:N=30:Z=7:Z2=1/1250:G2=8:G3=1.8:G4=.1

Z2 = initial zoom level
g3 = initial X image landscape position value
g4 = intiial Y screen-position value

BASIC Fourier example

The header on this was left intact to give credit to the original author Hal Chamberlin, and Allen Murphey who ported it to the Tandy Color Computer.  I made significant optimizations to the code such as putting constants in variables, only printing the minimum text needed, adding the hardware multiply patch, math logic optimizations, etc... which lets the code run in about half the original time.  I also added an improved graph of the data that simply draws lines between the data points to make the waveform look better.  The original version is posted on facebook in the 'I'm a CoCO Nut' group about a year ago (April 2021). 

The code looks a bit cryptic after the optimizations, but converting constants from ASCII to floating point is time consuming, and the interpreter has to do it every time a constant is used.

This update flips the graph to account for the difference between the standard X/Y coordinate layout which starts in the lower left corner with Y increasing as it goes up, and the screen layout where coordinates start in the upper left corner with Y increasing as it goes down the screen.  The multiply patch code is now grouped with the data to make it easier to add to other programs.

This is the video I made last year that compared the original code to the optimized code.  It does not show the flip in the Y axes.




100  REM FIG. 13-7 PROGRAM TO OPTIMALLY FILL WAVEFORM TABLES
110  REM MUSICAL APPLICATIONS OF MICROPROCESSORS
120  REM COPYRIGHT 1985 BY HAL CHAMBERLIN AND HAYDEN BOOKS
130  REM SEE TABLE FILLING BY FOURIER SERIES, PAGE 432
140  REM HTTPS://ARCHIVE.ORG/DETAILS/MUSICALAPPLICATI00CHAM
150  REM COCO EXTENDED COLOR BASIC MODIFICATIONS
160  REM BY R. ALLEN MURPHEY
170  REM FOR HTTP://DISCORD.COCOTALK.LIVE #ORCH90-COCODAC-16 CHANNEL
180  REM YOU PROBABLY WANT THIS POKE &HFFD9,0
1000 REM PROGRAM TO UTILIZE THE FOURIER SERIES TO FILL A WAVEFORM TABLE
1010 REM ARRAY A HOLDS THE AMPLITUDES OF THE HARMONICS
1020 REM ARRAY P HOLDS THE PHASES OF THE HARMONICS
1030 REM THE ZEROTH ELEMENT OF A AND P CORRESPONDS TO THE DC COMPONENT
1040 REM VARIABLE N HOLDS THE NUMBER OF THE HIGHEST HARMONIC TO PROCESS
1050 REM VARIABLE M HOLDS THE SIZE OF THE TABLE TO GENERATE
1060 REM VARIABLE Q IF 0 SELECTS RANDOM PHASES, IF 1 USES ARRAY P
1070 REM ARRAY T IS THE GENERATED TABLE WITH ALL ENTRIES BETWEEN BUT NOT
1080 REM INCLUDING -1 AND +1

REM 1090 DIM T(256):I=10:J=I

1090 I=10:J=I
1140 N=I
1150 M=256
1160 Q=1
2 Z=0:O=1:J=Z:I=Z:DIM T(256):A1=Z:A2=Z:S=6.28318:G=.99999:V=96:H=32:P=13
1200 REM SET UP THE AMPLITUDE AND PHASE ARRAYS

1210 FOR I=Z TO N:READ A(I):NEXT

1240 FOR I=Z TO N
1250     IF Q=Z THEN P(O)=RND(O):NEXT:GOTO 1300
1260     READ P(I)
1280 NEXT

1300 REM MAIN LOOP TO COMPUTE PRELIMINARY TABLE CONTENTS
1301 REM AND SCAN RESULTING TABLE FOR MAXIMUM ABSOLUTE VALUE

1305 F=S/M
1306 PRINT@Z,"SIN GEN STEP:     OF 255";
1310 FOR I=Z TO M-O
1315     PRINT@P,I;:T(I)=Z:A1=I*F
1340     FOR J=Z TO N:T(I)=T(I)+A(J)*COS(J*A1+S*P(J)):NEXT
1365    E=ABS(T(I)):IF E > A2 THEN A2=E
1370 NEXT

1500 REM NORMALIZE THE TABLE
1505 F=O/A2*G:FOR I=Z TO M-O:T(I)=T(I)*F:NEXT

1600 REM ADD CODE HERE TO OUTPUT THE TABLE IN SUITABLE FORM
1700 PMODE 4,1:PCLS:SCREEN 1,1
REM A2-# CONVERTS FROM STANDARD COORDINATE LAYOUT TO SCREEN LAYOUT
1730 FOR I=O TO M-O:LINE(I,V+INT(A2-T(I-O)*H))-(I,V+INT(A2-T(I)*H)),PSET:NEXT

1760 A$=INKEY$
1770 IF A$="" THEN 1760
1800 END



REM COCO3 HARDWARE MULTIPLY PATCH BY JAMES DIFFENDAFFER.  
REM JUST ADD A GOSUB 10000 AT THE TOP OF YOUR CODE TO ADD IT
REM ADDRESS WE ARE STORING THE MULTIPLY PATCH IN RAM

0 POKE 65497,0:CLS:GOSUB10000

10000 AD=VAL("&HFA0C")
REM POKE THE MULTIPLY PATCH INTO RAM
10001 FORI=0 TO 64:READ B$:A=VAL("&H"+B$):POKE AD+I,A:NEXT
REM MAKE BASIC JUMP TO OUR MULTIPY INSTEAD OF USING IT'S CODE.  $BB00 JMP $FA0C
10002 POKE VAL("&HBB02"),VAL("&H7E"):POKE VAL("&HBB03"),VAL("&HFA"):POKE VAL("&HBB04"),VAL("&H0C")
10003 RETURN
10004 DATA 32,79,E7,60,96,60,3D,ED,63,E6,60,96,5E,3D,ED,61,E6,60,96,5D
10005 DATA 3D,ED,65,E6,60,96,5F,3D,E3,62,ED,62,EC,65,E9,61,89,00,ED,60
10006 DATA EC,63,D3,15,97,16,D7,63,EC,61,D9,14,99,13,DD,14,A6,60,89,00
10007 DATA 97,13,32,67,39


REM ORIGINAL DATA FROM THE TOP OF THE CODE MOVED
REM TO ACCOUNT FOR THE MULTIPLY PATCH DATA

11000 REM AMPLITUDE DATA
11010 DATA 0,.8,.6,.2,.55,1.0,.7,.3,.2,.1,.05
11020 REM PHASE DATA
11030 DATA 0,0,.2,.4,.7,.45,.1,.5,.85,.9,0