// File: /workspace/github_c_makefile_star2_O0/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:
subq $0x38, %rsp
cmpl $0x0, 0x9c25(%rip)      # 0x12cd0
jbe 0x9147
movl 0x9c11(%rip), %eax      # 0x12cc8
movl %eax, %ecx
leaq 0x9c08(%rip), %rax      # 0x12cc8
movl 0xc(%rax,%rcx,4), %eax
movl %eax, 0x30(%rsp)
movl 0x9bfa(%rip), %eax      # 0x12cc8
movl %eax, %ecx
leaq 0x9bf1(%rip), %rax      # 0x12cc8
movl 0x2c(%rax,%rcx,4), %eax
movl %eax, 0x28(%rsp)
movl 0x9be3(%rip), %eax      # 0x12cc8
movl %eax, %ecx
leaq 0x9bda(%rip), %rax      # 0x12cc8
movl 0x4c(%rax,%rcx,4), %eax
movl %eax, 0x2c(%rsp)
cmpl $0x7, 0x9bcb(%rip)      # 0x12cc8
jne 0x9107
xorl %eax, %eax
movl %eax, 0x14(%rsp)
jmp 0x9114
movl 0x9bbb(%rip), %eax      # 0x12cc8
addl $0x1, %eax
movl %eax, 0x14(%rsp)
movl 0x14(%rsp), %eax
movl %eax, 0x9baa(%rip)      # 0x12cc8
movl 0x9bac(%rip), %eax      # 0x12cd0
addl $-0x1, %eax
movl %eax, 0x9ba3(%rip)      # 0x12cd0
movl 0x30(%rsp), %edi
movl 0x28(%rsp), %esi
movl 0x2c(%rsp), %edx
callq 0x4a00
movl %eax, 0x34(%rsp)
jmp 0x9312
cmpl $0x0, 0x9bee(%rip)      # 0x12d3c
jbe 0x922a
cmpl $0x8, 0x9b1d(%rip)      # 0x12c78
jae 0x922a
movl 0x9bcd(%rip), %eax      # 0x12d34
movl %eax, %ecx
leaq 0x9bc4(%rip), %rax      # 0x12d34
movl 0xc(%rax,%rcx,4), %eax
movl %eax, 0x20(%rsp)
movl 0x9bbe(%rip), %eax      # 0x12d3c
addl $-0x1, %eax
movl %eax, 0x9bb5(%rip)      # 0x12d3c
cmpl $0x7, 0x9ba6(%rip)      # 0x12d34
jne 0x9198
xorl %eax, %eax
movl %eax, 0x10(%rsp)
jmp 0x91a5
movl 0x9b96(%rip), %eax      # 0x12d34
addl $0x1, %eax
movl %eax, 0x10(%rsp)
movl 0x10(%rsp), %eax
movl %eax, 0x9b85(%rip)      # 0x12d34
movl 0x20(%rsp), %edi
movl $0x4, %esi
leaq 0x24(%rsp), %rdx
callq 0x4800
cmpl $0x0, %eax
jne 0x91cf
movl $0x0, 0x24(%rsp)
movl 0x24(%rsp), %edx
movl 0x9a9b(%rip), %eax      # 0x12c74
movl %eax, %ecx
leaq 0x9a8e(%rip), %rax      # 0x12c70
movl %edx, 0xc(%rax,%rcx,4)
movl 0x9a8c(%rip), %eax      # 0x12c78
addl $0x1, %eax
movl %eax, 0x9a83(%rip)      # 0x12c78
cmpl $0x7, 0x9a78(%rip)      # 0x12c74
jne 0x9206
xorl %eax, %eax
movl %eax, 0xc(%rsp)
jmp 0x9213
movl 0x9a68(%rip), %eax      # 0x12c74
addl $0x1, %eax
movl %eax, 0xc(%rsp)
movl 0xc(%rsp), %eax
movl %eax, 0x9a57(%rip)      # 0x12c74
movl $0x1, 0x34(%rsp)
jmp 0x9312
cmpl $0x0, 0x9b37(%rip)      # 0x12d68
jbe 0x930a
cmpl $0x8, 0x9a66(%rip)      # 0x12ca4
jae 0x930a
movl 0x9b16(%rip), %eax      # 0x12d60
movl %eax, %ecx
leaq 0x9b0d(%rip), %rax      # 0x12d60
movl 0xc(%rax,%rcx,4), %eax
movl %eax, 0x1c(%rsp)
movl 0x9b07(%rip), %eax      # 0x12d68
addl $-0x1, %eax
movl %eax, 0x9afe(%rip)      # 0x12d68
cmpl $0x7, 0x9aef(%rip)      # 0x12d60
jne 0x927b
xorl %eax, %eax
movl %eax, 0x8(%rsp)
jmp 0x9288
movl 0x9adf(%rip), %eax      # 0x12d60
addl $0x1, %eax
movl %eax, 0x8(%rsp)
movl 0x8(%rsp), %eax
movl %eax, 0x9ace(%rip)      # 0x12d60
movl 0x1c(%rsp), %edi
movl $0x4, %esi
leaq 0x18(%rsp), %rdx
callq 0x4800
cmpl $0x0, %eax
jne 0x92b2
movl $0x0, 0x18(%rsp)
movl 0x18(%rsp), %edx
movl 0x99e4(%rip), %eax      # 0x12ca0
movl %eax, %ecx
leaq 0x99d7(%rip), %rax      # 0x12c9c
movl %edx, 0xc(%rax,%rcx,4)
movl 0x99d5(%rip), %eax      # 0x12ca4
addl $0x1, %eax
movl %eax, 0x99cc(%rip)      # 0x12ca4
cmpl $0x7, 0x99c1(%rip)      # 0x12ca0
jne 0x92e9
xorl %eax, %eax
movl %eax, 0x4(%rsp)
jmp 0x92f6
movl 0x99b1(%rip), %eax      # 0x12ca0
addl $0x1, %eax
movl %eax, 0x4(%rsp)
movl 0x4(%rsp), %eax
movl %eax, 0x99a0(%rip)      # 0x12ca0
movl $0x1, 0x34(%rsp)
jmp 0x9312
movl $0x1, 0x34(%rsp)
movl 0x34(%rsp), %eax
addq $0x38, %rsp
retq
nopl (%rax,%rax)