Skip to content

Commit 7ec575b

Browse files
committed
Fixed (mostly) D flip flops. Converted 4Bit counter to use JK flip flops
1 parent baaeae3 commit 7ec575b

File tree

9 files changed

+187
-152
lines changed

9 files changed

+187
-152
lines changed

Assets/Scripts/Chips/Binary_Counter_4Bit.cs

Lines changed: 40 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,65 +9,47 @@ namespace Chips
99
{
1010
public class Binary_Counter_4Bit : Chip
1111
{
12-
public Binary_Counter_4Bit() : base(3, 4)
12+
/// <summary>
13+
/// CLK = 0,
14+
/// 1 = CLR
15+
/// </summary>
16+
public Binary_Counter_4Bit() : base(2, 4)
1317
{
14-
D_FlipFlop_Re chipA = new D_FlipFlop_Re();
15-
int indexA = AddChip(chipA);
16-
17-
D_FlipFlop_Re chipB = new D_FlipFlop_Re();
18-
int indexB = AddChip(chipB);
19-
20-
D_FlipFlop_Re chipC = new D_FlipFlop_Re();
21-
int indexC = AddChip(chipC);
22-
23-
D_FlipFlop_Re chipD = new D_FlipFlop_Re();
24-
int indexD = AddChip(chipD);
25-
26-
Gate gateE = new NANDGate();
27-
int indexE = AddGate(gateE);
28-
29-
Gate gateF = new ANDGate();
30-
int indexF = AddGate(gateF);
31-
32-
//Inputs
33-
AddWire(ID, new Wire(0, 0, indexE, false));
34-
AddWire(ID, new Wire(1, 1, indexE, false));
35-
AddWire(ID, new Wire(2, 0, indexF, false));
36-
37-
AddWire(ID, new Wire(0, 0, indexA, true));
38-
39-
AddWire(ID, new Wire(2, 3, indexA, true));
40-
AddWire(ID, new Wire(2, 3, indexB, true));
41-
AddWire(ID, new Wire(2, 3, indexC, true));
42-
AddWire(ID, new Wire(2, 3, indexD, true));
43-
44-
//Gate E
45-
AddWire(gateE.ID, new Wire(0, 1, indexF, false));
46-
47-
//Gate F
48-
AddWire(gateF.ID, new Wire(0, 2, indexA, true, true));
49-
AddWire(gateF.ID, new Wire(0, 2, indexB, true, true));
50-
AddWire(gateF.ID, new Wire(0, 2, indexC, true, true));
51-
AddWire(gateF.ID, new Wire(0, 2, indexD, true, true));
52-
53-
//Chip A
54-
AddWire(chipA.ID, new Wire(0, 0, -1, true));
55-
AddWire(chipA.ID, new Wire(1, 1, indexA, true));
56-
AddWire(chipA.ID, new Wire(1, 0, indexB, true));
57-
58-
//Chip B
59-
AddWire(chipB.ID, new Wire(0, 1, -1, true));
60-
AddWire(chipB.ID, new Wire(1, 1, indexB, true));
61-
AddWire(chipB.ID, new Wire(1, 0, indexC, true));
62-
63-
//Chip C
64-
AddWire(chipC.ID, new Wire(0, 2, -1, true));
65-
AddWire(chipC.ID, new Wire(1, 1, indexC, true));
66-
AddWire(chipC.ID, new Wire(1, 0, indexD, true));
67-
68-
//Chip D
69-
AddWire(chipD.ID, new Wire(0, 3, -1, true));
70-
AddWire(chipD.ID, new Wire(1, 1, indexD, true));
18+
JK_FlipFlop_Clr chip0 = new JK_FlipFlop_Clr();
19+
chip0.SetInputBit(0, true);
20+
chip0.SetInputBit(1, true);
21+
int index0 = AddChip(chip0);
22+
23+
JK_FlipFlop_Clr chip1 = new JK_FlipFlop_Clr();
24+
chip1.SetInputBit(0, true);
25+
chip1.SetInputBit(1, true);
26+
int index1 = AddChip(chip1);
27+
28+
JK_FlipFlop_Clr chip2 = new JK_FlipFlop_Clr();
29+
chip2.SetInputBit(0, true);
30+
chip2.SetInputBit(1, true);
31+
int index2 = AddChip(chip2);
32+
33+
JK_FlipFlop_Clr chip3 = new JK_FlipFlop_Clr();
34+
chip3.SetInputBit(0, true);
35+
chip3.SetInputBit(1, true);
36+
int index3 = AddChip(chip3);
37+
38+
AddWire(ID, new Wire(0, 2, index0, true));
39+
40+
AddWire(ID, new Wire(1, 3, index0, true));
41+
AddWire(ID, new Wire(1, 3, index1, true));
42+
AddWire(ID, new Wire(1, 3, index2, true));
43+
AddWire(ID, new Wire(1, 3, index3, true));
44+
45+
AddWire(chip0, new Wire(0, 2, index1, true));
46+
AddWire(chip1, new Wire(0, 2, index2, true));
47+
AddWire(chip2, new Wire(0, 2, index3, true));
48+
49+
AddWire(chip0, new Wire(0, 0, -1, true));
50+
AddWire(chip1, new Wire(0, 1, -1, true));
51+
AddWire(chip2, new Wire(0, 2, -1, true));
52+
AddWire(chip3, new Wire(0, 3, -1, true));
7153
}
7254
}
7355
}

Assets/Scripts/Chips/Chip.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ public abstract class Chip
6868
// When updating gates should the gate be updated everytime output changes or only based on the original output
6969
// Helps when dealing with gates that loop back onto themselves (Flip Flops)
7070
protected bool ScrubOutput = false;
71+
[IgnoreDataMember]
72+
public bool KeepDirty = false;
7173

7274
/// <summary>
7375
/// Create a new chip object
@@ -176,7 +178,14 @@ public BitArray Update(bool forceUpdate)
176178

177179
GetOutput(forceUpdate);
178180

179-
Dirty = (BitArray)(oldOutput.Xor(Output)).Clone();
181+
if (KeepDirty)
182+
{
183+
Dirty.Or(oldOutput.Xor(Output));
184+
}
185+
else
186+
{
187+
Dirty = (BitArray)oldOutput.Xor(Output).Clone();
188+
}
180189
OldInput = (BitArray)Input.Clone();
181190
FirstRun = false;
182191

@@ -269,7 +278,7 @@ protected virtual void GetOutput(bool forceUpdate)
269278
foreach (Wire wire in WireDict[guid])
270279
{
271280
if (wire.IsChip)
272-
{
281+
{
273282
if (wire.CircuitIndex == -1)
274283
{
275284
if (!outputQueue.Contains(guid))
@@ -283,10 +292,10 @@ protected virtual void GetOutput(bool forceUpdate)
283292
{
284293
Chip chip = Chips[wire.CircuitIndex];
285294
chip.SetInputBit(wire.ToIndex, FromValues[wire.FromIndex] ^ wire.invertValue);
286-
chip.Update(forceUpdate);
287-
//Debug.Log("Updated chip: " + (char)(wire.CircuitIndex + 65));
295+
chip.Update(false);
296+
//Debug.Log("Updated chip: " + (char)(Chips.FindIndex(x => x.ID == guid) + 65) + "->" + (char)(wire.CircuitIndex + 65));
288297

289-
if (forceUpdate || FirstRun || GetCardinality(chip.Dirty) > 0)
298+
if (GetCardinality(chip.Dirty) > 0)
290299
{
291300
if (!queue.Contains(chip.ID))
292301
{

Assets/Scripts/Chips/D_FlipFlop_Re.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class D_FlipFlop_Re : Chip
1616
public D_FlipFlop_Re() : base(4, 2)
1717
{
1818
ScrubOutput = true;
19+
KeepDirty = true;
1920

2021
Gate gateA = new NANDGate(3);
2122
int indexA = AddGate(gateA);
@@ -29,29 +30,30 @@ public D_FlipFlop_Re() : base(4, 2)
2930
Gate gateD = new NANDGate(3);
3031
int indexD = AddGate(gateD);
3132

32-
Gate gateE = new NANDGate();
33+
Gate gateE = new NANDGate(3);
3334
int indexE = AddGate(gateE);
3435

35-
Gate gateF = new NANDGate();
36-
int indexF = AddGate(gateF);
37-
38-
//Clock
39-
AddWire(ID, new Wire(0, 1, indexB, false));
40-
AddWire(ID, new Wire(0, 1, indexC, false));
36+
Gate gateF = new NANDGate(3);
37+
int indexF = AddGate(gateF);
4138

4239
//Data
43-
AddWire(ID, new Wire(1, 1, indexD, false));
40+
AddWire(ID, new Wire(1, 1, indexD));
4441

4542
//Clr
4643
AddWire(ID, new Wire(2, 2, indexB, false, true));
4744
AddWire(ID, new Wire(2, 2, indexD, false, true));
45+
AddWire(ID, new Wire(2, 2, indexF, false, true));
4846

4947
//Pr
5048
AddWire(ID, new Wire(3, 2, indexA, false, true));
51-
AddWire(ID, new Wire(3, 2, indexC, false, true));
49+
AddWire(ID, new Wire(3, 2, indexE, false, true));
50+
51+
//Clock
52+
AddWire(ID, new Wire(0, 1, indexB));
53+
AddWire(ID, new Wire(0, 1, indexC));
5254

5355
//Gate A
54-
AddWire(gateA.ID, new Wire(0, 0, indexB, false));
56+
AddWire(gateA.ID, new Wire(0, 0, indexB));
5557

5658
//Gate B
5759
AddWire(gateB.ID, new Wire(0, 1, indexA, false));

Assets/Scripts/Chips/Dual_D_FlipFlop.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ public Dual_D_FlipFlop() : base(8, 4)
2222
int index2 = AddChip(chip2);
2323

2424
// Clr
25-
AddWire(ID, new Wire(4, 2, index1, true));
26-
AddWire(ID, new Wire(5, 2, index2, true));
25+
AddWire(ID, new Wire(4, 2, index1, true, true));
26+
AddWire(ID, new Wire(5, 2, index2, true, true));
2727

2828
// Pr
29-
AddWire(ID, new Wire(6, 3, index1, true));
30-
AddWire(ID, new Wire(7, 3, index2, true));
29+
AddWire(ID, new Wire(6, 3, index1, true, true));
30+
AddWire(ID, new Wire(7, 3, index2, true, true));
3131

3232
// Clk
3333
AddWire(ID, new Wire(2, 0, index1, true));

Assets/Scripts/Chips/JK_FlipFlop_Clr.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@ namespace Chips
44
{
55
public class JK_FlipFlop_Clr : Chip
66
{
7-
7+
/// <summary>
8+
/// J = 0,
9+
/// K = 1,
10+
/// CLK = 2,
11+
/// CLR = 3
12+
///
13+
/// Q = 0,
14+
/// Q' = 1
15+
/// </summary>
816
public JK_FlipFlop_Clr() : base(4, 2)
917
{
1018
ScrubOutput = true;

Assets/Scripts/Chips/Octal_D_FlipFlop.cs

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Octal_D_FlipFlop : Chip
77
/// <summary>
88
/// Data = 0-7,
99
/// Clk = 8,
10-
/// Clr = 9
10+
/// OE = 9
1111
///
1212
/// Q = 0-7
1313
/// </summary>
@@ -37,10 +37,34 @@ public Octal_D_FlipFlop() : base(10, 8)
3737
D_FlipFlop_Re chip8 = new D_FlipFlop_Re();
3838
int index8 = AddChip(chip8);
3939

40-
BufferGate gateA = new BufferGate();
40+
ANDGate gate1 = new ANDGate();
41+
int i1 = AddGate(gate1);
42+
43+
ANDGate gate2 = new ANDGate();
44+
int i2 = AddGate(gate2);
45+
46+
ANDGate gate3 = new ANDGate();
47+
int i3 = AddGate(gate3);
48+
49+
ANDGate gate4 = new ANDGate();
50+
int i4 = AddGate(gate4);
51+
52+
ANDGate gate5 = new ANDGate();
53+
int i5 = AddGate(gate5);
54+
55+
ANDGate gate6 = new ANDGate();
56+
int i6 = AddGate(gate6);
57+
58+
ANDGate gate7 = new ANDGate();
59+
int i7 = AddGate(gate7);
60+
61+
ANDGate gate8 = new ANDGate();
62+
int i8 = AddGate(gate8);
63+
64+
NotGate gateA = new NotGate();
4165
int indexA = AddGate(gateA);
4266

43-
// Clr
67+
// OE
4468
AddWire(ID, new Wire(9, 0, indexA));
4569

4670
// Clk
@@ -64,24 +88,34 @@ public Octal_D_FlipFlop() : base(10, 8)
6488
AddWire(ID, new Wire(7, 1, index8, true));
6589

6690
// Gate A
67-
AddWire(gateA.ID, new Wire(0, 2, index1, true));
68-
AddWire(gateA.ID, new Wire(0, 2, index2, true));
69-
AddWire(gateA.ID, new Wire(0, 2, index3, true));
70-
AddWire(gateA.ID, new Wire(0, 2, index4, true));
71-
AddWire(gateA.ID, new Wire(0, 2, index5, true));
72-
AddWire(gateA.ID, new Wire(0, 2, index6, true));
73-
AddWire(gateA.ID, new Wire(0, 2, index7, true));
74-
AddWire(gateA.ID, new Wire(0, 2, index8, true));
91+
AddWire(gateA, new Wire(0, 1, i1));
92+
AddWire(gateA, new Wire(0, 1, i2));
93+
AddWire(gateA, new Wire(0, 1, i3));
94+
AddWire(gateA, new Wire(0, 1, i4));
95+
AddWire(gateA, new Wire(0, 1, i5));
96+
AddWire(gateA, new Wire(0, 1, i6));
97+
AddWire(gateA, new Wire(0, 1, i7));
98+
AddWire(gateA, new Wire(0, 1, i8));
99+
100+
// D FF
101+
AddWire(chip1, new Wire(0, 0, i1));
102+
AddWire(chip2, new Wire(0, 0, i2));
103+
AddWire(chip3, new Wire(0, 0, i3));
104+
AddWire(chip4, new Wire(0, 0, i4));
105+
AddWire(chip5, new Wire(0, 0, i5));
106+
AddWire(chip6, new Wire(0, 0, i6));
107+
AddWire(chip7, new Wire(0, 0, i7));
108+
AddWire(chip8, new Wire(0, 0, i8));
75109

76110
// Output
77-
AddWire(chip1.ID, new Wire(0, 0, -1, true));
78-
AddWire(chip2.ID, new Wire(0, 1, -1, true));
79-
AddWire(chip3.ID, new Wire(0, 2, -1, true));
80-
AddWire(chip4.ID, new Wire(0, 3, -1, true));
81-
AddWire(chip5.ID, new Wire(0, 4, -1, true));
82-
AddWire(chip6.ID, new Wire(0, 5, -1, true));
83-
AddWire(chip7.ID, new Wire(0, 6, -1, true));
84-
AddWire(chip8.ID, new Wire(0, 7, -1, true));
111+
AddWire(gate1, new Wire(0, 0, -1, true));
112+
AddWire(gate2, new Wire(0, 1, -1, true));
113+
AddWire(gate3, new Wire(0, 2, -1, true));
114+
AddWire(gate4, new Wire(0, 3, -1, true));
115+
AddWire(gate5, new Wire(0, 4, -1, true));
116+
AddWire(gate6, new Wire(0, 5, -1, true));
117+
AddWire(gate7, new Wire(0, 6, -1, true));
118+
AddWire(gate8, new Wire(0, 7, -1, true));
85119

86120
}
87121
}

0 commit comments

Comments
 (0)