// File: /workspace/github_c_makefile_star2_O1/hamsternz[P]emulate-risc-v/memorymap.c
// Function: memorymap_initialise
int memorymap_initialise(char *image) {
  struct region *r;
  if(!add_region(0x20400000, 118476, ROM_init, ROM_get, ROM_set, ROM_free, ROM_dump)) {
    display_log("Unable to add regions");
    return 0;
  }

  if(!add_region(0x80000000, 0x4000, RAM_init, RAM_get, RAM_set, RAM_free, RAM_dump)) {
    display_log("Unable to add regions");
    return 0;
  }
 
  // AON
  if(!add_region(0x10000000, 0x0170, RAM_init, RAM_get, RAM_set, RAM_free, RAM_dump)) {
    display_log("Unable to add regions");
    return 0;
  }

  // PRCI
  if(!add_region(0x10008000, 0x0FFF, PRCI_init, PRCI_get, PRCI_set, PRCI_free, PRCI_dump)) {
    display_log("Unable to add regions");
    return 0;
  }

  // GPIO 
  if(!add_region(0x10012000, 0x0FFF, GPIO_init, GPIO_get, GPIO_set, GPIO_free, GPIO_dump)) {
    display_log("Unable to add regions");
    return 0;
  }
  
  // UART 
  if(!add_region(0x10013000, 0x0FFF, UART_init, UART_get, UART_set, UART_free, UART_dump)) {
    display_log("Unable to add regions");
    return 0;
  }
  
  // SPI  
  if(!add_region(0x10014000, 0x0080, SPI_init, SPI_get, SPI_set, SPI_free, SPI_dump)) {
    display_log("Unable to add regions");
    return 0;
  }
  

  if(!add_region(0x02000000, 0x10000, CLINT_init, CLINT_get, CLINT_set, CLINT_free, CLINT_dump)) {
    display_log("Unable to add regions");
    return 0;
  }

  r = first_region;
  while(r != NULL) {
    if(!r->init(r)) {
       fprintf(stderr,"Unable to initialize region 0x%08x\n", r->base);
       return 0;
    }
    r = r->next;
  } 
  display_log("Memory map initialized");
  return 1;
}

// Assembly:
pushq %rbx
subq $0x10, %rsp
leaq 0x1e18(%rip), %rax      # 0x5663
movq %rax, (%rsp)
leaq 0x1b1a(%rip), %rdx      # 0x5370
leaq 0x1da9(%rip), %rcx      # 0x5606
leaq 0x1d3b(%rip), %r8       # 0x559f
leaq 0x1ea0(%rip), %r9       # 0x570b
movl $0x20400000, %edi       # imm = 0x20400000
movl $0x1cecc, %esi          # imm = 0x1CECC
callq 0x3a6f
testl %eax, %eax
je 0x3a48
leaq 0x996(%rip), %rbx       # 0x421f
movq %rbx, (%rsp)
leaq 0x660(%rip), %rdx       # 0x3ef4
leaq 0x927(%rip), %rcx       # 0x41c2
leaq 0x855(%rip), %r8        # 0x40f7
leaq 0xa1e(%rip), %r9        # 0x42c7
movl $0x80000000, %edi       # imm = 0x80000000
movl $0x4000, %esi           # imm = 0x4000
callq 0x3a6f
testl %eax, %eax
je 0x3a48
movq %rbx, (%rsp)
leaq 0x629(%rip), %rdx       # 0x3ef4
leaq 0x8f0(%rip), %rcx       # 0x41c2
leaq 0x81e(%rip), %r8        # 0x40f7
leaq 0x9e7(%rip), %r9        # 0x42c7
movl $0x10000000, %edi       # imm = 0x10000000
movl $0x170, %esi            # imm = 0x170
callq 0x3a6f
testl %eax, %eax
je 0x3a48
leaq 0x1989(%rip), %rax      # 0x5287
movq %rax, (%rsp)
leaq 0x179b(%rip), %rdx      # 0x50a4
leaq 0x18e5(%rip), %rcx      # 0x51f5
leaq 0x17e6(%rip), %r8       # 0x50fd
leaq 0x1a11(%rip), %r9       # 0x532f
movl $0x10008000, %edi       # imm = 0x10008000
movl $0xfff, %esi            # imm = 0xFFF
callq 0x3a6f
testl %eax, %eax
je 0x3a48
leaq 0x25e4(%rip), %rax      # 0x5f20
movq %rax, (%rsp)
leaq 0x2405(%rip), %rdx      # 0x5d4c
leaq 0x254f(%rip), %rcx      # 0x5e9d
leaq 0x2450(%rip), %r8       # 0x5da5
leaq 0x266c(%rip), %r9       # 0x5fc8
movl $0x10012000, %edi       # imm = 0x10012000
movl $0xfff, %esi            # imm = 0xFFF
callq 0x3a6f
testl %eax, %eax
je 0x3a48
leaq 0xede(%rip), %rax       # 0x4858
movq %rax, (%rsp)
leaq 0x983(%rip), %rdx       # 0x4308
leaq 0xc85(%rip), %rcx       # 0x4611
leaq 0x9d0(%rip), %r8        # 0x4363
leaq 0xed2(%rip), %r9        # 0x486c
movl $0x10013000, %edi       # imm = 0x10013000
movl $0xfff, %esi            # imm = 0xFFF
callq 0x3a6f
testl %eax, %eax
je 0x3a48
leaq 0x1f75(%rip), %rax      # 0x592d
movq %rax, (%rsp)
leaq 0x1d89(%rip), %rdx      # 0x574c
leaq 0x1ed3(%rip), %rcx      # 0x589d
leaq 0x1dd4(%rip), %r8       # 0x57a5
leaq 0x1ffd(%rip), %r9       # 0x59d5
movl $0x10014000, %edi       # imm = 0x10014000
movl $0x80, %esi
callq 0x3a6f
testl %eax, %eax
je 0x3a48
leaq 0x2272(%rip), %rax      # 0x5c64
movq %rax, (%rsp)
leaq 0x2017(%rip), %rdx      # 0x5a14
leaq 0x2161(%rip), %rcx      # 0x5b65
leaq 0x2062(%rip), %r8       # 0x5a6d
leaq 0x22fa(%rip), %r9       # 0x5d0c
movl $0x2000000, %edi        # imm = 0x2000000
movl $0x10000, %esi          # imm = 0x10000
callq 0x3a6f
testl %eax, %eax
je 0x3a48
leaq 0xd0cc(%rip), %rbx      # 0x10af8
movq (%rbx), %rbx
testq %rbx, %rbx
je 0x3a5c
movq %rbx, %rdi
callq *0x10(%rbx)
testl %eax, %eax
jne 0x3a2c
movq %rbx, %rdi
callq 0x225c
jmp 0x3a54
leaq 0x4477(%rip), %rdi      # 0x7ec6
callq 0x4d86
xorl %eax, %eax
addq $0x10, %rsp
popq %rbx
retq
leaq 0x449d(%rip), %rdi      # 0x7f00
callq 0x4d86
movl $0x1, %eax
jmp 0x3a56