(*
    Title: 	Fields - Definitions
    LastEdit:	"Fri Jan 29 11:16:07 1988"
    Author: 	Mick Jordan
		Cambridge University Computer Laboratory

    Bit field insertion/extraction on CARDINALS.
    The bits are numbered from 0 to BITSPERWORD-1
    where 0 is the least significant bit of the word.
    All bit field indices are inclusive.
*)

DEFINITION MODULE Fields;
FROM SYSTEM IMPORT WORD, BITSPERWORD, BYTESPERWORD;
FROM SystemTypes IMPORT Byte, Short;

EXPORT QUALIFIED 
    Extract, ExtractByte, ExtractShort,
    Insert, InsertByte, InsertShort;

CONST ShortSPERWORD = BITSPERWORD DIV 16;

TYPE
    WordBitRange = [0 .. BITSPERWORD-1];
    ShortRange = [0 .. ShortSPERWORD-1];
    ByteRange = [0 .. BYTESPERWORD-1];

PROCEDURE Extract(v: WORD; low, high: WordBitRange): CARDINAL;
(*  extract bits 'low' to 'high' from 'v' and return
    the right-justified result.
*)
PROCEDURE Insert(VAR v: WORD; low, high: WordBitRange; field: CARDINAL);
(*  insert 'field', appropriately shifted, in bit positions
    'low' to 'high' in 'v'.  No check is made that that the bits of 'field'
    lie within the specified bit range.
*)
PROCEDURE ExtractByte(v: WORD; byteno: ByteRange): Byte;
(*  get byte 'byteno', in order of address,:
        on VAX 0=ls byte, 3=ms byte
        on 68K 3=ls byte, 0=ms byte
*)
PROCEDURE InsertByte(VAR v: WORD; byteno: ByteRange; value: Byte);
(*  byte insertion as per ExtractByte
*)

PROCEDURE ExtractShort(v: WORD; shortno: ShortRange): Short;
(*  get short 'shortno', in order of address:
        on VAX 0=ls short, 1=ms short
        on 68K 1=ls short, 0=ms short
*)
PROCEDURE InsertShort(VAR v: WORD; shortno: ShortRange; value: Short);
(*  short insertion as per ExtractShort
*)

END Fields.
