start-microcode videocopy

\ COMM+0 window with in chars
\ COMM+1 window height in chars
\ COMM+2 audio read pointer

: 1+   d# 1 + ;
: @     dup c@ swap 1+ c@ swab or ;

\ 3F00-3FFF sound buffer
: audio
    FREQTICK c@
    dup COMM+2 c!
    h# 3f00 + c@
    dup SAMPLE_Lhi c! SAMPLE_Rhi c!
;

: move ( src dst cnt -- src' )
    over + >r
    begin
        over c@ over c!
        swap 1+ swap 1+
        dup d# 15 and d# 0 = if audio then
        dup r@ =
    until
    r> drop drop
;

: 8*    d# 8 * ;
: 16*   d# 16 * ;

: window ( src -- )
    d# 0 swap       ( y src )
    begin
        over d# 64 * COMM+0 c@ move
        swap d# 1 + swap
        over COMM+1 c@ =
    until
;

: fetch1 ( src -- src' v )
    dup 1+ swap 
    c@
;

: main
    d# 8000 FREQHZ c!

    begin
        audio
        YLINE c@ d# 256 =
    until
    h# 4000 fetch1 >r
    fetch1 COMM+0 c!
    d# 1 COMM+15 c!
    fetch1 COMM+1 c!
    RAM_CHR r@ 16* move
    RAM_PAL r@ 8* move
    window
    begin audio again
;

end-microcode
