;*****************************************************************************************
; 
; Filename:	reverb.src
;
; Author:	Alberto Geraci
;		BTX Sistemas	
;		Argentina.
;		
; Revision:	1.01
;
; Freq:		50MHz
;
; Compiled using: SX-Key.
;
; Date Written	: September 1, 2002
;
; Last Revised	: September 10, 2002
;
; Program Description:
;		
;		Audio reverb and echo with SX28 and 32K SRAM
;
; Interface Pins:
;		  ra.1 A/D converter clock
;		  ra.2 A/D converter data line
;		  ra.0 A/D chip select
;		  rb.0 74HC373 address low clock
;		  rb.1 74HC373 address high clock
;		  rb.2 74HC373 data clock
;		  rb.0 74HC373 data out enabled;
;		  rb.4 memory chip enable
;		  rb.5 memory output enable
;		  rb.6 memory write enable
;		  rb.7 Switch reverb/echo
; 		  rc   Data bus
;
;*****************************************************************************************
;
		device sx28l,stackx_optionx
		device oscxt5,turbo
		freq 50000000
		reset reset_entry

		org 10h
direch	 	= 	 $08
direcl 		= 	 $09
contah		=	 $0A
contal		=	 $0B
dato		=	 $0C
times		=	 $0D

CLKAD	equ	ra.1
D0	equ	ra.2
CS	equ	ra.0
clkl	equ	rb.0
clkh	equ	rb.1
clkd	equ	rb.2
clrd	equ	rb.3
CE	equ	rb.4
OE	equ	rb.5
WE	equ	rb.6
switch	equ	rb.7
databus equ	rc
;
;****************************************************************************************************************
;****************************************************************************************************************
conversion	jmp	@conver_sion
		ret
sacodato	jmp	@saco_dato
		ret
pausa		jmp	@pau_sa
		ret
grabo_mem	jmp	@grabomem
		ret
read_one	jmp	@readone
		ret
retardo		jmp	@delay
		ret
read_onec	jmp	@readonec
		ret
;****************************************************************************************************************
		org	50h
reset_entry 	
	 	mov	 ra,#%00000001 			;init rb
		mov 	 !ra,#%00000100
	 	mov	 rb,#%00000000 			;init rb
		mov 	 !rb,#%10000000
	 	mov	 rc,#%00000000 			;init rc
		mov 	 !rc,#%00000000
		mov	w,#$0E		;
		mov	M,w		;
 		mov	!ra,#%111111011	;
		mov     !rb,#%011111111 ;
		mov	w,#$0F		;
		mov	M,w		;
		clrb	CLKAD
		setb	CS
		clrb	clrd
		clrb	clkd
		clrb	clkl
		clrb	clkh
		setb	WE	;
		setb	OE	; mantengo lo stres bytes de control a alto la memoria esta en standby
		setb	CE	; y las I/O estan en hi-Z.
		mov	direcl,#0
		mov	direch,#0
		mov	contal,#0
		mov	contah,#0

;****************************************************************************************************************
; MAIN
;****************************************************************************************************************
;
loop		call	@grabo_mem		; all memory in zero to avoid noise at startup
		cje	contal,#255,readyh
		inc	contal
		jmp	@loop
readyh		clr	contal
		cje	contah,#127,readyl
		inc	contah
		jmp	@loop
readyl		clr	contah
		clr	contal		
;
principal					; reverb part
		sb	switch
		jmp	@sprincipal
		call 	@conversion
		mov	databus,dato
		setb	CE	
		clrb	clrd
		setb	clkd
		clrb	clkd
		call	@grabo_mem
		mov	 times,#$03	;
		call	@retardo
;
		mov	contah,direch
		mov	w,#8
		sub	contah,w
		call	@read_one
		mov	 times,#$03	;
		call	@retardo
;
		mov	w,#8
		sub	contah,w
		call	@read_one
		mov	 times,#$02	;
		call	@retardo
;
		mov	w,#8
		sub	contah,w
		call	@read_one
		mov	 times,#$01	;
		call	@retardo
;
		mov	w,#8
		sub	contah,w
		call	@read_one
;
		mov	w,#8
		sub	contah,w
		call	@read_one
;
ped		jmp	principal

sprincipal					; echo part
		snb	switch
		jmp	@principal
		call 	@conversion
		mov	databus,dato
		setb	CE	
		clrb	clrd
		setb	clkd
		clrb	clkd
		call	@grabo_mem
		mov	 times,#$7F	;
		call	@retardo
		call	@read_onec
		mov	 times,#$7F	;
		call	@retardo
sped		jmp	sprincipal

;******************************************************************************************************************
; 		org	400
;****************************************************************************************************************
;   SUBRUTINAS
;****************************************************************************************************************
conver_sion			; devuelve el valor leido del conversor en la variable DATO.
		mov	dato,#0
		clrb	CLKAD
		clrb	CS
		call	@pausa
		setb	CLKAD
		call	@pausa
		clrb	CLKAD
		call	@pausa
		setb	CLKAD
		call	@pausa
		clrb	CLKAD
		call	@pausa
;
		call	@sacodato
;
		setb	CLKAD
		call	@pausa
		clrb	CLKAD
		call	@pausa
;
		call	@sacodato
;
		setb	CLKAD
		call	@pausa
		clrb	CLKAD
		call	@pausa
;
		call	@sacodato
;
		setb	CLKAD
		call	@pausa
		clrb	CLKAD
		call	@pausa
;
		call	@sacodato
;
		setb	CLKAD
		call	@pausa
		clrb	CLKAD
		call	@pausa
;
		call	@sacodato
;
		setb	CLKAD
		call	@pausa
		clrb	CLKAD
		call	@pausa
;
		call	@sacodato
;
		setb	CLKAD
		call	@pausa
		clrb	CLKAD
		call	@pausa
;
		call	@sacodato
;
		setb	CLKAD
		call	@pausa
		clrb	CLKAD
		call	@pausa
;
		call	@sacodato
;
		setb	CLKAD
		call	@pausa
		clrb	CLKAD
		call	@pausa
		setb	CS
		retp
;****************************************************************************************************************
saco_dato
		snb	D0             ; comparo el D0 para ver si era alto
		jmp	@cenalto
		jmp	@cenbajo
cenalto		setb	$03.0
		rl	dato            ;
		retp
cenbajo		clrb	$03.0
		rl	dato            ;
		retp
;****************************************************************************************************************
pau_sa					; 0.25 uSeg minimo total 13 ciclos de reloj a 50 Mhz
		nop
		nop
		nop
		nop
		nop
		retp
;****************************************************************************************************************
	;*********************************************************************************
	; Function: 			delay ( send data delay )
	       ;********************************************************
;
delay
tagain		nop			;
		nop
		nop
		decsz	 times		;
		jmp	 tagain		;
		retp
	;*********************************************************************************
;****************************************************************************************************************
;****************************************************************************************************************
grabomem
		cje	direcl,#255,nova
		inc	direcl
		jmp	@seguir
nova		clr	direcl
		cje	direch,#127,dnova
		inc	direch
		jmp	@seguir
dnova		clr	direch
seguir		mov	databus,direcl
		setb	clkl
		setb	clkl
		clrb	clkl
		clrb	clkl
		mov	databus,direch
		setb	clkh
		setb	clkh
		clrb	clkh
		clrb	clkh
		clrb	CE	; permito que la memoria comience a funcionar ( sin standby )
		clrb	WE	; preparo para escribir el byte presente en I/O y el address
		setb	WE	; junto con la linea anterior es el pulso para escribir.
		setb	CE	; memory off
		retp
;****************************************************************************************************************
readone
		mov	databus,contah
		setb	clkh
		setb	clkh
		clrb	clkh
		clrb	clkh
		clrb	CE	; permito que la memoria comience a funcionar ( sin standby )
		setb	clrd	; pongo en hi-z el latch de datos
		clrb	OE	; leo memory
		retp
;****************************************************************************************************************
readonec
		mov	contah,direch
		mov	contal,direcl
		sb	contah.6
		jmp	@weset
		jmp	@wereset
weset		setb	contah.6
		jmp	@continue
wereset		clrb	contah.6	
continue	mov	databus,contah
		setb	clkh
		setb	clkh
		clrb	clkh
		clrb	clkh
		clrb	CE	; permito que la memoria comience a funcionar ( sin standby )
		setb	clrd	; pongo en hi-z el latch de datos
		clrb	OE	; leo memory
		retp
;****************************************************************************************************************
end











