Skip to content

Commit

Permalink
Added ring counter and moved things around (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesjiang52 committed Nov 5, 2018
1 parent 0428e5f commit eb8e565
Show file tree
Hide file tree
Showing 74 changed files with 3,023 additions and 2,074 deletions.
3 changes: 2 additions & 1 deletion bitwise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from . import arithmetic
from . import logic
from . import signal
from . import state
from . import storage
from ._version import __version__

__all__ = ["wire", "gate", "arithmetic", "logic", "signal", "storage"]
__all__ = ["wire", "gate", "arithmetic", "logic", "signal", "state", "storage"]
21 changes: 10 additions & 11 deletions bitwise/signal/MUX.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ class Multiplexer2To1:
multiplexer.
input_2: An object of type Wire. The second data input to the
multiplexer.
output: An object of type Wire. The output of the multiplexer, which
takes on the value of input_1 for a 1 select and input_2 for a 0
select.
output: An object of type Wire. The output of the multiplexer. Takes on
the value of input_1 for a 1 select and input_2 for a 0 select.
"""
def __init__(self, enable, select, input_1, input_2, output):
wire_1 = Wire()
Expand All @@ -52,9 +51,9 @@ class Multiplexer4To1:
select_2: An object of type Wire. The least significant bit of the
select input.
input_bus: An object of type Bus4. The data input to the multiplexer.
output: An object of type Wire. The output of the multiplexer, which
takes on the value of input_bus[0] for a (1, 1) select and
input_bus[3] for a (0, 0) select.
output: An object of type Wire. The output of the multiplexer. Takes on
the value of input_bus[0] for a (1, 1) select and input_bus[3] for
a (0, 0) select.
Raises:
TypeError: If input_bus is not a bus of width 4.
Expand Down Expand Up @@ -86,9 +85,9 @@ class Multiplexer8To1:
select_3: An object of type Wire. The least significant bit of the
select input.
input_bus: An object of type Bus8. The data input to the multiplexer.
output: An object of type Wire. The output of the multiplexer, which
takes on the value of input_bus[0] for a (1, 1, 1) select and
input_bus[7] for a (0, 0, 0) select.
output: An object of type Wire. The output of the multiplexer. Takes on
the value of input_bus[0] for a (1, 1, 1) select and input_bus[7]
for a (0, 0, 0) select.
Raises:
TypeError: If input_bus is not a bus of width 8.
Expand Down Expand Up @@ -120,8 +119,8 @@ class Multiplexer16To1:
select_bus: An object of type Bus4. select_bus[0] and select_bus[3] are
the most and least significant bit, respectively.
input_bus: An object of type Bus16. The data input to the multiplexer.
output: An object of type Wire. The output of the multiplexer, which
takes on the value of input_bus[0] for a (1, 1, 1, 1) select and
output: An object of type Wire. The output of the multiplexer. Takes on
the value of input_bus[0] for a (1, 1, 1, 1) select and
input_bus[15] for a (0, 0, 0, 0) select.
Raises:
Expand Down
41 changes: 21 additions & 20 deletions bitwise/storage/COUNT.py → bitwise/state/COUNT.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from .. import wire
from .. import gate
from .. import signal
from . import FLOP
from .. import storage

Wire = wire.Wire
Bus4 = wire.Bus4
Expand Down Expand Up @@ -39,15 +39,15 @@ def __init__(self, enable, clear_n, clock, output_1, output_2):

gate.ANDGate2(enable, output_2, and_1)

FLOP.TFlipFlopPresetClear(
storage.TFlipFlopPresetClear(
and_1,
vcc,
clear_n,
clock,
output_1,
output_not_1
)
FLOP.TFlipFlopPresetClear(
storage.TFlipFlopPresetClear(
enable,
vcc,
clear_n,
Expand Down Expand Up @@ -84,23 +84,23 @@ def __init__(self, enable, clear_n, clock, output_1, output_2, output_3):
gate.ANDGate2(enable, output_3, and_1)
gate.ANDGate2(and_1, output_2, and_2)

FLOP.TFlipFlopPresetClear(
storage.TFlipFlopPresetClear(
and_2,
vcc,
clear_n,
clock,
output_1,
output_not_1
)
FLOP.TFlipFlopPresetClear(
storage.TFlipFlopPresetClear(
and_1,
vcc,
clear_n,
clock,
output_2,
output_not_2
)
FLOP.TFlipFlopPresetClear(
storage.TFlipFlopPresetClear(
enable,
vcc,
clear_n,
Expand All @@ -115,7 +115,8 @@ class UpCounterMod16:
Args:
enable: An object of type Wire. Enables the counter.
clear_n: An object of type Wire. Clears output_bus to 0 asynchronously if its value is 0.
clear_n: An object of type Wire. Clears output_bus to 0 asynchronously
if its value is 0.
clock: An object of type Wire or Clock. The clock input to the counter.
output_bus: An object of type Bus4. The output of the counter.
output_bus[0] and output_bus[3] are the most and least significant
Expand Down Expand Up @@ -147,31 +148,31 @@ def __init__(self, enable, clear_n, clock, output_bus):
gate.ANDGate2(and_1, output_bus[2], and_2)
gate.ANDGate2(and_2, output_bus[1], and_3)

FLOP.TFlipFlopPresetClear(
storage.TFlipFlopPresetClear(
and_3,
vcc,
clear_n,
clock,
output_bus[0],
output_not_1
)
FLOP.TFlipFlopPresetClear(
storage.TFlipFlopPresetClear(
and_2,
vcc,
clear_n,
clock,
output_bus[1],
output_not_2
)
FLOP.TFlipFlopPresetClear(
storage.TFlipFlopPresetClear(
and_1,
vcc,
clear_n,
clock,
output_bus[2],
output_not_3
)
FLOP.TFlipFlopPresetClear(
storage.TFlipFlopPresetClear(
enable,
vcc,
clear_n,
Expand Down Expand Up @@ -227,13 +228,13 @@ def __init__(

signal.Multiplexer2To1(vcc, load_n, xor_1, load_2, mux_1)
signal.Multiplexer2To1(vcc, load_n, xor_2, load_1, mux_2)
FLOP.DFlipFlop(
storage.DFlipFlop(
mux_2,
clock,
output_1,
output_not_1
)
FLOP.DFlipFlop(
storage.DFlipFlop(
mux_1,
clock,
output_2,
Expand Down Expand Up @@ -300,19 +301,19 @@ def __init__(
signal.Multiplexer2To1(vcc, load_n, xor_1, load_3, mux_1)
signal.Multiplexer2To1(vcc, load_n, xor_2, load_2, mux_2)
signal.Multiplexer2To1(vcc, load_n, xor_3, load_1, mux_3)
FLOP.DFlipFlop(
storage.DFlipFlop(
mux_3,
clock,
output_1,
output_not_1
)
FLOP.DFlipFlop(
storage.DFlipFlop(
mux_2,
clock,
output_2,
output_not_2
)
FLOP.DFlipFlop(
storage.DFlipFlop(
mux_1,
clock,
output_3,
Expand Down Expand Up @@ -397,25 +398,25 @@ def __init__(
signal.Multiplexer2To1(vcc, load_n, xor_2, load_bus[2], mux_2)
signal.Multiplexer2To1(vcc, load_n, xor_3, load_bus[1], mux_3)
signal.Multiplexer2To1(vcc, load_n, xor_4, load_bus[0], mux_4)
FLOP.DFlipFlop(
storage.DFlipFlop(
mux_4,
clock,
output_bus[0],
output_not_1
)
FLOP.DFlipFlop(
storage.DFlipFlop(
mux_3,
clock,
output_bus[1],
output_not_2
)
FLOP.DFlipFlop(
storage.DFlipFlop(
mux_2,
clock,
output_bus[2],
output_not_3
)
FLOP.DFlipFlop(
storage.DFlipFlop(
mux_1,
clock,
output_bus[3],
Expand Down
8 changes: 4 additions & 4 deletions bitwise/storage/PISO.py → bitwise/state/PISO.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""

from .. import wire
from .. import storage
from . import SHIFT

Wire = wire.Wire
Bus4 = wire.Bus4
Expand Down Expand Up @@ -61,7 +61,7 @@ def __init__(
b_4 = Wire()
b_bus = Bus4(b_1, b_2, b_3, b_4)

storage.ShiftRegister4(
SHIFT.ShiftRegister4(
enable,
reset_n,
parallel_load_n,
Expand Down Expand Up @@ -124,7 +124,7 @@ def __init__(
b_8 = Wire()
b_bus = Bus8(b_1, b_2, b_3, b_4, b_5, b_6, b_7, b_8)

storage.ShiftRegister8(
SHIFT.ShiftRegister8(
enable,
reset_n,
parallel_load_n,
Expand Down Expand Up @@ -198,7 +198,7 @@ def __init__(
b_9, b_10, b_11, b_12, b_13, b_14, b_15, b_16
)

storage.ShiftRegister16(
SHIFT.ShiftRegister16(
enable,
reset_n,
parallel_load_n,
Expand Down
127 changes: 127 additions & 0 deletions bitwise/state/RING.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
"""
The following classes are defined:
RingCounter4
RingCounter8
RingCounter16
"""

from .. import wire
from .. import signal
from . import COUNT

Wire = wire.Wire
Bus4 = wire.Bus4
Bus8 = wire.Bus8
Bus16 = wire.Bus16


class RingCounter4:
"""Construct a new 4-bit ring counter.
Args:
enable: An object of type Wire. Enables the ring counter.
clear_n: An object of type Wire. Clears output_bus to (0, 0, 0, 1) (the
0 state) if its value is 0.
clock: An object of type Wire or Clock. The clock input.
output_bus: An object of type Bus4. The one-hot output of the ring
counter. Starts at (0, 0, 0, 1) and counts up to (1, 0, 0, 0).
Raises:
TypeError: If output_bus is not a bus of width 4.
"""
def __init__(self, enable, clear_n, clock, output_bus):
if len(output_bus) != 4:
raise TypeError(
"Expected bus of width 4, received bus of width {0}.".format(
len(output_bus)
)
)

vcc = Wire()
vcc.value = 1
output_1 = Wire()
output_2 = Wire()

COUNT.UpCounterMod4(enable, clear_n, clock, output_1, output_2)
signal.Decoder1Of4(vcc, output_1, output_2, output_bus)


class RingCounter8:
"""Construct a new 8-bit ring counter.
Args:
enable: An object of type Wire. Enables the ring counter.
clear_n: An object of type Wire. Clears output_bus to
(0, 0, 0, 0, 0, 0, 0, 1) (the 0 state) if its value is 0.
clock: An object of type Wire or Clock. The clock input.
output_bus: An object of type Bus8. The one-hot output of the ring
counter. Starts at (0, 0, 0, 0, 0, 0, 0, 1) and counts up to
(1, 0, 0, 0, 0, 0, 0, 0).
Raises:
TypeError: If output_bus is not a bus of width 8.
"""
def __init__(self, enable, clear_n, clock, output_bus):
if len(output_bus) != 8:
raise TypeError(
"Expected bus of width 8, received bus of width {0}.".format(
len(output_bus)
)
)

vcc = Wire()
vcc.value = 1
output_1 = Wire()
output_2 = Wire()
output_3 = Wire()

COUNT.UpCounterMod8(
enable,
clear_n,
clock,
output_1,
output_2,
output_3
)
signal.Decoder1Of8(vcc, output_1, output_2, output_3, output_bus)


class RingCounter16:
"""Construct a new 16-bit ring counter.
Args:
enable: An object of type Wire. Enables the ring counter.
clear_n: An object of type Wire. Clears output_bus to
(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1) (the 0 state) if
its value is 0.
clock: An object of type Wire or Clock. The clock input.
output_bus: An object of type Bus16. The one-hot output of the ring
counter. Starts at (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1)
and counts up to (1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0).
Raises:
TypeError: If output_bus is not a bus of width 16.
"""
def __init__(self, enable, clear_n, clock, output_bus):
if len(output_bus) != 16:
raise TypeError(
"Expected bus of width 16, received bus of width {0}.".format(
len(output_bus)
)
)

vcc = Wire()
vcc.value = 1
output_1 = Wire()
output_2 = Wire()
output_3 = Wire()
output_4 = Wire()
output_bus_int = Bus4(output_1, output_2, output_3, output_4)

COUNT.UpCounterMod16(
enable,
clear_n,
clock,
output_bus_int
)
signal.Decoder1Of16(vcc, output_bus_int, output_bus)

0 comments on commit eb8e565

Please sign in to comment.