//**************************************************************************************
//D0CS04.TST
//By B. Rosen and M. Thomson  							February 7, 1979
//Sub Test Program for D0 CS Module
//**************************************************************************************
get "d0cs.d"

//Edge pin and test connector signal busses available to this sub-test:

//{ALUA: ALUA.00,  ALUA.01,ALUA.02,ALUA.03,  ALUA.04,ALUA.05,ALUA.06,  ALUA.07,ALUA.08,ALUA.09,  ALUA.10,ALUA.11,ALUA.12,  ALUA.13,ALUA.14,ALUA.15}

//{BBDisp: BBDisp.0,BBDisp.1,BBDisp.2}

//{F1: F1.0,  F1.1,F1.2,F1.3}

//{F2: F2.0,  F2.1,F2.2,F2.3}

//{JA: JA.0,JA.1,  JA.2,JA.3,JA.4,  JA.5,JA.6,JA.7}

//{JC: JC.0,JC.1,JC.2}

//{R: R.00,  R.01,R.02,R.03,  R.04,R.05,R.06,  R.07,R.08,R.09,  R.10,R.11,R.12,  R.13,R.14,R.15}

//{WakeP': WakeP3',WakeP2',WakeP1'}

//**************************************************************************************
//Test 5: Test First Call

let Test5() be
[
	SpeakTest(5) //set initial conditions (see notes at end of D0CS.TST)

	let sb = 0
	let v = vec 64
	let str = v
	let st = vec 64

//This test is similar to test 4 but Calls are done instead of Branches.
//We verify TPC loading by UseCTask and checking APC.
//For this test, Call behaves exactly like GoTo.

	//J-TestResult'=1 from SpeakTest initialization

	for i = 0 to 15 do
	[
		FORMAT(st,"First Call Test: instruction number = <B>",i+32)

		{UseTPC'xxx} = 0; //During Cycle 0
		EClock()
		Check("LoadAPC'",{LoadAPC'},0,st,5000)

//We use Restore code here instead of Load APC code
		{LoadAPC'xxx} = 0

//Branch Burp cannot occur unless we branch
//Enable TPC reading
		{UseTPC'xxx} = {UseTPC'}
		EClock()
		{UseTPC'xxx} = 1
		{LoadAPC'xxx} = 1

		Check("F1",{F1},7,st,5001)
		Check("F2",{F2},#13,st,5002)
		Check("F1ok",{F1ok},1,st,5003)
		Check("EnabGrpA/B",{EnabGrpA/B},1,st,5004)
		Check("JC",{JC},Call,st,5005)
		Check("JA",{JA},i+33,st,5006)

		RxCIA()
		Check("CIA",(not {R})&#7777,i+32,"via R bus",5007)

		if i>0 then 
		[
			RxAPC()
			Check("APC",{R}&#7777,(i+32),st,5008) //checks addr + 1

		]
	]

	Check("Cycle0Feed'",{Cycle0Feed'},0,"after call test",5009)
] 

//**************************************************************************************
//Test 6: Test Return & Page Register

and Test6() be
[
	SpeakTest(6) //set initial conditions (see notes at end of D0CS.TST)

	let st = vec 64

	{WakeP'} = 7; //initialization

	for i = 0 to 15 do
	[
		FORMAT(st,"Return & Page Register Test: instruction number = <B>",i+48)

//This will be the place we GoTo, .+1 for all but last iteration
		{ALUA} = i eq 15? #400,i+49

//This line forces LoadAPC
		{ShortFldDisp'} = 0
		EClock()

//Actually Do the Load of APC
		{LoadAPC'xxx} = 0
		EClock()
		{LoadAPC'xxx} = 1

		Check("F1",{F1},5,st,6000)		//Load Page Code
		Check("F2",{F2},(i eq 15?1,i),st,6001)	//Load Page i except last time
		Check("F1ok",{F1ok},1,st,6002)
		Check("F2ok",{F2ok},0,st,6003)
		Check("JC",{JC},(i eq 0? GoTo,Return),st,6004)
		Check("JA",{JA},(i eq 0? #61,0),st,6005)

		RxCIA()
		Check("CIA",(not {R})&#7777,i+48,"via R bus",6006)

		if i>0 then 
		[
			//should have loaded Page Register with i
			//but of course, its now i-1 when you can read it
			RxPage()
			Check("Page",{R} rshift 12,i-1,"via R bus",6007)

			//APC is loaded with the address for the next instruction
			RxAPC()
			Check("APC",{R},i eq 15? #400,i+49,st,6008)
		]
	]
]

//**************************************************************************************
//Test 7: Test Wake-Up Encoder

and Test7() be
[
	SpeakTest(7) //set initial conditions (see notes at end of D0CS.TST)

	let sb = 0
	let v = vec 64
	let str = v
	let st = vec 64
	let oldlowval,oldhival = 0,0

	let WakeEncode = table [
		#00;#01;#02;#02;  #03;#03;#03;#03;
		#04;#05;#06;#06;  #07;#07;#07;#07;
		#10;#11;#12;#12;  #13;#13;#13;#13;
		#10;#11;#12;#12;  #13;#13;#13;#13;
		#14;#15;#16;#16;  #17;#17;#17;#17;
		#14;#15;#16;#16;  #17;#17;#17;#17;
		#14;#15;#16;#16;  #17;#17;#17;#17;
		#14;#15;#16;#16;  #17;#17;#17;#17 ]

	for i = 1 to 63 do
	[
		let lowval = i & 7
		let hival  = i rshift 3

		FORMAT(str,"Wake-Up encoder test: wakeups = <B>,<B>",oldhival,oldlowval)

		{WakeP'} = not oldlowval

//WaitForKey("just before t1 in wake-up test")
		//Load lo bits
		{J-TestResult'} = i eq 63?0,1
		EClock()
		{LoadAPC'xxx} = 0
		//Load hi bits
		{WakeP'} = not hival

//WaitForKey("just before t2 in wake-up test")
		EClock()

		if i eq 63 then EClock(); //Burp

		Check("JC",{JC},(i eq 63?GoTo,Branch),str,7000,i)
		Check("JA",{JA},(i eq 63?2,0),str,7001,i) //new page, addresses start at 0 again

		{LoadAPC'xxx} = 1
		RxCIA()
		Check("CIA",(not {R})&#7777,(i eq 63?#401,#400),str,7002,i)

//1st time thru, bits are in HiPri, 2nd time thru, APCTask
		RxAPC()
		let sb = WakeEncode!(i-1)
 
		if i>0 then 
			Check("APCTask",({R} rshift 12),sb,str,7003,i)

		oldlowval = lowval
		oldhival  = hival
	]

	TwoEClocks() //Nop @401
]

//**************************************************************************************
//Test 8: Test the BBFA function

and Test8() be
[
	SpeakTest(8) //set initial conditions (see notes at end of D0CS.TST)

	let str = vec 64

	for i = 0 to 7 do
	[
		FORMAT(str,"Testing BBFA function, iteration <B>",i)

		{BBDisp} = i
		{BBFA'} = 0
		{J-TestResult'} = i eq 7?0,1
		{LoadAPC'xxx} = 1
		{UseTPC'xxx} = 0
		EClock()
		Check("UseTPC'",{UseTPC'},1,str,8000,i)

		{UseTPC'xxx} = 1
		Check("LoadAPC'",{LoadAPC'},0,str,8001,i)

		{LoadAPC'xxx} = 0
		EClock()

		if i eq 7 then EClock(); //burp

		RxAPC()
		Check("APC",{R},i,str,8002,i)
	]

	{J-TestResult'} = 1
	TwoEClocks() //Nop @403
]