//MFieldLp.bcpl -- machine-independent part of "Field-loop" command
//	Last editted: 9 June 1979

get "mcommon.d"

external [
// MASM
	SelfRel; @WssCSS; ResetsCSS; MaskT

// MRGN
	AddToEveryTimeList; RemoveFromEveryTimeList

// MMENU
	CreateAction; @WsMarkA

// MCMD
	FormCmdMenu; SetAbort; @CmdAbortAct; LoadDone

// MINIT0
	@ACTS

// MGO
	@QuitF; @CantContinue

// MDEBUG
	WriteComment; STAT1; STAT2; STAT3

// Machine-dependent field overlay assembly file
	NFields; FieldTab; FBit1Tab

// Machine-dependent field overlay file
	FieldLoop; SetupFieldTest; ModifyField

// Machine-dependent debug file
	QuitTest

// Defined here
	FieldTest
]

let FieldTest(nil,nil) = valof
[	WssCSS("Instruction field for scope loop:")
	SetAbort(lv StopFldLoop,nil)
	QuitF = -1
	for I = 0 to NFields-1 do
		ACTS!I = CreateAction(SelfRel(FieldTab+I),lv TestFld,I)
	resultis FieldMenu
]


and FieldMenu(S,nil) be
[	WsMarkA(CmdAbortAct)
	for I = 0 to (QuitF < 0 ? NFields-1,3) do WsMarkA(ACTS!I)
]


and StopFldLoop(nil,nil,nil) be
[	ResetsCSS()
	WssCSS(QuitF ge 0 ? "Field iteration finished","XXX")
	if QuitF ge 0 then RemoveFromEveryTimeList(QuitF)
	QuitTest()
]


//Begin testing with 0 in the selected field and no-op in other fields.
//Modify the value under mouse control.  Note that TSTINS is the
//displacement of the instruction into LDRMEM while INSTST is a direct
//pointer to the instruction being tsted.
and TestFld(FldX,nil,nil) be
[	LoadDone = false	//Indicate storage smashed in case of
				//subsequent attempt to do "Dump" command
	CantContinue = CantContinue % didTest
	SetupFieldTest(FldX)
	STAT2 = " "; STAT3 = 0	//Start with 0 value in field
	ACTS!0 = CreateAction("+1",lv IncF,FldX)
	ACTS!1 = CreateAction("-1",lv DecF,FldX)
	ACTS!2 = CreateAction("Rshift",lv RshF,FldX)
	ACTS!3 = CreateAction("Lshift",lv LshF,FldX)
	QuitF = AddToEveryTimeList(FieldLoop,FldX)
	FormCmdMenu(); ChangeField(FldX)
]


and ChangeField(FldX) be
[	let NBits = FBit1Tab!(FldX+1)-FBit1Tab!FldX
	STAT3 = STAT3 & (MaskT!NBits)
	STAT1 = SelfRel(FieldTab+FldX)
	ModifyField(FBit1Tab!FldX,NBits,STAT3)
	WriteComment()
]


and RshF(FldX,nil,nil) be
[	STAT3 = STAT3 rshift 1; ChangeField(FldX)
]


and LshF(FldX,nil,nil) be
[	STAT3 = STAT3 lshift 1; ChangeField(FldX)
]


and IncF(FldX,nil,nil) be
[	STAT3 = STAT3+1; ChangeField(FldX)
]


and DecF(FldX,nil,nil) be
[	STAT3 = STAT3-1; ChangeField(FldX)
]