; Title - Happy Birthday Music
; By    - Adam Trionfo
; Rev   - 1.0 - August 26, 2011
; 
; About this Program
; ------------------ 
;
; This plays the "Happy Birthday" song.  The notes aren't quite right
; and the timing is wrong.  How about giving me a birthday present
; and helping to make this sound right? 
;
; To assemble Z-80 source code using ZMAC:
;  
; zmac -i -m -o birthday.bin -x birthday.lst birthday.asm

MUSSTK  EQU     $4F12            ; Music Stack Pointer

        INCLUDE "HVGLIB.H"

        ORG     $2000            ; First byte of Cartridge

        DB      "U"              ; User System Sentinel

        DW      MENUST           ; Next menu link
        DW      PRGNAM           ; Address of title for program
        DW      PRGST            ; Jump here if prog is selected

; Main Program
PRGST:  DI
        SYSTEM  (INTPC)
        ;
        DO      (SETOUT)         ; UPI - Set Display Ports 
        DB      90*2             ; ... Vertical Blanking Line (Line 90)
        DB      44               ; ... Left/Right Color Boundary
        DB      $08              ; ... Interrupt Mode
        ;
        DO      (COLSET)         ; UPI - Set Color Registers
        DW      COLTAB           ; ... Color Table
        ;
        DO      (FILL)           ; UPI - Screen Fill
        DW      NORMEM           ; ... Destination
        DW      4000D            ; ... Bytes to move
        DB      $00              ; ... Background color
        ;
        DO      (STRDIS)         ; UPI - Display String
        DB      10               ; ... X coordinate
        DB      30               ; ... Y coordinate
        DB      01101000B        ; ... Options (2x X-OR Write in Color 2)
        DW      STRING           ; ... Address of string to display
        ;
        DO     (ACTINT)          ; UPI - ACTivate sub timer INTerrupts
        ;
        DO      (BMUSIC)         ; UPI - Begin Playing "Happy Birthday" Music
        DW      MUSSTK           ; ... Music Stack
        DB      11000000B        ; ... Setup Voices, Use Tone A
        DW      BDAY             ; ... Music Score for "Happy Birthday"
        ;
        EXIT

LOOP:   NOP
        JP      LOOP             ; Infinite loop

; Color Table #1
COLTAB: DB      $00              ; Color 3 Left  - Black
        DB      $5A              ; Color 2 Left  - Red
        DB      $77              ; Color 1 Left  - Yellow
        DB      $07              ; Color 0 Left  - White
        DB      $00              ; Color 3 Right - Black
        DB      $5A              ; Color 2 Right - Red
        DB      $77              ; Color 1 Right - Yellow
        DB      $07              ; Color 0 Right - White

PRGNAM: DB      "HAPPY BIRTHDAY      (120 BPM)",0

STRING: DB      "  HAPPY    BIRTHDAY!",0

; "Happy Birthday" Music Score 
; 1/8 Note = 15          1/2 Note  = 60
; 1/4 Note = 30          Full Note = 120
BDAY:   
; First Six Notes - Okay        
        MASTER  OA1              ; Master Oscillator (User HVGLIB.H Note Value)
        VOLUME  $0A, $00         ; Sets Volumes TONE A = 10, Tone B = 0, and Tone C = 0
        NOTE1   15,G5            ; Duration = 1/60'th of a Sec, TA=
        LEGSTA                   ; Turn on LEGATO (slur)
        NOTE1   15,G5            ; 1/8 Note 
        NOTE1   30,A5            ; 1/4 Note
        NOTE1   30,G5            ; 1/4 Note 
        NOTE1   30,C4            ; 1/4 Note - ERROR, This sounds WRONG
        MASTER  OA3              ; Master Oscillator - Freq Needed to get B5
        NOTE1   60,B3            ; 1/2 Note

        MASTER  OA1              ; Master Oscillator (User HVGLIB.H Note Value)
        VOICEM  11110000B        ; ... Setup Voices, Use Tones A and B
        VOLUME  $AA,$00          ; Sets Volumes, Tone A = 10, Tone B = 10, and Tone C = 0
        LEGSTA                   ; Turn off LEGATO (slur), Turn on STACATO
        NOTE2   30,F4,G4
        VOICEM  11000000B        ; ... Setup Voices, Use Tones A
        VOLUME  $0A,$00          ; Sets Volumes TONE A = 10, Tone B = 0, and Tone C = 0
        NOTE1   15,G5            ; Duration = 1/60'th of a Sec, TA=
        LEGSTA                   ; Turn off STACATO, Turn on LEGATO (slur)
        NOTE1   15,G5            ; Duration = 1/60'th of a Sec, TA=
        NOTE1   30,A5            ; 1/4 Note          
        NOTE1   30,G5            ; 1/4 Note
        NOTE1   30,D4            ; 1/4 Note
        NOTE1   30,C4            ; 1/4 Note

        VOICEM  11110000B        ; Setup Voices, Use Tones A and B
        VOLUME  $AA,$00          ; Sets Volumes, Tone A = 10, Tone B = 10, and Tone C = 0
        LEGSTA                   ; Turn off LEGATO (slur), Turn on STACATO
        NOTE2   30,E4,G4         ; 1/4 Note
        VOICEM  11000000B        ; Setup Voices, Use Tones A
        VOLUME  $0A,$00          ; Sets Volumes TONE A = 10, Tone B = 0, and Tone C = 0
        NOTE1   15,G5            ; 1/8 Note
        LEGSTA                   ; Turn off STACATO, Turn on LEGATO (slur)
        NOTE1   15,G5            ; 1/4 Note
        NOTE1   30,G4            ; 1/4 Note               
        NOTE1   30,E4            ; 1/4 Note
        NOTE1   30,C4            ; 1/4 Note
        MASTER  OA3              ; Master Oscillator - Freq Needed to get B5
        NOTE1   30,B3            ; Play a B5, 1/4 Note
        MASTER  OA1              ; Master Oscillator (User HVGLIB.H Note Value)
        NOTE1   35,A5            ; Play a LITTLE longer than a 1/4 note

; Fix this part (Next two notes - not set up right)
        LEGSTA                   ; Turn off LEGATO (slur), Turn on STACATO
        VOICEM  11110000B        ; ... Setup Voices, Use Tones A and B
        VOLUME  $AA,$00          ; Sets Volumes, Tone A = 10, Tone B = 10, and Tone C = 0
        NOTE2   15,F4,A5
        VOLUME  $0A,$00          ; Sets Volumes TONE A = 10, Tone B = 0, and Tone C = 0
        NOTE2   15,F4,A5         ; A5 is near silent here


; This part is right
        LEGSTA                   ; Turn off STACATO, Turn on LEGATO (slur)
        VOLUME  $AA,$00          ; Sets Volumes, Tone A = 10, Tone B = 10, and Tone C = 0
        NOTE2   30,E4,G5         ; 1/4 Note
        VOLUME  $0A,$00          ; Sets Volumes TONE A = 10, Tone B = 0, and Tone C = 0
        NOTE2   30,C4,G5         ; G5 is near silent here (E4 plays for 1/2 Note)
        VOLUME  $AA,$00          ; Sets Volumes, Tone A = 10, Tone B = 10, and Tone C = 0
        MASTER  OA3              ; Master Oscillator - Freq Needed to get B5
        NOTE2   60,B3,D2         ; Play a B5 and a D4
        MASTER  OA1              ; Master Oscillator (User HVGLIB.H Note Value)

; WRONG

        VOICEM  11000000B        ; Setup Voices, Use Tones A
        VOLUME  $0A,$00          ; Sets Volumes TONE A = 10, Tone B = 0, and Tone C = 0
        NOTE1   70,C4            ; Play a LITTLE longer than a 1/2 note
        LEGSTA                   ; Turn off LEGATO (slur), Turn on STACATO
        VOICEM  11110000B        ; Setup Voices, Use Tones A and B
        NOTE2   40,E4,G4         ; Play a E4 and a G4        
        QUIET