; tunnel256: a flight down a tunnel of light, for DOS. ; ; Target: DOS .COM, 386+ with an x87, VGA mode 13h (320x200, 256 colours). ; Build: nasm -f bin tunnel.asm -o tunnel.com ; Run: DOSBox with cycles=max. Esc leaves. ; ; Every pixel asks the FPU two questions: which way is it from the centre ; (fpatan) and how deep into the tunnel it sits (k / distance). The answers ; become texture coordinates, scrolled by the frame counter in BP. bits 16 cpu 386 org 0x100 start: fninit mov ax, 0x0013 ; 320x200, 256 colours int 0x10 push word 0xA000 pop es cld ; --- palette: an amber ramp, then a cyan one, twice over --------------------- xor cx, cx palette: mov dx, 0x3C8 mov al, cl out dx, al ; colour index inc dx ; 0x3C9 takes red, green, blue and al, 63 ; level mov ah, al shr ah, 2 ; level / 4 mov bl, ah add bl, ah add bl, ah ; 3 * level / 4 test cl, 64 jz .amber xchg al, ah ; cyan is amber with red and blue swapped .amber: out dx, al ; red mov al, bl out dx, al ; green mov al, ah out dx, al ; blue inc cl jnz palette ; --- one frame ---------------------------------------------------------------- frame: xor di, di pixel: mov ax, 0xCCCD ; di * 0.8: DH = row, DL = column * 0.8 mul di sub dh, 100 ; centre the row sub dl, 128 ; centre the column movsx ax, dh mov [y], ax movsx ax, dl mov [x], ax fild word [y] fild word [x] fpatan ; angle around the axis, -pi..pi fmul dword [k_angle] fistp word [u] fild word [y] fmul st0, st0 fild word [x] fmul st0, st0 faddp st1, st0 fsqrt ; distance from the centre fdivr dword [k_depth] ; depth = k / distance fistp word [v] mov cx, [v] ; fog: the far end fades to black shr cx, 3 cmp cx, 63 jbe .near mov cl, 63 .near: neg cl add cl, 63 mov ax, [u] add ax, bp ; spin mov bx, [v] add bx, bp add bx, bp ; fly forward, twice as fast xor al, bl ; bit 6 picks the ramp, bits 0-5 the level mov ch, al and ch, 64 and al, 63 mul cl ; shade by fog shr ax, 6 or al, ch stosb cmp di, 64000 jb pixel inc bp ; next frame in al, 0x60 ; Esc has scancode 1 dec al jnz frame ret k_angle: dd 40.743665 ; 128 / pi k_depth: dd 2048.0 section .bss x: resw 1 y: resw 1 u: resw 1 v: resw 1