Skip to content

Commit

Permalink
Elf.scala sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Dolu1990 committed Aug 23, 2024
1 parent 1cc2b23 commit 5e02bf7
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/main/scala/spinal/lib/misc/Elf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package spinal.lib.misc

import net.fornwall.jelf.{ElfFile, ElfSection, ElfSectionHeader, ElfSymbol, ElfSymbolTableSection}
import spinal.lib.sim.SparseMemory
import spinal.core._

import java.io.File
import java.nio.file.Files
Expand Down Expand Up @@ -41,6 +42,41 @@ class Elf(val f : File, addressWidth : Int){
}
}


def getMemInit[T <: Data](ram: Mem[T],offset: BigInt, allowOverflow: Boolean = false) = {
val wordSize = ram.wordType.getBitsWidth / 8
val initContent = Array.fill[BigInt](ram.wordCount)(0)
foreachSection { section =>
if ((section.header.sh_flags & ElfSectionHeader.FLAG_ALLOC) != 0) {
val data = getData(section)
val memoryAddress = (section.header.sh_addr - offset) & ((BigInt(1) << addressWidth) - 1).toLong
for((byte, i) <- data.zipWithIndex){
val addressWithoutOffset = memoryAddress+i
val addressWord = addressWithoutOffset / wordSize
if (addressWord < 0 || addressWord >= initContent.size) {
assert(allowOverflow)
} else {
initContent(addressWord.toInt) |= BigInt(byte.toInt & 0xFF) << ((addressWithoutOffset.toInt % wordSize) * 8)
}
}
}
}
initContent
}

def init[T <: Data](ram: Mem[T], offset: BigInt, allowOverflow: Boolean = false): Unit = {
val initContent = getMemInit(ram, offset, allowOverflow)
ram.initBigInt(initContent)
}

def load[T <: Data](ram: Mem[T], offset: BigInt, allowOverflow: Boolean = false): Unit = {
val initContent = getMemInit(ram, offset, allowOverflow)
import spinal.core.sim._
for((e, i) <- initContent.zipWithIndex){
ram.setBigInt(i, e)
}
}

def getSymbolAddress(name : String): Long ={
val s = getELFSymbol(name)
s.st_value
Expand Down

0 comments on commit 5e02bf7

Please sign in to comment.