extrn WaitRetrace:far

data segment public
  extrn sine:dataptr           ;sine table
data ends


code segment public
assume cs:code,ds:data

public make_wob
make_wob proc pascal wob_pos,wob_height,wob_offset:word
  xor cx,cx                     ;row counter to 0
  call waitretrace              ;synchronization with cathode ray

next_line:
  inc cx                        ;increment row counter

  mov bx,cx                     ;define position within the wobbler
  sub bx,wob_pos
  mov si,bx                     ;note for end

  add bx,wob_offset             ;offset for movement
  and bx,63                     ;allow only values from 0..63 (array size)
  shl bx,1                      ;array access to words
  mov bx,sine[bx]              ;get value in bx

  cli                           ;clear interrupts, because it's VERY time critical
  mov dx,3dah                   ;select input status register 1

in_display:
  in al,dx                      ;wait for (horizontal) retrace
  test al,1
  je in_display
in_retrace:
  in al,dx                      ;wait for display
  test al,1
  jne in_retrace

  cmp cx,wob_pos                ;reached desired line ?
  jb next_line                  ;no -> set default value

  mov dx,3d4h                   ;CRTC register 4 (horizontal sync start)
  mov al,4                      ;select
  mov ah,bl                     ;get sine value
  out dx,ax                     ;and enter

  cmp si,wob_height              ;end reached ?
  jb next_line

  mov dx,3dah
wait1:
  in al,dx                      ;wait for (horizontal) retrace
  test al,1
  jne wait1
  mov dx,3d4h                   ;set sync start back to normal
  mov ax,5504h
  out dx,ax
  sti                           ;allow interrupts again
  ret
make_wob endp

code ends
end
