//
// Test program for VMEM
// last edited August 1, 1977  4:08 PM
//
// Copyright Xerox Corporation 1979

	get "streams.d"
	get "altofilesys.d"

external
[		// O.S.
	CreateDisplayStream; ShowDisplayStream
	OpenFile
	Closes; Gets; Puts; Wos; Ws; Wss
	Zero
	Timer
	dsp; keys
		// STANDARDRAM
	StandardRAM
		// Template
	PutTemplate
		// Trace
	Trace; ProcTrace; UnTrace
	TraceIndent
	TraceStream; TraceOuts; TraceLines
		// VMEM
	VmemSoft
	InitializeVmem
	AddBuffers; SnarfBuffer; UnsnarfBuffer
	MAPTRAP; LockCell; UnlockCell
	FlushBuffers
		// PRINTVMEMSTATE
	PrintVmemState
		// SOFTMAP
	NPROBE
		// ASMAP
	@VRR1; @VWR1; @VRR2; @VWR2
]

external	// entries for VMEM
[	CleanupLocks
	PageType
	PageGroupSize
	PageGroupBase
	PageGroupAlign
	DOPAGEIO
]

external	// links to ISF
[	InitFmap
	IndexedPageIO
]


manifest
[	dsize = 25000
	npages = #20
	maps = #1000
	dbs = #3000
]

static
[	pbase = 4
	pgmsk
	pstrm
	dbufs
	dhm
	ds
	fmap
]

let dmap() be
[	StandardRAM()
	let dd = vec dsize
	ds = CreateDisplayStream(40, dd, dsize)
	ShowDisplayStream(ds)
	TraceLines = 6
	TraceStream = ds
	Trace(MAPTRAP, "MAPTRAP")
	Trace(vwrite, "VWRITE")
	let dbpt = vec #400
	let dhmap = vec maps+3
	if (dhmap&1) ne 0 then dhmap = dhmap+1
	dhm = dhmap+2
	let db = vec dbs
	dbufs = db
	Zero(dbufs, dbs)
	let dcells = vec 5
	let davec = vec npages*2+1
	let fp = vec lFP
	Zero(fp, lFP)
	pstrm = OpenFile("TEST.VMEM.", ksTypeReadWrite, wordItem, 0, fp)
	Closes(pstrm)
	pgmsk = 0
	VmemSoft()
	InitializeVmem(dhmap+2, maps, dbpt, dcells, 5, dhmap, pmstat)
	InitFmap(davec, npages*2, fp, true)
	fmap = davec
	AddBuffers(dbufs, dbufs+dbs)
	pause("Initialized ...")
	for j = 0 to 6 do
	 [ vwrite(testaddr(j))!0 = testvalue(j); nzscan() ]
	for j = 0 to 6 do
	 [ let val = vwrite(testaddr(j))!0
	   Wos(ds, val)
	   if val ne testvalue(j) then Wss(TraceOuts, " ??????????")
	   Puts(TraceOuts, $*N)
	   nzscan()
	 ]
	FlushBuffers()
	PrintVmemState(ds)
	Wss(TraceOuts, "Done*N")
	pause("Done ...")
]

and testaddr(j) =
	pbase lshift 8 + (table[ #444;#1111;#1555;#2222;#2666;#3333;#3777 ])!j

and testvalue(j) = #111000+j

and pause(s) be
[	Ws(s)
	Gets(keys)
	Puts(dsp, $*N)
]

and nzscan() be
[	for p = dbufs to dbufs+dbs-1 do if @p ne 0 then
	   PutTemplate(TraceOuts, "...$7UO:$7UO*N", p, @p)
	for p = dhm by 2 to dhm+maps-2 do if @p ne 0 then
	   PutTemplate(TraceOuts, "$3UO:$7UO$7UO*N", p-dhm, @p, p!1)
]

and vwrite(addr) = VWR1(addr)

and CleanupLocks() be [ ]

and PageType(vp) = valof
[	let pn = vp-pbase
	if (pn & #177770) ne 0 then pause("Bad vp")
	let mask = 1 lshift pn
	if (pgmsk & mask) ne 0 resultis 1
	pgmsk = pgmsk + mask
	resultis -1
]

and PageGroupSize(vp) = 1
and PageGroupBase(vp) = vp
and PageGroupAlign(vp) = 0

and DOPAGEIO(vp, core, np, wflag) be
[	let t = vec 1
	let it = Timer(t)
	IndexedPageIO(fmap, 3*vp+2, core, np, wflag)
	PutTemplate(TraceOuts, "$S vp $UO, $UO page(s), core $UO: $UD ms*N",
	  (wflag? "Write", "Read"), vp, np, core, Timer(t)-it)
]

and pmstat(ptr, n) be
[	PutTemplate(TraceOuts, "MSPROC called with$7UO$7UO*N", ptr, n)
	if n ge 0 then
	 for p = ptr by 2 to ptr+n-2 do
	   PutTemplate(TraceOuts, "$9UO$7UO*N", p!0, p!1)
]