; Program SYNTH.BAS

; This program generates one of four waveforms as specified by switches
; pin_A1 and pin_A0, via the Motorola MC1408L8 DAC.  The timing is 
; selected by using switches pin_A3 and pin_A2.
;
; 00 - Square wave    01 - Rising Sawtooth  10 - Falling Sawtooth
; 11 - Triangle wave

; copyright Locksley Haynes, Morgan State University, Oct. 14, 1997.

; TICKit ------------------------------------> MC1408L8  DAC
; pin_D7(term 38) ---------------------------> A1 (term 5)
; pin_D6(term 37) ---------------------------> A2 (term 6)
; pin_D5(term 36) ---------------------------> A3 (term 7) 
; pin_D4(term 35) ---------------------------> A4 (term 8) 
; pin_D3(term 34) ---------------------------> A5 (term 9) 
; pin_D2(term 33) ---------------------------> A6 (term 10) 
; pin_D1(term 32) ---------------------------> A7 (term 11) 
; pin_D0(term 31) ---------------------------> A8 (term 12) 

DEF tic62_b       ;define the TICKit device in use
LIB fbasic.lib    ;library for basic routines

GLOBAL word speed

;Function sqr_wave generates a square wave form.
FUNC none sqr_wave
  LOCAL byte square [16] 0x00b 0x00b 0x00b 0x00b 0x00b 0x00b 0x00b 0x00b ~ 
                        ~0xffb 0xffb 0xffb 0xffb 0xffb 0xffb 0xffb 0xffb   
  LOCAL byte n 0b
BEGIN
   REP
     dport_set( square[n] ) 
     delay(speed)
     ++(n)
   UNTIL ==(n, 16b)
ENDFUN

;**Function rise_saw generates a rising sawtooth wave form
FUNC none rise_saw
   LOCAL byte n 0b
   LOCAL byte saw_rising [16] 0x00b 0x10b 0x20b 0x30b 0x40b 0x50b 0x60b ~                        
                   ~0x70b 0x80b 0x90b 0xa0b 0xb0b 0xc0b 0xd0b 0xe0b 0xf0b
BEGIN
  REP
    dport_set( saw_rising[n] )
    delay(speed)
    ++(n)
  UNTIL ==(n, 16b)
ENDFUN

;**Function fall_saw generates a falling sawtooth wave form.
FUNC none fall_saw
   LOCAL byte n 0b
   LOCAL byte saw_falling [16] 0xf0b 0xe0b 0xd0b 0xc0b 0xb0b 0xa0b 0x90b ~
                   ~0x80b 0x70b 0x60b 0x50b 0x40b 0x30b 0x20b 0x10b 0x00b
BEGIN
  REP
    dport_set( saw_falling[n])
    delay(speed)
    ++(n)
  UNTIL ==(n,16b)
ENDFUN

;**Function tri_wave generates a triangl wave form.
FUNC none tri_wave
  LOCAL byte n 0b
  LOCAL byte triangle [16] 0x00b 0x20b 0x40b 0x60b 0x80b 0xa0b 0xc0b 0xe0b
~
                   ~0xe0b 0xc0b 0xa0b 0x80b 0x60b 0x40b 0x20b 0x00b
BEGIN
  REP
    dport_set( triangle[n] )
    delay(speed)
    ++(n)
  UNTIL ==(n,16b)
ENDFUN

;***************************Main Function****************************
FUNC none main
  LOCAL byte wave_type
  LOCAL byte input
BEGIN
  REP
   atris_set(255b)   ;set address pins as inputs
   =(input, and(aport_get(), 0x0fb))  ;read input
   =(wave_type, and(input, 3b))      ;parse input to get wave_type
   =(input, >>(input))
   =(input, >>(input))
   =(speed, and(to_word(input), 3w))      ;get timing speed input
   =(speed, *(2w,+(speed,1w)))
   dtris_set( 0b )   ;set data pins as outputs
  
  
   ;select wave type
   IF ==(wave_type, 0b)
      sqr_wave()
   ELSEIF ==(wave_type, 1b)
      rise_saw()
   ELSEIF ==(wave_type, 2b)
      fall_saw()
   ElSE
      tri_wave()
   ENDIF

  LOOP
ENDFUN
;*********************************************************************