// File: /workspace/github_c_makefile_star2_O2/dwelch67[P]lc3/lc3dis.c
// Function: main
int main ( int argc, char *argv[] )
{
    unsigned int ra;
    unsigned int addr;
    unsigned int inst;
    unsigned int rd,rs1,rs2,simm,dest;
    FILE *fp;

    if(argc<2)
    {
        fprintf(stderr,".hex file not specified\n");
        return(1);
    }

    memset(mem,0xFF,sizeof(mem));
    memset(mem_hit,0x00,sizeof(mem_hit));

    fp=fopen(argv[1],"rt");
    if(fp==NULL)
    {
        fprintf(stderr,"Error opening file [%s]\n",argv[1]);
        return(1);
    }
    if(readhex(fp)) return(1);
    fclose(fp);

    for(addr=0;addr<=RAMMASK;addr++)
    {
        if(mem_hit[addr])
        {
            inst=mem[addr];
            printf("0x%04X: 0x%04X ",addr,inst);
            switch(inst&0xF000)
            {
                case 0x0000:
                {
                    printf("br");
                    if(inst&0x0800) printf("n");
                    if(inst&0x0400) printf("z");
                    if(inst&0x0200) printf("p");
                    simm=sext9(inst);
                    dest=(addr+1+simm)&0xFFFF;
                    printf(" 0x%04X",dest);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0x1000:
                {
                    rd=(inst>>9)&7;
                    rs1=(inst>>6)&7;
                    if(inst&0x20)
                    {
                        simm=sext5(inst);
                        printf("add r%u,r%u,#0x%04X",rd,rs1,simm);
                    }
                    else
                    {
                        rs2=(inst>>0)&7;
                        printf("add r%u,r%u,r%u",rd,rs1,rs2);
                    }
                    break;
                }
                case 0x2000:
                {
                    rd=(inst>>9)&7;
                    simm=sext9(inst);
                    dest=(addr+1+simm)&0xFFFF;
                    printf("ld r%u,0x%04X",rd,dest);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0x3000:
                {
                    rd=(inst>>9)&7;
                    simm=sext9(inst);
                    dest=(addr+1+simm)&0xFFFF;
                    printf("st r%u,0x%04X",rd,dest);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0x4000:
                {
                    if(inst&0x0800)
                    {
                        simm=sext11(inst);
                        dest=(addr+1+simm)&0xFFFF;
                        printf("jsr 0x%04X",dest);
                        if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                        else            printf(" ; +%u",simm);
                    }
                    else
                    {
                        rd=(inst>>6)&7;
                        printf("jsrr r%u",rd);
                    }
                    break;
                }
                case 0x5000:
                {
                    rd=(inst>>9)&7;
                    rs1=(inst>>6)&7;
                    if(inst&0x20)
                    {
                        simm=sext5(inst);
                        printf("and r%u,r%u,#0x%04X",rd,rs1,simm);
                    }
                    else
                    {
                        rs2=(inst>>0)&7;
                        printf("and r%u,r%u,r%u",rd,rs1,rs2);
                    }
                    break;
                }
                case 0x6000:
                {
                    rd=(inst>>9)&7;
                    rs1=(inst>>6)&7;
                    simm=sext6(inst);
                    printf("ldr r%u,r%u,#0x%04X",rd,rs1,simm);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0x7000:
                {
                    rd=(inst>>9)&7;
                    rs1=(inst>>6)&7;
                    simm=sext6(inst);
                    printf("str r%u,r%u,#0x%04X",rd,rs1,simm);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0x8000:
                {
                    printf("rti");
                    break;
                }
                case 0x9000:
                {
                    rd=(inst>>9)&7;
                    rs1=(inst>>6)&7;
                    printf("not r%u,r%u",rd,rs1);
                    break;
                }
                case 0xA000:
                {
                    rd=(inst>>9)&7;
                    simm=sext9(inst);
                    dest=(addr+1+simm)&0xFFFF;
                    printf("ldi r%u,0x%04X",rd,dest);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0xB000:
                {
                    rd=(inst>>9)&7;
                    simm=sext9(inst);
                    dest=(addr+1+simm)&0xFFFF;
                    printf("sti r%u,0x%04X",rd,dest);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0xC000:
                {
                    printf("ret");
                    break;
                }
                case 0xD000:
                {
                    printf("undefined");
                    break;
                }
                case 0xE000:
                {
                    rd=(inst>>9)&7;
                    simm=sext9(inst);
                    dest=(addr+1+simm)&0xFFFF;
                    printf("lea r%u,0x%04X",rd,dest);
                    if(simm&0x8000) printf(" ; -%u",(simm^0xFFFF)+1);
                    else            printf(" ; +%u",simm);
                    break;
                }
                case 0xF000:
                {
                    rd=inst&0xFF;
                    printf("trap 0x%02X",rd);
                    break;
                }
            }


            printf("\n");
        }
    }



    return(0);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
pushq $0x1
popq %r15
cmpl $0x1, %edi
jle 0x1773
movq %rsi, %rbx
leaq 0x2e08(%rip), %r14      # 0x4090
movl $0x20000, %edx          # imm = 0x20000
movq %r14, %rdi
movl $0xff, %esi
callq 0x1070
leaq 0x22def(%rip), %rdi     # 0x24090
movl $0x10000, %edx          # imm = 0x10000
xorl %esi, %esi
callq 0x1070
movq 0x8(%rbx), %rdi
leaq 0xda5(%rip), %rsi       # 0x205d
callq 0x10a0
testq %rax, %rax
je 0x1791
movq %rax, %r12
movq %rax, %rdi
callq 0x17b0
testl %eax, %eax
jne 0x1761
movq %r12, %rdi
callq 0x1050
movq $-0x10000, %r13         # imm = 0xFFFF0000
leaq 0xd15(%rip), %r15       # 0x2004
leaq 0x22d9a(%rip), %r12     # 0x24090
testq %r13, %r13
je 0x175e
cmpb $0x0, 0x10000(%r13,%r12)
je 0x1752
movzwl (%r14), %ebx
leal 0x10000(%r13), %esi
leaq 0xd59(%rip), %rdi       # 0x2079
movl %ebx, %edx
xorl %eax, %eax
callq 0x1060
movl %ebx, %eax
shrl $0xc, %eax
movslq (%r15,%rax,4), %rax
addq %r15, %rax
jmpq *%rax
leaq 0xd4b(%rip), %rdi       # 0x2089
xorl %eax, %eax
callq 0x1060
btl $0xb, %ebx
jae 0x1353
pushq $0x6e
popq %rdi
callq 0x1030
btl $0xa, %ebx
jae 0x1361
pushq $0x7a
popq %rdi
callq 0x1030
btl $0x9, %ebx
jae 0x136f
pushq $0x70
popq %rdi
callq 0x1030
andl $0x1ff, %ebx            # imm = 0x1FF
cmpw $0x100, %bx             # imm = 0x100
movl $0xfe00, %r12d          # imm = 0xFE00
movl $0x0, %eax
cmovbl %eax, %r12d
orl %ebx, %r12d
leal (%r12,%r13), %eax
addl $0x10001, %eax          # imm = 0x10001
movzwl %ax, %esi
leaq 0xd3e(%rip), %rdi       # 0x20dd
xorl %eax, %eax
callq 0x1060
cmpw $0x100, %bx             # imm = 0x100
jae 0x16f8
leaq 0xcdb(%rip), %rdi       # 0x2093
movl %r12d, %esi
jmp 0x1682
movl %ebx, %esi
shrl $0x9, %esi
andl $0x7, %esi
andl $0x1ff, %ebx            # imm = 0x1FF
xorl %eax, %eax
cmpw $0x100, %bx             # imm = 0x100
setb %al
shll $0x9, %eax
leal (%rax,%rbx), %ecx
leal (%rax,%rbx), %r12d
addl $0xfffffe00, %r12d      # imm = 0xFFFFFE00
movl $0xffff, %eax           # imm = 0xFFFF
andl %eax, %r12d
leal (%rcx,%r13), %edx
addl $0xfe01, %edx           # imm = 0xFE01
andl %eax, %edx
leaq 0xd72(%rip), %rdi       # 0x2176
jmp 0x166a
movl %ebx, %esi
shrl $0x9, %esi
andl $0x7, %esi
andl $0x1ff, %ebx            # imm = 0x1FF
xorl %eax, %eax
cmpw $0x100, %bx             # imm = 0x100
setb %al
shll $0x9, %eax
leal (%rax,%rbx), %ecx
leal (%rax,%rbx), %r12d
addl $0xfffffe00, %r12d      # imm = 0xFFFFFE00
movl $0xffff, %eax           # imm = 0xFFFF
andl %eax, %r12d
leal (%rcx,%r13), %edx
addl $0xfe01, %edx           # imm = 0xFE01
andl %eax, %edx
leaq 0xd0c(%rip), %rdi       # 0x2159
jmp 0x166a
btl $0xb, %ebx
jb 0x16a8
shrl $0x6, %ebx
andl $0x7, %ebx
leaq 0xc7c(%rip), %rdi       # 0x20e5
movl %ebx, %esi
jmp 0x14f2
leaq 0xcf1(%rip), %rdi       # 0x2168
jmp 0x169c
movl %ebx, %esi
shrl $0x9, %esi
andl $0x7, %esi
shrl $0x6, %ebx
andl $0x7, %ebx
leaq 0xcad(%rip), %rdi       # 0x213e
movl %ebx, %edx
xorl %eax, %eax
callq 0x1060
jmp 0x174a
movl %ebx, %esi
shrl $0x9, %esi
andl $0x7, %esi
andl $0x1ff, %ebx            # imm = 0x1FF
xorl %eax, %eax
cmpw $0x100, %bx             # imm = 0x100
setb %al
shll $0x9, %eax
leal (%rax,%rbx), %ecx
leal (%rax,%rbx), %r12d
addl $0xfffffe00, %r12d      # imm = 0xFFFFFE00
movl $0xffff, %eax           # imm = 0xFFFF
andl %eax, %r12d
leal (%rcx,%r13), %edx
addl $0xfe01, %edx           # imm = 0xFE01
andl %eax, %edx
leaq 0xbdb(%rip), %rdi       # 0x20be
jmp 0x166a
movzbl %bl, %esi
leaq 0xc93(%rip), %rdi       # 0x2185
xorl %eax, %eax
callq 0x1060
jmp 0x174a
movl %ebx, %esi
shrl $0x9, %esi
andl $0x7, %esi
andl $0x1ff, %ebx            # imm = 0x1FF
xorl %eax, %eax
cmpw $0x100, %bx             # imm = 0x100
setb %al
shll $0x9, %eax
leal (%rax,%rbx), %ecx
leal (%rax,%rbx), %r12d
addl $0xfffffe00, %r12d      # imm = 0xFFFFFE00
movl $0xffff, %eax           # imm = 0xFFFF
andl %eax, %r12d
leal (%rcx,%r13), %edx
addl $0xfe01, %edx           # imm = 0xFE01
andl %eax, %edx
leaq 0xb8a(%rip), %rdi       # 0x20cc
jmp 0x166a
movl %ebx, %esi
shrl $0x9, %esi
andl $0x7, %esi
movl %ebx, %edx
shrl $0x6, %edx
andl $0x7, %edx
andl $0x3f, %ebx
xorl %eax, %eax
cmpw $0x20, %bx
setb %al
shll $0x6, %eax
addl %ebx, %eax
addl $-0x40, %eax
movzwl %ax, %r12d
leaq 0xbb0(%rip), %rdi       # 0x2126
jmp 0x1605
movl %ebx, %esi
shrl $0x9, %esi
andl $0x7, %esi
movl %ebx, %edx
shrl $0x6, %edx
andl $0x7, %edx
testb $0x20, %bl
jne 0x1705
andl $0x7, %ebx
leaq 0xb10(%rip), %rdi       # 0x20ae
jmp 0x15c3
movl %ebx, %esi
shrl $0x9, %esi
andl $0x7, %esi
movl %ebx, %edx
shrl $0x6, %edx
andl $0x7, %edx
testb $0x20, %bl
jne 0x1725
andl $0x7, %ebx
leaq 0xb3f(%rip), %rdi       # 0x2102
movl %ebx, %ecx
jmp 0x1743
leaq 0xb9b(%rip), %rdi       # 0x216c
jmp 0x169c
movl %ebx, %esi
shrl $0x9, %esi
andl $0x7, %esi
movl %ebx, %edx
shrl $0x6, %edx
andl $0x7, %edx
andl $0x3f, %ebx
xorl %eax, %eax
cmpw $0x20, %bx
setb %al
shll $0x6, %eax
addl %ebx, %eax
addl $-0x40, %eax
movzwl %ax, %r12d
leaq 0xb0d(%rip), %rdi       # 0x2112
movl %r12d, %ecx
xorl %eax, %eax
callq 0x1060
cmpw $0x20, %bx
jb 0x1678
movl $0x10000, %esi          # imm = 0x10000
subl %r12d, %esi
leaq 0xa68(%rip), %rdi       # 0x208c
jmp 0x1682
movl %ebx, %esi
shrl $0x9, %esi
andl $0x7, %esi
andl $0x1ff, %ebx            # imm = 0x1FF
xorl %eax, %eax
cmpw $0x100, %bx             # imm = 0x100
setb %al
shll $0x9, %eax
leal (%rax,%rbx), %ecx
leal (%rax,%rbx), %r12d
addl $0xfffffe00, %r12d      # imm = 0xFFFFFE00
movl $0xffff, %eax           # imm = 0xFFFF
andl %eax, %r12d
leal (%rcx,%r13), %edx
addl $0xfe01, %edx           # imm = 0xFE01
andl %eax, %edx
leaq 0xae0(%rip), %rdi       # 0x214a
xorl %eax, %eax
callq 0x1060
cmpw $0x100, %bx             # imm = 0x100
jae 0x1615
leaq 0xa14(%rip), %rdi       # 0x2093
movl %r12d, %esi
xorl %eax, %eax
callq 0x1060
leaq 0x22a00(%rip), %r12     # 0x24090
jmp 0x174a
leaq 0xa9e(%rip), %rdi       # 0x213a
xorl %eax, %eax
callq 0x1060
jmp 0x174a
andl $0x7ff, %ebx            # imm = 0x7FF
xorl %eax, %eax
cmpw $0x400, %bx             # imm = 0x400
setb %al
shll $0xb, %eax
leal (%rax,%rbx), %ecx
leal (%rax,%rbx), %r12d
addl $0xfffff800, %r12d      # imm = 0xFFFFF800
movl $0xffff, %eax           # imm = 0xFFFF
andl %eax, %r12d
leal (%rcx,%r13), %esi
addl $0xf801, %esi           # imm = 0xF801
andl %eax, %esi
leaq 0x9f6(%rip), %rdi       # 0x20da
xorl %eax, %eax
callq 0x1060
cmpw $0x400, %bx             # imm = 0x400
jae 0x1615
jmp 0x1678
movl $0x10000, %esi          # imm = 0x10000
subl %r12d, %esi
jmp 0x161d
andl $0x1f, %ebx
xorl %eax, %eax
cmpw $0x10, %bx
setb %al
shll $0x5, %eax
addl %ebx, %eax
addl $-0x20, %eax
movzwl %ax, %ecx
leaq 0x977(%rip), %rdi       # 0x209a
jmp 0x1743
andl $0x1f, %ebx
xorl %eax, %eax
cmpw $0x10, %bx
setb %al
shll $0x5, %eax
addl %ebx, %eax
addl $-0x20, %eax
movzwl %ax, %ecx
leaq 0x9ab(%rip), %rdi       # 0x20ee
xorl %eax, %eax
callq 0x1060
pushq $0xa
popq %rdi
callq 0x1030
incq %r13
addq $0x2, %r14
jmp 0x12f6
xorl %r15d, %r15d
movl %r15d, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x287e(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0x8c0(%rip), %rdi       # 0x2044
pushq $0x18
popq %rsi
pushq $0x1
popq %rdx
callq 0x10c0
jmp 0x1761
movq 0x2860(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
movq 0x8(%rbx), %rdx
leaq 0x8ba(%rip), %rsi       # 0x2060
xorl %eax, %eax
callq 0x1090
jmp 0x1761
nop