diff --git a/NodeVector.c b/NodeVector.c index 91cb436..45201f3 100644 --- a/NodeVector.c +++ b/NodeVector.c @@ -30,6 +30,19 @@ void nodev_push_back(NodeVector *vector, Node node) { void nodev_erase(NodeVector *vector, int index) { node_free(nodev_at(vector, index)); + for (size_t i = 0; i < vector->count; i++) { + if (i == index) + continue; + Node *node = nodev_at(vector, i); + for (int w = 0; w < node->component.funData.outC; w++) { + Wire *wire = &node->wires[w]; + for (size_t c = 0; c < wire->conCount; c++) { + Connection *conn = &wire->connections[c]; + if (conn->dest > index) + conn->dest--; + } + } + } for (size_t i = index + 1; i < vector->count; i++) { vector->nodes[i - 1] = vector->nodes[i]; for (int j = 0; j < vector->nodes[i].component.funData.outC; j++) diff --git a/Save.c b/Save.c index a2fde50..0dbbb4a 100644 --- a/Save.c +++ b/Save.c @@ -60,57 +60,145 @@ typedef struct Modules { } Modules; bool save_as_module(NodeVector *vector, char *name) { + char funPath[256]; - sprintf(funPath, "res/%s.fun", name); + sprintf(funPath, "res/Modules/%s.fun", name); FILE *funFile = fopen(funPath, "wt"); if (funFile == NULL) { log_error("Couldn't open file %s!\n", funPath); return false; } - int inC = 0, outC = 0, assignC = 0, wireC = 0; - for (size_t i = 0; i < vector->count; i++) { - Node *node = nodev_at(vector, i); - switch (node->component.type) { - case CT_SWITCH: { - inC++; - break; - } - case CT_LED: { - outC++; - break; - } - case CT_MODULE: { - assignC++; - for (int w = 0; w < node->component.funData.outC; w++) { - wireC += node->wires[w].conCount; - } - break; - } - default: { - break; - } - } - } + Modules *modules = (Modules *)malloc(sizeof(Module) * vector->count); + for (size_t n = 0; n < vector->count; n++) { + modules[n].connections = NULL; + } - Modules *modules = (struct Modules *) malloc(sizeof(struct Modules) * assignC); + int inC = 0, outC = 0, assignC = 0, wireC = 0; + for (size_t n = 0; n < vector->count; n++) { + Node *node = nodev_at(vector, n); + switch (node->component.type) + { + case CT_SWITCH: + inC++; + break; + case CT_LED: + outC++; + assignC++; + break; + case CT_MODULE: + assignC++; + wireC += node->component.funData.outC; + break; + default: + break; + } + modules[n].num = node->component.funData.inC; + modules[n].connections = (InnerConnection *)malloc(sizeof(InnerConnection) * modules[n].num); + } - int index = 0; - for (size_t i = 0; i < vector->count; i++) { - Node *node = nodev_at(vector, i); - if (node->component.type == CT_MODULE) { - modules[index].connections = (struct InnerConnection *) malloc( - sizeof(struct InnerConnection) * node->component.funData.inC); - index++; - } - } + fprintf(funFile, "assigns %d\n", assignC); + fprintf(funFile, "in %d\n", inC); + fprintf(funFile, "out %d\n", outC); + fprintf(funFile, "wires %d\n", wireC); - for (size_t i = 0; i < vector->count; i++) { - Node *node = nodev_at(vector, i); - if (node->component.type == CT_MODULE) { - } + int wireIndex = 0, inputIndex = 0; + for (size_t n = 0; n < vector->count; n++) { + Node *node = nodev_at(vector, n); + for (int w = 0; w < node->component.funData.outC; w++) { + Wire *wire = &node->wires[w]; + for (size_t c = 0; c < wire->conCount; c++) { + Connection *conn = &wire->connections[c]; + switch (node->component.type) + { + case CT_SWITCH: + modules[conn->dest].connections[conn->pin].id = inputIndex; + modules[conn->dest].connections[conn->pin].type = INPUT; + break; + case CT_MODULE: + modules[conn->dest].connections[conn->pin].id = wireIndex; + modules[conn->dest].connections[conn->pin].type = WIRE; + break; + default: + break; + } + } + if (node->component.type == CT_MODULE) + wireIndex++; + } + if (node->component.type == CT_SWITCH) + inputIndex++; + } + + wireIndex = 0; + int outputIndex = 0; + for (size_t n = 0; n < vector->count; n++) { + Node *node = nodev_at(vector, n); + if (node->component.type == CT_MODULE) { + fprintf(funFile, "{"); + bool first = true; + for (int w = 0; w < node->component.funData.outC; w++) { + if (!first) + fprintf(funFile, ", "); + else + first = false; + fprintf(funFile, "W%d", wireIndex); + wireIndex++; + } + fprintf(funFile, "} = %s(", node->component.name); + first = true; + for (int i = 0; i < modules[n].num; i++) { + if (!first) + fprintf(funFile, ", "); + else + first = false; + switch (modules[n].connections[i].type) + { + case WIRE: + fprintf(funFile, "W"); + break; + case INPUT: + fprintf(funFile, "$"); + break; + } + fprintf(funFile, "%d", modules[n].connections[i].id); + } + fprintf(funFile, ")\n"); + } else if (node->component.type == CT_LED) { + fprintf(funFile, "%%%d = W%d\n", outputIndex, modules[n].connections[0].id); + outputIndex++; + } + } - fprintf(funFile, "assigns %d\nin %d\nout %d\n", assignC + wireC, inC, outC); + for (size_t n = 0; n < vector->count; n++) { + free(modules[n].connections); + } + free(modules); fclose(funFile); + + char cmpPath[256]; + sprintf(cmpPath, "res/Modules/%s.cmp", name); + FILE *cmpFile = fopen(cmpPath, "wt"); + if (cmpFile == NULL) { + log_error("Couldn't open file %s!\n", cmpPath); + return false; + } + int maxPin = max(inC, outC); + float w = 1, h = maxPin * 0.2f; + + char *upperName = (char *)malloc(sizeof(char) * (strlen(name) + 1)); + int i; + for (i = 0; upperName[i] != '\0'; i++) + upperName[i] = toupper(name[i]); + upperName[i + 1] = '\0'; + + fprintf(cmpFile, "%f %f\n", w, h); + fprintf(cmpFile, "L %f,%f %f,%f\n", 0.05f, 0.05f, w - 0.1f, 0.05f); + fprintf(cmpFile, "L %f,%f %f,%f\n", 0.05f, 0.05f, 0.05f, h - 0.1f); + fprintf(cmpFile, "L %f,%f %f,%f\n", 0.05f, h - 0.1f, w - 0.1f, h - 0.1f); + fprintf(cmpFile, "L %f,%f %f,%f\n", w - 0.1f, 0.05f, w - 0.1f, h - 0.1f); + fprintf(cmpFile, "TXT %f,%f %s\n", w / 2, h / 2, upperName); + + fclose(cmpFile); } \ No newline at end of file diff --git a/Save.h b/Save.h index 465179b..d6c3e51 100644 --- a/Save.h +++ b/Save.h @@ -3,6 +3,7 @@ #include #include +#include #include "NodeVector.h" #include "Utility.h" diff --git a/res/Modules/SAVETEST.fun b/res/Modules/SAVETEST.fun new file mode 100644 index 0000000..202e322 --- /dev/null +++ b/res/Modules/SAVETEST.fun @@ -0,0 +1,5 @@ +{W0} = and( +assigns 1 +in 2 +out 1 +wires 1 diff --git a/test.sav b/test.sav new file mode 100644 index 0000000..3987e74 --- /dev/null +++ b/test.sav @@ -0,0 +1,7 @@ +N and 1368.000000,900.000000 +N switch 716.000000,740.000000 +N switch 708.000000,1112.000000 +N led 1940.000000,904.000000 +C 0 0 3 0 +C 1 0 0 0 +C 2 0 0 1