// File: /workspace/github_c_makefile_star2_O1/hamsternz[P]emulate-risc-v/memory.c
// Function: memory_run
int  memory_run(void) {
  if( write_request_fifo.count > 0) {
    uint32_t addr, data;
    uint32_t mask;

    addr = write_request_fifo.address[write_request_fifo.read_ptr];
    mask = write_request_fifo.mask[write_request_fifo.read_ptr];
    data = write_request_fifo.data[write_request_fifo.read_ptr];

    write_request_fifo.read_ptr = (write_request_fifo.read_ptr == FIFO_SIZE-1) ? 0 : write_request_fifo.read_ptr+1;
    write_request_fifo.count--;

    return memorymap_write(addr, mask, data);
  }

  /* Process the read request queue */
  if( read_request_fifo.count > 0 && read_data_fifo.count < FIFO_SIZE) {
    uint32_t data, addr;
    /* Pull the address */
    addr = read_request_fifo.address[read_request_fifo.read_ptr];
    read_request_fifo.count--;
    read_request_fifo.read_ptr = (read_request_fifo.read_ptr == FIFO_SIZE-1) ? 0 : read_request_fifo.read_ptr+1;

    if(!memorymap_read(addr, 4, &data)) {
      data = 0;
    }
    /*Push the data */
    read_data_fifo.data[read_data_fifo.write_ptr] = data;
    read_data_fifo.count++;
    read_data_fifo.write_ptr = (read_data_fifo.write_ptr == FIFO_SIZE-1) ? 0 : read_data_fifo.write_ptr+1;

    return 1;
  }

  /* Process the fetch request queue */
  if( fetch_request_fifo.count > 0 && fetch_data_fifo.count < FIFO_SIZE) {
    uint32_t addr, data;

    addr = fetch_request_fifo.address[fetch_request_fifo.read_ptr];
    fetch_request_fifo.count--;
    fetch_request_fifo.read_ptr = (fetch_request_fifo.read_ptr == FIFO_SIZE-1) ? 0 : fetch_request_fifo.read_ptr+1;

    if(!memorymap_read( addr, 4, &data)) {
      data = 0;
    }

    fetch_data_fifo.data[fetch_data_fifo.write_ptr] = data;
    fetch_data_fifo.count++;
    fetch_data_fifo.write_ptr = (fetch_data_fifo.write_ptr == FIFO_SIZE-1) ? 0 : fetch_data_fifo.write_ptr+1; 

    return 1;
  }
  return 1;
}

// Assembly:
movl 0xaab9(%rip), %eax      # 0x10cdc
testl %eax, %eax
je 0x6263
movl 0xaaa7(%rip), %ecx      # 0x10cd4
leaq 0xaaa0(%rip), %rdx      # 0x10cd4
leal 0x1(%rcx), %r8d
xorl %r9d, %r9d
cmpq $0x7, %rcx
movl 0xc(%rdx,%rcx,4), %edi
movl 0x2c(%rdx,%rcx,4), %esi
movl 0x4c(%rdx,%rcx,4), %edx
cmovnel %r8d, %r9d
movl %r9d, 0xaa7e(%rip)      # 0x10cd4
decl %eax
movl %eax, 0xaa7e(%rip)      # 0x10cdc
jmp 0x3c63
pushq %rbx
subq $0x10, %rsp
movl 0xaada(%rip), %eax      # 0x10d48
testl %eax, %eax
je 0x62f1
cmpl $0x7, 0xaa0b(%rip)      # 0x10c84
ja 0x62f1
movl 0xaabf(%rip), %ecx      # 0x10d40
leaq 0xaab8(%rip), %rdx      # 0x10d40
decl %eax
leal 0x1(%rcx), %esi
xorl %ebx, %ebx
cmpq $0x7, %rcx
movl 0xc(%rdx,%rcx,4), %edi
movl %eax, 0xaaab(%rip)      # 0x10d48
cmovel %ebx, %esi
movl %esi, 0xaa9a(%rip)      # 0x10d40
leaq 0xc(%rsp), %rdx
movl $0x4, %esi
callq 0x3b0e
testl %eax, %eax
jne 0x62c1
movl $0x0, 0xc(%rsp)
movl 0xc(%rsp), %eax
movl 0xa9b5(%rip), %ecx      # 0x10c80
leaq 0xa9aa(%rip), %rdx      # 0x10c7c
movl %eax, 0xc(%rdx,%rcx,4)
incl 0xa9a8(%rip)            # 0x10c84
leal 0x1(%rcx), %eax
cmpq $0x7, %rcx
cmovel %ebx, %eax
movl %eax, 0xa994(%rip)      # 0x10c80
jmp 0x6375
movl 0xaa7d(%rip), %eax      # 0x10d74
testl %eax, %eax
je 0x6375
cmpl $0x7, 0xa9ae(%rip)      # 0x10cb0
ja 0x6375
movl 0xaa62(%rip), %ecx      # 0x10d6c
leaq 0xaa5b(%rip), %rdx      # 0x10d6c
decl %eax
leal 0x1(%rcx), %esi
xorl %ebx, %ebx
cmpq $0x7, %rcx
movl 0xc(%rdx,%rcx,4), %edi
movl %eax, 0xaa4e(%rip)      # 0x10d74
cmovel %ebx, %esi
movl %esi, 0xaa3d(%rip)      # 0x10d6c
leaq 0x8(%rsp), %rdx
movl $0x4, %esi
callq 0x3b0e
testl %eax, %eax
jne 0x634a
movl $0x0, 0x8(%rsp)
movl 0x8(%rsp), %eax
movl 0xa958(%rip), %ecx      # 0x10cac
leaq 0xa94d(%rip), %rdx      # 0x10ca8
movl %eax, 0xc(%rdx,%rcx,4)
incl 0xa94b(%rip)            # 0x10cb0
leal 0x1(%rcx), %eax
cmpq $0x7, %rcx
cmovel %ebx, %eax
movl %eax, 0xa937(%rip)      # 0x10cac
movl $0x1, %eax
addq $0x10, %rsp
popq %rbx
retq