add svd2funreg

This commit is contained in:
Clyne 2025-02-05 10:29:10 -05:00
parent 2a5fe35aee
commit 4acb48d15a
Signed by: clyne
GPG Key ID: 7BA5A2980566A649
4 changed files with 87 additions and 0 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "svd2funreg/pugixml"]
path = svd2funreg/pugixml
url = https://github.com/zeux/pugixml

2
svd2funreg/Makefile Normal file
View File

@ -0,0 +1,2 @@
all:
g++ -o main main.cpp pugixml/src/pugixml.cpp -I pugixml/src

81
svd2funreg/main.cpp Normal file
View File

@ -0,0 +1,81 @@
#include "pugixml.hpp"
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <string>
static void parse_peripheral(const pugi::xml_node& node);
static void parse_register(uint32_t base_address, const pugi::xml_node& node);
static void parse_field(const char *reg, const pugi::xml_node& node);
int main(int argc, char *argv[])
{
if (argc < 2)
return -1;
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_file(argv[1]);
if (!result)
return -1;
std::cout << std::hex;
std::cout << "namespace " << doc.child("device").child_value("name")
<< " {\n";
for (auto node : doc.child("device")
.child("peripherals")
.children("peripheral"))
{
parse_peripheral(node);
}
std::cout << "}\n";
}
void parse_peripheral(const pugi::xml_node& node)
{
const auto base = std::strtol(node.child_value("baseAddress"), nullptr, 0);
std::cout << " namespace " << node.child_value("name") << " {\n";
for (auto n : node.child("registers").children("register"))
parse_register(base, n);
std::cout << " }\n";
}
void parse_register(uint32_t base_address, const pugi::xml_node& node)
{
const auto offs = std::strtol(node.child_value("addressOffset"), nullptr, 0);
const auto size = std::strtol(node.child_value("size"), nullptr, 0);
const auto name = node.child_value("name");
std::cout << " struct " << name << " : public fr::MemRegister<";
switch (size) {
case 8: std::cout << "uint8_t, "; break;
case 16: std::cout << "uint16_t, "; break;
case 32:
default: std::cout << "uint32_t, "; break;
}
std::cout << "0x" << base_address + offs << "> {\n";
for (auto n : node.child("fields").children("field"))
parse_field(name, n);
std::cout << " };\n";
}
void parse_field(const char *reg, const pugi::xml_node& node)
{
const auto offs = std::strtol(node.child_value("bitOffset"), nullptr, 0);
const auto width = std::strtol(node.child_value("bitWidth"), nullptr, 0);
std::string name = node.child_value("name");
if (name == reg)
name += "_val";
std::cout << " using " << name << " = fr::RegisterMask<"
<< reg << ", (0x" << (1 << width) - 1 << "u << 0x" << offs << "u)>;\n";
}

1
svd2funreg/pugixml Submodule

@ -0,0 +1 @@
Subproject commit a305d01bc8dd3f3708c4ee80be8c87dfb51e13ad