Repository navigation
Expand file tree
/
Copy pathnotes.txt
More file actions
575 lines (428 loc) · 21.4 KB
/
Copy pathnotes.txt
File metadata and controls
575 lines (428 loc) · 21.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
Brainstorming on how I want the GUI to work...
I want to be able to define the primative gates via the GUI, so theoretically
the code itself would not implement the various logic gates (AND, OR, NOT, etc.)
but rather, the user of the software would define those using the app itself.
Each gate would be defined by specifying the number of inputs.
The number of outputs is not specified, as there will always only ever be a
single output.
Gates with both inputs and outputs would be relatively easy to implement, but
we need to make sure we have a way to also define components that do not have
an input--only an output--such as a clock source, fixed HI or fixed LO signal,
or a toggle switch. The fixed HI/LO component would just be a special case
of a toggle switch, and the clock source could be a toggle switch that automatically
toggles itself every second (or other time length).
Define Simple Gate / Component:
/ 0 Inputs / 1 Output (Used for a switch, clock source, fixed high, fixed low)
Choose < 1 Input / 1 Output (Used for a NOT gate / Inverter)
\ 2 Inputs / 1 Output (Used for AND, OR, XOR, NAND, NOR, XNOR)
Potential Object Attributes...
- Type:
* Input Component (Switch, Clock, Fixed HI/LO)
* Output Component (LED, 7-Segment display)
* Gate (AND, OR, XOR, NAND, NOR, XNOR, NOT)
* Integrated Circuit (wrapper around a circuit which attaches inputs and outputs?)
- Inputs dict
* Input label
- Outputs dict
* Output label
- Relationship between inputs and outputs
* Strings such as 'AND' would map to the python 'and' operator, etc.
* Implement as truth table?
* Optimize with K-Map?
Python only implements: AND, OR, NOT, but we can use the bitwise XOR since we are working with Boolean types
Toughts on how to store truth tables...
- should we generate truth tables dynamically using the gate's update() method?
- or should we define the intended truth table, and use it to check itself for proper function (unit test)?
We could use a hash of 2-dimensional arrays to store the truth tables for a 2-input gate as follows:
#
# Make 2x2 matrix (2-dimensional list/array)
#
and_truth = [[0 for x in range(2)] for y in range(2)]
#
# Populate the 2x2 array with values for and AND gate
#
and_truth[0][0] = 0
and_truth[0][1] = 0
and_truth[1][0] = 0
and_truth[1][1] = 1
#
# Plop the 2x2 into a hash keyed off of gate type/name
#
truth_dict = {}
truth_dict['AND'] = and_truth
#
# Show the value to prove the data structure is correct
#
truth_dict['AND'][0][0]
truth_dict['AND'][0][1]
truth_dict['AND'][1][0]
truth_dict['AND'][1][1]
AND
P Q Out
--- --- ---
0 0 0
0 1 0
1 0 0
1 1 1
OR
P Q Out
--- --- ---
0 0 0
0 1 1
1 0 1
1 1 1
XOR (The carrot ^ operator in Python)
P Q Out
--- --- ---
0 0 0
0 1 1
1 0 1
1 1 0
NOT
P Out
--- ---
0 1
1 0
foo = andGate()
foo.printState()
foo.toggleInputStateByName('IN_0')
foo.printState()
foo.toggleInputStateByName('IN_1')
foo.printState()
foo.toggleInputStateByName('IN_0')
foo.printState()
foo.toggleInputStateByName('IN_1')
foo.printState()
bar = orGate()
bar.printState()
bar.toggleInputStateByName('IN_0')
bar.printState()
bar.toggleInputStateByName('IN_1')
bar.printState()
bar.toggleInputStateByName('IN_0')
bar.printState()
bar.toggleInputStateByName('IN_1')
bar.printState()
lah = xorGate()
lah.printState()
lah.toggleInputStateByName('IN_0')
lah.printState()
lah.toggleInputStateByName('IN_1')
lah.printState()
lah.toggleInputStateByName('IN_0')
lah.printState()
lah.toggleInputStateByName('IN_1')
lah.printState()
dee = notGate()
dee.printState()
dee.toggleInputStateByName('IN_0')
dee.printState()
dee.toggleInputStateByName('IN_0')
dee.printState()
Okay, right now we have a generic logicGate class.
We also have specific gates that inherit from the logicGate class.
The behavior of the specific gates is hard-coded, like a primative.
For example, we have an andGate, orgate, xorGate, notGate.
Questions:
- do we really want to have these primatives?
- would the code be easier to maintain if we "created" each of these gates with their behavior dynamically?
- what if we could create the primative gates at startup, rather than having them hard coded?
andGate = logicGate('AND') # Give it a name
andGate.createInputConnection(2) # add 2 inputConnections
andGate.createOutputConnection(1) # add 1 outputConnection (could a gate have more than 1 output connection?)
# Build the truth table for an AND gate
and_truth = [[0 for x in range(2)] for y in range(2)]
and_truth[0][0] = 0
and_truth[0][1] = 0
and_truth[1][0] = 0
and_truth[1][1] = 1
# Configure the gate to use the truth table
andGate.defineTruthTable(and_truth)
At this point we'd have a gate the behaves like an AND gate
Anyway, we'd still end up defining an andGate class, because we would want to
be able to create many many instances of an andGate, and we don't want to have
to write all of that code every time to create 1 gate dynamically.
The interesting thing to consider is, with all outputs pre-calculated, would
it be faster than calculating the output with the logical operators?
Using the truth table method is certainly more general, and would make it easy
to create, say, 3 or more input gates if we wanted to (like a 3 input AND gate).
In fact, if we were using the truth table to define gate behavior, we could
even make a fullAdder "gate" with 2-inputs and 2-outputs. This is a case where
we'd have more than one output. Interesting.
Need to start looking at how we will implement gate connections, so we can
connect one gate to another gate through a "Wire" object. We need a wire
object so that we can eventually draw the wire on the screen, and eventually
we may want to be able to implement latency, so all gates and wire could have
a certain amount of latency. We'd then be able to implement the "Buffer"
gate, which is really just like a wire with some latency/delay.
We also need "special purpose gates" that arn't really gates, but circuit
elements:
Input elements:
1) Toggle Switch (input that we can flip on/off via the GUI)
2) Clock source (really like a toggle switch that toggles on/off automatically at certain Hz rate)
3) Fixed HI source (really just a toggle switch that is always on/1/true)
4) Fixed LO source (really just a toggle switch that is always off/0/false)
Output elements:
1) an LED / lamp - some GUI element that would indicate if the signal is HI/LO, ON/OFF, 0/1, true/false
2) 7-segment display (really just 8 LED's aranged in a special way, 8th is the decimal point)
Playing around with printing tuples...
i2 = [
(0, 0),
(0, 1),
(1, 0),
(1, 1)
]
i3 = [
(0, 0, 0),
(0, 0, 1),
(0, 1, 0),
(0, 1, 1),
(1, 0, 0),
(1, 0, 1),
(1, 1, 0),
(1, 1, 1)
]
for x, y, z in i3:
print("x {} y {} z {}".format(x,y,z))
i4 = [
(0, 0, 0, 0),
(0, 0, 0, 1),
(0, 0, 1, 0),
(0, 0, 1, 1),
(0, 1, 0, 0),
(0, 1, 0, 1),
(0, 1, 1, 0),
(0, 1, 1, 1),
(1, 0, 0, 0),
(1, 0, 0, 1),
(1, 0, 1, 0),
(1, 0, 1, 1),
(1, 1, 0, 0),
(1, 1, 0, 1),
(1, 1, 1, 0),
(1, 1, 1, 1)
]
for w, x, y, z in i4:
print("w {} x {} y {} z {}".format(w,x,y,z))
for a in i4:
print(a)
How would we implement the truthTable is there could be more than one output, say in the case of a Full Adder?
A_IN "Number A Input"
B_IN "Number B Input"
C_IN "Carry Input"
S_OUT "Sum Output"
C_OUT "Carry Output"
A_IN B_IN C_IN S_OUT C_OUT
----- ----- ----- ----- -----
0 0 0 0 0
0 0 1 1 0
0 1 0 1 0
0 1 1 0 1
1 0 0 1 0
1 0 1 0 1
1 1 0 0 1
1 1 1 1 1
Thought: Let's not implement this more general case (for now), as we'd end up with an "optimized" gate
to work like a full-adder, instead of an IC that was built with the primative gates, and the latency would
not be realistic as a result. The "Full Adder Gate" would be artificially fast. However, we could set
the latency to represent whatever the realistic speed would be. I eventually want to implement this more
general "gate by truth table" idea, but will keep it simple for now...
It would still be cool to be able to print out a truth table for any IC. We could build an input
component that would be multiple clock sources (one for each input), and that would be set to different
rates to ensure all inputs are covered. For example, for a 4-input IC, we could use 4 clock sources
with their rate set to:
1 Hz 0101010101010101 0101010101010101 0101010101010101
2 Hz 0011001100110011 0011001100110011 0011001100110011
4 Hz 0000111100001111 0000111100001111 0000111100001111
8 Hz 0000000011111111 0000000011111111 0000000011111111
inputConnections and outputConnections
--------------------------------------
All logicGates have a set of inputConnections and a single outputConnection
A logicCircuit contains many logicGates, and connection data
Since wire is really just a buffer gate (or dummy gate the just passes signal), we really don't need a separate
wire object, except when we get to developing the GUI, we will need to be able to draw the connections, so not
really sure yet if it will be useful to have a true wire object, or just treat wire as just another logicGate?
Let's start playing with how we are going to connect gates together.
- Should each gate have knowledge about what it's connected to? both inputs and outputs?
- Or should the higher-level logicCircuit object contain all of the connection information?
- or both?
...I'm going to start with the second option: there will be a logicCircuit object, and it will contain
all of the connection info about the gates contained within.
If we do this right, our logicCircuit could later become an integratedCircuit, which could be used to build up
more complex circuits. E.g. Flip-flops would be implemented as as integrated circuits.
the logicCircuit class
----------------------
I'm having trouble thinking about how things should be connected together, so let's start to zoom
out and look at a higher-level object in the hierarchy: the logicCircuit object
Questions:
- Will the logicCircuit be within the hierarchy of a logicGate?
- Or will the logicCircuit only contain other logicGate objects? <--- I think this, but not sure
- a logicCircuit should be able to become an intergated circuit
The logicCircuit will contain circuit components:
- logicGate (andGate, orGate, notGate, etc.)
- inputComponent (switch, clock source, input pin, etc.)
- outputComponent (LED, 7-deg display, output pin, etc.)
- connections
The question is: what do these connections look like? How should be keep track of connections
such that we can trigger a cascading update of a circuit? Say, if I flip a switch from 0 to 1,
then I want that to trigger the entire circuit to update itself.
Two things come to mind:
1) will the circuit be "running" continuously (like a real simulation)?
2) or will it only update when something changes?
I think the 1st option is the best, as we it will be easier to plot out signal at certain points
if we are attaching a probe to a circuit, and then we know for sure that the circuit is "live".
We want the model to accurately represent a real physical circuit.
Let's try to draw out how we would define a circuit, and see if that helps...
Let's define a full adder. It has a mixture of gates, and examples of outputs that have multiple destinations
(the output of some components connects to multiple inputs of other components)
switchA = switch()
switchB = switch()
switchC = switch()
xor1 = xorGate()
wire1 = wire() # switchA OUT --> xor1 IN_0
wire2 = wire() # switchB OUT --> xor1 IN_1
lampS = lamp()
xor2 = xorGate()
wire3 = wire() # xor1 OUT --> xor2 IN_0
wire4 = wire() # switchC OUT --> xor2 IN_1
wire5 = wire() # xor2 OUT --> lampS IN
and1 = andGate()
wire6 = wire() # switchA OUT --> and1 IN_0
wire7 = wire() # switchB OUT --> and1 IN_1
and2 = andGate()
wire9 = wire() # xor1 OUT --> and2 IN_0
wire10 = wire() # switchC OUT --> and2 IN_1
or1 = orGate()
wire8 = wire() # and1 OUT --> or1 IN_1
wire11 = wire() # and2 OUT --> or1 IN_0
lampC = lamp()
wire12 = wire() # or1 OUT --> lampC IN
I've drawn out what is connected to what next to each wire
So, assume the above objects are all implemented...how would we make the connections stick?
- would we call methods on the gates?
- or on the wires?
Let's try implementing the connect() method on the wire (aka connector)
If we want to connect two components to the same wire, we could do:
wire.connect(outputConnection, inputConnection) # Where outputConnection is the output of a circuit component
# and inputConnection is one of the inputs of a circuit component
# do we need to differentiate between input and output connections?
wire1.connect(switchA.get_connection('OUT'), xor1.get_connection('IN_0'))
wire2.connect(switchB.get_connection('OUT'), xor1.get_connection('IN_1'))
wire3.connect(xor1.get_connection('OUT'), xor2.get_connection('IN_0'))
wire4.connect(switchC.get_connection('OUT'), xor2.get_connection('IN_1'))
wire5.connect(xor2.get_connection('OUT'), lampS.get_connection('IN'))
wire6.connect(switchA.get_connection('OUT'), and1.get_connection('IN_0'))
wire7.connect(switchB.get_connection('OUT'), and1.get_connection('IN_1'))
wire8.connect(and1.get_connection('OUT'), or1.get_connection('IN_1'))
wire9.connect(xor1.get_connection('OUT'), and2.get_connection('IN_0'))
wire10.connect(switchC.get_connection('OUT'), and2.get_connection('IN_1'))
wire11.connect(and2.get_connection('OUT'), and2.get_connection('IN_0'))
wire12.connect(or1.get_connection('OUT'), lampC.get_connection('IN'))
...so each logicGate would have a get_connection() method that returns the inputConnection or outputConnection object?
Do we really need a separate "connection object"? (Keep this question in mind, and come back to)
How would we implement the connect() method? what would it do?
I think our logicCircuit object needs to hold a list of wire/connection objects
the logicCircuit object should have an update() method
When we call logicCircuit.update() it will run through the list of objects (in
the order they were connected?) and run the update() method on each what? each connection?
Or do we design the gates and wires to "know" what they are connected to, so we
could "ask" a gate "what is your IN_1 connected to?" and it would return a ref
to the connected object, which we could then further interogate.
The data structure would effectively be distributed throughout all of the
logicCircuit components.
There will be "simulation time" which will has some resolution (such as 1/4 sec, or 1/8 sec),
and the simulation engine will run in a continuous loop, updating the circuit every slice
of simulated time.
We dont want to let the loop run as fast as python can, as this would show up
as high CPU util on the system. We do a time.sleep(sim_time_res) where
sim_time_res is something like 1/4, 1/8, or 1/16 of a second.
Run this for an example:
#!/usr/bin/env python3
import time
world_time = 0.0
# Use one over a power of 2 such as: 1, 1/2, 1/4, 1/8, 1/16, 1/32, 1/64
world_time_res = 1/4 # resolution (e.g 1/10th of a sec)
clock_source = int(0) # current value of clock source (0 or 1 / Hi or Lo)
clock_hz = int(1) # in seconds
clock_elapsed = 0.0 # keep track of elapsed time since last transition
# after clock_hz, we transition Hi or Lo
while (world_time < 10):
print('The world_time is: {0:7.2f} clock_source: {1} elapsed {2:6.2f}'.format(world_time,clock_source,clock_elapsed))
world_time = world_time + world_time_res
clock_elapsed = clock_elapsed + world_time_res
if ( clock_elapsed >= clock_hz ):
print ('--- TICK -----------------------------------------------------------------------------')
clock_elapsed = 0.0
if ( clock_source == 1 ):
clock_source = 0
else:
clock_source = 1
time.sleep(world_time_res)
State Propagation
------------------
Example:
2-input-gate-with-1-output: we store state in 4 places:
inputConnection IN_0 state
- receives state via wire/connector from upstream component's outputConnection
inputConnection IN_1 state
- receives state via wire/connector from upstream component's outputConnection
gate state
- needed for GUI display of gates state, either "ON" or "OFF", solid or outline)
- will propagate to the outputConnection after latency timer expires
outputConnection OUT state
- receives state from gate after latency timer expires
what happens when we update any of these?
inputConnection['IN_0'].update()
- would simply check current state, and update GUI to show solid color for ON/HI, or outline for OFF/LO signal
- then call update() for the downstream component which would be the gate object itself
logicGate.update()
- just called by upstream component
- calculate state of gate based on the 2 inputs, and update gate state to new value
- if state of gate is ON/HI, then update GUI object with solid color, otherwise update with outline object
- call update() on downstream object only if latency timer has expired, which will be the outputConnection object
- otherwise, the propagate is "broken out of" so to speak
outputConnection('OUT').update()
- would simply check current state, and update GUI to show solid color for ON/HI, or outline for OFF/LO signal
- then call update() for the downstream component
So, we are updating the circuit in "chains" of update() calls. Visually, picture a line of update() calls starting
with each inputComponent, and running through the circuit all the way to the outputComponent
We could build a list of "propagation chains" and then run against them over and over again until a new GUI object is
dragged, positioned, and wired up, and then we'd have to re-generate the chains
State Propagation and Loops in a Circuit
----------------------------------------
I was thinking about how to propagate state through a circuit, and the idea of each gate calling it's own update()
method, and then immediately calling the update() method of connected gates, would work, but only if there were
no loops in the circuit. If there was a loop, like occurs in Flip Flops, then we'd end up stuck in an infinite
stream of calls to one part of the circuit, but we'd never get back to other parts of the circuit outside the
loop.
We also want to ensure that all gates appear to be updating at the same time, such that there is a progression
from left to right (with all inputs on the left, moving through the paths in the middle, to the outputs on the
left). We want a bredth-first propagation pattern, not a depth-first pattern. Although, I've not thought of
a specific case where depth-first could cause a problem, I'm trying to model how a real circuit would work, so
that we can eventually simulate real-world scenarios, and even real-world circuit design problems.
With all of that said, I think the solution to this propagation problem is to simply view a circuit as a collection
of objects, and we update them one-by-one in the order that they were added. The logicCircuit object would
contain a list of objects (inputs, gates, wires, outputs), and just run through the list over and over and over
again in a super fast loop, updating each object. Since the "simulated time clock" will be running much
faster than a clock source in the circuit, every gate will be updated many times in between each clock source
tick, we don't have to worry about the objects themselves calling update on their downstream neighbor, because
we'll be updating every circuit component hundreds of times a second. The "update resolution" will be much
more granular than any clock source in the circuit itself. Think of this as a "game loop" like you'd have in
a real-time action shooter game where the graphics on the screen are being updated many times per second, but
in this case, we will be updating the state of our circuit over and over in a loop many times per second.
State Propagation new thoughts
------------------------------
I was thinking about this some more, and have actually already partially implemented the way we will update
a circuit. It's actually much simpler than I was thinking, and will go like this:
- The logicCircuit object will contain a list of all circuit components.
- For each slice of time in our simulator, we will simply iterate through the list of objects, and call the update() method on the object
- The circuit will update itself over the course of several/many calls to
update() with the "upstream" gates being affected by their upstream connections,
and eventually as simulated time passes, and many update() calls have been made,
the "signal" will propagate through the circuit, just as a real-world circuit
would behave.
- We can take things further, and implement signal propagation delay (latency) for
the wire and gates to affect signal propagation. This should give us the control
we need to eventually deal with loops in a circuit (like in a Flip-Flop) in case
such a circuit isn't behaving exactly correct, we could tweak it into correctness
by adjusting relative latencies