anchor.diff:  	    );
      
      FIFOF#(Int#(32)) locf <- mkFIFOF;
  -   FIFO#(Bool) conff <- mkSizedFIFO(1);
      
      let verbose = True;
  -   let debug = False;
  +   let debug = True;
   
      Clock clk <- exposeCurrentClock;
      Reset rst <- exposeCurrentReset;
      BRAM2Port#(NeedleIdx, Char) needleBram  <- mkBRAM2Server(defaultValue);
      BRAM2Port#(NeedleIdx, Bit#(32)) mpNextBram <- mkBRAM2Server(defaultValue);
  -   FIFOF#(MemData#(haystackBusWidth)) haystackFifo <- mkFIFOF();
  +   FIFOF#(MemDataF#(haystackBusWidth)) haystackFifo <- mkFIFOF();
      Gearbox#(haystackBusBytes,1,Char) haystackGb <- mkNto1Gearbox(clk,rst,clk,rst);
   
      Reg#(Bit#(32)) cycleCnt <- mkReg(0);
      Reg#(Bit#(32)) lastHD <- mkReg(0);
      
  -   Reg#(Stage)    stage <- mkReg(Config_needle);
  +   Reg#(Stage)    stage <- mkReg(Initialized);
      Reg#(Bit#(32)) needleLenReg <- mkReg(0);
  -   Reg#(Bit#(32)) haystackLenReg <- mkReg(0);
  -   Reg#(Bit#(32)) haystackBase <- mkReg(0);
  -   Reg#(Bit#(32)) jReg <- mkReg(0); // offset in haystack
  -   Reg#(Bit#(32)) iReg <- mkReg(0); // offset in needle
  -   Reg#(Bit#(2))  epochReg <- mkReg(0);
  +   Reg#(Bit#(32)) jReg <- mkReg(1); // offset in haystack
  +   Reg#(Bit#(32)) iReg <- mkReg(1); // offset in needle
   
      BRAMPipeIn#(NeedleIdxWidth,configBusWidth) n2b <- mkBRAMPipeIn(0, needleBram.portB);
      BRAMPipeIn#(NeedleIdxWidth,configBusWidth) mp2b <- mkBRAMPipeIn(1, mpNextBram.portB);
  
positive.code: endinterface
  
  interface MPStreamEngine#(numeric type haystackBusWidth, numeric type configBusWidth);
     interface PipeIn#(MemData#(configBusWidth)) needle;
     interface PipeIn#(MemData#(configBusWidth)) mpNext;
     interface PipeIn#(MemData#(haystackBusWidth)) haystack;
     interface PipeOut#(Int#(32)) locdone;
  endinterface
  
  typedef Bit#(8) Char;
  
negative.code:    FIFO#(void) doneFifo <- mkFIFO;
     
     rule countCycles;
        //if (debug) $display("******************************************** %d", cycleCnt);
        cycleCnt <= cycleCnt+1;
     endrule
     
     rule haystackResp;
        let rv <- toGet(haystackFifo).get;
        if (debug) $display("mkMPEngine::haystackResp rv=%h", rv.data);
        haystackGb.enq(unpack(rv.data));
     endrule
     
     let x_i <- mkReg(0);
     let t_j <- mkReg(0);
     let m = needleLenReg;
     let i = iReg;
     let matchFsm <- mkAutoFSM(seq
        while (stage != Search) seq
  	 iReg <= 1;
        endseq
        action
  	 iReg <= 1;
  	 jReg <= 1;
  	 let t = haystackGb.first[0]; haystackGb.deq();
  	 t_j <= t;
  	 needleBram.portA.request.put(BRAMRequest{write:False, address: truncate(iReg-1), datain:?, responseOnWrite:?});
        endaction
        action
  	 let xNext <- needleBram.portA.response.get();
  	 x_i <= xNext;
        endaction
        while (stage == Search) seq
  	 $display("initial i=%d j=%d x_i=%h t_j=%h", iReg, jReg, x_i, t_j);
  	 while ((i == m + 1) || (i > 0 && x_i != t_j)) seq
  	    action
  	       mpNextBram.portA.request.put(BRAMRequest{write:False, address: truncate(iReg), datain:?, responseOnWrite:?});
        	    endaction
  	    action
  	       let mpNext <- mpNextBram.portA.response.get();
  	       iReg <= mpNext;
  	       needleBram.portA.request.put(BRAMRequest{write:False, address: truncate(mpNext-1), datain:?, responseOnWrite:?});
  	    endaction
  	    action
  	       let xNext <- needleBram.portA.response.get();
  	       x_i <= xNext;
  	       $display("i=%d m+1=%d x_i=%h t_j=%h", i, m+1, xNext, t_j);
  	    endaction
  	 endseq
  	 action
  	    iReg <= iReg + 1;
  	    jReg <= jReg + 1;
  	    needleBram.portA.request.put(BRAMRequest{write:False, address: truncate(iReg+1-1), datain:?, responseOnWrite:?});
  	    let t = haystackGb.first[0]; haystackGb.deq();
  	    t_j <= t;
  	    if (t == 0)
  	       locf.enq(-1);
  	 endaction
  	 action
  	    let xNext <- needleBram.portA.response.get();
  	    x_i <= xNext;
  	    $display("step    i=%d j=%d x_i=%h t_j=%h", iReg, jReg, xNext, t_j);
  	    if (iReg == m + 1) begin
  	       $display("match at j=%d", jReg - iReg);
  	       locf.enq(unpack(jReg - iReg));
  	    end
  	 endaction
        endseq // stage == Search
        endseq);
    
     interface PipeIn needle = n2b.pipe;
     interface PipeIn mpNext = mp2b.pipe;
     interface PipeIn haystack = toPipeIn(haystackFifo);
     interface PipeOut locdone = toPipeOut(locf);
  
     method Action clear();
        stage <= Initialized;
     endmethod
     method Action start(Bit#(32) needleLen);
        stage <= Search;
        jReg <= 1;
        iReg <= 1;
        needleLenReg <= needleLen;
     endmethod
  
  endmodule
  