4500: Replace <pre>-graph with nicer image.
[libguestfs-talks.git] / 2019-lightning-talk / pyrit.asm
1 ; Pyrit
2
3 ; a 256-byte intro by Rrrola <rrrola@gmail.com>
4
5 ; greets to everyone who's computer is too fast :)
6
7 ; This is loosely based on my intro 'Gem' (shown on Demobit),
8 ; but the code is much better.
9
10 ; Vector3: X right, Y down, Z forward.
11 ; On the FP stack it looks like {Y X Z} (Y is often used in comparisons).
12 ; In memory it looks like {Z X Y}, which saves a displacement byte.
13 ; (u'v) is the dot product: ux*vx + uy*vy + uz*vz.
14
15         xor ax,ax
16         xor bx,bx
17         mov cx,0ffh
18         mov di,-2
19 ;        mov ss,dx
20         mov sp,di
21         mov si,100h
22         mov bp,091ch
23
24 ; assume al=0 bx=0 sp=di=-2 si=0100h bp=09??h
25
26 ;Set video mode and earth+sky palette
27   dec di       ; u16[100h] = -20401, u16[10Ch] = -30515
28   mov al,13h
29   dec di       ; initial pixel_adr@di = -4
30
31 P:shr cl,1     ; B@cl = 0..8..31,31..0
32
33   int 10h      ; set video mode / color: bx=index dh=R ch=G cl=B
34
35   movsx cx,bl  ; 0..127,128..255 (palette index)
36   xor ch,cl    ; 0..127,127..0
37   mov cl,ch
38   mov ax,cx
39   mul ax       ; R@dh = 0..16..63,63..16..0
40
41   shr cx,1     ; G@ch = 0..63,63..0
42
43   inc bl       ; keep default color 0
44   js Q         ; R@dh = 0..63,63..16..0
45   xchg cl,dh   ; B@cl = 0..16..63,63..0
46 Q:mov ax,1010h
47   jnz P        ;bx=0 cx=0
48
49         ; Constants expected at a fixed address below.
50         mov ax,0b04fh
51         mov word[100h],ax
52         mov ax,04f13h
53         mov word[102h],ax
54         mov ax,0e9d0h
55         mov word[104h],ax
56         mov ax,010cdh
57         mov word[106h],ax
58         mov ax,0be0fh
59         mov word[108h],ax
60         mov ax,030cbh
61         mov word[10ah],ax
62         mov ax,088cdh
63         mov word[10ch],ax
64         mov ax,089e9h
65         mov word[10eh],ax
66
67 ;Each frame: Generate normals to p0..p11=[bp+200h,300h,...].
68 M:mov ax,0x4731 ; highest 9 bits: float32 exponent 1/256 (for T)
69                 ; lower byte = 2*number of rotations+1
70                 ; lowest 4 bits must be 0x1 for 'test cl,al'
71   mov dx,0xA000-10-20-20-4
72   mov es,dx    ; dx:bx = YX:XX = 0x9fca:0
73   pusha  ; adr:   -18 -16 -14 -12 -10  -8  -6  -4  -2
74          ; stack:  di  si  bp  sp  bx  dx  cx  ax   0
75          ; data:   -4 100 9??  -2  0  9fca {T/256}
76   mov cx,12
77 G:add bp,si    ; i@cx = 12...1; bp points to p[12-i]; carry=0
78   pusha
79
80 ;Generate 12 planes with unit normals.
81
82 ;  fld1         ; platonic dodecahedron: exact is atan((1+sqrt5)/2)=1.017rad
83   fld dword[di-2]    ;|t=T/256: morphing shape: cube, platonic12, rhombic12
84
85   fsincos
86
87   fldz               ;|a=0 b c (a*a+b*b+c*c = 1)
88 ;  fldlg2 ;irregular shape
89
90 N:test cl,al ;=1     ;|a b c
91   jnz K
92   fchs
93 K:fstp st3           ;|b c +-a (scramble so that all 12 planes are generated)
94   loop N  ;cl=0      ;|z x y
95
96 ;Do a bunch of slow rotations. z x y -> cx-sy cy+sx z
97
98 R:fstp st3           ;|x y z
99 Z:fld st1            ;|y x y z                     ;|x sy x cy z
100   fld dword[di-2]    ;|t=T/256
101   fsincos            ;|c=cos(t) s=sin(t) y x y z   ;|c s x sy x cy z
102   fmulp st4          ;|s y x cy z                  ;|s x sy cx cy z
103   fmulp              ;|sy x cy z                   ;|sx sy cx cy z
104   add al,0x7F ; loop 2x
105   jo Z
106   faddp st3          ;|sy cx cy+sx z
107   fsubp              ;|new.z=cx-sy .x=cy+sx .y=z
108   jc R       ; loop 24x
109
110 S:fstp dword[bp+si] ;[bp+100]=.z [bp+104]=.x [bp+108]=.y
111   sub si,di
112   jpo S
113
114   popa
115   loop G
116   popa
117
118 ; the visible pixels are A0000..AF9FF, I want X=0 Y=0 in the center
119 ;Each pixel: cx=T dx:bx=YX:XX(init=9fca:0) di=adr(init=-4)
120 X:inc dx       ; part of "dx:bx += 0x0000CCCD"
121 X2:
122   stosb
123
124   pusha        ; adr:     -18 -16 -14 -12 -10  -8  -6  -4  -2
125   fninit       ; stack:    di  si  bp  sp  bx  dx  cx  ax   0
126   mov bx,es    ; s16:  pixadr 100 9??  -2  ..X..Y  T result
127   mov di,-4 ;di = address of pushed ax
128
129 ;Compute ray direction.
130   fild word[BIG] ; store 28799 as a double, read as two floats
131   fst qword[bx]     ; t_front@float[bx] = 0, t_back@float[bx+4] = 6.879
132   fild word[di+4-9]
133   fild word[di+4-8]  ;|y=Y x=X z=BIG
134
135 ;Intersect the pyrite.
136   call GEM
137   popa         ; color -> pushed ax
138 ;  mov al,dl    ; show only palette
139
140 ;; Faster, but lower quality: draw each pixel twice.
141 ;  stosb
142 ;  add bx,0xCCCD; dx:bx = YXX += 0000CCCD
143 ;  adc dx,0
144
145   add bx,0xCCCD; dx:bx = YXX += 0000CCCD
146   jnc X2
147   jnz X        ; do 65536 pixels
148
149   in al,60h
150   dec ax  ; ah=0 (checkboard uses positive color indices)
151   loopnz M        ; T--
152 ;  ret          ; fallthrough
153
154 GEM:
155 ;Hit the pyrite.
156   xchg ax,cx   ; ax = T
157
158 ; Faster (+4 or +8 bytes): test the shape only in the center of the screen
159   add dh,dh
160   jo B
161   add dl,dl
162   jo B
163
164 ;Ray-plane intersection.
165 ;Find the front plane with maximum t and back plane with minimum t.
166 ; tf@[bx],    tb@[bx+4]      ray parameter t
167 ; pf@[bx+si], pb@[bx+4+si]   pointer to plane
168   mov cx,12  ; i@cx = 12...1
169 I:add bp,si    ; bp points to p[i]
170   fldlg2             ;|pd=0.301 y x z
171   fadd dword[bp+si]  ;|N=pd-(ro'p[i]) y x z  ; ro = 0 0 -1
172
173   push si      ; Dot product:
174 D:fld dword[bp+si]   ;|p[i].z ...
175   fmul st4           ;|rd.z*p[i].z ...
176   sub si,di    ; 100 104 108
177   jpo D              ;|(rd*p[i]).y .x .z N rd.y .x .z
178   pop si
179   faddp
180   faddp              ;|D=(rd'p[i]) N y x z
181
182 ;If we hit the plane from the front (D<0), update tf. Otherwise update tb.
183   push bx
184   fst dword[bp+di]; -> p[i].dot_rd (will be read later)
185   test [bp+di+2],sp ; sf=1 if we're in front of the plane
186   js FRONT
187   sub bx,di    ; bx = address of tf?tb
188 FRONT:         ; D<0:  if tf*D < N { tf=N/D; pf=current; }  maximalize tf
189   fld st0      ; D>=0: if tb*D < N { tb=N/D; pb=current; }  minimalize tb
190   fmul dword[bx]     ;|(tf?tb)*D D N y x z
191
192 ;;DosBOX-compatible FPU comparison, +5 bytes (+3 but we need ax)
193 ;  push ax
194 ;  fcomp st2          ;|D N y x z
195 ;  fnstsw ax
196 ;  sahf         ; cf = (tf?tb)*D < N
197 ;  pop ax
198 ;  jc NEXT
199
200   fcomip st2
201   jc NEXT
202
203 ;another alternative, +6 bytes
204 ;  fsub st2
205 ;  fstp dword[bp-8]
206 ;  test [bp-6],sp ; sf=1 if <0
207 ;  js NEXT
208
209
210   fdivr st1          ;|t=N/D N y x z
211   fst dword[bx] ; -> tf?tb
212   mov [bx+si],bp ; pf?pb = current
213 NEXT:
214   fcompp
215   pop bx             ;|y x z
216
217   mov dx,[bx+6]
218   cmp dx,[bx+2]  ; if tf>tb { no_hit: early exit }
219   jng B     ;si=100   ;|y x z
220
221   loop I
222
223 ;Reflect: reflect(i,n) = i - 2*n*(i'n)
224   mov bx,[bx+si] ; pf
225 Y:fld dword[bx+di]   ;|(rd'pf) rd.y .x .z  ; reads pf->dot_rd
226   fmul dword[bx+si]  ;|(rd'pf)*pf.z rd.y .x .z
227   fadd st0           ;|2*(rd'pf)*pf.z rd.y .x .z
228   fsubr st3          ;|R.z=rd.z-2*(rd'pf)*pf.z rd.y .x .z
229   sub si,di   ;100 104 108
230   jpo Y     ;si=10C  ;|(R=i-2*n(i'n)).y R.x R.z rd.y .x .z
231
232 ;Environment map: chessboard below, sky gradient above.
233 B:
234 ; Subtle highlight on the pyrite.
235   fld st0
236   fimul word[C]  ; 16993 (background) or 20036 (pyrit)
237   fistp dword[di]     ;|y x z
238   sar dword[di],22    ; if y>=-0.5 { chessboard } else { sky }
239   js E             ; the sky is just y (= y^2 after gamma)
240
241 ; Everything the same brightness. (-6 bytes)
242 ;  fist word[di]      ;|y x z
243 ;  sar word[di],8  ; if y>=-0.5 { chessboard } else { sky }
244 ;  js E            ; the sky is just y (= y^2 after gamma)
245
246 ; Dark background version.
247 ;  fist word[di]      ;|y x z
248 ;  shld cx,si,16-3
249 ;  xor cl,9        ; hit?8:9 - make the background darker
250 ;  sar word[di],cl ; if y>=-0.5 { chessboard } else { sky }
251 ;  js E            ; the sky is just y (= y^2 after gamma)
252
253   fidivr word[si]    ;|C/y x z (C = hit?-30515:-20401)
254   fmul st1,st0
255   fmul st2           ;|u=z*C/y v=x*C/y z
256
257   fistp dword[bp+di]
258   sub al,[bp+di+1]
259   fistp dword[bp+di]
260   xor al,[bp+di+1] ; xortex@ax = (T-u) XOR v
261
262 ;  aam -32-24     ; more interesting floor texture
263   and al,9<<3
264   add al,10<<3    ; tex = (xortex AND 0b1001) + 10 [10|11|18|19]
265   mul byte[di]
266   mov [di],ah     ; pushed al = tex*y
267
268 E:ret
269
270 BIG     dw 28799
271 C       dw 16993
272 C2      dw 20036