-- u-255 law conversion
-- takes 12 bit linear pcm and converts to u-255 8 bit and back
-- L. Stewart, last updated: September 20, 1979 3:47 PM
DIRECTORY
u255Defs: FROM "u255Defs",
InlineDefs: FROM "InlineDefs",
Mopcodes: FROM "Mopcodes";

u255
: PROGRAM
IMPORTS InlineDefs
EXPORTS u255Defs =
BEGIN OPEN InlineDefs;
u255Encode: CARDINAL = 562B;
u255Decode: CARDINAL = 563B;
Encode: PUBLIC PROCEDURE [v: INTEGER] RETURNS [INTEGER] =
BEGIN
RETURN[uCodeEncode[v]];
END;
uCodeEncode: PROCEDURE [v: INTEGER] RETURNS [INTEGER] =
MACHINE CODE BEGIN
Mopcodes.zLIW, u255Encode/256, u255Encode MOD 256;
Mopcodes.zJRAM;
END;
EncodeMesa: PUBLIC PROCEDURE [v: INTEGER] RETURNS [INTEGER] =
BEGIN
exp: INTEGER ← 7;
sign: BOOLEAN ← v<0;
BEGIN
IF sign THEN v←-v;
v ← v+204B;
IF v<0 THEN GOTO Overflow;
v ← BITSHIFT[v, -2]; -- align correctly for exp=7
-- This loop will definitely terminate before exp goes negative.
WHILE BITAND[v,10000B]=0 DO
exp ← exp-1;
v ← BITSHIFT[v,1];
ENDLOOP;
v ← BITSHIFT[v,-8];
v ← BITAND[v, 17B];
v ← v+BITSHIFT[exp,4];
IF sign THEN v ← v+200B;
RETURN[v];
EXITS
Overflow => IF sign THEN RETURN[377B] ELSE RETURN[177B];
END;
END;
Decode: PUBLIC PROCEDURE [v: INTEGER] RETURNS [INTEGER] =
BEGIN
RETURN[uCodeDecode[v]];
END;
uCodeDecode: PROCEDURE [v: INTEGER] RETURNS [INTEGER] =
MACHINE CODE BEGIN
Mopcodes.zLIW, u255Decode/256, u255Decode MOD 256;
Mopcodes.zJRAM;
END;
DecodeMesa: PUBLIC PROCEDURE [v: INTEGER] RETURNS [INTEGER] =
BEGIN
exp: INTEGER ← BITAND[7B,BITSHIFT[v,-4]];
sign: BOOLEAN ← BITAND[200B,v]#0;
v ← BITAND[v,17B];
v ← BITSHIFT[v,3];
v ← v+204B;
v ← BITSHIFT[v, exp];
v ← v-204B;
IF sign THEN v ← -v;
RETURN[v];
END;
-- Main Line Code
END.