circuit implementation
This commit is contained in:
parent
900c2c0b12
commit
8eb7a769b3
1 changed files with 47 additions and 28 deletions
75
src/main.zig
75
src/main.zig
|
@ -7,47 +7,36 @@ pub fn main() !void {
|
||||||
|
|
||||||
var input = Signal{ .digital = 1, .analog = 0.5 };
|
var input = Signal{ .digital = 1, .analog = 0.5 };
|
||||||
|
|
||||||
var not1 = try Not.init(allocator);
|
var circuit = Circuit.init(allocator);
|
||||||
defer not1.deinit(allocator);
|
defer circuit.deinit();
|
||||||
|
|
||||||
|
var not1 = try circuit.addComponent(Not);
|
||||||
not1.invert_output = false;
|
not1.invert_output = false;
|
||||||
not1.component.inputs.items[0] = &input; // manually set the input here
|
not1.component.inputs.items[0] = &input; // manually set the input here
|
||||||
|
|
||||||
var battery1 = try Battery.init(allocator);
|
var battery1 = try circuit.addComponent(Battery);
|
||||||
defer battery1.deinit(allocator);
|
var battery2 = try circuit.addComponent(Battery);
|
||||||
var battery2 = try Battery.init(allocator);
|
var battery3 = try circuit.addComponent(Battery);
|
||||||
defer battery2.deinit(allocator);
|
|
||||||
var battery3 = try Battery.init(allocator);
|
|
||||||
defer battery3.deinit(allocator);
|
|
||||||
battery1.value = -0.5;
|
battery1.value = -0.5;
|
||||||
battery2.value = 0.5;
|
battery2.value = 0.5;
|
||||||
battery3.value = -1.0;
|
battery3.value = -1.0;
|
||||||
|
|
||||||
var or1 = try Or.init(allocator);
|
var or1 = try circuit.addComponent(Or);
|
||||||
defer or1.deinit(allocator);
|
var or2 = try circuit.addComponent(Or);
|
||||||
var or2 = try Or.init(allocator);
|
|
||||||
defer or2.deinit(allocator);
|
|
||||||
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 Or.init(allocator);
|
var or3 = try circuit.addComponent(Or);
|
||||||
defer or3.deinit(allocator);
|
var or4 = try circuit.addComponent(Or);
|
||||||
var or4 = try Or.init(allocator);
|
var or5 = try circuit.addComponent(Or);
|
||||||
defer or4.deinit(allocator);
|
|
||||||
var or5 = try Or.init(allocator);
|
|
||||||
defer or5.deinit(allocator);
|
|
||||||
|
|
||||||
var or6 = try Or.init(allocator);
|
var or6 = try circuit.addComponent(Or);
|
||||||
defer or6.deinit(allocator);
|
var or7 = try circuit.addComponent(Or);
|
||||||
var or7 = try Or.init(allocator);
|
var or8 = try circuit.addComponent(Or);
|
||||||
defer or7.deinit(allocator);
|
var and1 = try circuit.addComponent(And);
|
||||||
var or8 = try Or.init(allocator);
|
var and2 = try circuit.addComponent(And);
|
||||||
defer or8.deinit(allocator);
|
|
||||||
var and1 = try And.init(allocator);
|
|
||||||
defer and1.deinit(allocator);
|
|
||||||
var and2 = try And.init(allocator);
|
|
||||||
defer and2.deinit(allocator);
|
|
||||||
or6.arithmetic_mode = true;
|
or6.arithmetic_mode = true;
|
||||||
or7.arithmetic_mode = true;
|
or7.arithmetic_mode = true;
|
||||||
or8.arithmetic_mode = true;
|
or8.arithmetic_mode = true;
|
||||||
|
@ -104,6 +93,36 @@ pub fn main() !void {
|
||||||
std.debug.print("{}\n{}\n{}\n{}\n{}\n\n", .{ or6.component.outputs.items[0], and1.component.outputs.items[0], or7.component.outputs.items[0], and2.component.outputs.items[0], or8.component.outputs.items[0] });
|
std.debug.print("{}\n{}\n{}\n{}\n{}\n\n", .{ or6.component.outputs.items[0], and1.component.outputs.items[0], or7.component.outputs.items[0], and2.component.outputs.items[0], or8.component.outputs.items[0] });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const Circuit = struct {
|
||||||
|
allocator: std.mem.Allocator,
|
||||||
|
components: Components,
|
||||||
|
source_components: SourceComponents,
|
||||||
|
|
||||||
|
pub fn init(allocator: std.mem.Allocator) Circuit {
|
||||||
|
return .{
|
||||||
|
.allocator = allocator,
|
||||||
|
.components = Components.empty,
|
||||||
|
.source_components = SourceComponents.empty,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deinit(self: *Circuit) void {
|
||||||
|
self.source_components.deinit(self.allocator);
|
||||||
|
for (0..self.components.items.len) |i| self.components.items[i].deinit(self.allocator);
|
||||||
|
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);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Components = std.ArrayListUnmanaged(Component);
|
||||||
|
const SourceComponents = std.ArrayListUnmanaged(*Component);
|
||||||
|
};
|
||||||
|
|
||||||
var null_signal = Signal{};
|
var null_signal = Signal{};
|
||||||
|
|
||||||
pub const Component = struct {
|
pub const Component = struct {
|
||||||
|
|
Loading…
Add table
Reference in a new issue