;**********************************************************************
;****       Assembly language routine for the Pocket Sampler       ****
;****    Written by Graham Cattley -- all modifications welcome    ****
;****       Published in Electronics Australia,  August 1996       ****
;****         Compiled using A86 Assembler v3.70  02/07/96         ****
;**********************************************************************



			;Initialisation


	push	BP		;Save BP
	mov	BP,SP		;Make our own Stack Pointer
				;
	mov 	SI,[bp+0C]	;Point to address of first parameter
	mov	DX,[SI]		;Data Register of parallel port

	mov	SI,[bp+0A]	;point to address of second parameter
	mov	BX,[SI]		;Number of samples to take	

	inc	DX		;Move to 
	inc	DX		;Output Control port

	mov	AX,02		;Wake up sampler
	out	DX,AX		;in case ADC is
	mov	AX,03		;disabled from
	out	DX,AX		;reading in the
	mov	AX,06		;voltage scale
	out	DX,AX		;information.

	dec 	DX		;Move to Input Status Register


			;Wait for A/D converter


START:	mov	CX,0FFFF	;Number of attempts
FINDIT:	in 	AL,DX		;Read in A/D converter status
	and 	AL,08		;Look at Error bit (1 = active, 0 = OK)
	jz	FOUNDIT		;Busy? Jump out if not.
	
	dec	CX		;Sampler didn't respond,
	jnz 	FINDIT		;loop back and try again.
	mov	AL,0FF		;Seems that the sampler isn't plugged in
	jmp 	EXIT		;Return with an error level 255.
	
		
			;Take INIT line high, and LF low.


FOUNDIT:inc	DX		;Up to Control Register
	mov	AL,02		;= 11101 (port inverts output)
	out	DX,AL		;Latch data on A/D's output latch
	dec	DX		;Move back to Status Register


			;Get low order nybble


	in	AL,DX		;Get first nybble
	ror	AL,1		;And move it
	ror	AL,1		;to the low
	ror	AL,1		;end of
	ror	AL,1		;the byte.
	and 	AL,0F		;Mask off top 4 bits
	mov	CL,AL		;And save it into CL


			;Toggle STROBE line low


	inc	DX		;Move to Control Register
	mov	AL,03		;=11100 (output inverted)	
	out	DX,AL		;Output it, sending STROBE line high
	dec	DX		;Move back down to Status Register


			;Get high order nybble


	in 	AL,DX		;Get second nybble
	and	AL,0F0 		;Mask off lower 4 bits
	or	CL,AL		;And stick it onto the top of CL


			;Reset STROBE and INIT lines
	

	inc	DX		;Move to Control Register again
	mov	AL,06		;=11001
	out	DX,AL		;resetting the STROBE and INIT lines.
	dec	DX		;Back to Status Register 

			
			;Fix up final byte


	xor	CL,088		;Invert the BUSY bits (088h = 10001000b) 


			;And loop.


	cs mov 	[BX + 0150],CL	;Put the assembled byte into storage area

	mov	SI,[bp+08]	;Get third parameter 
	mov 	AX,[SI]		;Delay Value
	cmp	AX,0		;Force zero flag
	jz	FINISH		;If Delay Value = 0 then check for next sample

LOOP2:	mov	CL,0FF		;Otherwise start delay
LOOP3:	dec	CL		;CL = 0?
	jnz	LOOP3		;No, loop back 
	dec	AX		;AX = 0?
	jnz	LOOP2		;No, loop back

FINISH:	dec	BX		;Move storage pointer
	jnz	START		;Go and start next sample
	mov 	AL, 00		;All samples were successful, so error = 0

EXIT:	mov	SI,[bp+06]	;Address of fourth parameter
	mov	[SI],AL		;Set Status parameter to current error level

	pop	BP		;Restore BP
	retf	0008		;And return, bypassing parameters on stack

