-
Notifications
You must be signed in to change notification settings - Fork 550
Expand file tree
/
Copy pathSimulator.cs
More file actions
720 lines (643 loc) · 24.4 KB
/
Simulator.cs
File metadata and controls
720 lines (643 loc) · 24.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using UnityEngine;
using DLS.Description;
using DLS.Game;
using Random = System.Random;
namespace DLS.Simulation
{
public static class Simulator
{
public static readonly Random rng = new();
static readonly Stopwatch stopwatch = Stopwatch.StartNew();
public static int stepsPerClockTransition;
public static int simulationFrame;
static uint pcg_rngState;
// When sim is first built, or whenever modified, it needs to run a less efficient pass in which the traversal order of the chips is determined
public static bool needsOrderPass;
// Every n frames the simulation permits some random modifications to traversal order of sequential chips (to randomize outcome of race conditions)
public static bool canDynamicReorderThisFrame;
static SimChip prevRootSimChip;
static double elapsedSecondsOld;
static double deltaTime;
static SimAudio audioState;
// Modifications to the sim are made from the main thread, but only applied on the sim thread to avoid conflicts
static readonly ConcurrentQueue<SimModifyCommand> modificationQueue = new();
// ---- Simulation outline ----
// 1) Forward the initial player-controlled input states to all connected pins.
// 2) Loop over all subchips not yet processed this frame, and process them if they are ready (i.e. all input pins have received all their inputs)
// * Note: this means that the input pins must be aware of how many input connections they have (pins choose randomly between conflicting inputs)
// * Note: if a pin has zero input connections, it should be considered as always ready
// 3) Forward the outputs of the processed subchips to their connected pins, and repeat steps 2 & 3 until no more subchips are ready for processing.
// 4) If all subchips have now been processed, then we're done. This is not necessarily the case though, since if an input pin depends on the output of its parent chip
// (directly or indirectly), then it won't receive all its inputs until the chip has already been run, meaning that the chip must be processed before it is ready.
// In this case we process one of the remaining unprocessed (and non-ready) subchips at random, and return to step 3.
//
// Optimization ideas (todo):
// * Compute lookup table for combinational chips
// * Ignore chip if inputs are same as last frame, and no internal pins changed state last frame.
// (would have to make exception for chips containing things like clock or key chip, which can activate 'spontaneously')
// * Create simplified connections network allowing only builtin chips to be processed during simulation
public static void RunSimulationStep(SimChip rootSimChip, DevPinInstance[] inputPins, SimAudio audioState)
{
Simulator.audioState = audioState;
audioState.InitFrame();
if (rootSimChip != prevRootSimChip)
{
needsOrderPass = true;
prevRootSimChip = rootSimChip;
}
pcg_rngState = (uint)rng.Next();
canDynamicReorderThisFrame = simulationFrame % 100 == 0;
simulationFrame++; //
// Step 1) Get player-controlled input states and copy values to the sim
foreach (DevPinInstance input in inputPins)
{
try
{
SimPin simPin = rootSimChip.GetSimPinFromAddress(input.Pin.Address);
PinState.Set(ref simPin.State, input.Pin.PlayerInputState);
input.Pin.State = input.Pin.PlayerInputState;
}
catch (Exception)
{
// Possible for sim to be temporarily out of sync since running on separate threads, so just ignore failure to find pin.
}
}
// Process
if (needsOrderPass)
{
StepChipReorder(rootSimChip);
needsOrderPass = false;
}
else
{
StepChip(rootSimChip);
}
UpdateAudioState();
}
public static void UpdateInPausedState()
{
if (audioState != null)
{
audioState.InitFrame();
UpdateAudioState();
}
}
static void UpdateAudioState()
{
double elapsedSeconds = stopwatch.Elapsed.TotalSeconds;
if (simulationFrame <= 1) deltaTime = 0;
else deltaTime = elapsedSeconds - elapsedSecondsOld;
elapsedSecondsOld = stopwatch.Elapsed.TotalSeconds;
audioState.NotifyAllNotesRegistered(deltaTime);
}
// Recursively propagate signals through this chip and its subchips
static void StepChip(SimChip chip)
{
// Propagate signal from all input dev-pins to all their connected pins
chip.Sim_PropagateInputs();
// NOTE: subchips are assumed to have been sorted in reverse order of desired visitation
for (int i = chip.SubChips.Length - 1; i >= 0; i--)
{
SimChip nextSubChip = chip.SubChips[i];
// Every n frames (for performance reasons) the simulation permits some random modifications to the chip traversal order.
// Here two chips may be swapped if they are not 'ready' (i.e. all inputs have not yet been received for this
// frame; indicating that the input relies on the output). The purpose of this reordering is to allow some variety in
// the outcomes of race-conditions (such as an SR latch having both inputs enabled, and then released).
if (canDynamicReorderThisFrame && i > 0 && !nextSubChip.Sim_IsReady() && RandomBool())
{
SimChip potentialSwapChip = chip.SubChips[i - 1];
if (!ChipTypeHelper.IsBusOriginType(potentialSwapChip.ChipType))
{
nextSubChip = potentialSwapChip;
(chip.SubChips[i], chip.SubChips[i - 1]) = (chip.SubChips[i - 1], chip.SubChips[i]);
}
}
if (nextSubChip.IsBuiltin) ProcessBuiltinChip(nextSubChip); // We've reached a built-in chip, so process it directly
else StepChip(nextSubChip); // Recursively process custom chip
// Step 3) Forward the outputs of the processed subchip to connected pins
nextSubChip.Sim_PropagateOutputs();
}
}
// Recursively propagate signals through this chip and its subchips
// In the process, reorder all subchips based on order in which they become ready for processing (have received all their inputs)
// Note: the order here is reversed, so those ready first will be at the end of the array
static void StepChipReorder(SimChip chip)
{
chip.Sim_PropagateInputs();
SimChip[] subChips = chip.SubChips;
int numRemaining = subChips.Length;
while (numRemaining > 0)
{
int nextSubChipIndex = ChooseNextSubChip(subChips, numRemaining);
SimChip nextSubChip = subChips[nextSubChipIndex];
// "Remove" the chosen subchip from remaining sub chips.
// This is done by moving it to the end of the array and reducing the length of the span by one.
// This also places the subchip into (reverse) order, so that the traversal order need to be determined again on the next pass.
(subChips[nextSubChipIndex], subChips[numRemaining - 1]) = (subChips[numRemaining - 1], subChips[nextSubChipIndex]);
numRemaining--;
// Process chosen subchip
if (nextSubChip.ChipType == ChipType.Custom) StepChipReorder(nextSubChip); // Recursively process custom chip
else ProcessBuiltinChip(nextSubChip); // We've reached a built-in chip, so process it directly
// Step 3) Forward the outputs of the processed subchip to connected pins
nextSubChip.Sim_PropagateOutputs();
}
}
static int ChooseNextSubChip(SimChip[] subChips, int num)
{
bool noSubChipsReady = true;
bool isNonBusChipRemaining = false;
int nextSubChipIndex = -1;
// Step 2) Loop over all subchips not yet processed this frame, and process them if they are ready
for (int i = 0; i < num; i++)
{
SimChip subChip = subChips[i];
if (subChip.Sim_IsReady())
{
noSubChipsReady = false;
nextSubChipIndex = i;
break;
}
isNonBusChipRemaining |= !ChipTypeHelper.IsBusOriginType(subChip.ChipType);
}
// Step 4) if no sub chip is ready to be processed, pick one at random (but save buses for last)
if (noSubChipsReady)
{
nextSubChipIndex = rng.Next(0, num);
// If processing in random order, save buses for last (since we must know all their inputs to display correctly)
if (isNonBusChipRemaining)
{
for (int i = 0; i < num; i++)
{
if (!ChipTypeHelper.IsBusOriginType(subChips[nextSubChipIndex].ChipType)) break;
nextSubChipIndex = (nextSubChipIndex + 1) % num;
}
}
}
return nextSubChipIndex;
}
public static void UpdateKeyboardInputFromMainThread()
{
SimKeyboardHelper.RefreshInputState();
}
public static bool RandomBool()
{
pcg_rngState = pcg_rngState * 747796405 + 2891336453;
uint result = ((pcg_rngState >> (int)((pcg_rngState >> 28) + 4)) ^ pcg_rngState) * 277803737;
result = (result >> 22) ^ result;
return result < uint.MaxValue / 2;
}
static void ProcessBuiltinChip(SimChip chip)
{
switch (chip.ChipType)
{
// ---- Process Built-in chips ----
case ChipType.Nand:
{
uint nandOp = 1 ^ (chip.InputPins[0].State & chip.InputPins[1].State);
chip.OutputPins[0].State = (ushort)(nandOp & 1);
break;
}
case ChipType.Clock:
{
bool high = stepsPerClockTransition != 0 && ((simulationFrame / stepsPerClockTransition) & 1) == 0;
PinState.Set(ref chip.OutputPins[0].State, high ? PinState.LogicHigh : PinState.LogicLow);
break;
}
case ChipType.Pulse:
{
const int pulseDurationIndex = 0;
const int pulseTicksRemainingIndex = 1;
const int pulseInputOldIndex = 2;
uint inputState = chip.InputPins[0].State;
bool pulseInputHigh = PinState.FirstBitHigh(inputState);
uint pulseTicksRemaining = chip.InternalState[pulseTicksRemainingIndex];
if (pulseTicksRemaining == 0)
{
bool isRisingEdge = pulseInputHigh && chip.InternalState[pulseInputOldIndex] == 0;
if (isRisingEdge)
{
pulseTicksRemaining = chip.InternalState[pulseDurationIndex];
chip.InternalState[pulseTicksRemainingIndex] = pulseTicksRemaining;
}
}
uint outputState = PinState.LogicLow;
if (pulseTicksRemaining > 0)
{
chip.InternalState[1]--;
outputState = PinState.LogicHigh;
}
else if (PinState.GetTristateFlags(inputState) != 0)
{
PinState.SetAllDisconnected(ref outputState);
}
chip.OutputPins[0].State = outputState;
chip.InternalState[pulseInputOldIndex] = pulseInputHigh ? 1u : 0;
break;
}
case ChipType.Split_4To1Bit:
{
uint inState4Bit = chip.InputPins[0].State;
chip.OutputPins[0].State = (inState4Bit >> 3) & PinState.SingleBitMask;
chip.OutputPins[1].State = (inState4Bit >> 2) & PinState.SingleBitMask;
chip.OutputPins[2].State = (inState4Bit >> 1) & PinState.SingleBitMask;
chip.OutputPins[3].State = (inState4Bit >> 0) & PinState.SingleBitMask;
break;
}
case ChipType.Merge_1To4Bit:
{
uint stateA = chip.InputPins[3].State & PinState.SingleBitMask; // lsb
uint stateB = chip.InputPins[2].State & PinState.SingleBitMask;
uint stateC = chip.InputPins[1].State & PinState.SingleBitMask;
uint stateD = chip.InputPins[0].State & PinState.SingleBitMask;
chip.OutputPins[0].State = stateA | stateB << 1 | stateC << 2 | stateD << 3;
break;
}
case ChipType.Merge_1To8Bit:
{
uint stateA = chip.InputPins[7].State & PinState.SingleBitMask; // lsb
uint stateB = chip.InputPins[6].State & PinState.SingleBitMask;
uint stateC = chip.InputPins[5].State & PinState.SingleBitMask;
uint stateD = chip.InputPins[4].State & PinState.SingleBitMask;
uint stateE = chip.InputPins[3].State & PinState.SingleBitMask;
uint stateF = chip.InputPins[2].State & PinState.SingleBitMask;
uint stateG = chip.InputPins[1].State & PinState.SingleBitMask;
uint stateH = chip.InputPins[0].State & PinState.SingleBitMask;
chip.OutputPins[0].State = stateA | stateB << 1 | stateC << 2 | stateD << 3 | stateE << 4 | stateF << 5 | stateG << 6 | stateH << 7;
break;
}
case ChipType.Merge_4To8Bit:
{
SimPin in4A = chip.InputPins[0];
SimPin in4B = chip.InputPins[1];
SimPin out8 = chip.OutputPins[0];
PinState.Set8BitFrom4BitSources(ref out8.State, in4B.State, in4A.State);
break;
}
case ChipType.Split_8To4Bit:
{
SimPin in8 = chip.InputPins[0];
SimPin out4A = chip.OutputPins[0];
SimPin out4B = chip.OutputPins[1];
PinState.Set4BitFrom8BitSource(ref out4A.State, in8.State, false);
PinState.Set4BitFrom8BitSource(ref out4B.State, in8.State, true);
break;
}
case ChipType.Split_8To1Bit:
{
uint in8 = chip.InputPins[0].State;
chip.OutputPins[0].State = (in8 >> 7) & PinState.SingleBitMask;
chip.OutputPins[1].State = (in8 >> 6) & PinState.SingleBitMask;
chip.OutputPins[2].State = (in8 >> 5) & PinState.SingleBitMask;
chip.OutputPins[3].State = (in8 >> 4) & PinState.SingleBitMask;
chip.OutputPins[4].State = (in8 >> 3) & PinState.SingleBitMask;
chip.OutputPins[5].State = (in8 >> 2) & PinState.SingleBitMask;
chip.OutputPins[6].State = (in8 >> 1) & PinState.SingleBitMask;
chip.OutputPins[7].State = (in8 >> 0) & PinState.SingleBitMask;
break;
}
case ChipType.TriStateBuffer:
{
SimPin dataPin = chip.InputPins[0];
SimPin enablePin = chip.InputPins[1];
SimPin outputPin = chip.OutputPins[0];
if (PinState.FirstBitHigh(enablePin.State)) outputPin.State = dataPin.State;
else PinState.SetAllDisconnected(ref outputPin.State);
break;
}
case ChipType.Key:
{
bool isHeld = SimKeyboardHelper.KeyIsHeld((char)chip.InternalState[0]);
chip.OutputPins[0].State = isHeld ? PinState.LogicHigh : PinState.LogicLow;
break;
}
case ChipType.DisplayRGB:
{
const uint addressSpace = 256;
uint addressPin = chip.InputPins[0].State;
uint redPin = chip.InputPins[1].State;
uint greenPin = chip.InputPins[2].State;
uint bluePin = chip.InputPins[3].State;
uint resetPin = chip.InputPins[4].State;
uint writePin = chip.InputPins[5].State;
uint refreshPin = chip.InputPins[6].State;
uint clockPin = chip.InputPins[7].State;
// Detect clock rising edge
bool clockHigh = PinState.FirstBitHigh(clockPin);
bool isRisingEdge = clockHigh && chip.InternalState[^1] == 0;
chip.InternalState[^1] = clockHigh ? 1u : 0;
if (isRisingEdge)
{
// Clear back buffer
if (PinState.FirstBitHigh(resetPin))
{
for (int i = 0; i < addressSpace; i++)
{
chip.InternalState[i + addressSpace] = 0;
}
}
// Write to back-buffer
else if (PinState.FirstBitHigh(writePin))
{
uint addressIndex = PinState.GetBitStates(addressPin) + addressSpace;
uint data = (uint)(PinState.GetBitStates(redPin) | (PinState.GetBitStates(greenPin) << 4) | (PinState.GetBitStates(bluePin) << 8));
chip.InternalState[addressIndex] = data;
}
// Copy back-buffer to display buffer
if (PinState.FirstBitHigh(refreshPin))
{
for (int i = 0; i < addressSpace; i++)
{
chip.InternalState[i] = chip.InternalState[i + addressSpace];
}
}
}
// Output current pixel colour
uint colData = chip.InternalState[PinState.GetBitStates(addressPin)];
chip.OutputPins[0].State = (ushort)((colData >> 0) & 0b1111); // red
chip.OutputPins[1].State = (ushort)((colData >> 4) & 0b1111); // green
chip.OutputPins[2].State = (ushort)((colData >> 8) & 0b1111); // blue
break;
}
case ChipType.DisplayDot:
{
const uint addressSpace = 256;
uint addressPin = chip.InputPins[0].State;
uint pixelInputPin = chip.InputPins[1].State;
uint resetPin = chip.InputPins[2].State;
uint writePin = chip.InputPins[3].State;
uint refreshPin = chip.InputPins[4].State;
uint clockPin = chip.InputPins[5].State;
// Detect clock rising edge
bool clockHigh = PinState.FirstBitHigh(clockPin);
bool isRisingEdge = clockHigh && chip.InternalState[^1] == 0;
chip.InternalState[^1] = clockHigh ? 1u : 0;
if (isRisingEdge)
{
// Clear back buffer
if (PinState.FirstBitHigh(resetPin))
{
for (int i = 0; i < addressSpace; i++)
{
chip.InternalState[i + addressSpace] = 0;
}
}
// Write to back-buffer
else if (PinState.FirstBitHigh(writePin))
{
uint addressIndex = PinState.GetBitStates(addressPin) + addressSpace;
uint data = PinState.GetBitStates(pixelInputPin);
chip.InternalState[addressIndex] = data;
}
// Copy back-buffer to display buffer
if (PinState.FirstBitHigh(refreshPin))
{
for (int i = 0; i < addressSpace; i++)
{
chip.InternalState[i] = chip.InternalState[i + addressSpace];
}
}
}
// Output current pixel colour
ushort pixelState = (ushort)chip.InternalState[PinState.GetBitStates(addressPin)];
chip.OutputPins[0].State = pixelState;
break;
}
case ChipType.dev_Ram_8Bit:
{
uint addressPin = chip.InputPins[0].State;
uint dataPin = chip.InputPins[1].State;
uint writeEnablePin = chip.InputPins[2].State;
uint resetPin = chip.InputPins[3].State;
uint clockPin = chip.InputPins[4].State;
// Detect clock rising edge
bool clockHigh = PinState.FirstBitHigh(clockPin);
bool isRisingEdge = clockHigh && chip.InternalState[^1] == 0;
chip.InternalState[^1] = clockHigh ? 1u : 0;
// Write/Reset on rising edge
if (isRisingEdge)
{
if (PinState.FirstBitHigh(resetPin))
{
for (int i = 0; i < 256; i++)
{
chip.InternalState[i] = 0;
}
}
else if (PinState.FirstBitHigh(writeEnablePin))
{
chip.InternalState[PinState.GetBitStates(addressPin)] = PinState.GetBitStates(dataPin);
}
}
// Output data at current address
chip.OutputPins[0].State = (ushort)chip.InternalState[PinState.GetBitStates(addressPin)];
break;
}
case ChipType.Rom_256x16:
{
const int ByteMask = 0b11111111;
uint address = PinState.GetBitStates(chip.InputPins[0].State);
uint data = chip.InternalState[address];
chip.OutputPins[0].State = (ushort)((data >> 8) & ByteMask);
chip.OutputPins[1].State = (ushort)(data & ByteMask);
break;
}
case ChipType.Buzzer:
{
int freqIndex = PinState.GetBitStates(chip.InputPins[0].State);
int volumeIndex = PinState.GetBitStates(chip.InputPins[1].State);
audioState.RegisterNote(freqIndex, (uint)volumeIndex);
break;
}
case ChipType.SPS:
{
const int ByteMask = 0b11111111;
double tps = Project.ActiveProject.simAvgTicksPerSec;
ushort sps = (ushort)tps;
ushort spc = (ushort)stepsPerClockTransition;
PinState.Set(ref chip.OutputPins[5].State, tps >= 65536 ? PinState.LogicHigh : PinState.LogicLow);
PinState.Set(ref chip.OutputPins[4].State, stepsPerClockTransition > 65535 ? PinState.LogicHigh : PinState.LogicLow);
chip.OutputPins[3].State = (ushort)(sps & ByteMask);
chip.OutputPins[2].State = (ushort)((sps >> 8) & ByteMask);
chip.OutputPins[1].State = (ushort)(spc & ByteMask);
chip.OutputPins[0].State = (ushort)((spc >> 8) & ByteMask);
break;
}
// ---- Bus types ----
default:
{
if (ChipTypeHelper.IsBusOriginType(chip.ChipType))
{
SimPin inputPin = chip.InputPins[0];
PinState.Set(ref chip.OutputPins[0].State, inputPin.State);
}
break;
}
}
}
public static SimChip BuildSimChip(ChipDescription chipDesc, ChipLibrary library)
{
return BuildSimChip(chipDesc, library, -1, null);
}
public static SimChip BuildSimChip(ChipDescription chipDesc, ChipLibrary library, int subChipID, uint[] internalState)
{
SimChip simChip = BuildSimChipRecursive(chipDesc, library, subChipID, internalState);
return simChip;
}
// Recursively build full representation of chip from its description for simulation.
static SimChip BuildSimChipRecursive(ChipDescription chipDesc, ChipLibrary library, int subChipID, uint[] internalState)
{
// Recursively create subchips
SimChip[] subchips = chipDesc.SubChips.Length == 0 ? Array.Empty<SimChip>() : new SimChip[chipDesc.SubChips.Length];
for (int i = 0; i < chipDesc.SubChips.Length; i++)
{
SubChipDescription subchipDesc = chipDesc.SubChips[i];
ChipDescription subchipFullDesc = library.GetChipDescription(subchipDesc.Name);
SimChip subChip = BuildSimChipRecursive(subchipFullDesc, library, subchipDesc.ID, subchipDesc.InternalData);
subchips[i] = subChip;
}
SimChip simChip = new(chipDesc, subChipID, internalState, subchips);
// Create connections
for (int i = 0; i < chipDesc.Wires.Length; i++)
{
simChip.AddConnection(chipDesc.Wires[i].SourcePinAddress, chipDesc.Wires[i].TargetPinAddress);
}
return simChip;
}
public static void AddPin(SimChip simChip, int pinID, bool isInputPin)
{
SimModifyCommand command = new()
{
type = SimModifyCommand.ModificationType.AddPin,
modifyTarget = simChip,
simPinToAdd = new SimPin(pinID, isInputPin, simChip),
pinIsInputPin = isInputPin
};
modificationQueue.Enqueue(command);
}
public static void RemovePin(SimChip simChip, int pinID)
{
SimModifyCommand command = new()
{
type = SimModifyCommand.ModificationType.RemovePin,
modifyTarget = simChip,
removePinID = pinID
};
modificationQueue.Enqueue(command);
}
public static void AddSubChip(SimChip simChip, ChipDescription desc, ChipLibrary chipLibrary, int subChipID, uint[] subChipInternalData)
{
SimModifyCommand command = new()
{
type = SimModifyCommand.ModificationType.AddSubchip,
modifyTarget = simChip,
chipDesc = desc,
lib = chipLibrary,
subChipID = subChipID,
subChipInternalData = subChipInternalData
};
modificationQueue.Enqueue(command);
}
public static void AddConnection(SimChip simChip, PinAddress source, PinAddress target)
{
SimModifyCommand command = new()
{
type = SimModifyCommand.ModificationType.AddConnection,
modifyTarget = simChip,
sourcePinAddress = source,
targetPinAddress = target
};
modificationQueue.Enqueue(command);
}
public static void RemoveConnection(SimChip simChip, PinAddress source, PinAddress target)
{
SimModifyCommand command = new()
{
type = SimModifyCommand.ModificationType.RemoveConnection,
modifyTarget = simChip,
sourcePinAddress = source,
targetPinAddress = target
};
modificationQueue.Enqueue(command);
}
public static void RemoveSubChip(SimChip simChip, int id)
{
SimModifyCommand command = new()
{
type = SimModifyCommand.ModificationType.RemoveSubChip,
modifyTarget = simChip,
removeSubChipID = id
};
modificationQueue.Enqueue(command);
}
// Note: this should only be called from the sim thread
public static void ApplyModifications()
{
while (modificationQueue.Count > 0)
{
needsOrderPass = true;
if (modificationQueue.TryDequeue(out SimModifyCommand cmd))
{
if (cmd.type == SimModifyCommand.ModificationType.AddSubchip)
{
SimChip newSubChip = BuildSimChip(cmd.chipDesc, cmd.lib, cmd.subChipID, cmd.subChipInternalData);
cmd.modifyTarget.AddSubChip(newSubChip);
}
else if (cmd.type == SimModifyCommand.ModificationType.RemoveSubChip)
{
cmd.modifyTarget.RemoveSubChip(cmd.removeSubChipID);
}
else if (cmd.type == SimModifyCommand.ModificationType.AddConnection)
{
cmd.modifyTarget.AddConnection(cmd.sourcePinAddress, cmd.targetPinAddress);
}
else if (cmd.type == SimModifyCommand.ModificationType.RemoveConnection)
{
cmd.modifyTarget.RemoveConnection(cmd.sourcePinAddress, cmd.targetPinAddress); //
}
else if (cmd.type == SimModifyCommand.ModificationType.AddPin)
{
cmd.modifyTarget.AddPin(cmd.simPinToAdd, cmd.pinIsInputPin);
}
else if (cmd.type == SimModifyCommand.ModificationType.RemovePin)
{
cmd.modifyTarget.RemovePin(cmd.removePinID);
}
}
}
}
public static void Reset()
{
simulationFrame = 0;
modificationQueue?.Clear();
stopwatch.Restart();
elapsedSecondsOld = 0;
}
struct SimModifyCommand
{
public enum ModificationType
{
AddSubchip,
RemoveSubChip,
AddConnection,
RemoveConnection,
AddPin,
RemovePin
}
public ModificationType type;
public SimChip modifyTarget;
public ChipDescription chipDesc;
public ChipLibrary lib;
public int subChipID;
public uint[] subChipInternalData;
public PinAddress sourcePinAddress;
public PinAddress targetPinAddress;
public SimPin simPinToAdd;
public bool pinIsInputPin;
public int removePinID;
public int removeSubChipID;
}
}
}