anchor.diff:     return rv;
   endmodule
   
  +typedef struct {
  +    Bit#(BurstLenSize) len;
  +    Bool               last;
  +} NextReq deriving (Bits, Eq);
  +
  +function NextReq getNext(Bit#(32) len, Bit#(BurstLenSize) burst);
  +   NextReq v;
  +   v.last = (len <= extend(burst));
  +   v.len = v.last ? truncate(len) : burst;
  +   return v;
  +endfunction
  +
   module mkMemReadEngineBuff#(Integer bufferSizeBytes) (MemReadEngine#(busWidth, userWidth, cmdQDepth, numServers))
      provisos (Div#(busWidth,8,busWidthBytes),
   	     Mul#(busWidthBytes,8,busWidth),
  
positive.code:    endrule
           
     for (Integer idx = 0; idx < valueOf(numServers); idx = idx + 1)
        rule store_cmd if (!clientInFlight[idx]);
  	 let cmd <- toGet(clientRequest[idx]).get();
  	 clientInFlight[idx] <= True;
  	 clientCommand[idx] <= cmd;
           clientLen[idx] <= cmd.len;
           clientBase[idx] <= cmd.base;
  	 if (verbose) $display("mkMemReadEngineBuff::%d store_cmd %d %d", counter, idx, clientAvail[idx].read);
        endrule
  
     Reg#(Bit#(TAdd#(1,serverIdxSz))) loadIdx <- mkReg(0);
     rule load_ctxt_a;
        if (clientInFlight[loadIdx]) begin
           Bit#(BurstLenSize) cmd_len = clientCommand[loadIdx].burstLen;
           let last_burst = clientLen[loadIdx] <= extend(clientCommand[loadIdx].burstLen);
  	 if (last_burst)
               cmd_len = truncate(clientLen[loadIdx]);
  	 let cond0 <- clientAvail[loadIdx].maybeDecrement(unpack(extend(cmd_len>>beatShift)));
  	 serverCheckAvail.enq(tuple4(truncate(loadIdx),cond0,last_burst,cmd_len));
  	 if (verbose) $display("mkMemReadEngineBuff::%d load_ctxt_b clientAvail[%d] %d burstLen %d cond0 %d last_burst %d", counter, loadIdx, clientAvail[loadIdx].read(), cmd_len>>beatShift, cond0, last_burst);
        end
        if(loadIdx+1 >= fromInteger(valueOf(numServers)))
           loadIdx <= 0;
  
negative.code:    endrule
  
     // should use an EHR for clientInFlight to avoid the need for this pragma
     (* descending_urgency = "rule_requestServer, rule_startNew" *)
     rule rule_requestServer;
        match {.idx, .cond0,.last_burst,.cmd_len} <- toGet(serverCheckAvail).get;
        if  (cond0) begin
  	 if (verbose) $display("mkMemReadEngineBuff::%d rule_requestServer clientLen %d idx %d cond0 %d last_burst %d", counter, clientLen[idx], idx, cond0, last_burst);
  	 serverRequest.enq(tuple6(idx,clientCommand[idx].sglId,clientBase[idx],clientCommand[idx].tag,last_burst,cmd_len));
           clientBase[idx] <= clientBase[idx] + extend(cmd_len);
           clientLen[idx] <= clientLen[idx] - extend(cmd_len);
           clientNext[idx] <= getNext(clientLen[idx], clientCommand[idx].burstLen);
  	 if (last_burst) begin
  	    if (verbose) $display("mkMemReadEngineBuff::%d rule_requestServer last_burst %d", counter, last_burst);
  	    clientInFlight[idx] <= False;
  	 end
        end
  