// File: 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
subq $0x180, %rsp            # imm = 0x180
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x29c0
cmpl $0x2, -0x8(%rbp)
jge 0x238a
movq 0x6c81(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x3c94(%rip), %rsi      # 0x6004
movb $0x0, %al
callq 0x21c0
movl $0x1, -0x4(%rbp)
movl $0x1, -0x50(%rbp)
jmp 0x299b
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x3c75(%rip), %rsi      # 0x600e
callq 0x2170
movq %rax, -0x140(%rbp)
jmp 0x23a7
movq -0x140(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x23e6
movl $0x1, -0x4(%rbp)
movl $0x1, -0x50(%rbp)
jmp 0x299b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x29e0
jmp 0x29b0
movq -0x58(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x3250
movb %al, -0x141(%rbp)
jmp 0x23ff
movb -0x141(%rbp), %al
testb $0x1, %al
jne 0x2436
jmp 0x240b
movq 0x6bce(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
leaq 0x3bf5(%rip), %rsi      # 0x6011
movb $0x0, %al
callq 0x21c0
movl $0x1, -0x4(%rbp)
movl $0x1, -0x50(%rbp)
jmp 0x299b
leaq -0x20(%rbp), %rdi
callq 0x3150
jmp 0x2441
jmp 0x2443
movq -0x58(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x2fd0
jmp 0x2456
jmp 0x2458
leaq -0x20(%rbp), %rdi
callq 0x30b0
movl %eax, -0x148(%rbp)
jmp 0x2469
movl -0x148(%rbp), %eax
cmpl $0x10, %eax
jb 0x2939
leaq -0x20(%rbp), %rdi
movl $0x10, %esi
callq 0x3170
movl %eax, -0x14c(%rbp)
jmp 0x248e
movl -0x14c(%rbp), %eax
cmpl $0x4489, %eax           # imm = 0x4489
jne 0x2922
movq -0x40(%rbp), %rsi
leaq 0x3b7b(%rip), %rdi      # 0x6025
xorl %eax, %eax
callq 0x2050
jmp 0x24b3
movq -0x58(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x3760
movl %eax, -0x150(%rbp)
jmp 0x24cc
movl -0x150(%rbp), %eax
movl %eax, -0x4c(%rbp)
cmpl $0xffffff5f, -0x4c(%rbp) # imm = 0xFFFFFF5F
je 0x24fc
movq 0x6afb(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
movl -0x4c(%rbp), %edx
movl -0x4c(%rbp), %ecx
leaq 0x3b43(%rip), %rsi      # 0x6038
movb $0x0, %al
callq 0x21c0
movq -0x58(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x3760
movl %eax, -0x154(%rbp)
jmp 0x2515
movl -0x154(%rbp), %eax
movl %eax, -0x4c(%rbp)
cmpl $0xffffff5f, -0x4c(%rbp) # imm = 0xFFFFFF5F
je 0x2545
movq 0x6ab2(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
movl -0x4c(%rbp), %edx
movl -0x4c(%rbp), %ecx
leaq 0x3b26(%rip), %rsi      # 0x6064
movb $0x0, %al
callq 0x21c0
movq -0x58(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x3760
movl %eax, -0x158(%rbp)
jmp 0x255e
movl -0x158(%rbp), %eax
movl %eax, -0x4c(%rbp)
cmpl $0xffffff5f, -0x4c(%rbp) # imm = 0xFFFFFF5F
je 0x258e
movq 0x6a69(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
movl -0x4c(%rbp), %edx
movl -0x4c(%rbp), %ecx
leaq 0x3add(%rip), %rsi      # 0x6064
movb $0x0, %al
callq 0x21c0
movq -0x58(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x3760
movl %eax, -0x15c(%rbp)
jmp 0x25a7
movl -0x15c(%rbp), %eax
movl %eax, -0x4c(%rbp)
cmpl $0xfa, -0x4c(%rbp)
je 0x25cf
cmpl $0xfb, -0x4c(%rbp)
je 0x25cf
cmpl $0xfe, -0x4c(%rbp)
jne 0x2920
movl -0x4c(%rbp), %esi
leaq 0x3aae(%rip), %rdi      # 0x6087
xorl %eax, %eax
callq 0x2050
jmp 0x25e2
cmpl $0xfe, -0x4c(%rbp)
jne 0x2732
movl $0x0, -0x94(%rbp)
movl $0x0, -0x98(%rbp)
cmpl $0x16, -0x98(%rbp)
jae 0x2665
movq -0x58(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x3760
movl %eax, -0x160(%rbp)
jmp 0x2625
movl -0x160(%rbp), %eax
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jge 0x2636
jmp 0x2665
movl -0x4c(%rbp), %eax
movb %al, %cl
movl -0x98(%rbp), %eax
movb %cl, -0x90(%rbp,%rax)
movl -0x98(%rbp), %eax
movl %eax, -0x94(%rbp)
movl -0x98(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x98(%rbp)
jmp 0x2603
leaq 0x3a2e(%rip), %rdi      # 0x609a
xorl %eax, %eax
callq 0x2050
jmp 0x2675
movl $0x0, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
cmpl -0x94(%rbp), %eax
jae 0x26be
movl -0x9c(%rbp), %eax
movzbl -0x90(%rbp,%rax), %esi
leaq 0x3a02(%rip), %rdi      # 0x60a4
xorl %eax, %eax
callq 0x2050
jmp 0x26ab
jmp 0x26ad
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x267f
leaq 0x3a2d(%rip), %rdi      # 0x60f2
xorl %eax, %eax
callq 0x2050
jmp 0x26ce
cmpl $0x6, -0x94(%rbp)
jb 0x272d
movzbl -0x90(%rbp), %esi
movzbl -0x8f(%rbp), %edx
movzbl -0x8e(%rbp), %eax
movl %eax, -0x164(%rbp)
movb -0x8d(%rbp), %cl
movl $0x80, %r8d
shll %cl, %r8d
movl -0x164(%rbp), %ecx
movzbl -0x8c(%rbp), %r9d
movzbl -0x8b(%rbp), %edi
movq %rsp, %rax
movl %edi, (%rax)
leaq 0x398a(%rip), %rdi      # 0x60ac
xorl %eax, %eax
callq 0x2050
jmp 0x272b
jmp 0x272d
jmp 0x291e
movl -0x4c(%rbp), %eax
andl $0xfe, %eax
cmpl $0xfa, %eax
jne 0x291c
movl $0x0, -0x124(%rbp)
leaq -0x120(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x20e0
movl $0x0, -0x128(%rbp)
cmpl $0x80, -0x128(%rbp)
jae 0x27d1
movq -0x58(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x3760
movl %eax, -0x168(%rbp)
jmp 0x2791
movl -0x168(%rbp), %eax
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jge 0x27a2
jmp 0x27d1
movl -0x4c(%rbp), %eax
movb %al, %cl
movl -0x128(%rbp), %eax
movb %cl, -0x120(%rbp,%rax)
movl -0x128(%rbp), %eax
movl %eax, -0x124(%rbp)
movl -0x128(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x128(%rbp)
jmp 0x276c
leaq 0x3909(%rip), %rdi      # 0x60e1
xorl %eax, %eax
callq 0x2050
jmp 0x27e1
movl $0x0, -0x12c(%rbp)
cmpl $0x80, -0x12c(%rbp)
jae 0x291a
movl $0x0, -0x130(%rbp)
cmpl $0x10, -0x130(%rbp)
jae 0x2847
movl -0x12c(%rbp), %eax
movl -0x130(%rbp), %ecx
addl %ecx, %eax
movzbl -0x120(%rbp,%rax), %esi
leaq 0x387b(%rip), %rdi      # 0x60a6
xorl %eax, %eax
callq 0x2050
jmp 0x2834
jmp 0x2836
movl -0x130(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x130(%rbp)
jmp 0x2805
leaq 0x38a6(%rip), %rdi      # 0x60f4
xorl %eax, %eax
callq 0x2050
jmp 0x2857
movl $0x0, -0x134(%rbp)
cmpl $0x10, -0x134(%rbp)
jae 0x28f4
movl -0x12c(%rbp), %eax
addl -0x134(%rbp), %eax
movl %eax, %eax
movzbl -0x120(%rbp,%rax), %eax
cmpl $0x20, %eax
jl 0x28cc
movl -0x12c(%rbp), %eax
addl -0x134(%rbp), %eax
movl %eax, %eax
movzbl -0x120(%rbp,%rax), %eax
cmpl $0x7f, %eax
jge 0x28cc
movl -0x12c(%rbp), %eax
movl -0x134(%rbp), %ecx
addl %ecx, %eax
movzbl -0x120(%rbp,%rax), %esi
leaq 0x3836(%rip), %rdi      # 0x60f7
xorl %eax, %eax
callq 0x2050
jmp 0x28ca
jmp 0x28de
leaq 0x3827(%rip), %rdi      # 0x60fa
xorl %eax, %eax
callq 0x2050
jmp 0x28dc
jmp 0x28de
jmp 0x28e0
movl -0x134(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x134(%rbp)
jmp 0x2861
leaq 0x37f7(%rip), %rdi      # 0x60f2
xorl %eax, %eax
callq 0x2050
jmp 0x2904
jmp 0x2906
movl -0x12c(%rbp), %eax
addl $0x10, %eax
movl %eax, -0x12c(%rbp)
jmp 0x27eb
jmp 0x291c
jmp 0x291e
jmp 0x2920
jmp 0x2934
leaq -0x20(%rbp), %rdi
movl $0x1, %esi
callq 0x31e0
jmp 0x2932
jmp 0x2934
jmp 0x2458
movq -0x58(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x2fd0
movb %al, -0x169(%rbp)
jmp 0x2952
movb -0x169(%rbp), %al
testb $0x1, %al
jne 0x2960
jmp 0x295e
jmp 0x2982
jmp 0x2962
leaq -0x20(%rbp), %rdi
callq 0x30b0
movl %eax, -0x170(%rbp)
jmp 0x2973
movl -0x170(%rbp), %eax
cmpl $0x0, %eax
ja 0x2443
movq -0x58(%rbp), %rdi
callq 0x2140
jmp 0x298d
movl $0x0, -0x4(%rbp)
movl $0x1, -0x50(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x29e0
movl -0x4(%rbp), %eax
addq $0x180, %rsp            # imm = 0x180
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x2210
nopl (%rax)