Skip to content

Commit

Permalink
Place waypoints on wire midpoints rather than junctions, allow comman…
Browse files Browse the repository at this point in the history
…d line digital import
  • Loading branch information
rj45 committed Jul 2, 2024
1 parent 04f6eb2 commit e526c58
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 27 deletions.
41 changes: 27 additions & 14 deletions src/import/digital.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ typedef struct IVec2 {
typedef struct WireEnd {
IVec2 pos;
bool visited;
enum { IN_PORT, OUT_PORT, WIRE, WAYPOINT } type;
enum { IN_PORT, OUT_PORT, WIRE, JUNCTION } type;
union {
PortRef portRef;
IVec2 farSide;
};
IVec2 midpoint;
} WireEnd;

typedef struct DigWire {
Expand Down Expand Up @@ -184,7 +185,7 @@ ID find_symbol_kind(Circuit *circ, const char *name) {
return NO_ID;
}

void import_digital(Circuit *circ, char *buffer) {
bool import_digital(Circuit *circ, char *buffer) {
arr(uint32_t) stack = 0;
arr(WireEnd) inPorts = 0;
arr(WireEnd) outPorts = 0;
Expand All @@ -200,7 +201,7 @@ void import_digital(Circuit *circ, char *buffer) {
LXMLDocument doc;
if (!LXMLDocument_load_memory(&doc, buffer)) {
log_error("Failed to load XML");
return;
return false;
}

LXMLNode *root = doc.root;
Expand Down Expand Up @@ -257,17 +258,24 @@ void import_digital(Circuit *circ, char *buffer) {
}
}

IVec2 midpoint = {
.x = (positions[0].x + positions[1].x) / 2,
.y = (positions[0].y + positions[1].y) / 2,
};

DigWire digWire = {
.valid = true,
.ends =
{
{
.pos = positions[0],
.type = WIRE,
.midpoint = midpoint,
},
{
.pos = positions[1],
.type = WIRE,
.midpoint = midpoint,
},
},
};
Expand Down Expand Up @@ -553,7 +561,7 @@ void import_digital(Circuit *circ, char *buffer) {
if (
digWire->ends[k].pos.x == digWireEnds[i].key.x &&
digWire->ends[k].pos.y == digWireEnds[i].key.y) {
digWire->ends[k].type = WAYPOINT;
digWire->ends[k].type = JUNCTION;
}
}
}
Expand Down Expand Up @@ -596,12 +604,12 @@ void import_digital(Circuit *circ, char *buffer) {
break;
case WIRE:
break;
case WAYPOINT: {
case JUNCTION: {
bool found = false;
for (int l = 0; l < arrlen(waypoints); l++) {
if (
waypoints[l].pos.x == end->pos.x &&
waypoints[l].pos.y == end->pos.y) {
waypoints[l].pos.x == end->midpoint.x &&
waypoints[l].pos.y == end->midpoint.y) {
found = true;
break;
}
Expand All @@ -612,16 +620,15 @@ void import_digital(Circuit *circ, char *buffer) {
if (otherEnd->type == IN_PORT || otherEnd->type == OUT_PORT) {
ref = otherEnd->portRef;
log_debug(
"Waypoint at %d, %d belongs to {%x %x}", end->pos.x, end->pos.y,
ref.symbol, ref.port);
"Waypoint at %d, %d belongs to {%x %x}", end->midpoint.x,
end->midpoint.y, ref.symbol, ref.port);
} else {
log_debug(
"Waypoint at %d, %d has no port", end->pos.x, end->pos.y);
// TODO: pick the closest endpoint and attach the waypoint to it.
// Ideally we would put these waypoints on the root wire, but we
// don't know which wire that will be yet.
"Waypoint at %d, %d has no port", end->midpoint.x,
end->midpoint.y);
}
arrput(waypoints, ((WireEnd){.pos = end->pos, .portRef = ref}));
arrput(
waypoints, ((WireEnd){.pos = end->midpoint, .portRef = ref}));
}
break;
}
Expand Down Expand Up @@ -713,7 +720,12 @@ void import_digital(Circuit *circ, char *buffer) {
arrsetlen(netWires, 0);
}

bool ret = true;

goto pass;
fail:
ret = false;
pass:
arrfree(stack);
arrfree(inPorts);
arrfree(outPorts);
Expand All @@ -725,4 +737,5 @@ void import_digital(Circuit *circ, char *buffer) {
}
hmfree(digWireEnds);
LXMLDocument_free(&doc);
return ret;
}
2 changes: 1 addition & 1 deletion src/import/import.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@

#include "core/core.h"

void import_digital(Circuit *circ, char *buffer);
bool import_digital(Circuit *circ, char *buffer);

#endif // IMPORT_H
8 changes: 5 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ static void init(void *user_data) {

app->pzoom = draw_get_zoom(&app->draw);

if (app->filename) {
ui_import(&app->ui, app->filename);
}

log_info("initialization complete, entering main loop");
}

Expand Down Expand Up @@ -390,9 +394,7 @@ sapp_desc sokol_main(int argc, char *argv[]) {
#endif

my_app_t *app = malloc(sizeof(my_app_t));
*app = (my_app_t){
.filename = "testdata/alu_1bit_2gatemux.dig",
};
*app = (my_app_t){0};

if (argc > 1) {
app->filename = argv[1];
Expand Down
43 changes: 34 additions & 9 deletions src/ui/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,24 @@ ui_about(CircuitUI *ui, struct nk_context *ctx, float width, float height) {
}
}

static void ui_load_example(CircuitUI *ui, const char *filename) {
static bool ui_import_data(CircuitUI *ui, char *data) {
ui_reset(ui);

if (!import_digital(&ui->ux.view.circuit, data)) {
return false;
}

ux_route(&ui->ux);
ux_build_bvh(&ui->ux);

// autoroute_dump_anchor_boxes(ui->circuit.ux.router);

ui->showIntro = false;

return true;
}

static bool ui_load_example(CircuitUI *ui, const char *filename) {
assetsys_file_t file;
assetsys_file(ui->assetsys, filename, &file);
int fileSize = assetsys_file_size(ui->assetsys, file);
Expand All @@ -273,18 +290,26 @@ static void ui_load_example(CircuitUI *ui, const char *filename) {

log_info("Loading file %s, %d bytes\n", filename, fileSize);

ui_reset(ui);

import_digital(&ui->ux.view.circuit, buffer);
return ui_import_data(ui, buffer);
}

free(buffer);
bool ui_import(CircuitUI *ui, const char *filename) {
FILE *fp = fopen(filename, "r");
if (!fp) {
return false;
}

ux_route(&ui->ux);
ux_build_bvh(&ui->ux);
fseek(fp, 0, SEEK_END);
int fileSize = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *buffer = malloc(fileSize + 1);
fread(buffer, 1, fileSize, fp);
buffer[fileSize] = 0;
fclose(fp);

// autoroute_dump_anchor_boxes(ui->circuit.ux.router);
log_info("Loading file %s, %d bytes\n", filename, fileSize);

ui->showIntro = false;
return ui_import_data(ui, buffer);
}

static void ui_intro_dialog(
Expand Down
1 change: 1 addition & 0 deletions src/ui/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,6 @@ void ui_draw(CircuitUI *ui);
bool ui_background_save(CircuitUI *ui, const char *filename, bool skipWhenBusy);
void ui_reset(CircuitUI *ui);
void ui_set_scale(CircuitUI *ui, int scale);
bool ui_import(CircuitUI *ui, const char *filename);

#endif // UI_H

0 comments on commit e526c58

Please sign in to comment.