! Ethernet Interface Module

constinteger   Undefined Error   =  0,
               Success           =  1,
               Bad Function Code =  2,
               Bad Port Number   =  3,
               Port not owned    =  4,
               No Free Port      =  5,
               Port already owned=  6,
               Packet was NAKed  =  7,
               Port was closed   =  8,
               Port was open     =  9,
               Bad Transmit Length=10,
               abort             = 11,
               bus error         = 12

conststring (31)array   errors(0:12) = 
   "undefined error",
   "success",
   "bad function code",
   "bad port number",
   "port not owned",
   "no free port",
   "port already owned",
   "packet was NAKed",
   "port was closed",
   "port was open",
   "bad transmit length",
   "receive abort",
   "bus error"

Include  "moose:mouse.inc"

systemroutinespec  phex(integer  what)

Recordformat  reqfm (Record (messagefm) msg,
                     Integer  tag,
                     (Byte  code or  byte  result),
                     Byte  port,
                     (Byte  rdte or  Byte  port0mode),
                     (Byte  rsap or  Byte  channel),
                     bytename  buffer,
                     (Integer  length OrInteger  maxbytes orinteger  resbytes))

Ownrecord (mailboxfm)Name   ombox==NIL
!Ownrecord(reqfm)%Name      rep

Externalroutine  Ether Start
   Integer  t

   t = Find Entry ("ETHER_REQ",poa_logdict)
   Signal  3,0,0,"Failed to find ETHER_REQ" if  t = 0
   ombox == record(integer(t))
End 

Routine  Transact (Record (reqfm)name  req)
   integer  i
   record (semaphorefm)     sem
   record (mailboxfm)       imbox
   Record (reqfm)name       rep

   on  0 start 
      printstring("IO_Eth: unexpected event ")
      write(event_event, 0);  space;  write(event_sub, 0)
      space;  phex(event_extra);  space
      printstring(event_message);  newline
      signal  15, 12, req_code, "Unexpected bus error"
   finish 
   ether start if  ombox == NIL
   req_tag = 0
   setup semaphore (sem)
   setup mailbox (imbox,sem)
   send message (req,ombox,imbox)
   rep == receive message (imbox)
   i = rep_result
   Signal  15,i,0,errors(i) If  i # 1
End 

externalroutine  EtherTransmitBlock(integer  Port,Bytes,bytename  Buffer)
   record (reqfm) req=0
   req_code    = 5
   req_port    = port
   req_length  = bytes
   req_buffer  == buffer
   transact (req)
end 

externalroutine  EtherReceiveBlock(integer  Port,MaxBytes,integername  Bytes,ByteName  Buffer)
   Record (reqfm) req=0
   req_code    = 6
   req_port    = port
   req_maxbytes= maxbytes
   req_buffer  == buffer
   transact (req)
   bytes = req_resbytes
end 

externalroutine  EtherOpenPort(integer  Port,RemoteStation,RemotePort)
   Record (reqfm) req=0
   req_code = 3
   req_port = port
   req_rdte = Remote Station
   req_rsap = Remote Port
   transact (req)
end 

externalroutine  EtherClosePort(integer  Port)
   Record (reqfm) req=0
   req_code = 4
   req_port = port
   transact (req)
end 

externalroutine  EtherClaimPort(integer  port)
   Record (reqfm) req=0
   req_code = 1
   req_port = port
   transact (req)
end 

externalintegerfn  EtherAllocatePort
   Record (reqfm) req=0
   req_code =1
   req_port = 255
   transact(req)
   Result  = req_port
end 

externalroutine  Ether Allocate Zero
   Ether Claim Port (0)
End 

externalroutine  EtherFreePort(integer  Port)
   Record (reqfm) req=0
   req_code = 2
   req_port = port
   transact (req)
end 

externalintegerfn  EtherStationAddress
   Record (reqfm) req=0
   req_code = 7
   transact (Req)
   Result  = req_port
end