// File: /workspace/github_cpp_makefile_star2_O2/joncampbell123[P]floppytools/kryoflux6.cpp
// Function: main
int main(int argc,char **argv) {
    struct flux_bits fb;
    struct kryoflux_event ev;
    int c;

    if (argc < 2) {
        fprintf(stderr,"%s <raw>\n",argv[0]);
        return 1;
    }

    FILE *fp = fopen(argv[1],"rb");
    if (fp == NULL) return 1;

    if (!autodetect_flux_bits_mfm(fb,ev,fp)) {
        fprintf(stderr,"Autodetect failure\n");
        return 1;
    }

    /* TODO: autodetection is possible (for MFM at least).
     *       look at the histogram code in kryoflux2.cpp */

    /* from my tests:
     *
     * distance of 10101010101010 = 49
     * change in distance to encode 1001001001001001 = 23.
     *
     * shortest is therefore 49 - 23 = 26 which is the distance to encode 111111111111 */

    fb.clear();

    do {
        kryoflux_bits_refill(fb,ev,fp);

        /*                                                      *            */
        /*                                            1 0 1 0 0 0 0 1   (A1) */
        /* look for A1 sync (100010010001). Look for '0100010010001001' */
        /*                                            ................  16 bits */
        /*                                            4-->4-->8-->9-->  */
        /*                                            3210321032103210  */
        while (fb.avail() >= MFM_A1_SYNC_LENGTH) {
            if (fb.peek(MFM_A1_SYNC_LENGTH) == MFM_A1_SYNC) {
                printf("Sync at about %lu\n",ev.offset);

                c = flux_bits_mfm_decode(fb,ev,fp);
                if (c != -MFM_A1_SYNC_BYTE) fprintf(stderr,"Unexpected decode from MFM_A1_SYNC 0x%x %d\n",c,c);

                c = flux_bits_mfm_decode(fb,ev,fp);
                if (c != -MFM_A1_SYNC_BYTE) fprintf(stderr,"Next byte not MFM_A1_SYNC 0x%x %d\n",c,c);

                c = flux_bits_mfm_decode(fb,ev,fp);
                if (c != -MFM_A1_SYNC_BYTE) fprintf(stderr,"Next byte not MFM_A1_SYNC 0x%x %d\n",c,c);

                c = flux_bits_mfm_decode(fb,ev,fp);
                int type_c = c;
                if (c == 0xFA || c == 0xFB || c == 0xFE) {
                    printf("Sync type: 0x%02x\n",c);

                    if (c == 0xFE) {
                        unsigned char tmp[30];
                        unsigned int tmpsz=0;

                        for (unsigned int i=0;i < (6u/*header*/+16u/*bytes of 0x4e*/);i++) {
                            c = flux_bits_mfm_decode(fb,ev,fp);
                            if (c < 0) break;
                            tmp[i] = (unsigned char)c;
                            tmpsz = i;
                        }

                        printf(" Header: ");
                        for (unsigned int i=0;i < tmpsz;i++) printf("0x%02x ",tmp[i]);
                        printf("\n");

                        if (tmpsz >= 6) {
                            printf("  Track=%u side=%u sector=%u size=%u crc=0x%02x%02x\n",
                                tmp[0],
                                tmp[1],
                                tmp[2],
                                128 << tmp[3],
                                tmp[4],tmp[5]);
                        }

                        {
                            unsigned char t[4];
                            mfm_crc16fd_t crc;

                            t[0] = t[1] = t[2] = 0xA1;
                            t[3] = (unsigned char)type_c;
                            crc = mfm_crc16fd_update(0xffff,t,4);
                            crc = mfm_crc16fd_update(crc,tmp,4);

                            mfm_crc16fd_t check;

                            check = (tmp[4] << 8u) + tmp[5];

                            if (check != crc)
                                fprintf(stderr,"Checksum failed (got 0x%04x expect 0x%04x)\n",crc,check);
                        }
                    }
                    else if ((c&0xFE) == 0xFA) {
                        unsigned char tmp[128];
                        unsigned int tmpsz=0;

                        memset(tmp,0,sizeof(tmp));
                        for (unsigned int i=0;i < 128u;i++) {
                            c = flux_bits_mfm_decode(fb,ev,fp);
                            if (c < 0) break;
                            tmp[i] = (unsigned char)c;
                            tmpsz = i;
                        }

                        printf(" Sector contents:\n");

                        for (unsigned int i=0;i < 128;i += 16) {
                            for (unsigned int c=0;c < 16;c++)
                                printf("%02x ",tmp[i+c]);

                            printf("  ");

                            for (unsigned int c=0;c < 16;c++) {
                                if (tmp[i+c] >= 32 && tmp[i+c] < 127)
                                    printf("%c",tmp[i+c]);
                                else
                                    printf(".");
                            }

                            printf("\n");
                        }
                    }
                }
            }
            else {
                fb.get(1);
            }
        }

        if (!kryoflux_bits_refill(fb,ev,fp))
            break;
    } while (fb.avail() > 0);

    fclose(fp);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
leaq -0x60(%rbp), %rax
andq $0x0, 0x10(%rax)
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
pushq $0x1
popq %rbx
cmpl $0x1, %edi
jle 0x1730
movq 0x8(%rsi), %rdi
leaq 0x1cf8(%rip), %rsi      # 0x300e
callq 0x1110
testq %rax, %rax
je 0x1713
movq %rax, %r14
leaq -0x44(%rbp), %rdi
leaq -0x70(%rbp), %rsi
movq %rax, %rdx
callq 0x1a79
testb %al, %al
je 0x174d
leaq -0x44(%rbp), %rdi
callq 0x19f4
leaq -0x44(%rbp), %r15
pushq $0x2e
popq %r12
leaq 0x1d4f(%rip), %rbx      # 0x30a6
movq %r14, -0x30(%rbp)
movq %r15, %rdi
leaq -0x70(%rbp), %rsi
movq %r14, %rdx
callq 0x1908
movq %r15, %rdi
callq 0x1992
cmpl $0x10, %eax
jb 0x16e6
movq %r15, %rdi
pushq $0x10
popq %rsi
callq 0x19fe
cmpl $0x4489, %eax           # imm = 0x4489
jne 0x1471
movq -0x68(%rbp), %rsi
leaq 0x1c89(%rip), %rdi      # 0x3025
xorl %eax, %eax
callq 0x1040
movq %r15, %rdi
leaq -0x70(%rbp), %rsi
movq %r14, %rdx
callq 0x1cc1
cmpl $0xffffff5f, %eax       # imm = 0xFFFFFF5F
jne 0x1683
movq %r15, %rdi
leaq -0x70(%rbp), %rsi
movq %r14, %rdx
callq 0x1cc1
cmpl $0xffffff5f, %eax       # imm = 0xFFFFFF5F
jne 0x16a4
movq %r15, %rdi
leaq -0x70(%rbp), %rsi
movq %r14, %rdx
callq 0x1cc1
cmpl $0xffffff5f, %eax       # imm = 0xFFFFFF5F
jne 0x16c5
movq %r15, %rdi
leaq -0x70(%rbp), %rsi
movq %r14, %rdx
callq 0x1cc1
movl %eax, %r13d
leal -0xfa(%r13), %eax
cmpl $0x4, %eax
ja 0x136a
pushq $0x13
popq %rcx
btl %eax, %ecx
jae 0x136a
leaq 0x1c61(%rip), %rdi      # 0x3087
movl %r13d, %esi
xorl %eax, %eax
callq 0x1040
cmpl $0xfe, %r13d
jne 0x1481
xorl %r13d, %r13d
xorl %r15d, %r15d
cmpq $0x16, %r13
je 0x1502
leaq -0x44(%rbp), %rdi
leaq -0x70(%rbp), %rsi
movq %r14, %rdx
callq 0x1cc1
testl %eax, %eax
js 0x1502
movb %al, -0xf0(%rbp,%r13)
movl %r13d, %r15d
incq %r13
jmp 0x143f
movq %r15, %rdi
pushq $0x1
popq %rsi
callq 0x1a3a
jmp 0x136a
andl $0xfe, %r13d
cmpl $0xfa, %r13d
jne 0x136a
xorps %xmm0, %xmm0
movaps %xmm0, -0x80(%rbp)
movaps %xmm0, -0x90(%rbp)
movaps %xmm0, -0xa0(%rbp)
movaps %xmm0, -0xb0(%rbp)
movaps %xmm0, -0xc0(%rbp)
movaps %xmm0, -0xd0(%rbp)
movaps %xmm0, -0xe0(%rbp)
movaps %xmm0, -0xf0(%rbp)
xorl %r14d, %r14d
cmpq $0x80, %r14
je 0x15f5
movq %r15, %rdi
leaq -0x70(%rbp), %rsi
movq -0x30(%rbp), %rdx
callq 0x1cc1
testl %eax, %eax
js 0x15f5
movb %al, -0xf0(%rbp,%r14)
incq %r14
jmp 0x14d0
leaq 0x1b91(%rip), %rdi      # 0x309a
xorl %eax, %eax
callq 0x1040
movl %r15d, %r14d
xorl %r13d, %r13d
cmpq %r13, %r14
je 0x1537
movzbl -0xf0(%rbp,%r13), %esi
leaq 0x1b79(%rip), %rdi      # 0x30a4
xorl %eax, %eax
callq 0x1040
incq %r13
jmp 0x1516
pushq $0xa
popq %rdi
callq 0x1160
cmpl $0x5, %r15d
jbe 0x1589
movzbl -0xf0(%rbp), %esi
movb -0xed(%rbp), %cl
movl $0x80, %r8d
shll %cl, %r8d
movzbl -0xef(%rbp), %edx
movzbl -0xee(%rbp), %ecx
movzbl -0xec(%rbp), %r9d
movzbl -0xeb(%rbp), %eax
movl %eax, (%rsp)
leaq 0x1b2a(%rip), %rdi      # 0x30ac
xorl %eax, %eax
callq 0x1040
movl $0xfea1a1a1, -0x34(%rbp) # imm = 0xFEA1A1A1
movl $0xffff, %edi           # imm = 0xFFFF
leaq -0x34(%rbp), %rsi
pushq $0x4
popq %r13
movq %r13, %rdx
callq 0x1d5f
movq -0x30(%rbp), %r14
leaq -0x44(%rbp), %r15
movzwl %ax, %edi
leaq -0xf0(%rbp), %rsi
movq %r13, %rdx
callq 0x1d5f
movzwl -0xec(%rbp), %ecx
rolw $0x8, %cx
movzwl %cx, %ecx
movzwl %ax, %edx
cmpl %edx, %ecx
je 0x136a
movq 0x3a01(%rip), %rax      # 0x4fe0
movq (%rax), %rdi
leaq 0x1af8(%rip), %rsi      # 0x30e1
xorl %eax, %eax
callq 0x1170
jmp 0x136a
leaq 0x1b14(%rip), %rdi      # 0x3110
callq 0x1180
leaq -0xf0(%rbp), %r13
xorl %r15d, %r15d
cmpq $0x7f, %r15
ja 0x1676
xorl %r14d, %r14d
cmpq $0x10, %r14
je 0x162f
movzbl (%r13,%r14), %esi
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1040
incq %r14
jmp 0x1614
leaq 0x1ad7(%rip), %rdi      # 0x310d
xorl %eax, %eax
callq 0x1040
xorl %r14d, %r14d
cmpq $0x10, %r14
je 0x1664
movzbl (%r13,%r14), %eax
leal -0x20(%rax), %ecx
cmpb $0x5f, %cl
jb 0x1657
movl %r12d, %eax
movzbl %al, %edi
callq 0x1160
incq %r14
jmp 0x1640
pushq $0xa
popq %rdi
callq 0x1160
addq $0x10, %r15
addq $0x10, %r13
jmp 0x160b
movq -0x30(%rbp), %r14
leaq -0x44(%rbp), %r15
jmp 0x136a
movq 0x3956(%rip), %rcx      # 0x4fe0
movq (%rcx), %rdi
leaq 0x19a4(%rip), %rsi      # 0x3038
movl %eax, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x1170
jmp 0x13bd
movq 0x3935(%rip), %rcx      # 0x4fe0
movq (%rcx), %rdi
leaq 0x19af(%rip), %rsi      # 0x3064
movl %eax, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x1170
jmp 0x13d7
movq 0x3914(%rip), %rcx      # 0x4fe0
movq (%rcx), %rdi
leaq 0x198e(%rip), %rsi      # 0x3064
movl %eax, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x1170
jmp 0x13f1
movq %r15, %rdi
leaq -0x70(%rbp), %rsi
movq %r14, %rdx
callq 0x1908
testb %al, %al
je 0x1709
movq %r15, %rdi
callq 0x1992
testl %eax, %eax
jne 0x135b
movq %r14, %rdi
callq 0x10e0
xorl %ebx, %ebx
leaq -0x60(%rbp), %rdi
callq 0x178a
movl %ebx, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x38a9(%rip), %rax      # 0x4fe0
movq (%rax), %rdi
movq (%rsi), %rdx
leaq 0x18c0(%rip), %rsi      # 0x3004
xorl %eax, %eax
callq 0x1170
jmp 0x1713
movq 0x388c(%rip), %rax      # 0x4fe0
movq (%rax), %rcx
leaq 0x18b3(%rip), %rdi      # 0x3011
pushq $0x13
popq %rsi
pushq $0x1
popq %rdx
callq 0x11d0
jmp 0x1713
jmp 0x1775
jmp 0x1775
jmp 0x1775
jmp 0x1775
jmp 0x1775
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x178a
movq %rbx, %rdi
callq 0x11b0
nop