diff --git a/src/main.zig b/src/main.zig index 4feb37d..c5fa292 100644 --- a/src/main.zig +++ b/src/main.zig @@ -5,72 +5,80 @@ pub fn main() !void { defer _ = gpa.deinit(); const allocator = gpa.allocator(); - var input = Signal{ .digital = 1, .analog = 0.5 }; + // var input = Signal{ .digital = 1, .analog = 0.5 }; var circuit = Circuit.init(allocator); defer circuit.deinit(); - var not1 = try circuit.addComponent(Not); - not1.invert_output = false; - not1.component.inputs.items[0] = .{ .signal = &input }; // manually set the input here + // var not1 = try circuit.addComponent(Not); + // not1.invert_output = false; + // not1.component.inputs.items[0] = .{ .signal = &input }; // manually set the input here - var battery1 = try circuit.addComponent(Battery); - var battery2 = try circuit.addComponent(Battery); - var battery3 = try circuit.addComponent(Battery); - battery1.value = -0.5; - battery2.value = 0.5; - battery3.value = -1.0; + // var battery1 = try circuit.addComponent(Battery); + var battery1 = try Battery.init(allocator); + defer battery1.deinit(allocator); + try circuit.components.append(circuit.allocator, battery1.component); + battery1.component = circuit.components.items[0]; + try circuit.source_components.append(circuit.allocator, &battery1.component); + // std.debug.print("{any}\n\n", .{circuit.components.items}); + // std.debug.print("{any}\n\n", .{circuit.source_components.items}); + std.debug.print("{*} == {*}\n\n", .{ &circuit.components.items[0], circuit.source_components.items[0] }); + // var battery2 = try circuit.addComponent(Battery); + // var battery3 = try circuit.addComponent(Battery); + // battery1.value = -0.5; + // battery2.value = 0.5; + // battery3.value = -1.0; - var or1 = try circuit.addComponent(Or); - var or2 = try circuit.addComponent(Or); - or1.arithmetic_mode = true; - or2.arithmetic_mode = true; - try or1.component.setNumInputs(allocator, 3); - try or2.component.setNumInputs(allocator, 3); + // var or1 = try circuit.addComponent(Or); + // var or2 = try circuit.addComponent(Or); + // or1.arithmetic_mode = true; + // or2.arithmetic_mode = true; + // try or1.component.setNumInputs(allocator, 3); + // try or2.component.setNumInputs(allocator, 3); - var or3 = try circuit.addComponent(Or); - var or4 = try circuit.addComponent(Or); - var or5 = try circuit.addComponent(Or); + // var or3 = try circuit.addComponent(Or); + // var or4 = try circuit.addComponent(Or); + // var or5 = try circuit.addComponent(Or); - var or6 = try circuit.addComponent(Or); - var or7 = try circuit.addComponent(Or); - var or8 = try circuit.addComponent(Or); - var and1 = try circuit.addComponent(And); - var and2 = try circuit.addComponent(And); - or6.arithmetic_mode = true; - or7.arithmetic_mode = true; - or8.arithmetic_mode = true; - and1.arithmetic_mode = true; - and2.arithmetic_mode = true; + // var or6 = try circuit.addComponent(Or); + // var or7 = try circuit.addComponent(Or); + // var or8 = try circuit.addComponent(Or); + // var and1 = try circuit.addComponent(And); + // var and2 = try circuit.addComponent(And); + // or6.arithmetic_mode = true; + // or7.arithmetic_mode = true; + // or8.arithmetic_mode = true; + // and1.arithmetic_mode = true; + // and2.arithmetic_mode = true; - battery1.component.connect(0, &or3.component, 1); - battery1.component.connect(0, &or4.component, 1); - battery1.component.connect(0, &or5.component, 1); + // battery1.component.connect(0, &or3.component, 1); + // battery1.component.connect(0, &or4.component, 1); + // battery1.component.connect(0, &or5.component, 1); - battery2.component.connect(0, &or6.component, 0); - battery2.component.connect(0, &or7.component, 0); - battery2.component.connect(0, &or8.component, 0); + // battery2.component.connect(0, &or6.component, 0); + // battery2.component.connect(0, &or7.component, 0); + // battery2.component.connect(0, &or8.component, 0); - battery3.component.connect(0, &and1.component, 0); - battery3.component.connect(0, &and2.component, 0); + // battery3.component.connect(0, &and1.component, 0); + // battery3.component.connect(0, &and2.component, 0); - not1.component.connect(0, &or3.component, 0); - not1.component.connect(0, &or1.component, 0); - not1.component.connect(0, &or1.component, 1); + // not1.component.connect(0, &or3.component, 0); + // not1.component.connect(0, &or1.component, 0); + // not1.component.connect(0, &or1.component, 1); - or1.component.connect(0, &or4.component, 0); - or1.component.connect(0, &or2.component, 0); - or1.component.connect(0, &or2.component, 1); - or2.component.connect(0, &or5.component, 0); + // or1.component.connect(0, &or4.component, 0); + // or1.component.connect(0, &or2.component, 0); + // or1.component.connect(0, &or2.component, 1); + // or2.component.connect(0, &or5.component, 0); - or3.component.connect(0, &or6.component, 1); - or4.component.connect(0, &or7.component, 1); - or5.component.connect(0, &or8.component, 1); + // or3.component.connect(0, &or6.component, 1); + // or4.component.connect(0, &or7.component, 1); + // or5.component.connect(0, &or8.component, 1); - or6.component.connect(0, &and1.component, 1); - and1.component.connect(0, &or1.component, 2); - or7.component.connect(0, &and2.component, 1); - and2.component.connect(0, &or2.component, 2); + // or6.component.connect(0, &and1.component, 1); + // and1.component.connect(0, &or1.component, 2); + // or7.component.connect(0, &and2.component, 1); + // and2.component.connect(0, &or2.component, 2); try circuit.tick(); @@ -127,13 +135,13 @@ pub const Circuit = struct { self.components.deinit(self.allocator); } - pub fn addComponent(self: *Circuit, comptime T: type) !T { - var c = try T.init(self.allocator); - errdefer c.deinit(self.allocator); - try self.components.append(self.allocator, c.component); - if (T == Battery) try self.source_components.append(self.allocator, &c.component); - return c; - } + // pub fn addComponent(self: *Circuit, comptime T: type) !T { + // var c = try T.init(self.allocator); + // errdefer c.deinit(self.allocator); + // try self.components.append(self.allocator, c.component); + // // if (T == Battery) try self.source_components.append(self.allocator, &c.component); + // return c; + // } pub fn tick(self: *Circuit) !void { var process_order_solver = try ProcessOrderSolver.init(self); @@ -162,11 +170,14 @@ pub const Circuit = struct { } pub fn solve(self: *ProcessOrderSolver) ProcessOrder { + // std.debug.print("{any}\n\n", .{self.circuit.source_components.items}); for (self.circuit.source_components.items) |source_component| { var component = source_component; // while (true) blk: { // component.process(); - self.solved[self.componentIndex(component).?] = true; + // std.debug.print("source component {any}\n\n", .{source_component}); + const idx = self.componentIndex(component); + self.solved[idx.?] = true; component = component.outputs.items[0].connection.?; std.debug.print("{any}\n", .{component}); // } @@ -176,9 +187,16 @@ pub const Circuit = struct { fn componentIndex(self: ProcessOrderSolver, component: *Component) ?usize { for (self.circuit.components.items, 0..) |c, i| { - std.debug.print("{any} == {any}\n", .{ component, &c }); + // std.debug.print("{any} == {any}\n", .{ component, &c }); + std.debug.print("{*} == {*}\n", .{ component, &c }); + // std.debug.print("{s}\n{s}\n{any}\n\n", .{ + // std.fmt.fmtSliceHexLower(std.mem.asBytes(component)), + // std.fmt.fmtSliceHexLower(std.mem.asBytes(&c)), + // std.mem.eql(u8, std.mem.asBytes(component), std.mem.asBytes(&c)), + // }); + // if (std.mem.eql(u8, std.mem.asBytes(component), std.mem.asBytes(&c))) { if (component == &c) { - std.debug.print("component index {d}\n", .{i}); + // std.debug.print("component index {d}\n", .{i}); return i; } } @@ -264,13 +282,6 @@ pub const Component = struct { }; const Inputs = std.ArrayListUnmanaged(Input); const Outputs = std.ArrayListUnmanaged(Output); - - // const Connection = struct { - // from: *Component, - // from_idx: usize, - // to: *Component, - // to_idx: usize, - // }; }; pub const Signal = struct {