-- beadsDefs.mesa

DIRECTORY InlineDefs:FROM"InlineDefs";
BeadsDefs:DEFINITIONS IMPORTS InlineDefs =BEGIN

PtrToCardArray:TYPE=POINTER TO ARRAY[0..2] OF CARDINAL;

wholeWire:INTEGER=10000;
CR:CHARACTER=’
;
Color:TYPE={none,r,b,g,y,o};
Direction:TYPE={h,v};
BeadType:TYPE={none,all,rg,rb,bg,tt,dd,ttV,ddV,endG,endR,endB,stub,
jctnG,jctnR,jctnB,bf,wireG,wireR,wireB,wireO,endO};

Stick:TYPE=RECORD[dir:Direction,color:Color,x,y,end:INTEGER];
maxStick:CARDINAL=300;
StickNo:TYPE=[0..maxStick];
StickStuff:TYPE=ARRAY StickNo OF Stick;

Contact:TYPE=RECORD[x,y:CARDINAL,t:BeadType];
maxContact:CARDINAL=200;
ContactNo:TYPE=[0..maxContact];
ConStuff:TYPE=ARRAY ContactNo OF Contact;

BeadStuff:TYPE=ARRAY BeadNo OF Bead;
BeadPtr:TYPE=LONG POINTER TO Bead;
Bead:TYPE=RECORD[
x,y:INTEGER,
h,w:INTEGER,
beadU,beadD,beadL,beadR,beadT:CARDINAL,
t:BeadType,
external:CARDINAL,
circuit:INTEGER,
wire:BOOLEAN,
dumB1:CARDINAL,
noodle:CARDINAL,
nextBelow:CARDINAL];
WorkStuff:TYPE=ARRAY BeadNo OF Work;
WorkPtr:TYPE=LONG POINTER TO Work;
Work:TYPE=RECORD[
sort:CARDINAL,
newY:INTEGER,
stiff,processed:BOOLEAN,
chain:CARDINAL,
backSort:CARDINAL,
processNo:CARDINAL,
oldY:INTEGER,
seen:CARDINAL];
BeadNo:TYPE=[0..2];
topBead:CARDINAL;
noBead:CARDINAL;
bead:BeadPtr;
work:WorkPtr;

desP:POINTER TO desStuff;
desStuff:TYPE=ARRAY BeadType OF DesRec;
DesRec:TYPE=RECORD[
level,short,h:INTEGER,
print:CHARACTER,
toCode:INTEGER,
toWire,toWireR,w,wsR,wsU,stickOutToRight,lessLevel,bitPerLevel:INTEGER←NULL,
bitPerLevelV:INTEGER←NULL,
rotate,rwc,uwc:BeadType←NULL,
color:Color←NULL,
trueIfTrans,blueOnly,rgOnly,rg:BOOLEAN←NULL,ug:BOOLEAN←NULL];
DesArray:TYPE=ARRAY [0..7] OF ARRAY [0..7] OF INTEGER;
Des4:DesArray;
Des5:DesArray;
Des6:DesArray;
Des7:DesArray;

maxY:INTEGER;
maxX:INTEGER;

getWhatYouSee,bendingWires,debugBeads,smallDisplay,fallBack:BOOLEAN;
trackBead:CARDINAL;

topNoodle:CARDINAL=2000; --=1000;

noodleChain:POINTER TO ARRAY [0..2] OF CARDINAL;
noodleDX:POINTER TO ARRAY [0..2] OF INTEGER;
noodleDY:POINTER TO ARRAY [0..2] OF INTEGER;
freeNoodleChain:CARDINAL;

BentWireData:TYPE=RECORD[this,next,old,oldOld:CARDINAL,
thisX,thisY,nextX,nextY,oldX,oldY,firstX,lastX,
dx,dy,nextdx,nextdy,olddx,olddy:INTEGER];
BentWire:TYPE=POINTER TO BentWireData;

--
procedures for looking at things
PrintBeads:PROCEDURE;
PrintGeometry:PROCEDURE[STRING];
PrintAllSticks:PROCEDURE[POINTER TO StickStuff,CARDINAL];
PrintAllContacts:PROCEDURE[POINTER TO ConStuff,CARDINAL];
PrintLong:PROCEDURE[i:LONG CARDINAL];
ShowStats:PROCEDURE[s:STRING,a,b:CARDINAL];
PrintDes:PROCEDURE[x,y,z:POINTER TO DesArray];
StartDisplay:PROCEDURE;
Display:PROCEDURE;
ManipulateDisplay:PROCEDURE RETURNS[BOOLEAN];

--major structural procedures
FromInputToBeads:PROCEDURE[STRING,BOOLEAN];
PutTheBeadsWhereTheyBelong:PROCEDURE[BOOLEAN,[0..256)];
Fall:PROCEDURE;
TurnWiresToBeads:PROCEDURE;
Local:PROCEDURE;
CleanUp:PROCEDURE;
Clean:PROCEDURE;

--utility routines
Rotate,Reflect,RotateBack:PROCEDURE;
InsertBead:PROCEDURE[Bead];
CheckBeads:PROCEDURE;
Deltas:PROCEDURE[i,j:CARDINAL] RETURNS[INTEGER,INTEGER];
IfTrans:PROCEDURE[CARDINAL,INTEGER] RETURNS[INTEGER];
InitBeadWork:PROCEDURE[CARDINAL,BeadPtr];
StartBentWire:PROCEDURE[CARDINAL,BentWire];
AdvanceBentWire:PROCEDURE[BentWire];
MergeLeft,MergeRight,ReleaseLink:PROCEDURE[j,old:CARDINAL];
WireFall:PROCEDURE[CARDINAL] RETURNS[BOOLEAN];
BeadFall:PROCEDURE[CARDINAL,INTEGER] RETURNS[BOOLEAN];
TurnWire:PROCEDURE[i:CARDINAL,bpi:BeadPtr];
FixWires:PROCEDURE;
FixWire:PROCEDURE[i:CARDINAL,bpi:BeadPtr];
ScourBeads:PROCEDURE;
ScavageBeads:PROCEDURE;
MakeCircuits:PROCEDURE;
WriteOneNumber:PROCEDURE[STRING,INTEGER];
FindBoundingBox:PROCEDURE;
EnumerateBeads:PROCEDURE[PROCEDURE[CARDINAL,BeadPtr]];
EnumerateSortedBottomUp:PROCEDURE[PROCEDURE[CARDINAL,BeadPtr]];
FullTest:PROCEDURE;

--////Start Below Defs/////
ProcessPair:TYPE=PROCEDURE[i,j:CARDINAL] RETURNS[ok:BOOLEAN];

noBelow,worstBelow:CARDINAL;
timeV5,timeV6:LONG CARDINAL;

AllocateBelow:PROCEDURE;
FreeBelow:PROCEDURE;
ResetBelow:PROCEDURE;
MakeBelow:PROCEDURE;
ShowBelow:PROCEDURE;
TryAllBelow:PROCEDURE[CARDINAL,ProcessPair] RETURNS[BOOLEAN];

--////End Below Defs/////


Variant:TYPE={tied,left,right,up,down,bead,wire};

Find
:PROCEDURE[b:BeadType,i:CARDINAL] RETURNS[CARDINAL];
NumRelatives:PROCEDURE[i:CARDINAL] RETURNS[CARDINAL];
AndOfRelatives:PROCEDURE[i,j:CARDINAL] RETURNS[BOOLEAN];
Attach:PROCEDURE[i:CARDINAL,variant:Variant,j:CARDINAL];
AttachAll:PROCEDURE[i,j:CARDINAL];
Move:PROCEDURE[variant:Variant,i:CARDINAL,dx,dy:INTEGER] RETURNS[r:BOOLEAN];
Initialize:PROCEDURE;
Related:PROCEDURE[i,j:CARDINAL] RETURNS[BOOLEAN];
Overlap:PROCEDURE[i,j:CARDINAL] RETURNS[BOOLEAN];
MinSepInY:PROCEDURE[i,j:CARDINAL] RETURNS[INTEGER];
MinSepInX:PROCEDURE[i,j:CARDINAL] RETURNS[INTEGER];
Put:PROCEDURE[i:CARDINAL,dx,dy,h,w:INTEGER];
Tied:PROCEDURE[i,j:CARDINAL] RETURNS[BOOLEAN];
InsertJogs:PROCEDURE[i:CARDINAL,horizontal,vertical:Variant];
RemovePaths:PROCEDURE[i,j:CARDINAL,v:Variant];
FindPath:PROCEDURE[i,j:CARDINAL,v:Variant] RETURNS[BOOLEAN];
RoomFor:PROCEDURE[i:CARDINAL] RETURNS[r:BOOLEAN];
Restore:PROCEDURE;
Abort:PROCEDURE;
Commit:PROCEDURE;
DeleteBead:PROCEDURE[i:CARDINAL];
Remove:PROCEDURE[i:CARDINAL];
ClearShift:PUBLIC PROCEDURE;
Shift:PROCEDURE[dx,dy:INTEGER] RETURNS[BOOLEAN];
Process:PROCEDURE;


Get:PROCEDURE[i:CARDINAL]RETURNS[BeadPtr]=INLINE
BEGIN RETURN[bead+InlineDefs.LongMult[i,SIZE[Bead]]]; END;

GetW:PROCEDURE[i:CARDINAL]RETURNS[WorkPtr]=INLINE
BEGIN RETURN[work+InlineDefs.LongMult[i,SIZE[Work]]]; END;

END..

The modules which implement Beadsdefs are:
BeadsCtl - which does very little except decide what should be done
CreateBeads - which makes beads out of Sticks
BeadsPrint - which makes the pretty pictures on the screen
- and formats major data structures for printing
- and holds global data and utility routines
BeadsBelow - whose job is to enumerate every bead "below" a bead
BeadsPut - places the beads in the compression logic
BeadsFall - straightens out bent wires
- lowers beads placed unnecessarily high
- combines overlapping beads
- Other micsellaneous wire related stuff
BeadsClean - Tries to reduce Capacitance by moving several beads
BeadsLocal - Makes Local transformations
- makes "free" transformations each pass
BeadsMove - Utility subroutines for BeadsLocal
BeadsTest - checks a bead figure for design rule violation

Start your study of Beadsdefs with the module called beadsCtl.
Then Glance at the start of beadsPrint to see what Deltas does.
Then leap into beadsPut.