Skip to content

Commit 19ca9ac

Browse files
committed
[verilator] Support loading binary files
Signed-off-by: Chris Frantz <[email protected]>
1 parent d4df30f commit 19ca9ac

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

hw/dv/verilator/cpp/dpi_memutil.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <cassert>
88
#include <cstring>
99
#include <fcntl.h>
10+
#include <filesystem>
11+
#include <fstream>
1012
#include <iostream>
1113
#include <libelf.h>
1214
#include <sstream>
@@ -92,6 +94,8 @@ static MemImageType GetMemImageTypeByName(const std::string &name) {
9294
return kMemImageElf;
9395
if (name == "vmem")
9496
return kMemImageVmem;
97+
if (name == "bin")
98+
return kMemImageBin;
9599

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

128+
// Load a binary image into an array of bytes.
129+
static std::vector<uint8_t> LoadBinary(const std::string &filepath) {
130+
std::ifstream source(filepath, std::ios::binary);
131+
if (!source.good()) {
132+
std::ostringstream oss;
133+
oss << "Cannot read file: `" << filepath << "'.";
134+
throw std::runtime_error(oss.str());
135+
}
136+
137+
source.seekg(0, std::ios::end);
138+
size_t length = source.tellg();
139+
source.seekg(0, std::ios::beg);
140+
141+
std::vector<uint8_t> buf(length);
142+
source.read(reinterpret_cast<char *>(buf.data()), length);
143+
return buf;
144+
}
145+
124146
// Generate a single array of bytes representing the contents of PT_LOAD
125147
// segments of the ELF file. Like objcopy, this generates a single "giant
126148
// segment" whose first byte corresponds to the first byte of the lowest
@@ -406,6 +428,9 @@ void DpiMemUtil::LoadFileToNamedMem(bool verbose, const std::string &name,
406428
case kMemImageVmem:
407429
m.LoadVmem(filepath);
408430
break;
431+
case kMemImageBin:
432+
m.Write(0, LoadBinary(filepath));
433+
break;
409434
default:
410435
assert(0);
411436
}

hw/dv/verilator/cpp/dpi_memutil.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ enum MemImageType {
2020
kMemImageUnknown = 0,
2121
kMemImageElf,
2222
kMemImageVmem,
23+
kMemImageBin,
2324
};
2425

2526
// Staged data for a given memory area.

0 commit comments

Comments
 (0)