;   VMEMA.ASM - actual mapping done in microcode
;   last modified June 14, 1976  2:36 PM
;
; Copyright Xerox Corporation 1979
	.EXTN	MAPTRAP,SOFTMAPFLAG
;
	.ENT	REHASHMAP
	.ENT	VRR,VWR,VRR1,VWR1,VRR2,VWR2,VRRP,VWRP
	.ENT	DOUBLEADD1
	.ENT	RDREF0,RDREF1,WTREF0	; instructions to overwrite
	.ENT	VmemRam
;
	.DUSR	RDREF = 74400
	.DUSR	WTREF = 75000
	.DUSR	SWAT = 77400
;
	getframe = 370
	return = 366
;
	.ZREL
VRR:	VRRX
VWR:	VWRX
VRR1:	VRR1X
VWR1:	VWR1X
VRR2:	VRR2X
VWR2:	VWR2X
VRRP:	VRRPX
VWRP:	VWRPX
;
DOUBLEADD1:DBLX
;
	.SREL
REHASHMAP:REMAP
RDREF0:	DOAR1
RDREF1:	DORM1
WTREF0:	DOAW1
VmemRam: VMR
;
	.NREL
;
;  ROUTINES TO DO SOFTWARE MAPPING
;  IF THERE IS A FAULT, A BCPL ROUTINE 'MAPTRAP' IS CALLED
;  IF THERE IS NO FAULT, NO BCPL STACK SPACE IS USED
;
MAPTR:	0	; save hashmap ptr
MPTRP:	MAPTRAP
;
VRRX:	STA	3,1,2
	MOV	0,3
	LDA	0,0,3
	LDA	1,1,3
	JMP	DOAR1
;
VRRPX:	LDA	1,RMSK
	AND	0,1
	SUBS	1,0
	MOVS	1,1
	JMP	VRR2X
VRR1X:	MOV	0,1
	SUB	0,0
VRR2X:	STA	3,1,2	;SAVE RETURN
DOAR1:	RDREF		;microcode instruction
	JMP	NOTINRD	;MISSING PAGE ON READ
	MOV	3,0
	LDA	3,1,2
	JMP	1,3	;RETURN WITH CORE ADDR. IN AC0
NOTINRD:STA	3,MAPTR
	LDA	3,RMSK
	AND	3,0
	JSR	@getframe
	  7
	SWAT
	SUB	1,1	;READ REF
MTRAP:	STA	1,6,2
	LDA	0,5,2
	LDA	3,LMSK
	AND	0,3
	LDA	0,4,2
	ADDS	3,0	; form hash key
	LDA	3,MAPTR
	STA	3,3,2
	JSRII	MPTRP
	3
	LDA	0,4,2
	LDA	1,5,2
	LDA	3,6,2
	LDA	2,0,2	; get rid of frame
	MOV	3,3,SZR
	JMP	DOAW1
	JMP	DOAR1
;
VWRX:	STA	3,1,2
	MOV	0,3
	LDA	0,0,3
	LDA	1,1,3
	JMP	DOAW1
;
VWRPX:	LDA	1,RMSK
	AND	0,1
	SUBS	1,0
	MOVS	1,1
	JMP	VWR2X
VWR1X:	MOV	0,1
	SUB	0,0
VWR2X:	STA	3,1,2
DOAW1:	WTREF		;microcode instruction
	JMP	WFAULT	;NOT IN OR NOT DIRTY
	MOV	3,0
	LDA	3,1,2
	JMP	1,3
WFAULT:	STA	3,MAPTR
	LDA	3,RMSK
	AND	3,0
	JSR	@getframe
	  7
	SWAT
	ADC	1,1	;WRITE REF
	JMP	MTRAP
;
LMSK:	177400
RMSK:	377
;
; COMPUTE HASH INDEX FOR NEW PAGE WHEN
; RECONSTRUCTING HASH TABLE AFTER PAGE FAULT
;
REMAP:	STA	3,1,2
	LDA	1,RMSK
	ANDS	0,1
	LDA	3,LMSK
	ANDS	3,0
DORM1:	RDREF			;AC0,AC1 HAS VIRTUAL ADDR
	MOV	3,0,SKP	; NO SKIP, HASHMAP PTR IN AC3
	SUB	0,0	; SKIP, ENTRY ALREADY PRESENT
	LDA	3,1,2
	JMP	1,3		;RETURNS POINTER TO EMPTY ENTRY
;
; INCREMENT 32-BIT COUNTER ADDRESSED BY AC0
;
DBLX:	STA	3,1,2
	MOV	0,3
	ISZ	1,3
	JMP	.+3
	ISZ	0,3
	JMP	.+1
	LDA	3,1,2
	JMP	1,3
;
; Set up RAM access instructions
VMR:	LDA	0,RDRX
	STA	0,DOAR1
	STA	0,DORM1
	LDA	0,WTRX
	STA	0,DOAW1
	SUB	0,0
	STA	0,@SMF
	JMP	1,3
;
RDRX:	RDREF
WTRX:	WTREF
SMF:	SOFTMAPFLAG
;
	.END