// File: /workspace/github_cpp_makefile_star2_O3/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
testq %rsi, %rsi
movq %rdi, 0x10(%rsp)
je 0x450c
movq %rsi, %r12
movl %edx, 0x8(%rsp)
movb (%rsi), %al
andb $0xf, %al
movl %eax, 0x4(%rsp)
movzbl 0x1(%rsi), %edi
callq 0x7978
movl %eax, %r14d
movzbl 0x2(%r12), %edi
callq 0x7978
movl %eax, %r13d
movzbl 0x3(%r12), %edi
callq 0x7978
movl %eax, %ebp
movzbl 0x4(%r12), %edi
callq 0x7978
movl %eax, %r15d
movzbl 0x5(%r12), %edi
callq 0x7978
movl %eax, %ebx
movzbl 0x6(%r12), %edi
callq 0x7978
movl %ebp, %r11d
movzbl %r14b, %edx
leal 0x76c(%rdx), %ecx
leal 0x7d0(%rdx), %r12d
cmpb $0x50, %dl
cmovael %ecx, %r12d
cmpb $0x64, %r13b
sete %dl
cmpb $0x64, %r11b
sete %cl
orb %dl, %cl
cmpb $0x64, %r15b
sete %sil
cmpb $0x64, %bl
sete %dl
orb %sil, %dl
orb %cl, %dl
cmpb $0x64, %al
sete %cl
orb %dl, %cl
jne 0x4523
movzbl %bl, %ecx
movzbl %r15b, %edx
movzbl %r11b, %esi
movzbl %r13b, %ebp
testb $0x3, %r14b
jne 0x4403
imull $0x5c29, %r12d, %edi    # imm = 0x5C29
rorw $0x2, %di
movzwl %di, %r8d
leaq 0x5ca4(%rip), %rdi      # 0xa080
cmpl $0x28f, %r8d            # imm = 0x28F
ja 0x440a
imull $0x51f, %r12d, %r8d     # imm = 0x51F
shrl $0x13, %r8d
imull $0x190, %r8d, %r8d      # imm = 0x190
movl %r12d, %r9d
subl %r8d, %r9d
testw %r9w, %r9w
je 0x440a
leaq 0x5c46(%rip), %rdi      # 0xa050
movl %r13d, (%rsp)
movl %r12d, %r8d
imulq $0x1e13380, %r8, %r8    # imm = 0x1E13380
movzbl %al, %r9d
imulq $0x3c, %rcx, %r10
imulq $0xe10, %rdx, %rdx      # imm = 0xE10
imulq $0x15180, %rsi, %rcx    # imm = 0x15180
addq %rdx, %rcx
addq %r10, %rcx
addq %r9, %rcx
addq %r8, %rcx
movslq -0x4(%rdi,%rbp,4), %rdx
imulq $0x15180, %rdx, %r8     # imm = 0x15180
leal -0x1(%r12), %edx
movzwl %dx, %esi
shrl $0x2, %edx
movl %esi, %edi
shrl $0x2, %edi
imull $0x147b, %edi, %edi     # imm = 0x147B
shrl $0x11, %edi
subl %edi, %edx
shrl $0x4, %esi
imull $0xa3e, %esi, %esi      # imm = 0xA3E
shrl $0x10, %esi
addl %edx, %esi
imull $0x15180, %esi, %edx    # imm = 0x15180
addl $0xfda18e00, %edx       # imm = 0xFDA18E00
addq %rcx, %rdx
leaq (%r8,%rdx), %r9
addq $-0x15180, %r9          # imm = 0xFFFEAE80
movq 0x10(%rsp), %rdi
leaq 0x2dd0(%rdi), %r14
movb 0x2dd0(%rdi), %cl
cmpb $0x1, %cl
jne 0x455a
movl 0x2db4(%rdi), %esi
incl %esi
movb $0x1, %r13b
movl 0x8(%rsp), %edx
cmpl %edx, %esi
jne 0x4543
movl 0x2dd4(%rdi), %esi
leal 0x1e(%rsi), %r8d
movl %r8d, 0x2dd4(%rdi)
cmpl $0x3c9, %esi            # imm = 0x3C9
jle 0x46b4
movl %r8d, %esi
imulq $0x10624dd3, %rsi, %rsi # imm = 0x10624DD3
shrq $0x26, %rsi
imull $0x3e8, %esi, %esi      # imm = 0x3E8
subl %esi, %r8d
movl %r8d, 0x2dd4(%rdi)
movq 0x2dd8(%rdi), %rsi
incq %rsi
movq %rsi, 0x2dd8(%rdi)
jmp 0x46bb
xorl %r13d, %r13d
xorl %r11d, %r11d
xorl %r15d, %r15d
xorl %ebx, %ebx
xorl %eax, %eax
movl $0x0, 0x4(%rsp)
jmp 0x452c
movq 0x10(%rsp), %rdi
movl 0x8(%rsp), %edx
cmpb $0x0, 0x2dd0(%rdi)
je 0x454e
movl %r13d, (%rsp)
leaq 0x2dd0(%rdi), %r14
xorl %r13d, %r13d
movb $0x0, (%r14)
movb $0x1, %r8b
xorl %ecx, %ecx
jmp 0x4564
movb $0x0, 0x2dbc(%rdi)
jmp 0x4647
movb $0x1, %r13b
xorl %r8d, %r8d
movl 0x8(%rsp), %edx
testb %r13b, %r13b
je 0x4582
cmpb $0x0, 0x2dbc(%rdi)
je 0x4582
movq 0x2dc8(%rdi), %rsi
cmpq %r9, %rsi
jne 0x4656
movb %r13b, 0x2dbc(%rdi)
movq %r9, 0x2dc8(%rdi)
testb %r8b, %r8b
je 0x45c9
leaq 0x5a50(%rip), %rdx      # 0x9fec
leaq 0x5a0a(%rip), %rdi      # 0x9fad
testb %cl, %cl
cmovneq %rdx, %rdi
movq %r12, 0x8(%rsp)
movl %eax, %r12d
movl %r11d, %ebp
callq 0x21b0
movq 0x10(%rsp), %rdi
movl %ebp, %r11d
movl %r12d, %eax
movq 0x8(%rsp), %r12
cmpb $0x0, (%r14)
je 0x460e
movzbl 0x4(%rsp), %esi
movzbl (%rsp), %ecx
movzbl %r11b, %r8d
movzbl %r15b, %r9d
movzbl %bl, %r10d
movzbl %al, %r11d
movl 0x2dd4(%rdi), %ebx
subq $0x8, %rsp
leaq 0x58c1(%rip), %rdi      # 0x9eba
movl %r12d, %edx
xorl %eax, %eax
pushq %rbx
pushq %r11
pushq %r10
callq 0x2040
addq $0x38, %rsp
jmp 0x464b
testb %r13b, %r13b
je 0x4647
movzbl 0x4(%rsp), %esi
movzbl (%rsp), %ecx
movzbl %r11b, %r8d
movzbl %r15b, %r9d
movzbl %bl, %r10d
movzbl %al, %r11d
leaq 0x58c8(%rip), %rdi      # 0x9efb
movl %r12d, %edx
xorl %eax, %eax
pushq %r11
pushq %r10
callq 0x2040
addq $0x28, %rsp
jmp 0x464b
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
incq %rsi
cmpq %rsi, %r9
jne 0x46a6
cmpb $0x1, 0x2dbd(%rdi)
jne 0x4692
movl %edx, %esi
subl 0x2dc0(%rdi), %esi
cmpl $0x22, %esi
jne 0x4692
testb %cl, %cl
jne 0x4692
movb $0x1, 0x2dd0(%rdi)
movl $0x14, 0x2dd4(%rdi)
movq %r9, 0x2dd8(%rdi)
movb $0x1, %cl
movl %edx, 0x2dc0(%rdi)
movw $0x101, 0x2dbc(%rdi)    # imm = 0x101
jmp 0x4589
movw $0x1, 0x2dbc(%rdi)
jmp 0x4589
movq 0x2dd8(%rdi), %rsi
movb $0x1, %r13b
cmpq %r9, %rsi
jne 0x4543
xorl %r8d, %r8d
jmp 0x4564
nop