-- u255Test.mesa  
-- L. Stewart, last updated: September 20, 1979  3:49 PM

DIRECTORY
  DisplayDefs: FROM "DisplayDefs",
  InlineDefs: FROM "InlineDefs",
  IODefs: FROM "IODefs",
  RamDefs: FROM "RamDefs",
  u255Defs: FROM "u255Defs",
  WFDefs: FROM "WFDefs";

u255Test: PROGRAM
  IMPORTS DisplayDefs, InlineDefs, IODefs, RamDefs, u255Defs, WFDefs =

  BEGIN OPEN InlineDefs, IODefs, u255Defs, WFDefs;
  sysDispPages: CARDINAL ← 40;
  useUC: BOOLEAN ← FALSE;
  low: ARRAY [0..177B] OF INTEGER;
  out: ARRAY [0..177B] OF INTEGER;
  high: ARRAY [0..177B] OF INTEGER;
  SetDisplay: PUBLIC PROCEDURE =
    BEGIN
    WF0["Display Pages? "L];
    sysDispPages ← ReadNumber[sysDispPages, 10];
    WFCR[];
    DisplayDefs.SetSystemDisplaySize[sysDispPages, (3*sysDispPages)/2];
    END;
  EndOfString: PUBLIC PROCEDURE [c: CHARACTER] RETURNS [b: BOOLEAN] =
    BEGIN SELECT c FROM CR, TAB, LF, SP => b ← TRUE; ENDCASE => b ← FALSE; END;
  YesOrNo: PUBLIC PROCEDURE [s: STRING] RETURNS [b: BOOLEAN] =
    BEGIN
    WF1["%s ", s];
    SELECT ReadChar[] FROM 'Y, 'y, CR => b ← TRUE; ENDCASE => b ← FALSE;
    WF0[IF b THEN "Yes*n" ELSE "No*n"];
    END;
  ToggleUC: PROCEDURE =
    BEGIN
    useUC ← NOT useUC;
    WF0["Microcode is "L];
    WF0[IF useUC THEN "ON" ELSE "OFF"];
    WFCR[];
    END;
  EC: PROCEDURE [v: INTEGER] RETURNS [INTEGER] =
    BEGIN IF useUC THEN RETURN[Encode[v]] ELSE RETURN[EncodeMesa[v]]; END;
  DC: PROCEDURE [v: INTEGER] RETURNS [INTEGER] =
    BEGIN IF useUC THEN RETURN[Decode[v]] ELSE RETURN[DecodeMesa[v]]; END;
  TestDecode: PROCEDURE =
    BEGIN
    i, o: INTEGER;
    WF0["Decode*n"L];
    WF0[" 12 bit is right justified, for convenience.*n"L];
    WF0[" input in octal, end with DEL.*n"L];
    DO
      i ← ReadNumber[i, 8 ! Rubout => GOTO Reject];
      IF i NOT IN [0..377B] THEN BEGIN WriteLine["Value too big."L]; LOOP; END;
      o ← DC[i];
      WF2["   input (oct): %3b,  output (dec): %6d *n", i, o/16];
      ENDLOOP;
    EXITS Reject => WFCR[];
    END;
  TestEncode: PROCEDURE =
    BEGIN
    i, in, o: INTEGER;
    WF0["Encode*n"L];
    WF0[" 12 bit is right justified, for convenience.*n"L];
    WF0[" input in decimal, end with DEL.*n"L];
    DO
      i ← ReadNumber[i, 10 ! Rubout => GOTO Reject];
      IF i NOT IN [-2048..2047] THEN
        BEGIN WriteLine["Value too big."L]; LOOP; END;
      in ← BITSHIFT[i, 4];  -- left justify
      o ← EC[in];
      WF2["   pcm (dec): %6d,  u-255 (oct): %3b *n", i, o];
      ENDLOOP;
    EXITS Reject => WFCR[];
    END;
  PrintDecodeTable: PROCEDURE =
    BEGIN
    i, j, k, l: INTEGER;
    WF0["Decode Table*n*n   "L];
    FOR j IN [0..7] DO WF1["%6d ", j]; ENDLOOP;
    WFCR[];
    FOR i IN [0..15] DO
      WF1["%2b:", i];
      FOR j IN [0..7] DO
        k ← i + (j*16); l ← BITSHIFT[DC[k], -4]; WF1["%6d ", l]; ENDLOOP;
      WFCR[];
      ENDLOOP;
    END;
  PrintEncodeTable: PROCEDURE =
    BEGIN
    j, k, l: INTEGER;
    WF0["Encode Table*n*n    "L];
    FOR j IN [0..177B] DO low[j] ← out[j] ← high[j] ← -1; ENDLOOP;
    l ← 0;
    low[0] ← 0;
    FOR j IN [0..2047] DO
      k ← EC[j*16];
      IF k < l THEN EXIT;
      out[k] ← DC[k]/16;
      IF k > l THEN low[k] ← j ELSE high[l] ← j;
      l ← k;
      ENDLOOP;
    WF0["code     low    out   high*n"];
    FOR j IN [0..177B] DO
      WF4["%5b   %4d   %4d   %4d*n", j, low[j], out[j], high[j]]; ENDLOOP;
    END;
  LoadMicrocode: PROCEDURE =
    BEGIN
    s: STRING ← [40];
    uc: RamDefs.MuImage;
    WF0["Load microcode.*n  microcode.br filename? "];
    ReadID[s];
    uc ← RamDefs.ReadPackedMuFile[s];
    IF RamDefs.LoadRamAndBoot[uc, FALSE] # 0 THEN WF0[" ...failed.*n"]
    ELSE WF0[" ...OK.*n"];
    RamDefs.ReleaseMuImage[uc];
    END;

  u255MainLoop: PUBLIC PROCEDURE =
    BEGIN
    c: CHARACTER;
    DO
      ENABLE Rubout => CONTINUE;
      WF0[">"];
      SELECT ReadChar[] FROM
        '  => WF0["*n"L];
        '. => WF0[".*n"L];
        '- =>
          BEGIN
          WF0["-- "L];
          UNTIL (c ← ReadChar[]) = CR DO WriteChar[c]; ENDLOOP;
          WF0["*n"L];
          END;
        'L, 'l => LoadMicrocode[];
        'D, 'd => TestDecode[];
        'E, 'e => TestEncode[];
        'M, 'm =>
          BEGIN
          WF0["Miscellaneous "L];
          SELECT ReadChar[] FROM
            'D, 'd => SetDisplay[];
            '? => WF0[" Display.*n"L];
            ENDCASE => WF0["???*n"L];
          END;
        'P, 'p =>
          BEGIN
          WF0["Print "L];
          SELECT ReadChar[] FROM
            'D, 'd => PrintDecodeTable[];
            'E, 'e => PrintEncodeTable[];
            '? => WF0[" Decode Table, Encode Table. *n"L];
            ENDCASE => WF0["???*n"L];
          END;
        'Q, 'q => IF YesOrNo["Quit."L] THEN RETURN[];
        'U, 'u => ToggleUC[];
        '? =>
          BEGIN
          WF0["Decode "];
          WF0["Encode "];
          WF0["Filter "];
          WF0["Load Microcode "];
          WF0["Miscillaneous "];
          WF0["Print "];
          WF0["Quit "];
          WF0["u(Toggle ucode) "];
          WF0["*n"];
          END;
        ENDCASE => WF0["???*n"L];
      ENDLOOP;
    END;

  -- Main Line Code
  u255MainLoop[];
  END.