bits 16
%ifdef BOOTSECT
org 7c00h
%else
org 100h
%endif
NUM_POINTS equ 8192
SHADOW_OFFS equ 5
%macro SETPAL 4
push word %4
push word %3
push word %2
push word %1
call setpal
add sp, 8
%endmacro
start:
%ifdef BOOTSECT
xor ax, ax
mov ds, ax
mov es, ax
mov ss, ax
jmp 00:.setcs
.setcs:
mov sp, 0
%endif
mov ax, 13h
int 10h
mov ax, es
add ax, 1000h
mov es, ax
SETPAL 80h, 40, 40, 80
SETPAL 81h, 16, 16, 32
SETPAL 82h, 96, 192, 96
.mainloop:
call animate
mov eax, 80808080h
mov ecx, 16000
xor di, di
rep stosd
push word SHADOW_OFFS
push word 81h
call drawsierp
mov bp, sp
mov word [bp + 2], 0
mov word [bp], 82h
call drawsierp
add sp, 4
push ds
push es
mov ax, es
mov ds, ax
xor si, si
mov ax, 0a000h
mov es, ax
xor di, di
mov ecx, 16000
call wait_vblank
rep movsd
pop es
pop ds
%ifdef BOOTSECT
jmp .mainloop
%else
in al, 60h
dec al
jnz .mainloop
mov ax, 3
int 10h
ret
%endif
wait_vblank:
mov dx, 3dah
.inblank:
in al, dx
and al, 8
jnz .inblank
.notblank:
in al, dx
and al, 8
jz .notblank
ret
drawsierp:
mov bp, sp
mov ax, [sierp_verts]
mov bx, [sierp_verts + 2]
mov [sierp_pt], ax
mov [sierp_pt + 2], bx
mov cx, NUM_POINTS
.loop:
call rand
mov bx, 3
xor dx, dx
div bx
mov edi, sierp_verts
movzx ebx, dx
lea ebx, [ebx * 4 + edi]
mov ax, [bx]
add ax, [sierp_pt]
shr ax, 1
mov [sierp_pt], ax
mov di, ax
mov ax, [bx + 2]
add ax, [sierp_pt + 2]
shr ax, 1
mov [sierp_pt + 2], ax
add ax, [bp + 4]
mov bx, ax
shl ax, 8
shl bx, 6
add bx, ax
add bx, [bp + 4]
mov al, [bp + 2]
mov byte [es:bx + di], al
dec cx
jnz .loop
ret
align 2
sierp_verts:
dw 160, 40
dw 240, 160
dw 80, 160
sierp_pt dw 0, 0
sierp_vel:
dw 1, 1
dw -1, 1
dw -1, -1
animate:
mov cx, 3
mov di, sierp_verts
mov si, sierp_vel
.loop:
mov ax, [di]
add ax, [si]
jl .xout
cmp ax, 320-SHADOW_OFFS
jge .xout
jmp .skip_xflip
.xout:
sub ax, [si]
neg word [si]
.skip_xflip:
mov [di], ax
add di, 2
add si, 2
mov ax, [di]
add ax, [si]
jl .yout
cmp ax, 200-SHADOW_OFFS
jge .yout
jmp .skip_yflip
.yout:
sub ax, [si]
neg word [si]
.skip_yflip:
mov [di], ax
add di, 2
add si, 2
dec cx
jnz .loop
ret
setpal:
mov bp, sp
mov dx, 3c8h
mov al, [bp + 2]
out dx, al
inc dx
mov al, [bp + 4]
shr al, 2
out dx, al
mov al, [bp + 6]
shr al, 2
out dx, al
mov al, [bp + 8]
shr al, 2
out dx, al
ret
rand:
mov eax, [randval]
mul dword [randmul]
add eax, 12345
and eax, 0x7fffffff
mov [randval], eax
shr eax, 16
ret
align 4
randval dd 0
randmul dd 1103515245
%ifdef BOOTSECT
times 510-($-$$) db 0
db 0x55,0xaa
%endif