+; disp palette
+ mov ax, 0a000h ; video segment points to video memory
+ mov ds, ax
+ mov bx, 0 ; video offset
+ mov cx, 0 ; y
+.y_loop:
+ mov dx, 0 ; x
+.x_loop:
+ mov ax, dx
+ test ax, 0ff00h ; lower 8 bits 0, highest 1, Z flag is set while < 256, test = and but doesnt change ax
+ jz .skip_clear_ax
+ mov al, 0
+.skip_clear_ax:
+ mov [bx], al ; pixel written on screen
+ inc bx ; increment
+ inc dx
+ cmp dx, 320
+ jnz .x_loop
+ inc cx
+ cmp cx, 200
+ jnz .y_loop
+
+; setup grayscale palette (64 first colors because ramdac uses 6 bits / color so 2^6)
+; ram dac (digital to analog converter) tis vga
+; in a ^ register which index we want to write
+; 3 writes in another ramdac register (data)
+; ramdac feature: when you need to set a palette
+ mov al, 0 ; first index
+ mov dx, 3c8h ; ramdac index registe
+ out dx, al ; because io port (address) > 255
+ inc dx ; ramdac data register: 3c9h
+.pal_loop:
+ out dx, al ; r
+ out dx, al ; g
+ out dx, al ; b
+ inc al
+ test al, 3fh ; test with 3fh to keep the lowest 6 bits of al
+ jnz .pal_loop
+
+; setup lovebug palette
+ SETPAL 64, 0, 255, 0 ; chr(64) = @
+ SETPAL 65, 0, 0, 0 ; A
+ SETPAL 66, 50, 50, 50 ; B
+ SETPAL 67, 57, 4, 4 ; C
+ SETPAL 68, 108, 9, 9 ; D
+ SETPAL 69, 164, 4, 4 ; E
+
+; disable all interrupts (for bios to not read the keyboard)
+ cli
+
+ ; ds on 0 because rand uses it for accesses to mem
+ xor ax, ax
+ mov ds, ax
+ mov ax, backbuffer
+ shr ax, 4 ; to use it as segment! (shift right)
+ mov es, ax
+main_loop:
+; draw to back buffer
+ xor bx, bx ; mov bx, 0 to clear the register
+.static_loop:
+ call rand
+ and ax, 3fh ; last six bits of eax (see rand)
+ mov [es:bx], al ; [] -> bx offset and default segment ds we change it to es
+ inc bx
+ cmp bx, 64000 ; num pixels
+ jnz .static_loop
+
+ ; draw lovebug
+ mov di, (200 - 32) * 320 + 160 - 16
+ mov ax, [num_frames]
+ shr ax, 2 ; /4
+ cmp ax, 200 - 32
+ jz .end
+ mov bx, ax
+ shl ax, 8
+ shl bx, 6
+ add ax, bx
+ sub di, ax
+ call rand
+ and ax, 3 ; random value in 0, 2
+ sub ax, 1 ; random value in -1, 1
+ add di, ax
+ mov si, lovebug
+ call blit32
+
+; wait for vblank
+.vsync_blank:
+ mov dx, 3dah
+ in al, dx
+ and al, 8
+ jnz .vsync_blank
+.vsync_visible:
+ in al, dx
+ and al, 8 ; the 4th bit is 1 when we are in the vertical blanking period
+ jz .vsync_visible
+
+; copy backbuffer to video memory
+ push es
+ push ds
+ mov ax, es
+ mov ds, ax ; ds points to backbuffer now (was in es)
+ xor si, si
+ mov ax, 0a000h
+ mov es, ax
+ xor di, di ; write in es:di
+ mov ecx, 16000 ; 16000 double words (dwords) = 64000 bytes
+ rep movsd
+ pop ds
+ pop es
+
+ inc word [num_frames]
+; moved above:
+; cmp word [num_frames], 200 - 32
+; jz .end
+
+ in al, 64h ; 60h = keyb data port, 64h = keyb status port
+ and al, 1 ; 1 = OUTBUF_FULL = the keyb controller out buf is full
+ jz main_loop ; no key pressed, loop back
+ in al, 60h ; reads the keyb that was pressed to reset the flag
+
+.end:
+; re-enable all interrupts
+ sti
+
+; text mode
+ mov ax, 3 ; text mode
+ int 10h
+; use bios to print
+; Int 10/AH=0Eh
+ mov si, str_frames
+ call print_str
+ mov ax, [num_frames]
+ call print_num
+ mov si, str_newline
+ call print_str
+
+ mov dx, 80h ; default to load from drive 80h
+ cmp byte [saved_drive_num], 80h
+ jnz .not_hd
+ inc dl ; next hd
+.not_hd:
+; load hard disk boot sector
+ xor ax, ax
+ mov es, ax
+ mov bx, 7c00h
+ mov ah, 02h
+ mov al, 1
+ mov ch, 0
+ mov cl, 1
+ mov dh, 0
+ int 13h
+ jnc 7c00h