local RS = game:GetService("ReplicatedStorage") local G = require(RS:WaitForChild("ImGui")) local ImNodes = require(RS:WaitForChild("ImNodes"))(G) local L = require(RS:WaitForChild("LogicSim"))(G, ImNodes) local function out(g, n) return L.Value(g, L.OutPin(n.Id, 1)) end local function inp(g, n) -- value driving input 1 of a sink (via its LED state) return g.nodes[n.Id].state.on and 1 or 0 end -- full adder truth table local g, n = L.Examples.FullAdder() for v = 0, 7 do local a, b, c = v % 2, (v // 2) % 2, (v // 4) % 2 n.A.state.on, n.B.state.on, n.Cin.state.on = a == 1, b == 1, c == 1 L.Step(g, 0) local s = a + b + c assert(inp(g, n.Sum) == s % 2 and inp(g, n.Carry) == s // 2, `fa {a}{b}{c}`) end -- ripple adder 4-bit g, n = L.Examples.RippleAdder4() for _, ab in { { 5, 9 }, { 15, 1 }, { 7, 7 } } do for i = 1, 4 do n.A[i].state.on = (ab[1] // 2 ^ (i - 1)) % 2 == 1 n.B[i].state.on = (ab[2] // 2 ^ (i - 1)) % 2 == 1 end L.Step(g, 0) local s = ab[1] + ab[2] assert(n.Hex.state.value == s % 16 and inp(g, n.Carry) == s // 16, `ripple {ab[1]}+{ab[2]}`) end -- SR latch holds g, n = L.Examples.SRLatch() n.S.state.on = true; L.Step(g, 0) n.S.state.on = false; L.Step(g, 0) assert(inp(g, n.Q) == 1 and inp(g, n.NotQ) == 0, "set holds") n.R.state.on = true; L.Step(g, 0) n.R.state.on = false; L.Step(g, 0) assert(inp(g, n.Q) == 0 and inp(g, n.NotQ) == 1, "reset holds") -- D flip-flop rising edge g = L.New() local d, clk, ff = L.AddNode(g, "Switch"), L.AddNode(g, "Switch"), L.AddNode(g, "DFlipFlop") L.Connect(g, d, 1, ff, 1); L.Connect(g, clk, 1, ff, 2) local function q() return L.Value(g, L.OutPin(ff.Id, 1)) end d.state.on = true; L.Step(g, 0) assert(q() == 0, "no edge yet") clk.state.on = true; L.Step(g, 0) assert(q() == 1 and L.Value(g, L.OutPin(ff.Id, 2)) == 0, "latched on rise") d.state.on = false; L.Step(g, 0) assert(q() == 1, "holds while clk high") clk.state.on = false; L.Step(g, 0) assert(q() == 1, "falling edge ignored") clk.state.on = true; L.Step(g, 0) assert(q() == 0, "second rise") -- blinker toggles g, n = L.Examples.Blinker() L.Step(g, 0.3) assert(inp(g, n.LED) == 1, "blink on") L.Step(g, 0.25) assert(inp(g, n.LED) == 0, "blink off") -- save -> clear -> load round trip (no ImNodes context: graph only) g, n = L.Examples.SRLatch() n.S.state.on = true; L.Step(g, 0); n.S.state.on = false; L.Step(g, 0) local saved = L.Save(g) g = nil local g2 = L.Load(saved) L.Step(g2, 0) assert(g2.nodes[n.Q.Id].state.on == true and L.Save(g2) == saved, "round trip") g2.nodes[n.R.Id].state.on = true; L.Step(g2, 0) -- loaded graph still evaluates g2.nodes[n.R.Id].state.on = false; L.Step(g2, 0) assert(g2.nodes[n.Q.Id].state.on == false and g2.nodes[n.NotQ.Id].state.on == true, "loaded latch resets") -- with ImNodes context: ini embedded and restored ImNodes.CreateContext() local s2 = L.Save(g2) assert(string.find(s2, "\nINI\n", 1, true), "ini section") local g3 = L.Load(s2) L.Step(g3, 0) assert(L.Save(g3) == s2, "round trip with ini\n" .. s2 .. "\n---\n" .. L.Save(g3)) ImNodes.DestroyContext() print("OK")