// File: /workspace/github_c_makefile_star2_O2/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 0xadaa(%rip), %eax      # 0x10ccc
testl %eax, %eax
je 0x5f62
movl 0xad98(%rip), %ecx      # 0x10cc4
leaq 0xad91(%rip), %rdx      # 0x10cc4
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, 0xad6f(%rip)      # 0x10cc4
decl %eax
movl %eax, 0xad6f(%rip)      # 0x10ccc
jmp 0x3a97
pushq %rbx
subq $0x10, %rsp
movl 0xadcb(%rip), %eax      # 0x10d38
testl %eax, %eax
je 0x5fe3
cmpl $0x7, 0xacfc(%rip)      # 0x10c74
ja 0x5fe3
movl 0xadb0(%rip), %ecx      # 0x10d30
leaq 0xada9(%rip), %rdx      # 0x10d30
decl %eax
leal 0x1(%rcx), %esi
xorl %ebx, %ebx
cmpq $0x7, %rcx
movl 0xc(%rdx,%rcx,4), %edi
movl %eax, 0xad9c(%rip)      # 0x10d38
cmovel %ebx, %esi
movl %esi, 0xad8b(%rip)      # 0x10d30
pushq $0x4
popq %rsi
leaq 0xc(%rsp), %rdx
callq 0x395a
testl %eax, %eax
je 0x5fba
movl 0xc(%rsp), %eax
movl 0xacb0(%rip), %ecx      # 0x10c70
leaq 0xaca5(%rip), %rdx      # 0x10c6c
movl %eax, 0xc(%rdx,%rcx,4)
incl 0xaca3(%rip)            # 0x10c74
leal 0x1(%rcx), %eax
cmpq $0x7, %rcx
cmovel %ebx, %eax
movl %eax, 0xac8f(%rip)      # 0x10c70
jmp 0x605d
movl 0xad7b(%rip), %eax      # 0x10d64
testl %eax, %eax
je 0x605d
cmpl $0x7, 0xacac(%rip)      # 0x10ca0
ja 0x605d
movl 0xad60(%rip), %ecx      # 0x10d5c
leaq 0xad59(%rip), %rdx      # 0x10d5c
decl %eax
leal 0x1(%rcx), %esi
xorl %ebx, %ebx
cmpq $0x7, %rcx
movl 0xc(%rdx,%rcx,4), %edi
movl %eax, 0xad4c(%rip)      # 0x10d64
cmovel %ebx, %esi
movl %esi, 0xad3b(%rip)      # 0x10d5c
pushq $0x4
popq %rsi
leaq 0x8(%rsp), %rdx
callq 0x395a
testl %eax, %eax
je 0x6036
movl 0x8(%rsp), %eax
movl 0xac60(%rip), %ecx      # 0x10c9c
leaq 0xac55(%rip), %rdx      # 0x10c98
movl %eax, 0xc(%rdx,%rcx,4)
incl 0xac53(%rip)            # 0x10ca0
leal 0x1(%rcx), %eax
cmpq $0x7, %rcx
cmovel %ebx, %eax
movl %eax, 0xac3f(%rip)      # 0x10c9c
pushq $0x1
popq %rax
addq $0x10, %rsp
popq %rbx
retq