// DispProms.bcpl last modified by:
//   Pier November 10, 1983  2:15 PM: Added Empty to Disp PROM
 //   Taft June 9, 1983  6:17 PM: LF and Alto waveforms for white borders; remove IDISProms 
//   Taft September 11, 1982  2:36 PM: new LF waveforms 
//   Gene January 20, 1982: eliminate duplicate prom production
//   PIER July 14, 1981: added DispM proms for LF and Alto displays

external DisplayProms
get "DoradoProms.defs"

//proms for Ken's real display controller
let DisplayProms(mem) be
	[
	let buff = vec 512
	if (StEq(mem,"DISPY") % StEq(mem,"DISPL") % (StEq(mem,"DisPromA"))) & ICtype eq MC10149 then
		[
		MakeDisProm(buff)
		Header("DisPromA",4,buff,256,0)
		PromCommand("DisPromA-i15")
		]
	if (StEq(mem,"DISPY") % StEq(mem,"DISPL") % (StEq(mem,"DisPromB"))) & ICtype eq MC10149 then
		[
		MakeDisProm(buff)
		Header("DisPromB",4,buff,256,0)
		PromCommand("DisPromB-l15")
		]
	if StEq(mem,"DISPM") then mem!0 = DoAll
	if StEq(mem,"Disp-LF") & ICtype eq MC10149 then  // define the Prom
		[
		MakeHRomLF(buff)
		Header("LFProm-Low",4,buff,256,12)
		PromCommand("LFProm-b20","0","0")  // low 256 words
		Header("LFProm-High",4,buff+256,256,12)
		PromCommand("LFProm-a16","0","0")  // high 256 words
		]
	if StEq(mem,"Disp-Alto") & ICtype eq MC10149 then  // define the Prom
		[
		MakeHRomAlto(buff)
		Header("AltoProm",4,buff,256,12)
		PromCommand("AltoProm-b20","0","0") //only need low 256 words
		]
	]

// This PROM generates the following waveform:
//    336 bits of sync
//     20 bits of blanked left border
//   1072 bits visible
//     12 bits of blanked right border
//   ----
//   1440 bits total
// Also it generates 360 bits of HalfLine starting 1440/2 = 720 bits after the
// start of sync.

and MakeHRomLF(buff) be
	[
	manifest [ notSync = #10; HSync = 4; HBlank = 2; HalfLine = 1 ]
	Zero(buff,512)
	for addr = 0 to 511 do
 	  [
	  // The HRom clock ticks once for every four pixels.
	  // Additionally, HSync starts during the LAST HRom clock of a scan line
	  // instead of the first one; this accounts for the +1 below:
	  let pixelsSinceSync = 4*((addr+1) rem 512)
	  let Contents = pixelsSinceSync ls 336 % pixelsSinceSync ge 1440? HSync, notSync
	  if pixelsSinceSync ls 336+20 % pixelsSinceSync ge 336+20+1072 then
	    Contents = Contents % HBlank
	  if pixelsSinceSync ge 1440/2 & pixelsSinceSync ls 1440/2+336 then
	    Contents = Contents % HalfLine
	  buff!addr = Contents
 	  ]
	]


// This PROM generates the following waveform:
//     68 bits of sync
//     20 bits of blanked left border
//    656 bits visible
//     16 bits of blanked right border
//   ----
//    760 bits total
// Also it generates 68 bits of HalfLine starting 760/2 = 380 bits after the
// start of sync.
// *** Note: this has been changed since last tested ***

and MakeHRomAlto(buff) be
	[
	manifest [ notSync = #10; HSync = 4; HBlank = 2; HalfLine = 1 ]
	Zero(buff,256)
	for addr = 0 to 255 do
 	  [
	  // The HRom clock ticks once for every four pixels.
	  // Additionally, HSync starts during the LAST HRom clock of a scan line
	  // instead of the first one; this accounts for the +1 below:
	  let pixelsSinceSync = 4*((addr+1) rem 256)
	  let Contents = pixelsSinceSync ls 68 % pixelsSinceSync ge 760? HSync, notSync
	  if pixelsSinceSync ls 68+20 % pixelsSinceSync ge 68+20+656 then
	    Contents = Contents % HBlank
	  if pixelsSinceSync ge 760/2 & pixelsSinceSync ls 760/2+68 then
	    Contents = Contents % HalfLine
	  buff!addr = Contents
 	  ]
	]

and MakeDisProm(buff) be
	[
	manifest [ Full = #10; WriteEn = 4; ReadEn = 2; Empty = 1 ]
	Zero(buff,256)
	for adr = 0 to 255 do
		[
		let contents = 0
		let RP = adr rshift 4
		let SP = adr & #17
		let Delta = (RP-SP)&#17
		if Delta eq 1 then
			[ contents<<MCM149.Pin15 = 1; contents<<MCM149.Pin14 = 1 ]
		if Delta eq 0 then
			[ contents<<MCM149.Pin12 = 1 ]
buff!adr = contents
		]
	]