// File: /workspace/github_cpp_makefile_star2_O2/joncampbell123[P]floppytools/kryoflux7.cpp
// Function: main
int main(int argc,char **argv) {
    struct flux_bits fb;
    struct kryoflux_event ev;
    std::vector<std::string> cappaths;
    std::string path;
    FILE *dsk_fp;
    int c;

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

    for (int i=1;i < argc;i++) {
        char *a = argv[i];

        if (*a == '-') {
            do { a++; } while (*a == '-');

            if (!strcmp(a,"1.4mb")) {
                sectors = 18;
                heads = 2;
                tracks = 80;
                sector_size = 512;
            }
            else if (!strcmp(a,"720k")) {
                sectors = 9;
                heads = 2;
                tracks = 80;
                sector_size = 512;
            }
            else {
                fprintf(stderr,"Unknown switch %s\n",a);
                return 1;
            }
        }
        else {
            cappaths.push_back(argv[i]);
        }
    }

    dsk_fp = fopen("disk.img","wb");
    if (dsk_fp == NULL) return 1;

    captured.resize(heads * sectors * tracks);

    for (size_t capidx=0;capidx < cappaths.size();capidx++) {
        for (unsigned int track=0;track < tracks;track++) {
            for (unsigned int head=0;head < heads;head++) {
                {
                    char tmp[128];

                    sprintf(tmp,"track%02u.%u.raw",track,head);
                    path = cappaths[capidx] + "/" + tmp;
                }

                printf("%s...\n",path.c_str());

                FILE *fp = fopen(path.c_str(),"rb");
                if (fp == NULL) {
                    printf("Failed to open\n");
                    continue;
                }

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

                flux_bits ofb = fb;

                int adj_span = 8;
                int dadj_span = 8;

                for (int adj_c = 0;adj_c <= (adj_span*2);adj_c++) {
                    for (int dadj_c = 0;dadj_c <= (dadj_span*2);dadj_c++) {
                        unsigned int capcount = 0;

                        int adj = adj_c;
                        int dadj = dadj_c;

                        if (adj >= (adj_span+1)) adj -= adj_span*2;
                        if (dadj >= (dadj_span+1)) dadj -= dadj_span*2;

                        if ((int)ofb.shortest+(int)adj <= 0)
                            continue;
                        fb.shortest = ofb.shortest + adj;

                        if ((int)ofb.dist+(int)dadj <= 0)
                            continue;
                        fb.dist = ofb.dist + dadj;

                        unsigned long snum = ((track * heads) + head) * sectors;

                        for (size_t i=0;i < sectors;i++)
                            capcount += captured[i+snum];

                        if (capcount >= sectors) {
                            printf("Track %u head %u already captured\n",track,head);
                            adj_c = 99;
                            dadj_c = 99;
                            break;
                        }
                        else {
                            printf("Track %u head %u capture progress: %u/%u adj=%d dadj=%d ",track,head,capcount,sectors,adj,dadj);
                            for (size_t i=0;i < sectors;i++) printf("%u",captured[i+snum]?1:0);
                            printf("\n");
                        }

                        fseek(fp,0,SEEK_SET);
                        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) {
                                    process_sync(dsk_fp,fb,ev,fp);
                                }
                                else {
                                    fb.get(1);
                                }
                            }

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

                {
                    unsigned int capcount = 0;
                    {
                        unsigned long snum = ((track * heads) + head) * sectors;
                        for (size_t i=0;i < sectors;i++)
                            capcount += captured[i+snum];
                    }

                    if (capcount < sectors) {
                        printf("Track %u head %u not captured fully (%u < %u)\n",track,head,capcount,sectors);
                    }
                }

                fclose(fp);
            }
        }
    }

    {
        size_t count = 0;

        for (size_t i=0;i < captured.size();i++) {
            if (captured[i]) count++;
        }

        printf("Capture report: %zu captured / %zu total\n",count,captured.size());

        if (count < captured.size()) {
            printf("Missing sectors:\n");
            for (size_t i=0;i < captured.size();i++) {
                if (!captured[i])
                    printf(" %zu (C/H/S %u/%u/%u)\n",
                        i,
                        i / heads / sectors,
                        (i / sectors) % heads,
                        (i % sectors) + 1);
            }
        }
    }

    fclose(dsk_fp);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x198, %rsp            # imm = 0x198
movq %rsi, %r14
leaq -0xe0(%rbp), %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
andq $0x0, -0x60(%rbp)
leaq -0xb8(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movaps %xmm0, -0x70(%rbp)
movb $0x0, (%rax)
pushq $0x1
popq %rax
movl %eax, -0x40(%rbp)
cmpl $0x2, %edi
jl 0x2e10
movl %edi, %ebx
pushq $0x1
popq %r12
leaq -0x1b0(%rbp), %r13
cmpq %rbx, %r12
je 0x29e2
movq (%r14,%r12,8), %r15
cmpb $0x2d, (%r15)
jne 0x298a
cmpb $0x2d, 0x1(%r15)
leaq 0x1(%r15), %r15
je 0x2949
movq %r15, %rdi
leaq 0x27b9(%rip), %rsi      # 0x5117
callq 0x21d0
testl %eax, %eax
je 0x29b2
movq %r15, %rdi
leaq 0x27ac(%rip), %rsi      # 0x511d
callq 0x21d0
testl %eax, %eax
jne 0x2f41
movl $0x9, 0x57b0(%rip)      # 0x8138
jmp 0x29bc
movq %r13, %rdi
movq %r15, %rsi
leaq -0x130(%rbp), %rdx
callq 0x2090
leaq -0x70(%rbp), %rdi
movq %r13, %rsi
callq 0x3290
movq %r13, %rdi
callq 0x20b0
jmp 0x29da
movl $0x12, 0x577c(%rip)     # 0x8138
movl $0x2, 0x5776(%rip)      # 0x813c
movl $0x50, 0x5770(%rip)     # 0x8140
movl $0x200, 0x576a(%rip)    # imm = 0x200
incq %r12
jmp 0x2936
leaq 0x274c(%rip), %rdi      # 0x5135
leaq 0x274e(%rip), %rsi      # 0x513e
callq 0x2170
testq %rax, %rax
je 0x2f5c
movq %rax, %r14
movl 0x5731(%rip), %esi      # 0x8138
imull 0x572e(%rip), %esi      # 0x813c
imull 0x572b(%rip), %esi      # 0x8140
leaq 0x9754(%rip), %rdi      # 0xc170
xorl %edx, %edx
callq 0x304a
leaq -0x1b0(%rbp), %rbx
leaq -0x110(%rbp), %r15
leaq -0x80(%rbp), %r13
xorl %esi, %esi
movq %r14, -0x50(%rbp)
movq -0x68(%rbp), %rax
subq -0x70(%rbp), %rax
sarq $0x5, %rax
cmpq %rax, %rsi
jae 0x2e29
xorl %edx, %edx
movq %rsi, %rax
shlq $0x5, %rax
movq %rax, -0x90(%rbp)
movq %rsi, -0x88(%rbp)
cmpl 0x56d3(%rip), %edx      # 0x8140
jae 0x2e08
xorl %ecx, %ecx
movl %edx, -0x2c(%rbp)
cmpl 0x56be(%rip), %ecx      # 0x813c
jae 0x2e01
movq %rbx, %rdi
leaq 0x26b3(%rip), %rsi      # 0x5141
movl %ecx, -0x30(%rbp)
xorl %eax, %eax
callq 0x2050
movq -0x70(%rbp), %rsi
addq -0x90(%rbp), %rsi
movq %r15, %rdi
leaq 0x26a5(%rip), %rdx      # 0x5152
callq 0x30ea
leaq -0x130(%rbp), %r12
movq %r12, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x30c1
leaq -0xc8(%rbp), %rdi
movq %r12, %rsi
callq 0x2180
movq %r12, %rdi
callq 0x20b0
movq %r15, %rdi
callq 0x20b0
movq -0xc8(%rbp), %rsi
leaq 0x2660(%rip), %rdi      # 0x5154
xorl %eax, %eax
callq 0x2040
movq -0xc8(%rbp), %rdi
leaq 0x2652(%rip), %rsi      # 0x515b
callq 0x2170
testq %rax, %rax
je 0x2dad
movq %rax, %rbx
movq %r13, %rdi
leaq -0xf0(%rbp), %rsi
movq %rax, %rdx
callq 0x3d01
testb %al, %al
je 0x2ddb
movl -0x78(%rbp), %eax
movq %rax, -0x98(%rbp)
movl -0x74(%rbp), %eax
movl %eax, -0x44(%rbp)
xorl %r12d, %r12d
cmpl $0x10, %r12d
jg 0x2d33
leal -0x10(%r12), %ecx
cmpl $0x9, %r12d
cmovll %r12d, %ecx
movq -0x98(%rbp), %rax
movq %rcx, -0xa8(%rbp)
addl %ecx, %eax
movl %eax, -0x34(%rbp)
xorl %eax, %eax
movq %r12, -0xa0(%rbp)
cmpl $0x11, %eax
je 0x2d0c
leal -0x10(%rax), %ecx
cmpl $0x9, %eax
movq %rax, -0x40(%rbp)
cmovbl %eax, %ecx
cmpl $0x0, -0x34(%rbp)
jle 0x2d01
movl -0x34(%rbp), %eax
movl %eax, -0x78(%rbp)
movl %ecx, %eax
addl -0x44(%rbp), %eax
jle 0x2d01
movl %ecx, -0x48(%rbp)
movl %eax, -0x74(%rbp)
movl 0x5580(%rip), %r8d      # 0x8138
movl 0x557d(%rip), %r14d     # 0x813c
imull -0x2c(%rbp), %r14d
addl -0x30(%rbp), %r14d
imull %r8d, %r14d
xorl %r12d, %r12d
xorl %r15d, %r15d
movl %r8d, %eax
cmpq %rax, %r15
jae 0x2c01
leaq (%r14,%r15), %rsi
leaq 0x958b(%rip), %rdi      # 0xc170
callq 0x3006
andq (%rax), %rdx
cmpq $0x1, %rdx
sbbl $-0x1, %r12d
incq %r15
movl 0x5539(%rip), %r8d      # 0x8138
jmp 0x2bd2
cmpl %r8d, %r12d
jae 0x2d14
movl -0x48(%rbp), %eax
movl %eax, (%rsp)
leaq 0x257e(%rip), %rdi      # 0x5195
movl -0x2c(%rbp), %esi
movl -0x30(%rbp), %edx
movl %r12d, %ecx
movq -0xa8(%rbp), %r9
xorl %eax, %eax
callq 0x2040
xorl %r12d, %r12d
leaq -0xf0(%rbp), %r15
movl 0x54fa(%rip), %eax      # 0x8138
cmpq %rax, %r12
jae 0x2c6f
leaq (%r14,%r12), %rsi
leaq 0x9522(%rip), %rdi      # 0xc170
callq 0x3006
xorl %esi, %esi
testq %rdx, (%rax)
setne %sil
leaq 0x256b(%rip), %rdi      # 0x51ce
xorl %eax, %eax
callq 0x2040
incq %r12
jmp 0x2c38
pushq $0xa
popq %rdi
callq 0x21c0
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2160
movq %r13, %rdi
callq 0x3c7c
movq -0x50(%rbp), %r14
movq -0xa0(%rbp), %r12
movq %r13, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x3b90
movq %r13, %rdi
callq 0x3c1a
cmpl $0x10, %eax
jb 0x2ce3
movq %r13, %rdi
pushq $0x10
popq %rsi
callq 0x3c86
cmpl $0x4489, %eax           # imm = 0x4489
jne 0x2cd6
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x239c
jmp 0x2ca4
movq %r13, %rdi
pushq $0x1
popq %rsi
callq 0x3cc2
jmp 0x2ca4
movq %r13, %rdi
movq %r15, %rsi
movq %rbx, %rdx
callq 0x3b90
testb %al, %al
je 0x2d01
movq %r13, %rdi
callq 0x3c1a
testl %eax, %eax
jne 0x2c96
movq -0x40(%rbp), %rax
incl %eax
jmp 0x2b7a
incl %r12d
jmp 0x2b47
leaq 0x2457(%rip), %rdi      # 0x5172
movl -0x2c(%rbp), %esi
movl -0x30(%rbp), %edx
xorl %eax, %eax
callq 0x2040
pushq $0x64
popq %rax
movl %eax, %r12d
jmp 0x2b47
movl 0x53fe(%rip), %r8d      # 0x8138
movl 0x53fb(%rip), %r15d     # 0x813c
imull -0x2c(%rbp), %r15d
addl -0x30(%rbp), %r15d
imull %r8d, %r15d
xorl %r12d, %r12d
xorl %r14d, %r14d
movl %r8d, %eax
cmpq %rax, %r14
jae 0x2d83
leaq (%r15,%r14), %rsi
leaq 0x9409(%rip), %rdi      # 0xc170
callq 0x3006
andq (%rax), %rdx
cmpq $0x1, %rdx
sbbl $-0x1, %r12d
incq %r14
movl 0x53b7(%rip), %r8d      # 0x8138
jmp 0x2d54
cmpl %r8d, %r12d
jae 0x2d9f
leaq 0x2442(%rip), %rdi      # 0x51d1
movl -0x2c(%rbp), %esi
movl -0x30(%rbp), %edx
movl %r12d, %ecx
xorl %eax, %eax
callq 0x2040
movq %rbx, %rdi
callq 0x2140
movq -0x50(%rbp), %r14
jmp 0x2db9
leaq 0x2573(%rip), %rdi      # 0x5327
callq 0x21f0
movl -0x30(%rbp), %ecx
incl %ecx
leaq -0x1b0(%rbp), %rbx
leaq -0x110(%rbp), %r15
movq -0x88(%rbp), %rsi
movl -0x2c(%rbp), %edx
jmp 0x2a78
movq 0x51fe(%rip), %rax      # 0x7fe0
movq (%rax), %rcx
leaq 0x2372(%rip), %rdi      # 0x515e
pushq $0x13
popq %rsi
pushq $0x1
popq %rdx
callq 0x2240
movq %rbx, %rdi
callq 0x2140
jmp 0x2db9
incl %edx
jmp 0x2a67
incq %rsi
jmp 0x2a3b
movq 0x51c9(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
movq (%r14), %rdx
leaq 0x22ce(%rip), %rsi      # 0x50f2
jmp 0x2f55
xorl %ebx, %ebx
leaq 0x933e(%rip), %r15      # 0xc170
xorl %r12d, %r12d
movq 0x9344(%rip), %rax      # 0xc180
movl 0x9346(%rip), %ecx      # 0xc188
subq 0x9327(%rip), %rax      # 0xc170
leaq (%rcx,%rax,8), %rdx
cmpq %rdx, %r12
jae 0x2e6d
movq %r15, %rdi
movq %r12, %rsi
callq 0x3006
andq (%rax), %rdx
cmpq $0x1, %rdx
sbbq $-0x1, %rbx
incq %r12
jmp 0x2e35
leaq 0x238c(%rip), %rdi      # 0x5200
movq %rbx, %rsi
xorl %eax, %eax
callq 0x2040
movq 0x92fb(%rip), %rax      # 0xc180
movl 0x92fd(%rip), %ecx      # 0xc188
subq 0x92de(%rip), %rax      # 0xc170
leaq (%rcx,%rax,8), %rax
cmpq %rax, %rbx
jae 0x2f30
leaq 0x2470(%rip), %rdi      # 0x5316
callq 0x21f0
leaq 0x92be(%rip), %rbx      # 0xc170
leaq 0x2371(%rip), %r15      # 0x522a
xorl %r12d, %r12d
movq 0x92bd(%rip), %rax      # 0xc180
movl 0x92bf(%rip), %ecx      # 0xc188
subq 0x92a0(%rip), %rax      # 0xc170
leaq (%rcx,%rax,8), %rax
cmpq %rax, %r12
jae 0x2f30
movq %rbx, %rdi
movq %r12, %rsi
callq 0x3006
testq %rdx, (%rax)
jne 0x2f2b
movl 0x524d(%rip), %ecx      # 0x813c
movq %r12, %rax
xorl %edx, %edx
divq %rcx
movl 0x523b(%rip), %esi      # 0x8138
xorl %edx, %edx
divq %rsi
movq %rax, %r9
movq %r12, %rax
xorl %edx, %edx
divq %rsi
movq %rdx, %r8
xorl %edx, %edx
divq %rcx
movq %rdx, %rcx
incq %r8
movq %r15, %rdi
movq %r12, %rsi
movq %r9, %rdx
xorl %eax, %eax
callq 0x2040
incq %r12
jmp 0x2ebc
movl $0x0, -0x40(%rbp)
movq %r14, %rdi
callq 0x2140
jmp 0x2f5c
movq 0x5098(%rip), %rax      # 0x7fe0
movq (%rax), %rdi
leaq 0x21d0(%rip), %rsi      # 0x5122
movq %r15, %rdx
xorl %eax, %eax
callq 0x21e0
leaq -0xc8(%rbp), %rdi
callq 0x20b0
leaq -0x70(%rbp), %rdi
callq 0x3122
leaq -0xe0(%rbp), %rdi
callq 0x315c
movl -0x40(%rbp), %eax
addq $0x198, %rsp            # imm = 0x198
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x2fc5
jmp 0x2fc5
jmp 0x2fc5
jmp 0x2fc5
movq %rax, %rbx
leaq -0x110(%rbp), %rdi
jmp 0x2fb4
jmp 0x2fc5
jmp 0x2fc5
movq %rax, %rbx
leaq -0x1b0(%rbp), %rdi
callq 0x20b0
jmp 0x2fc8
jmp 0x2fc5
jmp 0x2fc5
jmp 0x2fc5
jmp 0x2fc5
jmp 0x2fc5
movq %rax, %rbx
leaq -0xc8(%rbp), %rdi
callq 0x20b0
leaq -0x70(%rbp), %rdi
callq 0x3122
leaq -0xe0(%rbp), %rdi
callq 0x315c
movq %rbx, %rdi
callq 0x2220
nop