├── .gitignore ├── CircuitSim ├── CircuitSim.sln ├── CircuitSim │ ├── App.config │ ├── App.xaml │ ├── App.xaml.cs │ ├── BaseObjects │ │ ├── CircuitObject.cs │ │ ├── Input.xaml │ │ ├── Input.xaml.cs │ │ ├── Output.xaml │ │ ├── Output.xaml.cs │ │ └── PowerObject.cs │ ├── CircuitSim.csproj │ ├── FlipFlop │ │ ├── D.xaml │ │ ├── D.xaml.cs │ │ ├── JK.xaml │ │ └── JK.xaml.cs │ ├── Gates │ │ ├── And.xaml │ │ ├── And.xaml.cs │ │ ├── Not.xaml │ │ ├── Not.xaml.cs │ │ ├── Or.xaml │ │ └── Or.xaml.cs │ ├── IO │ │ ├── Clock.xaml │ │ ├── Clock.xaml.cs │ │ ├── Junction.xaml │ │ ├── Junction.xaml.cs │ │ ├── Keypad.xaml │ │ ├── Keypad.xaml.cs │ │ ├── LED.xaml │ │ ├── LED.xaml.cs │ │ ├── Power.xaml │ │ ├── Power.xaml.cs │ │ ├── TextLED.xaml │ │ ├── TextLED.xaml.cs │ │ ├── ToggleSwitch.xaml │ │ └── ToggleSwitch.xaml.cs │ ├── Images │ │ ├── And.png │ │ ├── Clock.png │ │ ├── D.png │ │ ├── JK.png │ │ ├── Not.png │ │ ├── Or.png │ │ ├── Power.png │ │ └── Thumbs.db │ ├── MainWindow.xaml │ ├── MainWindow.xaml.cs │ └── Properties │ │ ├── AssemblyInfo.cs │ │ ├── Resources.Designer.cs │ │ ├── Resources.resx │ │ ├── Settings.Designer.cs │ │ └── Settings.settings └── CircuitSimTest │ ├── CircuitSimTest.csproj │ ├── FlipFlopTest.cs │ ├── GatesTest.cs │ ├── IOTest.cs │ ├── Integration.cs │ └── Properties │ └── AssemblyInfo.cs ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .vs 2 | bin 3 | obj 4 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.22129.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CircuitSim", "CircuitSim\CircuitSim.csproj", "{AF729E14-2C34-4DA0-A744-0FFD9A73AF12}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CircuitSimTest", "CircuitSimTest\CircuitSimTest.csproj", "{F31E32F6-BC99-4572-B178-C8893E6E08EC}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {AF729E14-2C34-4DA0-A744-0FFD9A73AF12}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {AF729E14-2C34-4DA0-A744-0FFD9A73AF12}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {AF729E14-2C34-4DA0-A744-0FFD9A73AF12}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {AF729E14-2C34-4DA0-A744-0FFD9A73AF12}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {F31E32F6-BC99-4572-B178-C8893E6E08EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {F31E32F6-BC99-4572-B178-C8893E6E08EC}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {F31E32F6-BC99-4572-B178-C8893E6E08EC}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {F31E32F6-BC99-4572-B178-C8893E6E08EC}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | EndGlobal 29 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/App.xaml: -------------------------------------------------------------------------------- 1 |  5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/App.xaml.cs: -------------------------------------------------------------------------------- 1 | using System.Windows; 2 | 3 | namespace CircuitSim 4 | { 5 | /// 6 | /// Interaction logic for App.xaml 7 | /// 8 | public partial class App : Application 9 | { 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/BaseObjects/CircuitObject.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Windows; 3 | using System.Windows.Controls; 4 | using System.Windows.Input; 5 | using System.Windows.Media; 6 | 7 | namespace CircuitSim.BaseObjects 8 | { 9 | public class CircuitObject : UserControl 10 | { 11 | public static readonly DependencyProperty CanMoveProperty = DependencyProperty.Register("CanMove", typeof(bool), typeof(CircuitObject), new PropertyMetadata(true)); 12 | /// 13 | /// Allows the circuit objects to be able to be frozen. 14 | /// 15 | public bool CanMove 16 | { 17 | get { return (bool)GetValue(CanMoveProperty); } 18 | set { SetValue(CanMoveProperty, value); } 19 | } 20 | 21 | //The anchor point of the object when being moved 22 | private Point _anchorPoint; 23 | 24 | //The current location of the point 25 | private Point _currentPoint; 26 | 27 | //The transformer that will change the position of the object 28 | private TranslateTransform _transform = new TranslateTransform(); 29 | 30 | //Boolean to check if the object is being dragged 31 | private bool _isInDrag = false; 32 | 33 | //The lines being connected to the input 34 | private List _attachedInputLines; 35 | 36 | //The lines being connected to the output 37 | private List _attachedOutputLines; 38 | 39 | /// 40 | /// Creates a new Circuit Object to be manipulated 41 | /// 42 | public CircuitObject() 43 | { 44 | //Set the events for the object 45 | this.MouseLeftButtonDown += DragObject_MouseLeftButtonDown; 46 | this.MouseMove += DragObject_MouseMove; 47 | this.MouseLeftButtonUp += DragObject_MouseLeftButtonUp; 48 | 49 | //Initialize the lists 50 | _attachedInputLines = new List(); 51 | _attachedOutputLines = new List(); 52 | } 53 | 54 | /// 55 | /// Called when the mouse button is held on the object 56 | /// 57 | /// The element that is calling the event 58 | /// The event parameters 59 | private void DragObject_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) 60 | { 61 | //Don't start the drag if we can't interact with the object 62 | if (CanMove == false) 63 | return; 64 | 65 | //Get the element the object is directly over 66 | var x = Mouse.DirectlyOver; 67 | 68 | //Don't drag when on input/output 69 | if (x is Border) 70 | return; 71 | 72 | //Get the element that called it 73 | var element = sender as FrameworkElement; 74 | 75 | //Set the variables up to the event parameters 76 | _anchorPoint = e.GetPosition(null); 77 | _isInDrag = true; 78 | 79 | //Hide the mouse and signal that the event was handled. 80 | element.CaptureMouse(); 81 | e.Handled = true; 82 | } 83 | 84 | /// 85 | /// Called when the user lets go of the mouse. 86 | /// 87 | /// The element that is calling the event 88 | /// The event parameters 89 | private void DragObject_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e) 90 | { 91 | //Make sure the object is being dragged 92 | if (_isInDrag) 93 | { 94 | //Stop dragging and uncapture the mouse 95 | _isInDrag = false; 96 | var element = sender as FrameworkElement; 97 | element.ReleaseMouseCapture(); 98 | e.Handled = true; 99 | } 100 | } 101 | 102 | /// 103 | /// Called when the user drags the mouse. 104 | /// 105 | /// The element that is calling the event 106 | /// The event parameters 107 | private void DragObject_MouseMove(object sender, System.Windows.Input.MouseEventArgs e) 108 | { 109 | //Make sure the object is being dragged 110 | if (_isInDrag) 111 | { 112 | //Get the current position of the element 113 | var element = sender as FrameworkElement; 114 | _currentPoint = e.GetPosition(null); 115 | 116 | //Transform the element based off the last position 117 | _transform.X += _currentPoint.X - _anchorPoint.X; 118 | _transform.Y += _currentPoint.Y - _anchorPoint.Y; 119 | 120 | //Transform the attached line if its an input (uses EndPoint) 121 | foreach (LineGeometry attachedLine in _attachedInputLines) 122 | { 123 | attachedLine.EndPoint = MoveLine(attachedLine.EndPoint, 124 | (_currentPoint.X - _anchorPoint.X), 125 | (_currentPoint.Y - _anchorPoint.Y)); 126 | } 127 | 128 | //Transform the attached line if its an output (uses StartPoint) 129 | foreach (LineGeometry attachedLine in _attachedOutputLines) 130 | { 131 | attachedLine.StartPoint = MoveLine(attachedLine.StartPoint, 132 | (_currentPoint.X - _anchorPoint.X), 133 | (_currentPoint.Y - _anchorPoint.Y)); 134 | } 135 | 136 | //Transform the elements location 137 | this.RenderTransform = _transform; 138 | //Update the anchor point 139 | _anchorPoint = _currentPoint; 140 | } 141 | } 142 | 143 | /// 144 | /// Translates a lines position. 145 | /// 146 | /// The point of the line to move 147 | /// The amount to translate by in the X axis 148 | /// The amount to translate by in the Y axis 149 | /// 150 | private Point MoveLine(Point PointToMove, double AmountToMoveX, double AmountToMoveY) 151 | { 152 | Point transformedPoint = new Point(); 153 | transformedPoint.X = PointToMove.X + AmountToMoveX; 154 | transformedPoint.Y = PointToMove.Y + AmountToMoveY; 155 | return transformedPoint; 156 | } 157 | 158 | /// 159 | /// Adds an input line to the list of attached lines 160 | /// 161 | /// The line to add 162 | public void AttachInputLine(LineGeometry line) 163 | { 164 | _attachedInputLines.Add(line); 165 | } 166 | 167 | /// 168 | /// Adds an output line to the list of attached lines 169 | /// 170 | /// The line to add 171 | public void AttachOutputLine(LineGeometry line) 172 | { 173 | _attachedOutputLines.Add(line); 174 | } 175 | } 176 | } 177 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/BaseObjects/Input.xaml: -------------------------------------------------------------------------------- 1 |  8 | 9 | 10 | 11 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/BaseObjects/Input.xaml.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Windows.Controls; 3 | 4 | namespace CircuitSim.BaseObjects 5 | { 6 | /// 7 | /// Interaction logic for Input.xaml 8 | /// 9 | public partial class Input : UserControl, PowerObject 10 | { 11 | /// 12 | /// Called when the state of the input is changed 13 | /// 14 | public event StateChangedHandler StateChanged; 15 | public delegate void StateChangedHandler(); 16 | 17 | /// 18 | /// The output state that is linked to the input. Only one output to an input. 19 | /// 20 | private Output _state; 21 | /// 22 | /// The delayed state of the input. Doesn't get updated internally until one tick has passed. 23 | /// 24 | private bool _delayedState; 25 | 26 | /// 27 | /// The current state of the output linked to the input 28 | /// 29 | public bool State 30 | { 31 | get 32 | { 33 | //Returns the state of the connected output, otherwise returns false (since no output connected) 34 | return _delayedState; 35 | } 36 | set 37 | { 38 | throw new Exception("Cannot set the state directly"); 39 | } 40 | } 41 | 42 | /// 43 | /// Creates a new input. 44 | /// 45 | public Input() : this(false) 46 | { 47 | } 48 | 49 | /// 50 | /// Creates a new input. 51 | /// 52 | /// The state the input should be in 53 | public Input(bool state) 54 | { 55 | _state = null; 56 | _delayedState = state; 57 | InitializeComponent(); 58 | } 59 | 60 | /// 61 | /// Links an input to an output 62 | /// 63 | /// The output to link to 64 | public void LinkInputs(Output output) 65 | { 66 | //Makes sure that it only listens to one output event at a time 67 | if (_state != null) 68 | _state.StateChange -= _state_StateChange; 69 | 70 | //Sets the state to the output 71 | _state = output; 72 | } 73 | 74 | /// 75 | /// Called when the state of the input is changed 76 | /// 77 | public void _state_StateChange() 78 | { 79 | //Set the delayed state AFTER a tick 80 | _delayedState = _state.State; 81 | 82 | //Make sure there is a subscriber to the event 83 | if (StateChanged != null) 84 | { 85 | //Passes the value of the output to the circuit 86 | StateChanged(); 87 | } 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/BaseObjects/Output.xaml: -------------------------------------------------------------------------------- 1 |  8 | 9 | 10 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/BaseObjects/Output.xaml.cs: -------------------------------------------------------------------------------- 1 | using System.Windows.Controls; 2 | 3 | namespace CircuitSim.BaseObjects 4 | { 5 | /// 6 | /// Interaction logic for Output.xaml 7 | /// 8 | public partial class Output : UserControl, PowerObject 9 | { 10 | /// 11 | /// Called when the state of the output is changed 12 | /// 13 | public event StateChangeHandler StateChange; 14 | public delegate void StateChangeHandler(); 15 | 16 | // The state of the output 17 | private bool _state; 18 | 19 | /// 20 | /// The current state of the Output 21 | /// 22 | public bool State 23 | { 24 | get 25 | { 26 | return _state; 27 | } 28 | set 29 | { 30 | _state = value; 31 | } 32 | } 33 | 34 | /// 35 | /// Creates a new output class 36 | /// 37 | public Output() 38 | { 39 | _state = false; 40 | InitializeComponent(); 41 | } 42 | 43 | /// 44 | /// Updates any connected inputs. Called once per tick. 45 | /// 46 | public void CallChange() 47 | { 48 | if (StateChange != null) 49 | { 50 | StateChange(); 51 | } 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/BaseObjects/PowerObject.cs: -------------------------------------------------------------------------------- 1 |  2 | namespace CircuitSim.BaseObjects 3 | { 4 | public interface PowerObject 5 | { 6 | /// 7 | /// The state of the current power object 8 | /// 9 | bool State { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/CircuitSim.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {AF729E14-2C34-4DA0-A744-0FFD9A73AF12} 8 | WinExe 9 | Properties 10 | CircuitSim 11 | CircuitSim 12 | v4.5 13 | 512 14 | {60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 15 | 4 16 | 17 | 18 | AnyCPU 19 | true 20 | full 21 | false 22 | bin\Debug\ 23 | DEBUG;TRACE 24 | prompt 25 | 4 26 | 27 | 28 | AnyCPU 29 | pdbonly 30 | true 31 | bin\Release\ 32 | TRACE 33 | prompt 34 | 4 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 4.0 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | MSBuild:Compile 56 | Designer 57 | 58 | 59 | Designer 60 | MSBuild:Compile 61 | 62 | 63 | Designer 64 | MSBuild:Compile 65 | 66 | 67 | Designer 68 | MSBuild:Compile 69 | 70 | 71 | Designer 72 | MSBuild:Compile 73 | 74 | 75 | Designer 76 | MSBuild:Compile 77 | 78 | 79 | Designer 80 | MSBuild:Compile 81 | 82 | 83 | Designer 84 | MSBuild:Compile 85 | 86 | 87 | Designer 88 | MSBuild:Compile 89 | 90 | 91 | Designer 92 | MSBuild:Compile 93 | 94 | 95 | Designer 96 | MSBuild:Compile 97 | 98 | 99 | Designer 100 | MSBuild:Compile 101 | 102 | 103 | Designer 104 | MSBuild:Compile 105 | 106 | 107 | Designer 108 | MSBuild:Compile 109 | 110 | 111 | Designer 112 | MSBuild:Compile 113 | 114 | 115 | MSBuild:Compile 116 | Designer 117 | 118 | 119 | App.xaml 120 | Code 121 | 122 | 123 | 124 | Input.xaml 125 | 126 | 127 | Output.xaml 128 | 129 | 130 | 131 | D.xaml 132 | 133 | 134 | JK.xaml 135 | 136 | 137 | And.xaml 138 | 139 | 140 | Not.xaml 141 | 142 | 143 | Or.xaml 144 | 145 | 146 | Clock.xaml 147 | 148 | 149 | Junction.xaml 150 | 151 | 152 | Keypad.xaml 153 | 154 | 155 | LED.xaml 156 | 157 | 158 | Power.xaml 159 | 160 | 161 | TextLED.xaml 162 | 163 | 164 | ToggleSwitch.xaml 165 | 166 | 167 | MainWindow.xaml 168 | Code 169 | 170 | 171 | 172 | 173 | Code 174 | 175 | 176 | True 177 | True 178 | Resources.resx 179 | 180 | 181 | True 182 | Settings.settings 183 | True 184 | 185 | 186 | ResXFileCodeGenerator 187 | Resources.Designer.cs 188 | 189 | 190 | SettingsSingleFileGenerator 191 | Settings.Designer.cs 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 | 227 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/FlipFlop/D.xaml: -------------------------------------------------------------------------------- 1 |  9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/FlipFlop/D.xaml.cs: -------------------------------------------------------------------------------- 1 | using CircuitSim.BaseObjects; 2 | using System.Windows.Media; 3 | 4 | namespace CircuitSim.FlipFlop 5 | { 6 | /// 7 | /// Interaction logic for D.xaml 8 | /// 9 | public partial class D : CircuitObject 10 | { 11 | /// 12 | /// Checks if a clock has occured or not 13 | /// 14 | private bool _lastClock; 15 | 16 | /// 17 | /// Creates a new D FlipFlop 18 | /// 19 | public D() 20 | { 21 | InitializeComponent(); 22 | //Subscribe to input changes 23 | InputClock.StateChanged += InputClock_StateChanged; 24 | 25 | //Set the output state 26 | Output.State = false; 27 | OutputInverted.State = true; 28 | 29 | //The flipflip hasn't been clocked yet 30 | _lastClock = false; 31 | } 32 | 33 | /// 34 | /// Called when the clock is ticked. 35 | /// 36 | private void InputClock_StateChanged() 37 | { 38 | //If it hasn't been clocked AND the clock is high 39 | if (!_lastClock && InputClock.State == true) 40 | { 41 | //The state is set to the input data 42 | Output.State = InputData.State; 43 | OutputInverted.State = !InputData.State; 44 | 45 | //Set the output colors on the flipflop 46 | if (Output.State) 47 | { 48 | QRect.Fill = new SolidColorBrush(Colors.Red); 49 | QInvertedRect.Fill = new SolidColorBrush(Colors.Black); 50 | } 51 | else 52 | { 53 | QRect.Fill = new SolidColorBrush(Colors.Black); 54 | QInvertedRect.Fill = new SolidColorBrush(Colors.Red); 55 | } 56 | 57 | //Set the last clock to true 58 | _lastClock = true; 59 | } 60 | else if (InputClock.State == false) 61 | { 62 | //Set the clock to false 63 | _lastClock = false; 64 | } 65 | } 66 | 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/FlipFlop/JK.xaml: -------------------------------------------------------------------------------- 1 |  9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/FlipFlop/JK.xaml.cs: -------------------------------------------------------------------------------- 1 | using CircuitSim.BaseObjects; 2 | using System.Windows.Media; 3 | 4 | namespace CircuitSim.FlipFlop 5 | { 6 | /// 7 | /// Interaction logic for JK.xaml 8 | /// 9 | public partial class JK : CircuitObject 10 | { 11 | /// 12 | /// Checks if a clock has occured or not 13 | /// 14 | private bool _lastClock; 15 | 16 | /// 17 | /// Creates a new JK FlipFlop 18 | /// 19 | public JK() 20 | { 21 | InitializeComponent(); 22 | //Subscribe to input changes 23 | InputClock.StateChanged += InputClock_StateChanged; 24 | 25 | //Set the output state 26 | Output.State = false; 27 | OutputInverted.State = true; 28 | 29 | //The flipflip hasn't been clocked yet 30 | _lastClock = false; 31 | } 32 | 33 | /// 34 | /// Called when the clock is ticked. 35 | /// 36 | private void InputClock_StateChanged() 37 | { 38 | //If it hasn't been clocked AND the clock is high 39 | if (!_lastClock && InputClock.State == true) 40 | { 41 | if (InputJ.State && InputK.State) 42 | { 43 | //Toggle state when both J & K are high 44 | Output.State = !Output.State; 45 | OutputInverted.State = !OutputInverted.State; 46 | } 47 | else if (InputJ.State) 48 | { 49 | //Toggle true when J is high 50 | Output.State = true; 51 | OutputInverted.State = false; 52 | } 53 | else if (InputK.State) 54 | { 55 | //Toggle false when K is high 56 | Output.State = false; 57 | OutputInverted.State = true; 58 | } 59 | 60 | //Set the output colors on the flipflop 61 | if (Output.State) 62 | { 63 | QRect.Fill = new SolidColorBrush(Colors.Red); 64 | QInvertedRect.Fill = new SolidColorBrush(Colors.Black); 65 | } 66 | else 67 | { 68 | QRect.Fill = new SolidColorBrush(Colors.Black); 69 | QInvertedRect.Fill = new SolidColorBrush(Colors.Red); 70 | } 71 | 72 | //Set the last clock to true 73 | _lastClock = true; 74 | } 75 | else if (InputClock.State == false) 76 | { 77 | //Set the clock to false 78 | _lastClock = false; 79 | } 80 | } 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/Gates/And.xaml: -------------------------------------------------------------------------------- 1 |  9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/Gates/And.xaml.cs: -------------------------------------------------------------------------------- 1 | using CircuitSim.BaseObjects; 2 | using System.Windows; 3 | 4 | namespace CircuitSim.Gates 5 | { 6 | /// 7 | /// Interaction logic for And.xaml 8 | /// 9 | public partial class And : CircuitObject 10 | { 11 | /// 12 | /// Creates a new AND gate 13 | /// 14 | public And() 15 | { 16 | InitializeComponent(); 17 | 18 | //Subscribe to the input events 19 | InputOne.StateChanged += StateChange; 20 | InputTwo.StateChanged += StateChange; 21 | } 22 | 23 | /// 24 | /// Called when the state of an input changes 25 | /// 26 | private void StateChange() 27 | { 28 | //Set the output to high if both inputs are true. 29 | if (InputOne.State == true && InputTwo.State == true) 30 | Output.State = true; 31 | else 32 | Output.State = false; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/Gates/Not.xaml: -------------------------------------------------------------------------------- 1 |  9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/Gates/Not.xaml.cs: -------------------------------------------------------------------------------- 1 | using CircuitSim.BaseObjects; 2 | 3 | namespace CircuitSim.Gates 4 | { 5 | /// 6 | /// Interaction logic Not.xaml 7 | /// 8 | public partial class Not : CircuitObject 9 | { 10 | /// 11 | /// Creates a new NOT gate 12 | /// 13 | public Not() 14 | { 15 | InitializeComponent(); 16 | 17 | //Subscribe to the input events 18 | Input.StateChanged += StateChange; 19 | } 20 | 21 | /// 22 | /// Called when the state of an input changes 23 | /// 24 | private void StateChange() 25 | { 26 | //Inverts the input 27 | Output.State = !Input.State; 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/Gates/Or.xaml: -------------------------------------------------------------------------------- 1 |  9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/Gates/Or.xaml.cs: -------------------------------------------------------------------------------- 1 | using CircuitSim.BaseObjects; 2 | 3 | namespace CircuitSim.Gates 4 | { 5 | /// 6 | /// Interaction logic Or.xaml 7 | /// 8 | public partial class Or : CircuitObject 9 | { 10 | /// 11 | /// Creates a new OR gate 12 | /// 13 | public Or() 14 | { 15 | InitializeComponent(); 16 | 17 | //Subscribe to the input events 18 | InputOne.StateChanged += StateChange; 19 | InputTwo.StateChanged += StateChange; 20 | } 21 | 22 | /// 23 | /// Called when the state of an input changes 24 | /// 25 | private void StateChange() 26 | { 27 | //If either of the inputs are true, sets the output to high 28 | if (InputOne.State == true || InputTwo.State == true) 29 | Output.State = true; 30 | else 31 | Output.State = false; 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/IO/Clock.xaml: -------------------------------------------------------------------------------- 1 |  10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/IO/Clock.xaml.cs: -------------------------------------------------------------------------------- 1 | using CircuitSim.BaseObjects; 2 | using System.Windows.Media; 3 | 4 | namespace CircuitSim.IO 5 | { 6 | /// 7 | /// Interaction logic for Clock.xaml 8 | /// 9 | public partial class Clock : CircuitObject 10 | { 11 | /// 12 | /// Creates a new clock that toggles every tick. 13 | /// 14 | public Clock() 15 | { 16 | InitializeComponent(); 17 | 18 | //Sets the state to false 19 | PowerOutput.State = false; 20 | //Subscribe to the output change 21 | PowerOutput.StateChange += PowerOutput_StateChange; 22 | } 23 | 24 | /// 25 | /// Called when the output changes 26 | /// 27 | private void PowerOutput_StateChange() 28 | { 29 | //Change the output to be the opposite of the current output 30 | PowerOutput.State = !PowerOutput.State; 31 | 32 | //Set the output colors on the flipflop 33 | if (PowerOutput.State) 34 | { 35 | ClockRect.Fill = new SolidColorBrush(Colors.Red); 36 | } 37 | else 38 | { 39 | ClockRect.Fill = new SolidColorBrush(Colors.Black); 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/IO/Junction.xaml: -------------------------------------------------------------------------------- 1 |  9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/IO/Junction.xaml.cs: -------------------------------------------------------------------------------- 1 | using CircuitSim.BaseObjects; 2 | using System.Windows.Media; 3 | 4 | namespace CircuitSim.IO 5 | { 6 | /// 7 | /// Interaction logic for Junction.xaml 8 | /// 9 | public partial class Junction : CircuitObject 10 | { 11 | /// 12 | /// Creates a new Junction 13 | /// 14 | public Junction() 15 | { 16 | InitializeComponent(); 17 | 18 | //Subscribe to the output change 19 | Input.StateChanged += Input_StateChanged; 20 | } 21 | 22 | /// 23 | /// Called when the input changes 24 | /// 25 | private void Input_StateChanged() 26 | { 27 | //Sets the line of the junction to the current output 28 | if (Input.State) 29 | { 30 | JunctionLine.Stroke = new SolidColorBrush(Colors.Red); 31 | } 32 | else 33 | { 34 | JunctionLine.Stroke = new SolidColorBrush(Colors.Black); 35 | } 36 | 37 | //Mirrors the input 38 | Output.State = Input.State; 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/IO/Keypad.xaml: -------------------------------------------------------------------------------- 1 |  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 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/IO/Keypad.xaml.cs: -------------------------------------------------------------------------------- 1 | using CircuitSim.BaseObjects; 2 | using System; 3 | using System.Windows.Controls; 4 | using System.Windows.Media; 5 | 6 | namespace CircuitSim.IO 7 | { 8 | /// 9 | /// Interaction logic for Keypad.xaml 10 | /// 11 | public partial class Keypad : CircuitObject 12 | { 13 | /// 14 | /// Creates a new clock that toggles every tick. 15 | /// 16 | public Keypad() 17 | { 18 | InitializeComponent(); 19 | 20 | //Sets the state to false 21 | Output1.State = false; 22 | } 23 | 24 | private void Radio_Checked(object sender, System.Windows.RoutedEventArgs e) 25 | { 26 | byte val = byte.Parse((String)((RadioButton)e.OriginalSource).Content, System.Globalization.NumberStyles.AllowHexSpecifier); 27 | SetState(val); 28 | } 29 | 30 | private void SetState(byte state) 31 | { 32 | Output1.State = GetBit(state, 3); 33 | Output2.State = GetBit(state, 2); 34 | Output3.State = GetBit(state, 1); 35 | Output4.State = GetBit(state, 0); 36 | } 37 | 38 | public static bool GetBit(byte b, int bitNumber) 39 | { 40 | return (b & (1 << bitNumber)) != 0; 41 | } 42 | 43 | private void One_Checked(object sender, System.Windows.RoutedEventArgs e) 44 | { 45 | SetState(1); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/IO/LED.xaml: -------------------------------------------------------------------------------- 1 |  9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/IO/LED.xaml.cs: -------------------------------------------------------------------------------- 1 | using CircuitSim.BaseObjects; 2 | using System.Windows; 3 | using System.Windows.Media; 4 | 5 | namespace CircuitSim.IO 6 | { 7 | /// 8 | /// Interaction logic for LED.xaml 9 | /// 10 | public partial class LED : CircuitObject 11 | { 12 | /// 13 | /// Creates a new LED 14 | /// 15 | public LED() 16 | { 17 | InitializeComponent(); 18 | 19 | //Subscribe to the input change 20 | LeftInput.StateChanged += LedStateChanged; 21 | TopInput.StateChanged += LedStateChanged; 22 | BottomInput.StateChanged += LedStateChanged; 23 | } 24 | 25 | /// 26 | /// Called when an input is changed 27 | /// 28 | private void LedStateChanged() 29 | { 30 | //Reset the internal state 31 | bool StateSet = false; 32 | RightOutput.State = false; 33 | 34 | //Check if any Input is high. If so, set state to high 35 | foreach (UIElement e in LEDGrid.Children) 36 | { 37 | if (e is Input) 38 | { 39 | Input IO = (Input)e; 40 | if (IO.State) 41 | { 42 | StateSet = true; 43 | RightOutput.State = true; 44 | break; 45 | } 46 | } 47 | } 48 | 49 | //Color the LED based on the internal state 50 | if (StateSet) 51 | { 52 | LEDRect.Fill = new SolidColorBrush(Colors.Red); 53 | } 54 | else 55 | { 56 | LEDRect.Fill = new SolidColorBrush(Colors.Black); 57 | } 58 | } 59 | 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/IO/Power.xaml: -------------------------------------------------------------------------------- 1 |  10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/IO/Power.xaml.cs: -------------------------------------------------------------------------------- 1 | using CircuitSim.BaseObjects; 2 | 3 | namespace CircuitSim.IO 4 | { 5 | /// 6 | /// Interaction logic for Power.xaml 7 | /// 8 | public partial class Power : CircuitObject 9 | { 10 | /// 11 | /// Creates a new power object 12 | /// 13 | public Power() 14 | { 15 | InitializeComponent(); 16 | 17 | //Sets the output of the power to true 18 | PowerOutput.State = true; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/IO/TextLED.xaml: -------------------------------------------------------------------------------- 1 |  9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 28 | 29 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/IO/TextLED.xaml.cs: -------------------------------------------------------------------------------- 1 | using CircuitSim.BaseObjects; 2 | using System.Collections; 3 | 4 | namespace CircuitSim.IO 5 | { 6 | /// 7 | /// Interaction logic for TextLED.xaml 8 | /// 9 | public partial class TextLED : CircuitObject 10 | { 11 | /// 12 | /// Creates a new TextLED 13 | /// 14 | public TextLED() 15 | { 16 | InitializeComponent(); 17 | 18 | //Subscribes to the 8-bit input 19 | Input1.StateChanged += StateChanged; 20 | Input2.StateChanged += StateChanged; 21 | Input3.StateChanged += StateChanged; 22 | Input4.StateChanged += StateChanged; 23 | Input5.StateChanged += StateChanged; 24 | Input6.StateChanged += StateChanged; 25 | Input7.StateChanged += StateChanged; 26 | Input8.StateChanged += StateChanged; 27 | } 28 | 29 | /// 30 | /// Called when any bit is changed 31 | /// 32 | private void StateChanged() 33 | { 34 | //Put the bits in a boolean array 35 | bool[] boolArray = new bool[] {Input8.State, 36 | Input7.State, 37 | Input6.State, 38 | Input5.State, 39 | Input4.State, 40 | Input3.State, 41 | Input2.State, 42 | Input1.State}; 43 | 44 | //Convert the boolean array to a bit array 45 | BitArray array = new BitArray(boolArray); 46 | 47 | //Set the binary output 48 | BinaryOutput.Content = "0b"; 49 | for (int i = boolArray.Length - 1; i >= 0; i--) 50 | { 51 | bool bit = boolArray[i]; 52 | BinaryOutput.Content += bit ? "1" : "0"; 53 | } 54 | 55 | //Get the integer value of the binary 56 | var result = new int[1]; 57 | array.CopyTo(result, 0); 58 | DecimalOutput.Content = result[0]; 59 | 60 | //Get the hexadecimal value of the integer 61 | HexOutput.Content = string.Format("0x{0}", result[0].ToString("X2")); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/IO/ToggleSwitch.xaml: -------------------------------------------------------------------------------- 1 |  9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/IO/ToggleSwitch.xaml.cs: -------------------------------------------------------------------------------- 1 | using CircuitSim.BaseObjects; 2 | using System.Windows.Input; 3 | using System.Windows.Media; 4 | 5 | namespace CircuitSim.IO 6 | { 7 | /// 8 | /// Interaction logic for ToggleSwitch.xaml 9 | /// 10 | public partial class ToggleSwitch : CircuitObject 11 | { 12 | //The internal state of the toggle switch 13 | private bool _toggleState; 14 | 15 | /// 16 | /// Creates a new ToggleSwitch 17 | /// 18 | public ToggleSwitch() 19 | { 20 | InitializeComponent(); 21 | } 22 | 23 | /// 24 | /// Called when the toggle switch is clicked 25 | /// 26 | private void ToggleRect_MouseDown(object sender, MouseButtonEventArgs e) 27 | { 28 | //Toggles the internal state of the switch 29 | _toggleState = !_toggleState; 30 | 31 | //Sets the color of the LED 32 | if (_toggleState) 33 | { 34 | ToggleRect.Fill = new SolidColorBrush(Colors.Red); 35 | } 36 | else 37 | { 38 | ToggleRect.Fill = new SolidColorBrush(Colors.Black); 39 | } 40 | 41 | //Changes the output of the ToggleSwitch 42 | Output.State = _toggleState; 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/Images/And.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/snowl/CircuitSimulator/3113cd37b1684b15da4d011223b4ccadaed36db7/CircuitSim/CircuitSim/Images/And.png -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/Images/Clock.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/snowl/CircuitSimulator/3113cd37b1684b15da4d011223b4ccadaed36db7/CircuitSim/CircuitSim/Images/Clock.png -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/Images/D.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/snowl/CircuitSimulator/3113cd37b1684b15da4d011223b4ccadaed36db7/CircuitSim/CircuitSim/Images/D.png -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/Images/JK.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/snowl/CircuitSimulator/3113cd37b1684b15da4d011223b4ccadaed36db7/CircuitSim/CircuitSim/Images/JK.png -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/Images/Not.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/snowl/CircuitSimulator/3113cd37b1684b15da4d011223b4ccadaed36db7/CircuitSim/CircuitSim/Images/Not.png -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/Images/Or.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/snowl/CircuitSimulator/3113cd37b1684b15da4d011223b4ccadaed36db7/CircuitSim/CircuitSim/Images/Or.png -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/Images/Power.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/snowl/CircuitSimulator/3113cd37b1684b15da4d011223b4ccadaed36db7/CircuitSim/CircuitSim/Images/Power.png -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/Images/Thumbs.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/snowl/CircuitSimulator/3113cd37b1684b15da4d011223b4ccadaed36db7/CircuitSim/CircuitSim/Images/Thumbs.db -------------------------------------------------------------------------------- /CircuitSim/CircuitSim/MainWindow.xaml: -------------------------------------------------------------------------------- 1 |  8 | 9 | 10 | 11 |