;---------------------------------------------------------------
;                     ZX81 AY SOUND ROUTINE
;                    BY ZEDDY-BR  *  01/2006
;
; Sintax:   PRINT USR 16516,R1,D1,R2,D2,R3,D3,...
;           Where Rn is an AY register and Dn is
;           data to be writen in that register
;---------------------------------------------------------------

#define db .byte      ; TASM cross-assembler definitions
#define dw .word
#define ds .block
#define org .org
#define end .end


; compile with: tasm -80 -b sound.asm sound.p

        org     $4009

;= System variables ============================================

        db      0               ;VERSN
        dw      0               ;E_PPC
        dw      dfile           ;D_FILE
        dw      dfile+1         ;DF_CC
        dw      var             ;VARS
        dw      0               ;DEST
        dw      var+1           ;E_LINE
        dw      last-1          ;CH_ADD
        dw      0               ;X_PTR
        dw      last            ;STKBOT
        dw      last            ;STKEND
        db      0               ;BERG
        dw      membot          ;MEM
        db      0               ;not used
        db      2               ;DF_SZ
        dw      10              ;S_TOP
        db      $FF,$FF,$FF     ;LAST_K
        db      55              ;MARGIN
        dw      line10          ;NXTLIN
        dw      0               ;OLDPPC
        db      0               ;FLAGX
        dw      0               ;STRLEN
        dw      $0C8D           ;T_ADDR
        dw      0               ;SEED
        dw      $FFFF           ;FRAMES
        db      0,0             ;COORDS
        db      $BC             ;PR_CC
        db      33,24           ;S_POSN
        db      01000000B       ;CDFLAG
        ds      33              ;Print buffer
membot: ds      30              ;Calculatorīs memory area
        ds      2               ;not used

;= First BASIC line, asm code ==================================

line0:  db      0,0
        dw      line10-$-2
        db      $ea             ; REM
        ld      a,(hl)
        jp      sound
        nop
        nop
	db      $3f,$3d,$00,$38,$34,$3a,$33,$29
        db      $00,$17,$00,$27,$3e,$00,$3f,$2a
        db      $29,$29,$3e,$16,$27,$37
        db      $76
        db      $76

sound:  rst     20h             ;collect next character
        call    $0d92           ;evaluate a class-6 expression (AY register)
        rst     20h             ;collect next character
        call    $0d92           ;evaluate a class-6 expression (AY data)
        call    $0bf5           ;stack-to-bc routine (AY register in C, AY data in B)
        ld      a,c
        out     ($df),a         ;set AY register
        ld      a,b
        out     ($0f),a         ;write data to AY  
        rst     18h             ;collect a character
        cp      $1a             ;check for more parameters
        jr      z,sound
        ld      sp,($4002)      ;set SP from ERR_SP, skiping the PRINT routine
        ret
 
        db      $76             ;N/L

line10: db      0,10
        dw      dfile-$-2
        db      $f1                              ; LET
        db      $38,$34,$3a,$33,$29              ; SOUND
        db      $14,$1d,$22,$21,$1d,$20          ; =16514
        db      $7e                              ; FP mark
        db      $8f,$01,$04,$00,$00              ; FP number
        db      $76                              ;N/L

;- Display file --------------------------------------------

dfile:  db      $76
        db      $76,$76,$76,$76,$76,$76,$76,$76
        db      $76,$76,$76,$76,$76,$76,$76,$76
        db      $76,$76,$76,$76,$76,$76,$76,$76

;- BASIC-Variables ----------------------------------------

var:    db      $80

;- End of program area ----------------------------

last:

        end

