// File: /workspace/github_cpp_makefile_star2_O3/joncampbell123[P]floppytools/kryoflux5.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);
                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]);
                        }
                    }
                    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 $0xc8, %rsp
xorps %xmm0, %xmm0
movups %xmm0, -0x50(%rbp)
movq $0x0, -0x40(%rbp)
cmpl $0x1, %edi
jle 0x17ca
movq 0x8(%rsi), %rdi
leaq 0x1c2b(%rip), %rsi      # 0x300e
callq 0x1110
movl $0x1, %r14d
testq %rax, %rax
je 0x17a7
movq %rax, %rbx
leaq -0x38(%rbp), %rdi
leaq -0x60(%rbp), %rsi
movq %rax, %rdx
callq 0x1abc
testb %al, %al
je 0x17da
leaq -0x38(%rbp), %rdi
callq 0x1a60
leaq -0x38(%rbp), %r14
leaq 0x1c80(%rip), %r12      # 0x30a6
movq %r14, %rdi
leaq -0x60(%rbp), %rsi
movq %rbx, %rdx
callq 0x1992
movq %r14, %rdi
callq 0x1a14
cmpl $0x10, %eax
jb 0x1779
movq %r14, %rdi
movl $0x10, %esi
callq 0x1a6e
cmpl $0x4489, %eax           # imm = 0x4489
jne 0x1556
movq -0x58(%rbp), %rsi
leaq 0x1bbc(%rip), %rdi      # 0x3025
xorl %eax, %eax
callq 0x1040
movq %r14, %rdi
leaq -0x60(%rbp), %rsi
movq %rbx, %rdx
callq 0x1d0e
cmpl $0xffffff5f, %eax       # imm = 0xFFFFFF5F
jne 0x1716
movq %r14, %rdi
leaq -0x60(%rbp), %rsi
movq %rbx, %rdx
callq 0x1d0e
cmpl $0xffffff5f, %eax       # imm = 0xFFFFFF5F
jne 0x1737
movq %r14, %rdi
leaq -0x60(%rbp), %rsi
movq %rbx, %rdx
callq 0x1d0e
cmpl $0xffffff5f, %eax       # imm = 0xFFFFFF5F
jne 0x1758
movq %r14, %rdi
leaq -0x60(%rbp), %rsi
movq %rbx, %rdx
callq 0x1d0e
movl %eax, %r13d
leal -0xfa(%r13), %eax
cmpl $0x4, %eax
ja 0x1435
movl $0x13, %ecx
btl %eax, %ecx
jae 0x1435
leaq 0x1b92(%rip), %rdi      # 0x3087
movl %r13d, %esi
xorl %eax, %eax
callq 0x1040
cmpl $0xfe, %r13d
jne 0x1568
xorl %r13d, %r13d
xorl %r15d, %r15d
movq %r14, %rdi
leaq -0x60(%rbp), %rsi
movq %rbx, %rdx
callq 0x1d0e
testl %eax, %eax
js 0x1667
movb %al, -0xe0(%rbp,%r13)
movl %r13d, %r15d
leaq 0x1(%r13), %rax
movq %rax, %r13
cmpq $0x16, %rax
jne 0x150e
leaq 0x1b56(%rip), %rdi      # 0x309a
xorl %eax, %eax
callq 0x1040
movl $0x15, %r15d
jmp 0x167e
movq %r14, %rdi
movl $0x1, %esi
callq 0x1a94
jmp 0x1435
andl $0xfe, %r13d
cmpl $0xfa, %r13d
jne 0x1435
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
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)
xorl %r14d, %r14d
leaq -0x38(%rbp), %rdi
leaq -0x60(%rbp), %rsi
movq %rbx, %rdx
callq 0x1d0e
testl %eax, %eax
js 0x15dc
movb %al, -0xe0(%rbp,%r14)
incq %r14
cmpq $0x80, %r14
jne 0x15b4
leaq 0x1b01(%rip), %rdi      # 0x30e4
callq 0x1160
leaq -0xe0(%rbp), %r13
xorl %r15d, %r15d
xorl %r14d, %r14d
movzbl (%r13,%r14), %esi
movq %r12, %rdi
xorl %eax, %eax
callq 0x1040
incq %r14
cmpq $0x10, %r14
jne 0x15f5
leaq 0x1acc(%rip), %rdi      # 0x30e1
xorl %eax, %eax
callq 0x1040
xorl %r14d, %r14d
movzbl (%r13,%r14), %eax
leal -0x20(%rax), %ecx
cmpb $0x5f, %cl
jb 0x1632
movl $0x2e, %eax
movzbl %al, %edi
callq 0x1140
incq %r14
cmpq $0x10, %r14
jne 0x161f
movl $0xa, %edi
callq 0x1140
leaq 0x10(%r15), %rax
addq $0x10, %r13
cmpq $0x70, %r15
movq %rax, %r15
jb 0x15f2
leaq -0x38(%rbp), %r14
jmp 0x1435
leaq 0x1a2c(%rip), %rdi      # 0x309a
xorl %eax, %eax
callq 0x1040
testl %r15d, %r15d
je 0x1707
movl %r15d, %r14d
xorl %r15d, %r15d
leaq 0x1a19(%rip), %r13      # 0x30a4
movzbl -0xe0(%rbp,%r15), %esi
movq %r13, %rdi
xorl %eax, %eax
callq 0x1040
incq %r15
cmpq %r15, %r14
jne 0x168b
movl $0xa, %edi
callq 0x1140
cmpl $0x5, %r14d
leaq -0x38(%rbp), %r14
jbe 0x1435
movzbl -0xe0(%rbp), %esi
movb -0xdd(%rbp), %cl
movl $0x80, %r8d
shll %cl, %r8d
movzbl -0xdf(%rbp), %edx
movzbl -0xde(%rbp), %ecx
movzbl -0xdc(%rbp), %r9d
movzbl -0xdb(%rbp), %eax
movl %eax, (%rsp)
leaq 0x19b1(%rip), %rdi      # 0x30ac
xorl %eax, %eax
callq 0x1040
jmp 0x1435
movl $0xa, %edi
callq 0x1140
jmp 0x1435
movq 0x38c3(%rip), %rcx      # 0x4fe0
movq (%rcx), %rdi
leaq 0x1911(%rip), %rsi      # 0x3038
movl %eax, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x1150
jmp 0x148a
movq 0x38a2(%rip), %rcx      # 0x4fe0
movq (%rcx), %rdi
leaq 0x191c(%rip), %rsi      # 0x3064
movl %eax, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x1150
jmp 0x14a4
movq 0x3881(%rip), %rcx      # 0x4fe0
movq (%rcx), %rdi
leaq 0x18fb(%rip), %rsi      # 0x3064
movl %eax, %edx
movl %eax, %ecx
xorl %eax, %eax
callq 0x1150
jmp 0x14be
movq %r14, %rdi
leaq -0x60(%rbp), %rsi
movq %rbx, %rdx
callq 0x1992
testb %al, %al
je 0x179c
movq %r14, %rdi
callq 0x1a14
testl %eax, %eax
jne 0x1426
movq %rbx, %rdi
callq 0x10e0
xorl %r14d, %r14d
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x17b5
callq 0x10d0
movl %r14d, %eax
addq $0xc8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rsi, %rdi
callq 0x1202
movl $0x1, %r14d
jmp 0x17a7
callq 0x11e0
jmp 0x17a7
jmp 0x17e9
jmp 0x17e9
jmp 0x17e9
jmp 0x17e9
movq %rax, %rbx
movq -0x50(%rbp), %rdi
testq %rdi, %rdi
je 0x17fa
callq 0x10d0
movq %rbx, %rdi
callq 0x1190
nop