;MEMBUF.68K	MAR-04-89	(ALSO SEE "INFOSTR")
;Memory buffer handler
;Written by Loren Blaney for DFM Engineering.
;
;REVISION HISTORY:
;
;WARNING:
;If the buffer is not correctly sized, memory can be bombed.
;

	NOLIST
	INCLUDE	SYSPAG
	LIST

DEVNUM	EQU	6		;Device number of this handler

	ORG	$0E00
START	EQU	@

;======================================================================
;ENTRY POINTS:
;
MEMBUF	DC.L	OPENI		;$00 = Initialize for input
	DC.L	OPENO		;$04 = Initialize for output
	DC.L	CHIN		;$08 = Input byte from buffer
	DC.L	CHOUT		;$0C = Output byte to buffer
	DC.L	CLOSE		;$10 = Terminate output
	DC.L	GETINFO		;$14 = Get info
	DC.L	SETBUF		;$18 = Set up buffer

;VARIABLES:
BUFFER	DC.L	DEFBUF		;Pointer to buffer space
BUFEND	DC.L	DEFEND		;Pointer to end of buffer (+1)
INPTR	DC.L	DEFBUF		;Pointer to location to read from
OUTPTR	DC.L	DEFBUF		;Pointer to location to store into

;----------------------------------------------------------------------
;Reset the input pointer to the start of the buffer
;
OPENI	MOVE.L	BUFFER.L,INPTR.L
	RTS

;----------------------------------------------------------------------
;Reset the output pointer to the start of the buffer
;
OPENO	MOVE.L	BUFFER.L,OUTPTR.L
	RTS

;----------------------------------------------------------------------
;Get byte from buffer and return it in D0
; This does not check for reading beyond end of buffer
;
CHIN	MOVE.L	A6,-(SP)	;Save A6

	MOVEA.L	INPTR.L,A6
	MOVEQ	#0,D0		;Make sure high bytes are clear
	MOVE.B	(A6)+,D0

	CMPA.L	BUFEND.L,A6	;Is pointer beyond end of buffer?
	BLO.S	CHIN10		;Branch if not
	JSR	VERROR		;Flag error
	ASCII	"61 - READ BEYOND END OF BUFFER"
	DC.B	0
	BRA.S	CHIN90		;Exit
CHIN10
	MOVE.L	A6,INPTR.L	;Save incremented pointer
CHIN90
	MOVEA.L	(SP)+,A6	;Restore A6
	RTS
	
;----------------------------------------------------------------------
;Output the byte in D0 to the buffer
;
CHOUT	MOVE.L	A6,-(SP)	;Save A6

	MOVEA.L	OUTPTR.L,A6	;Get output pointer
	MOVE.B	D0,(A6)+	;Store byte into buffer

	CMPA.L	BUFEND.L,A6	;Is pointer beyond end of buffer?
	BLO.S	CHOUT10		;Branch if not
	JSR	VERROR		;Flag error
	ASCII	"62 - WRITE BEYOND END OF BUFFER"
	DC.B	0
	BRA.S	CHOUT90		;Exit
CHOUT10
	MOVE.L	A6,OUTPTR.L	;Save incremented pointer
CHOUT90
	MOVEA.L	(SP)+,A6	;Restore A6
	RTS
	
;----------------------------------------------------------------------
;Output an EOF to the buffer
;
CLOSE	MOVE.L	D0,-(SP)	;Save D0

	MOVEQ	#EOF,D0
	BSR.S	CHOUT

	MOVE.L	(SP)+,D0	;Restore D0
	RTS

;-----------------------------------------------------------------------
;Return the address of the information block in D0
;
GETINFO	MOVE.L	#INFO,D0
	RTS

INFO	DC.L	START		;HANDLER START AND END ADDRESSES
	DC.L	END
	DC.L	INFOSTR
INFOSTR	ASCII	"MEMBUF    MAR-04-89  Memory buffer"
	DC.B	0

;-----------------------------------------------------------------------
;Set up buffer space
; Inputs: A0 = Buffer address
;	  D0 = Size in bytes
;
SETBUF	MOVE.L	A0,BUFFER.L
	MOVE.L	A0,BUFEND.L	;BUFEND = BUFFER +size +1
	ADD.L	D0,BUFEND.L
	ADDQ.L	#1,BUFEND.L
	BSR	OPENO
	BRA	OPENI		;(PBRA)

;-----------------------------------------------------------------------
;Default buffer space
;
DEFBUF
DEFEND	EQU	START +$200
	DCB.B	DEFEND-@,EOF


END	EQU	@-1		;Address where this handler ends

	IF	END >= $1000
	ERROR -- PROGRAM IS TOO LONG
	ENDIF

;======================================================================
;Hook handler into the device handler table
;
	ORG	4 *DEVNUM +DEVTBL
	DC.L	MEMBUF

	END
===============================
;Hook handler into the device handler table
;
	ORG	4 