-- initialization module of silicon (pretty picture) program
-- modified by McCreight, December 30, 1982  4:51 PM

DIRECTORY
  ChipUserInt,
  multiGraphicsDefs,
  InlineDefs,
  ImageDefs,
  AltoFileDefs,
  StreamDefs,
  StringDefs,
  IODefs,
  MiscDefs,
  SegmentDefs,
  ppdddefs,ppddefs,
  ppdefs,
  ppoutdefs,
  ppMainDefs;

ppInit: PROGRAM
  IMPORTS
    ImageDefs, ppdddefs, multiGraphicsDefs, ppdefs, ppddefs, ppMainDefs,
    InlineDefs, StreamDefs, IODefs, MiscDefs, SegmentDefs, StringDefs,
    ppoutdefs, ChipUserInt
  EXPORTS ppdefs = PUBLIC
  BEGIN
  OPEN ppdefs, ppdddefs,ppddefs, ppMainDefs, multiGraphicsDefs, InlineDefs, StringDefs, ppoutdefs;

  --core banks 4 & 5 use scheme (NOTE: set mesabanks to 170000B)
  --	1000000B:	color buffer (160x481 = 76960 = 226240B)
  --			empty (8 = 10B)
  --	1226250B:	color table (76 = 114B)
  --			empty (12 = 14B)
  --	1226400B:	B&W table (808x38 = 30704 = 73760B)
  --			empty (16 = 20B)
  --	1322400B:	color tables (avail:23296 = 55400B
  --				each table is 48 = 60B words,
  --				so 60 tables is 2880 wds
  --				or avail is room for 485 tables)

  colorWasOn: BOOLEAN;

QuitSig:PUBLIC SIGNAL = CODE;
RestSig:PUBLIC SIGNAL = CODE;

cMos:PUBLIC BOOLEAN ← FALSE;

  fontNumber: CARDINAL←0;
  fontNumberMax: CARDINAL = 3;
cfa:POINTER TO AltoFileDefs.CFA;
fa:AltoFileDefs.FA;
fp:AltoFileDefs.FP;
cfile:StreamDefs.DiskHandle;
chr:CHARACTER;
dotSeen:BOOLEAN;

  cmdLineFile: BOOLEAN;
  cmFileName:STRING←[30];
  ii, jj: CARDINAL;

  blankColors:ARRAY [0..48) OF CARDINAL←ALL[255];


  MaintainColorSanity: ImageDefs.CleanupProcedure =
    BEGIN OPEN ChipUserInt;
    SELECT why FROM
      Abort, OutLd =>
        {colorWasOn ← ColorIsOn[]; ColorOff[]};
      InLd => IF colorWasOn THEN ColorOn[];
      ENDCASE => ERROR;
    END;

  cleanupItem: ImageDefs.CleanupItem ←
    [link: NIL,
    mask: ImageDefs.CleanupMask[Abort]+
      ImageDefs.CleanupMask[OutLd]+
      ImageDefs.CleanupMask[InLd],
    proc: MaintainColorSanity
    ];


  config: SegmentDefs.MemoryConfig ←
    SegmentDefs.GetMemoryConfig[];

nullChrThing: keCheckProc = {return←FALSE; execute←TRUE};

  IF InlineDefs.BITAND[config.banks, 6000B]#0 THEN
    BEGIN OPEN IODefs;
    WriteLine["MesaBanks must be set to 171777 from Executive"L];
    WriteLine["and debugger must be reinstalled."L];
    WriteLine["(Confirm)"L];
    []←ReadChar[];
    ImageDefs.StopMesa[];
    END;



  cellStack ← NIL;
  masterList ← NIL;
  initAll[]; -- starts repainting immediately
  ImageDefs.AddCleanupProcedure[@cleanupItem];

	cmdLineFile←dotSeen←FALSE;
	cfa←MiscDefs.CommandLineCFA[];
	fa←cfa↑.fa;
	fp←cfa↑.fp;
	cfile←StreamDefs.CreateByteStream[
		SegmentDefs.InsertFile[@fp,SegmentDefs.Read],StreamDefs.Read];
	StreamDefs.JumpToFA[cfile,@fa];
	IODefs.SetInputStream[cfile];
	cmFileName.length←0;
	UNTIL cfile.endof[cfile] DO
	  chr←IODefs.ReadChar[];
	  IF chr='. THEN
	    BEGIN
		dotSeen←TRUE;
	    END;
	  IF chr='  THEN
	    BEGIN
		IF cmFileName.length>0 THEN EXIT;
	    END
	   ELSE IF chr=15C THEN EXIT
	   ELSE AppendChar[cmFileName,chr];
	ENDLOOP;
	cfile.destroy[cfile];
	IF cmFileName.length>0 THEN
	    BEGIN
		cmdLineFile←TRUE;
		IF NOT dotSeen THEN
		    BEGIN
			AppendString[cmFileName,".chip"];
		    END;
	    END;

  DO --  UNTIL quitflg DO

    InitHeap[1];

    masterList ← NIL;
    cellStack ← NIL;
    noChange ← FALSE;
    wClosestDisp ← FALSE;
    wiringOrnSet ← FALSE;
    moving ← FALSE;
    copying ← FALSE;
    areaSel ← FALSE;
    moveMarked ← FALSE;
    cellList ← NIL;
    pushLevel ← 0;
    currentLevel ← cut;
    curcol ← 0;
    cellNameMode ← FALSE;

    dChange ← TRUE;

    mode ← wire;
    setSelColor[0];
    unDelPnt ← NIL;
    unDelGrpCnt ← unDelItemCnt ← 0;
    selCount ← 0;
    doingAreaSel ← FALSE;
    locCell←"";
    locIName←"";
    insideCell←"";

    IF cmdLineFile THEN
      BEGIN
	cmdLineFile←FALSE;
	dChange ← TRUE;
	IF openIfile[cmFileName] THEN
	    BEGIN
		typeOut["", "Reading File:", cmFileName];
		[masterList, cellList] ← readAll[];
		closeFile[];
		FOR ii IN [0..cmFileName.length) DO IF cmFileName[ii]='.
		  THEN BEGIN cmFileName.length←ii;EXIT;END;ENDLOOP;
		fileName←newString[cmFileName];
		anyChanges ← TRUE;
		sinceIOchanges ← FALSE;
	    END;
	refreshTypeInScreen[];
      END;

    DO OPEN ChipUserInt;

      SquirrelAway: PROC =
        BEGIN
        IF cellStack#NIL AND HeSaysYes["You're pushed down below the top level design",
          "Shall I try to pop you back to top level first?"]
        THEN
          WHILE cellStack#NIL DO
            doPop[! ANY => EXIT];
            ENDLOOP;
        IF openOfile["ChipmonkBug.chip", TRUE] THEN
          {writeAll[masterList, cellList]; closeFile[]};
        END;

      [] ← CmdTwerp[nullChrThing
        ! Punt => CONTINUE; -- do another command
        RestSig => EXIT; -- restart Chipmonk
        QuitSig => GOTO ChipmonkExit; -- he asked for it
        ABORTED =>
          BEGIN
          IF sinceIOchanges AND
            HeSaysYes["Shall I squirrel away the design as best I can before",
            "leaving Chipmonk?",
            "(will use ChipmonkBug.chip)"] THEN SquirrelAway[];
          GOTO ChipmonkExit;
          END;
        UNWIND => ERROR;
        ANY => 
          BEGIN
          SELECT sinceIOchanges FROM
            FALSE => Explain["We're going into the debugger but I think your design is",
              "correct as it sits in its file."];
            ENDCASE => IF HeSaysYes["Shall I squirrel away the design as best I can before",
              "going into the debugger?",
              "(will use ChipmonkBug.chip)"] THEN SquirrelAway[];
          REJECT;
          END];
      ENDLOOP;
    REPEAT ChipmonkExit => NULL;
    ENDLOOP;

  writeCTab[];
  SetColorTable[LOOPHOLE[@blankColors]];
  ReplaceColorArea[x1: 0, x2: colWidth-1, y1: 0, y2: colHeight-1, c: 0];

  FOR ii IN [1..10000] DO -- wait for Godot.
    jj: CARDINAL ← ii+1;
    jj←jj/2;
  ENDLOOP;

  ChipUserInt.ColorOff[];
  ImageDefs.RemoveCleanupProcedure[@cleanupItem];
  ImageDefs.StopMesa[];

  END.