Visual studio c# and visual basic with logic gates
Circuits/.DS_Store
__MACOSX/Circuits/._.DS_Store
Circuits/Circuits.userprefs
<Properties StartupConfiguration="{A1EF5537-EB77-4262-8A4F-48498ECD08EA}|Default"> <MonoDevelop.Ide.Workbench ActiveDocument="Circuits/Pin.cs"> <Files> <File FileName="Circuits/AndGate.cs" Line="10" Column="42" /> <File FileName="Circuits/Wire.cs" Line="1" Column="1" /> <File FileName="Circuits/Program.cs" Line="17" Column="42" /> <File FileName="Circuits/Pin.cs" Line="1" Column="1" /> </Files> </MonoDevelop.Ide.Workbench> <MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" /> <MonoDevelop.Ide.DebuggingService.Breakpoints> <BreakpointStore /> </MonoDevelop.Ide.DebuggingService.Breakpoints> <MonoDevelop.Ide.DebuggingService.PinnedWatches /> <MultiItemStartupConfigurations /> <MonoDevelop.Ide.ItemProperties.Circuits PreferredExecutionTarget="MonoDevelop.Default" /> </Properties>
Circuits/Circuits/AndGate.cs
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Diagnostics; namespace Circuits { /// <summary> /// This class implements an AND gate with two inputs /// and one output. /// </summary> public class AndGate { // left is the left-hand edge of the main part of the gate. // So the input pins are further left than left. protected int left; // top is the top of the whole gate protected int top; // width and height of the main part of the gate protected const int WIDTH = 40; protected const int HEIGHT = 40; // length of the connector legs sticking out left and right protected const int GAP = 10; protected Brush selectedBrush = Brushes.Red; protected Brush normalBrush = Brushes.LightGray; /// <summary> /// This is the list of all the pins of this gate. /// An AND gate always has two input pins (0 and 1) /// and one output pin (number 2). /// </summary> protected List<Pin> pins = new List<Pin>(); protected bool selected = false; public AndGate(int x, int y) { pins.Add(new Pin(this, true, 20)); pins.Add(new Pin(this, true, 20)); pins.Add(new Pin(this, false, 20)); MoveTo(x, y); // move the gate and position the pins } /// <summary> /// Indicates whether this gate is the current one selected. /// </summary> public bool Selected { get { return selected; } set { selected = value; } } public int Left { get { return left; } } public int Top { get { return top; } } public List<Pin> Pins { get { return pins; } } /// <summary> /// True if the given (x,y) position is roughly /// on top of this gate. /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public bool IsMouseOn(int x, int y) { if (left <= x && x < left + WIDTH && top <= y && y < top + HEIGHT) return true; else return false; } public void Draw(Graphics paper) { Brush brush; if (selected) { brush = selectedBrush; } else { brush = normalBrush; } foreach (Pin p in pins) p.Draw(paper); // AND is simple, so we can use a circle plus a rectange. // An alternative would be to use a bitmap. paper.FillEllipse(brush, left, top, WIDTH, HEIGHT); paper.FillRectangle(brush, left, top, WIDTH/2, HEIGHT); } public void MoveTo(int x, int y) { Debug.WriteLine("pins = " + pins.Count); left = x; top = y; // must move the pins too pins[0].X = x - GAP; pins[0].Y = y + GAP; pins[1].X = x - GAP; pins[1].Y = y + HEIGHT - GAP; pins[2].X = x + WIDTH + GAP; pins[2].Y = y + HEIGHT / 2; } } }
__MACOSX/Circuits/Circuits/._AndGate.cs
Circuits/Circuits/obj/Debug/Circuits.csproj.FilesWrittenAbsolute.txt
/Users/Jashwanth/Downloads/Circuits/Circuits/obj/Debug/Circuits.Form1.resources /Users/Jashwanth/Downloads/Circuits/Circuits/obj/Debug/Circuits.Properties.Resources.resources /Users/Jashwanth/Downloads/Circuits/Circuits/bin/Debug/Circuits.pdb /Users/Jashwanth/Downloads/Circuits/Circuits/bin/Debug/Circuits.exe /Users/Jashwanth/Downloads/Circuits/Circuits/obj/Debug/Circuits.exe /Users/Jashwanth/Downloads/Circuits/Circuits/obj/Debug/Circuits.pdb
Circuits/Circuits/obj/Debug/Circuits.csproj.CoreCompileInputs.cache
c8c75224a9f2075f0c5561e3358dc4d3e8264652
Circuits/Circuits/obj/Debug/Circuits.pdb
__MACOSX/Circuits/Circuits/obj/Debug/._Circuits.pdb
Circuits/Circuits/obj/Debug/Circuits.exe
__MACOSX/Circuits/Circuits/obj/Debug/._Circuits.exe
Circuits/Circuits/obj/Debug/TempPE/Properties.Resources.Designer.cs.dll
__MACOSX/Circuits/Circuits/obj/Debug/TempPE/._Properties.Resources.Designer.cs.dll
__MACOSX/Circuits/Circuits/obj/Debug/._TempPE
Circuits/Circuits/obj/Debug/GenerateResource-ResGen.read.1.tlog
^\\OSLO.SCMS.WAIKATO.AC.NZ\HOME\TELPHICK\DESKTOP\CIRCUITS\CIRCUITS\FORM1.RESX ^\\OSLO.SCMS.WAIKATO.AC.NZ\HOME\TELPHICK\DESKTOP\CIRCUITS\CIRCUITS\PROPERTIES\RESOURCES.RESX C:\WINDOWS\MICROSOFT.NET\FRAMEWORK\V2.0.50727\CONFIG\SECURITY.CONFIG
__MACOSX/Circuits/Circuits/obj/Debug/._GenerateResource-ResGen.read.1.tlog
Circuits/Circuits/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache
__MACOSX/Circuits/Circuits/obj/Debug/._DesignTimeResolveAssemblyReferencesInput.cache
Circuits/Circuits/obj/Debug/Circuits.csproj.FileListAbsolute.txt
//oslo.scms.waikato.ac.nz/home/telphick/Desktop/Circuits/Circuits/bin/Debug/Circuits.exe //oslo.scms.waikato.ac.nz/home/telphick/Desktop/Circuits/Circuits/bin/Debug/Circuits.pdb //oslo.scms.waikato.ac.nz/home/telphick/Desktop/Circuits/Circuits/obj/Debug/ResolveAssemblyReference.cache //oslo.scms.waikato.ac.nz/home/telphick/Desktop/Circuits/Circuits/obj/Debug/Circuits.Form1.resources //oslo.scms.waikato.ac.nz/home/telphick/Desktop/Circuits/Circuits/obj/Debug/Circuits.Properties.Resources.resources //oslo.scms.waikato.ac.nz/home/telphick/Desktop/Circuits/Circuits/obj/Debug/Circuits.exe //oslo.scms.waikato.ac.nz/home/telphick/Desktop/Circuits/Circuits/obj/Debug/Circuits.pdb //oslo.scms.waikato.ac.nz/home/telphick/Desktop/Circuits/Circuits/obj/Debug/GenerateResource-ResGen.read.1.tlog //oslo.scms.waikato.ac.nz/home/telphick/Desktop/Circuits/Circuits/obj/Debug/GenerateResource-ResGen.write.1.tlog /Users/Jashwanth/Downloads/Circuits/Circuits/bin/Debug/Circuits.exe /Users/Jashwanth/Downloads/Circuits/Circuits/bin/Debug/Circuits.pdb /Users/Jashwanth/Downloads/Circuits/Circuits/obj/Debug/Circuits.Form1.resources /Users/Jashwanth/Downloads/Circuits/Circuits/obj/Debug/Circuits.Properties.Resources.resources /Users/Jashwanth/Downloads/Circuits/Circuits/obj/Debug/Circuits.csproj.GenerateResource.Cache /Users/Jashwanth/Downloads/Circuits/Circuits/obj/Debug/Circuits.csproj.CoreCompileInputs.cache /Users/Jashwanth/Downloads/Circuits/Circuits/obj/Debug/Circuits.exe /Users/Jashwanth/Downloads/Circuits/Circuits/obj/Debug/Circuits.pdb
__MACOSX/Circuits/Circuits/obj/Debug/._Circuits.csproj.FileListAbsolute.txt
Circuits/Circuits/obj/Debug/Circuits.csproj.GenerateResource.Cache
Circuits/Circuits/obj/Debug/Circuits.Form1.resources
__MACOSX/Circuits/Circuits/obj/Debug/._Circuits.Form1.resources
Circuits/Circuits/obj/Debug/GenerateResource-ResGen.write.1.tlog
^\\OSLO.SCMS.WAIKATO.AC.NZ\HOME\TELPHICK\DESKTOP\CIRCUITS\CIRCUITS\FORM1.RESX|\\OSLO.SCMS.WAIKATO.AC.NZ\HOME\TELPHICK\DESKTOP\CIRCUITS\CIRCUITS\PROPERTIES\RESOURCES.RESX \\OSLO.SCMS.WAIKATO.AC.NZ\HOME\TELPHICK\DESKTOP\CIRCUITS\CIRCUITS\OBJ\DEBUG\CIRCUITS.FORM1.RESOURCES \\OSLO.SCMS.WAIKATO.AC.NZ\HOME\TELPHICK\DESKTOP\CIRCUITS\CIRCUITS\OBJ\DEBUG\CIRCUITS.PROPERTIES.RESOURCES.RESOURCES
__MACOSX/Circuits/Circuits/obj/Debug/._GenerateResource-ResGen.write.1.tlog
Circuits/Circuits/obj/Debug/Circuits.Properties.Resources.resources
__MACOSX/Circuits/Circuits/obj/Debug/._Circuits.Properties.Resources.resources
__MACOSX/Circuits/Circuits/obj/._Debug
__MACOSX/Circuits/Circuits/._obj
Circuits/Circuits/bin/Debug/Circuits.pdb
Circuits/Circuits/bin/Debug/Circuits.exe
Circuits/Circuits/Circuits.userprefs
<Properties StartupConfiguration="{A1EF5537-EB77-4262-8A4F-48498ECD08EA}|Default"> <MonoDevelop.Ide.Workbench ActiveDocument="Program.cs"> <Files> <File FileName="Form1.cs" Line="1" Column="1" /> <File FileName="Program.cs" Line="11" Column="23" /> </Files> </MonoDevelop.Ide.Workbench> <MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" /> <MonoDevelop.Ide.DebuggingService.Breakpoints> <BreakpointStore /> </MonoDevelop.Ide.DebuggingService.Breakpoints> <MonoDevelop.Ide.DebuggingService.PinnedWatches /> <MonoDevelop.Ide.ItemProperties.Circuits PreferredExecutionTarget="MonoDevelop.Default" /> </Properties>
Circuits/Circuits/Pin.cs
using System; using System.Collections.Generic; using System.Text; using System.Drawing; namespace Circuits { /// <summary> /// Each Pin represents an input or an output of a gate. /// Every Pin knows which gate it belongs to /// (and the Gate property returns this). /// /// Input pins can be connected to at most one wire /// (see the InputWire property). /// /// Output pins may have lots of wires pointing to them, /// but they don't know anything about this. /// </summary> public class Pin { protected int x,y; protected bool input; protected int length; protected AndGate owner; protected Wire connection; public Pin(AndGate gate, bool input, int length) { this.owner = gate; this.input = input; this.length = length; } /// <summary> /// A read-only property that returns true for input pins /// and false for output pins. /// </summary> public bool IsInput { get { return input; } } /// <summary> /// Returns true for output pins, false for input pins. /// </summary> public bool IsOutput { get { return !input; } } /// <summary> /// This read-only property returns the gate that this pin /// belongs to. /// </summary> public AndGate Owner { get { return owner; } } /// <summary> /// For input pins, this gets or sets the wire that is coming /// into the pin. (Input pins can only be connected to one wire) /// For output pins, sets are ignored and get always returns null. /// </summary> public Wire InputWire { get { return connection; } set { if (input) { connection = value; } } } /// <summary> /// Get or set the X position of this pin. /// For input pins, this is at the left hand side of the pin. /// For output pins, this is at the right hand side. /// </summary> public int X { get { return x; } set { x = value; } } /// <summary> /// Get or set the Y position of this pin. /// </summary> public int Y { get { return y; } set { y = value; } } /// <summary> /// True if (mouseX, mouseY) is within 3 pixels of the business /// end of the pin. /// </summary> /// <param name="mouseX">The X position of the mouse</param> /// <param name="mouseY">The Y position of the mouse</param> /// <returns>true if mouse is close to the main end of the pin</returns> public bool isMouseOn(int mouseX, int mouseY) { int diffX = mouseX - x; int diffY = mouseY - y; return diffX * diffX + diffY * diffY <= 5*5; } public void Draw(Graphics paper) { Brush brush = Brushes.DarkGray; if (input) { paper.FillRectangle(brush, x - 1, y - 1, length, 3); } else { paper.FillRectangle(brush, x - length + 1, y - 1, length, 3); } } public override string ToString() { if (input) return "InPin(" + x + "," + y + ")"; else return "OutPin(" + x + "," + y + ")"; } } }
__MACOSX/Circuits/Circuits/._Pin.cs
Circuits/Circuits/Form1.resx
text/microsoft-resx 2.0 System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 17, 17 iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8 YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAgxJREFUOE+lkvtL U2EYx+0PEbtpFwnBKPGKiJImGP0gYhIYs1E5GF5gIxkpA00JRSmMEF0ohMh+GaRWYlqabMVcNdS2QpaI VqiDIYhk397vA6fXhCjyhYdzeM/5fp7vczkAdeL2cwho7v/wWzT1zcN+Pwhr51uY2/y41PQaF+wzKKiZ QvaN58g0jyLd5KEUcQbg+84P/Cm2tncQjW3j68YWIqubCC3FcOJc478BAuGoZM6zvoRnakXEruEIjhc4 /g5gZop9c+voGAyLbQIfeBZxLL9BA1jzXvuGbWamuKh+GmmVbswE19A59FEBbmoAG7YbsLtm2mZmiml9 cvabNDwpz6YB7LYBoMXCumkJr7LOmnnHzBQ/9X2Bo2cOibm1GsBREbAQiYmw/8lnuCeWkVzcgnZlnw1j 3HV/wuNXK6i/9x5Hc6wawDlTXHbLJ+LZUBQPRyKwdQdxutwl1h+NLXHh5Ht1ewBHsiwawCW57HyDAfWR dvl0uhZQ1eqX8aVc7EKLqrum651ATLf9OJx5XQM4KmY0xPzZ0hFAiQJnXB0WwME0E3IsL5B17ZlADqWb NYDrOepdlcysmTWWOrxqbceRWtaLk0VO1XW72D5Vckd2gMBfq8zdpmUG62NJvKM4+XyziDk24xmfWoGE s1c0gHPmbrPTpHNJKOCo2G1mZs20zcwUJ5yp1AB5+8/zEwgF5GMVDxh4AAAAAElFTkSuQmCC
__MACOSX/Circuits/Circuits/._Form1.resx
Circuits/Circuits/Circuits.sln
Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 2012 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Circuits", "Circuits.csproj", "{A1EF5537-EB77-4262-8A4F-48498ECD08EA}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {A1EF5537-EB77-4262-8A4F-48498ECD08EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {A1EF5537-EB77-4262-8A4F-48498ECD08EA}.Debug|Any CPU.Build.0 = Debug|Any CPU {A1EF5537-EB77-4262-8A4F-48498ECD08EA}.Release|Any CPU.ActiveCfg = Release|Any CPU {A1EF5537-EB77-4262-8A4F-48498ECD08EA}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal
Circuits/Circuits/Form1.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Diagnostics; namespace Circuits { /// <summary> /// The main GUI for the COMP104 digital circuits editor. /// This has a toolbar, containing buttons called buttonAnd, buttonOr, etc. /// The contents of the circuit are drawn directly onto the form. /// /// </summary> public partial class Form1 : Form { /// <summary> /// The (x,y) mouse position of the last MouseDown event. /// </summary> protected int startX, startY; /// <summary> /// If this is non-null, we are inserting a wire by /// dragging the mouse from startPin to some output Pin. /// </summary> protected Pin startPin = null; /// <summary> /// The (x,y) position of the current gate, just before we started dragging it. /// </summary> protected int currentX, currentY; /// <summary> /// The set of gates in the circuit /// </summary> protected List<AndGate> gates = new List<AndGate>(); /// <summary> /// The set of connector wires in the circuit /// </summary> protected List<Wire> wires = new List<Wire>(); /// <summary> /// The currently selected gate, or null if no gate is selected. /// </summary> protected AndGate current = null; /// <summary> /// The new gate that is about to be inserted into the circuit /// </summary> protected AndGate newGate = null; public Form1() { InitializeComponent(); DoubleBuffered = true; } /// <summary> /// Finds the pin that is close to (x,y), or returns /// null if there are no pins close to the position. /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public Pin findPin(int x, int y) { foreach (AndGate g in gates) { foreach (Pin p in g.Pins) { if (p.isMouseOn(x, y)) return p; } } return null; } private void Form1_Paint(object sender, PaintEventArgs e) { foreach (AndGate g in gates) { g.Draw(e.Graphics); } foreach (Wire w in wires) { w.Draw(e.Graphics); } if (startPin != null) { e.Graphics.DrawLine(Pens.White, startPin.X, startPin.Y, currentX, currentY); } if (newGate != null) { // show the gate that we are dragging into the circuit newGate.MoveTo(currentX, currentY); newGate.Draw(e.Graphics); } } private void Form1_MouseClick(object sender, MouseEventArgs e) { if (current != null) { current.Selected = false; current = null; this.Invalidate(); } // See if we are inserting a new gate if (newGate != null) { newGate.MoveTo(e.X, e.Y); gates.Add(newGate); newGate = null; this.Invalidate(); } else { // search for the first gate under the mouse position foreach (AndGate g in gates) { if (g.IsMouseOn(e.X, e.Y)) { g.Selected = true; current = g; this.Invalidate(); break; } } } } private void Form1_MouseDown(object sender, MouseEventArgs e) { if (current == null) { // try to start adding a wire startPin = findPin(e.X, e.Y); } else if (current.IsMouseOn(e.X, e.Y)) { // start dragging the current object around startX = e.X; startY = e.Y; currentX = current.Left; currentY = current.Top; } } private void Form1_MouseMove(object sender, MouseEventArgs e) { if (startPin != null) { Debug.WriteLine("wire from "+startPin+" to " + e.X + "," + e.Y); currentX = e.X; currentY = e.Y; this.Invalidate(); // this will draw the line } else if (startX >= 0 && startY >= 0 && current != null) { Debug.WriteLine("mouse move to " + e.X + "," + e.Y); current.MoveTo(currentX + (e.X - startX), currentY + (e.Y - startY)); this.Invalidate(); } else if (newGate != null) { currentX = e.X; currentY = e.Y; this.Invalidate(); } } private void Form1_MouseUp(object sender, MouseEventArgs e) { if (startPin != null) { // see if we can insert a wire Pin endPin = findPin(e.X, e.Y); if (endPin != null) { Debug.WriteLine("Trying to connect " + startPin + " to " + endPin); Pin input, output; if (startPin.IsOutput) { input = endPin; output = startPin; } else { input = startPin; output = endPin; } if (input.IsInput && output.IsOutput) { if (input.InputWire == null) { Wire newWire = new Wire(output, input); input.InputWire = newWire; wires.Add(newWire); } else { MessageBox.Show("That input is already used."); } } else { MessageBox.Show("Error: you must connect an output pin to an input pin."); } } startPin = null; this.Invalidate(); } // We have finished moving/dragging startX = -1; startY = -1; currentX = 0; currentY = 0; } private void buttonAnd_Click(object sender, EventArgs e) { newGate = new AndGate(0, 0); } } }
__MACOSX/Circuits/Circuits/._Form1.cs
Circuits/Circuits/Form1.Designer.cs
namespace Circuits { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); this.toolStrip1 = new System.Windows.Forms.ToolStrip(); this.buttonAnd = new System.Windows.Forms.ToolStripButton(); this.toolStrip1.SuspendLayout(); this.SuspendLayout(); // // toolStrip1 // this.toolStrip1.BackColor = System.Drawing.SystemColors.ControlLight; this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.buttonAnd}); this.toolStrip1.Location = new System.Drawing.Point(0, 0); this.toolStrip1.Name = "toolStrip1"; this.toolStrip1.Size = new System.Drawing.Size(472, 25); this.toolStrip1.TabIndex = 0; this.toolStrip1.Text = "toolStrip1"; // // buttonAnd // this.buttonAnd.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; this.buttonAnd.Image = ((System.Drawing.Image)(resources.GetObject("buttonAnd.Image"))); this.buttonAnd.ImageTransparentColor = System.Drawing.Color.Magenta; this.buttonAnd.Name = "buttonAnd"; this.buttonAnd.Size = new System.Drawing.Size(23, 22); this.buttonAnd.Text = "And"; this.buttonAnd.Click += new System.EventHandler(this.buttonAnd_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.SystemColors.HotTrack; this.ClientSize = new System.Drawing.Size(472, 307); this.Controls.Add(this.toolStrip1); this.Name = "Form1"; this.Text = "Digital Circuits 104"; this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseClick); this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp); this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove); this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown); this.toolStrip1.ResumeLayout(false); this.toolStrip1.PerformLayout(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.ToolStrip toolStrip1; private System.Windows.Forms.ToolStripButton buttonAnd; } }
__MACOSX/Circuits/Circuits/._Form1.Designer.cs
Circuits/Circuits/Properties/AssemblyInfo.cs
using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; // General Information about an assembly is controlled through the following // set of attributes. Change these attribute values to modify the information // associated with an assembly. [assembly: AssemblyTitle("Circuits")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("The University of Waikato")] [assembly: AssemblyProduct("Circuits")] [assembly: AssemblyCopyright("Copyright © The University of Waikato 2007")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] // Setting ComVisible to false makes the types in this assembly not visible // to COM components. If you need to access a type in this assembly from // COM, set the ComVisible attribute to true on that type. [assembly: ComVisible(false)] // The following GUID is for the ID of the typelib if this project is exposed to COM [assembly: Guid("419adfd9-6448-4bc8-8561-56d9f5d43900")] // Version information for an assembly consists of the following four values: // // Major Version // Minor Version // Build Number // Revision // [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")]
__MACOSX/Circuits/Circuits/Properties/._AssemblyInfo.cs
Circuits/Circuits/Properties/Settings.Designer.cs
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. // Runtime Version:4.0.30319.235 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace Circuits.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); public static Settings Default { get { return defaultInstance; } } } }
__MACOSX/Circuits/Circuits/Properties/._Settings.Designer.cs
Circuits/Circuits/Properties/Resources.resx
text/microsoft-resx 2.0 System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
__MACOSX/Circuits/Circuits/Properties/._Resources.resx
Circuits/Circuits/Properties/Settings.settings
__MACOSX/Circuits/Circuits/Properties/._Settings.settings
Circuits/Circuits/Properties/Resources.Designer.cs
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by a tool. // Runtime Version:4.0.30319.235 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace Circuits.Properties { using System; /// <summary> /// A strongly-typed resource class, for looking up localized strings, etc. /// </summary> // This class was auto-generated by the StronglyTypedResourceBuilder // class via a tool like ResGen or Visual Studio. // To add or remove a member, edit your .ResX file then rerun ResGen // with the /str option, or rebuild your VS project. [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] internal class Resources { private static global::System.Resources.ResourceManager resourceMan; private static global::System.Globalization.CultureInfo resourceCulture; [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] internal Resources() { } /// <summary> /// Returns the cached ResourceManager instance used by this class. /// </summary> [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] internal static global::System.Resources.ResourceManager ResourceManager { get { if (object.ReferenceEquals(resourceMan, null)) { global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Circuits.Properties.Resources", typeof(Resources).Assembly); resourceMan = temp; } return resourceMan; } } /// <summary> /// Overrides the current thread's CurrentUICulture property for all /// resource lookups using this strongly typed resource class. /// </summary> [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] internal static global::System.Globalization.CultureInfo Culture { get { return resourceCulture; } set { resourceCulture = value; } } } }
__MACOSX/Circuits/Circuits/Properties/._Resources.Designer.cs
__MACOSX/Circuits/Circuits/._Properties
Circuits/Circuits/Circuits.csproj
Debug AnyCPU 8.0.50727 2.0 {A1EF5537-EB77-4262-8A4F-48498ECD08EA} WinExe Properties Circuits Circuits 3.5 v2.0 true full false bin\Debug\ DEBUG;TRACE prompt 4 AllRules.ruleset pdbonly true bin\Release\ TRACE prompt 4 AllRules.ruleset Form Form1.cs Designer Form1.cs ResXFileCodeGenerator Resources.Designer.cs Designer True Resources.resx True SettingsSingleFileGenerator Settings.Designer.cs True Settings.settings True
__MACOSX/Circuits/Circuits/._Circuits.csproj
Circuits/Circuits/Wire.cs
using System; using System.Collections.Generic; using System.Text; using System.Drawing; namespace Circuits { /// <summary> /// A wire connects between two pins. /// That is, it connects the output pin FromPin /// to the input pin ToPin. /// </summary> public class Wire { protected bool selected = false; protected Pin fromPin, toPin; public Wire(Pin from, Pin to) { fromPin = from; toPin = to; } /// <summary> /// Indicates whether this gate is the current one selected. /// </summary> public bool Selected { get { return selected; } set { selected = value; } } /// <summary> /// The output pin that this wire is connected to. /// </summary> public Pin FromPin { get { return fromPin; } } /// <summary> /// The input pin that this wire is connected to. /// </summary> public Pin ToPin { get { return toPin; } } public void Draw(Graphics paper) { Pen wire = new Pen(selected ? Color.Red : Color.White, 3); paper.DrawLine(wire, fromPin.X, fromPin.Y, toPin.X, toPin.Y); } } }
__MACOSX/Circuits/Circuits/._Wire.cs
Circuits/Circuits/Program.cs
using System; using System.Collections.Generic; using System.Windows.Forms; namespace Circuits { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
__MACOSX/Circuits/Circuits/._Program.cs
__MACOSX/Circuits/._Circuits
Circuits/Circuits.sln
Microsoft Visual Studio Solution File, Format Version 11.00 # Visual Studio 2010 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Circuits", "Circuits\Circuits.csproj", "{A1EF5537-EB77-4262-8A4F-48498ECD08EA}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {A1EF5537-EB77-4262-8A4F-48498ECD08EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {A1EF5537-EB77-4262-8A4F-48498ECD08EA}.Debug|Any CPU.Build.0 = Debug|Any CPU {A1EF5537-EB77-4262-8A4F-48498ECD08EA}.Release|Any CPU.ActiveCfg = Release|Any CPU {A1EF5537-EB77-4262-8A4F-48498ECD08EA}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection EndGlobal
__MACOSX/Circuits/._Circuits.sln
Circuits/Circuits.suo
__MACOSX/Circuits/._Circuits.suo
__MACOSX/._Circuits
471502366/OrGateRed.png
__MACOSX/471502366/._OrGateRed.png
471502366/NotGateAllRed.png
__MACOSX/471502366/._NotGateAllRed.png
471502366/AndGateRed.png
__MACOSX/471502366/._AndGateRed.png
471502366/AndGateAllRed.png
__MACOSX/471502366/._AndGateAllRed.png
471502366/NotGateRed.png
__MACOSX/471502366/._NotGateRed.png
471502366/OrGateAllRed.png
__MACOSX/471502366/._OrGateAllRed.png
__MACOSX/._471502366
1964939428.pdf
1
COMP104-17B Assignment 1: Digital Circuit Design
Goals The main goals of this assignment are for you to learn how to write a
whole hierarchy of classes, and use subtyping (polymorphism) to make your programs work on just the superclass, rather than all the subclasses. This will simplify your programs and make it easy to add new subclasses. In short, you’ll be a far more elegant and sophisticated programmer!
Overview You are going to write a simple Digital Circuit Design program that
electronics engineers could use to design and test simple digital circuits. It will allow you to create various kinds of logic gates (AND/OR/NOT etc.), input sources and output lamps, move them around the design window, add wires to connect them together, and so on. Each logic gate and wire will be represented in the program by an object with methods like Draw, and properties like Left and Top.
You are given a very simple working version of this digital circuits program, which only handles AND gates and wires. Your first task will be to refactor this program to use inheritance, so that it is easier to add new kinds of logic gates. After you have done this, and added several new kinds of logic gates, you will add evaluation methods to the gates, so that each gate behaves like a real digital gate and computes its output signal based on what inputs it is connected to.
The final step will be to add support for compound circuits – so that an engineer can design a small circuit, then group all the gates in that circuit into one “uber-gate”, which can be moved around, copied and reused as a single object. For example, this could be used to create a half-adder gate from several simple gates, then two copies of that half-adder could be combined to give a 1-bit full adder.
COMP104
2
Above is a UML class diagram that shows an example of the kind of structure your final program should have (I have omitted Pin and its relationships).
Step 1: Becoming familiar with the example program Download the example Circuits.zip file from Moodle and unzip it in your
COMP104 directory. Open up the Circuits\Circuits.sln file with Visual Studio and explore the code and run the program until you understand how it works. Read the documentation at the top of each class to get an overview of the program.
Step 2: Creating an abstract Gate superclass This is the most important step of this project. You must create a new
Gate class, then move some of the AndGate methods, properties and variables up into your new Gate class. Which ones should you move up? The ones that seem to be useful for all kinds of Gates, and are not specific to AND gates. Then change your AndGate class so that it inherits from Gate. Don't forget to make methods virtual.
[Note: It would also be possible to change the Wire class to be a subclass of Gate if you want, but the advantages of doing this seem reasonably minor to me.]
Next change all the references to AndGate in the Form1 class so that they use Gate objects instead. (The only call to AndGate that you will need to keep is the one in the buttonAnd_Click handler, where a new AndGate object is created and put into the currentGate variable, which will now be of type Gate). Make sure that your program runs correctly, and test its behaviour.
If you find that your Gate class needs to define a method (like Draw), so that Form1 can call it, but the implementation code inside that method is obviously specific to AND gates, then make it an abstract method in the Gate class and override it with the correct implementation in the AndGate subclass. For example:
public abstract void Draw(..); //in Gate class public override void Draw(..) { ... } //in AndGate class
Once you have done this successfully, you have generalized your Form1 program so that it can work on all kinds of Gates, not just AND gates!
In the rest of this assignment, we will be adding lots of interesting kinds of new Gates by implementing new subclasses. However, your Form1 code must not change very much as these subclasses are added.
Assignment 1: Digital Circuit Design
3
Typically, you will have to just add one button click handler for each new subclass, which creates an instance of that subclass and assigns it to the newGate variable. The goal is to make as few changes to Form1.cs as possible throughout the rest of this assignment. This shows how a good object-oriented design allows us to add new functionality to a program by just adding new subclasses to it.
Step 3: New Logic Gates (3 marks) Now implement two new kinds of Gates: OR gates and NOT gates. Each
of these will be a subclass of Gate, so must override various Gate methods and maybe add some extra methods. Each of these subclasses is worth one mark. Note that your new gates should turn red when selected, just like an AndGate does. As well as OR and NOT gates, you could implement a PAD gate, which is just a small rectangle with one input pin and one output pin.
For each subclass you create, you should add a button to the toolbar with a handler that creates a new instance of your subclass, and assigns it to the newGate variable. This allows the engineer to drag it into the circuit and position it with a click.
Step 4: Input Sources and Output Lamps (1 mark each)
Add a new subclass of Gate called InputSource. This should have no input pins and just one output pin. It should contain a boolean variable that says whether the output pin is currently high voltage (which represents a true value being output) or a zero voltage (which represents a false value being output). An InputSource gate should look different when its boolean value is high. For example, the inside of the gate might be a different colour (avoid red), or it might display a “1” or “0” to show its on/off status. Each time an InputSource gate is selected, its boolean value should toggle. That is, selecting it once should change the boolean value from false (the default) to true, then selecting it again should change it from true back to false.
Similarly, create another subclass of Gate called OutputLamp. This will have one input pin and no output pins. Like the InputSource, it contains a boolean variable that records its current on/off status. It should glow like a small coloured lamp when that variable is true, and be dark when it is false. Add buttons for both of these new subclasses of Gate, and then test that you can add them to circuits and use wires to connect them to other gates.
Step 5: Evaluation Facilities (2 marks) Now extend your Gate class by adding an abstract Evaluate() method
that returns a boolean result. Each subclass will implement this method in a different way, so that it computes the correct logical result for that kind of gate. For example, the Evaluate() method of an InputSource will just return its internal on/off boolean variable. The Evaluate() method of the AndGate class will return true if both the input pins evaluate to true,
COMP104
4
or false otherwise. (If an input pin is not connected to a wire, you should display an error message and assume that the pin is false.) The code to do this will look something like this (you should extend this to handle not- connected pins):
Gate gateA = pins[0].InputWire.FromPin.Owner; Gate gateB = pins[1].InputWire.FromPin.Owner; return gateA.Evaluate() && gateB.Evaluate();
The Evaluate() method of an OutputLamp will evaluate its input pin (like gateA of the AndGate code) and set its internal on/off boolean variable to the result of this evaluation. So the output lamps trigger a series of Evaluate() calls to other gates in the circuit, then they display and remember the boolean results of the evaluations.
Add a toolbar button, “Evaluate”, whose handler loops through all the gates and asks each OutputLamp to evaluate itself. (Use the C# is or as operators to find out which gates are OutputLamps). To test your evaluation functions, use your program to design an exclusive-or circuit with two InputSources and one OutputLamp.
Step 6: Copy and Paste of Gates (1 mark)
Now extend your Gate class by adding an abstract Clone() method that returns a fresh copy of the gate. Each subclass will implement this in a different way, by making a copy of itself. Note that it will also need to make a clone of each of its pins.
Add a “Copy” button to the toolbar, that calls the Clone() method of the currently selected gate (if a gate is selected) and assigns it to the newGate variable. This will allow engineers to copy a single gate. Not very useful, but this feature can become much more useful once you've implemented compound gates.
Step 7: Compound Gates (6 marks) A very powerful and useful feature of most digital circuit editors is that
they allow a collection of Gates to be grouped together into one compound Gate. The resulting compound Gate can then be treated as a new primitive Gate, selected as one whole group, moved around together, and maybe copied and pasted etc.
Implement a Compound class, which will be another subclass of Gate, but will contain a list of Gate objects and an AddGate method that adds a Gate to that list. Add a “Start Group” button to the toolbar, whose handler creates a new empty Compound gate and stores it in a new instance variable of Form1, called newCompound. If a Gate is selected while newCompound is non-null, that gate is added into the newCompound object. Then add an “End Group” button to the toolbar, with a handler that moves newCompound into newGate and resets newCompound to null.
Assignment 1: Digital Circuit Design
5
1. Implement the Compound Move method so that all its gates move together. [2 marks]
2. Implement the Compound Selected property so that either all gates are
selected, or none are selected. [2 marks] 3. See if you can implement the Compound Clone method. This is
seriously hard, because you need to clone the wires as well as the gates, and make sure that the wires connect between the right gates. [2 marks]
Schedule Week 7 (by Fri 1 Sept): You should have split the AndGate class into a Gate superclass and
an AndGate subclass, and changed the Form1 class to use Gate objects.
Week 8 (by Fri 8 Sept): Implement the other Gate subclasses and the Evaluation features. You may also want to implement the Copy/Paste feature
Week 9 (by Fri 15 Sept): Implement the compound Gates feature. Test and document your program thoroughly.
Marking and Submission: During Week 9, ask a demonstrator in your 104 lab to mark your program, and then submit it. The deadline for submitting this assignment is Friday 15 September at 4pm, but you should aim to submit well before this to avoid the rush. Use PeaZip to compress your program solution into a .zip file and submit it via Moodle.
This assignment will be marked out of 20. You can earn a maximum of 12 marks for the various features of your program. The remaining 8 marks will be allocated by the tutors, after submission, for good use of inheritance, coding style and documentation. Note that when you override a method in a subclass, we will not require you to document that method, since it will inherit documentation from the superclass definition (which should be documented!).
Questions 1. Is it a better idea to fully document the Gate class or the AndGate
subclass? Can you inherit comments? 2. What is the advantage of making a method abstract in the superclass
rather than just writing a virtual method with no code in the body of the method? Is there any disadvantage to an abstract method?
3. If a class has an abstract method in it, does the class have to be
abstract? 4. What would happen in your program if one of the gates added to your
Compound Gate is another Compound Gate? Is your design robust enough to cope with this situation?
COMP104
6
COMP104-17B Assignment 1 Hand-in due 4pm 15th Sept 2017 Use PeaZip to compress the Visual Studio folder containing your solution program code into a .zip file and submit it via Moodle.
Student Declaration of Originality We (the partners listed below) declare that the program that we have had verified and which is submitted in Moodle is entirely our own work. We have not worked together with other people, except for doing pair-programming involving the two partners shown below. We have suitably acknowledged (referenced) any parts of other programs that we used. We understand that if we have breached the above conditions we will be sent to the University Disciplinary Committee. Note: This project will be marked only if this Declaration of Originality has been signed by both partners (if doing pair-programming) or by Partner 1 (if the project was done alone). Partner 1 Partner 2 Name: _____________________ Name: ______________________ ID Number: _______________ __ ID Number: __________________ Signed: ____________________ Signed: _____________________ Date: ______________________ Date: _______________________
Functionality (to be demonstrated in the lab) Basic Gate 1: And Gate refactor _____ /1 mark Basic Gate 2: One of Or/Not/Pad _____ /1 mark Basic Gate 3: Another of Or/Not/Pad _____ /1 mark Basic Gate 4: Input Source _____ /1 mark Basic Gate 5: Output Lamp _____ /1 mark Evaluation Facility: _____ /2 marks Copy and Paste: _____ /1 mark Compound Gate 1: Moving _____ /2 marks Compound Gate 2: Selecting _____ /2 marks Compound Gate 2: Cloning _____ /2 marks
Functionality Total (maximum 12): _____ /12 marks
Coding Style & Use of Objects Coding style: _____ (marked by tutor) Inheritance: _____ (marked by tutor) Documentation: _____ (marked by tutor)
Coding Style Total: _____ /8 marks
Total: _____ /20 marks