Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions hw/dv/verilator/cpp/dpi_memutil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <cassert>
#include <cstring>
#include <fcntl.h>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <libelf.h>
#include <sstream>
Expand Down Expand Up @@ -92,6 +94,8 @@ static MemImageType GetMemImageTypeByName(const std::string &name) {
return kMemImageElf;
if (name == "vmem")
return kMemImageVmem;
if (name == "bin")
return kMemImageBin;

std::ostringstream oss;
oss << "Unknown image type: `" << name << "'.";
Expand Down Expand Up @@ -121,6 +125,24 @@ static MemImageType DetectMemImageType(const std::string &filepath) {
return image_type;
}

// Load a binary image into an array of bytes.
static std::vector<uint8_t> LoadBinary(const std::string &filepath) {
std::ifstream source(filepath, std::ios::binary);
if (!source.good()) {
std::ostringstream oss;
oss << "Cannot read file: `" << filepath << "'.";
throw std::runtime_error(oss.str());
}

source.seekg(0, std::ios::end);
size_t length = source.tellg();
source.seekg(0, std::ios::beg);

std::vector<uint8_t> buf(length);
source.read(reinterpret_cast<char *>(buf.data()), length);
return buf;
}

// Generate a single array of bytes representing the contents of PT_LOAD
// segments of the ELF file. Like objcopy, this generates a single "giant
// segment" whose first byte corresponds to the first byte of the lowest
Expand Down Expand Up @@ -406,6 +428,9 @@ void DpiMemUtil::LoadFileToNamedMem(bool verbose, const std::string &name,
case kMemImageVmem:
m.LoadVmem(filepath);
break;
case kMemImageBin:
m.Write(0, LoadBinary(filepath));
break;
default:
assert(0);
}
Expand Down
1 change: 1 addition & 0 deletions hw/dv/verilator/cpp/dpi_memutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ enum MemImageType {
kMemImageUnknown = 0,
kMemImageElf,
kMemImageVmem,
kMemImageBin,
};

// Staged data for a given memory area.
Expand Down
Loading