finish adding getInput/Output
This commit is contained in:
parent
1e1033a118
commit
222a89addf
1 changed files with 12 additions and 23 deletions
35
src/main.zig
35
src/main.zig
|
@ -11,17 +11,6 @@ pub fn main() !void {
|
|||
|
||||
var circuit = Circuit.init(allocator);
|
||||
defer circuit.deinit();
|
||||
|
||||
var microchip = try circuit.addComponent(Microchip);
|
||||
try microchip.component.resizeOutputs(allocator, 1);
|
||||
|
||||
var not1 = try microchip.circuit.addComponent(Not);
|
||||
var not2 = try microchip.circuit.addComponent(Not);
|
||||
not2.invert_output = false;
|
||||
try microchip.circuit.connectComponents(¬1.component, 0, ¬2.component, 0);
|
||||
try microchip.circuit.connectComponents(¬2.component, 0, ¬1.component, 0);
|
||||
|
||||
try microchip.connectOutput(¬2.component, 0, 0);
|
||||
}
|
||||
|
||||
test "basic circuit" {
|
||||
|
@ -32,7 +21,7 @@ test "basic circuit" {
|
|||
|
||||
var not1 = try circuit.addComponent(Not);
|
||||
not1.invert_output = false;
|
||||
not1.component.inputs.items[0] = .{ .signal = &input }; // manually set the input here
|
||||
not1.component.inputs.items[0].signal = &input; // manually set the input here
|
||||
|
||||
var battery1 = try circuit.addComponent(Battery);
|
||||
var battery2 = try circuit.addComponent(Battery);
|
||||
|
@ -94,18 +83,18 @@ test "basic circuit" {
|
|||
|
||||
try circuit.tick();
|
||||
|
||||
try std.testing.expectEqual(Signal{ .analog = 0.0 }, or1.component.outputs.items[0].signal);
|
||||
try std.testing.expectEqual(Signal{ .analog = 0.0 }, or2.component.outputs.items[0].signal);
|
||||
try std.testing.expectEqual(Signal{ .analog = 0.0 }, or1.component.getOutput(0).*);
|
||||
try std.testing.expectEqual(Signal{ .analog = 0.0 }, or2.component.getOutput(0).*);
|
||||
|
||||
try std.testing.expectEqual(Signal{ .analog = 0.5 }, or3.component.outputs.items[0].signal);
|
||||
try std.testing.expectEqual(Signal{ .analog = -0.5 }, or4.component.outputs.items[0].signal);
|
||||
try std.testing.expectEqual(Signal{ .analog = -0.5 }, or5.component.outputs.items[0].signal);
|
||||
try std.testing.expectEqual(Signal{ .analog = 0.5 }, or3.component.getOutput(0).*);
|
||||
try std.testing.expectEqual(Signal{ .analog = -0.5 }, or4.component.getOutput(0).*);
|
||||
try std.testing.expectEqual(Signal{ .analog = -0.5 }, or5.component.getOutput(0).*);
|
||||
|
||||
try std.testing.expectEqual(Signal{ .analog = 1.0 }, or6.component.outputs.items[0].signal);
|
||||
try std.testing.expectEqual(Signal{ .analog = -1.0 }, and1.component.outputs.items[0].signal);
|
||||
try std.testing.expectEqual(Signal{ .analog = 0.0 }, or7.component.outputs.items[0].signal);
|
||||
try std.testing.expectEqual(Signal{ .analog = -0.0 }, and2.component.outputs.items[0].signal);
|
||||
try std.testing.expectEqual(Signal{ .analog = 0.0 }, or8.component.outputs.items[0].signal);
|
||||
try std.testing.expectEqual(Signal{ .analog = 1.0 }, or6.component.getOutput(0).*);
|
||||
try std.testing.expectEqual(Signal{ .analog = -1.0 }, and1.component.getOutput(0).*);
|
||||
try std.testing.expectEqual(Signal{ .analog = 0.0 }, or7.component.getOutput(0).*);
|
||||
try std.testing.expectEqual(Signal{ .analog = -0.0 }, and2.component.getOutput(0).*);
|
||||
try std.testing.expectEqual(Signal{ .analog = 0.0 }, or8.component.getOutput(0).*);
|
||||
}
|
||||
|
||||
pub const Circuit = struct {
|
||||
|
@ -334,7 +323,7 @@ pub const Component = struct {
|
|||
|
||||
fn connect(self: *Component, allocator: std.mem.Allocator, self_idx: usize, to: *Component, to_idx: usize) !void {
|
||||
const input = &to.inputs.items[to_idx];
|
||||
input.signal = &self.outputs.items[self_idx].signal;
|
||||
input.signal = self.getOutput(self_idx);
|
||||
input.connection = .{
|
||||
.component = self,
|
||||
.idx = self_idx,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue