Skip to content

Commit

Permalink
More work on the modulization
Browse files Browse the repository at this point in the history
  • Loading branch information
gorbit99 committed Nov 23, 2019
1 parent d594b60 commit f0086f9
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 40 deletions.
13 changes: 13 additions & 0 deletions NodeVector.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down
168 changes: 128 additions & 40 deletions Save.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
1 change: 1 addition & 0 deletions Save.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include "NodeVector.h"
#include "Utility.h"

Expand Down
5 changes: 5 additions & 0 deletions res/Modules/SAVETEST.fun
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{W0} = and(
assigns 1
in 2
out 1
wires 1
7 changes: 7 additions & 0 deletions test.sav
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f0086f9

Please sign in to comment.