-- XMesaOps.Mesa  Edited for XMesa by Levin on:  March 2, 1979  6:49 PM

DIRECTORY
  AllocDefs: FROM "AllocDefs" USING[AllocInfo],
  AltoDefs: FROM "altodefs" USING [BytesPerPage, MaxVMPage, PageNumber, PageSize],
  AltoFileDefs: FROM "altofiledefs" USING [FP, vDA],
  MemoryOps: FROM "memoryops" USING [BankIndex],
  SegmentDefs: FROM "segmentdefs" USING [FrobLink, FrobSize, ObjectType, SegmentType,
	FileSegmentClass, FileHandle, FileSegmentHandle, LockCount, SegmentLocation, FileHint,
	RemoteSegProc, RefCount, SegCount],
  XMesaDefs: FROM "XMesaDefs" USING [MaxXPage];

XMESA: DEFINITIONS SHARES SegmentDefs =
  BEGIN
  OPEN AltoDefs, SegmentDefs;

  -- The following section consists of data structures that logically belong in SegmentDefs.
  -- With a little luck, in Mesa 6.0 we should be able to eliminate all this stuff!

  -- Primitive System Objects: 

  XSegmentObject: PRIVATE TYPE = segment XObject;
  XFileObject: PRIVATE TYPE = file XObject;
  XSegmentHandle: PRIVATE TYPE = POINTER TO XSegmentObject;
  XFileHandle: PRIVATE TYPE = POINTER TO XFileObject;
  XFileSegmentObject: PRIVATE TYPE = file XSegmentObject;
  XFileSegmentHandle: PRIVATE TYPE = POINTER TO XFileSegmentObject;
  XDataSegmentObject: PRIVATE TYPE = data XSegmentObject;
  XDataSegmentHandle: PRIVATE TYPE = POINTER TO XDataSegmentObject;

  XObject: PRIVATE TYPE = RECORD [
    busy: PRIVATE BOOLEAN,
    body: SELECT tag: ObjectType FROM
      free => [
	seal: PRIVATE [0..37B],
	size: PRIVATE FrobSize,
	fwdp, backp: PRIVATE FrobLink],
      segment => [
	SELECT type: SegmentType FROM
	  data => [
	    unused: [0..1],
	    XMpage: [0..XMesaDefs.MaxXPage],
	    pages: [1..MaxVMPage+1],
	    VMpage: [0..MaxVMPage]],
	  file => [
	    swappedin: BOOLEAN,
	    read, write: BOOLEAN,
	    class: FileSegmentClass,
	    VMpage: [0..MaxVMPage],
	    file: FileHandle,
	    inuse: BOOLEAN,
	    base: PageNumber,
	    pages: [1..MaxVMPage+1],
	    lock: LockCount,
	    location: SELECT loc: SegmentLocation FROM
	      disk => [
		hint: FileHint],
	      remote => [
		proc: RemoteSegProc,
		info: POINTER TO XSegInfo],
	      ENDCASE],
	  ENDCASE],
      file => [
	open: BOOLEAN,
	length: BOOLEAN,
	lengthvalid: BOOLEAN,
	read, write, append: BOOLEAN,
	lock: LockCount,
	lengthchanged: BOOLEAN,
	unused: [0..177B],
	swapcount: RefCount,
	segcount: SegCount,
	fp: AltoFileDefs.FP],
      length => [
	unused: [0..7B],
	byte: [0..BytesPerPage],
	page: PageNumber,
	file: FileHandle,
	da: AltoFileDefs.vDA],
      ENDCASE];

  XMremote: RemoteSegProc = LOOPHOLE[123123B];

  XSegInfo: PRIVATE TYPE = RECORD[
      seal: [0..77B], XMpage: [0..XMesaDefs.MaxXPage],
      body: SELECT OVERLAID * FROM
	free => [link: POINTER TO XSegInfo, freeCount: CARDINAL],
	hint => [hint: FileHint],
	ENDCASE];
  InUseSeal: [0..77B] = 63B;
  FreeSeal: [0..77B] = 14B;
  XSegInfoIndex: TYPE = [0..(PageSize-2)/SIZE[XSegInfo]);	-- 2 words for header at front of page
  XSegInfoTable: PRIVATE TYPE = RECORD[
      nextPage: [0..MaxVMPage],
      freeCount: [0..LAST[XSegInfoIndex]+1],
      freeHead: POINTER TO XSegInfo,
      table: ARRAY XSegInfoIndex OF XSegInfo];

  ChocolateToVanilla: PROCEDURE [XFileSegmentHandle, PageNumber] RETURNS [PageNumber];
  VanillaToChocolate: PROCEDURE [FileSegmentHandle, PageNumber] RETURNS [PageNumber];

  -- This section consists of private agreements between the modules that comprise the
  -- XMesa extensions.  Some of these definitions are required by the XMesa extensions that
  -- reside in the debugger.

  BankOption: TYPE = [0..4];
  AnyBank: BankOption = 4;
  Bank1X: PageNumber = 256; Bank2X: PageNumber = 257; Bank3X: PageNumber = 258;

  BankMasks: ARRAY MemoryOps.BankIndex OF CARDINAL = [10B, 4B, 2B, 1B];

  SwapOutFileSegment: PROCEDURE[SegmentDefs.FileSegmentHandle];

  PageMapAllocInfo: AllocDefs.AllocInfo =
	AllocDefs.AllocInfo[0, easy, topdown, initial, other, FALSE, FALSE];

  END.