# Twister Effect in HIRES

A TWISTER is an oldschool demo effect showing an overlay of different sinus waves which gives the impression of a winding spiral in 3D. The task was to implement that effect on a standard Apple II with 48k RAM @1MHz CPU speed.

I am not a great AppleSoft coder but I sometimes like using AppleSoft for rapid prototyping of effects.

I searched my code dump space and found some AppleSoft code snippets which I wrote back in the days to see how a TWISTER effect might look like in HGR and what parameters I can play with to modify the appearance of the effect.

The small AppleSoft code below draws a TWISTER. It is of course pretty slow at 1 MHz but using an emulator at full speed allows to study the effect when modifying the algorithm parameters:

`````` 110 REM TWISTER-DEMO
220 HGR
330 FOR A2 = 1 TO 360 STEP 3
440 AM = 120+COS(A2*0.0174533)*100
550 AN = -3.141593+SIN(A2*0.0174533)*3.141593
660 FOR Y = 1 TO 159 STEP 2
765 FV = Y/AM+AN
870 X1 = 35*SIN(FV)
980 X2 = 35*SIN(FV+1.570796)
1090 REM X3 = 140+35*SIN(FV+3.141593)
1195 X3 = -X1
12100 REM X4 = 140+35*SIN(FV+4.712389)
13105 X4 = -X2
14108 HCOLOR = 0: HPLOT 105,Y TO 175,Y
15110 IF X1 < X2 THEN HCOLOR = 1: HPLOT 140+X1,Y TO 140+X2,Y: GOTO 130
16120 IF X3 < X4 THEN HCOLOR = 1: HPLOT 140+X3,Y TO 140+X4,Y
17130 IF X2 < X3 THEN HCOLOR = 2: HPLOT 140+X2,Y TO 140+X3,Y: GOTO 150
18140 IF X4 < X1 THEN HCOLOR = 2: HPLOT 140+X4,Y TO 140+X1,Y
19150 NEXT Y
20160 NEXT A2
21170 GOTO 30``````

# Implementation in Assembly

The main task after playing around with the AppleSoft mockup was to transform the AppleSoft code into assembly language and make the algorithm as fast as possible.

The basic idea was to reduce needed CPU cycles by only drawing each second line which halves the required CPU cylces. Furthermore the boundary points of the sine wave overlays for each animation frame are precalculated during the setup procedure of the algorithm and stor4d in data tables in memory.

The effect uses 64 animation frames and hence requires 64 memory pages of 256 bytes as table data resulting in a 16k data table. Each data set for a line contains four 1-byte values representing the boundary points of the four sine waves. Hence a dataset of 256 byte can hold the data for 64 lines. Since we are plotting only every second line on the screen the height of the effect is 128 lines which is nice with a total of 192 screen lines in HIRES.

The calculation of the sine waves requires a sine table and a fast table based multiplication routine which was re-used from the 3D-Engine. The pre-calculation of the boundary points requires about a second and can be perceived from the black screen before the twister is displayed. Auto-generating the table data keeps the codse size of the effect small (the code resides below HIRES page 1 from \$0A00 on) and setting up the boundary tables is rather fast.

Evaluating theboundary tables and drawing the TWISTER is done by using a modified rasterfill algorithm from the 3D-Engine which was derived from Andy McFadden's fdraw-routine package.

With this general framework a fast algorithm can be set up. The following video shows an implementation in assembler at 1MHz with 30fps: