From b5831ee54e8f6f6127b5cd5992f387e40009d250 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 12 Jun 2024 14:18:06 +0200 Subject: WIP and gate test --- Circuit.cpp | 21 +++++++++++++-------- circuits/and-test.txt | 13 +++++++++++++ main.cpp | 7 +++++-- 3 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 circuits/and-test.txt diff --git a/Circuit.cpp b/Circuit.cpp index 22ea98c..8137939 100644 --- a/Circuit.cpp +++ b/Circuit.cpp @@ -1,4 +1,5 @@ #include "Circuit.h" +#include "Exception.h" #include "NodeFactory.h" void Circuit::create(string label, vector nodes) { @@ -9,29 +10,33 @@ void Circuit::create(string label, vector nodes) { } void Circuit::new_node(string label, string type) { - if (nodes.find(label) != nodes.end()) return; // TODO: exception! + if (nodes.find(label) != nodes.end()) + throw CircuitException("node with label \"%s\" already exists!", label.c_str()); Node * node = NodeFactory::create(type); - if (node == nullptr) return; // TODO: exception? + if (node == nullptr) + throw CircuitException("unknown type \"%s\"", type.c_str()); nodes[label] = node; - // printf("[%s] (%s)\n", label.c_str(), type.c_str()); + printf("[%s] (%s)\n", label.c_str(), type.c_str()); } void Circuit::new_net(string src, vector dests) { Net * net = new Net(); nets.push_back(net); + Node * node = find_node(src); + if (node == nullptr) + throw CircuitException("unknown source node \"%s\"", src.c_str()); + node->setOutput(net); + for (auto dest : dests) { Node * node = find_node(dest); - if (node == nullptr) continue; // TODO: exception! + if (node == nullptr) + throw CircuitException("unknown destination node \"%s\"", dest.c_str()); node->addInput(net); } - - Node * node = find_node(src); - if (node == nullptr) return; // TODO: exception! - node->setOutput(net); } void Circuit::sim() { diff --git a/circuits/and-test.txt b/circuits/and-test.txt new file mode 100644 index 0000000..7ab3d78 --- /dev/null +++ b/circuits/and-test.txt @@ -0,0 +1,13 @@ +# full adder circuit +# vim:ft=cfg +A: INPUT_LOW; +OUT: PROBE; +# OUT: A; +A: OUT; +# B: INPUT_LOW; +# OUT: PROBE; +# TEST: AND; +# A: TEST; +# B: TEST; +# OUT: TEST; + diff --git a/main.cpp b/main.cpp index 728155f..aab566c 100644 --- a/main.cpp +++ b/main.cpp @@ -14,7 +14,7 @@ int main(int argc, char** argv) { main_parser.set_circuit(circuit); - ifstream file("circuits/full-adder.txt"); + ifstream file("circuits/and-test.txt"); try { file >> main_parser; @@ -22,11 +22,14 @@ int main(int argc, char** argv) { } catch (ParserException & e) { cout << "Parser error: " << e.what() << endl; return EXIT_FAILURE; + } catch (CircuitException & e) { + cout << "Circuit error: " << e.what() << endl; + return EXIT_FAILURE; } try { circuit.sim(); - } catch (CircuitException& e) { + } catch (CircuitException & e) { cout << "Circuit error: " << e.what() << endl; return EXIT_FAILURE; } -- cgit v1.2.3