This commit is contained in:
Jeeves 2025-02-12 01:37:16 -07:00
parent 0e05fa0241
commit 33e730a5a8

View file

@ -1,10 +1,6 @@
const std = @import("std");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
// const allocator = gpa.allocator();
var input = Signal{ .digital = 1, .analog = 0.5 };
var battery1 = Battery{ .value = -0.5 };
@ -51,19 +47,12 @@ pub fn main() !void {
or5.process();
or8.process();
std.debug.print("Input:\n{}\n\n", .{input});
std.debug.print("{}\n{}\n\n", .{ or1.output, or2.output });
std.debug.print("{}\n{}\n{}\n\n", .{ or3.output, or4.output, or5.output });
std.debug.print("{}\n{}\n{}\n{}\n{}\n\n", .{ or6.output, and1.output, or7.output, and2.output, or8.output });
// std.debug.print("{}\n{}\n{}\n{}\n", .{ input, or6.output, or7.output, or8.output });
}
pub const Component = struct {
x: u16 = 0,
y: u16 = 0,
processFn: *const fn (*Component, []*Signal) []Signal,
};
pub const Signal = struct {
digital: i2 = 0,
analog: f32 = 0.0,
@ -85,61 +74,18 @@ pub const Signal = struct {
}
};
pub const Circuit = struct {
width: u16 = 8,
height: u16 = 8,
components: std.ArrayList(Component),
pub fn init(allocator: std.mem.Allocator) Circuit {
return .{
.components = std.ArrayList(Component).init(allocator),
};
}
pub fn deinit(self: *Circuit) void {
self.components.deinit();
}
};
pub const Battery = struct {
// component: Component = .{ .processFn = process },
value: f32,
output: Signal = .{},
pub fn process(self: *Battery) void {
// const self: *Battery = @fieldParentPtr("component", component);
// return &[_]Signal{.{
// .digital = std.math.sign(self.value),
// .analog = self.value,
// }};
self.output.digital = @intFromFloat(std.math.sign(self.value));
self.output.analog = self.value;
}
};
// pub const Battery = struct {
// // component: Component = .{},
// output: *Wire,
// value: f32,
// invert_output: bool = false,
// pub fn process(self: *Battery) void {
// if (self.invert_output) {
// self.output.digital = self.value != 0.0;
// self.output.analogSet(self.value);
// } else {
// self.output.digital = self.value == 0.0;
// self.output.analogSet(1.0 - @abs(self.value));
// }
// }
// };
pub const Not = struct {
// component: Component = .{},
input: *Signal,
output: Signal = .{},
@ -158,8 +104,6 @@ pub const Not = struct {
};
pub const And = struct {
// component: Component,
inputs: []*Signal,
output: Signal = .{},
@ -173,17 +117,14 @@ pub const And = struct {
self.output.digital = self.inputs[0].digital;
self.output.analog = self.inputs[0].analog;
for (self.inputs[1..]) |input| {
// self.output.digital = self.output.digital and input.digital;
self.output.digital = 0;
self.output.digital = 0; // TODO
self.output.analog *= input.analog;
}
} else {
self.output.digital = self.inputs[0].digital;
self.output.analog = self.inputs[0].analog;
for (self.inputs[1..]) |input| {
// self.output.digital = self.output.digital and input.digital;
self.output.digital = 0;
// self.output.analog = @min(self.output.analog, input.analog);
self.output.digital = 0; // TODO
self.output.analog = switch (std.math.order(@abs(self.output.analog), @abs(input.analog))) {
.lt => self.output.analog,
.eq => @min(self.output.analog, input.analog), // TODO what does this *actually* do?
@ -212,8 +153,6 @@ test "min" {
}
pub const Or = struct {
// component: Component,
inputs: []*Signal,
output: Signal = .{},
@ -227,17 +166,14 @@ pub const Or = struct {
self.output.digital = self.inputs[0].digital;
self.output.analog = self.inputs[0].analog;
for (self.inputs[1..]) |input| {
// self.output.digital = self.output.digital and input.digital;
self.output.digital = 0;
self.output.digital = 0; // TODO
self.output.analog += input.analog;
}
} else {
self.output.digital = self.inputs[0].digital;
self.output.analog = self.inputs[0].analog;
for (self.inputs[1..]) |input| {
// self.output.digital = self.output.digital and input.digital;
self.output.digital = 0;
// std.debug.print("MAX {d} {d}, {any}\n", .{ self.output.analog, input.analog, std.math.order(self.output.analog, input.analog) });
self.output.digital = 0; // TODO
self.output.analog = switch (std.math.order(@abs(self.output.analog), @abs(input.analog))) {
.lt => input.analog,
.eq => @max(self.output.analog, input.analog), // TODO what does this *actually* do?