//D0i1.bcpl--Define screen areas, allocate storage, and initialize print
//  control stuff.  Code here is executed only on Midas/I and the storage
//  allocated is preserved in Midas.State.
//	Last edited: 1 May 1981

get "mcommon.d"
get "d0.d"
manifest [ get "d0regmem.d" ]
manifest [
	VAtablen = (#40000/BlockSize)+1	//Maximum size for 16K IMX
	AAtablen = (#40000/BlockSize)+1
]

external [
// MINIT0
	@MBlock

// MINIT1
	SaveStatics; NewMPDispBlock; GetZStorage

// MIDAS
	MidasSwat

// MASM
	GetStorage

// D0MEM
	GenCorrPar

// D0VM
	VAtab; AAtab

// Defined here
	InitMPInterface; MIMtab; BootTable; NBootInst
	@ScreenHeight; @ScreenWidth
	d0go; d0rnvr; d0wnvr; d0rsr; d0wsr
	d0rdt; d0wrtt; d0rdmi; d0rdtpc; d0wrttpc; d0mem
]

static [
	@ScreenHeight = MaxLineN+11	//Number of screen lines
	@ScreenWidth = 76		//Number of chars/line
	MIMtab; BootTable; NBootInst=0
	d0go; d0rnvr; d0wnvr; d0rsr; d0wsr
	d0rdt; d0wrtt; d0rdmi; d0rdtpc; d0wrttpc; d0mem
]


//"InitMPInterface" allocates storage for vectors and initializes screen
//areas.  Args to NewMPDispBlock are line number, left character (0 to
//ScreenWidth-1), identifying character for use in command files, width
//in characters
let InitMPInterface() be
[	for I = 0 to 19 do
	[ NewMPDispBlock(I,0,$A,18)
	  NewMPDispBlock(I,20,$B,19)
	  NewMPDispBlock(I,41,$C,34)
	]
//Initialize VM stuff--here so that VM is cleared on RunProg
	VAtab = GetZStorage(VAtablen)
	AAtab = GetZStorage(AAtablen)

	MIMtab = GetZStorage(MIMlen*3)
	BootTable = MIMtab

//For now copy the overlays into MIMtab; eventually replace this stuff
//by some scheme whereby the output files of Micro/MicroD are loaded
//directly as for KERNEL.

	AddMicOvl(lv d0go,#1,15,table [
	    4000b;  135351b;   0b lshift 12; //  OVERLAYAREA
	    4150b;    3004b;  11b lshift 12; //   (+1)
	    6150b;   65006b;  11b lshift 12; //   (+2)
	    2147b;   21011b;  15b lshift 12; //   (+3)
	    6047b;   23400b;   4b lshift 12; //   (+4)
	    4163b;    3014b;  15b lshift 12; //  BPENTRY
	      50b;   24021b;   1b lshift 12; //   (+1)
	      50b;   25020b;   1b lshift 12; //   (+2)
	    2024b;  101032b;  15b lshift 12; //  BREAK
	    2150b;   24424b;  11b lshift 12; //  CHKFF
	    2024b;  103033b;  15b lshift 12; //  MIDASFAULT
	      45b;    1030b;   1b lshift 12; //  USERFAULTSTART
	      50b;   25040b;   1b lshift 12; //   (+1)
	    4001b;  103341b;   0b lshift 12; //  SENDMSG
	      50b;   25011b;   0b lshift 12; //   (+1)
		] )

	AddMicOvl(lv d0rnvr,#2,8,table [
	   74150b;   65002b;  15b lshift 12; //  OVERLAYAREA
	      50b;  125005b;   1b lshift 12; //   (+1)
	   62150b;   65006b;  15b lshift 12; //   (+2)
	      50b;  125010b;   5b lshift 12; //   (+3)
	   64150b;   41012b;  15b lshift 12; //   (+4)
	      50b;  125015b;  11b lshift 12; //   (+5)
	   66150b;   41016b;  15b lshift 12; //   (+6)
	      50b;   125010b;   14b lshift 12; //   (+7)
		] )

	AddMicOvl(lv d0wnvr,#3,7,table [
//Patched first mi to not write PCF here.
//	     150b;   31003b;   1b lshift 12; //  OVERLAYAREA
	     150b;   21002b;   1b lshift 12; //  OVERLAYAREA
	     165b;  127005b;   1b lshift 12; //   (+1)
	     150b;   11007b;   1b lshift 12; //   (+2)
	     150b;   13010b;   5b lshift 12; //   (+3)
	     166b;  131012b;   5b lshift 12; //   (+4)
	     150b;   15015b;   5b lshift 12; //   (+5)
	     150b;   27010b;  10b lshift 12; //   (+6)
		] )

	AddMicOvl(lv d0rsr,#4,13,table [
	    4000b;  125350b;   0b lshift 12; //  OVERLAYAREA
	    4017b;  103004b;   5b lshift 12; //   (+1)
	    4150b;    3007b;   5b lshift 12; //   (+2)
	      20b;  137011b;   5b lshift 12; //   (+3)
	    1104b;  121013b;   5b lshift 12; //   (+4)
	    1150b;  125015b;   5b lshift 12; //   (+5)
	      37b;  137016b;   1b lshift 12; //   (+6)
	    1104b;  131025b;   1b lshift 12; //   (+7)
	   30150b;   27025b;   1b lshift 12; //   (+10)
	   36150b;   27025b;  15b lshift 12; //   (+11)
	   44147b;   21026b;  15b lshift 12; //  NOTIFYBOTHWAYS
	   30050b;   25401b;   0b lshift 12; //   (+1)
	   66150b;   41014b;  14b lshift 12; //   (+2)
		] )

	AddMicOvl(lv d0wsr,#5,16,table [
	      20b;  137221b;  10b lshift 12; //  OVERLAYAREA
	     304b;  121004b;  11b lshift 12; //   (+1)
	    1150b;  125007b;  11b lshift 12; //   (+2)
	      17b;  103223b;  14b lshift 12; //   (+3)
	      50b;  103013b;  15b lshift 12; //   (+4)
	      20b;  137014b;   5b lshift 12; //   (+5)
	    1104b;  135016b;   5b lshift 12; //   (+6)
	      37b;  137025b;   1b lshift 12; //   (+7)
	   30050b;  125033b;   1b lshift 12; //  WRITER0
	   36050b;  125033b;  15b lshift 12; //   (+1)
	    1100b;  111026b;   1b lshift 12; //   (+2)
	   44147b;   21031b;  15b lshift 12; //  NOTIFYBOTHWAYS
	   30050b;   25401b;   0b lshift 12; //   (+1)
	   44150b;   65026b;  15b lshift 12; //  NOTIFYNEXTCOM
	   42050b;  125036b;  15b lshift 12; //  ENTERTASK0
	   40050b;    7033b;  15b lshift 12; //   (+1)
		] )

	AddMicOvl(lv d0rdt,#6,14,table [
	    4000b; 125350b;  0b lshift 12; //  OVERLAYAREA
	      50b; 125004b;  5b lshift 12; //   (+1)
	   30020b;  77007b;  1b lshift 12; //   (+2)
	    1174b; 131010b;  5b lshift 12; //   (+3)
	    1104b; 123012b;  5b lshift 12; //   (+4)
	    4017b; 103015b;  5b lshift 12; //   (+5)
	    4150b;   3016b;  5b lshift 12; //   (+6)
	      37b; 137020b;  1b lshift 12; //   (+7)
	    1104b; 133027b;  1b lshift 12; //   (+10)
	   42050b; 125024b; 15b lshift 12; //  GETT
	   44050b;  25026b; 15b lshift 12; //   (+1)
	   44147b;  21031b; 15b lshift 12; //  NOTIFYBOTHWAYS
	   30050b;  25401b;  0b lshift 12; //   (+1)
	     150b;  65015b;  4b lshift 12; //   (+2)
		] )

	AddMicOvl(lv d0wrtt,#7,14,table [
	    4000b;  125350b;   0b lshift 12; //  OVERLAYAREA
	      50b;  125004b;   5b lshift 12; //   (+1)
	    4000b;  135351b;   0b lshift 12; //   (+2)
	    2150b;   27011b;  15b lshift 12; //   (+3)
	      20b;   77013b;   1b lshift 12; //   (+4)
	    1174b;  131015b;   5b lshift 12; //   (+5)
	    1104b;  127016b;   5b lshift 12; //   (+6)
	    4017b;  103020b;   5b lshift 12; //   (+7)
	    4150b;    3023b;   5b lshift 12; //   (+10)
	      37b;  137025b;   1b lshift 12; //   (+11)
	    1100b;  111031b;   1b lshift 12; //   (+12)
	   66150b;   41031b;  15b lshift 12; //  PUTT
	   44147b;   21032b;  15b lshift 12; //  NOTIFYBOTHWAYS
	      50b;   25401b;   0b lshift 12; //   (+1)
		] )

	AddMicOvl(lv d0rdmi,#10,15,table [
	    4000b;  135351b;   0b lshift 12; //  OVERLAYAREA
	    2050b;  125005b;   5b lshift 12; //   (+1)
	      20b;   41224b;   1b lshift 12; //   (+2)
	    2050b;  125341b;  14b lshift 12; //   (+3)
	       0b;   43224b;   1b lshift 12; //   (+4)
	    2050b;  125341b;  14b lshift 12; //   (+5)
	       0b;   47225b;   1b lshift 12; //   (+6)
	    2050b;  125020b;  15b lshift 12; //   (+7)
	    4001b;  103341b;   0b lshift 12; //   (+10)
	      50b;   25011b;   0b lshift 12; //   (+11)
	    2147b;   21026b;   5b lshift 12; //  RDCS
	      47b;   35431b;   1b lshift 12; //   (+1)
	    4001b;  113033b;   1b lshift 12; //   (+2)
	      47b;   27035b;   1b lshift 12; //   (+3)
	   54150b;   65400b;  14b lshift 12; //   (+4)
		] )

	AddMicOvl(lv d0rdtpc,#11,14,table [
	    4000b;  125350b;   0b lshift 12; //  OVERLAYAREA
	      50b;  125004b;   5b lshift 12; //   (+1)
	   30020b;   77007b;   1b lshift 12; //   (+2)
	    1174b;  131010b;   5b lshift 12; //   (+3)
	    1104b;  123012b;   5b lshift 12; //   (+4)
	    4017b;  103015b;   5b lshift 12; //   (+5)
	    4150b;    3016b;   5b lshift 12; //   (+6)
	      37b;  137020b;   1b lshift 12; //   (+7)
	    1104b;  133027b;   1b lshift 12; //   (+10)
	   30047b;   27024b;   1b lshift 12; //  GETTPC
	   50150b;   27026b;  15b lshift 12; //   (+1)
	   44147b;   21031b;  15b lshift 12; //  NOTIFYBOTHWAYS
	   30050b;   25401b;   0b lshift 12; //   (+1)
	   66150b;   41014b;  14b lshift 12; //   (+2)
		] )

	AddMicOvl(lv d0wrttpc,#12,8,table [
	    4017b;  103002b;   5b lshift 12; //  OVERLAYAREA
	    4150b;    3004b;   5b lshift 12; //   (+1)
	      37b;  137006b;   5b lshift 12; //   (+2)
	    1100b;  111011b;   5b lshift 12; //   (+3)
	     147b;   21013b;   1b lshift 12; //   (+4)
	      45b;   37400b;   0b lshift 12; //   (+5)
	   40147b;   21017b;  15b lshift 12; //  RETLOC
	      50b;   25401b;   0b lshift 12; //   (+1)
		] )

	AddMicOvl(lv d0mem,#13,7,table [
	    4000b;  125350b;   0b lshift 12; //  OVERLAYAREA
	    2174b;    1004b;  15b lshift 12; //   (+1)
	      50b;   25601b;   1b lshift 12; //   (+2)
	      150b;   125011b;   0b lshift 12; // INTLK
	  142674b;    1006b;  1b lshift 12; //   (+4)
	  143274b;    1006b;  1b lshift 12; //   (+5)
	  143674b;    1007b;  1b lshift 12; //   (+6)
		] )

]


and AddMicOvl(lvStatic,index,len,mis) be
[	let Stat = GetStorage(3)
	rv lvStatic = Stat
//Build overlay descriptor
	Stat!0 = index
	Stat!1 = len
	Stat!2 = BootTable
//Insert absolute address and fixup parity for each m-i
	for I = 0 to len-1 do
	[ let DVec = mis+(3*I)
	  DVec!2 = DVec!2+#7500+I
	  GenCorrPar(DVec)
	]
	MBlock(BootTable,mis,3*len)
	BootTable = BootTable+3*len
	if BootTable ge (MIMtab+(3*MIMlen)) then MidasSwat(MIMovf)
]