	.mcall	.MODULE
	.MODULE	MEMMAP,comment=<Map i/o page>,release=V09,version=06

;**************************************************************;
;                                                              ;   
;   Copyright (c) 1987            Bob Schor                    ;
;                                 Eye and Ear Hospital         ;
;                                 230 Lothrop St.              ;
;                                 Pittsburgh, PA   15213       ;
;                                                              ;
; All rights reserved.  May not be copied without this notice. ;
;                                                              ;
;**************************************************************;

;**************************************************************;
;                                                              ;
;   Copyright (c) 1989            Bob Schor                    ;
;                                 Eye and Ear Institute        ;
;                                 203 Lothrop St.              ;
;                                 Pittsburgh, PA   15213       ;
;                                                              ;
; All rights reserved.  May not be copied without this notice. ;
;                                                              ;
;**************************************************************;

;exhibit memory map of i/o page, to show peripheral space

;b schor	may, 1987

;modifications --
;	june, 1989	Psects $CODE and $DATA added, .MODULE added


;define our .psects before calling SUMAC to force ours low

	.psect	$code	ro,i,lcl,rel,con
	.psect	$data	rw,d,lcl,rel,con

;uses SUMAC for structured macro code

	.library	/lib:sumac/	;use external macro definition file

	.mcall	SUMAC
	SUMAC				;initialization macro

	.dsabl	gbl			;if undefined symbol, force error


HEAD	sysmac, <system macros>

	.mcall	.PRINT			;to announce identification
	.mcall	.GVAL			;used to get system info
	.mcall	.TRPSET			;catch traps to 4, 10
	.mcall	.EXIT			;return to RT-11 on errors

HEAD	syscon, <system constants>

	tsx$	=	100000		;TSX bit in sysgen word

HEAD	offset, <monitor offsets>

	sysgen	=	372		;offset to sysgen features

HEAD	tsx, <TSX emt codes>
	do.tsx	=	emt + 375	;general TSX "do" emt

	tsx.rt	=	140		;real-time high byte
	mapio.	=	5		;map i/o page


HEAD	MAPIO, <map i/o page for TSX>
;	MAPIO
;returns with carry set if failurs

	.macro	MAPIO

	PUSH	r0			;save register
	movb	#mapio., tsxarg		;save low byte, emt number
	movb	#tsx.rt, tsxarg+1	;save "real-time" emt flag
	mov	#tsxarg, r0		;point to emt area
	do.tsx				;call on TSX
	POP	r0			;restore register

	.endm	MAPIO


.sbttl	+++++++++++++++++++++++++++
.sbttl	+	main routine      +
.page
.sbttl	+++++++++++++++++++++++++++

	.psect	$code

LABEL	memmap, <start of routine>
	NOTE
	.PRINT	#modnam			;identify module
	NOTE
	INVOKE	init			;initialize variables
	INVOKE	shomap			;show device maps
	.TRPSET	#emtarg, #0		;reset trap catcher
	.EXIT				;and return to RT-11


PROCED	init, <Initialization>
	INVOKE	istsx			;see if TSX
	IF	in.tsx, ne		;if TSX,
		MAPIO			;attempt to map i/o page
		IF	cs		;if we cannot,
			NOTE	<Cannot map i/o page, need MEMMAP privilege>
			.EXIT		;return to TSX
		ENDIF
	ENDIF
	clr	in.dev			;not starting out in device space
	mov	#1, exists		;assume device exists at outset
	NOTE	<The following registers exists on the i/o page -- >
	NOTE
	.TRPSET	#emtarg, #trap4		;set trap catcher
ENDPROC


PROCED	istsx, <ensure TSX>
	.GVAL	#emtarg, #sysgen	;return sysgen options
	bit	#tsx$, r0		;test TSX bit 
	IF	eq			;if missing,
		clr	in.tsx		;not in TSX
	ELSE
		mov	#1, in.tsx	;yes, in TSX
	ENDIF
ENDPROC


PROCED	shomap, <show device maps>
	FOR	r1, #160000, #177776, #2	;for each word in i/o space,
		tst	(r1)		;try to access word
		nop			;allow trap to occur
		IF	in.dev, ne	;if processing device already,
			IF	exists, ne	;if device exists,
						;do nothing
			ELSE
				INVOKE	enddev	;found end of device space
			ENDIF
		ELSE			;no device so far
			IF	exists, ne	;if device now exists,
				INVOKE	begdev	;found start of device space
			ELSE
						;do nothing
			ENDIF
		ENDIF
		inc	exists		;assume device follows
	ENDFOR
	mov	#0, r1			;point past i/o page
	IF	in.dev, ne		;if processing device,
		INVOKE	enddev		;end device space at end of memory
	ENDIF
ENDPROC


PROCED	begdev, <Found new device space>
	PNOTE	<     >
	TYP.8	r1
	mov	r1, first		;remember start of device
	inc	in.dev			;remember device exists
ENDPROC


PROCED	enddev, <Found end of device space>
	PUSH	r1			;save pointer
	sub	#2, r1			;back up pointer
	IF	r1, ne, first		;if multiple registers,
		PNOTE	<    -    >
		TYP.8	r1		;show end of space
	ENDIF
	NOTE
	POP	r1			;restore pointer
	clr	in.dev			;no longer inside device space
ENDPROC


TASK	trap4, <handle traps to 4, 10>
	IF	cc			;trap to 4
;		NOTE	<Trap to 4>
		clr	exists		;say no device here
	ELSE
		NOTE	<Mysterious trap to 10>
	ENDIF
	.TRPSET	#emtarg, #trap4		;set trap catcher
ENDTASK

	.sbttl	++++++++++++++++++++++++
	.sbttl	+	variables      +
	.page
	.sbttl	++++++++++++++++++++++++

	.psect	$data

tsxarg:	.blkw	10			;area for tsx arguments
in.tsx:	.word	0			;non-zero if running from tsx
in.dev:	.word	0			;non-zero if in device space
exists:	.word	0			;non-zero if device (memory) exists
emtarg:	.blkw	10			;area for emt arguments
first:	.word	0			;first register in set

LABEL	modnam, <module name>
	.NLCSI				;generates .asciz module string
	.even

	ENDEND

	.end	memmap
