├── .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 |
25 |
26 |
27 |
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 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
25 |
26 |
27 |
28 |
29 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
59 |
60 |
61 |
--------------------------------------------------------------------------------
/CircuitSim/CircuitSim/MainWindow.xaml.cs:
--------------------------------------------------------------------------------
1 | using CircuitSim.BaseObjects;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Reflection;
5 | using System.Timers;
6 | using System.Windows;
7 | using System.Windows.Controls;
8 | using System.Windows.Documents;
9 | using System.Windows.Input;
10 | using System.Windows.Media;
11 |
12 | namespace CircuitSim
13 | {
14 | ///
15 | /// Interaction logic for MainWindow.xaml
16 | ///
17 | public partial class MainWindow : Window
18 | {
19 | //The boolean that signifys when an output is being linked
20 | private bool _linkingStarted = false;
21 | //The temporary line that shows when linking an output
22 | private LineGeometry _tempLink;
23 | //The output that is being linked to
24 | private Output _tempOutput;
25 | //A list of all power objects that exist on the canvas
26 | private List _powerList;
27 | //The timer that runs the simulation
28 | private Timer _internalTick;
29 |
30 | ///
31 | /// Called on first start
32 | ///
33 | public MainWindow()
34 | {
35 | InitializeComponent();
36 |
37 | //Create the Power Object list
38 | _powerList = new List();
39 |
40 | //Set up the internal timer at 100 ms
41 | _internalTick = new Timer { Interval = 100 };
42 | _internalTick.Elapsed += TickOutputs;
43 | _internalTick.Enabled = true;
44 | _internalTick.Start();
45 | }
46 |
47 | ///
48 | /// Called when the window is loaded
49 | ///
50 | private void Window_Loaded(object sender, RoutedEventArgs e)
51 | {
52 | //Create the events on load
53 | CircuitCanvas.MouseDown += CircuitCanvas_MouseDown;
54 | CircuitCanvas.MouseMove += CircuitCanvas_MouseMove;
55 | CircuitCanvas.MouseUp += CircuitCanvas_MouseUp;
56 | CircuitCanvas.Drop += CircuitCanvas_Drop;
57 | }
58 |
59 | ///
60 | /// Called when the canvas is clicked on
61 | ///
62 | private void CircuitCanvas_MouseDown(object sender, MouseButtonEventArgs e)
63 | {
64 | //Get the position of the mouse relative to the circuit canvas
65 | Point MousePosition = e.GetPosition(CircuitCanvas);
66 |
67 | //Do a hit test under the mouse position
68 | HitTestResult result = VisualTreeHelper.HitTest(CircuitCanvas, MousePosition);
69 |
70 | //Make sure that there is something under the mouse
71 | if (result == null || result.VisualHit == null)
72 | return;
73 |
74 | //If the mouse has hit a border
75 | if (result.VisualHit is Border)
76 | {
77 | //Get the parent class of the border
78 | Border border = (Border)result.VisualHit;
79 | var IO = border.Parent;
80 |
81 | //If the parent class is an Output
82 | if (IO is Output)
83 | {
84 | //Cast to output
85 | Output IOOutput = (Output)IO;
86 |
87 | //Get the center of the output relative to the canvas
88 | Point position = IOOutput.TransformToAncestor(CircuitCanvas).Transform(new Point(IOOutput.ActualWidth / 2, IOOutput.ActualHeight / 2));
89 |
90 | //Creates a new line
91 | _linkingStarted = true;
92 | _tempLink = new LineGeometry(position, position);
93 |
94 | //Assign it to the list of connections to be displayed
95 | Connections.Children.Add(_tempLink);
96 |
97 | //Assign the temporary output to the current output
98 | _tempOutput = (Output)IO;
99 |
100 | e.Handled = true;
101 | }
102 | }
103 | }
104 |
105 | ///
106 | /// Called when the mouse moves
107 | ///
108 | private void CircuitCanvas_MouseMove(object sender, MouseEventArgs e)
109 | {
110 | //If there is a linking in progress
111 | if (_linkingStarted)
112 | {
113 | //Move the link endpoint to the current location of the mouse
114 | _tempLink.EndPoint = e.GetPosition(CircuitCanvas);
115 | e.Handled = true;
116 | }
117 | }
118 |
119 | ///
120 | /// Called when the mouse button is let go
121 | ///
122 | private void CircuitCanvas_MouseUp(object sender, MouseButtonEventArgs e)
123 | {
124 | //If there is a linking in progress
125 | if (_linkingStarted)
126 | {
127 | //Temporary value to show
128 | bool linked = false;
129 |
130 | //Get the type of the element that the mouse went up on
131 | var BaseType = e.Source.GetType().BaseType;
132 |
133 | if (BaseType == typeof(CircuitObject))
134 | {
135 | //Convert to a circuit object
136 | CircuitObject obj = (CircuitObject)e.Source;
137 |
138 | //Get the position of the mouse relative to the circuit object
139 | Point MousePosition = e.GetPosition(obj);
140 |
141 | //Get the element underneath the mouse
142 | HitTestResult result = VisualTreeHelper.HitTest(obj, MousePosition);
143 |
144 | //Return if there is no element under the cursor
145 | if (result == null || result.VisualHit == null)
146 | {
147 | //Remove the temporary line
148 | Connections.Children.Remove(_tempLink);
149 | _tempLink = null;
150 | _linkingStarted = false;
151 | return;
152 | }
153 |
154 | //If the underlying element is a border element
155 | if (result.VisualHit is Border)
156 | {
157 | Border border = (Border)result.VisualHit;
158 | var IO = border.Parent;
159 |
160 | //Check if the border element is a input element in disguise
161 | if (IO is Input)
162 | {
163 | //Convert to a input element
164 | Input IOInput = (Input)IO;
165 |
166 | //Get the center of the input relative to the canvas
167 | Point inputPoint = IOInput.TransformToAncestor(CircuitCanvas).Transform(new Point(IOInput.ActualWidth / 2, IOInput.ActualHeight / 2));
168 |
169 | //Ends the line in the centre of the input
170 | _tempLink.EndPoint = inputPoint;
171 |
172 | //Links the output to the input
173 | IOInput.LinkInputs(_tempOutput);
174 |
175 | //Adds to the global list
176 | _powerList.Add(_tempOutput);
177 | _powerList.Add(IOInput);
178 |
179 | //Attaches the line to the object
180 | obj.AttachInputLine(_tempLink);
181 |
182 | //Some evil casting (the outputs' parent of the parent is the circuit object that contains the output). Attaches the output side to the object
183 | ((CircuitObject)((Grid)_tempOutput.Parent).Parent).AttachOutputLine(_tempLink);
184 |
185 | //Set linked to true
186 | linked = true;
187 | }
188 | }
189 | }
190 |
191 | //If it isn't linked remove the temporary link
192 | if (!linked)
193 | {
194 | Connections.Children.Remove(_tempLink);
195 | _tempLink = null;
196 | }
197 |
198 | //Stop handling linking
199 | _linkingStarted = false;
200 | e.Handled = true;
201 | }
202 | }
203 |
204 | ///
205 | /// Called when an element is clicked on in the selector
206 | ///
207 | private void ObjectSelector_PreviewMouseDown(object sender, MouseButtonEventArgs e)
208 | {
209 | //Don't do anything if no element clicked
210 | if (ObjectSelector.SelectedItem == null)
211 | return;
212 |
213 | //Copy the element to the drag & drop clipboard
214 | DragDrop.DoDragDrop(ObjectSelector, ObjectSelector.SelectedItem, DragDropEffects.Copy | DragDropEffects.Move);
215 | }
216 |
217 | ///
218 | /// Called when a element is dropped onto the canvas
219 | ///
220 | private void CircuitCanvas_Drop(object sender, DragEventArgs e)
221 | {
222 | //Get the type of element that is dropped onto the canvas
223 | String[] allFormats = e.Data.GetFormats();
224 | //Make sure there is a format there
225 | if (allFormats.Length == 0)
226 | return;
227 |
228 | string ItemType = allFormats[0];
229 |
230 | //Create a new type of the format
231 | CircuitObject instance = (CircuitObject)Assembly.GetExecutingAssembly().CreateInstance(ItemType);
232 |
233 | //If the format doesn't exist do nothing
234 | if (instance == null)
235 | return;
236 |
237 | //Add the element to the canvas
238 | CircuitCanvas.Children.Add(instance);
239 |
240 | //Get the point of the mouse relative to the canvas
241 | Point p = e.GetPosition(CircuitCanvas);
242 |
243 | //Take 15 from the mouse position to center the element on the mouse
244 | Canvas.SetLeft(instance, p.X - 15);
245 | Canvas.SetTop(instance, p.Y - 15);
246 | }
247 |
248 | ///
249 | /// Called when the step button is clicked
250 | ///
251 | private void StepButton_Click(object sender, RoutedEventArgs e)
252 | {
253 | //Tick the simulation once
254 | TickOutputs(null, null);
255 | }
256 |
257 | ///
258 | /// Called when the pause button is clicked
259 | ///
260 | private void ToggleTimerButton_Click(object sender, RoutedEventArgs e)
261 | {
262 | //If the simulation timer is running
263 | if (_internalTick.Enabled)
264 | {
265 | //Turns the simulation ticker off
266 | _internalTick.Enabled = false;
267 | ToggleTimerButton.Content = "Un-Pause";
268 | }
269 | else
270 | {
271 | //Turns the simulation ticker on
272 | _internalTick.Enabled = true;
273 | ToggleTimerButton.Content = "Pause";
274 | }
275 | }
276 |
277 | ///
278 | /// Called when the timer slider's value is changed
279 | ///
280 | private void TimerSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs e)
281 | {
282 | //Sets the label to the current value
283 | TimerTickLabel.Content = string.Format("{0}ms", Math.Floor(TimerSlider.Value));
284 |
285 | //If the timer is not null (is null at startup)
286 | if (_internalTick != null)
287 | {
288 | //Sets the timer interval to the slider
289 | _internalTick.Interval = TimerSlider.Value;
290 | }
291 | }
292 |
293 | ///
294 | /// Ticks the simulation once
295 | ///
296 | private void TickOutputs(object sender, ElapsedEventArgs e)
297 | {
298 | //Runs the simulation on the UI thread
299 | Dispatcher.BeginInvoke(new Action(() =>
300 | {
301 | //A list of IO that has already been ticked
302 | List tickedObjects = new List();
303 |
304 | //Go through each output
305 | foreach (PowerObject o in _powerList)
306 | {
307 | //If the output hasn't been ticked (CAN BE in the list more in the once!)
308 | if (!tickedObjects.Contains(o))
309 | {
310 | if (o is Output)
311 | {
312 | //Calls the events linked to the output
313 | ((Output)o).CallChange();
314 | }
315 | else if (o is Input)
316 | {
317 | //Changes the state of the input
318 | ((Input)o)._state_StateChange();
319 | }
320 | //Adds the output to the ticked object list
321 | tickedObjects.Add(o);
322 | }
323 | }
324 | }));
325 | }
326 | }
327 | }
328 |
--------------------------------------------------------------------------------
/CircuitSim/CircuitSim/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Resources;
3 | using System.Runtime.CompilerServices;
4 | using System.Runtime.InteropServices;
5 | using System.Windows;
6 |
7 | // General Information about an assembly is controlled through the following
8 | // set of attributes. Change these attribute values to modify the information
9 | // associated with an assembly.
10 | [assembly: AssemblyTitle("CircuitSim")]
11 | [assembly: AssemblyDescription("")]
12 | [assembly: AssemblyConfiguration("")]
13 | [assembly: AssemblyCompany("")]
14 | [assembly: AssemblyProduct("CircuitSim")]
15 | [assembly: AssemblyCopyright("Copyright © 2014")]
16 | [assembly: AssemblyTrademark("")]
17 | [assembly: AssemblyCulture("")]
18 |
19 | // Setting ComVisible to false makes the types in this assembly not visible
20 | // to COM components. If you need to access a type in this assembly from
21 | // COM, set the ComVisible attribute to true on that type.
22 | [assembly: ComVisible(false)]
23 |
24 | //In order to begin building localizable applications, set
25 | //CultureYouAreCodingWith in your .csproj file
26 | //inside a . For example, if you are using US english
27 | //in your source files, set the to en-US. Then uncomment
28 | //the NeutralResourceLanguage attribute below. Update the "en-US" in
29 | //the line below to match the UICulture setting in the project file.
30 |
31 | //[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
32 |
33 |
34 | [assembly: ThemeInfo(
35 | ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
36 | //(used if a resource is not found in the page,
37 | // or application resource dictionaries)
38 | ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
39 | //(used if a resource is not found in the page,
40 | // app, or any theme specific resource dictionaries)
41 | )]
42 |
43 |
44 | // Version information for an assembly consists of the following four values:
45 | //
46 | // Major Version
47 | // Minor Version
48 | // Build Number
49 | // Revision
50 | //
51 | // You can specify all the values or you can default the Build and Revision Numbers
52 | // by using the '*' as shown below:
53 | // [assembly: AssemblyVersion("1.0.*")]
54 | [assembly: AssemblyVersion("1.0.0.0")]
55 | [assembly: AssemblyFileVersion("1.0.0.0")]
56 |
--------------------------------------------------------------------------------
/CircuitSim/CircuitSim/Properties/Resources.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.34014
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace CircuitSim.Properties
12 | {
13 |
14 |
15 | ///
16 | /// A strongly-typed resource class, for looking up localized strings, etc.
17 | ///
18 | // This class was auto-generated by the StronglyTypedResourceBuilder
19 | // class via a tool like ResGen or Visual Studio.
20 | // To add or remove a member, edit your .ResX file then rerun ResGen
21 | // with the /str option, or rebuild your VS project.
22 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
23 | [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
24 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
25 | internal class Resources
26 | {
27 |
28 | private static global::System.Resources.ResourceManager resourceMan;
29 |
30 | private static global::System.Globalization.CultureInfo resourceCulture;
31 |
32 | [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
33 | internal Resources()
34 | {
35 | }
36 |
37 | ///
38 | /// Returns the cached ResourceManager instance used by this class.
39 | ///
40 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
41 | internal static global::System.Resources.ResourceManager ResourceManager
42 | {
43 | get
44 | {
45 | if ((resourceMan == null))
46 | {
47 | global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("CircuitSim.Properties.Resources", typeof(Resources).Assembly);
48 | resourceMan = temp;
49 | }
50 | return resourceMan;
51 | }
52 | }
53 |
54 | ///
55 | /// Overrides the current thread's CurrentUICulture property for all
56 | /// resource lookups using this strongly typed resource class.
57 | ///
58 | [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
59 | internal static global::System.Globalization.CultureInfo Culture
60 | {
61 | get
62 | {
63 | return resourceCulture;
64 | }
65 | set
66 | {
67 | resourceCulture = value;
68 | }
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/CircuitSim/CircuitSim/Properties/Resources.resx:
--------------------------------------------------------------------------------
1 |
2 |
3 |
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 | text/microsoft-resx
107 |
108 |
109 | 2.0
110 |
111 |
112 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
113 |
114 |
115 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
116 |
117 |
--------------------------------------------------------------------------------
/CircuitSim/CircuitSim/Properties/Settings.Designer.cs:
--------------------------------------------------------------------------------
1 | //------------------------------------------------------------------------------
2 | //
3 | // This code was generated by a tool.
4 | // Runtime Version:4.0.30319.34014
5 | //
6 | // Changes to this file may cause incorrect behavior and will be lost if
7 | // the code is regenerated.
8 | //
9 | //------------------------------------------------------------------------------
10 |
11 | namespace CircuitSim.Properties
12 | {
13 |
14 |
15 | [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
16 | [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
17 | internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
18 | {
19 |
20 | private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
21 |
22 | public static Settings Default
23 | {
24 | get
25 | {
26 | return defaultInstance;
27 | }
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/CircuitSim/CircuitSim/Properties/Settings.settings:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/CircuitSim/CircuitSimTest/CircuitSimTest.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Debug
5 | AnyCPU
6 | {F31E32F6-BC99-4572-B178-C8893E6E08EC}
7 | Library
8 | Properties
9 | CircuitSimTest
10 | CircuitSimTest
11 | v4.5
12 | 512
13 | {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
14 | 10.0
15 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)
16 | $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages
17 | True
18 | UnitTest
19 |
20 |
21 | true
22 | full
23 | false
24 | bin\Debug\
25 | DEBUG;TRACE
26 | prompt
27 | 4
28 |
29 |
30 | pdbonly
31 | true
32 | bin\Release\
33 | TRACE
34 | prompt
35 | 4
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 | {af729e14-2c34-4da0-a744-0ffd9a73af12}
69 | CircuitSim
70 |
71 |
72 |
73 |
74 |
75 |
76 | False
77 |
78 |
79 | False
80 |
81 |
82 | False
83 |
84 |
85 | False
86 |
87 |
88 |
89 |
90 |
91 |
92 |
99 |
--------------------------------------------------------------------------------
/CircuitSim/CircuitSimTest/FlipFlopTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Microsoft.VisualStudio.TestTools.UnitTesting;
3 | using CircuitSim.FlipFlop;
4 | using CircuitSim.BaseObjects;
5 |
6 | namespace CircuitSimTest
7 | {
8 | [TestClass]
9 | public class FlipFlopTest
10 | {
11 | [TestMethod]
12 | public void TestD()
13 | {
14 | D DFlipFlop = new D();
15 | Input DInputData = (Input)DFlipFlop.FindName("InputData");
16 | Input DInputClock = (Input)DFlipFlop.FindName("InputClock");
17 | Output DOutput = (Output)DFlipFlop.FindName("Output");
18 |
19 | Output testOutput = new Output();
20 | testOutput.State = false;
21 | Output testClock = new Output();
22 | testClock.State = false;
23 |
24 | DInputData.LinkInputs(testOutput);
25 | DInputData._state_StateChange();
26 |
27 | DInputClock.LinkInputs(testClock);
28 | DInputClock._state_StateChange();
29 |
30 | Assert.IsTrue(DOutput.State == false);
31 |
32 | //Set data to true
33 | testOutput.State = true;
34 | DInputData._state_StateChange();
35 |
36 | Assert.IsTrue(DOutput.State == false);
37 |
38 | //Pulse clock
39 | testClock.State = true;
40 | DInputClock._state_StateChange();
41 |
42 | Assert.IsTrue(DOutput.State == true);
43 |
44 | //Set data to false
45 | testOutput.State = false;
46 | DInputData._state_StateChange();
47 |
48 | Assert.IsTrue(DOutput.State == true);
49 |
50 | //Pulse clock
51 | testClock.State = false;
52 | DInputClock._state_StateChange();
53 |
54 | Assert.IsTrue(DOutput.State == true);
55 |
56 | testClock.State = true;
57 | DInputClock._state_StateChange();
58 |
59 | Assert.IsTrue(DOutput.State == false);
60 | }
61 |
62 | [TestMethod]
63 | public void TestJK()
64 | {
65 | JK JKFlipFlop = new JK();
66 | Input JKInputJ = (Input)JKFlipFlop.FindName("InputJ");
67 | Input JKInputClock = (Input)JKFlipFlop.FindName("InputClock");
68 | Input JKInputK = (Input)JKFlipFlop.FindName("InputK");
69 | Output JKOutput = (Output)JKFlipFlop.FindName("Output");
70 |
71 | Output testJ = new Output();
72 | testJ.State = false;
73 | Output testClock = new Output();
74 | testClock.State = false;
75 | Output testK = new Output();
76 | testK.State = false;
77 |
78 | JKInputJ.LinkInputs(testJ);
79 | JKInputJ._state_StateChange();
80 |
81 | JKInputClock.LinkInputs(testClock);
82 | JKInputClock._state_StateChange();
83 |
84 | JKInputK.LinkInputs(testK);
85 | JKInputK._state_StateChange();
86 |
87 | Assert.IsTrue(JKOutput.State == false);
88 |
89 | //Test Toggle
90 | testJ.State = true;
91 | JKInputJ._state_StateChange();
92 | testK.State = true;
93 | JKInputK._state_StateChange();
94 | testClock.State = true;
95 | JKInputClock._state_StateChange();
96 |
97 | Assert.IsTrue(JKOutput.State == true);
98 |
99 | //Test K = 1 J = 0
100 | testClock.State = false;
101 | JKInputClock._state_StateChange();
102 | testJ.State = false;
103 | JKInputJ._state_StateChange();
104 | testClock.State = true;
105 | JKInputClock._state_StateChange();
106 |
107 | Assert.IsTrue(JKOutput.State == false);
108 |
109 | //Test J = 1 K = 0
110 | testClock.State = false;
111 | JKInputClock._state_StateChange();
112 | testJ.State = true;
113 | JKInputJ._state_StateChange();
114 | testK.State = false;
115 | JKInputK._state_StateChange();
116 | testClock.State = true;
117 | JKInputClock._state_StateChange();
118 |
119 | Assert.IsTrue(JKOutput.State == true);
120 |
121 | //Test no change (j&k = 0)
122 | testClock.State = false;
123 | JKInputClock._state_StateChange();
124 | testJ.State = false;
125 | JKInputJ._state_StateChange();
126 | testK.State = false;
127 | JKInputK._state_StateChange();
128 | testClock.State = true;
129 | JKInputClock._state_StateChange();
130 |
131 | Assert.IsTrue(JKOutput.State == true);
132 | }
133 | }
134 | }
135 |
--------------------------------------------------------------------------------
/CircuitSim/CircuitSimTest/GatesTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Microsoft.VisualStudio.TestTools.UnitTesting;
3 | using CircuitSim.Gates;
4 | using System.Windows;
5 | using CircuitSim.BaseObjects;
6 | using System.Windows.Media;
7 |
8 | namespace CircuitSimTest
9 | {
10 | [TestClass]
11 | public class GatesTest
12 | {
13 | [TestMethod]
14 | public void TestNot()
15 | {
16 | Not not = new Not();
17 | Input notInput = (Input)not.FindName("Input");
18 | Output notOutput = (Output)not.FindName("Output");
19 |
20 | Output testOutput = new Output();
21 | testOutput.State = false;
22 |
23 | notInput.LinkInputs(testOutput);
24 | notInput._state_StateChange();
25 |
26 | Assert.IsTrue(notOutput.State == true);
27 | Assert.IsTrue(notInput.State == false);
28 |
29 | testOutput.State = true;
30 | notInput._state_StateChange();
31 |
32 | Assert.IsTrue(notOutput.State == false);
33 | Assert.IsTrue(notInput.State == true);
34 | }
35 |
36 | [TestMethod]
37 | public void TestAnd()
38 | {
39 | And and = new And();
40 | Input InputOne = (Input)and.FindName("InputOne");
41 | Input InputTwo = (Input)and.FindName("InputTwo");
42 | Output andOutput = (Output)and.FindName("Output");
43 |
44 | Output testOutputOne = new Output();
45 | testOutputOne.State = false;
46 | Output testOutputTwo = new Output();
47 | testOutputTwo.State = false;
48 |
49 | InputOne.LinkInputs(testOutputOne);
50 | InputTwo.LinkInputs(testOutputTwo);
51 |
52 | InputOne._state_StateChange();
53 | InputTwo._state_StateChange();
54 |
55 | //Input 0 0
56 | Assert.IsTrue(InputOne.State == false);
57 | Assert.IsTrue(InputTwo.State == false);
58 | Assert.IsTrue(andOutput.State == false);
59 |
60 | testOutputOne.State = true;
61 | InputOne._state_StateChange();
62 |
63 | //Input 1 0
64 | Assert.IsTrue(InputOne.State == true);
65 | Assert.IsTrue(InputTwo.State == false);
66 | Assert.IsTrue(andOutput.State == false);
67 |
68 | testOutputTwo.State = true;
69 | InputTwo._state_StateChange();
70 |
71 | //Input 1 1
72 | Assert.IsTrue(InputOne.State == true);
73 | Assert.IsTrue(InputTwo.State == true);
74 | Assert.IsTrue(andOutput.State == true);
75 |
76 | testOutputOne.State = false;
77 | InputOne._state_StateChange();
78 |
79 | //Input 0 1
80 | Assert.IsTrue(InputOne.State == false);
81 | Assert.IsTrue(InputTwo.State == true);
82 | Assert.IsTrue(andOutput.State == false);
83 | }
84 |
85 | [TestMethod]
86 | public void TestOr()
87 | {
88 | Or or = new Or();
89 | Input InputOne = (Input)or.FindName("InputOne");
90 | Input InputTwo = (Input)or.FindName("InputTwo");
91 | Output orOutput = (Output)or.FindName("Output");
92 |
93 | Output testOutputOne = new Output();
94 | testOutputOne.State = false;
95 | Output testOutputTwo = new Output();
96 | testOutputTwo.State = false;
97 |
98 | InputOne.LinkInputs(testOutputOne);
99 | InputTwo.LinkInputs(testOutputTwo);
100 |
101 | InputOne._state_StateChange();
102 | InputTwo._state_StateChange();
103 |
104 | //Input 0 0
105 | Assert.IsTrue(InputOne.State == false);
106 | Assert.IsTrue(InputTwo.State == false);
107 | Assert.IsTrue(orOutput.State == false);
108 |
109 | testOutputOne.State = true;
110 | InputOne._state_StateChange();
111 |
112 | //Input 1 0
113 | Assert.IsTrue(InputOne.State == true);
114 | Assert.IsTrue(InputTwo.State == false);
115 | Assert.IsTrue(orOutput.State == true);
116 |
117 | testOutputTwo.State = true;
118 | InputTwo._state_StateChange();
119 |
120 | //Input 1 1
121 | Assert.IsTrue(InputOne.State == true);
122 | Assert.IsTrue(InputTwo.State == true);
123 | Assert.IsTrue(orOutput.State == true);
124 |
125 | testOutputOne.State = false;
126 | InputOne._state_StateChange();
127 |
128 | //Input 0 1
129 | Assert.IsTrue(InputOne.State == false);
130 | Assert.IsTrue(InputTwo.State == true);
131 | Assert.IsTrue(orOutput.State == true);
132 | }
133 | }
134 | }
135 |
--------------------------------------------------------------------------------
/CircuitSim/CircuitSimTest/IOTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Microsoft.VisualStudio.TestTools.UnitTesting;
3 | using CircuitSim.BaseObjects;
4 | using CircuitSim.IO;
5 | using System.Windows.Shapes;
6 | using System.Windows.Media;
7 | using System.Windows.Controls;
8 | using System.Windows;
9 | using System.Windows.Input;
10 | using System.Windows.Controls.Primitives;
11 |
12 | namespace CircuitSimTest
13 | {
14 | [TestClass]
15 | public class IOTest
16 | {
17 | [TestMethod]
18 | public void TestInputOnTick()
19 | {
20 | Output o = new Output();
21 |
22 | Input i = new Input();
23 | i.LinkInputs(o);
24 | Assert.IsTrue(i.State == false);
25 | o.State = true;
26 |
27 | //Hasn't been ticked yet
28 | Assert.IsTrue(i.State == false);
29 | i._state_StateChange();
30 |
31 | //Has been ticked
32 | Assert.IsTrue(i.State == true);
33 | }
34 |
35 | [TestMethod]
36 | public void TestInputOnNoTick()
37 | {
38 | Output o = new Output();
39 |
40 | Input i = new Input();
41 | i.LinkInputs(o);
42 | Assert.IsTrue(i.State == false);
43 | o.State = true;
44 |
45 | Assert.IsTrue(i.State == false);
46 | }
47 |
48 | [TestMethod]
49 | public void TestOutputOnTick()
50 | {
51 | Output o = new Output();
52 | bool testCallback = false;
53 | o.StateChange += delegate()
54 | {
55 | testCallback = true;
56 | };
57 |
58 | Assert.IsTrue(o.State == false);
59 | o.State = true;
60 | o.CallChange();
61 | Assert.IsTrue(testCallback == true);
62 | }
63 |
64 | [TestMethod]
65 | public void TestOutputOnNoTick()
66 | {
67 | Output o = new Output();
68 | bool testCallback = false;
69 | o.StateChange += delegate()
70 | {
71 | testCallback = true;
72 | };
73 |
74 | Assert.IsTrue(o.State == false);
75 | o.State = true;
76 | Assert.IsTrue(testCallback == false);
77 | }
78 |
79 | [TestMethod]
80 | public void TestJunction()
81 | {
82 | Junction junction = new Junction();
83 | Input JunctionInput = (Input)junction.FindName("Input");
84 | Output JunctionOutput = (Output)junction.FindName("Output");
85 |
86 | Output testOutput = new Output();
87 | testOutput.State = false;
88 |
89 | JunctionInput.LinkInputs(testOutput);
90 | JunctionInput._state_StateChange();
91 |
92 | Assert.IsTrue(JunctionOutput.State == false);
93 |
94 | testOutput.State = true;
95 | JunctionInput._state_StateChange();
96 |
97 | Assert.IsTrue(JunctionOutput.State == true);
98 | }
99 |
100 | [TestMethod]
101 | public void TestClock()
102 | {
103 | Clock clock = new Clock();
104 | Output ClockOutput = (Output)clock.FindName("PowerOutput");
105 |
106 | Assert.IsTrue(ClockOutput.State == false);
107 |
108 | ClockOutput.CallChange();
109 | Assert.IsTrue(ClockOutput.State == true);
110 |
111 | ClockOutput.CallChange();
112 | Assert.IsTrue(ClockOutput.State == false);
113 | }
114 |
115 | [TestMethod]
116 | public void TestLED()
117 | {
118 | LED led = new LED();
119 | Input LEDInput = (Input)led.FindName("LeftInput");
120 | Rectangle LEDRectangle = (Rectangle)led.FindName("LEDRect");
121 |
122 | Output testOutput = new Output();
123 | testOutput.State = false;
124 |
125 | LEDInput.LinkInputs(testOutput);
126 | LEDInput._state_StateChange();
127 |
128 | Assert.IsTrue(((SolidColorBrush)LEDRectangle.Fill).Color == Colors.Black);
129 |
130 | testOutput.State = true;
131 | LEDInput._state_StateChange();
132 |
133 | Assert.IsTrue(((SolidColorBrush)LEDRectangle.Fill).Color == Colors.Red);
134 | }
135 |
136 | [TestMethod]
137 | public void TestPower()
138 | {
139 | Power power = new Power();
140 | Output PowerOutput = (Output)power.FindName("PowerOutput");
141 |
142 | Assert.IsTrue(PowerOutput.State == true);
143 | }
144 |
145 | [TestMethod]
146 | public void TestToggleSwitch()
147 | {
148 | ToggleSwitch toggle = new ToggleSwitch();
149 | Ellipse ToggleButton = (Ellipse)toggle.FindName("ToggleRect");
150 | Output ToggleOutput = (Output)toggle.FindName("Output");
151 |
152 | Assert.IsTrue(ToggleOutput.State == false);
153 |
154 | //Simulate click on the toggle button
155 | ToggleButton.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left) { RoutedEvent = Mouse.MouseDownEvent, Source = this });
156 | ToggleOutput.CallChange();
157 |
158 | Assert.IsTrue(ToggleOutput.State == true);
159 | }
160 |
161 | [TestMethod]
162 | public void TestTextLED()
163 | {
164 | TextLED textLED = new TextLED();
165 |
166 | //Linked backwards to have compatibility with CEDAR
167 | Input InputOne = (Input)textLED.FindName("Input8");
168 | Input InputTwo = (Input)textLED.FindName("Input7");
169 |
170 | Label DecimalLabel = (Label)textLED.FindName("DecimalOutput");
171 | Label HexadecimalLabel = (Label)textLED.FindName("HexOutput");
172 | Label BinaryLabel = (Label)textLED.FindName("BinaryOutput");
173 |
174 | Output outputOne = new Output();
175 | InputOne.LinkInputs(outputOne);
176 | Output outputTwo = new Output();
177 | InputTwo.LinkInputs(outputTwo);
178 |
179 | Assert.IsTrue((string)DecimalLabel.Content == "0");
180 | Assert.IsTrue((string)HexadecimalLabel.Content == "0x00");
181 | Assert.IsTrue((string)BinaryLabel.Content == "0b00000000");
182 |
183 | outputOne.State = true;
184 | InputOne._state_StateChange();
185 |
186 | Console.WriteLine(BinaryLabel.Content);
187 | Assert.IsTrue((int)DecimalLabel.Content == 1);
188 | Assert.IsTrue((string)HexadecimalLabel.Content == "0x01");
189 | Assert.IsTrue((string)BinaryLabel.Content == "0b00000001");
190 |
191 | outputOne.State = false;
192 | InputOne._state_StateChange();
193 | outputTwo.State = true;
194 | InputTwo._state_StateChange();
195 |
196 | Assert.IsTrue((int)DecimalLabel.Content == 2);
197 | Assert.IsTrue((string)HexadecimalLabel.Content == "0x02");
198 | Assert.IsTrue((string)BinaryLabel.Content == "0b00000010");
199 |
200 | outputOne.State = true;
201 | InputOne._state_StateChange();
202 |
203 | Assert.IsTrue((int)DecimalLabel.Content == 3);
204 | Assert.IsTrue((string)HexadecimalLabel.Content == "0x03");
205 | Assert.IsTrue((string)BinaryLabel.Content == "0b00000011");
206 | }
207 | }
208 | }
209 |
--------------------------------------------------------------------------------
/CircuitSim/CircuitSimTest/Integration.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using CircuitSim;
3 | using Microsoft.VisualStudio.TestTools.UnitTesting;
4 | using CircuitSim.BaseObjects;
5 |
6 | namespace CircuitSimTest
7 | {
8 | [TestClass]
9 | public class Integration
10 | {
11 | [TestMethod]
12 | public void TestLinkingInputs()
13 | {
14 | Input input = new Input();
15 | Output output = new Output();
16 |
17 | input.LinkInputs(output);
18 |
19 | Assert.IsTrue(input.State == false);
20 | Assert.IsTrue(output.State == false);
21 |
22 | output.State = true;
23 | input._state_StateChange();
24 |
25 | Assert.IsTrue(input.State == true);
26 | Assert.IsTrue(output.State == true);
27 | }
28 |
29 | [TestMethod]
30 | public void TestInputEvent()
31 | {
32 | Input input = new Input();
33 | Output output = new Output();
34 | input.StateChanged += delegate ()
35 | {
36 | Assert.IsTrue(input.State == output.State);
37 | };
38 |
39 | input.LinkInputs(output);
40 |
41 | Assert.IsTrue(input.State == false);
42 | Assert.IsTrue(output.State == false);
43 |
44 | output.State = true;
45 | input._state_StateChange();
46 |
47 | Assert.IsTrue(input.State == true);
48 | Assert.IsTrue(output.State == true);
49 | }
50 |
51 | [TestMethod]
52 | public void TestOutputEvent()
53 | {
54 | Output output = new Output();
55 | output.StateChange += delegate ()
56 | {
57 | Assert.IsTrue(output.State == true);
58 | };
59 |
60 | output.State = true;
61 | }
62 | }
63 | }
64 |
--------------------------------------------------------------------------------
/CircuitSim/CircuitSimTest/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("SEDARTest")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("SEDARTest")]
13 | [assembly: AssemblyCopyright("Copyright © 2014")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("f31e32f6-bc99-4572-b178-c8893e6e08ec")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014 David Diaz
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | CircuitSimulator
2 | ================
3 |
4 | Basic Circuit Simulator built as a university project using WPF.
5 |
6 | 
7 |
--------------------------------------------------------------------------------