// File: /workspace/github_cpp_makefile_star2_O1/ke6jjj[P]r-dat/AudioFrameReceiver.cc
// Function: AudioFrameReceiver::HandleDateTime(unsigned char const*, unsigned int)
void
AudioFrameReceiver::HandleDateTime(const uint8_t *item, uint32_t absoluteFrame)
{
  uint8_t dow, year, mon, day, hour, min, sec;
  int likelyYear;
  bool timeIsValid = false;
  bool droppedSync = false;
  uint64_t abs_seconds;

  if (item != NULL) {
    //
    // Date and time.
    //
    dow = item[0] & 0xf;
    year = BCDDecode(item[1]);
    mon = BCDDecode(item[2]);
    day = BCDDecode(item[3]);
    hour = BCDDecode(item[4]);
    min = BCDDecode(item[5]);
    sec = BCDDecode(item[6]);

    if (year < 80)
      likelyYear = year + 2000;
    else
      likelyYear = year + 1900;
    
    //
    // Check if anything seems funny (bad BCD decodes)
    //
    if (mon != 100 && day != 100 && hour != 100 && min != 100 && sec != 100) {
      //
      // Determine the absolute date time in seconds since 1900.
      //
      abs_seconds = SecondsSince1900(likelyYear, mon, day, hour, min, sec);
      timeIsValid = true;
    }
  } else {
    dow = year = mon = day = hour = min = sec = 0;
  }

  if (mHaveDateTimeSync) {
    //
    // Syncronization is in effect. Make sure that everything is still ok.
    //
    if (timeIsValid && absoluteFrame == mLastAbsoluteFrameNumber + 1) {
      //
      // Everything seems ok so far. We've got a new timestamp and there
      // hasn't been a discontiuity in the frame stream.
      //
      // Advance our current clock by 30ms.
      //
      mCurrentDateTimeMilliseconds += 30;
      if (mCurrentDateTimeMilliseconds >= 1000) {
        mCurrentDateTimeMilliseconds %= 1000;
        mCurrentDateTimeSeconds += 1;
      }

      //
      // Does the second hand of our clock still match the transmitted
      // clock?
      //
      if (mCurrentDateTimeSeconds != abs_seconds) {
        //
        // No. We're no longer in sync.
        //
        mHaveDateTimeSync = false;
        droppedSync = true;
      }
    } else {
      //
      // We lost date and time sub-code or there was a jump in absolute
      // time.
      //
      mHaveDateTimeSync = false;
      droppedSync = true;
    }
  }

  //
  // Regardless of the current sync state, keep the closest-to-the-second
  // engine running, possibly establishing sync when it detects a long
  // (34-frame) second.
  //
  if (mHaveLastDateTime && timeIsValid) {
    //
    // We have a previous date and time stamp. Has the time changed
    // since then?
    //
    if (mLastDateTimeSeconds != abs_seconds) {
      //
      // The time has changed. Is it a discontinuity or is it a simple
      // advancement?
      //
      if (abs_seconds == mLastDateTimeSeconds + 1) {
        //
        // Simple advancement. This is a chance to achive a frame
        // lock and/or confirm that the lock is still in place.
        // In order to do so, however, we have to observe two successive
        // increments. Do we have a previous increment observed?
        //           
        if (mHaveLastChangeFrame) {
          //
          // DAT provides 33 1/3 frames per second. Since timestamps
          // can only be placed at the begining of frames, timestamps
          // should advance in the following pattern:
          //
          // 1. 34 frames (No millisecond offset)
          // 2. 33 frames (20 millisecond offset)
          // 3. 33 frames (10 millisecond offset)
          //
          // The timestamp that started with the 34-frame sequence is the
          // one that is closest to the real start of the second.
          //
          uint32_t frames_per_second = absoluteFrame - mLastChangeFrame;
          if (frames_per_second == 34) {
            //
            // We've just received received the frame that ends pattern
            // 1 and starts pattern 2. This means this frame marks the
            // start of pattern 2.
            //
            // If we aren't time synchronized then we can be now.
            //
            if (!mHaveDateTimeSync) {
              //
              // We don't currently have a frame lock. Let's mark
              // that we do and that the last second change occured as
              // pattern 1.
              //
              mHaveDateTimeSync = true;
              mCurrentDateTimeMilliseconds = 20;
              mCurrentDateTimeSeconds = abs_seconds;
            }
          }
        }

        //
        // Mark that this is the new last change frame.
        //
        mHaveLastChangeFrame = true;
        mLastChangeFrame = absoluteFrame;
      } else {
        //
        // There's been a bad discontinuity. Mark that we no longer
        // have a good roll-over frame.
        //
        mHaveLastChangeFrame = false;
      }
    }
  }
    
  //
  // Record that we now have a date and time.
  //
  mHaveLastDateTime = timeIsValid;
  mLastDateTimeSeconds = abs_seconds;

  if (droppedSync && mHaveDateTimeSync)
    printf("Date     time: ------- SYNC DROPPED AND REESTABLISHED --------\n");
  else if (droppedSync)
    printf("Date     time: ------- SYNC DROPPED --------------------------\n");

  if (mHaveDateTimeSync) {
    printf("Date     time: %02d %04d-%02d-%02d %02d:%02d:%02d.%03d (SYNCED)\n",
      dow,
      likelyYear,
      mon,
      day,
      hour,
      min,
      sec,
      mCurrentDateTimeMilliseconds
    );
  } else if (timeIsValid) {
    printf("Date     time: %02d %04d-%02d-%02d %02d:%02d:%02d\n",
      dow,
      likelyYear,
      mon,
      day,
      hour,
      min,
      sec
    );
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
testq %rsi, %rsi
je 0x44a5
movq %rsi, %r13
movl %edx, 0xc(%rsp)
movb (%rsi), %al
andb $0xf, %al
movl %eax, 0x14(%rsp)
movzbl 0x1(%rsi), %edi
callq 0x7910
movb %al, 0xb(%rsp)
movzbl 0x2(%r13), %edi
callq 0x7910
movl %eax, %r14d
movzbl 0x3(%r13), %edi
callq 0x7910
movl %eax, %ebp
movzbl 0x4(%r13), %edi
callq 0x7910
movl %eax, %r12d
movzbl 0x5(%r13), %edi
callq 0x7910
movl %eax, %r15d
movzbl 0x6(%r13), %edi
callq 0x7910
movb 0xb(%rsp), %dil
movl %r14d, %r11d
movzbl %dil, %edx
leal 0x76c(%rdx), %ecx
leal 0x7d0(%rdx), %r14d
cmpb $0x50, %dl
cmovael %ecx, %r14d
cmpb $0x64, %r11b
sete %dl
movl %ebp, 0x10(%rsp)
cmpb $0x64, %bpl
movl %eax, %ebp
sete %cl
orb %dl, %cl
cmpb $0x64, %r12b
sete %al
cmpb $0x64, %r15b
sete %dl
orb %al, %dl
orb %cl, %dl
cmpb $0x64, %bpl
sete %al
orb %dl, %al
jne 0x44c4
movzbl %r15b, %ecx
movzbl %r12b, %edx
movzbl 0x10(%rsp), %esi
movzbl %r11b, %eax
movl %r14d, %r8d
testb $0x3, %dil
jne 0x442f
imull $0xc28f5c29, %r14d, %r9d # imm = 0xC28F5C29
rorl $0x2, %r9d
leaq 0x5c64(%rip), %rdi      # 0xa070
cmpl $0x28f5c28, %r9d        # imm = 0x28F5C28
ja 0x4436
imulq $0xa3d70b, %r8, %r9     # imm = 0xA3D70B
shrq $0x20, %r9
imull $0x190, %r9d, %r9d      # imm = 0x190
movl %r14d, %r10d
subl %r9d, %r10d
je 0x4436
leaq 0x5c0a(%rip), %rdi      # 0xa040
imulq $0x1e13380, %r8, %r8    # imm = 0x1E13380
movzbl %bpl, %r9d
imulq $0x3c, %rcx, %r10
imulq $0xe10, %rdx, %rdx      # imm = 0xE10
imulq $0x15180, %rsi, %rcx    # imm = 0x15180
addq %rdx, %rcx
addq %r10, %rcx
addq %r8, %rcx
addq %r9, %rcx
movslq -0x4(%rdi,%rax,4), %rax
imulq $0x15180, %rax, %rax    # imm = 0x15180
leal -0x1(%r14), %edx
imulq $0x51eb851f, %rdx, %rsi # imm = 0x51EB851F
shrl $0x2, %edx
movq %rsi, %rdi
shrq $0x25, %rdi
subl %edi, %edx
shrq $0x27, %rsi
addl %edx, %esi
imull $0x15180, %esi, %edx    # imm = 0x15180
addl $0xfda18e00, %edx       # imm = 0xFDA18E00
addq %rcx, %rdx
addq %rdx, %rax
addq $-0x15180, %rax         # imm = 0xFFFEAE80
movb $0x1, %dil
jmp 0x44c6
xorl %r11d, %r11d
movl $0x0, 0x10(%rsp)
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %ebp, %ebp
xorl %edi, %edi
movl $0x0, 0x14(%rsp)
jmp 0x44ca
xorl %edi, %edi
movl 0xc(%rsp), %edx
cmpb $0x1, 0x2dd0(%rbx)
jne 0x4526
testb %dil, %dil
je 0x452a
movl 0x2db4(%rbx), %ecx
incl %ecx
cmpl %edx, %ecx
jne 0x452a
movl 0x2dd4(%rbx), %esi
leal 0x1e(%rsi), %ecx
movl %ecx, 0x2dd4(%rbx)
cmpl $0x3ca, %esi            # imm = 0x3CA
jl 0x451d
movl %ecx, %esi
imulq $0x10624dd3, %rsi, %rsi # imm = 0x10624DD3
shrq $0x26, %rsi
imull $0x3e8, %esi, %esi      # imm = 0x3E8
subl %esi, %ecx
movl %ecx, 0x2dd4(%rbx)
incq 0x2dd8(%rbx)
cmpq %rax, 0x2dd8(%rbx)
jne 0x452a
xorl %ecx, %ecx
jmp 0x4533
movb $0x0, 0x2dd0(%rbx)
movb $0x1, %cl
movb 0x2dbc(%rbx), %sil
andb %dil, %sil
cmpb $0x1, %sil
jne 0x45a4
movq 0x2dc8(%rbx), %rsi
cmpq %rax, %rsi
je 0x45a4
incq %rsi
cmpq %rsi, %rax
jne 0x459d
cmpb $0x1, 0x2dbd(%rbx)
jne 0x458e
movl %edx, %esi
subl 0x2dc0(%rbx), %esi
cmpl $0x22, %esi
jne 0x458e
cmpb $0x0, 0x2dd0(%rbx)
jne 0x458e
movb $0x1, 0x2dd0(%rbx)
movl $0x14, 0x2dd4(%rbx)
movq %rax, 0x2dd8(%rbx)
movb $0x1, 0x2dbd(%rbx)
movl %edx, 0x2dc0(%rbx)
jmp 0x45a4
movb $0x0, 0x2dbd(%rbx)
movb %dil, 0x2dbc(%rbx)
movq %rax, 0x2dc8(%rbx)
testb %cl, %cl
je 0x45ee
leaq 0x5a1f(%rip), %rax      # 0x9fdc
movl %edi, 0xc(%rsp)
leaq 0x59d5(%rip), %rdi      # 0x9f9d
cmpb $0x0, 0x2dd0(%rbx)
cmovneq %rax, %rdi
movl %ebp, %r13d
movl %r15d, %ebp
movl %r11d, %r15d
callq 0x21b0
movl 0xc(%rsp), %edi
movl %r15d, %r11d
movl %ebp, %r15d
movl %r13d, %ebp
cmpb $0x1, 0x2dd0(%rbx)
jne 0x4638
movzbl 0x14(%rsp), %esi
movzbl %r11b, %ecx
movzbl 0x10(%rsp), %r8d
movzbl %r12b, %r9d
movzbl %r15b, %r10d
movzbl %bpl, %r11d
movl 0x2dd4(%rbx), %ebx
subq $0x8, %rsp
leaq 0x5887(%rip), %rdi      # 0x9eaa
movl %r14d, %edx
xorl %eax, %eax
pushq %rbx
pushq %r11
pushq %r10
callq 0x2040
addq $0x38, %rsp
jmp 0x4677
testb %dil, %dil
je 0x4673
movzbl 0x14(%rsp), %esi
movzbl %r11b, %ecx
movzbl 0x10(%rsp), %r8d
movzbl %r12b, %r9d
movzbl %r15b, %r10d
movzbl %bpl, %r11d
leaq 0x588c(%rip), %rdi      # 0x9eeb
movl %r14d, %edx
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x2040
addq $0x28, %rsp
jmp 0x4677
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq