logicgate: make the first wire into an input win in the simulator
- Commit
d385adff65a7031332a57fec79cc372ac7de8ab1- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-02 01:33 UTC (4 days ago)
- Parents
1293880
Symptom: a sandbox component restored with two wires into one input behaved one way in its own editor and the opposite way inside a level. With wires level-input then LOW into a NOT, the editor showed NOT(input); the level simulated NOT(LOW), so the chip output ignored its input. Root cause: restored components enter the catalog unsanitized. Circuit.sanitized (applied when the component itself is opened) and Circuit.connect both keep the first wire into an input, but Simulator built its input-source map by unconditional assignment, so the last wire won for any board that reached it unsanitized (level drafts are sanitized; catalog components are not). Fix: only fill an input source slot that is still empty, so the first wire wins in the simulator exactly as in the editor. Verified: new SimulatorTest case failed before the fix (expected 0, actual 1) and passes after; full :games:logicgate:testDevUnitTest, :games:logicgate:lint and :games:logicgate:lintVitalRelease all pass.
Diff
games/logicgate/src/main/java/com/vayunmathur/games/logicgate/domain/sim/Simulator.kt
diff --git a/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/domain/sim/Simulator.kt b/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/domain/sim/Simulator.ktindex 24f143d058..753bb55e97 100644--- a/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/domain/sim/Simulator.kt+++ b/games/logicgate/src/main/java/com/vayunmathur/games/logicgate/domain/sim/Simulator.kt@@ -71,7 +71,13 @@ class Simulator( val src = index[w.from.node] ?: continue val dst = index[w.to.node] ?: continue if (w.from.index !in outPins[src].indices || w.to.index !in inPins[dst].indices) continue- inSrc[dst][w.to.index] = outBase[src] + w.from.index+ // First wire into an input wins, matching Circuit.sanitized and+ // connect (which replaces any wire already on the input): a+ // stale board carrying a second wire into one input must not+ // simulate differently in a level than it does in its editor.+ if (inSrc[dst][w.to.index] == -1) {+ inSrc[dst][w.to.index] = outBase[src] + w.from.index+ } } scratch = Array(nodes.size) { IntArray(inPins[it].size) } nodes.forEachIndexed { i, n ->
games/logicgate/src/test/java/com/vayunmathur/games/logicgate/domain/SimulatorTest.kt
diff --git a/games/logicgate/src/test/java/com/vayunmathur/games/logicgate/domain/SimulatorTest.kt b/games/logicgate/src/test/java/com/vayunmathur/games/logicgate/domain/SimulatorTest.ktindex 68c2f17180..062b258c82 100644--- a/games/logicgate/src/test/java/com/vayunmathur/games/logicgate/domain/SimulatorTest.kt+++ b/games/logicgate/src/test/java/com/vayunmathur/games/logicgate/domain/SimulatorTest.kt@@ -3,11 +3,14 @@ package com.vayunmathur.games.logicgate.domain import com.vayunmathur.games.logicgate.domain.levels.LevelRegistry import com.vayunmathur.games.logicgate.domain.model.Chips import com.vayunmathur.games.logicgate.domain.model.Circuit+import com.vayunmathur.games.logicgate.domain.model.LevelInput import com.vayunmathur.games.logicgate.domain.model.LevelIo+import com.vayunmathur.games.logicgate.domain.model.LevelOutput import com.vayunmathur.games.logicgate.domain.model.Pin import com.vayunmathur.games.logicgate.domain.model.PinRef import com.vayunmathur.games.logicgate.domain.model.PinSide import com.vayunmathur.games.logicgate.domain.model.Point+import com.vayunmathur.games.logicgate.domain.model.Wire import com.vayunmathur.games.logicgate.domain.sim.Simulator import com.vayunmathur.games.logicgate.domain.sim.Verifier import kotlin.test.Test@@ -75,6 +78,34 @@ class SimulatorTest { assertContentEquals(intArrayOf(1), sim.outputs()) } + @Test+ fun aSecondWireIntoOneInputDoesNotOverrideTheFirst() {+ // A circuit built by the editor can never hold two wires into one+ // input (connect replaces, and sanitized keeps the first), but a+ // stale or restored component board is loaded into the catalog+ // unsanitized. The simulator must agree with the editor about+ // such a board: first wire wins, so NOT sees the level input+ // (1 -> 0), not the LOW wire listed second (0 -> 1).+ val io = LevelIo(listOf(Pin("a")), listOf(Pin("o")))+ var c = Circuit.forLevel(io)+ val notId: com.vayunmathur.games.logicgate.domain.model.NodeId+ val lowId: com.vayunmathur.games.logicgate.domain.model.NodeId+ c.addGate(Chips.NOT, Point(0f, 0f)).let { c = it.first; notId = it.second }+ c.addGate(Chips.LOW, Point(0f, 0f)).let { c = it.first; lowId = it.second }+ val inputId = c.nodes.filterIsInstance<LevelInput>().first().id+ val outputId = c.nodes.filterIsInstance<LevelOutput>().first().id+ fun outPin(id: com.vayunmathur.games.logicgate.domain.model.NodeId) = PinRef(id, PinSide.OUT, 0)+ fun inPin(id: com.vayunmathur.games.logicgate.domain.model.NodeId) = PinRef(id, PinSide.IN, 0)+ c = c.copy(+ wires = listOf(+ Wire(outPin(inputId), inPin(notId)),+ Wire(outPin(lowId), inPin(notId)),+ Wire(outPin(notId), inPin(outputId)),+ ),+ )+ assertContentEquals(intArrayOf(0), Simulator(c, io).step(intArrayOf(1)).outputs)+ }+ @Test fun registerProgramCounterAndRamTracesMatchTheirSpecs() { for (id in listOf("bit", "reg8", "pc4", "ram4")) {