
;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
 XREF  _LVOOpen
 XREF  _LVORead
 XREF  _LVOClose

     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      readfile         ;read in a text file
 tst.l    d0
 bne      cleanup          ;exit if the file has not been read
 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   buffer,ac_ptr(a3) ;address of sound data from file 
 move.w   #1125,ac_len(a3) ;length (in words) of the data
 move.w   #64,ac_vol(a3)   ;maximum volume
 move.w   #508,ac_per(a3)  ;440 Hz
 jsr      makesound        ;one second of sound, one second of silence
 move.l   waveadd,ac_ptr(a3) ;sine wave sound data
 move.w   #16,ac_len(a3)     ;length (in words) of the data
 jsr      makesound
 move.l   squareadd,ac_ptr(a3) ;square wave data
 jsr      makesound
 move.l   sawadd,ac_ptr(a3) ;saw tooth wave data
 jsr      makesound
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   #2250,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,buffer         ;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,waveadd        ;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
 rts
     
freememory:
 tst.l    buffer
 beq      memout
 move.l   _AbsExecBase,a6    ;exec library address
 move.l   #2250,d0           ;number of bytes allocated for this buffer by
                             ;AllocMem command
 move.l buffer,a1            ;pointer to buffer
 jsr      _LVOFreeMem(a6)    ;frees the memory
 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

readfile:
 move.l   dosbase,a6          ;dos library address
 move.l   #filename,d1        ;name of file to be read
 move.l   #1005,d2            ;the accessmode is 1005 for a file already open
                              ;1006 to create a new file
 clr.l    d0
 jsr      _LVOOpen(a6)        ;open the file
 move.l   d0,d1
 beq      seterrorflag        ;branch if unsuccessful
 movem.l  d0,filehandle       ;store the filehandle
 move.l   buffer,d2           ;pointer to memory buffer
 clr.l    d0
 move.l   #2250,d3            ;number of bytes to be read
 jsr      _LVORead(a6)        ;read the data
 cmpi.l   #-1,d0
 beq      seterrorflag        ;branch if unsuccessful
 clr.l    d0
 move.l   filehandle,d1       ;obtain the filehandle
 jsr      _LVOClose(a6)       ;close the file
 clr.l    d0
 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
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
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
squareadd:
 dc.l  0
sawadd:
 dc.l  0
 END

