misc
This commit is contained in:
parent
1dedf1eedf
commit
00a41cd614
1 changed files with 110 additions and 164 deletions
274
src/main.zig
274
src/main.zig
|
@ -5,82 +5,53 @@ pub fn main() !void {
|
|||
defer _ = gpa.deinit();
|
||||
// const allocator = gpa.allocator();
|
||||
|
||||
var input = Signal{ .digital = 1, .analog = 1.0 };
|
||||
var input = Signal{ .digital = 1, .analog = 0.5 };
|
||||
|
||||
var not1 = Not{ .input = &input };
|
||||
var not2 = Not{ .input = ¬1.output, .invert_output = false };
|
||||
var not3 = Not{ .input = ¬2.output };
|
||||
var battery1 = Battery{ .value = -0.5 };
|
||||
var battery2 = Battery{ .value = 0.5 };
|
||||
var battery3 = Battery{ .value = -1.0 };
|
||||
|
||||
not1.process();
|
||||
not2.process();
|
||||
not3.process();
|
||||
var or1_inputs = [_]*Signal{ &input, &input, undefined };
|
||||
var or1 = Or{ .inputs = &or1_inputs, .arithmetic_mode = true };
|
||||
var or2_inputs = [_]*Signal{ &or1.output, &or1.output, undefined };
|
||||
var or2 = Or{ .inputs = &or2_inputs, .arithmetic_mode = true };
|
||||
|
||||
std.debug.print("{}\n{}\n{}\n{}\n", .{ input, not1.output, not2.output, not3.output });
|
||||
var or3_inputs = [_]*Signal{ &input, &battery1.output };
|
||||
var or3 = Or{ .inputs = &or3_inputs };
|
||||
var or4_inputs = [_]*Signal{ &or1.output, &battery1.output };
|
||||
var or4 = Or{ .inputs = &or4_inputs };
|
||||
var or5_inputs = [_]*Signal{ &or2.output, &battery1.output };
|
||||
var or5 = Or{ .inputs = &or5_inputs };
|
||||
|
||||
// // analog in
|
||||
// var wire1 = Wire{};
|
||||
// var wire2 = Wire{};
|
||||
// var wire3 = Wire{};
|
||||
var or6_inputs = [_]*Signal{ &battery2.output, &or3.output };
|
||||
var or6 = Or{ .inputs = &or6_inputs, .arithmetic_mode = true };
|
||||
var and1_inputs = [_]*Signal{ &battery3.output, &or6.output };
|
||||
var and1 = And{ .inputs = &and1_inputs };
|
||||
var or7_inputs = [_]*Signal{ &battery2.output, &or4.output };
|
||||
var or7 = Or{ .inputs = &or7_inputs, .arithmetic_mode = true };
|
||||
var and2_inputs = [_]*Signal{ &battery3.output, &or7.output };
|
||||
var and2 = And{ .inputs = &and2_inputs };
|
||||
var or8_inputs = [_]*Signal{ &battery2.output, &or5.output };
|
||||
var or8 = Or{ .inputs = &or8_inputs, .arithmetic_mode = true };
|
||||
|
||||
// // orange battery
|
||||
// var wire4 = Wire{};
|
||||
// var wire5 = Wire{};
|
||||
// var wire6 = Wire{};
|
||||
or1_inputs[2] = &and1.output;
|
||||
or2_inputs[2] = &and2.output;
|
||||
|
||||
// // cblueorange battery
|
||||
// var wire7 = Wire{};
|
||||
// var wire8 = Wire{};
|
||||
// var wire9 = Wire{};
|
||||
battery1.process();
|
||||
battery2.process();
|
||||
battery3.process();
|
||||
or3.process();
|
||||
or6.process();
|
||||
and1.process();
|
||||
or1.process();
|
||||
or4.process();
|
||||
or7.process();
|
||||
and2.process();
|
||||
or2.process();
|
||||
or5.process();
|
||||
or8.process();
|
||||
|
||||
// // red battery
|
||||
// var wire10 = Wire{};
|
||||
// var wire11 = Wire{};
|
||||
|
||||
// // grey OR gates
|
||||
// var wire12 = Wire{};
|
||||
// var wire13 = Wire{};
|
||||
// var wire14 = Wire{};
|
||||
|
||||
// var wire15 = Wire{};
|
||||
|
||||
// // pink OR gates
|
||||
// var wire16 = Wire{};
|
||||
// var wire17 = Wire{};
|
||||
// var wire18 = Wire{};
|
||||
|
||||
// // right side flippity floppities
|
||||
// var wire19 = Wire{};
|
||||
// var wire20 = Wire{};
|
||||
// var wire21 = Wire{};
|
||||
// var wire22 = Wire{};
|
||||
// var wire23 = Wire{};
|
||||
|
||||
// var battery1 = Battery{ .value = -0.5 };
|
||||
// var battery2 = Battery{ .value = 0.5 };
|
||||
// var battery3 = Battery{ .value = -1.0 };
|
||||
|
||||
// var or1_inputs = [_]*Wire{ &wire1, &wire2, &wire3 };
|
||||
// var or1 = Or{ .inputs = &or1_inputs };
|
||||
// var or2_inputs = [_]*Wire{ &};
|
||||
// var or2 = Or{ .inputs = &or2_inputs };
|
||||
|
||||
// var or3_inputs = [_]*Wire{};
|
||||
// var or3 = Or{ .inputs = &or3_inputs };
|
||||
// var or4_inputs = [_]*Wire{};
|
||||
// var or4 = Or{ .inputs = &or4_inputs };
|
||||
// var or5_inputs = [_]*Wire{};
|
||||
// var or5 = Or{ .inputs = &or5_inputs };
|
||||
|
||||
// var or6_inputs = [_]*Wire{};
|
||||
// var or6 = Or{ .inputs = &or6_inputs };
|
||||
// var and1 = And{};
|
||||
// var or7_inputs = [_]*Wire{};
|
||||
// var or7 = Or{ .inputs = &or7_inputs };
|
||||
// var and2 = And{};
|
||||
// var or8_inputs = [_]*Wire{};
|
||||
// var or8 = Or{ .inputs = &or8_inputs };
|
||||
|
||||
// std.debug.print("{}\n", .{wire3});
|
||||
std.debug.print("{}\n{}\n{}\n{}\n", .{ input, or6.output, or7.output, or8.output });
|
||||
}
|
||||
|
||||
pub const Component = struct {
|
||||
|
@ -128,36 +99,22 @@ pub const Circuit = struct {
|
|||
};
|
||||
|
||||
pub const Battery = struct {
|
||||
component: Component = .{ .processFn = process },
|
||||
// component: Component = .{ .processFn = process },
|
||||
value: f32,
|
||||
|
||||
pub fn process(component: *Component, inputs: []*Signal) []Signal {
|
||||
_ = inputs;
|
||||
const self: *Battery = @fieldParentPtr("component", component);
|
||||
return &[_]Signal{.{
|
||||
.digital = std.math.sign(self.value),
|
||||
.analog = self.value,
|
||||
}};
|
||||
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 Wire = struct {
|
||||
// digital: bool = false,
|
||||
// analog: f32 = 0.0,
|
||||
|
||||
// pub fn format(self: Wire, comptime fmt: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void {
|
||||
// _ = .{ fmt, options };
|
||||
// try writer.print("Wire({s} | {d:0>1.4})", .{
|
||||
// if (self.digital) "1" else "0",
|
||||
// self.analog,
|
||||
// });
|
||||
// }
|
||||
|
||||
// fn analogSet(self: *Wire, value: f32) void {
|
||||
// self.analog = std.math.clamp(value, 0.0, 1.0);
|
||||
// }
|
||||
// };
|
||||
|
||||
// pub const Battery = struct {
|
||||
// // component: Component = .{},
|
||||
|
||||
|
@ -187,7 +144,7 @@ pub const Not = struct {
|
|||
|
||||
pub fn process(self: *Not) void {
|
||||
if (self.invert_output) {
|
||||
self.output.digital = -self.input.digital;
|
||||
self.output.digital = 1 - @as(i2, @intCast(@abs(self.input.digital)));
|
||||
self.output.analog = 1.0 - @abs(self.input.analog);
|
||||
} else {
|
||||
self.output.digital = self.input.digital;
|
||||
|
@ -197,79 +154,68 @@ pub const Not = struct {
|
|||
}
|
||||
};
|
||||
|
||||
// pub const And = struct {
|
||||
// // component: Component,
|
||||
pub const And = struct {
|
||||
// component: Component,
|
||||
|
||||
// inputs: []*Wire,
|
||||
// output: *Wire,
|
||||
inputs: []*Signal,
|
||||
output: Signal = .{},
|
||||
|
||||
// // if false, is in Minimum Input mode
|
||||
// // if true, is in Multiply Inputs mode
|
||||
// arithmetic_mode: bool = false,
|
||||
// if false, is in Minimum Input mode
|
||||
// if true, is in Multiply Inputs mode
|
||||
arithmetic_mode: bool = false,
|
||||
|
||||
// // TODO check implementation
|
||||
// pub fn process(self: *And) void {
|
||||
// if (self.arithmetic_mode) {
|
||||
// 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.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.analog = @min(self.output.analog, input.analog);
|
||||
// }
|
||||
// }
|
||||
// self.output.analog = std.math.clamp(self.output.analog, 0.0, 1.0);
|
||||
// // var digital = self.inputs[0].digital;
|
||||
// // if (digital) for (1..self.inputs.len - 1) |i| if (!self.inputs[i].digital) {
|
||||
// // digital = false;
|
||||
// // break;
|
||||
// // };
|
||||
// // self.output.digital = digital;
|
||||
// TODO check implementation
|
||||
pub fn process(self: *And) void {
|
||||
if (self.arithmetic_mode) {
|
||||
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 *= 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.analog = std.math.clamp(self.output.analog, 0.0, 1.0);
|
||||
}
|
||||
};
|
||||
|
||||
// // var best = self.inputs[0].analog;
|
||||
// // for (self.inputs[1..]) |input| best = @min(best, input.analog);
|
||||
// // self.output.analog = best;
|
||||
// }
|
||||
// };
|
||||
pub const Or = struct {
|
||||
// component: Component,
|
||||
|
||||
// pub const Or = struct {
|
||||
// // component: Component,
|
||||
inputs: []*Signal,
|
||||
output: Signal = .{},
|
||||
|
||||
// inputs: []*Wire,
|
||||
// output: *Wire,
|
||||
// if false, is in Maximum Input mode
|
||||
// if true, is in Add Inputs mode
|
||||
arithmetic_mode: bool = false,
|
||||
|
||||
// // if false, is in Maximum Input mode
|
||||
// // if true, is in Add Inputs mode
|
||||
// arithmetic_mode: bool = false,
|
||||
|
||||
// // TODO check implementation
|
||||
// pub fn process(self: *Or) void {
|
||||
// if (self.arithmetic_mode) {
|
||||
// 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.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.analog = @max(self.output.analog, input.analog);
|
||||
// }
|
||||
// }
|
||||
// self.output.analog = std.math.clamp(self.output.analog, 0.0, 1.0);
|
||||
// // self.output.digital = 1;
|
||||
// // for (self.inputs) |input| if (input.digital) {
|
||||
// // self.output.digital = true;
|
||||
// // break;
|
||||
// // };
|
||||
// }
|
||||
// };
|
||||
// TODO check implementation
|
||||
pub fn process(self: *Or) void {
|
||||
if (self.arithmetic_mode) {
|
||||
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 += 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 = @max(self.output.analog, input.analog);
|
||||
}
|
||||
}
|
||||
self.output.analog = std.math.clamp(self.output.analog, 0.0, 1.0);
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue