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

Thursday, October 14, 2021

Some 6502 disassembly for Apple II fans

This code is from an article in COMPUTE! magazine I was pointed to when I tried porting some BASIC raytracing code to the Apple II.  It's not quite what I needed for that, but it does support some things I added to the latest MC-10 ROM I've been working on.
It took a couple hours to reverse engineer it back to source code. I didn't look up the ROM calls or BASIC tokens, but for the most part I thought that was obvious.  Since I haven't used the 6502 in a while there may be a couple syntax issues, but it should be easy to fix if there are.

One possible change is the comment related to P.PWR.  That may be the current DATA pointer.

Apple enhancer file maker

10 FOR I = 768 TO 887: READ A : POKE I,A:CK = CK + A: NEXT
20 IF CK < > 14482 THEN PRINT"ERROR IN DATA STATEMENTS.": STOP
30 PRINT CHR$ (4)"BSAVE APPLE.ENHANCER,A$300,L$77"
40 DATA 169,76,141,245,3,169,16,141
50 DATA 246,3,169,3,141,247,3,96
60 DATA 160,0,177,184,217,115,3,240
70 DATA 11,200,192,3,240,3,76,20
80 DATA 3,32,201,222,140,118,3,230
90 DATA 184,208,2,230,185,32,103,221
100 DATA 172,118,3,192,1,240,10,192
110 DATA 2,240,35,32,82,231,76,65
120 DATA 217,169,3,32,214,211,165,185
130 DATA 72,165,184,72,165,118,72,165
140 DATA 117,72,169,176,72,32,82,231
150 DATA 32,65,217,76,210,215,32,82
160 DATA 231,32,26,214,56,165,155,233
170 DATA 1,164,156,176,1,136,133,125
180 DATA 132,126,96,171,176,174,0,0




;******************************************
;* Program:
;*  APPLE.ENHANCER
;*  COMPUTE! ISSUE 72 / MAY 1986 / PAGE 75
;******************************************
;* Author:
;*  Mark Russinovich
;*
;* Disassembly:
;*  James Diffendaffer
;******************************************
;* Expands Applesoft BASIC to use a variable containing a line number as a parameter for the following keywords:
;* & GOTO
;* & GOSUB
;* & RESTORE
;******************************************

    ; code start
    org    $0300
    
    ; change the BASIC RAM hook to JMP to our patch
    lda    #$4c        ; opcode for jmp
    sta    $3f5
    lda    #<L0310        ; LSB of the patch entry point
    sta    $03f6
    lda    #>L0310        ; MSB of the patch entry point
    sta    $03f7
    rts                ; return after patching BASIC

; entry point for expanded BASIC functions
L0310:
    ldy    #$00        ; clear Y for no offset from pointer
    lda    (TXTPTRL),y    ; get current character pointed to by BASIC parser

    ; is it one of the tokens?
L0314:
    cmp    L0373,y
    beq    L0324        ; branch if it is
    iny                ; next address
    cpy    #$03        ; only loop 2 times
    beq    L0321        ; branch to Syntax Error
    jmp    L0314        ; loop
    
;    bne    L0314        ; loop  (replaces previous 2 lines)

L0321:
    jsr    SYNERR        ; exit with Syntax Error?  $DEC9
    sty    L0376        ; save
    inc    <TXTPTR        ; increment LSB of current program execution pointer
    bne    L032D
    inc    >TXTPTR        ; increment MSB of current program execution pointer
L032D:
    jsr    FRMNUM        ; jump to format number $DD67
    ldy    L0376        ; load TOKEN from temp
    cpy    #$01        ; GOSUB?
    beq    L0341        ;
    cpy    #$02        ; RESTORE?
    beq L035e
    jsr    GETADR        ; get address from variable
    jmp    $d941        ; call to ROM GOTO code following line # parse ?

; & GOSUB variable
L0341:
    lda    #$03        ; how much space we need on the stack
    jsr CHKMEM        ; is there enough memory?
    
    ; Does CHKMEM give an error or did this code just ignore it?

    ; push info for BASIC's RETURN from GOSUB on to stack
    ;
    ; push the current program parse pointer onto the stack
    lda    >TXTPTR        ; MSB
    pha
    lda    <TXTPTR        ; LSB
    pha
    ; push the current line number onto the stack
    lda    >CURLIN
    pha
    lda    <CURLIN
    pha
    ; push type of stack frame onto stack
    lda    #$b0        ; GOSUB token
    pha

L0355:
    ; get address from variable
    jsr    GETADR
    jsr    $D941        ;??
    jmp    NEWSTT        ; jump to new start address GOTO GOSUB

; & RESTORE variable or #
L035e:
    jsr    GETADR        ; get address
    jsr    FNDLIN        ; find the line
    sec
    lda    LOWTR

    sbc    #$01        ; decrement LOWTR
    ldy    EXPSGN        ; put EXPSGN
    bcs    L036e        ;
    dey
; save address returned in A & Y
L036e:
    sta    P.PWR
    sty    P.PWR+1
    rts
L0373:
    dcb    $AB            ; Applesoft GOTO token
L0374:
    dcb    $B0            ; Applesoft GOSUB token
L0375:
    dcb    $AE            ; Applesoft RESTORE token
L0376:
    $00,$00            ; variables

Wednesday, August 26, 2020

Article: Understanding Quake's Fast Inverse Square Root

This article pretty much confirms what I suspected, but the author explains it pretty well.
The only thing really missing is how the "magic number" was derived to minimize the error.
This is just speculation on my part, but I see two potential approaches.
The first would be to pick some mid point value that reduces the maximum error for any legal number (within the range of floating point numbers).
The second would be to pick a value that is the mid point for the most common values a program uses, thus minimizing the error for that program more than for any value.
The first would be easy to calculate, the 2nd would require a lot of number logging & crunching.
https://betterexplained.com/articles/understanding-quakes-fast-inverse-square-root/