anchor.diff:           end
         endcase
   
  -      // swap Top and Left on Horizon/Vertical mode edge
  -      Vector#(64, Bit#(8)) tmpLeft = x.left;
  -      Vector#(65, Bit#(8)) tmpTop = x.top;
  -      if (mode == 15) begin
  -         tmpLeft = takeAt(1, x.top);
  -         tmpTop  = cons(x.top[0], x.left);
  -      end
  -      reg_i  <= (mode == 33) ? (tagged Invalid) : (tagged Valid IntraRef_t {left:tmpLeft, top:tmpTop});
  -
  -      moded1 <= (mode == 33) ? 24 : mode;
  +      return y;
  +   endfunction
  +
  +   rule stage1(isValid(id));
  +      Vector#(34, Bit#(8)) x0 = take(buff);
  +      Vector#(34, Bit#(8)) x1 = (sflag[0] == 1) ? takeAt(1, buff) : x0;
  +      Vector#(32, Bit#(8)) y = ?;
  +      Vector#(32, Bit#(8)) z = ?;
  +
  +      // Mapping to correct order
  +      for(Integer i = 0; i < 32; i = i + 1) begin
  +         let idx = mapTbl[mode][i];
  +         y[i] = x0[idx];
  +         z[i] = x1[idx];
  +      end
  +
  +      buff <= shiftInAtN(buff, ?);
  +      sflag <= (sflag >> 1);
  +      fifo_s1.enq(append(y, z));
  +   endrule
  +
  +
  +
  +   interface io_in  =  interface Put; 
  +                           method Action put(IntraCmd_t x) if (!isValid(id));
  +                              id <= tagged Valid x.id;
  +
  +                              let xL = cons(?, x.refs.left);
  +                              let xT = x.refs.top;
  +                              let xMode = x.mode;
  +
  +                              buff <= getRefPixels(x.mode, xL, xT);
  +                              mode <= xMode;
  +
  +                              Bit#(31) flags = ?;
  +                              for(Integer i = 0; i < 32; i = i + 1) begin
  +                                 flags[i] = mapShift[xMode][i];
  +                              end
  +                              sflag <= flags;
  +
  +                              Bit#(14) sum = 0;
  +                              for(Integer i = 0; i < 32; i = i + 1) begin
  +                                 sum = sum + zeroExtend(xL[1+i]) + zeroExtend(xT[1+i]);
  +                              end
  +                              dcVal <= truncate(sum >> 6);
  +                           endmethod
  +                      endinterface;
  +
  +   interface io_out = toGet(fifo_o);
  +endmodule
  +
  +
  +(* synthesize *)
  +module mkIntra32(IIntra32);
  +   FIFOF#(IntraRef_t)                              fifo_i   <- mkPipelineFIFOF;
  +   FIFOF#(Vector#(32, Bit#(8)))                    fifo_o   <- mkPipelineFIFOF;
  +   Reg#(Bit#(6))                                   mode     <- mkReg(0);            // 0-Mode2, 1-Mode3..., 15-Mode17, 16-Mode34, 17-Mode33..., 32-Mode18
  +
  +   IIntra#(32) dut <- mkIntra;
  +
  +`ifdef XXX
  +   Reg#(Maybe#(Vector#(64, Bit#(8))))              stage0   <- mkReg(tagged Invalid);
  +   FIFOF#(Tuple2#(Bit#(6), Vector#(64, Bit#(8))))  fifo_s1  <- mkPipelineFIFOF;
  +   FIFOF#(Tuple3#(Bit#(4), Vector#(32, Bit#( 8)), Vector#(32, Bit#( 8)))) stage1 <- mkPipelineFIFOF;
  +   FIFOF#(Vector#(2, (Vector#(32, Bit#(13)))))     stage2   <- mkPipelineFIFOF;
  +   Reg#(Vector#(32, Bit#(8)))                      pixd1    <- mkRegU;
  +   Reg#(Bit#(6))                                   cnt1     <- mkReg(0);
  +`endif
  +
  +   Reg#(Bit#(8))                                   dcVal    <- mkRegU;
  +
  +   Integer chen_debug = 0;
  +
  +`ifdef XXX
  +   rule do_stage0;
  +      let x = fifo_i.first;
  +      fifo_i.deq;
  +      let xL = cons(?, x.left);
  +      let xT = x.top;
  +      Vector#(64, Bit#(8)) y = ?;
  +
  +
         mode   <= mode + 1;
  -      stage0 <= tagged Valid y;
  -      cnt1   <= (mode < 16) ? ~0 : 0;
  +
  +      fifo_s1.enq(tuple2(mode, y));
   
         if (chen_debug == 1) begin
            if (mode == 2)
  
positive.code:       end
        fifo_o.enq(y);
     endrule
  
  
     interface io_in  =  interface Put; 
                             method Action put(IntraRef_t x) if (!isValid(reg_i));
                                reg_i <= tagged Valid x;
                                Bit#(14) sum = 0;
                                for(Integer i = 0; i < 32; i = i + 1) begin
                                   sum = sum + zeroExtend(x.left[i]) + zeroExtend(x.top[1+i]);
  
negative.code:    endrule
  
  
     rule do_stage2(stage1.notEmpty);
        let x = stage1.first;
        stage1.deq;
  
        let modeX = tpl_1(x);
        let y0 = tpl_2(x);
        let y1 = tpl_3(x);
  
        Vector#(32, Bit#(13)) xm;
        Vector#(32, Bit#(13)) xn;
  