B1        .set         0300h      ; 512 words of data RAM
B0_D      .set         0100h      ; 512 words of data RAM
B0_P      .set         0FE00h     ; 512 words of program RAM
****************************************************************
* DSK = 1   =>   Program bude pouzivan na DSK                  *
*       0   =>   Program bude pouzivan v simulatoru            *
****************************************************************
DSK    .set 0

       .mmregs

       .if DSK = 1
       .sect "promenne"
TA       .word     7              ; Fcut = 3968.25 Hz pro -20 dB
RA       .word     7              ; Fcut = 3968.25 Hz pro -20 dB
TB       .word    36              ; Fs = 7936.5 Hz
RB       .word    36              ; Fs = 7936.5 Hz
 ;---------------------------------------------------------------;
 ; Bit definition of the control register in the AIC             ;
 ; viz. DSP Starter Kit Users's Guide pages from B-1 to B-34     ;
 ;---------------------------------------------------------------;
 ;              ______ 0/1 gain control bits
 ;             | _____ 0/1 gain control bits
 ;             || ____ 0/1 disables/enables the AUX IN+ and AUX IN- pins
 ;             ||| ___ 0/1 disables/enables the loopback function
 ;             |||| __ 0/1 deletes/insert the input bandpass filter
 ;             |||||
AIC_CTR  .word 11001b
********************************************************************
       .endif
****************************************************************
*   Set up the ISR vector                                      *
****************************************************************
         .sect "vectors"
       .if DSK = 1
         .space 6 * 16         ;02 ;
         .space 2 * 16         ;08 ;
rint:    B       RECEIVE       ;0A ; Serial prot receive interrupt RINT.
xint:    B       TRANSMIT      ;0C ; Serial port transmit interrupt XINT.
         .space  22 * 16       ;0E ;
         ;-------------------------------------------------------
       .else
         B       START         ; Skok na zacatek programu v simulatoru
       .endif

       .text
START:
       .if DSK = 1             ; Inicializace desticky
******************************************************************
* TMS32C05X INITIALIZATION                                       *
******************************************************************
         SETC    INTM             ; Disable interrupts
         LDP     #0               ; Set data page pointer
         OPL     #0834h,PMST      ;
         LACC    #0               ;
         SAMM    CWSR             ; Set software wait state to 0
         SAMM    PDWSR            ;
* Reset AIC by writing to PA2 (address >52) to DSK
         SPLK    #022h,IMR        ; Using XINT syn TX & RX
         CALL    AICINIT          ; initialize AIC and enable interrupts
*******************************************************************
* This routine enables serial port rx interrupts & configures        *
* TLC32040 for the frame sync. When RINT is triggered, read a     *
* dummy data word from the AIC then generate a sine wave to       *
* send out.                                                       *
*******************************************************************
         CLRC    OVM              ; OVM = 0
         SPM     0                ; PM = 0
         SPLK    #012h,IMR
         CLRC    INTM             ; enable
       .endif

********************************************************************
* Misto pro vlozeni inicializacni casti                            *
*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*

        SETC         SXM        ; Set sign extension mode
        SETC         OVM        ; Set overflow mode
        CLRC         CNF        ; Set block B0 at <dma> 100h
        SPM          0          ; Set MPY output shifter to 0

; Reverze move FIR coef. to data memory block B0

        LAR          AR2,#B0_D+FIR_LEN    ; Load AR2 with the address B0_D
        MAR          *,AR2
        RPT          #FIR_LEN     ; Repeat (N-1) times
        BLPD         #FIR_DATA,*- ; Move the coef. to block B0
        SETC         CNF          ; Set block B0_D to B0_P at <dma> 0FE00h

; Clear circular buffer , Block B1 <dma> 300h 

        LAR          AR1,#B1      ; Load AR1 with the address B1
        MAR          *,AR1        ; Set AR(ARP) to 1
        ZAP                       ; Zero accumulator and P register
        RPT          #FIR_LEN     ;
        SACH         *+           ; Save the upper accumulator (0)

; Setup circular adressing

        LDP	     #0
        SPLK	     #B1,CBSR1
        SPLK	     #B1+FIR_LEN,CBER1
        SPLK	     #1001b,CBCR
        LAR          AR1,#B1
        MAR	     *,AR1

*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*

WAIT
       .if DSK = 1
         IDLE                      ; wait for a transmit or receive
         NOP
         NOP
         B       WAIT
       .endif
;------- end of main program ----------

***************************************************************************
***************************************************************************



; RECIEVER INTERRUPT SERVICE ROUTINE

RECEIVE:
       .if DSK = 1            ;Precteni vstupniho vzorku z A/D prevodniku
         LDP     #TA		        ;??????????????????????????????????????????
         LAMM    DRR          ;read data from DRR
       .else
         LAMM    PA2          ;Precteni vstupniho vzorku ze souboru
       .endif 
******************************************************************************
; Sem se vlozi program pro zpracovani signalu
; Vstupni i vystupni 14-ti bitovy vzorek je v akumulatoru ulozen ve formatu
; ????????????????xxxxxxxxxxxxxx00
; |               |             |__Dve nuly na konci
; |               |________________Vlastni vzorek ve dvojkovem doplnku
; |                                tj. rozsah -8192 az 8191
; |________________________________16-krat nevime co zde je
; Samozrejme v simulatoru to je takto:
; ????????????????xxxxxxxxxxxxxxxx
*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*
          LDP     #(B1/128)             ;begin of B1
          SACL    *+                    ;place sample to circular buffer
          RPTZ    #FIR_LEN
          MAC     #B0_P,*+              ;(FIR_LEN+1) multiply & acumulate
          APAC

;pokud by to na desce blblo, tak zde RPT #15
;leze z toho ale pak polovicni amplituda
          RPT     #14                   ;move high acc to low acc
          SFR
*<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*
       .if DSK = 1                      ; Presun vyst. vzorku do D/A prevodniku
          AND     #0FFFCh
          RPT     #14                   ; Posun doleva
          SFL                           ;
          RPT     #14                   ; Nasunuti znamenkoveho bitu
          SFR                           ;
          SAMM    DXR                   ;
          RETE                          ;
       .else                            ; Presun vystupniho vzorku do souboru
          SAMM    PA4
          B       WAIT                  ; Skok na zacatek smycky
       .endif                           ; Vystup

       .if DSK = 1                      ; Inicializace AIC
; TRANSMIT INTERRUPT SERVICE ROUTINE
TRANSMIT:
         RETE
******************************************************************
*  DESCRIPTION: This routine initializes the TLC320C40 for       *
*        a 8Khz sample rate with a gain setting of 1             *
******************************************************************
* aic initialization data
AICINIT: SPLK    #20h,TCR           ; To generate 4 MHz from Tout
         SPLK    #4,PRD             ; for AIC master clock
         MAR     *,AR0
         LACC    #0008h             ; Non continuous mode
         SACL    SPC                ; FSX as input
         LACC    #00c8h             ; 16 bit words
         SACL    SPC
         LACC    #080h              ; Pulse AIC reset by setting it low
         SACH    DXR
         SACL    GREG
         LAR     AR0,#0FFFFh
         RPT     #10000             ; and taking it high after 10000 cycles
         LACC    *,0,AR0            ; (.5ms at 50ns)
         SACH    GREG
         ;------------------------
         LDP     #TA              ;
         SETC    SXM              ;
         LACC    TA,9             ; Initialized TA and RA register
         ADD     RA,2             ;
         CALL    AIC_2ND          ;
         ;------------------------
         LDP     #TB              ;
         LACC    TB,9             ; Initialized TB and RB register
         ADD     RB,2             ;
         ADD     #02h             ;
         CALL    AIC_2ND          ;
         ;------------------------
         LDP     #AIC_CTR         ;
         LACC    AIC_CTR,2        ; Initialized control register
         ADD     #03h             ;
         CALL    AIC_2ND          ;
         RET                      ;
AIC_2ND:
         LDP     #0               ;
         SACH    DXR              ;
         CLRC    INTM             ;
         IDLE                     ;
         ADD     #6h,15           ; 0000 0000 0000 0011 XXXX XXXX XXXX XXXX b
         SACH    DXR              ;
         IDLE                     ;
         SACL    DXR              ;
         IDLE                     ;
         LACL    #0               ;
         SACL    DXR              ; make sure the word got sent
         IDLE                     ;
         SETC    INTM             ;
         RET                      ;
       .endif

;*************************************************************************
;       Coefficient initialization -- Q15 Scaling
;*************************************************************************
         .data
FIR_DATA:
; Koeficienty cislicove pasmove propusti FIR
          .WORD   -5
          .WORD   -2
          .WORD   11
          .WORD   -20
          .WORD   45
          .WORD   235
          .WORD   103
          .WORD   -255
          .WORD   -130
          .WORD   -40
          .WORD   -727
          .WORD   -638
          .WORD   985
          .WORD   1345
          .WORD   21
          .WORD   790
          .WORD   2009
          .WORD   -2421
          .WORD   -7742
          .WORD   -2393
          .WORD   8838
          .WORD   8838
          .WORD   -2393
          .WORD   -7742
          .WORD   -2421
          .WORD   2009
          .WORD   790
          .WORD   21
          .WORD   1345
          .WORD   985
          .WORD   -638
          .WORD   -727
          .WORD   -40
          .WORD   -130
          .WORD   -255
          .WORD   103
          .WORD   235
          .WORD   45
          .WORD   -20
          .WORD   11
          .WORD   -2
FIR_END   .WORD   -5

FIR_LEN   .set    (FIR_END - FIR_DATA)

        .end