// File: /workspace/github_c_makefile_star2_O2/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 $0x878, %rsp            # imm = 0x878
movq %rdi, %rbx
leaq -0x8a0(%rbp), %r14
leaq -0xa0(%rbp), %r15
pushq $0x10
popq %r12
movl $0x0, -0x2c(%rbp)
movq %rdi, -0x40(%rbp)
movq %r14, %rdi
movl $0x3ff, %esi            # imm = 0x3FF
movq %rbx, %rdx
callq 0x1080
testq %rax, %rax
je 0x1982
movl -0x2c(%rbp), %esi
incl %esi
cmpb $0x3a, -0x8a0(%rbp)
movl %esi, -0x2c(%rbp)
jne 0x188d
movzwl -0x89f(%rbp), %eax
movw %ax, -0xa0(%rbp)
movb $0x0, -0x9e(%rbp)
movq %r15, %rdi
xorl %esi, %esi
movl %r12d, %edx
callq 0x10b0
movq %rax, -0x48(%rbp)
movl %eax, %ebx
addl $0x5, %ebx
pushq $0x1
popq %rax
movl %eax, %r14d
xorl %r13d, %r13d
cmpq %r13, %rbx
je 0x18a0
movl %r14d, %eax
movb -0x8a0(%rbp,%rax), %al
movb %al, -0xa0(%rbp)
leal 0x1(%r14), %eax
movb -0x8a0(%rbp,%rax), %al
movb %al, -0x9f(%rbp)
movb $0x0, -0x9e(%rbp)
movq %r15, %rdi
xorl %esi, %esi
movl %r12d, %edx
callq 0x10b0
movb %al, -0x4a0(%rbp,%r13)
incq %r13
addl $0x2, %r14d
jmp 0x1842
leaq 0x8fd(%rip), %rdi       # 0x2191
xorl %eax, %eax
callq 0x1060
jmp 0x17e4
xorl %eax, %eax
xorl %ecx, %ecx
cmpq %rax, %rbx
je 0x18b5
addb -0x4a0(%rbp,%rax), %cl
incq %rax
jmp 0x18a4
testb %cl, %cl
je 0x18ca
leaq 0x8ed(%rip), %rdi       # 0x21ad
movl -0x2c(%rbp), %esi
xorl %eax, %eax
callq 0x1060
movzbl -0x49d(%rbp), %esi
cmpl $0x2, %esi
sete %al
movq -0x48(%rbp), %rdx
testb $0x1, %dl
sete %cl
orb %al, %cl
je 0x198b
movl $0x0, -0x30(%rbp)
testl %esi, %esi
movl %esi, -0x34(%rbp)
je 0x1913
cmpl $0x1, %esi
je 0x199d
leaq 0x8d3(%rip), %rdi       # 0x21da
movl -0x2c(%rbp), %edx
xorl %eax, %eax
callq 0x1060
jmp 0x196b
movzwl -0x49f(%rbp), %eax
rolw $0x8, %ax
movzwl %ax, %edi
movl %edx, %r14d
xorl %eax, %eax
cmpq %r14, %rax
jae 0x196b
cmpl $0x10000, %edi          # imm = 0x10000
jae 0x1958
leal 0x1(%rdi), %r13d
leaq 0x4(%rax), %rbx
movzbl -0x49c(%rbp,%rax), %esi
movzbl -0x49b(%rbp,%rax), %edx
callq 0x11cc
movq %rbx, %rax
movl %r13d, %edi
jmp 0x1926
leaq 0x863(%rip), %rdi       # 0x21c2
movl $0x10000, %esi          # imm = 0x10000
xorl %eax, %eax
callq 0x1060
cmpl $0x1, -0x34(%rbp)
movq -0x40(%rbp), %rbx
leaq -0x8a0(%rbp), %r14
jne 0x17e4
jmp 0x199d
movl $0x0, -0x30(%rbp)
jmp 0x199d
leaq 0x860(%rip), %rdi       # 0x21f2
callq 0x1040
pushq $0x1
popq %rax
movl %eax, -0x30(%rbp)
movl -0x30(%rbp), %eax
addq $0x878, %rsp            # imm = 0x878
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq