start of Microchip
This commit is contained in:
parent
38e140a16e
commit
4e7c8a8989
1 changed files with 51 additions and 21 deletions
72
src/main.zig
72
src/main.zig
|
@ -12,19 +12,16 @@ pub fn main() !void {
|
||||||
var circuit = Circuit.init(allocator);
|
var circuit = Circuit.init(allocator);
|
||||||
defer circuit.deinit();
|
defer circuit.deinit();
|
||||||
|
|
||||||
var not1 = try circuit.addComponent(Not);
|
var microchip = try circuit.addComponent(Microchip);
|
||||||
var not2 = try circuit.addComponent(Not);
|
try microchip.component.resizeOutputs(allocator, 1);
|
||||||
not2.invert_output = false;
|
|
||||||
try circuit.connectComponents(¬1.component, 0, ¬2.component, 0);
|
|
||||||
try circuit.connectComponents(¬2.component, 0, ¬1.component, 0);
|
|
||||||
|
|
||||||
std.debug.print("before {}\n", .{not2.component.outputs.items[0].signal});
|
var not1 = try microchip.circuit.addComponent(Not);
|
||||||
try circuit.tick();
|
var not2 = try microchip.circuit.addComponent(Not);
|
||||||
std.debug.print("after {}\n", .{not2.component.outputs.items[0].signal});
|
not2.invert_output = false;
|
||||||
try circuit.tick();
|
try microchip.circuit.connectComponents(¬1.component, 0, ¬2.component, 0);
|
||||||
std.debug.print("after {}\n", .{not2.component.outputs.items[0].signal});
|
try microchip.circuit.connectComponents(¬2.component, 0, ¬1.component, 0);
|
||||||
try circuit.tick();
|
|
||||||
std.debug.print("after {}\n", .{not2.component.outputs.items[0].signal});
|
try microchip.connectOutput(¬2.component, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
test "basic circuit" {
|
test "basic circuit" {
|
||||||
|
@ -150,7 +147,7 @@ pub const Circuit = struct {
|
||||||
if (self.update_order == null) try self.calculateUpdateOrder();
|
if (self.update_order == null) try self.calculateUpdateOrder();
|
||||||
for (self.update_order.?) |component| {
|
for (self.update_order.?) |component| {
|
||||||
std.log.debug("updating {}@{d}", .{ component, self.componentIndex(component).? });
|
std.log.debug("updating {}@{d}", .{ component, self.componentIndex(component).? });
|
||||||
component.process();
|
try component.process();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -244,7 +241,7 @@ pub const Component = struct {
|
||||||
inputs: Inputs,
|
inputs: Inputs,
|
||||||
outputs: Outputs,
|
outputs: Outputs,
|
||||||
|
|
||||||
processFn: *const fn (*Component) void,
|
processFn: *const fn (*Component) AllocatorError!void,
|
||||||
deinitFn: *const fn (*Component, std.mem.Allocator) void,
|
deinitFn: *const fn (*Component, std.mem.Allocator) void,
|
||||||
|
|
||||||
pub const Input = struct {
|
pub const Input = struct {
|
||||||
|
@ -273,7 +270,7 @@ pub const Component = struct {
|
||||||
name: []const u8,
|
name: []const u8,
|
||||||
inputs_len: usize,
|
inputs_len: usize,
|
||||||
outputs_len: usize,
|
outputs_len: usize,
|
||||||
processFn: *const fn (*Component) void,
|
processFn: *const fn (*Component) AllocatorError!void,
|
||||||
deinitFn: *const fn (*Component, std.mem.Allocator) void,
|
deinitFn: *const fn (*Component, std.mem.Allocator) void,
|
||||||
) !void {
|
) !void {
|
||||||
var inputs = Inputs.empty;
|
var inputs = Inputs.empty;
|
||||||
|
@ -301,8 +298,8 @@ pub const Component = struct {
|
||||||
self.deinitFn(self, allocator);
|
self.deinitFn(self, allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub inline fn process(self: *Component) void {
|
pub inline fn process(self: *Component) AllocatorError!void {
|
||||||
self.processFn(self);
|
try self.processFn(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO allow inserting the new elements at an arbitrary index
|
// TODO allow inserting the new elements at an arbitrary index
|
||||||
|
@ -373,6 +370,37 @@ pub const Signal = struct {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub const Microchip = struct {
|
||||||
|
component: Component,
|
||||||
|
|
||||||
|
circuit: Circuit,
|
||||||
|
|
||||||
|
pub fn init(allocator: std.mem.Allocator) !*Microchip {
|
||||||
|
var self = try allocator.create(Microchip);
|
||||||
|
errdefer allocator.destroy(self);
|
||||||
|
self.circuit = Circuit.init(allocator);
|
||||||
|
errdefer self.circuit.deinit();
|
||||||
|
try Component.init(&self.component, allocator, "Microchip", 0, 0, &process, &deinit);
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deinit(component: *Component, allocator: std.mem.Allocator) void {
|
||||||
|
const self: *Microchip = @fieldParentPtr("component", component);
|
||||||
|
self.circuit.deinit();
|
||||||
|
allocator.destroy(self);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn process(component: *Component) AllocatorError!void {
|
||||||
|
const self: *Microchip = @fieldParentPtr("component", component);
|
||||||
|
try self.circuit.tick();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn connectOutput(self: *Microchip, inner: *Component, inner_idx: usize, outer_idx: usize) !void {
|
||||||
|
_ = .{ self, inner, inner_idx, outer_idx };
|
||||||
|
@compileError("TODO");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
pub const Battery = struct {
|
pub const Battery = struct {
|
||||||
component: Component,
|
component: Component,
|
||||||
|
|
||||||
|
@ -390,7 +418,7 @@ pub const Battery = struct {
|
||||||
allocator.destroy(self);
|
allocator.destroy(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn process(component: *Component) void {
|
pub fn process(component: *Component) AllocatorError!void {
|
||||||
const self: *Battery = @fieldParentPtr("component", component);
|
const self: *Battery = @fieldParentPtr("component", component);
|
||||||
component.outputs.items[0].signal.digital = @intFromFloat(std.math.sign(self.value));
|
component.outputs.items[0].signal.digital = @intFromFloat(std.math.sign(self.value));
|
||||||
component.outputs.items[0].signal.analog = self.value;
|
component.outputs.items[0].signal.analog = self.value;
|
||||||
|
@ -415,7 +443,7 @@ pub const Not = struct {
|
||||||
allocator.destroy(self);
|
allocator.destroy(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn process(component: *Component) void {
|
pub fn process(component: *Component) AllocatorError!void {
|
||||||
const self: *Not = @fieldParentPtr("component", component);
|
const self: *Not = @fieldParentPtr("component", component);
|
||||||
if (self.invert_output) {
|
if (self.invert_output) {
|
||||||
component.outputs.items[0].signal.digital = 1 - @as(i2, @intCast(@abs(component.inputs.items[0].signal.digital)));
|
component.outputs.items[0].signal.digital = 1 - @as(i2, @intCast(@abs(component.inputs.items[0].signal.digital)));
|
||||||
|
@ -449,7 +477,7 @@ pub const And = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO check implementation
|
// TODO check implementation
|
||||||
pub fn process(component: *Component) void {
|
pub fn process(component: *Component) AllocatorError!void {
|
||||||
const self: *And = @fieldParentPtr("component", component);
|
const self: *And = @fieldParentPtr("component", component);
|
||||||
if (self.arithmetic_mode) {
|
if (self.arithmetic_mode) {
|
||||||
component.outputs.items[0].signal.digital = component.inputs.items[0].signal.digital;
|
component.outputs.items[0].signal.digital = component.inputs.items[0].signal.digital;
|
||||||
|
@ -512,7 +540,7 @@ pub const Or = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO check implementation
|
// TODO check implementation
|
||||||
pub fn process(component: *Component) void {
|
pub fn process(component: *Component) AllocatorError!void {
|
||||||
const self: *Or = @fieldParentPtr("component", component);
|
const self: *Or = @fieldParentPtr("component", component);
|
||||||
if (self.arithmetic_mode) {
|
if (self.arithmetic_mode) {
|
||||||
component.outputs.items[0].signal.digital = component.inputs.items[0].signal.digital;
|
component.outputs.items[0].signal.digital = component.inputs.items[0].signal.digital;
|
||||||
|
@ -553,3 +581,5 @@ pub const Or = struct {
|
||||||
// or1.process();
|
// or1.process();
|
||||||
// try std.testing.expectEqual(-0.5, or1.output.analog);
|
// try std.testing.expectEqual(-0.5, or1.output.analog);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
const AllocatorError = std.mem.Allocator.Error;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue