This commit is contained in:
Jeeves 2025-03-03 18:38:36 -07:00
parent 82537b90ba
commit e7a2656d32

View file

@ -5,80 +5,72 @@ 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 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 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 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.resizeInputs(allocator, 3);
try or2.component.resizeInputs(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();
@ -119,13 +111,13 @@ pub fn main() !void {
pub const Circuit = struct {
allocator: std.mem.Allocator,
components: Components,
source_components: SourceComponents,
source_components: Components,
pub fn init(allocator: std.mem.Allocator) Circuit {
return .{
.allocator = allocator,
.components = Components.empty,
.source_components = SourceComponents.empty,
.source_components = Components.empty,
};
}
@ -135,13 +127,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);
@ -150,8 +142,7 @@ pub const Circuit = struct {
_ = process_order;
}
const Components = std.ArrayListUnmanaged(Component);
const SourceComponents = std.ArrayListUnmanaged(*Component);
const Components = std.ArrayListUnmanaged(*Component);
const ProcessOrder = []*Component;
const ProcessOrderSolver = struct {
@ -170,7 +161,6 @@ 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: {
@ -178,30 +168,29 @@ pub const Circuit = struct {
// 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});
component = component.outputs.items[0].connection.?;
// }
}
return &[_]*Component{};
}
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("{*} == {*}\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});
return i;
}
}
return null;
// return std.mem.indexOfScalar(Component, self.circuit.components.items, component.*);
// for (self.circuit.components.items, 0..) |c, i| {
// // 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) {
// return i;
// }
// }
// return null;
return std.mem.indexOfScalar(*Component, self.circuit.components.items, component);
}
};
};
@ -243,7 +232,7 @@ pub const Component = struct {
// TODO allow inserting the new elements at an arbitrary index
// TODO ensure this won't break the opposide side's connections
pub fn setNumInputs(self: *Component, allocator: std.mem.Allocator, new_len: usize) !void {
pub fn resizeInputs(self: *Component, allocator: std.mem.Allocator, new_len: usize) !void {
const old_len = self.inputs.items.len;
try self.inputs.resize(allocator, new_len);
if (new_len > old_len) for (old_len..new_len) |i| {
@ -252,9 +241,8 @@ pub const Component = struct {
}
// TODO allow inserting the new elements at an arbitrary index
// TODO is this allocating the new output Signals on the stack or dumbthing?
// TODO ensure this won't break the opposide side's connections
pub fn setNumOutputs(self: *Component, allocator: std.mem.Allocator, new_len: usize) !void {
pub fn resizeOutputs(self: *Component, allocator: std.mem.Allocator, new_len: usize) !void {
const old_len = self.outputs.items.len;
try self.outputs.resize(allocator, new_len);
if (new_len > old_len) for (old_len..new_len) |i| {