// MDfixup -- JCN fixup pass
// last edited November 3, 1978  5:24 PM

	get "mddecl.d"
	get "mdfields.d"

external	// defined here
[	FixupJCN	// ()
]

external	// used
[		// MDmain
	@IP
	@NInstructions
	@DMachine
		// MDprescan
	@PageMask
]


structure IMA:	// decompositions of a real 12-bit address
[	blank bit 4
	page bit 6
	// Dorado
	= [	p0thru4 bit 5
		pbit5 bit 1
	  ]
	// D0
	= [	blank bit 4
		wd0thru1 bit 2
	  ]
	wd bit 6
	= [	wd2thru3 bit 2
		wd4thru7 bit 4
		= [	wd4thru6 bit 3
			wdbit7 bit 1
		  ]
		= [	blank bit 1
			wd5thru6 bit 2
			blank bit 1
		  ]
	  ]
	= [	wd2thru6 bit 5
		blank bit 1
	  ]
	= [	blank bit 1
		wd3thru6 bit 4
		blank bit 1
	  ]
]


let FixupJCN() be
// Fill in branch control fields of instructions
// Does no error checking!
[	for i = 0 to NInstructions-1 do
	[ let ip = IP(i)
	  if ip>>IM.returns loop
	  let i1 = ip>>IM.W1
	  if i1 eq WExt loop
	  let a0 = ip>>IM.W0
	  let a1 = IP(i1)>>IM.W0
	  test DMachine eq 0
	  ifso	// Fill in FA
	  [ test ip>>IM.iscond
	     ifso  ip>>TI.JA2thru6 = a1<<IMA.wd2thru6
	     ifnot ip>>TI.JA2thru7 = a1<<IMA.wd
	    ip>>TI.JA0thru1 = a1<<IMA.wd0thru1
	  ]
	  ifnot	// Fill in JN, possibly JC and FF
	  test DMachine eq 1
	  ifso	// Dorado model 0
	  [ test ip>>IM.iscond & ip>>IM.jbc
	    ifso	  // must use short form
	      ip>>TI.JNfast = a1<<IMA.wd5thru6
	    ifnot test (a1&globalZero0) eq 0
	    ifso	  // use global call
	      ip>>TI.JCglobal, ip>>TI.JNglobal =
	        JCglobalV, a1<<IMA.page
	    ifnot test (a1&PageMask) eq (a0&PageMask)
	    ifso	  // local branch
	      ip>>TI.JClocal, ip>>TI.JNlocal =
	        JClocalV, a1<<IMA.wd
	    ifnot	  // long branch
	      ip>>TI.JClong, ip>>TI.JNlong, ip>>TI.FF =
	        JClongV, a1&17B, a1 rshift 4
	  ]
	  ifnot	// Dorado model 1
	  [ test ip>>IM.iscond & ip>>IM.jbc
	    ifso	  // must use short form
	      ip>>TI.JN1fast2thru3, ip>>TI.JN1fast5thru6 =
	        a1<<IMA.wd2thru3, a1<<IMA.wd5thru6
	    ifnot test (a1&globalZero1) eq 0
	    ifso	  // use global call
	      ip>>TI.JC1global, ip>>TI.JN1global2thru6, ip>>TI.JN1bit7 =
	        JC1globalV, a1<<IMA.p0thru4, a1<<IMA.pbit5
	    ifnot test (a1&PageMask) eq (a0&PageMask)
	    ifso	  // local branch
	      ip>>TI.JC1local, ip>>TI.JN1local2thru6, ip>>TI.JN1bit7 =
	        JC1localV, a1<<IMA.wd2thru6, a1<<IMA.wdbit7
	    ifnot	  // long branch
	      ip>>TI.JC1long, ip>>TI.JN1long4thru6, ip>>TI.JN1bit7, ip>>TI.FF =
	        JC1longV, a1<<IMA.wd4thru6, a1<<IMA.wdbit7, a1 rshift 4
	  ]
	]
]