/* --- Copyright University of Sussex 1990. All rights reserved. ----------
 > File:            C.vms/src/pwmio.p
 > Purpose:
 > Author:          Ben Rubenstein et al (see revisions)
 */

;;; --------------------- PWM I/O ROUTINES -------------------------------

#_INCLUDE 'declare.ph'
#_INCLUDE 'io.ph'
#_INCLUDE 'vmsdefs.ph'
#_INCLUDE 'signals.ph'

section $-Sys;

global constant
		procedure (Io$-Line_write, Io$-Do_interact_read)
	;

endsection;


;;; ----------------------------------------------------------------------

section $-Sys$-Io => pwmensureprompt;

	/*	Ensure that next call of Pwm_record_read (below) will issue a prompt.
		Used by setpop, ved_pop.
	*/
define vars pwmensureprompt(flag);
	lvars flag;
	if flag then
		dev_in!D_FLAGS _biset _M_D_TERM_PROMPT
	else
		dev_in!D_FLAGS _biclear _M_D_TERM_PROMPT
	endif -> dev_in!D_FLAGS
enddefine;

	/*	Read PWM 'terminal'
		Will only be called for dev_in
	*/
define lconstant Pwm_record_read(dev, _bsub, userbuf, _nbytes);
	lvars buf = dev!D_IN_BUFFER, dev, userbuf, _count, _nbytes, _bsub;

	if (Do_interact_read(dev, dev_out) -> _count) then
		;;; some new input was read
		if _zero(_count) then
			;;; eof
			dev!D_FLAGS _biset _M_D_TERM_PROMPT -> dev!D_FLAGS;
			return(0)
		elseif buf!BUF_START[_count _sub _1] == _:`\n` then
			dev!D_FLAGS _biset _M_D_TERM_PROMPT -> dev!D_FLAGS
		else
			dev!D_FLAGS _biclear _M_D_TERM_PROMPT -> dev!D_FLAGS
		endif
	endif;

	_int(_nbytes) -> _nbytes;
	if _nbytes _lt _count then _nbytes -> _count endif;
	_bmove(@@(b)[_count], buf@BUF_START[buf!BUF_POSITION],
									userbuf@(w->b)[_int(_bsub) _sub _1]) -> ;
	buf!BUF_POSITION _add _count -> buf!BUF_POSITION;
	_pint(_count)
enddefine;

endsection;		/* $-Sys$-Io */


section $-Sys$-Pwm;

	/*	Alter devices to work with the PWM
		dev_in and raw_dev_in are different devices for the same
		mailbox, but raw_dev_out == dev_out
	*/
define Make_devin();
	lconstant _TERMFLAGS = _M_D_LOGICAL_TERM _biset _M_D_INTERACTIVE;
	_TERMFLAGS _biset _M_D_TERM_PROMPT -> dev_in!D_FLAGS;
	Pwm_record_read -> dev_in!D_READ;

	_TERMFLAGS -> raw_dev_in!D_FLAGS;
	;;; make the two input devices share the same buffer
	raw_dev_in!D_IN_BUFFER -> dev_in!D_IN_BUFFER;

	_TERMFLAGS -> dev_out!D_FLAGS;
	$-Sys$-Io$-Line_write -> dev_out!D_WRITE;		;;; makes this buffered
	;;; make writes to output mailbox hang up waiting for PWM to read
	dev_out!D_CTRL_BLK!ICB_FLAGS _biset _M_ICB_MBX_OUTWAIT
											-> dev_out!D_CTRL_BLK!ICB_FLAGS;
enddefine;

endsection;		/* $-Sys$-Pwm */


/* --- Revision History ---------------------------------------------------
--- John Gibson, Dec  4 1990
		Moved signal mailbox code to vdpwm.p
--- John Gibson, Oct 26 1990
		Changed _M_D_T*ERMINAL to _M_D_LOGICAL_TERM
--- John Gibson, Oct 23 1990
		Changed -Pwm_record_read- to have new-style args for device read
		procedures.
--- John Gibson, Oct 10 1990
		VMS _extern changed to return proper system call result (thus test
		for success is now result _bitst _1).
--- John Gibson, Apr 11 1989
		Revised code for signal mailbox, transferred to here from vdpwm.p
--- John Gibson, Feb 23 1989
		Changed Pwm_record_read to use new Do_interact_read. Make_devin
		now sets _M_D_LOGICAL_TERM flag on PWM output mailbox.
--- John Gibson, Feb 21 1989
		Added enabling of AST on PWM signal mailbox.
--- John Gibson, Feb 19 1989
		Substituted _M_D_RAWTERM term flag set with _M_D_TERM_PROMPT not set
		Included io.ph
--- John Gibson, Oct 26 1988
		Added VMS PWM code received from Paul White at SD, suitably
		modified.
--- John Gibson, Mar 28 1988
		Moved out of sysio.p
 */
