 ;-----------------------------\
 ;escape 4k demo                 \
 ;made for code craft contest      \
 ;code - tom/kulture 1999            \
 ;------------------------------------ \

 ;Data creator--------------------------------------------------------------|

 ;r0 - points to free memory at the end of the code

 adr r0,bss

 ;*****************************************************************
 ;Lookup table for the tunnel plotter
 ;255*255*4
 ;1/SQR(dx^2+dy^2)<<8 + angle*(256/360)*57.3

 ;57.3*255/360 to convet radians to 0->255
 ldfs f4,f_deg
 ;for some reason the fp cant cope with mov f0,#16384...
 ldfs f5,f_16384

 mov r14,#-64
.y_tlook
 flts f1,r14
 mufs f3,f1,f1
 mov r13,#-64
.x_tlook
 ;Calculate angle of x,y
 flts f0,r13
 cmf f0,#0
 polnes f2,f0,f1
 mufnes f2,f2,f4
 fixne r1,f2
 moveq r1,#64

 ;Calculate 1/sqr(....)
 mufs f2,f0,f0
 adfs f2,f2,f3
 sqts f2,f2
 cmf f2,#0
 frdnes f2,f2,#0.5
 mufnes f2,f2,f5
 fixne r2,f2
 moveq r2,#1<<17

 ;and store
 and r1,r1,#255
 orr r1,r1,r2,lsl #8
 str r1,[r0],#4

 add r13,r13,#1
 cmp r13,#64
 bne x_tlook

 add r14,r14,#1
 cmp r14,#64
 bne y_tlook

 ;*****************************************************************
 ;Radiuses for tunnel
 ;256*4
 ;Radius for each angle...

 ;the circle
 str r0,radius_circle
 mov r1,#64
 mov r2,#256
.radius_c
 str r1,[r0],#4
 subs r2,r2,#1
 bne radius_c

 ;flower
 str r0,radius_flower
 mov r2,#0
 ldfs f1,f_rad4
 ldfs f2,f_128
.radius_f
 flts f0,r2
 mufs f3,f0,f1
 coss f3,f3
 mufs f3,f2,f3
 fix r1,f3
 eor r1,r1,r1,asr #31 ;mvnlt r1,r1
 add r1,r1,#64
 str r1,[r0],#4
 add r2,r2,#1
 cmp r2,#256
 bne radius_f

 ;triangle
 str r0,radius_triangle
 mov r2,#256 ;angle
 mov r3,#(255/360)*150 ;theta
 ldfs f1,f_128
 ldfs f2,f_s30
 ldfs f3,f_rad
.radius_t
 flts f0,r3
 mufs f0,f3,f0
 sins f0,f0
 cmf f0,#0
 dvfnes f0,f1,f0
 mufnes f0,f2,f0
 fixne r1,f0
 moveq r1,#128
 str r1,[r0],#4
 subs r3,r3,#1
 cmp r3,#(255/360)*30
 moveq r3,#(255/360)*150 ;theta
 subs r2,r2,#1
 bne radius_t


 ;*****************************************************************
 ;Cos
 ;256*4+256*4
.cosine
 str r0,cos
 mov r1,r0
 add r0,r0,#256*4
 mov r2,#0
 ldfs f1,f_rad
 ldfs f2,f_16384
.coslook
 flts f0,r2
 mufs f0,f1,f0
 coss f0,f0
 mufs f0,f2,f0
 fix r3,f0
 str r3,[r0],#4
 str r3,[r1],#4
 add r2,r2,#1
 cmp r2,#256
 bne coslook

 ;*****************************************************************
.division
 ;Division
 ;256*4
 str r0,div
 mov r1,#0
 str r1,[r0],#4
 mov r2,#1
 ldfs f1,f_16384
.divlook
 flts f0,r2
 frds f0,f0,#1
 mufs f0,f1,f0
 fix r1,f0
 str r1,[r0],#4
 add r2,r2,#1
 cmp r2,#256
 bne divlook
 b ef

 ;they have to be here so they are in ldr range...
.f_rad
 dcfs (360/255)/57.3
.f_rad4
 dcfs 4*(360/255)/57.3
.f_rad64
 dcfs (360/64)/57.3
.f_deg
 dcfs 57.3*(255/360)
.f_s30
 dcfs SIN(30/57.3)
.f_16384
 dcfs 16384
.f_128
 dcfs 128
.f_16
 dcfs 16

.ef
 ;*****************************************************************
 ;make a 64*64 texture
 ;Choose the random texture...
 mov r14,r0
 swi OS_ReadMonotonicTime
 ands r0,r0,#1
 beq t2

.t1
 mov r0,r14
 str r0,tex
 ldfs f2,f_128
 ldfs f3,f_16
 mov r14,#64
.t_yloop1
 sub r1,r14,#32
 flts f1,r1
 dvfs f1,f1,f3
 mufs f1,f1,f1
 mov r13,#64
.t_xloop1
 sub r1,r13,#32
 flts f0,r1
 dvfs f0,f0,f3
 mufs f0,f0,f0
 adfs f0,f0,f1
 coss f0,f0
 mufs f0,f0,f2
 fix r1,f0
 adds r1,r1,#128
 movlt r1,#0
 cmp r1,#256
 movge r1,#255
 strb r1,[r0],#1
 subs r13,r13,#1
 bne t_xloop1
 subs r14,r14,#1
 bne t_yloop1

 b t_end
.t2
 mov r0,r14
 str r0,tex
 ldfs f2,f_128
 ldfs f3,f_16
 ldfs f4,f_rad64
 mov r14,#64
.t_yloop2
 flts f1,r14
 mufs f1,f1,f4
 coss f1,f1
 mov r13,#64
.t_xloop2
 flts f0,r13
 mufs f0,f0,f4
 coss f0,f0
 mufs f0,f0,f1
 mufs f0,f0,f2
 fix r1,f0
 adds r1,r1,#128
 movlt r1,#0
 cmp r1,#256
 movge r1,#255
 strb r1,[r0],#1
 subs r13,r13,#1
 bne t_xloop2
 subs r14,r14,#1
 bne t_yloop2

.t_end
 ;*****************************************************************
 ;calculate bumpmap from texture
 ;64*64*4
 str r0,bump
 ldr r12,tex
 mov r14,#0
.b_yloop
 mov r13,#0
.b_xloop
 sub r1,r13,#1
 and r1,r1,#63
 add r2,r13,#1
 and r2,r2,#63
 sub r3,r14,#1
 and r3,r3,#63
 add r4,r14,#1
 and r4,r4,#63
 ;
 ;left
 add r5,r1,r14,lsl #6
 ldrb r5,[r12,r5]
 ;right
 add r6,r2,r14,lsl #6
 ldrb r6,[r12,r6]
 ;up
 add r7,r13,r4,lsl #6
 ldrb r7,[r12,r7]
 ;down
 add r8,r13,r3,lsl #6
 ldrb r8,[r12,r8]

 sub r5,r5,r6
 sub r7,r7,r8
 adds r5,r5,#64
 movlt r5,#0
 cmp r5,#128
 movgt r5,#128
 adds r7,r7,#64
 movlt r7,#0
 cmp r7,#128
 movgt r7,#128

 orr r5,r5,r7,lsl #8
 str r5,[r0],#4

 add r13,r13,#1
 cmp r13,#64
 bne b_xloop
 add r14,r14,#1
 cmp r14,#64
 bne b_yloop

 ;calculate environment map
 ;128*128
 str r0,env
 mov r14,#128
.e_yloop
 sub r2,r14,#64
 mul r1,r2,r2
 flts f0,r1
 mov r13,#128
.e_xloop
 sub r2,r13,#64
 mul r1,r2,r2
 flts f1,r1
 adfs f1,f1,f0
 sqts f1,f1
 mufs f1,f1,#4
 fix r1,f1
 rsbs r1,r1,#255
 movlt r1,#0
 strb r1,[r0],#1
 subs r13,r13,#1
 bne e_xloop
 subs r14,r14,#1
 bne e_yloop

 str r0,free
.pal
 ;turn palette to blue...
 ldr r14,free
 mov r0,#19
 mov r1,#0
 mov r2,#16
 mov r3,#0
.pmloop_b
 strb r0,[r14],#1
 strb r1,[r14],#1
 strb r2,[r14],#1
 strb r3,[r14],#1
 strb r3,[r14],#1
 strb r1,[r14],#1
 add r1,r1,#1
 cmp r1,#128
 bne pmloop_b
.pmloop_w
 strb r0,[r14],#1
 strb r1,[r14],#1
 strb r2,[r14],#1
 sub r3,r1,#128
 mov r3,r3,lsl #1
 strb r3,[r14],#1
 strb r3,[r14],#1
 strb r1,[r14],#1
 add r1,r1,#1
 cmp r1,#256
 bne pmloop_w

 ldr r0,free
 mov r1,#256*6
 swi OS_WriteN

 ;the end
 b data_end

