;-----------------------------------------------------
; MAZURKA.ASM - By Claudio Fin - Gennaio 2001
; (http://stor.altervista.org/pic/pic.htm)
;-----------------------------------------------------
        PROCESSOR       16F84  ;4Mhz
        RADIX           DEC
        INCLUDE         "P16F84.INC"
        __CONFIG        3FF1H

;-----------------------------------------------------
; Definizione variabili del programma
;-----------------------------------------------------
        ORG     0CH
NCONT   RES     1       ;Numero note
PTAB    RES     1       ;Indice dell'elem. in tabella
RITAR   RES     2       ;Valore ritardo per onda quadra
RIPET   RES     2       ;Numero Ripetizioni
DRITAR  RES     2       ;Valore ritardo - 1
RDEL    RES     2       ;Valore ritardo per delay

;-----------------------------------------------------
; Inizio programma all'indirizzo 0.
;-----------------------------------------------------
        ORG     0

        goto    inizio   ;Salta le tabelle

;-----------------------------------------------------
; Tabelle note con 4 dati per nota:
;   0 - Low  byte ritardo onda quadra
;   1 - High byte ritardo onda quadra
;   2 - Low  byte numero ripetizioni
;   3 - High byte numero ripetizioni
;
;   Ritardo     = ( (1000000 / Hz) - 16 ) / 18
;   Ripetizioni = Secondi * Hz
;-----------------------------------------------------
; Ritornello 1
;-----------------------------------------------------
tabr1   clrf    PCLATH
        movf    PTAB,w
        addwf   PCL,f

        retlw   70        
        retlw   0        
        retlw   118
        retlw   0        

        retlw   83        
        retlw   0        
        retlw   99
        retlw   0        

        retlw   62        
        retlw   0        
        retlw   132
        retlw   0        

        retlw   83        
        retlw   0        
        retlw   99
        retlw   0        

        retlw   70        
        retlw   0        
        retlw   118
        retlw   0        

        retlw   83        
        retlw   0        
        retlw   99
        retlw   0        
;------------------------
        retlw   52        
        retlw   0        
        retlw   58
        retlw   1        

        retlw   52
        retlw   0
        retlw   157
        retlw   0

        retlw   83        
        retlw   0        
        retlw   99
        retlw   0        

        retlw   70
        retlw   0        
        retlw   235
        retlw   0
;------------------------
        retlw   70        
        retlw   0        
        retlw   118
        retlw   0        

        retlw   83        
        retlw   0        
        retlw   99
        retlw   0        

        retlw   62        
        retlw   0        
        retlw   132
        retlw   0        

        retlw   83        
        retlw   0        
        retlw   99
        retlw   0        

        retlw   70        
        retlw   0        
        retlw   118
        retlw   0        

        retlw   83        
        retlw   0        
        retlw   99
        retlw   0        
;------------------------
        retlw   70
        retlw   0        
        retlw   235
        retlw   0        

        retlw   70
        retlw   0        
        retlw   118
        retlw   0        

        retlw   74
        retlw   0        
        retlw   111
        retlw   0        

        retlw   79
        retlw   0        
        retlw   210
        retlw   0        
;------------------------
        retlw   55
        retlw   0        
        retlw   40
        retlw   1        

        retlw   55
        retlw   0        
        retlw   40
        retlw   1        

        retlw   62
        retlw   0        
        retlw   8
        retlw   1        
;------------------------
        retlw   62
        retlw   0        
        retlw   132
        retlw   0      

        retlw   66
        retlw   0        
        retlw   125
        retlw   0      

        retlw   70
        retlw   0        
        retlw   214
        retlw   1      
;------------------------
        retlw   70
        retlw   0        
        retlw   118
        retlw   0      

        retlw   74
        retlw   0        
        retlw   111
        retlw   0      

        retlw   79
        retlw   0        
        retlw   163
        retlw   1      
;------------------------
        retlw   79
        retlw   0        
        retlw   105
        retlw   0      

        retlw   83
        retlw   0        
        retlw   99
        retlw   0      

        retlw   105
        retlw   0        
        retlw   58
        retlw   1      
;------------------------
        retlw   70        
        retlw   0        
        retlw   118
        retlw   0        

        retlw   83        
        retlw   0        
        retlw   99
        retlw   0        

        retlw   62        
        retlw   0        
        retlw   132
        retlw   0        

        retlw   83        
        retlw   0        
        retlw   99
        retlw   0        

        retlw   70        
        retlw   0        
        retlw   118
        retlw   0        

        retlw   83        
        retlw   0        
        retlw   99
        retlw   0        
;------------------------
        retlw   52        
        retlw   0        
        retlw   58
        retlw   1        

        retlw   52
        retlw   0
        retlw   157
        retlw   0

        retlw   83        
        retlw   0        
        retlw   99
        retlw   0        

        retlw   70
        retlw   0        
        retlw   235
        retlw   0

;-----------------------------------------------------
tabr2   clrf    PCLATH
        movf    PTAB,w
        addwf   PCL,f

        retlw   70        
        retlw   0        
        retlw   118
        retlw   0        

        retlw   83        
        retlw   0        
        retlw   99
        retlw   0        

        retlw   62        
        retlw   0        
        retlw   132
        retlw   0        

        retlw   83        
        retlw   0        
        retlw   99
        retlw   0        

        retlw   70        
        retlw   0        
        retlw   118
        retlw   0        

        retlw   83        
        retlw   0        
        retlw   99
        retlw   0        
;------------------------
        retlw   70
        retlw   0        
        retlw   235
        retlw   0        

        retlw   70
        retlw   0        
        retlw   118
        retlw   0        

        retlw   74
        retlw   0        
        retlw   111
        retlw   0        

        retlw   79
        retlw   0        
        retlw   210
        retlw   0        
;------------------------
        retlw   55
        retlw   0        
        retlw   40
        retlw   1        

        retlw   55
        retlw   0        
        retlw   40
        retlw   1        

        retlw   62
        retlw   0        
        retlw   8
        retlw   1        
;------------------------
        retlw   62
        retlw   0        
        retlw   132
        retlw   0      

        retlw   66
        retlw   0        
        retlw   125
        retlw   0      

        retlw   70
        retlw   0        
        retlw   214
        retlw   1      
;------------------------
        retlw   55
        retlw   0        
        retlw   148
        retlw   0
    
        retlw   62
        retlw   0        
        retlw   132
        retlw   0
    
        retlw   83
        retlw   0        
        retlw   99
        retlw   0
    
        retlw   70
        retlw   0        
        retlw   118
        retlw   0

;-----------------------------------------------------
        ORG     100H

tabr3   movlw   1
        movwf   PCLATH
        movf    PTAB,w
        addwf   PCL,f

        retlw   79
        retlw   0        
        retlw   105
        retlw   0

        retlw   94
        retlw   0        
        retlw   88
        retlw   0

        retlw   105
        retlw   0        
        retlw   157
        retlw   0
    
        retlw   105
        retlw   0        
        retlw   157
        retlw   0
    
        retlw   2
        retlw   0        
        retlw   68
        retlw   22

;-----------------------------------------------------
; Ritornello 2
;-----------------------------------------------------
tabr4   movlw   1
        movwf   PCLATH
        movf    PTAB,w
        addwf   PCL,f

        retlw   83
        retlw   0
        retlw   198
        retlw   0

        retlw   52
        retlw   0
        retlw   58
        retlw   1

        retlw   55
        retlw   0
        retlw   40
        retlw   1
;------------------------
        retlw   62
        retlw   0
        retlw   132
        retlw   0

        retlw   66
        retlw   0
        retlw   125
        retlw   0

        retlw   62
        retlw   0
        retlw   132
        retlw   0

        retlw   83
        retlw   0
        retlw   99
        retlw   0

        retlw   62
        retlw   0
        retlw   132
        retlw   0

        retlw   70
        retlw   0
        retlw   118
        retlw   0
;------------------------
        retlw   79
        retlw   0
        retlw   105
        retlw   0

        retlw   83
        retlw   0
        retlw   99
        retlw   0

        retlw   79
        retlw   0
        retlw   105
        retlw   0

        retlw   70
        retlw   0
        retlw   118
        retlw   0

        retlw   62
        retlw   0
        retlw   132
        retlw   0

        retlw   79
        retlw   0
        retlw   105
        retlw   0
;------------------------
        retlw   83
        retlw   0
        retlw   81
        retlw   2
;------------------------
        retlw   83
        retlw   0
        retlw   198
        retlw   0

        retlw   79
        retlw   0
        retlw   210
        retlw   0

        retlw   83
        retlw   0
        retlw   198
        retlw   0
;------------------------
        retlw   66
        retlw   0
        retlw   125
        retlw   0

        retlw   83
        retlw   0
        retlw   99
        retlw   0

        retlw   55
        retlw   0
        retlw   148
        retlw   0

        retlw   70
        retlw   0
        retlw   118
        retlw   0

        retlw   46
        retlw   0
        retlw   96
        retlw   1
;------------------------
        retlw   83
        retlw   0
        retlw   198
        retlw   0

        retlw   79
        retlw   0
        retlw   210
        retlw   0

        retlw   83
        retlw   0
        retlw   198
        retlw   0
;------------------------
        retlw   62
        retlw   0
        retlw   132
        retlw   0

        retlw   83
        retlw   0
        retlw   99
        retlw   0

        retlw   52
        retlw   0
        retlw   157
        retlw   0

        retlw   62
        retlw   0
        retlw   132
        retlw   0

        retlw   41
        retlw   0
        retlw   140
        retlw   1
;------------------------
        retlw   83
        retlw   0
        retlw   198
        retlw   0

        retlw   52
        retlw   0
        retlw   58
        retlw   1

        retlw   55
        retlw   0
        retlw   40
        retlw   1
;------------------------
        retlw   62
        retlw   0
        retlw   132
        retlw   0

        retlw   66
        retlw   0
        retlw   125
        retlw   0

        retlw   62
        retlw   0
        retlw   132
        retlw   0

        retlw   83
        retlw   0
        retlw   99
        retlw   0

        retlw   62
        retlw   0
        retlw   132
        retlw   0

        retlw   70
        retlw   0
        retlw   118
        retlw   0
;------------------------
        retlw   79
        retlw   0
        retlw   105
        retlw   0

        retlw   83
        retlw   0
        retlw   99
        retlw   0

        retlw   79
        retlw   0
        retlw   105
        retlw   0

        retlw   70
        retlw   0
        retlw   118
        retlw   0

        retlw   62
        retlw   0
        retlw   132
        retlw   0

        retlw   79
        retlw   0
        retlw   105
        retlw   0
;------------------------
        retlw   83
        retlw   0
        retlw   81
        retlw   2
;------------------------
        retlw   83
        retlw   0
        retlw   198
        retlw   0

        retlw   79
        retlw   0
        retlw   210
        retlw   0

        retlw   83
        retlw   0
        retlw   198
        retlw   0
;------------------------
        retlw   66
        retlw   0
        retlw   125
        retlw   0

        retlw   83
        retlw   0
        retlw   99
        retlw   0

        retlw   55
        retlw   0
        retlw   148
        retlw   0

        retlw   70
        retlw   0
        retlw   118
        retlw   0

        retlw   46
        retlw   0
        retlw   96
        retlw   1

;-----------------------------------------------------
        ORG     200H

tabr5   movlw   2
        movwf   PCLATH
        movf    PTAB,w
        addwf   PCL,f

        retlw   79
        retlw   0
        retlw   105
        retlw   0

        retlw   83
        retlw   0
        retlw   99
        retlw   0

        retlw   88
        retlw   0
        retlw   93
        retlw   0

        retlw   83
        retlw   0
        retlw   99
        retlw   0

        retlw   52
        retlw   0
        retlw   157
        retlw   0

        retlw   55
        retlw   0
        retlw   148
        retlw   0
;------------------------
        retlw   62
        retlw   0
        retlw   8
        retlw   1

        retlw   62
        retlw   0
        retlw   8
        retlw   1

        retlw   2
        retlw   0
        retlw   68
        retlw   22

;-----------------------------------------------------
; Ritornello 3
;-----------------------------------------------------
tabr6   movlw   2
        movwf   PCLATH
        movf    PTAB,w
        addwf   PCL,f

        retlw   79
        retlw   0
        retlw   59
        retlw   1

        retlw   83
        retlw   0
        retlw   99
        retlw   0

        retlw   94
        retlw   0
        retlw   88
        retlw   0

        retlw   105
        retlw   0
        retlw   78
        retlw   0
;------------------------
        retlw   79
        retlw   0
        retlw   163
        retlw   1

        retlw   62
        retlw   0
        retlw   8
        retlw   1
;------------------------
        retlw   52
        retlw   0
        retlw   116
        retlw   2

        retlw   46
        retlw   0
        retlw   96
        retlw   1
;------------------------
        retlw   70
        retlw   0
        retlw   193
        retlw   2
;------------------------
        retlw   59
        retlw   0
        retlw   164
        retlw   1

        retlw   62
        retlw   0
        retlw   132
        retlw   0

        retlw   70
        retlw   0
        retlw   118
        retlw   0

        retlw   79
        retlw   0
        retlw   105
        retlw   0
;------------------------
        retlw   83
        retlw   0
        retlw   41
        retlw   1

        retlw   79
        retlw   0
        retlw   105
        retlw   0

        retlw   94
        retlw   0
        retlw   88
        retlw   0

        retlw   83
        retlw   0
        retlw   99
        retlw   0
;------------------------
        retlw   105
        retlw   0
        retlw   236
        retlw   0

        retlw   70
        retlw   0
        retlw   118
        retlw   0

        retlw   62
        retlw   0
        retlw   132
        retlw   0

        retlw   59
        retlw   0
        retlw   140
        retlw   0
;------------------------
        retlw   55
        retlw   0
        retlw   81
        retlw   2

        retlw   52
        retlw   0
        retlw   58
        retlw   1

        retlw   79
        retlw   0
        retlw   59
        retlw   1

        retlw   83
        retlw   0
        retlw   99
        retlw   0

        retlw   94
        retlw   0
        retlw   88
        retlw   0

        retlw   105
        retlw   0
        retlw   78
        retlw   0
;------------------------
        retlw   79
        retlw   0
        retlw   163
        retlw   1

        retlw   62
        retlw   0
        retlw   8
        retlw   1
;------------------------
        retlw   44
        retlw   0
        retlw   236
        retlw   2

        retlw   46
        retlw   0
        retlw   96
        retlw   1
;------------------------
        retlw   59
        retlw   0
        retlw   48
        retlw   2

        retlw   70
        retlw   0
        retlw   235
        retlw   0
;------------------------
        retlw   70
        retlw   0
        retlw   118
        retlw   0

        retlw   74
        retlw   0
        retlw   111
        retlw   0

        retlw   70
        retlw   0
        retlw   118
        retlw   0

        retlw   62
        retlw   0
        retlw   132
        retlw   0

        retlw   59
        retlw   0
        retlw   140
        retlw   0

        retlw   46
        retlw   0
        retlw   176
        retlw   0
;------------------------
        retlw   52
        retlw   0
        retlw   215
        retlw   1

        retlw   79
        retlw   0
        retlw   105
        retlw   0

        retlw   70
        retlw   0
        retlw   118
        retlw   0

        retlw   79
        retlw   0
        retlw   105
        retlw   0
;------------------------
        retlw   83
        retlw   0
        retlw   41
        retlw   1

        retlw   52
        retlw   0
        retlw   157
        retlw   0

        retlw   59
        retlw   0
        retlw   140
        retlw   0

        retlw   70
        retlw   0
        retlw   118
        retlw   0
;------------------------
        retlw   79
        retlw   0
        retlw   210
        retlw   0

        retlw   79
        retlw   0
        retlw   210
        retlw   0

        retlw   2
        retlw   0
        retlw   68
        retlw   22

;-----------------------------------------------------
; Inizio effettivo del programma.
; Vengono settate le porte A e B come ingressi,
; tranne il pin RB0 che e' un'uscita.
;-----------------------------------------------------
inizio  bsf     STATUS,RP0
        movlw   00011111B
        movwf   TRISA 
        movlw   11111110B
        movwf   TRISB 
        bcf     STATUS,RP0

        call    pl1
        call    pl2
        call    pl3

        call    pl1
        call    pl2
        call    pl3

        call    pl4
        call    pl5

        call    pl1
        call    pl2
        call    pl3

        call    pl6

        sleep
 
;-----------------------------------------------------
; Suona 42 note leggendo i dati dalla tabella tabr1
; NCONT e' il contatore di note
; PTAB e' l'indice puntatore alla tabella
; Vengono caricati RITAR e RIPET e chiamata la
; subroutine qemit.
;-----------------------------------------------------
pl1     movlw   42
        movwf   NCONT      ;42 note da suonare
        clrf    PTAB       ;Indice tabella a zero

play1   call    tabr1      ;Leggi valore
        movwf   RITAR      ;Low byte ritardo onda
        incf    PTAB,f     ;Punta all'elemento seguente

        call    tabr1
        movwf   RITAR+1    ;High byte ritardo onda
        incf    PTAB,f

        call    tabr1
        movwf   RIPET      ;Low byte ripetiz.
        incf    PTAB,f

        call    tabr1
        movwf   RIPET+1    ;High byte ripetiz.
        incf    PTAB,f

        call    qemit      ;Emissione onda quadra

        decfsz  NCONT,f        
        goto    play1
        return

;-----------------------------------------------------
; Suona 20 note leggendo i dati dalla tabella tabr2
;-----------------------------------------------------
pl2     movlw   20
        movwf   NCONT      ;20 note da suonare
        clrf    PTAB       ;Indice tabella a zero

play2   call    tabr2      ;Leggi valore
        movwf   RITAR      ;Low byte ritardo onda
        incf    PTAB,f     ;Punta all'elemento seguente

        call    tabr2
        movwf   RITAR+1    ;High byte ritardo onda
        incf    PTAB,f

        call    tabr2
        movwf   RIPET      ;Low byte ripetiz.
        incf    PTAB,f

        call    tabr2
        movwf   RIPET+1    ;High byte ripetiz.
        incf    PTAB,f

        call    qemit      ;Emissione onda quadra

        decfsz  NCONT,f        
        goto    play2
        return

;-----------------------------------------------------
; Suona 5 note leggendo i dati dalla tabella tabr3
;-----------------------------------------------------
pl3     movlw   5
        movwf   NCONT      ;5 note da suonare
        clrf    PTAB       ;Indice tabella a zero

play3   call    tabr3      ;Leggi valore
        movwf   RITAR      ;Low byte ritardo onda
        incf    PTAB,f     ;Punta all'elemento seguente

        call    tabr3
        movwf   RITAR+1    ;High byte ritardo onda
        incf    PTAB,f

        call    tabr3
        movwf   RIPET      ;Low byte ripetiz.
        incf    PTAB,f

        call    tabr3
        movwf   RIPET+1    ;High byte ripetiz.
        incf    PTAB,f

        call    qemit      ;Emissione onda quadra

        decfsz  NCONT,f        
        goto    play3
        return

;-----------------------------------------------------
; Suona 56 note leggendo i dati dalla tabella tabr4
;-----------------------------------------------------
pl4     movlw   56
        movwf   NCONT
        clrf    PTAB       ;Indice tabella a zero

play4   call    tabr4      ;Leggi valore
        movwf   RITAR      ;Low byte ritardo onda
        incf    PTAB,f     ;Punta all'elemento seguente

        call    tabr4
        movwf   RITAR+1    ;High byte ritardo onda
        incf    PTAB,f

        call    tabr4
        movwf   RIPET      ;Low byte ripetiz.
        incf    PTAB,f

        call    tabr4
        movwf   RIPET+1    ;High byte ripetiz.
        incf    PTAB,f

        call    qemit      ;Emissione onda quadra

        decfsz  NCONT,f        
        goto    play4
        return

;-----------------------------------------------------
; Suona 9 note leggendo i dati dalla tabella tabr5
;-----------------------------------------------------
pl5     movlw   9
        movwf   NCONT
        clrf    PTAB       ;Indice tabella a zero

play5   call    tabr5      ;Leggi valore
        movwf   RITAR      ;Low byte ritardo onda
        incf    PTAB,f     ;Punta all'elemento seguente

        call    tabr5
        movwf   RITAR+1    ;High byte ritardo onda
        incf    PTAB,f

        call    tabr5
        movwf   RIPET      ;Low byte ripetiz.
        incf    PTAB,f

        call    tabr5
        movwf   RIPET+1    ;High byte ripetiz.
        incf    PTAB,f

        call    qemit      ;Emissione onda quadra

        decfsz  NCONT,f        
        goto    play5
        return

;-----------------------------------------------------
; Suona 50 note leggendo i dati dalla tabella tabr6
;-----------------------------------------------------
pl6     movlw   50
        movwf   NCONT
        clrf    PTAB       ;Indice tabella a zero

play6   call    tabr6      ;Leggi valore
        movwf   RITAR      ;Low byte ritardo onda
        incf    PTAB,f     ;Punta all'elemento seguente

        call    tabr6
        movwf   RITAR+1    ;High byte ritardo onda
        incf    PTAB,f

        call    tabr6
        movwf   RIPET      ;Low byte ripetiz.
        incf    PTAB,f

        call    tabr6
        movwf   RIPET+1    ;High byte ripetiz.
        incf    PTAB,f

        call    qemit      ;Emissione onda quadra

        decfsz  NCONT,f        
        goto    play6
        return

;-----------------------------------------------------
; Emissione onda quadra da pin RB0.
; Input: RITAR  ritardo onda quadra
;        RIPET  ripetizioni onda quadra
; Si calcola DRITAR che e' il valore di RITAR
; diminuito di 1 (per avere un duty cycle 50%)
;-----------------------------------------------------
qemit   movf    RITAR+1,w   ;Trasferisce RITAR in
        movwf   DRITAR+1    ;DRITAR e decrementa
        decf    RITAR,w     ;di 1
        movwf   DRITAR
        incf    DRITAR,w
        btfsc   STATUS,z
        decf    DRITAR+1,f        

qemit1  bsf     PORTB,0    ;Bit out a 1
        movf    RITAR,w
        movwf   RDEL
        movf    RITAR+1,w
        movwf   RDEL+1

        call    delay      ;Ritardo on

        bcf     PORTB,0    ;Bit out a 0
        movf    DRITAR,w
        movwf   RDEL
        movf    DRITAR+1,w
        movwf   RDEL+1

        call    delay      ;Ritardo off

        decf    RIPET,f
        incf    RIPET,w
        btfsc   STATUS,z
        decf    RIPET+1,f
        movf    RIPET,w
        iorwf   RIPET+1,w
        btfss   STATUS,z
        goto    qemit1
        return

;-----------------------------------------------------
; Routine di ritardo per onda quadra
; Input  RDEL (valore a 16 bit formato low/high)
;-----------------------------------------------------
delay   decf    RDEL,f
        incf    RDEL,w
        btfsc   STATUS,z
        decf    RDEL+1,f
        movf    RDEL,w
        iorwf   RDEL+1,w
        btfss   STATUS,z
        goto    delay
        return
;-----------------------------------------------------
        END
