// File: /workspace/github_c_makefile_star2_O1/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 0x1a34
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 0x1a4b
movq %rbx, -0x38(%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 0x1978
movq %r14, -0x30(%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 0x18f9
xorl %eax, %eax
xorl %ecx, %ecx
addb -0x490(%rbp,%rax), %cl
incq %rax
cmpq %rax, %r15
jne 0x1949
testb %cl, %cl
leaq 0x830(%rip), %r12       # 0x2191
movq -0x30(%rbp), %r14
je 0x1978
leaq 0x83f(%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 0x1a39
testl %esi, %esi
movl %esi, -0x30(%rbp)
je 0x19bb
cmpl $0x1, %esi
movq -0x38(%rbp), %rbx
je 0x1a34
leaq 0x82b(%rip), %rdi       # 0x21da
movl %r13d, %edx
xorl %eax, %eax
callq 0x1060
jmp 0x1a23
testl %r14d, %r14d
movq -0x38(%rbp), %rbx
je 0x1a23
movzwl -0x48f(%rbp), %eax
rolw $0x8, %ax
movzwl %ax, %edi
movl %r14d, %r14d
xorl %eax, %eax
cmpl $0x10000, %edi          # imm = 0x10000
je 0x1a10
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 0x788(%rip), %r12       # 0x2191
cmpq %r14, %r15
jb 0x19d7
jmp 0x1a23
leaq 0x7ab(%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 0x1a4b
leaq 0x7b2(%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