-- File: JaMCIF.mesa
-- Written by Martin Newell February 1980
-- JaM interface for running CIFControl.mesa
-- Last edited: November 5, 1981 10:31 AM

DIRECTORY

CIFControlDefs: FROM "CIFControlDefs" USING [ParseCIF, EndCIF],
CIFDevicesDefs: FROM "CIFDevicesDefs" USING [SelectDevice, OutputDevice,
Draw, DrawFrame, SetScale, SetClipRegion, LoadLayer, SetLayerVisible,
LookupLayerString],
CIFUtilitiesDefs: FROM "CIFUtilitiesDefs" USING [Rectangle, GetClipRectangle,
SetUniformView, TrackBox, GetBaseClipRectangle, GetDisplayContext,
SetClipRectangle, InitCedarGraphics, ClearClipRectangle],
DisplayDefs: FROM "DisplayDefs" USING [SetTypeScript],
Graphics: FROM "Graphics" USING [DisplayContext, InitContext, DrawRectangle,
SetTexture, grey, Texture, Translate],
IcarusInputDefs: FROM "IcarusInputDefs" USING [ParseIcarus],
IntDefs: FROM "IntDefs" USING [InitInterpreter, IBoundBox],
ParserDefs: FROM "ParserDefs" USING [InitParser],
IntTransDefs: FROM "IntTransDefs" USING [Push, Pop, Rotate],
IODefs: FROM "IODefs" USING [WriteLine],
JaMFnsDefs: FROM "JaMFnsDefs" USING [Register, PopString, PopInteger, PushBoolean,
PushReal, GetReal],
KeyDefs: FROM "KeyDefs" USING [MouseButton],
OutputDefs: FROM "OutputDefs" USING [InitOutput],
StreamDefs: FROM "StreamDefs" USING [NewByteStream, Read, Write,
Append],
StringDefs: FROM "StringDefs" USING [AppendChar, AppendString];

JaMCIF: PROGRAM
IMPORTS CIFDevicesDefs, CIFUtilitiesDefs, DisplayDefs, Graphics, IcarusInputDefs, ParserDefs, IntDefs, IntTransDefs, IODefs, JaMFnsDefs, CIFControlDefs, OutputDefs, StreamDefs, StringDefs =
BEGIN
OPEN CIFDevicesDefs, CIFUtilitiesDefs, DisplayDefs, Graphics, IcarusInputDefs, IntDefs, IODefs, JaMFnsDefs, KeyDefs, CIFControlDefs, OutputDefs, StreamDefs, StringDefs;

--Initialization

InitView: PROCEDURE =
BEGIN --expects nothing
cr: Rectangle ← GetBaseClipRectangle[];
InitContext[GetDisplayContext[]];
SetClipRectangle[cr];
END;

--I/O

CallParseCIF: PROCEDURE =
BEGIN --expects <filename> (STRING)
--returns <ok> (BOOLEAN)
filename: STRING ← [50];
PopString[filename];
PushBoolean[ParseCIF[DefaultExtension[filename,"cif"]]];
[,,,] ← IBoundBox[];
IntTransDefs.Push[]; --to allow for global transformation
END;

CallParseIcarus: PROCEDURE =
BEGIN --expects <filename, icunits, cifunits>
--returns <ok> (BOOLEAN)
cifunits: LONG INTEGER ← PopInteger[];
icunits: LONG INTEGER ← PopInteger[];
filename: STRING ← [50];
ok: BOOLEAN;
nDefs: INTEGER;
PopString[filename];
[ok,nDefs] ← ParseIcarus[DefaultExtension[filename,"ic"], icunits, cifunits, 1];
PushBoolean[ok];
IntTransDefs.Push[]; --to allow for global transformation
END;

CallEndCIF: PROCEDURE =
BEGIN --expects nothing
EndCIF[];
END;

CallIBoundBox: PROCEDURE =
BEGIN --expects nothing, returns <left,bottom,right,top>
left,right,bottom,top: LONG INTEGER;
[left,right,bottom,top] ← IBoundBox[];
PushReal[left];
PushReal[bottom];
PushReal[right];
PushReal[top];
END;

--Device selection

Select: PROCEDURE =
BEGIN --expects <<device dependent parameters>, deviceName> (STRING)
deviceName: STRING ← [20];
PopString[deviceName];
[] ← SelectDevice[deviceName];
END;

--Device output

Output: PROCEDURE =
BEGIN --expects <parameters dependent on current device>
OutputDevice[];
END;

CallDraw: PROCEDURE =
BEGIN
Draw[];
END;

--Viewing control

CallDrawFrame: PROCEDURE =
--draw frame around clipping region, partitioned as appropriate for device
BEGIN
DrawFrame[];
END;

CallTrackBox: PROCEDURE =
BEGIN --expects <x,y, mouse(4,2,1)> (REAL)
mouseButtons: KeyDefs.MouseButton;
mouse: INTEGER ← PopInteger[];
y: REAL ← GetReal[];
x: REAL ← GetReal[];
SELECT mouse FROM
4 => mouseButtons ← Red;
2 => mouseButtons ← Yellow;
1 => mouseButtons ← Blue;
ENDCASE => RETURN;
TrackBox[x,y, mouseButtons];
END;

Expand: PROCEDURE =
BEGIN --expects <left,bottom,right,top> (REAL)
r: Rectangle ← GetClipRectangle[];
top: REAL ← GetReal[];
right: REAL ← GetReal[];
bottom: REAL ← GetReal[];
left: REAL ← GetReal[];
SetUniformView[[MIN[left,right],MIN[bottom,top],MAX[left,right],MAX[bottom,top]], r];
END;

Contract: PROCEDURE =
BEGIN --expects <left,bottom,right,top> (REAL)
r: Rectangle ← GetClipRectangle[];
top: REAL ← GetReal[];
right: REAL ← GetReal[];
bottom: REAL ← GetReal[];
left: REAL ← GetReal[];
SetUniformView[r, [MIN[left,right],MIN[bottom,top],MAX[left,right],MAX[bottom,top]]];
END;

Scroll: PROCEDURE =
BEGIN --expects <xfrom,yfrom, xto,yto> (REAL)
dc: DisplayContext ← GetDisplayContext[];
yto: REAL ← GetReal[];
xto: REAL ← GetReal[];
yfrom: REAL ← GetReal[];
xfrom: REAL ← GetReal[];
Translate[dc, [xto-xfrom, yto-yfrom]];
END;

SetRotate: PROCEDURE =
BEGIN --expects <xRot,yRot> (INTEGER)
yRot: LONG INTEGER ← PopInteger[];
xRot: LONG INTEGER ← PopInteger[];
IntTransDefs.Pop[];
IntTransDefs.Push[];
IntTransDefs.Rotate[xRot,yRot];
[] ← IBoundBox[]; --to cause bounding boxes to be rebound
END;

CallSetScale: PROCEDURE =
BEGIN --expects <factor> (REAL)
--sets scale to map from chip coords to currently selected output device coords
factor: REAL ← GetReal[];
SetScale[factor];
END;

CallSetClipRegion: PROCEDURE =
BEGIN --expects <left,bottom,right,top> (REAL)
rt: Rectangle;
rt.ury ← GetReal[];
rt.urx ← GetReal[];
rt.lly ← GetReal[];
rt.llx ← GetReal[];
SetClipRegion[rt];
END;

CallGetClipRegion: PROCEDURE =
BEGIN --returns <left,bottom,right,top> (REAL)
rt: Rectangle ← GetClipRectangle[];
PushReal[rt.llx];
PushReal[rt.lly];
PushReal[rt.urx];
PushReal[rt.ury];
END;

CallSetLayerVisible: PROCEDURE =
BEGIN --expects <layer> (STRING)
layer: STRING ← [10];
PopString[layer];
SetLayerVisible[LookupLayerString[layer], TRUE];
END;

CallSetLayerInVisible: PROCEDURE =
BEGIN --expects <layer> (STRING)
layer: STRING ← [10];
PopString[layer];
SetLayerVisible[LookupLayerString[layer], FALSE];
END;

--General Device parameters

CallLoadStipple: PROCEDURE =
BEGIN --expects <layer g0 g1 g2 g3> (STRING, INTEGERs)
g3: CARDINAL ← PopInteger[];
g2: CARDINAL ← PopInteger[];
g1: CARDINAL ← PopInteger[];
g0: CARDINAL ← PopInteger[];
layer: STRING ← [10];
PopString[layer];
LoadLayer[LookupLayerString[layer], g0, g1, g2, g3];
END;

CallLoadColor: PROCEDURE =
BEGIN --expects <layer hue sat bri> (STRING, INTEGERs)
bri: CARDINAL ← PopInteger[];
sat: CARDINAL ← PopInteger[];
hue: CARDINAL ← PopInteger[];
layer: STRING ← [10];
PopString[layer];
LoadLayer[LookupLayerString[layer], hue, sat, bri, 0];
END;


--Misc

CallDrawBoxArea: PROCEDURE =
BEGIN --expects <left,bottom,right,top> (REAL)
top: REAL ← GetReal[];
right: REAL ← GetReal[];
bottom: REAL ← GetReal[];
left: REAL ← GetReal[];
dc: DisplayContext ← GetDisplayContext[];
SetTexture[dc, grey];
DrawRectangle[dc, [left,bottom],[right,top]];
END;

StippleBox: PROCEDURE =
BEGIN --expects <left,bottom,right,top, layer#> (REALs, INTEGER)
layer: CARDINAL ← PopInteger[];
top: REAL ← GetReal[];
right: REAL ← GetReal[];
bottom: REAL ← GetReal[];
left: REAL ← GetReal[];
dc: DisplayContext ← GetDisplayContext[];
SetTexture[dc, Stipple[layer]];
DrawRectangle[dc, [left,bottom],[right,top]];
END;

Erase: PROCEDURE =
BEGIN --erase clip region
ClearClipRectangle[];
END;


--Private procedures--

MakeLog: PROCEDURE =
BEGIN
WriteLine["Typescript on Magic.log"];
SetTypeScript[NewByteStream["Magic.log", Read+Write+Append]];
END;

DefaultExtension: PUBLIC PROCEDURE[name,ext: STRING] RETURNS[STRING] =
--set extension if not already present
BEGIN
i: CARDINAL;
FOR i IN [0..name.length) DO
IF name[i]=’. THEN RETURN[name];
ENDLOOP;
AppendChar[name,’.];
AppendString[name,ext];
RETURN[name];
END;


Stipple: ARRAY [0..7] OF Texture ← [
000020B,--implant
040501B,--diffusion
014102B,--poly
165627B,--contact
000050B,--metal
001110B,--buried
001122B,--glass
177777B];--undef

--*** START Code ***

InitCedarGraphics[];
InitView[];
MakeLog[];

--Initial initialization of modules
IF ~ParserDefs.InitParser[] THEN WriteLine["Initial InitParser Error"];
IF ~IntDefs.InitInterpreter[] THEN WriteLine["Initial InitInterpreter Error"];
IF ~OutputDefs.InitOutput[] THEN WriteLine["Initial InitOutput Error"];

--Initialization
Register["initview",InitView];

--I/O
Register["parsecif",CallParseCIF];
Register["parseic",CallParseIcarus];
Register["endcif",CallEndCIF];
Register["boundbox",CallIBoundBox];

--Device selection
Register["select",Select];

--Device output
Register["output",Output];
Register["drawscreen",CallDraw];

--Viewing control
Register["drawframe",CallDrawFrame];
Register["trackbox",CallTrackBox];
Register["expand",Expand];
Register["contract",Contract];
Register["scroll",Scroll];
Register["rotate",SetRotate];
Register["setscale",CallSetScale];
Register["setclipregion",CallSetClipRegion];
Register["getclipregion",CallGetClipRegion];
Register["vis",CallSetLayerVisible];
Register["invis",CallSetLayerInVisible];

--General Device parameters
Register["loadstipple",CallLoadStipple];
Register["loadcolor",CallLoadColor];

--Misc
Register["drawboxarea",CallDrawBoxArea];
Register["stipplebox",StippleBox];
Register["erase",Erase];

END.