// P R I N T
//   errors 3000
//
// Procedures to accomplish the "printing" function 
//
// PrintInit(firstTime)
//	Called at the beginning of the print run.
// PrintClose(finalTime)
//	Called at the end of the print run.
// Print(pg, last, firstAdr, lastAdr)
//	Called to print the page described by pg>>PageG.
//

get "PDInternals.d"

// outgoing procedures
external
	[
	PrintInit
	PrintClose
	PrintError
	Print
	]

// incoming procedures
external
	[
//PRINTDEBUG
	PDisplay

//PRINTSLOW
	PSlow
	VersatecRoutine;VersatecInit;VersatecClose
	HgRoutine;HgInit;HgClose

//PRINTSLOT
	PSlot;SLOTInit

//PRINTORBIT
	POrbit;POrbitInit

//OS
	MoveBlock

//PDPRINT
	PDError

//CURSOR
	CursorChar
	CursorDigit
	CursorToggle
	]

// incoming statics
external
	[
	Debug
	printerDevice
	]

// internal statics
static
	[
//Debugging stuff
	printCall
	cursorPageNumber
	]

// File-wide structure and manifest declarations.

// Procedures

//p is a pointer to pages structure
//nPages is the number of defined pages

let PrintInit(firstTime, p, nPages, nCopies) be
 [
	cursorPageNumber=0
	CursorChar($P)
	CursorToggle(3)
	switchon printerDevice into
	[
	case printerSlot: SLOTInit(); endcase
	case printerVersatec: if firstTime then VersatecInit(); endcase
	case printerHg: if firstTime then HgInit(); endcase
	case printerPimlico:
	case printerPuffin: 
		POrbitInit(p,nPages,nCopies); endcase
	case printerDover: //endcase intentionally omitted
	case printerSequoia: POrbitInit(); endcase
	default:	endcase
	]
 ]

and PrintClose(finalTime) be
 [
	if finalTime ne 0 then
	switchon printerDevice into
	[
	case 	printerVersatec: VersatecClose();endcase
	case printerHg: HgClose();endcase
	default:
	]
 ]

and Print(pg, last, firstAdr, lastAdr) be
[
   cursorPageNumber=cursorPageNumber+1
   CursorDigit(cursorPageNumber)
   CursorToggle(3)

   let numBands=pg>>PageG.LastBand-pg>>PageG.FirstBand+1

   switchon printerDevice into
	[
	case printerSlot:	PSlot(pg, last, firstAdr, lastAdr)
			endcase;
	case printerVersatec:	if numBands le 0 then return		//Empty page
		PSlow(VersatecRoutine, pg)
		endcase;
	case printerHg:	if numBands le 0 then return		//Empty page
		PSlow(HgRoutine, pg)
		endcase;
	case printerPuffin:
	case printerDover:
	case printerSequoia:
	case printerPimlico:
		[
		let fc=POrbit(pg, firstAdr, lastAdr, last)
		if fc ne 0 then PrintError(3400+fc)
		endcase
		]
	case -1: if numBands le 0 then return
		PDisplay(pg)
		endcase
	default:		endcase
   ]
]

and PrintError(code; numargs n) be
[
	let cursor=vec 15
	MoveBlock(cursor, #431, 16)
	if n then [ CursorChar($K);CursorDigit(code); PDError(code) ]

	//Wait for return key to go down and up:
	until (@#177036&#10) eq 0 do loop
	while (@#177036&#10) eq 0 do loop

	MoveBlock(#431, cursor, 16)
]