Skip to content

Commit 6339c90

Browse files
committed
Remove cloned simulator
1 parent 75e25bb commit 6339c90

File tree

2 files changed

+61
-63
lines changed

2 files changed

+61
-63
lines changed

src/app.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,8 +1447,8 @@ impl App {
14471447
// }
14481448

14491449
if self.current_dirty || self.simulator.is_none() {
1450-
let mut sim = Simulator::new(self.db.clone());
1451-
sim.compute();
1450+
let mut sim = Simulator::new();
1451+
sim.compute(&self.db);
14521452
self.simulator = Some(sim);
14531453
self.current_dirty = false;
14541454
}

src/simulator.rs

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ impl Value {
7777
}
7878

7979
pub struct Simulator {
80-
db: DB,
8180
/// Final result - maps each pin to its current value
8281
pub current: HashMap<Pin, Value>,
8382
/// Number of iterations taken in last compute
@@ -89,23 +88,22 @@ pub struct Simulator {
8988
}
9089

9190
impl Simulator {
92-
pub(crate) fn new(db: DB) -> Self {
91+
pub(crate) fn new() -> Self {
9392
Self {
94-
db,
9593
current: Default::default(),
9694
last_iterations: 0,
9795
status: SimulationStatus::Running,
9896
current_iteration: 0,
9997
}
10098
}
10199

102-
fn rebuild_sorted_instances(&self) -> Vec<InstanceId> {
103-
let mut ids: Vec<InstanceId> = self.db.types.keys().collect();
100+
fn rebuild_sorted_instances(&self, db: &DB) -> Vec<InstanceId> {
101+
let mut ids: Vec<InstanceId> = db.types.keys().collect();
104102
ids.sort_unstable();
105103
ids
106104
}
107105

108-
pub fn compute(&mut self) -> HashSet<Pin> {
106+
pub fn compute(&mut self, db: &DB) -> HashSet<Pin> {
109107
log::info!("=== Begin simulation ===");
110108

111109
self.current_iteration = 0;
@@ -115,25 +113,25 @@ impl Simulator {
115113
let mut previous_state: HashMap<Pin, Value>;
116114
let mut stable_count = 0;
117115

118-
let power_ids: Vec<_> = self.db.powers.keys().collect();
116+
let power_ids: Vec<_> = db.powers.keys().collect();
119117
for &id in &power_ids {
120-
self.evaluate_power(id);
118+
self.evaluate_power(db, id);
121119
}
122120

123121
while self.current_iteration < MAX_ITERATIONS {
124122
previous_state = self.current.clone();
125123

126-
let sorted_instances = self.rebuild_sorted_instances();
124+
let sorted_instances = self.rebuild_sorted_instances(db);
127125
for &id in &sorted_instances {
128-
match self.db.ty(id) {
126+
match db.ty(id) {
129127
InstanceKind::Wire => {
130-
self.evaluate_wire(id);
128+
self.evaluate_wire(db, id);
131129
}
132130
InstanceKind::Gate(_) => {
133-
self.evaluate_gate(id);
131+
self.evaluate_gate(db, id);
134132
}
135133
InstanceKind::Lamp => {
136-
self.evaluate_lamp(id);
134+
self.evaluate_lamp(db, id);
137135
}
138136
InstanceKind::Power | InstanceKind::CustomCircuit(_) => {}
139137
}
@@ -171,47 +169,47 @@ impl Simulator {
171169
.collect()
172170
}
173171

174-
fn evaluate_power(&mut self, id: InstanceId) {
175-
let p = self.db.get_power(id);
176-
let out = self.db.power_output(id);
172+
fn evaluate_power(&mut self, db: &DB, id: InstanceId) {
173+
let p = db.get_power(id);
174+
let out = db.power_output(id);
177175
let val = if p.on { Value::One } else { Value::Zero };
178176
self.current.insert(out, val);
179177
}
180178

181-
fn evaluate_wire(&mut self, id: InstanceId) {
179+
fn evaluate_wire(&mut self, db: &DB, id: InstanceId) {
182180
let input = {
183-
let start = self.db.wire_start(id);
184-
let end = self.db.wire_end(id);
181+
let start = db.wire_start(id);
182+
let end = db.wire_end(id);
185183

186-
if self.db.pin_info(start).kind == PinKind::Input {
184+
if db.pin_info(start).kind == PinKind::Input {
187185
start
188186
} else {
189187
end
190188
}
191189
};
192-
let other = if self.db.wire_start(id) == input {
193-
self.db.wire_end(id)
190+
let other = if db.wire_start(id) == input {
191+
db.wire_end(id)
194192
} else {
195-
self.db.wire_start(id)
193+
db.wire_start(id)
196194
};
197195

198-
let result = self.get_pin_value(input);
196+
let result = self.get_pin_value(db, input);
199197

200198
self.current.insert(input, result);
201199
self.current.insert(other, result);
202200
}
203201

204-
fn evaluate_gate(&mut self, id: InstanceId) {
205-
let InstanceKind::Gate(kind) = self.db.ty(id) else {
202+
fn evaluate_gate(&mut self, db: &DB, id: InstanceId) {
203+
let InstanceKind::Gate(kind) = db.ty(id) else {
206204
return;
207205
};
208206

209-
let inp1 = self.db.gate_inp1(id);
210-
let inp2 = self.db.gate_inp2(id);
211-
let out = self.db.gate_output(id);
207+
let inp1 = db.gate_inp1(id);
208+
let inp2 = db.gate_inp2(id);
209+
let out = db.gate_output(id);
212210

213-
let a = self.get_pin_value(inp1);
214-
let b = self.get_pin_value(inp2);
211+
let a = self.get_pin_value(db, inp1);
212+
let b = self.get_pin_value(db, inp2);
215213

216214
let out_val = match kind {
217215
GateKind::And => a.and(b),
@@ -225,21 +223,21 @@ impl Simulator {
225223
self.current.insert(out, out_val);
226224
}
227225

228-
fn evaluate_lamp(&mut self, id: InstanceId) {
229-
let inp = self.db.lamp_input(id);
230-
let val = self.get_pin_value(inp);
226+
fn evaluate_lamp(&mut self, db: &DB, id: InstanceId) {
227+
let inp = db.lamp_input(id);
228+
let val = self.get_pin_value(db, inp);
231229
self.current.insert(inp, val);
232230
}
233231

234-
fn get_pin_value(&self, pin: Pin) -> Value {
235-
let mut connected = self.db.connected_pins(pin);
232+
fn get_pin_value(&self, db: &DB, pin: Pin) -> Value {
233+
let mut connected = db.connected_pins(pin);
236234
connected.push(pin);
237235
connected.sort_unstable();
238236
connected.dedup();
239237

240238
let mut result = Value::Zero;
241239
for other in connected {
242-
if self.db.pin_info(other).kind != PinKind::Output {
240+
if db.pin_info(other).kind != PinKind::Output {
243241
continue;
244242
}
245243
if let Some(&val) = self.current.get(&other) {
@@ -301,8 +299,8 @@ mod tests {
301299

302300
add_connection(&mut db, power_out, lamp_in);
303301

304-
let mut sim = Simulator::new(db.clone());
305-
let result = sim.compute();
302+
let mut sim = Simulator::new();
303+
let result = sim.compute(&db);
306304

307305
assert!(result.contains(&power_out), "Power output should be on");
308306
assert!(result.contains(&lamp_in), "Lamp input should be on");
@@ -319,8 +317,8 @@ mod tests {
319317

320318
add_connection(&mut db, power_out, lamp_in);
321319

322-
let mut sim = Simulator::new(db.clone());
323-
let result = sim.compute();
320+
let mut sim = Simulator::new();
321+
let result = sim.compute(&db);
324322

325323
assert!(!result.contains(&power_out), "Power output should be off");
326324
assert!(!result.contains(&lamp_in), "Lamp input should be off");
@@ -345,8 +343,8 @@ mod tests {
345343
add_connection(&mut db, power2_out, gate_in2);
346344
add_connection(&mut db, gate_out, lamp_in);
347345

348-
let mut sim = Simulator::new(db.clone());
349-
let result = sim.compute();
346+
let mut sim = Simulator::new();
347+
let result = sim.compute(&db);
350348

351349
assert!(result.contains(&gate_out), "AND gate output should be on");
352350
assert!(result.contains(&lamp_in), "Lamp should be on");
@@ -408,8 +406,8 @@ mod tests {
408406
// Set S=1, R=0
409407
db.get_power_mut(s_power).on = true;
410408

411-
let mut sim = Simulator::new(db.clone());
412-
let _result = sim.compute();
409+
let mut sim = Simulator::new();
410+
let _result = sim.compute(&db);
413411

414412
// Q should be high (1)
415413
let q_output = db.gate_output(nor1);
@@ -437,8 +435,8 @@ mod tests {
437435
// Set S=0, R=1
438436
db.get_power_mut(r_power).on = true;
439437

440-
let mut sim = Simulator::new(db.clone());
441-
let _result = sim.compute();
438+
let mut sim = Simulator::new();
439+
let _result = sim.compute(&db);
442440

443441
// Q should be low (0)
444442
let q_output = db.gate_output(nor1);
@@ -465,13 +463,13 @@ mod tests {
465463

466464
// First set the latch to SET state (S=1, R=0)
467465
db.get_power_mut(s_power).on = true;
468-
let mut sim = Simulator::new(db.clone());
469-
sim.compute();
466+
let mut sim = Simulator::new();
467+
sim.compute(&db);
470468

471469
// Now change to HOLD state (S=0, R=0)
472470
db.get_power_mut(s_power).on = false;
473-
let mut sim2 = Simulator::new(db.clone());
474-
let _result = sim2.compute();
471+
let mut sim2 = Simulator::new();
472+
let _result = sim2.compute(&db);
475473

476474
// Q should still be high (1) - holding previous SET state
477475
let q_output = db.gate_output(nor1);
@@ -493,8 +491,8 @@ mod tests {
493491
db.get_power_mut(s_power).on = true;
494492
db.get_power_mut(r_power).on = true;
495493

496-
let mut sim = Simulator::new(db.clone());
497-
let _result = sim.compute();
494+
let mut sim = Simulator::new();
495+
let _result = sim.compute(&db);
498496

499497
// Both outputs should be 0 (since NOR with any input 1 gives 0)
500498
let q_output = db.gate_output(nor1);
@@ -515,8 +513,8 @@ mod tests {
515513

516514
// Start in SET state (S=1, R=0)
517515
db.get_power_mut(s_power).on = true;
518-
let mut sim = Simulator::new(db.clone());
519-
sim.compute();
516+
let mut sim = Simulator::new();
517+
sim.compute(&db);
520518

521519
let q_output = db.gate_output(nor1);
522520
let q_value = sim.current.get(&q_output).copied().unwrap_or(Value::X);
@@ -525,8 +523,8 @@ mod tests {
525523
// Transition to RESET state (S=0, R=1)
526524
db.get_power_mut(s_power).on = false;
527525
db.get_power_mut(r_power).on = true;
528-
let mut sim2 = Simulator::new(db.clone());
529-
sim2.compute();
526+
let mut sim2 = Simulator::new();
527+
sim2.compute(&db);
530528

531529
let q_value_after = sim2.current.get(&q_output).copied().unwrap_or(Value::X);
532530
let q_bar_output = db.gate_output(nor2);
@@ -544,8 +542,8 @@ mod tests {
544542
// Set S=1, R=0
545543
db.get_power_mut(s_power).on = true;
546544

547-
let mut sim = Simulator::new(db.clone());
548-
sim.compute();
545+
let mut sim = Simulator::new();
546+
sim.compute(&db);
549547

550548
// Should stabilize in reasonable number of iterations
551549
// For a simple SR latch, should be < 10 iterations
@@ -565,8 +563,8 @@ mod tests {
565563
// Set S=1, R=0
566564
db.get_power_mut(s_power).on = true;
567565

568-
let mut sim = Simulator::new(db.clone());
569-
sim.compute();
566+
let mut sim = Simulator::new();
567+
sim.compute(&db);
570568

571569
// The simulator should handle the cycle without infinite recursion
572570
// If we get here, cycle detection worked (test would hang/crash otherwise)

0 commit comments

Comments
 (0)