// File: /workspace/github_c_makefile_star2_O3/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 0xab69(%rip), %eax      # 0x10cdc
testl %eax, %eax
je 0x61b3
movl 0xab57(%rip), %ecx      # 0x10cd4
leaq 0xab50(%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, 0xab2e(%rip)      # 0x10cd4
decl %eax
movl %eax, 0xab2e(%rip)      # 0x10cdc
jmp 0x3c48
pushq %rbx
subq $0x10, %rsp
movl 0xab8a(%rip), %eax      # 0x10d48
testl %eax, %eax
je 0x6236
cmpl $0x7, 0xaabb(%rip)      # 0x10c84
ja 0x6236
movl 0xab6f(%rip), %ecx      # 0x10d40
leaq 0xab68(%rip), %rdx      # 0x10d40
decl %eax
leal 0x1(%rcx), %esi
xorl %ebx, %ebx
cmpq $0x7, %rcx
movl 0xc(%rdx,%rcx,4), %edi
movl %eax, 0xab5b(%rip)      # 0x10d48
cmovel %ebx, %esi
movl %esi, 0xab4a(%rip)      # 0x10d40
leaq 0xc(%rsp), %rdx
movl $0x4, %esi
callq 0x3af9
testl %eax, %eax
je 0x620d
movl 0xc(%rsp), %eax
movl 0xaa6d(%rip), %ecx      # 0x10c80
leaq 0xaa62(%rip), %rdx      # 0x10c7c
movl %eax, 0xc(%rdx,%rcx,4)
incl 0xaa60(%rip)            # 0x10c84
leal 0x1(%rcx), %eax
cmpq $0x7, %rcx
cmovel %ebx, %eax
movl %eax, 0xaa4c(%rip)      # 0x10c80
jmp 0x62b2
movl 0xab38(%rip), %eax      # 0x10d74
testl %eax, %eax
je 0x62b2
cmpl $0x7, 0xaa69(%rip)      # 0x10cb0
ja 0x62b2
movl 0xab1d(%rip), %ecx      # 0x10d6c
leaq 0xab16(%rip), %rdx      # 0x10d6c
decl %eax
leal 0x1(%rcx), %esi
xorl %ebx, %ebx
cmpq $0x7, %rcx
movl 0xc(%rdx,%rcx,4), %edi
movl %eax, 0xab09(%rip)      # 0x10d74
cmovel %ebx, %esi
movl %esi, 0xaaf8(%rip)      # 0x10d6c
leaq 0x8(%rsp), %rdx
movl $0x4, %esi
callq 0x3af9
testl %eax, %eax
je 0x628b
movl 0x8(%rsp), %eax
movl 0xaa1b(%rip), %ecx      # 0x10cac
leaq 0xaa10(%rip), %rdx      # 0x10ca8
movl %eax, 0xc(%rdx,%rcx,4)
incl 0xaa0e(%rip)            # 0x10cb0
leal 0x1(%rcx), %eax
cmpq $0x7, %rcx
cmovel %ebx, %eax
movl %eax, 0xa9fa(%rip)      # 0x10cac
movl $0x1, %eax
addq $0x10, %rsp
popq %rbx
retq