// File: /workspace/github_c_makefile_star2_O0/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
subq $0x890, %rsp            # imm = 0x890
movq %rdi, -0x10(%rbp)
movl $0x0, -0x880(%rbp)
movl $0x0, -0x870(%rbp)
leaq -0x460(%rbp), %rdi
movq -0x10(%rbp), %rdx
movl $0x3ff, %esi            # imm = 0x3FF
callq 0x1060
cmpq $0x0, %rax
je 0x1f12
movl -0x870(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x870(%rbp)
movsbl -0x460(%rbp), %eax
cmpl $0x3a, %eax
je 0x1c53
movl -0x870(%rbp), %esi
leaq 0x50d(%rip), %rdi       # 0x2157
movb $0x0, %al
callq 0x1040
jmp 0x1c03
movb -0x45f(%rbp), %al
movb %al, -0x60(%rbp)
movb -0x45e(%rbp), %al
movb %al, -0x5f(%rbp)
movb $0x0, -0x5e(%rbp)
leaq -0x60(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x10, %edx
callq 0x1090
movl %eax, -0x878(%rbp)
movl $0x0, -0x86c(%rbp)
movl -0x86c(%rbp), %eax
movl -0x878(%rbp), %ecx
addl $0x5, %ecx
cmpl %ecx, %eax
jae 0x1d02
movl -0x86c(%rbp), %eax
shll %eax
addl $0x1, %eax
movl %eax, %eax
movb -0x460(%rbp,%rax), %al
movb %al, -0x60(%rbp)
movl -0x86c(%rbp), %eax
shll %eax
addl $0x2, %eax
movl %eax, %eax
movb -0x460(%rbp,%rax), %al
movb %al, -0x5f(%rbp)
movb $0x0, -0x5e(%rbp)
leaq -0x60(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
movl $0x10, %edx
callq 0x1090
movb %al, %cl
movl -0x86c(%rbp), %eax
movb %cl, -0x860(%rbp,%rax)
movl -0x86c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x86c(%rbp)
jmp 0x1c8b
movb $0x0, -0x871(%rbp)
movl $0x0, -0x86c(%rbp)
movl -0x86c(%rbp), %eax
movl -0x878(%rbp), %ecx
addl $0x5, %ecx
cmpl %ecx, %eax
jae 0x1d54
movl -0x86c(%rbp), %eax
movzbl -0x860(%rbp,%rax), %ecx
movzbl -0x871(%rbp), %eax
addl %ecx, %eax
movb %al, -0x871(%rbp)
movl -0x86c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x86c(%rbp)
jmp 0x1d13
cmpb $0x0, -0x871(%rbp)
je 0x1d71
movl -0x870(%rbp), %esi
leaq 0x409(%rip), %rdi       # 0x2173
movb $0x0, %al
callq 0x1040
movzbl -0x85f(%rbp), %eax
movl %eax, -0x864(%rbp)
movl -0x864(%rbp), %eax
shll $0x8, %eax
movl %eax, -0x864(%rbp)
movzbl -0x85e(%rbp), %eax
orl -0x864(%rbp), %eax
movl %eax, -0x864(%rbp)
cmpl $0xffff, -0x864(%rbp)   # imm = 0xFFFF
jbe 0x1dc5
movl -0x864(%rbp), %esi
leaq 0x3cf(%rip), %rdi       # 0x2188
movb $0x0, %al
callq 0x1040
jmp 0x1c03
movb -0x85d(%rbp), %al
movb %al, -0x881(%rbp)
movzbl -0x881(%rbp), %eax
cmpl $0x2, %eax
je 0x1e07
movl -0x878(%rbp), %eax
andl $0x1, %eax
cmpl $0x0, %eax
je 0x1e05
leaq 0x3ae(%rip), %rdi       # 0x21a0
movb $0x0, %al
callq 0x1040
movl $0x1, -0x4(%rbp)
jmp 0x1f19
jmp 0x1e07
movzbl -0x881(%rbp), %eax
movl %eax, -0x888(%rbp)
testl %eax, %eax
je 0x1e2e
jmp 0x1e1a
movl -0x888(%rbp), %eax
subl $0x1, %eax
je 0x1ee2
jmp 0x1ee4
movl $0x0, -0x86c(%rbp)
movl -0x86c(%rbp), %eax
cmpl -0x878(%rbp), %eax
jae 0x1ee0
cmpl $0xffff, -0x864(%rbp)   # imm = 0xFFFF
jbe 0x1e6c
movl -0x864(%rbp), %esi
leaq 0x325(%rip), %rdi       # 0x2188
movb $0x0, %al
callq 0x1040
jmp 0x1ee0
movl -0x864(%rbp), %edi
movl %edi, %eax
addl $0x1, %eax
movl %eax, -0x864(%rbp)
movl -0x86c(%rbp), %eax
addl $0x4, %eax
addl $0x0, %eax
movl %eax, %eax
movb -0x860(%rbp,%rax), %cl
movl -0x86c(%rbp), %eax
addl $0x4, %eax
addl $0x1, %eax
movl %eax, %eax
movzbl %cl, %esi
movzbl -0x860(%rbp,%rax), %edx
callq 0x11a0
movl -0x864(%rbp), %eax
cmpl -0x880(%rbp), %eax
jbe 0x1eca
movl -0x864(%rbp), %eax
movl %eax, -0x880(%rbp)
jmp 0x1ecc
movl -0x86c(%rbp), %eax
addl $0x4, %eax
movl %eax, -0x86c(%rbp)
jmp 0x1e38
jmp 0x1eff
jmp 0x1eff
movzbl -0x881(%rbp), %esi
movl -0x870(%rbp), %edx
leaq 0x2b4(%rip), %rdi       # 0x21ac
movb $0x0, %al
callq 0x1040
movzbl -0x881(%rbp), %eax
cmpl $0x1, %eax
jne 0x1f0d
jmp 0x1f12
jmp 0x1c03
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x890, %rsp            # imm = 0x890
popq %rbp
retq