! Temporary 10-MHz Ethernet Utilities

%include "ether:lance.inc"

%routine point (%bytename high,%halfname low,%name object)
! Point the composite pointer HIGH.LOW at OBJECT
%integer address = addr(object)
  low <- address
  high <- address>>16
%end

%record(drf)%map new ring
! Acquire a new LANCE buffer descriptor ring off the heap
%record(drf)%name ring
  ring == record((heapget(sizeof(ring)+8)+7)&\7)
  ring = 0
  %result == ring
%end

! Test buffer format
%recordformat bf(%bytearray raw(1:556) %orc
                 %bytearray ddte,sdte(1:6),%half dec pty, iso length,
                 %bytearray data(1:900))

%record(bf)%map new buffer
! Grab a new test buffer off the heap
%record(bf)%name b
  b == new(b)
  b = 0
  %result == b
%end

%record(bf)%map buffer of(%record(df)%name desc)
! Access the buffer pointed at by the composite pointer in DESC
  %result == record(desc_highad<<16+desc_lowad)
%end

! DTE manipulation routines

%routine copy dte(%bytename from,to)
! Copy six bytes from FROM to TO
%integer i
  %for i = 1,1,6 %cycle
    to = from; from==from[1]; to == to[1]
  %repeat
%end

%routine set dte(%bytename b,%string(17)s)
! Convert the string representation in S of a DTE into six bytes at B
%bytename c==length(s)
%integer i

  %routine hex
  %integer d
    d = c; c == c[1]
    d = d&95 %if 'a'<=d<='f'
    %signal 4 %if d<'0' %or d>'F'
    %signal 4 %if '9'<d<'A'
    d = d-7 %if d>'9'
    b = b<<4+d-'0'
  %end

  %on 4 %start
    %signal 4,2,,"Invalid DTE string ".s
  %finish

  %signal 4 %unless c=17
  c == c[1]; b = 0; hex; hex; %signal 4 %unless c='-'; b == b[1]
  c == c[1]; b = 0; hex; hex; %signal 4 %unless c='-'; b == b[1]
  c == c[1]; b = 0; hex; hex; %signal 4 %unless c='-'; b == b[1]
  c == c[1]; b = 0; hex; hex; %signal 4 %unless c='-'; b == b[1]
  c == c[1]; b = 0; hex; hex; %signal 4 %unless c='-'; b == b[1]
  c == c[1]; b = 0; hex; hex
%end

%string(17)%fn get dte(%bytename b)
! Convert the six byte DTE at B into its string representation
%string(17)s=""

  %integerfn h(%integer n)
    n = n&15; n = n+7 %if n>9; %result = n+'0'
  %end

  %routine hh
    s = s.tostring(h(b>>4))
    s = s.tostring(h(b))
    b == b[1]
  %end
  
  %routine m
    s = s."-"
  %end

  hh; m; hh; m; hh; m; hh; m; hh; m; hh
  %result = s
%end

%routine convert dte to halfwords(%bytename b,%halfname h)
! Copy six bytes from B to H, swapping bytes en route
  h    = b[1]<<8+b; b == b[2]
  h[1] = b[1]<<8+b; b == b[2]
  h[2] = b[1]<<8+b; b == b[2]
%end
