// File: ke6jjj[P]r-dat/NRZISyncDeframer.cc
// Function: NRZISyncDeframer::ReceiveBit(bool)
void
NRZISyncDeframer::ReceiveBit(bool bit_in)
{
  bool bit;
  size_t bit_count;
  
  //
  // NRZI decode the bit.
  //
  bit = bit_in != mLastBit;
  mLastBit = bit_in;

  //
  // Accumulate a new bit into the large shift register.
  //
  mFrame  &= 0x1ff;
  mFrame <<= 1;
  mFrame  |= bit;

  //
  // If the caller is trying to detect a track start, do pre-amble
  // detection.
  //  
  if (!mTrackDetected) {
    //
    // Check for pre-amble words every ten symbols.
    //
    if (++mPreambleCheck == 10) {
      //
      // If this is a pre-amble sequence (all ones) then increment the preamble
      // count. Otherwise, reset it. (This is used in track start/stop
      // detection)
      //
      mPreambleCheck = 0;
      if (mFrame == 0x3ff)
        mPreambleSymbolCount++;
      else
        mPreambleSymbolCount = 0;
    }
  }
  
  //
  // Are we currently searching for frame sync and have we received
  // a sync word?
  //
  if ((mFrame & 0x1ff) == 0x111) {
    //
    // We've found a sync pattern. Enter sync state.
    //
    mSyncBitCount = 0;
    mState = STATE_SYNCED;

    //
    // Notify the upstream frame receiver of the sync word.
    //
    mReceiver->ReceiveWord(mFrame);
  } else if (mState == STATE_SYNCED) {
    //
    // We're in a synchronized state. We should keep accepting bits into
    // the accumulator until a full frame is built up. Has a frame been
    // built up?
    //
    mSyncBitCount += 1;
    if (mSyncBitCount == 10) {
      //
      // There's a full frame built up.
      //
      mSyncBitCount = 0;
      
      //
      // Deliver it upstream.
      //
      mReceiver->ReceiveWord(mFrame);
    }
  }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movb %sil, %al
movq %rdi, -0x8(%rbp)
andb $0x1, %al
movb %al, -0x9(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x20(%rbp)
movb -0x9(%rbp), %cl
andb $0x1, %cl
movzbl %cl, %ecx
movb 0x10(%rax), %dl
andb $0x1, %dl
movzbl %dl, %edx
cmpl %edx, %ecx
setne %cl
andb $0x1, %cl
movb %cl, -0xa(%rbp)
movb -0x9(%rbp), %cl
andb $0x1, %cl
movb %cl, 0x10(%rax)
movl 0xc(%rax), %ecx
andl $0x1ff, %ecx            # imm = 0x1FF
movl %ecx, 0xc(%rax)
movl 0xc(%rax), %ecx
shll %ecx
movl %ecx, 0xc(%rax)
movb -0xa(%rbp), %cl
andb $0x1, %cl
movzbl %cl, %ecx
orl 0xc(%rax), %ecx
movl %ecx, 0xc(%rax)
testb $0x1, 0x8(%rax)
jne 0x33a8
movq -0x20(%rbp), %rcx
movq 0x30(%rcx), %rax
addq $0x1, %rax
movq %rax, 0x30(%rcx)
cmpq $0xa, %rax
jne 0x33a6
movq -0x20(%rbp), %rax
movq $0x0, 0x30(%rax)
cmpl $0x3ff, 0xc(%rax)       # imm = 0x3FF
jne 0x3398
movq -0x20(%rbp), %rax
movq 0x28(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x28(%rax)
jmp 0x33a4
movq -0x20(%rbp), %rax
movq $0x0, 0x28(%rax)
jmp 0x33a6
jmp 0x33a8
movq -0x20(%rbp), %rax
movl 0xc(%rax), %eax
andl $0x1ff, %eax            # imm = 0x1FF
cmpl $0x111, %eax            # imm = 0x111
jne 0x33dc
movq -0x20(%rbp), %rax
movq $0x0, 0x18(%rax)
movl $0x1, 0x20(%rax)
movq 0x38(%rax), %rdi
movl 0xc(%rax), %esi
callq 0x3560
jmp 0x3419
movq -0x20(%rbp), %rax
cmpl $0x1, 0x20(%rax)
jne 0x3417
movq -0x20(%rbp), %rax
movq 0x18(%rax), %rcx
addq $0x1, %rcx
movq %rcx, 0x18(%rax)
cmpq $0xa, 0x18(%rax)
jne 0x3415
movq -0x20(%rbp), %rax
movq $0x0, 0x18(%rax)
movq 0x38(%rax), %rdi
movl 0xc(%rax), %esi
callq 0x3560
jmp 0x3417
jmp 0x3419
addq $0x20, %rsp
popq %rbp
retq
nop