
;Sound part 1
;assembly source code
      INCLUDE "EXEC/TYPES.I" 
      INCLUDE "HARDWARE/CUSTOM.I"

 ;External references
 ;the amiga linker will link these to the program


 XREF  _AbsExecBase        ;exec library address
                           ;always stored in the same location
 XREF  _LVOOpenLibrary     ;exec library routines
 XREF  _LVOAllocMem
 XREF  _LVOCloseLibrary
 XREF  _LVOFreeMem
 XREF  _custom              ;custom register address


 XREF  _LVODelay           ;dos library routines

     CSECT text,code       ;this directive is used by the Lattice Assembler

main:
 jsr      openlibraries    ;open the libraries
 tst.l    d0
 bne      cleanup          ;exit if there is an error
 move.l   dosbase,a6
 jsr      allocatememory   ;allocate chip memory   
 tst.l    d0
 bne      cleanup
 jsr      copydata
 lea      _custom,a5       ;base address of custom chip  
 move.w   #$0001,dmacon(a5) ;disable channel 0 DMA
 move.w   #$ff,adkcon(a5)  ;disable modulation
 lea      aud0(a5),a3      ;store address of audio channel 0
 move.l   #perval,a4
 move.l   waveadd,ac_ptr(a3) ;address of sound data from file 
 move.w   #16,ac_len(a3) ;length (in words) of the data
 move.l   #0,d6
soundloop:
 move.w   #64,ac_vol(a3)
 move.w   0(a4,d6.l),ac_per(a3)  ;440 Hz
 jsr      makesound        ;one second of sound, one second of silence
 add.l    #2,d6
 cmp.l    #24,d6
 bne      soundloop
 move.l   waveadd2,ac_ptr(a3) ;address of sound data from file 
 move.w   #8,ac_len(a3) ;length (in words) of the data
 move.l   #0,d6
soundloop2:
 move.w   #64,ac_vol(a3)
 move.w   0(a4,d6.l),ac_per(a3)  ;440 Hz
 jsr      makesound        ;one second of sound, one second of silence
 add.l    #2,d6
 cmp.l    #24,d6
 bne      soundloop2
 move.l   waveadd3,ac_ptr(a3) ;address of sound data from file 
 move.w   #4,ac_len(a3) ;length (in words) of the data
 move.l   #0,d6
soundloop3:
 move.w   #64,ac_vol(a3)
 move.w   0(a4,d6.l),ac_per(a3)  ;440 Hz
 jsr      makesound        ;one second of sound, one second of silence
 add.l    #2,d6
 cmp.l    #24,d6
 bne      soundloop3
cleanup:
 jsr      freememory       ;free the memory buffer
 jsr      closelibraries   ;close the libraries
 rts

makesound:
 move.w  #$8001,dmacon(a5) ;enable audio channel 0 DMA
 move.l   dosbase,a6
 move.l   #100,d1
 jsr      _LVODelay(a6)    ;delay 
  move.w   #$0001,dmacon(a5) ;disable audio channel 0 DMA
 move.l   dosbase,a6
 move.l   #100,d1
 jsr      _LVODelay(a6)    ;delay again
 rts 

openlibraries:
 move.l   _AbsExecBase,a6  ;exec library address
 lea      dosname,a1       ;pointer to string containing dos library name
 clr.l    d0               ;any version 
 jsr      _LVOOpenLibrary(a6) ;open the dos library
 movem.l  d0,dosbase       ;and store the returned pointer
 tst.l    d0               ;check for success
 beq      seterrorflag     ;branch if unsuccessful
 clr.l    d0
 rts

closelibraries:
 move.l   _AbsExecBase,a6   ;close dos library 
 move.l   dosbase,a1
 beq      nodoslib
 jsr      _LVOCloseLibrary(a6)
nodoslib:
 rts

seterrorflag:               ;set a flag if there is an error
 move.l   #1,d0
 rts

allocatememory:             ;chipmemory for sound data
 move.l   _AbsExecBase,a6   ;exec library address
 move.l   #32,d0            ;number of bytes required
 move.l   #65539,d1         ;type of memory required (chip memory or clear)
 jsr      _LVOAllocMem(a6)  ;allocate the memory
 movem.l  d0,waveadd        ;store the pointer
 beq      seterrorflag     
 move.l   #16,d0            ;number of bytes required
 move.l   #65539,d1         ;type of memory required (chip memory or clear)
 jsr      _LVOAllocMem(a6)  ;allocate the memory
 movem.l  d0,waveadd2        ;store the pointer
 beq      seterrorflag     
 move.l   #8,d0            ;number of bytes required
 move.l   #65539,d1         ;type of memory required (chip memory or clear)
 jsr      _LVOAllocMem(a6)  ;allocate the memory
 movem.l  d0,waveadd3        ;store the pointer
 beq      seterrorflag     
 move.l   #32,d0            ;number of bytes required
 move.l   #65539,d1         ;type of memory required (chip memory or clear)
 jsr      _LVOAllocMem(a6)  ;allocate the memory
 movem.l  d0,squareadd      ;store the pointer
 beq      seterrorflag     
 move.l   #32,d0            ;number of bytes required
 move.l   #65539,d1         ;type of memory required (chip memory or clear)
 jsr      _LVOAllocMem(a6)  ;allocate the memory
 movem.l  d0,sawadd         ;store the pointer
 beq      seterrorflag     
 clr.l    d0
 rts

copydata:                   ;all the sound data is copied into chip memory  
 move.l   #31,d1
 lea      waveform,a1
 move.l   waveadd,a2
more1:
 move.b   (a1)+,(a2)+
 dbra     d1,more1
 move.l   #31,d1
 lea      square,a1
 move.l   squareadd,a2
more2:
 move.b   (a1)+,(a2)+
 dbra     d1,more2
 move.l   #31,d1
 lea      saw,a1
 move.l   sawadd,a2
more3:
 move.b   (a1)+,(a2)+
 dbra     d1,more3
 move.l   #15,d1
 lea      waveform_oct2,a1
 move.l   waveadd2,a2
more4
 move.b   (a1)+,(a2)+
 dbra     d1,more4
 move.l   #7,d1
 lea      waveform_oct3,a1
 move.l   waveadd3,a2
more5:
 move.b   (a1)+,(a2)+
 dbra     d1,more5
 rts
     
freememory:
 move.l   _AbsExecBase,a6    ;exec library address
 move.l   #32,d0           
 move.l waveadd,a1           ;pointer to buffer
 jsr      _LVOFreeMem(a6)    ;frees the memory
 move.l   #32,d0             ;number of bytes allocated 
 move.l squareadd,a1         ;pointer to buffer
 jsr      _LVOFreeMem(a6)    ;frees the memory
 move.l   #32,d0             ;number of bytes allocated 
 move.l sawadd,a1            ;pointer to buffer
 jsr      _LVOFreeMem(a6)    ;frees the memory
memout: 
 rts


       CSECT data              ;SECTION DATA,data used by Metacomco Assembler
dosname:
 dc.b     'dos.library',0       ;name of dos library
 ds.w     0                     ;word alignment
filename:
 dc.b     'sploit',0            ;name of file to be read, for sound data
 ds.w     0                    
dosbase:
 dc.l     0
filehandle:                     ;filehandle for file to be read
 dc.l     0
buffer:                         ;pointer to memory buffer
 dc.l     0
perval:              ;period values c-c'
 dc.w 428,404,381,360,339,320,302,285,269,254,240,226,214,0
waveform:                       ;sinewave data
 dc.b 0,-20,-38,-56,-71,-83,-92,-98,-100,-98,-92,-83,-71,-56,-38,-20
 dc.b 0,20,38,56,71,83,92,98,100,98,92,83,71,56,38,20
waveform_oct2:                       ;sinewave data
 dc.b 0,-38,-71,-92,-100,-92,-71,-38
 dc.b 0,38,71,92,100,92,71,38
waveform_oct3:                       ;sinewave data
 dc.b 0,-71,-100,-71
 dc.b 0,71,100,71
square:                         ;square wave data
 dc.b 0,127,127,120,127,120,127,120,127,120,127,120,127,120,127,120
 dc.b 0,-127,-127,-120,-127,-120,-127,-120
 dc.b -127,-127,-120,-127,-120,-127,-120,-127
saw:                            ;saw tooth wave data
 dc.b 0,8,16,24,32,40,48,56,64,72,80,88,96,104,112,120,127
 dc.b -120,-112,-104,-96,-88,-80,-72,-64,-56,-48,-40,-32,-24,-16,-8
waveadd:
 dc.l  0
waveadd2:
 dc.l  0
waveadd3:
 dc.l  0
squareadd:
 dc.l  0
sawadd:
 dc.l  0
 END

