// File: 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
subq $0x190, %rsp            # imm = 0x190
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2a90
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 0x2a6d
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x3c75(%rip), %rsi      # 0x600e
callq 0x2170
movq %rax, -0x150(%rbp)
jmp 0x23a7
movq -0x150(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x23e6
movl $0x1, -0x4(%rbp)
movl $0x1, -0x50(%rbp)
jmp 0x2a6d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2ab0
jmp 0x2a82
movq -0x58(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x3320
movb %al, -0x151(%rbp)
jmp 0x23ff
movb -0x151(%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 0x2a6d
leaq -0x20(%rbp), %rdi
callq 0x3220
jmp 0x2441
jmp 0x2443
movq -0x58(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x30a0
jmp 0x2456
jmp 0x2458
leaq -0x20(%rbp), %rdi
callq 0x3180
movl %eax, -0x158(%rbp)
jmp 0x2469
movl -0x158(%rbp), %eax
cmpl $0x10, %eax
jb 0x2a0b
leaq -0x20(%rbp), %rdi
movl $0x10, %esi
callq 0x3240
movl %eax, -0x15c(%rbp)
jmp 0x248e
movl -0x15c(%rbp), %eax
cmpl $0x4489, %eax           # imm = 0x4489
jne 0x29f4
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 0x3830
movl %eax, -0x160(%rbp)
jmp 0x24cc
movl -0x160(%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 0x3830
movl %eax, -0x164(%rbp)
jmp 0x2515
movl -0x164(%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 0x3830
movl %eax, -0x168(%rbp)
jmp 0x255e
movl -0x168(%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 0x3830
movl %eax, -0x16c(%rbp)
jmp 0x25a7
movl -0x16c(%rbp), %eax
movl %eax, -0x4c(%rbp)
movl -0x4c(%rbp), %eax
movl %eax, -0x68(%rbp)
cmpl $0xfa, -0x4c(%rbp)
je 0x25d5
cmpl $0xfb, -0x4c(%rbp)
je 0x25d5
cmpl $0xfe, -0x4c(%rbp)
jne 0x29f2
movl -0x4c(%rbp), %esi
leaq 0x3aa8(%rip), %rdi      # 0x6087
xorl %eax, %eax
callq 0x2050
jmp 0x25e8
cmpl $0xfe, -0x4c(%rbp)
jne 0x2804
movl $0x0, -0x94(%rbp)
movl $0x0, -0x98(%rbp)
cmpl $0x16, -0x98(%rbp)
jae 0x266b
movq -0x58(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x3830
movl %eax, -0x170(%rbp)
jmp 0x262b
movl -0x170(%rbp), %eax
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jge 0x263c
jmp 0x266b
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 0x2609
leaq 0x3a28(%rip), %rdi      # 0x609a
xorl %eax, %eax
callq 0x2050
jmp 0x267b
movl $0x0, -0x9c(%rbp)
movl -0x9c(%rbp), %eax
cmpl -0x94(%rbp), %eax
jae 0x26c4
movl -0x9c(%rbp), %eax
movzbl -0x90(%rbp,%rax), %esi
leaq 0x39fc(%rip), %rdi      # 0x60a4
xorl %eax, %eax
callq 0x2050
jmp 0x26b1
jmp 0x26b3
movl -0x9c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x9c(%rbp)
jmp 0x2685
leaq 0x3a40(%rip), %rdi      # 0x610b
xorl %eax, %eax
callq 0x2050
jmp 0x26d4
cmpl $0x6, -0x94(%rbp)
jb 0x2733
movzbl -0x90(%rbp), %esi
movzbl -0x8f(%rbp), %edx
movzbl -0x8e(%rbp), %eax
movl %eax, -0x174(%rbp)
movb -0x8d(%rbp), %cl
movl $0x80, %r8d
shll %cl, %r8d
movl -0x174(%rbp), %ecx
movzbl -0x8c(%rbp), %r9d
movzbl -0x8b(%rbp), %edi
movq %rsp, %rax
movl %edi, (%rax)
leaq 0x3984(%rip), %rdi      # 0x60ac
xorl %eax, %eax
callq 0x2050
jmp 0x2731
jmp 0x2733
movb $-0x5f, -0x9e(%rbp)
movb $-0x5f, -0x9f(%rbp)
movb $-0x5f, -0xa0(%rbp)
movb -0x68(%rbp), %al
movb %al, -0x9d(%rbp)
movl $0xffff, %edi           # imm = 0xFFFF
leaq -0xa0(%rbp), %rsi
movl $0x4, %edx
callq 0x3920
movw %ax, -0x176(%rbp)
jmp 0x2770
movw -0x176(%rbp), %ax
movw %ax, -0xa2(%rbp)
movzwl -0xa2(%rbp), %edi
leaq -0x90(%rbp), %rsi
movl $0x4, %edx
callq 0x3920
movw %ax, -0x178(%rbp)
jmp 0x279f
movw -0x178(%rbp), %ax
movw %ax, -0xa2(%rbp)
movzbl -0x8c(%rbp), %eax
shll $0x8, %eax
movzbl -0x8b(%rbp), %ecx
addl %ecx, %eax
movw %ax, -0xa4(%rbp)
movzwl -0xa4(%rbp), %eax
movzwl -0xa2(%rbp), %ecx
cmpl %ecx, %eax
je 0x27ff
movq 0x6800(%rip), %rax      # 0x8fe0
movq (%rax), %rdi
movzwl -0xa2(%rbp), %edx
movzwl -0xa4(%rbp), %ecx
leaq 0x38e9(%rip), %rsi      # 0x60e1
movb $0x0, %al
callq 0x21c0
jmp 0x29f0
movl -0x4c(%rbp), %eax
andl $0xfe, %eax
cmpl $0xfa, %eax
jne 0x29ee
movl $0x0, -0x134(%rbp)
leaq -0x130(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x20e0
movl $0x0, -0x138(%rbp)
cmpl $0x80, -0x138(%rbp)
jae 0x28a3
movq -0x58(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x3830
movl %eax, -0x17c(%rbp)
jmp 0x2863
movl -0x17c(%rbp), %eax
movl %eax, -0x4c(%rbp)
cmpl $0x0, -0x4c(%rbp)
jge 0x2874
jmp 0x28a3
movl -0x4c(%rbp), %eax
movb %al, %cl
movl -0x138(%rbp), %eax
movb %cl, -0x130(%rbp,%rax)
movl -0x138(%rbp), %eax
movl %eax, -0x134(%rbp)
movl -0x138(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x138(%rbp)
jmp 0x283e
leaq 0x3863(%rip), %rdi      # 0x610d
xorl %eax, %eax
callq 0x2050
jmp 0x28b3
movl $0x0, -0x13c(%rbp)
cmpl $0x80, -0x13c(%rbp)
jae 0x29ec
movl $0x0, -0x140(%rbp)
cmpl $0x10, -0x140(%rbp)
jae 0x2919
movl -0x13c(%rbp), %eax
movl -0x140(%rbp), %ecx
addl %ecx, %eax
movzbl -0x130(%rbp,%rax), %esi
leaq 0x37a9(%rip), %rdi      # 0x60a6
xorl %eax, %eax
callq 0x2050
jmp 0x2906
jmp 0x2908
movl -0x140(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x140(%rbp)
jmp 0x28d7
leaq 0x3800(%rip), %rdi      # 0x6120
xorl %eax, %eax
callq 0x2050
jmp 0x2929
movl $0x0, -0x144(%rbp)
cmpl $0x10, -0x144(%rbp)
jae 0x29c6
movl -0x13c(%rbp), %eax
addl -0x144(%rbp), %eax
movl %eax, %eax
movzbl -0x130(%rbp,%rax), %eax
cmpl $0x20, %eax
jl 0x299e
movl -0x13c(%rbp), %eax
addl -0x144(%rbp), %eax
movl %eax, %eax
movzbl -0x130(%rbp,%rax), %eax
cmpl $0x7f, %eax
jge 0x299e
movl -0x13c(%rbp), %eax
movl -0x144(%rbp), %ecx
addl %ecx, %eax
movzbl -0x130(%rbp,%rax), %esi
leaq 0x3790(%rip), %rdi      # 0x6123
xorl %eax, %eax
callq 0x2050
jmp 0x299c
jmp 0x29b0
leaq 0x3781(%rip), %rdi      # 0x6126
xorl %eax, %eax
callq 0x2050
jmp 0x29ae
jmp 0x29b0
jmp 0x29b2
movl -0x144(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x144(%rbp)
jmp 0x2933
leaq 0x373e(%rip), %rdi      # 0x610b
xorl %eax, %eax
callq 0x2050
jmp 0x29d6
jmp 0x29d8
movl -0x13c(%rbp), %eax
addl $0x10, %eax
movl %eax, -0x13c(%rbp)
jmp 0x28bd
jmp 0x29ee
jmp 0x29f0
jmp 0x29f2
jmp 0x2a06
leaq -0x20(%rbp), %rdi
movl $0x1, %esi
callq 0x32b0
jmp 0x2a04
jmp 0x2a06
jmp 0x2458
movq -0x58(%rbp), %rdx
leaq -0x20(%rbp), %rdi
leaq -0x48(%rbp), %rsi
callq 0x30a0
movb %al, -0x17d(%rbp)
jmp 0x2a24
movb -0x17d(%rbp), %al
testb $0x1, %al
jne 0x2a32
jmp 0x2a30
jmp 0x2a54
jmp 0x2a34
leaq -0x20(%rbp), %rdi
callq 0x3180
movl %eax, -0x184(%rbp)
jmp 0x2a45
movl -0x184(%rbp), %eax
cmpl $0x0, %eax
ja 0x2443
movq -0x58(%rbp), %rdi
callq 0x2140
jmp 0x2a5f
movl $0x0, -0x4(%rbp)
movl $0x1, -0x50(%rbp)
leaq -0x48(%rbp), %rdi
callq 0x2ab0
movl -0x4(%rbp), %eax
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
movq -0x60(%rbp), %rdi
callq 0x2210
nopl (%rax,%rax)