this is a Mess

This commit is contained in:
Jeeves 2025-02-13 03:39:56 -07:00
parent 579fe85f02
commit 82537b90ba

View file

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