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