//
// Transitory initialization code for KBD package
// last edited August 25, 1980  1:40 PM
//
// Copyright Xerox Corporation 1979, 1980

	get "kbdDefs.d"
	get "streams.d"
	get "altodefs.d"

external [			// procedures defined
	KBDinit	// ([zone, extraSpace]) -> keystream
	]


external	[			// procedures used
		// O.S.
	DefaultArgs
	MoveBlock; Zero
	Noop; TruePredicate
		// KBD
	kbdGets; kbdPuts; kbdReset; kbdEmpty
	kbdState; KBDHandler
		// Interrupt
	FindInterruptMask; InitializeInterrupt
	]

external [			// statics used
		// O.S.
	sysZone
		// KBD
	@OldUS; @NewUS
	kbdTrapTable
	kbdOverflowProc
	kbdTrapProc
	]


manifest [
	interruptStackNeeded = 19	// space required by Interrupt
	kbdStackNeeded = interruptStackNeeded+80	// minimum stack space
	kbdWorkingSpace = 2*USsize+#20+lST	// working space needed
	]


let KBDinit(zone, extraSpace; numargs na) = valof
[
	DefaultArgs(lv na, 0, sysZone, 0)
	kbdState()
	let len = (kbdStackNeeded+kbdWorkingSpace)+extraSpace
	let Space = (zone>>ZN.Allocate)(zone, len)	// *** kludge around problem in O.S. Allocate
	OldUS = Space
	NewUS = OldUS+USsize
	kbdTrapTable = NewUS+USsize
	let newkeys = kbdTrapTable+#20
	let kbdStack = newkeys+lST

	newkeys>>ST.gets = kbdGets
	newkeys>>ST.puts = kbdPuts
	newkeys>>ST.endof = kbdEmpty
	newkeys>>ST.reset = kbdReset

	kbdOverflowProc = Noop
	kbdTrapProc = TruePredicate

	MoveBlock(OldUS, Keys, 4)
	OldUS!4=@buttons

	let WakeBit = FindInterruptMask(1 lshift (KeyboardLevel-1))
	InitializeInterrupt(kbdStack, len-kbdWorkingSpace, WakeBit, KBDHandler)
	@displayInterrupt = @displayInterrupt % WakeBit

	resultis newkeys

]