schema_editor/comdel/domain/comdel_generator.cpp

375 lines
15 KiB
C++
Raw Permalink Normal View History

//
// Created by bbr on 08.05.22..
//
#include <set>
#include "comdel_generator.h"
namespace domain {
2022-05-27 06:18:17 +00:00
void generate_schema(std::string &librarySource, Schema *schema, std::ostream &buffer) {
buffer << "@source \"" << librarySource << "\"" << std::endl << std::endl;
2022-05-27 06:18:17 +00:00
buffer << "@schema {" << std::endl;
2022-05-27 06:18:17 +00:00
for (auto &componentInstance: schema->componentInstances) {
buffer << "\t" << "@instance " << componentInstance->name << " " << componentInstance->component.getName()
<< " {" << std::endl;
buffer << "\t\t" << "@position (" << componentInstance->position.first << ", "
<< componentInstance->position.second << ")" << std::endl;
2022-05-27 06:18:17 +00:00
for (auto &attribute: componentInstance->attributes) {
buffer << "\t\t" << "@attribute " << attribute.name << " " << attribute.value.stringify() << std::endl;
}
buffer << "\t}" << std::endl << std::endl;
}
2022-05-27 06:18:17 +00:00
for (auto &busInstance: schema->busInstances) {
buffer << "\t" << "@instance " << busInstance->name << " " << busInstance->bus.getName() << " {"
<< std::endl;
buffer << "\t\t" << "@position (" << busInstance->position.first << ", " << busInstance->position.second
<< ")" << std::endl;
buffer << "\t\t" << "@size " << busInstance->size << std::endl;
buffer << "\t}" << std::endl << std::endl;
}
2022-05-27 06:18:17 +00:00
for (auto &conn: schema->connections) {
auto busConn = dynamic_cast<domain::BusConnectionInstance *>(conn.get());
if (busConn) {
buffer << "\t" << "@connection (" << busConn->instance->name << "."
<< busConn->connection.getComponent().pin << ", " << busConn->bus->name << ") {" << std::endl;
2022-05-27 06:18:17 +00:00
for (auto attribute: busConn->attributes) {
buffer << "\t\t" << "@attribute " << attribute.name << " " << attribute.value.stringify()
<< std::endl;
}
2022-05-27 06:18:17 +00:00
buffer << "\t" << "}" << std::endl;
}
2022-05-27 06:18:17 +00:00
auto dirConn = dynamic_cast<domain::DirectConnectionInstance *>(conn.get());
if (dirConn) {
buffer << "\t" << "@connection (" << dirConn->instance->name << "."
<< dirConn->connection.getComponent().pin << ", "
<< dirConn->bus->name << ", "
<< dirConn->secondInstance->name << "." << dirConn->connection.getSecondComponent()->pin
<< ") {" << std::endl;
for (auto attribute: dirConn->attributes) {
buffer << "\t\t" << "@attribute " << attribute.name << " " << attribute.value.stringify()
<< std::endl;
}
2022-05-27 06:18:17 +00:00
buffer << "\t" << "}" << std::endl;
}
}
2022-05-27 06:18:17 +00:00
buffer << "}" << std::endl;
}
2022-05-27 06:18:17 +00:00
std::set<std::string> createImports(Schema *schema);
2022-05-27 06:18:17 +00:00
std::map<string, string> generateWires(Schema *schema, ostream &ostream);
2022-05-27 06:18:17 +00:00
void generateSubComponents(Schema *schema, map<string, string> &wires, ostream &buffer);
2022-05-27 06:18:17 +00:00
void generateDisplay(Schema *schema, ostream &buffer);
2022-05-27 06:18:17 +00:00
void generate_comdel(Schema *schema, Library &library, std::ostream &buffer) {
buffer << library.getHeader() << std::endl;
2022-05-14 13:58:44 +00:00
2022-05-27 06:18:17 +00:00
std::set<std::string> imports = createImports(schema);
2022-05-27 06:18:17 +00:00
for (auto &import: imports) {
buffer << "#include \"" << library.getComponentDirectory() << "\\" << import << "\"" << std::endl;
}
2022-05-27 06:18:17 +00:00
buffer << "\n\n" << std::endl;
buffer << "component System" << std::endl << "{" << std::endl;
if (library.getComponentHeader().has_value()) {
buffer << library.getComponentHeader().value() << endl;
}
2022-05-27 06:18:17 +00:00
auto wires = generateWires(schema, buffer);
2022-05-27 06:18:17 +00:00
generateSubComponents(schema, wires, buffer);
2022-05-27 06:18:17 +00:00
generateDisplay(schema, buffer);
buffer << "}";
2022-05-15 23:02:34 +00:00
}
2022-05-27 06:18:17 +00:00
void generateBus(BusInstance *bus, ostream &buffer);
2022-05-27 06:18:17 +00:00
void generateConnection(ConnectionInstance *connection, ostream &buffer);
2022-05-27 06:18:17 +00:00
void generateDisplay(Schema *schema, ostream &buffer) {
buffer << "\n\tdisplay {\n";
2022-05-14 13:58:44 +00:00
2022-05-27 06:18:17 +00:00
for (auto &component: schema->componentInstances) {
buffer << "\t\tcomponent { x: " << component->position.first << "; y: " << component->position.second
<< "; ref: \"" << component->name << "\"; }" << std::endl;
}
2022-05-27 06:18:17 +00:00
for (auto &bus: schema->busInstances) {
generateBus(bus.get(), buffer);
}
2022-05-14 13:58:44 +00:00
2022-05-27 06:18:17 +00:00
buffer << "\n";
2022-05-14 13:58:44 +00:00
2022-05-27 06:18:17 +00:00
for (auto &connection: schema->connections) {
generateConnection(connection.get(), buffer);
}
2022-05-14 13:58:44 +00:00
2022-05-27 06:18:17 +00:00
buffer << "\t}\n";
2022-05-14 13:58:44 +00:00
}
2022-05-27 06:18:17 +00:00
void generateConnection(ConnectionInstance *connection, ostream &buffer) {
buffer << "\t\tline {x1:" << connection->start.first << "; y1:" << connection->start.second << "; " <<
"x2:" << connection->end.first << "; y2:" << connection->end.second << ";}" << "\n";
2022-05-25 05:39:45 +00:00
}
2022-05-27 06:18:17 +00:00
void generateBus(BusInstance *bus, ostream &buffer) {
buffer << "\n";
buffer << "\t\t// " << bus->name << " bus\n\n";
2022-05-14 13:58:44 +00:00
2022-05-27 06:18:17 +00:00
if (bus->bus.getDisplayBus().has_value()) {
bus->bus.getDisplayBus()->comdel(buffer, bus->position.first, bus->position.second, bus->size);
}
}
2022-05-25 05:39:45 +00:00
2022-05-27 06:18:17 +00:00
void generateWire(std::string &name, Wire &wire, ostream &buffer);
std::map<string, string> generateWires(Schema *schema, ostream &buffer) {
std::set<string> usedNames;
std::map<string, string> usedMappings;
for (auto &bus: schema->busInstances) {
buffer << "\t//" << bus->name << std::endl;
for (auto &wire: bus->bus.getWires()) {
std::string name = wire.getName();
if (usedNames.count(wire.getName()) > 0) {
name = bus->name + "__" + wire.getName();
}
if (wire.isHidden()) {
name = "--" + name;
}
usedNames.insert(name);
usedMappings.insert(std::make_pair(bus->name + "." + wire.getName(), name));
2022-05-14 13:58:44 +00:00
2022-05-27 06:18:17 +00:00
generateWire(name, wire, buffer);
}
2022-05-27 06:18:17 +00:00
buffer << std::endl << std::endl;
}
2022-05-27 06:18:17 +00:00
return usedMappings;
}
2022-05-27 06:18:17 +00:00
void generateWire(std::string &name, Wire &wire, ostream &buffer) {
buffer << "\t";
switch (wire.getType()) {
case Wire::R_WIRE:
buffer << "r_wire";
break;
case Wire::WIRED_AND:
buffer << "wired_and";
break;
case Wire::WIRED_OR:
buffer << "wired_or";
break;
default:
buffer << "wire";
}
if (wire.getWidth() != 1) {
buffer << "<" << wire.getWidth() << ">";
}
buffer << " " << name << ";" << std::endl;
}
2022-05-27 06:18:17 +00:00
std::set<std::string> createImports(Schema *schema) {
std::set<std::string> importSet;
for (auto &component: schema->componentInstances) {
importSet.insert(component->component.getSource());
}
return importSet;
}
2022-05-27 06:18:17 +00:00
void
generateAutomaticPin(DirectConnectionInstance *connection, string name, string pin, map<string, string> wireNames,
stringstream &buffer);
2022-05-27 06:18:17 +00:00
void generateSingleAutomaticPin(DirectConnectionInstance *instance, string name, string pin,
map<string, string> &wireNames,
stringstream &buffer);
2022-05-27 06:18:17 +00:00
void generateComponent(Schema *schema, map<string, string> &wires, ostream &buffer,
shared_ptr<ComponentInstance> &component);
2022-05-15 21:55:03 +00:00
void generateSubComponents(Schema *schema, map<string, string> &wires, ostream &buffer) {
2022-05-27 06:18:17 +00:00
buffer << "\t// components --------------------------------------------" << std::endl;
2022-05-27 06:18:17 +00:00
for (auto &component: schema->componentInstances) {
if (component->component.getType() == Component::MEMORY) {
generateComponent(schema, wires, buffer, component);
}
}
2022-05-27 06:18:17 +00:00
for (auto &component: schema->componentInstances) {
if (component->component.getType() != Component::MEMORY) {
generateComponent(schema, wires, buffer, component);
}
2022-05-15 21:55:03 +00:00
}
}
2022-05-27 06:18:17 +00:00
void generateComponent(Schema *schema, map<string, string> &wires, ostream &buffer,
shared_ptr<ComponentInstance> &component) {
buffer << "\tsubcomponent " << component->component.getName() << " " << component->name;
2022-05-15 21:55:03 +00:00
2022-05-27 06:18:17 +00:00
std::optional<InstanceAttribute> memory;
2022-05-15 21:55:03 +00:00
2022-05-27 06:18:17 +00:00
std::vector<InstanceAttribute> attributes;
for (auto &attr: component->attributes) {
if (attr.name != "_memory") {
attributes.push_back(attr);
} else if (attr.name == "_memory" && attr.value.asMemoryReference().has_value()) {
memory = attr;
}
2022-05-15 21:55:03 +00:00
}
2022-05-27 06:18:17 +00:00
if (!attributes.empty()) {
buffer << "<";
buffer << attributes[0].value.stringify();
for (int i = 1; i < attributes.size(); i++) {
buffer << ", " << attributes[i].value.stringify();
}
buffer << ">";
2022-05-15 21:55:03 +00:00
}
2022-05-27 06:18:17 +00:00
stringstream tempOutput;
for (auto &pin: component->component.getPins()) {
if (schema->hasConnection(component->name, pin.getName())) {
auto conn = schema->getConnection(component->name, pin.getName());
auto busConn = dynamic_cast<BusConnectionInstance *>(conn);
if (busConn != nullptr) {
for (auto wire: busConn->connection.getWires()) {
if (wire.isType(Value::ATTRIBUTE_REFERENCE)) {
auto attribute = busConn->getAttribute(wire.asReference());
if (wire.isType(Value::WIRE_REFERENCE)) {
tempOutput << wires[busConn->bus->name + "." + attribute.value.asReference()] << ", ";
} else if (wire.isType(Value::NIL)) {
tempOutput << "*, ";
} else {
tempOutput << attribute.value.stringify() << ", ";
}
} else {
2022-05-27 06:18:17 +00:00
if (wire.isType(Value::WIRE_REFERENCE)) {
tempOutput << wires[busConn->bus->name + "." + wire.asReference()] << ", ";
} else if (wire.isType(Value::NIL)) {
tempOutput << "*, ";
} else {
tempOutput << wire.stringify() << ", ";
}
}
2022-05-27 06:18:17 +00:00
}
}
auto dirConn = dynamic_cast<DirectConnectionInstance *>(conn);
if (dirConn != nullptr) {
if (dirConn->bus->bus.getType() == Bus::AUTOMATIC) {
generateAutomaticPin(dirConn, component->name, pin.getName(), wires, tempOutput);
} else {
2022-05-27 06:18:17 +00:00
generateSingleAutomaticPin(dirConn, component->name, pin.getName(), wires, tempOutput);
}
}
2022-05-27 06:18:17 +00:00
} else {
// if no connection exists than defaults must exist
for (auto &wire: *pin.getWires()) {
2022-06-05 23:10:11 +00:00
if(wire.isType(Value::NIL)) {
tempOutput << "*, ";
} else {
tempOutput << wire.stringify() << ", ";
}
}
}
}
2022-05-27 06:18:17 +00:00
auto wireList = tempOutput.str();
wireList.pop_back();
wireList.pop_back(); // remove last COMMA(", ")
2022-05-27 06:18:17 +00:00
buffer << "(" << wireList << ")";
2022-05-15 21:55:03 +00:00
2022-05-27 06:18:17 +00:00
if (memory.has_value()) {
buffer << " uses " << *memory->value.asMemoryReference();
}
buffer << ";" << std::endl;
2022-05-15 21:55:03 +00:00
}
2022-05-27 06:18:17 +00:00
void generateSingleAutomaticPin(DirectConnectionInstance *connection, string name, string pin,
map<string, string> &wireNames, stringstream &buffer) {
std::vector<Value> wires;
std::string selected;
std::vector<Value> defaults;
if (connection->instance->name == name && connection->connection.getComponent().pin == pin) {
wires = connection->connection.getWires();
2022-06-05 23:10:11 +00:00
selected = connection->attributes[0].value.asString();
2022-05-27 06:18:17 +00:00
defaults = *connection->instance->component.getPin(pin).getWires();
} else {
2022-05-27 06:18:17 +00:00
wires = *connection->connection.getSecondWires();
2022-06-05 23:10:11 +00:00
selected = connection->attributes[1].value.asString();
2022-05-27 06:18:17 +00:00
defaults = *connection->secondInstance->component.getPin(pin).getWires();
}
2022-05-27 06:18:17 +00:00
for (int i = 0; i < wires.size(); i++) {
if (wires[i].isType(Value::STRING)) {
if (wires[i].asString() == selected) {
2022-06-05 23:10:11 +00:00
auto wireName = connection->bus->name + "." + connection->bus->bus.getWires()[0].getName();
buffer << wireNames[wireName] << ", ";
} else if(defaults[i].isType(Value::NIL)) {
buffer << "*, ";
2022-05-27 06:18:17 +00:00
} else {
2022-06-05 23:10:11 +00:00
buffer << defaults[i].stringify() << ", ";
2022-05-27 06:18:17 +00:00
}
2022-06-05 23:10:11 +00:00
} else if(defaults[i].isType(Value::NIL)) {
buffer << "*, ";
} else {
2022-05-27 06:18:17 +00:00
buffer << wires[i].stringify() << ", ";
}
2022-05-27 06:18:17 +00:00
}
}
void
generateAutomaticPin(DirectConnectionInstance *connection, string name, string pin, map<string, string> wireNames,
stringstream &buffer) {
std::vector<Value> wires;
if (connection->instance->name == name && connection->connection.getComponent().pin == pin) {
wires = connection->connection.getWires();
} else {
2022-05-27 06:18:17 +00:00
wires = *connection->connection.getSecondWires();
}
for (auto &wire: wires) {
if (wire.isType(Value::ATTRIBUTE_REFERENCE)) {
auto attribute = connection->getAttribute(wire.asReference());
if (wire.isType(Value::WIRE_REFERENCE)) {
buffer << wireNames[connection->bus->name + "." + attribute.value.asReference()] << ", ";
} else if (wire.isType(Value::NIL)) {
buffer << "*, ";
} else {
buffer << attribute.value.stringify() << ", ";
}
} else {
2022-05-27 06:18:17 +00:00
if (wire.isType(Value::WIRE_REFERENCE)) {
buffer << wireNames[connection->bus->name + "." + wire.asReference()] << ", ";
} else if (wire.isType(Value::NIL)) {
buffer << "*, ";
} else {
buffer << wire.stringify() << ", ";
}
}
}
}
} // domain