// File: /workspace/github_c_makefile_star2_O3/dwelch67[P]lc3/readhex.c
// Function: readhex
int readhex ( FILE *fp )
{

    char gstring[80];
    char newline[1024];
    unsigned char hexline[1024];

#define RAMMASK 0xFFFF


    //unsigned int errors;

    //unsigned int addhigh;
    unsigned int add;
    unsigned int data;

    unsigned int ra;
    //unsigned int rb;

    //unsigned int pages;
    //unsigned int page;

    unsigned int line;

    unsigned char checksum;

    unsigned int len;
    unsigned int hexlen;
    unsigned int maxadd;

    unsigned char t;

    maxadd=0;

    //addhigh=0;

    line=0;
    while(fgets(newline,sizeof(newline)-1,fp))
    {
        line++;
        //printf("%s",newline);
        if(newline[0]!=':')
        {
            printf("Syntax error <%u> no colon\n",line);
            continue;
        }
        gstring[0]=newline[1];
        gstring[1]=newline[2];
        gstring[2]=0;
        len=strtoul(gstring,NULL,16);
        for(ra=0;ra<(len+5);ra++)
        {
            gstring[0]=newline[(ra<<1)+1];
            gstring[1]=newline[(ra<<1)+2];
            gstring[2]=0;
            hexline[ra]=(unsigned char)strtoul(gstring,NULL,16);
        }
        checksum=0;
        for(ra=0;ra<(len+5);ra++) checksum+=hexline[ra];
        //checksum&=0xFF;
        if(checksum)
        {
            printf("checksum error <%u>\n",line);
        }
        add=hexline[1]; add<<=8;
        add|=hexline[2];
        //add|=addhigh;
        if(add>RAMMASK)
        {
            printf("address too big 0x%08X\n",add);
            //return(1);
            continue;
        }
        t=hexline[3];
        if(t!=0x02)
        {
            if(len&1)
            {
                printf("bad length\n");
                return(1);
            }
        }

        //:0011223344
        //;llaaaattdddddd
        //01234567890
        switch(t)
        {
            case 0x00:
            {
                for(ra=0;ra<len;ra+=4)
                {
                    if(add>RAMMASK)
                    {
                        printf("address too big 0x%08X\n",add);
                        break;
                    }
                    load_word(add++,hexline[ra+4+0],hexline[ra+4+1]);
                    if(add>maxadd) maxadd=add;
                }
                break;
            }
            case 0x01:
            {
//                printf("End of data\n");
                break;
            }
            default:
            {
                printf("UNKNOWN type %02X <%u>\n",t,line);
                break;
            }
        }
        if(t==0x01) break; //end of data
    }

    //printf("%u lines processed\n",line);
    //printf("%08X\n",maxadd);

    //for(ra=0;ra<maxadd;ra+=4)
    //{
        //printf("0x%08X: 0x%08X\n",ra,ram[ra>>2]);
    //}

    return(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x868, %rsp            # imm = 0x868
movq %rdi, %rbx
leaq -0x890(%rbp), %r14
leaq 0x938(%rip), %r12       # 0x2191
xorl %r13d, %r13d
movq %r14, %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %rbx, %rdx
callq 0x1080
testq %rax, %rax
je 0x1a3d
incl %r13d
cmpb $0x3a, -0x890(%rbp)
je 0x18ae
xorl %r15d, %r15d
movq %r12, %rdi
movl %r13d, %esi
xorl %eax, %eax
callq 0x1060
movq %r14, %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %rbx, %rdx
callq 0x1080
incl %r13d
testq %rax, %rax
jne 0x1878
jmp 0x1a54
movq %rbx, -0x40(%rbp)
movzwl -0x88f(%rbp), %eax
movw %ax, -0x90(%rbp)
movb $0x0, -0x8e(%rbp)
leaq -0x90(%rbp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x10b0
movq %rax, %r14
addl $0x5, %eax
je 0x1981
movq %r14, -0x30(%rbp)
movl %eax, -0x34(%rbp)
movl %eax, %r15d
movl $0x1, %r12d
xorl %r14d, %r14d
movl %r12d, %eax
movb -0x890(%rbp,%rax), %al
movb %al, -0x90(%rbp)
leal 0x1(%r12), %eax
movb -0x890(%rbp,%rax), %al
movb %al, -0x8f(%rbp)
movb $0x0, -0x8e(%rbp)
movq %rbx, %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x10b0
movb %al, -0x490(%rbp,%r14)
incq %r14
addl $0x2, %r12d
cmpq %r14, %r15
jne 0x18fc
cmpl $0x0, -0x34(%rbp)
leaq 0x83e(%rip), %r12       # 0x2191
movq -0x30(%rbp), %r14
je 0x1981
xorl %eax, %eax
xorl %ecx, %ecx
addb -0x490(%rbp,%rax), %cl
incq %rax
cmpq %rax, %r15
jne 0x195d
testb %cl, %cl
je 0x1981
leaq 0x836(%rip), %rdi       # 0x21ad
movl %r13d, %esi
xorl %eax, %eax
callq 0x1060
movzbl -0x48d(%rbp), %esi
cmpl $0x2, %esi
sete %al
testb $0x1, %r14b
sete %cl
orb %al, %cl
je 0x1a42
testl %esi, %esi
movl %esi, -0x30(%rbp)
je 0x19c4
cmpl $0x1, %esi
movq -0x40(%rbp), %rbx
je 0x1a3d
leaq 0x822(%rip), %rdi       # 0x21da
movl %r13d, %edx
xorl %eax, %eax
callq 0x1060
jmp 0x1a2c
testl %r14d, %r14d
movq -0x40(%rbp), %rbx
je 0x1a2c
movzwl -0x48f(%rbp), %eax
rolw $0x8, %ax
movzwl %ax, %edi
movl %r14d, %r14d
xorl %eax, %eax
cmpl $0x10000, %edi          # imm = 0x10000
je 0x1a19
leal 0x1(%rdi), %r12d
leaq 0x4(%rax), %r15
movzbl -0x48c(%rbp,%rax), %esi
movzbl -0x48b(%rbp,%rax), %edx
callq 0x122c
movq %r15, %rax
movl %r12d, %edi
leaq 0x77f(%rip), %r12       # 0x2191
cmpq %r14, %r15
jb 0x19e0
jmp 0x1a2c
leaq 0x7a2(%rip), %rdi       # 0x21c2
movl $0x10000, %esi          # imm = 0x10000
xorl %eax, %eax
callq 0x1060
cmpl $0x1, -0x30(%rbp)
leaq -0x890(%rbp), %r14
jne 0x185c
xorl %r15d, %r15d
jmp 0x1a54
leaq 0x7a9(%rip), %rdi       # 0x21f2
callq 0x1040
movl $0x1, %r15d
movl %r15d, %eax
addq $0x868, %rsp            # imm = 0x868
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq