first successful simulation!

This commit is contained in:
Jeeves 2025-02-12 01:04:53 -07:00
parent 00a41cd614
commit 0e05fa0241

View file

@ -26,11 +26,11 @@ pub fn main() !void {
var or6_inputs = [_]*Signal{ &battery2.output, &or3.output }; var or6_inputs = [_]*Signal{ &battery2.output, &or3.output };
var or6 = Or{ .inputs = &or6_inputs, .arithmetic_mode = true }; var or6 = Or{ .inputs = &or6_inputs, .arithmetic_mode = true };
var and1_inputs = [_]*Signal{ &battery3.output, &or6.output }; var and1_inputs = [_]*Signal{ &battery3.output, &or6.output };
var and1 = And{ .inputs = &and1_inputs }; var and1 = And{ .inputs = &and1_inputs, .arithmetic_mode = true };
var or7_inputs = [_]*Signal{ &battery2.output, &or4.output }; var or7_inputs = [_]*Signal{ &battery2.output, &or4.output };
var or7 = Or{ .inputs = &or7_inputs, .arithmetic_mode = true }; var or7 = Or{ .inputs = &or7_inputs, .arithmetic_mode = true };
var and2_inputs = [_]*Signal{ &battery3.output, &or7.output }; var and2_inputs = [_]*Signal{ &battery3.output, &or7.output };
var and2 = And{ .inputs = &and2_inputs }; var and2 = And{ .inputs = &and2_inputs, .arithmetic_mode = true };
var or8_inputs = [_]*Signal{ &battery2.output, &or5.output }; var or8_inputs = [_]*Signal{ &battery2.output, &or5.output };
var or8 = Or{ .inputs = &or8_inputs, .arithmetic_mode = true }; var or8 = Or{ .inputs = &or8_inputs, .arithmetic_mode = true };
@ -51,7 +51,10 @@ pub fn main() !void {
or5.process(); or5.process();
or8.process(); or8.process();
std.debug.print("{}\n{}\n{}\n{}\n", .{ input, or6.output, or7.output, or8.output }); 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 { pub const Component = struct {
@ -150,7 +153,7 @@ pub const Not = struct {
self.output.digital = self.input.digital; self.output.digital = self.input.digital;
self.output.analog = self.input.analog; self.output.analog = self.input.analog;
} }
self.output.analog = std.math.clamp(self.output.analog, 0.0, 1.0); self.output.analog = std.math.clamp(self.output.analog, -1.0, 1.0);
} }
}; };
@ -180,13 +183,34 @@ pub const And = struct {
for (self.inputs[1..]) |input| { for (self.inputs[1..]) |input| {
// self.output.digital = self.output.digital and input.digital; // self.output.digital = self.output.digital and input.digital;
self.output.digital = 0; self.output.digital = 0;
self.output.analog = @min(self.output.analog, input.analog); // self.output.analog = @min(self.output.analog, input.analog);
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?
.gt => input.analog,
};
} }
} }
self.output.analog = std.math.clamp(self.output.analog, 0.0, 1.0); self.output.analog = std.math.clamp(self.output.analog, -1.0, 1.0);
} }
}; };
test "min" {
var a = Signal{ .analog = 0.0 };
var b = Signal{ .analog = 1.0 };
var inputs = [_]*Signal{ &a, &b };
var and1 = And{ .inputs = &inputs };
and1.process();
try std.testing.expectEqual(0.0, and1.output.analog);
a.analog = -0.5;
b.analog = -0.2;
and1.process();
try std.testing.expectEqual(-0.2, and1.output.analog);
}
pub const Or = struct { pub const Or = struct {
// component: Component, // component: Component,
@ -213,9 +237,30 @@ pub const Or = struct {
for (self.inputs[1..]) |input| { for (self.inputs[1..]) |input| {
// self.output.digital = self.output.digital and input.digital; // self.output.digital = self.output.digital and input.digital;
self.output.digital = 0; self.output.digital = 0;
self.output.analog = @max(self.output.analog, input.analog); // std.debug.print("MAX {d} {d}, {any}\n", .{ self.output.analog, input.analog, std.math.order(self.output.analog, input.analog) });
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?
.gt => self.output.analog,
};
} }
} }
self.output.analog = std.math.clamp(self.output.analog, 0.0, 1.0); self.output.analog = std.math.clamp(self.output.analog, -1.0, 1.0);
} }
}; };
test "max" {
var a = Signal{ .analog = 0.0 };
var b = Signal{ .analog = 1.0 };
var inputs = [_]*Signal{ &a, &b };
var or1 = Or{ .inputs = &inputs };
or1.process();
try std.testing.expectEqual(1.0, or1.output.analog);
a.analog = -0.5;
b.analog = -0.2;
or1.process();
try std.testing.expectEqual(-0.5, or1.output.analog);
}