落地映像读写:72 字节头、段布局、FNV-1a、.stb 与 sidecar。
- 指令与映像.md 冻结:头字段表、常量表/函数表行、段序、数据段对齐、sidecar 格式 - Types.h 补 TypeTag;Image.h/cpp:ImageView 校验、write_ret_main、文件与 sidecar - isa CMake 改 target_include_directories PUBLIC
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
/**
|
||||
* @file Image.h
|
||||
* @brief 映像头、段、FNV-1a 哈希、.stb 文件与 sidecar
|
||||
* @author
|
||||
* @date 2026-08-19
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "isa/Types.h"
|
||||
|
||||
namespace isa {
|
||||
|
||||
// 魔数 "STSC"(小端 u32)、版本
|
||||
static const uint32_t kMagic = 0x43545353u;
|
||||
static const uint32_t kVersion = 1;
|
||||
|
||||
// 头与表行宽(见 Doc/isa/指令与映像.md)
|
||||
static const size_t kHeaderSize = 72;
|
||||
static const size_t kConstEntrySize = 12;
|
||||
static const size_t kFuncRowSize = 12;
|
||||
|
||||
// FNV-1a 64:basis / prime(见 Doc/isa/指令与映像.md)
|
||||
static const uint64_t kFnvBasis = 0xcbf29ce484222325ull;
|
||||
static const uint64_t kFnvPrime = 0x100000001b3ull;
|
||||
|
||||
// FNV-1a 64 增量与一次性
|
||||
uint64_t fnv1a64_update(uint64_t h, const uint8_t* data, size_t len);
|
||||
uint64_t fnv1a64(const uint8_t* data, size_t len);
|
||||
|
||||
// 映像头(字节布局见 Doc/isa/指令与映像.md)
|
||||
struct ImageHeader {
|
||||
uint32_t cycle_limit;
|
||||
uint32_t dt_ms;
|
||||
uint64_t project_hash;
|
||||
uint32_t entry_fn_id;
|
||||
uint32_t n_globals;
|
||||
uint32_t n_i;
|
||||
uint32_t n_q;
|
||||
uint32_t n_m;
|
||||
uint32_t n_consts;
|
||||
uint32_t n_funcs;
|
||||
uint32_t offset_const;
|
||||
uint32_t offset_funcs;
|
||||
uint32_t offset_code;
|
||||
uint32_t offset_fb;
|
||||
uint32_t offset_data;
|
||||
};
|
||||
|
||||
// 常量表一行
|
||||
struct ConstEntry {
|
||||
types::TypeTag tag;
|
||||
uint64_t value;
|
||||
};
|
||||
|
||||
// 函数表一行
|
||||
struct FuncRow {
|
||||
uint32_t nregs;
|
||||
uint32_t code_offset; // 相对字节码段起点
|
||||
uint32_t code_len; // 指令条数
|
||||
};
|
||||
|
||||
// I/O 绑定(sidecar 一行)
|
||||
struct IoBinding {
|
||||
std::string var;
|
||||
uint32_t slot;
|
||||
uint32_t channel;
|
||||
uint32_t bit;
|
||||
bool is_input; // true = [[io.input]],false = [[io.output]]
|
||||
};
|
||||
|
||||
// 只读视图:校验过的映像
|
||||
class ImageView {
|
||||
public:
|
||||
static ImageView from(const uint8_t* buf, size_t len);
|
||||
static ImageView from(const std::vector<uint8_t>& buf);
|
||||
|
||||
bool ok() const;
|
||||
const std::string& error() const;
|
||||
const ImageHeader& header() const;
|
||||
|
||||
const uint8_t* raw() const;
|
||||
size_t raw_len() const;
|
||||
|
||||
ConstEntry const_entry(size_t i) const;
|
||||
FuncRow func_row(size_t i) const;
|
||||
|
||||
const uint8_t* code_bytes() const; // 字节码段起点
|
||||
size_t code_len() const; // 字节数
|
||||
const uint8_t* data_bytes() const; // 数据段起点
|
||||
size_t data_len() const; // 字节数
|
||||
|
||||
private:
|
||||
ImageView();
|
||||
|
||||
const uint8_t* buf_;
|
||||
size_t len_;
|
||||
bool ok_;
|
||||
std::string err_;
|
||||
ImageHeader hdr_;
|
||||
};
|
||||
|
||||
// 写最小映像:空槽 + MAIN(fn_id=0)+ 一条 RET
|
||||
std::vector<uint8_t> write_ret_main(uint32_t cycle_limit, uint32_t dt_ms,
|
||||
uint64_t project_hash);
|
||||
|
||||
// .stb 文件读写(纯字节,校验交给 read_image / ImageView)
|
||||
bool write_stb_file(const char* path, const std::vector<uint8_t>& image,
|
||||
std::string* err);
|
||||
bool read_stb_file(const char* path, std::vector<uint8_t>* image,
|
||||
std::string* err);
|
||||
|
||||
// sidecar 生成与写文件(格式见 Doc/isa/指令与映像.md)
|
||||
std::string make_sidecar(const std::vector<IoBinding>& bindings);
|
||||
bool write_sidecar_file(const char* path,
|
||||
const std::vector<IoBinding>& bindings,
|
||||
std::string* err);
|
||||
}
|
||||
@@ -20,6 +20,13 @@ namespace isa {
|
||||
// 差值可负、为 DATE_AND_TIME 与 64 位定时器预留。槽位按 8 字节对齐。
|
||||
using TIME = int64_t;
|
||||
|
||||
// 槽 / 常量表类型标记,数值冻结:0=BOOL、1=INT、2=TIME(见映像规范)
|
||||
enum TypeTag : uint8_t {
|
||||
Bool = 0,
|
||||
Int = 1,
|
||||
Time = 2,
|
||||
};
|
||||
|
||||
// 饱和算术:夹到 INT 最小/最大,编译期与 VM 共用同一规则。
|
||||
|
||||
inline INT sat_add(INT a, INT b) {
|
||||
|
||||
Reference in New Issue
Block a user