Added comdel system generation
// todo display generation
This commit is contained in:
1 parent
74a7ef3e7a
commit
45a668fc46
17 files changed
+450
-52
No files matched your search
@@ -0,0 +1,273 @@
|
||||
//
|
||||
// Created by bbr on 08.05.22..
|
||||
//
|
||||
|
||||
#include <set>
|
||||
#include "comdel_generator.h"
|
||||
|
||||
namespace domain {
|
||||
|
||||
void generateSchemaFile(std::string& librarySource, Schema *schema, std::ostream &buffer) {
|
||||
buffer << "@source " << librarySource << std::endl << std::endl;
|
||||
|
||||
buffer << "@schema {" << std::endl;
|
||||
|
||||
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;
|
||||
|
||||
for(auto &attribute: componentInstance->attributes) {
|
||||
buffer << "\t\t" << "@attribute " << attribute.name << " " << attribute.value.stringify() << std::endl;
|
||||
}
|
||||
|
||||
buffer << "\t}" << std::endl << std::endl;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
for(auto attribute: busConn->attributes) {
|
||||
buffer << "\t\t" << "@attribute " << attribute.name << " " << attribute.value.stringify() << std::endl;
|
||||
}
|
||||
|
||||
buffer << "\t" << "}" << std::endl;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
buffer << "\t" << "}" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
buffer << "}" << std::endl;
|
||||
}
|
||||
|
||||
std::set<std::string> createImports(Schema *schema);
|
||||
|
||||
std::map<string, string> generateWires(Schema *schema, ostream &ostream);
|
||||
|
||||
void generateSubComponents(Schema *schema, map <string, string> &wires, ostream &buffer);
|
||||
|
||||
void generateComdelFile(Schema *schema, Library &library, std::ostream &buffer) {
|
||||
buffer << library.getHeader() << std::endl;
|
||||
|
||||
std::set<std::string> imports = createImports(schema);
|
||||
|
||||
for(auto& import: imports) {
|
||||
buffer << "#include \"" << library.getComponentDirectory() << "\\" << import << "\"" << std::endl;
|
||||
}
|
||||
|
||||
buffer << "\n\n" << std::endl;
|
||||
|
||||
buffer << "component System" << std::endl << "{" << std::endl;
|
||||
|
||||
auto wires = generateWires(schema, buffer);
|
||||
|
||||
generateSubComponents(schema, wires, buffer);
|
||||
|
||||
buffer << "}";
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
generateWire(name, wire, buffer);
|
||||
}
|
||||
buffer << std::endl << std::endl;
|
||||
}
|
||||
return usedMappings;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
std::set<std::string> createImports(Schema *schema) {
|
||||
std::set<std::string> importSet;
|
||||
for(auto &component: schema->componentInstances) {
|
||||
importSet.insert(component->component.getSource());
|
||||
}
|
||||
return importSet;
|
||||
}
|
||||
|
||||
void generateAutomaticPin(DirectConnectionInstance *connection, string name, string pin, map<string, string> wireNames, stringstream &buffer);
|
||||
|
||||
void
|
||||
generateSingleAutomaticPin(DirectConnectionInstance *instance, string name, string pin,
|
||||
map <string, string> &wireNames,
|
||||
stringstream &buffer);
|
||||
|
||||
void generateSubComponents(Schema *schema, map<string, string> &wires, ostream &buffer) {
|
||||
|
||||
buffer << "\t// components --------------------------------------------" << std::endl;
|
||||
|
||||
for(auto& component: schema->componentInstances) {
|
||||
buffer << "\tsubcomponent " << component->component.getName() << " " << component->name;
|
||||
if(!component->attributes.empty()) {
|
||||
buffer << "<";
|
||||
buffer << component->attributes[0].value.stringify();
|
||||
for(int i=1; i<component->attributes.size(); i++) {
|
||||
buffer << ", " << component->attributes[i].value.stringify();
|
||||
}
|
||||
buffer << ">";
|
||||
}
|
||||
|
||||
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 {
|
||||
if(wire.isType(Value::WIRE_REFERENCE)) {
|
||||
tempOutput << wires[busConn->bus->name + "." + wire.asReference()] << ", ";
|
||||
} else if(wire.isType(Value::NIL)) {
|
||||
tempOutput << "*, ";
|
||||
} else {
|
||||
tempOutput << wire.stringify() << ", ";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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 {
|
||||
generateSingleAutomaticPin(dirConn, component->name, pin.getName(), wires, tempOutput);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// if no connection exists than defaults must exist
|
||||
for(auto& wire: *pin.getWires()) {
|
||||
tempOutput << wire.stringify() << ", ";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto wireList = tempOutput.str();
|
||||
wireList.pop_back();
|
||||
wireList.pop_back(); // remove last COMMA(", ")
|
||||
|
||||
buffer << "(" << wireList << ");" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
selected = connection->attributes[0].value.asReference();
|
||||
defaults = *connection->instance->component.getPin(pin).getWires();
|
||||
} else {
|
||||
wires = *connection->connection.getSecondWires();
|
||||
selected = connection->attributes[1].value.asReference();
|
||||
defaults = *connection->secondInstance->component.getPin(pin).getWires();
|
||||
}
|
||||
|
||||
for(int i=0; i<wires.size(); i++) {
|
||||
if(wires[i].isType(Value::STRING)) {
|
||||
if(wires[i].asString() == selected) {
|
||||
buffer << wireNames[connection->bus->name + "." + connection->bus->bus.getWires()[0].getName()] << ", ";
|
||||
} else {
|
||||
buffer << defaults[i].stringify();
|
||||
}
|
||||
} else {
|
||||
buffer << wires[i].stringify() << ", ";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
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 {
|
||||
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
|
||||
@@ -0,0 +1,16 @@
|
||||
#ifndef COMDEL_GENERATOR_H
|
||||
#define COMDEL_GENERATOR_H
|
||||
|
||||
#include <sstream>
|
||||
#include "schema.h"
|
||||
#include "library.h"
|
||||
|
||||
namespace domain {
|
||||
|
||||
void generateSchemaFile(std::string& librarySource, Schema *schema, std::ostream &buffer);
|
||||
|
||||
void generateComdelFile(Schema *schema, Library &library, std::ostream &buffer);
|
||||
|
||||
} // domain
|
||||
|
||||
#endif //COMDEL_GENERATOR_H
|
||||
@@ -7,6 +7,16 @@ ConnectionInstance::ConnectionInstance(ComponentInstance *instance, std::vector<
|
||||
: instance(instance), attributes(attributes), connection(connection)
|
||||
{}
|
||||
|
||||
InstanceAttribute ConnectionInstance::getAttribute(string attribute) {
|
||||
for(auto& attr: attributes) {
|
||||
if(attr.name == attribute) {
|
||||
return attr;
|
||||
}
|
||||
}
|
||||
throw std::exception();
|
||||
}
|
||||
|
||||
|
||||
BusConnectionInstance::BusConnectionInstance(ComponentInstance *instance, std::vector<InstanceAttribute> attributes, BusInstance *bus, Connection connection)
|
||||
: ConnectionInstance(instance, attributes, connection), bus(bus)
|
||||
{}
|
||||
|
||||
@@ -20,6 +20,8 @@ public:
|
||||
std::vector<InstanceAttribute> attributes;
|
||||
|
||||
ConnectionInstance(ComponentInstance *instance, std::vector<InstanceAttribute> attributes, Connection connection);
|
||||
|
||||
InstanceAttribute getAttribute(string attribute);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -18,7 +18,7 @@ Pin::Pin(std::string name, PinType type, std::string tooltip, PinConnection conn
|
||||
: name(name), type(type), tooltip(tooltip), connection(connection), display(display), wires(wires)
|
||||
{}
|
||||
|
||||
std::string Pin::getName() {
|
||||
std::string &Pin::getName() {
|
||||
return name;
|
||||
}
|
||||
Pin::PinType Pin::getType() {
|
||||
|
||||
+1
-1
@@ -50,7 +50,7 @@ private:
|
||||
public:
|
||||
Pin(std::string name, PinType type, std::string tooltip, PinConnection connection, Display display, std::optional<std::vector<Value>> wires);
|
||||
|
||||
std::string getName();
|
||||
std::string &getName();
|
||||
PinType getType();
|
||||
std::string getTooltip();
|
||||
Display &getDisplay();
|
||||
|
||||
@@ -6,4 +6,23 @@ Schema::Schema()
|
||||
{
|
||||
}
|
||||
|
||||
bool Schema::hasConnection(string& component, string& pin) {
|
||||
return getConnection(component, pin) != nullptr;
|
||||
}
|
||||
|
||||
ConnectionInstance* Schema::getConnection(string& component, string& pin) {
|
||||
for(auto& conn: connections) {
|
||||
if(conn->instance->name == component && conn->connection.getComponent().pin == pin) {
|
||||
return conn.get();
|
||||
}
|
||||
auto dirConn = dynamic_cast<DirectConnectionInstance*>(conn.get());
|
||||
if(dirConn != nullptr) {
|
||||
if(dirConn->secondInstance->name == component && conn->connection.getSecondComponent()->pin == pin) {
|
||||
return dirConn;
|
||||
}
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace domain
|
||||
@@ -35,6 +35,9 @@ public:
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool hasConnection(string& component, string& pin);
|
||||
ConnectionInstance* getConnection(string& component, string& pin);
|
||||
};
|
||||
|
||||
} // namespace domain
|
||||
|
||||
Reference in new issue
Block a user