├── .gitignore ├── LICENSE ├── README.md ├── pyarch ├── __init__.py ├── combinational │ ├── __init__.py │ ├── arithmetics.py │ ├── decoders.py │ └── gates.py ├── connectors │ ├── __init__.py │ └── transport.py ├── requirements.txt ├── sequential │ ├── __init__.py │ └── ff.py ├── test │ ├── plug-in-2x4Decoder.py │ ├── plug-in-GrayCode.py │ ├── plug-in-PipoAdder.py │ ├── plug-in-SRff.py │ ├── plug-in-and.py │ └── plug-in-fulladder.py └── utils │ ├── __init__.py │ ├── ioManager.py │ └── new.py ├── samples ├── __init__.py ├── plug-in-2x4Decoder.py ├── plug-in-GrayCode.py ├── plug-in-PipoAdder.py ├── plug-in-SRff.py ├── plug-in-and.py └── plug-in-fulladder.py ├── setup.cfg └── setup.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | env/ 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | 27 | # PyInstaller 28 | # Usually these files are written by a python script from a template 29 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 30 | *.manifest 31 | *.spec 32 | 33 | # Installer logs 34 | pip-log.txt 35 | pip-delete-this-directory.txt 36 | 37 | # Unit test / coverage reports 38 | htmlcov/ 39 | .tox/ 40 | .coverage 41 | .coverage.* 42 | .cache 43 | nosetests.xml 44 | coverage.xml 45 | *,cover 46 | .hypothesis/ 47 | 48 | # Translations 49 | *.mo 50 | *.pot 51 | 52 | # Django stuff: 53 | *.log 54 | local_settings.py 55 | 56 | # Flask stuff: 57 | instance/ 58 | .webassets-cache 59 | 60 | # Scrapy stuff: 61 | .scrapy 62 | 63 | # Sphinx documentation 64 | docs/_build/ 65 | 66 | # PyBuilder 67 | target/ 68 | 69 | # IPython Notebook 70 | .ipynb_checkpoints 71 | 72 | # pyenv 73 | .python-version 74 | 75 | # celery beat schedule file 76 | celerybeat-schedule 77 | 78 | # dotenv 79 | .env 80 | 81 | # virtualenv 82 | venv/ 83 | ENV/ 84 | 85 | # Spyder project settings 86 | .spyderproject 87 | 88 | # Rope project settings 89 | .ropeproject 90 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 jumpip 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PyArch 2 | #### Hardware Abstraction Library in Python 3 | [![PyPI version](https://badge.fury.io/py/pyarch.svg)](https://badge.fury.io/py/pyarch) 4 | [![License](http://img.shields.io/:license-mit-blue.svg)](http://doge.mit-license.org) 5 | 6 | 7 | 1. [About PyArch](#about-pyarch) 8 | 2. [Why PyArch](#why-pyarch) 9 | 3. [Capabilities](#capabilities) 10 | 4. [Installation](#installation) 11 | 5. [Inbuilt Abstractions](#inbuilt-abstractions) 12 | 6. [How To Use?](#how-to-use) 13 | 7. [Create your own custom abstraction](#creating-a-custom-hardware-abstraction) 14 | 8. [To-do list](#to-do-list) 15 | 9. [Contributing](#contributing) 16 | 10. [License](LICENSE) 17 | 18 | ### About PyArch 19 | PyArch is basically, a hardware abstraction library that can be used to model a digital system. It provides construct for modeling the system hierarchically and supports both top-down and bottom-up design methodologies. 20 | 21 | ### Why PyArch? 22 | As computer science students, we were reluctant to learn a totally new language ([see VHDL](https://en.wikipedia.org/wiki/VHDL)) for hardware modelling. So, we came up with a our small-scaled version of hardware modelling in Python, a language that is pretty easy to learn! 23 | 24 | ### Capabilities 25 | - __Supports hierarchy__: A digital system can be modeled as a set of interconnected components; each component, in turn, can be modeled as a set of interconnected subcomponents. 26 | 27 | eg: A Full Adder can be implemented using two Half Adders and one Or Gate. 28 | 29 | - __Supports flexible design methodologies__: 30 | - **Top-down:** A top-down approach (also known as stepwise design) is essentially the breaking down of a system to gain insight into the sub-systems that make it up. In a top-down approach an overview of the system is formulated, specifying but not detailing any first-level subsystems. 31 | - **Bottom-up:** The bottom up design model starts with most specific and basic components. It proceeds with composing higher level of components by using basic or lower level components. 32 | - **Mixed:** Both, top-down and bottom-up approaches are not practical individually. Instead, a good combination of both is recommended. 33 | 34 | - __Structural description style__: PyArch is objected oriented. Hence, it supports structural description style. Any hardware maybe emulated by specifying its sub-components using existing abstractions, or creating new ones. 35 | 36 | - __Wide range of Abstraction levels__: Ranging from abstract behavioral descriptions [see Hardware](utils/new.py) to [precise gate level descriptions](combinational/gates.py) to hardware built on top of these abstractions ([Decoders](combinational/decoders.py), [Arithmetic circuits such as Adders](combinational/arithmetics.py)) 37 | 38 | ### Installation 39 | ``` 40 | git clone https://github.com/jumpip/PyArch.git & cd PyArch/ 41 | python setup.py install 42 | ``` 43 | 44 | or 45 | 46 | ``` 47 | pip install pyarch 48 | ``` 49 | 50 | ### Inbuilt Abstractions: 51 | - Gates 52 | - AndGate 53 | - NandGate 54 | - OrGate 55 | - NorGate 56 | - XorGate 57 | - NotGate 58 | - Decoders 59 | - Decoder 1x2 60 | - Decoder 2x4 61 | - Arithmetic circuits 62 | - Half Adder 63 | - Full Adder 64 | - Parallel-in Parallel-out Adder 65 | - Asynchronous timing circuits 66 | - S-R flip flop 67 | - D flip flop 68 | 69 | ### How to use? 70 | See [Samples](samples/) 71 | 72 | ### Creating a custom Hardware Abstraction 73 | 74 | **Some Basic Rules** 75 | - Every Class/hardware extends on Hardware. 76 | - Every Class must be declared as [New-Style](https://docs.python.org/2/reference/datamodel.html#new-style-and-classic-classes) not Classic-Style. 77 | - All the logic goes inside the hardware method of your component's Class. 78 | - With the help of getSignal and propagateSignal methods of Wire, read changes from input Wire instances, use your logic on them, and emit result through the output Wire instance. 79 | 80 | Example: 81 | ```python 82 | class NotGate(new.Hardware,object): 83 | def __init__(self,x,o): 84 | try: 85 | (len(x) != 1 or len(o) != 1) 86 | except NotImplementedError: 87 | print('Invalid Connections') 88 | super(NotGate,self).__init__([x, o]) 89 | input = [] 90 | input.append(x) 91 | self.input = input # A list of all the inputs 92 | self.output = o 93 | self.x = x 94 | self.o = o 95 | hardware = partial(hardware, self) 96 | 97 | def hardware(self): 98 | xSig = self.x[0].getSignal() 99 | try: 100 | xSig 101 | except NameError: 102 | self.o[0].propagateSignal(undefined) 103 | if(xSig == 1): 104 | self.o[0].propagateSignal(0) 105 | else: 106 | self.o[0].propagateSignal(1) 107 | ``` 108 | ###Create using already existing Abstractions 109 | Let's create a Binary to Gray code convertor 110 | 111 | Example: 112 | ```python 113 | class GrayCode_CVT(new.Hardware,object): 114 | def __init__(self,inp,out): 115 | super(GrayCode_CVT,self).__init__([inp,out]) 116 | input=[] 117 | input.append(inp) 118 | self.input=input 119 | self.output=out 120 | inp[0].on('signal',partial(self.hardware,inp,out)) #MSB remains same after conversion 121 | self.components.append(gates.XorGate([inp[0]],[inp[1]],[out[1]])) 122 | self.components.append(gates.XorGate([inp[1]],[inp[2]],[out[2]])) 123 | self.components.append(gates.XorGate([inp[2]],[inp[3]],[out[3]])) 124 | 125 | def hardware(self,inp,out): 126 | sig = inp[0].getSignal() 127 | if sig: 128 | out[0].propagateSignal(1) 129 | else: 130 | out[0].propagateSignal(0) 131 | ``` 132 | ##### Inspired from [Architect](https://github.com/mbad0la/Architect) 133 | 134 | ### To-Do List 135 | - [ ] Multiplexers 136 | - [ ] Counters 137 | - [ ] Shift Registers 138 | - [ ] Pulse triggered FFs (JK FF, T FF) 139 | - [x] Asynchronous inputs (Clear, Enable) 140 | - [x] Clocked inputs 141 | 142 | ### Contributing 143 | Pick any of the topics in the task list above and get hacking! Or raise an issue or a pull request. 144 | 145 | This repository is maintained by [Jeevesh Narang](https://github.com/JeeveshN), [Prachi Manchanda](https://github.com/prachi1210) and [Mansimar Kaur](https://github.com/mansimarkaur) 146 | ### License 147 | PyArch is distributed under the [MIT License](LICENSE) 148 | -------------------------------------------------------------------------------- /pyarch/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jumpip/pyarch/e78a62d1ddec4472171c141c5aef40a900b4f75b/pyarch/__init__.py -------------------------------------------------------------------------------- /pyarch/combinational/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jumpip/pyarch/e78a62d1ddec4472171c141c5aef40a900b4f75b/pyarch/combinational/__init__.py -------------------------------------------------------------------------------- /pyarch/combinational/arithmetics.py: -------------------------------------------------------------------------------- 1 | import gates 2 | import sys 3 | sys.path.insert(0, '../utils') 4 | import new 5 | sys.path.insert(0, '../connectors') 6 | import transport 7 | 8 | class HalfAdder(new.Hardware,object): 9 | def __init__(self,x,s): 10 | try: 11 | (len(x) != 2 or len(s) != 2) 12 | except NotImplementedError: 13 | print('Invalid Connections') 14 | input=[] 15 | input.append(x) 16 | self.input=input 17 | self.output=s 18 | super(HalfAdder,self).__init__([x, s]) 19 | self.components.append(gates.XorGate([x[0]],[x[1]],[s[1]])) 20 | self.components.append(gates.AndGate([x[0]],[x[1]],[s[0]])) 21 | 22 | class FullAdder(new.Hardware,object): 23 | def __init__(self,x,s): 24 | try: 25 | (len(x)!=3 or len(s)!=2) 26 | except NotImplementedError: 27 | print('Invalid Connections') 28 | super(FullAdder,self).__init__([x,s]) 29 | input=[] 30 | input.append(x) 31 | self.input=input 32 | self.output=s 33 | self.internalWiring = transport.wires(3) 34 | self.components.append(HalfAdder([x[0],x[1]],[self.internalWiring[0], self.internalWiring[1]])) 35 | self.components.append(HalfAdder([self.internalWiring[1],x[2]],[self.internalWiring[2],s[1]])) 36 | self.components.append(gates.OrGate([self.internalWiring[0]],[self.internalWiring[2]],[s[0]])) 37 | 38 | class PipoAdder(new.Hardware,object): 39 | 40 | def __init__(self,x,y,s): 41 | try: 42 | (len(x) != len(y) or len(s) != len(x)+1) 43 | except NotImplementedError: 44 | print('Invalid Connections') 45 | super(PipoAdder,self).__init__([x,y,s]) 46 | input = [] 47 | input.append(x) 48 | input.append(y) 49 | self.input=input 50 | self.output=s 51 | size = len(x) 52 | self.internalWiring = transport.wires(size) 53 | if size > 1: 54 | self.components.append(FullAdder([x[0], y[0], self.internalWiring[0]], [self.internalWiring[1], s[size]])) 55 | for i in xrange(size-1): 56 | self.components.append(FullAdder([x[i], y[i], self.internalWiring[i]], [self.internalWiring[i+1], s[size-i]])) 57 | self.components.append(FullAdder([x[size-1], y[size-1], self.internalWiring[size-1]], [s[0], s[1]])) 58 | else: 59 | self.components.append(FullAdder([x[0], y[0], self.internalWiring[0]], [s[0], s[1]])) 60 | self.internalWiring[0].propagateSignal(0) 61 | -------------------------------------------------------------------------------- /pyarch/combinational/decoders.py: -------------------------------------------------------------------------------- 1 | import gates 2 | import sys 3 | sys.path.insert(0, '../utils') 4 | import new 5 | sys.path.insert(0, '../connectors') 6 | import transport 7 | 8 | class Decoder1x2(new.Hardware,object): 9 | def __init__(self,x,o): 10 | try: 11 | (len(x) != 1 or len(o) != 1) 12 | except NotImplementedError: 13 | print('Invalid Connections') 14 | super(Decoder1x2,self).__init__([x,[o[0],x[0]]]) 15 | self.components.append(gates.NotGate(x,o)) 16 | 17 | class Decoder2x4(new.Hardware,object): 18 | def __init__(self,x0,x1,o): 19 | try: 20 | (len(x0)!=1 or len(x1)!=1 or len(o)!=4) 21 | except NotImplementedError: 22 | print('Invalid Connections') 23 | super(Decoder2x4,self).__init__([x0,x1,o]) 24 | input=[] 25 | input.append(x0) 26 | input.append(x1) 27 | self.input=input 28 | self.output=o 29 | self.internalWiring = transport.wires(2) 30 | self.components.append(Decoder1x2(x0, [self.internalWiring[0]])) 31 | self.components.append(Decoder1x2(x1, [self.internalWiring[1]])) 32 | self.components.append(gates.AndGate([self.internalWiring[0]],[self.internalWiring[1]],[o[0]])) 33 | self.components.append(gates.AndGate([self.internalWiring[0]],x1, [o[1]])) 34 | self.components.append(gates.AndGate(x0, [self.internalWiring[1]], [o[2]])) 35 | self.components.append(gates.AndGate(x0,x1,[o[3]])) 36 | -------------------------------------------------------------------------------- /pyarch/combinational/gates.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.insert(0, '../utils') 3 | import ioManager 4 | import new 5 | from functools import partial 6 | 7 | class AndGate(new.Hardware,object): 8 | 9 | def __init__(self,x,y,o): 10 | try: 11 | (len(x) != 1 or len(y) != 1 or len(o) != 1) 12 | except NotImplementedError: 13 | print('Invalid Connections') 14 | super(AndGate,self).__init__([x,y,o]) 15 | input=[] 16 | input.append(x) 17 | input.append(y) 18 | self.input=input 19 | self.output=o 20 | self.x = x 21 | self.y = y 22 | self.o = o 23 | x[0].on('signal', self.hardware) 24 | y[0].on('signal', self.hardware) 25 | 26 | def hardware(self): 27 | xSig = self.x[0].getSignal() 28 | ySig = self.y[0].getSignal() 29 | try: 30 | xSig, ySig 31 | except NameError: 32 | self.o[0].propagateSignal(None) 33 | if(xSig == 0 or ySig == 0): 34 | self.o[0].propagateSignal(0) 35 | else: 36 | self.o[0].propagateSignal(1) 37 | 38 | class NandGate(new.Hardware,object): 39 | 40 | def __init__(self,x,y,o): 41 | try: 42 | (len(x) != 1 or len(y) != 1 or len(o) != 1) 43 | except NotImplementedError: 44 | print('Invalid Connections') 45 | super(NandGate,self).__init__([x,y,o]) 46 | input=[] 47 | input.append(x) 48 | input.append(y) 49 | self.input=input 50 | self.output=o 51 | self.x = x 52 | self.y = y 53 | self.o = o 54 | x[0].on('signal', self.hardware) 55 | y[0].on('signal', self.hardware) 56 | 57 | def hardware(self): 58 | xSig = self.x[0].getSignal() 59 | ySig = self.y[0].getSignal() 60 | try: 61 | xSig, ySig 62 | except NameError: 63 | self.o[0].propagateSignal(None) 64 | if(xSig == 0 or ySig == 0): 65 | self.o[0].propagateSignal(1) 66 | else: 67 | self.o[0].propagateSignal(0) 68 | 69 | class OrGate(new.Hardware,object): 70 | 71 | def __init__(self,x,y,o): 72 | try: 73 | (len(x) != 1 or len(y) != 1 or len(o) != 1) 74 | except NotImplementedError: 75 | print('Invalid Connections') 76 | super(OrGate,self).__init__([x, y, o]) 77 | input=[] 78 | input.append(x) 79 | input.append(y) 80 | self.input=input 81 | self.output=o 82 | self.x = x 83 | self.y = y 84 | self.o = o 85 | x[0].on('signal', self.hardware) 86 | y[0].on('signal', self.hardware) 87 | hardware = partial(self.hardware, self) 88 | 89 | def hardware(self): 90 | xSig = self.x[0].getSignal() 91 | ySig = self.y[0].getSignal() 92 | try: 93 | xSig, ySig 94 | except NameError: 95 | self.o[0].propagateSignal(None) 96 | if(xSig == 1 or ySig == 1): 97 | self.o[0].propagateSignal(1) 98 | else: 99 | self.o[0].propagateSignal(0) 100 | 101 | class NorGate(new.Hardware,object): 102 | 103 | def __init__(self,x,y,o): 104 | try: 105 | (len(x) != 1 or len(y) != 1 or len(o) != 1) 106 | except NotImplementedError: 107 | print('Invalid Connections') 108 | super(NorGate,self).__init__([x, y, o]) 109 | input=[] 110 | input.append(x) 111 | input.append(y) 112 | self.input=input 113 | self.output=o 114 | self.x = x 115 | self.y = y 116 | self.o = o 117 | x[0].on('signal', self.hardware) 118 | y[0].on('signal', self.hardware) 119 | 120 | def hardware(self): 121 | xSig = self.x[0].getSignal() 122 | ySig = self.y[0].getSignal() 123 | try: 124 | xSig, ySig 125 | except NameError: 126 | self.o[0].propagateSignal(None) 127 | if(xSig == 1 or ySig == 1): 128 | self.o[0].propagateSignal(0) 129 | else: 130 | self.o[0].propagateSignal(1) 131 | 132 | 133 | class NotGate(new.Hardware,object): 134 | 135 | def __init__(self,x,o): 136 | try: 137 | (len(x) != 1 or len(o) != 1) 138 | except NotImplementedError: 139 | print('Invalid Connections') 140 | super(NotGate,self).__init__([x, o]) 141 | input=[] 142 | input.append(x) 143 | self.input=input 144 | self.output=o 145 | self.x = x 146 | self.o = o 147 | x[0].on('signal', self.hardware) 148 | 149 | def hardware(self): 150 | xSig = self.x[0].getSignal() 151 | try: 152 | xSig 153 | except NameError: 154 | self.o[0].propagateSignal(undefined) 155 | if(xSig == 1): 156 | self.o[0].propagateSignal(0) 157 | else: 158 | self.o[0].propagateSignal(1) 159 | 160 | class XorGate(new.Hardware,object): 161 | 162 | def __init__(self,x,y,o): 163 | try: 164 | (len(x) != 1 or len(y) != 1 or len(o) != 1) 165 | except NotImplementedError: 166 | print('Invalid Connections') 167 | super(XorGate,self).__init__([x, y, o]) 168 | input=[] 169 | input.append(x) 170 | input.append(y) 171 | self.input=input 172 | self.output=o 173 | self.x = x 174 | self.y = y 175 | self.o = o 176 | x[0].on('signal', self.hardware) 177 | y[0].on('signal', self.hardware) 178 | 179 | def hardware(self): 180 | xSig = self.x[0].getSignal() 181 | ySig = self.y[0].getSignal() 182 | try: 183 | xSig, ySig 184 | except NameError: 185 | self.o[0].propagateSignal(undefined) 186 | if(xSig != ySig): 187 | self.o[0].propagateSignal(1) 188 | else: 189 | self.o[0].propagateSignal(0) 190 | -------------------------------------------------------------------------------- /pyarch/connectors/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jumpip/pyarch/e78a62d1ddec4472171c141c5aef40a900b4f75b/pyarch/connectors/__init__.py -------------------------------------------------------------------------------- /pyarch/connectors/transport.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.insert(0, '../utils') 3 | import ioManager 4 | import new 5 | import threading 6 | from functools import partial 7 | from pyee import EventEmitter 8 | from hiatus import set_interval,clear_interval 9 | 10 | 11 | def wires(number): 12 | wireSet = list() 13 | for i in xrange(0,number): 14 | wireSet.append(Wire(None)) 15 | return wireSet 16 | 17 | class Wire(EventEmitter,object): 18 | 19 | def propagateSignal(self,newSig): 20 | oldSig = self.signal 21 | self.signal = newSig 22 | if(oldSig != self.signal): 23 | self.emit('signal') 24 | 25 | def getSignal(self): 26 | return self.signal 27 | 28 | def __init__(self,sig): 29 | super(Wire,self).__init__() 30 | self.signal = sig 31 | 32 | class Pulse(Wire,object): 33 | 34 | def alter(self): 35 | self.propagateSignal(int(not self.signal)) 36 | 37 | def switchOn(self): 38 | if(not self.interval): 39 | self.signal = self.val 40 | self.interval = set_interval(self.alter,self.timePeriod) 41 | 42 | def switchOff(self): 43 | if(self.interval): 44 | clear_interval(self.interval) 45 | self.signal = None 46 | self.interval = None 47 | 48 | def __init__(self,time,val): 49 | super(Pulse,self).__init__() 50 | self.val = val 51 | self.timePeriod = time 52 | self.interval = None 53 | -------------------------------------------------------------------------------- /pyarch/requirements.txt: -------------------------------------------------------------------------------- 1 | Pyee == 3.0.1 2 | Hiatus == 0.0.1 -------------------------------------------------------------------------------- /pyarch/sequential/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jumpip/pyarch/e78a62d1ddec4472171c141c5aef40a900b4f75b/pyarch/sequential/__init__.py -------------------------------------------------------------------------------- /pyarch/sequential/ff.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.insert(0, '../utils') 3 | import new 4 | sys.path.insert(0, '../combinational') 5 | import gates 6 | sys.path.insert(0, '../connectors') 7 | import transport 8 | from functools import partial 9 | 10 | class SRFlipFlop(new.Hardware,object): 11 | 12 | def __init__(self,s,r,qqbar,c): 13 | try: 14 | (len(s) != 1 or len(r) != 1 or len(qqbar) != 2) 15 | except NotImplementedError: 16 | print('Invalid Connections') 17 | super(SRFlipFlop,self).__init__([s,r,[qqbar[0]]]) 18 | input=[] 19 | input.append(s) 20 | input.append(r) 21 | input.append(c) 22 | self.input=input 23 | self.output=qqbar 24 | self.internalWiring = transport.wires(2) 25 | self.components.append(gates.AndGate(c,s,[self.internalWiring[0]])) 26 | self.components.append(gates.AndGate(c,r,[self.internalWiring[1]])) 27 | self.components.append(gates.NorGate([self.internalWiring[0]],[qqbar[0]],[qqbar[1]])) 28 | self.components.append(gates.NorGate([self.internalWiring[1]],[qqbar[1]],[qqbar[0]])) 29 | 30 | class DFlipFlop(new.Hardware,object): 31 | 32 | def __init__(self,d,qqbar,c): 33 | try: 34 | len(d) !=1 or len(qqbar)!=2 35 | except NotImplementedError: 36 | print('Invalid Connections') 37 | super(DFlipFlop,self).__init__([d,qqbar[0]]) 38 | input=[] 39 | input.append(d) 40 | input.append(c) 41 | self.input=input 42 | self.output=qqbar 43 | self.internalWiring = transport.wires(3) 44 | self.components.append(gates.NotGate(d, [self.internalWiring[0]])) 45 | self.components.append(gates.NandGate(c, d, [self.internalWiring[1]])) 46 | self.components.append(gates.NandGate(c, [self.internalWiring[0]], [self.internalWiring[2]])) 47 | self.components.append(gates.NorGate([self.internalWiring[1]], [qqbar[1]], [qqbar[0]])) 48 | self.components.append(gates.NorGate([self.internalWiring[2]], [qqbar[0]], [qqbar[1]])) 49 | -------------------------------------------------------------------------------- /pyarch/test/plug-in-2x4Decoder.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.insert(0, '../utils') 3 | import ioManager 4 | import new 5 | sys.path.insert(0, '../connectors') 6 | import transport 7 | sys.path.insert(0,'../combinational') 8 | import decoders 9 | 10 | inputA = transport.wires(1) 11 | inputB = transport.wires(1) 12 | output = transport.wires(4) 13 | 14 | iohandler = ioManager.StringIO(decoders.Decoder2x4(inputA,inputB,output)) 15 | 16 | print iohandler.input('0','1') 17 | -------------------------------------------------------------------------------- /pyarch/test/plug-in-GrayCode.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.insert(0, '../utils') 3 | import ioManager 4 | import new 5 | sys.path.insert(0, '../connectors') 6 | import transport 7 | sys.path.insert(0,'../combinational') 8 | import gates 9 | from functools import partial 10 | class GrayCode_CVT(new.Hardware,object): 11 | def __init__(self,inp,out): 12 | super(GrayCode_CVT,self).__init__([inp,out]) 13 | input=[] 14 | input.append(inp) 15 | self.input=input 16 | self.output=out 17 | inp[0].on('signal',partial(self.hardware,inp,out)) 18 | self.components.append(gates.XorGate([inp[0]],[inp[1]],[out[1]])) 19 | self.components.append(gates.XorGate([inp[1]],[inp[2]],[out[2]])) 20 | self.components.append(gates.XorGate([inp[2]],[inp[3]],[out[3]])) 21 | 22 | def hardware(self,inp,out): 23 | sig = inp[0].getSignal() 24 | if sig: 25 | out[0].propagateSignal(1) 26 | else: 27 | out[0].propagateSignal(0) 28 | 29 | 30 | inputA = transport.wires(4) 31 | out = transport.wires(4) 32 | hware = GrayCode_CVT(inputA,out) 33 | iohandler = ioManager.StringIO(hware) 34 | 35 | print iohandler.input('1001') 36 | -------------------------------------------------------------------------------- /pyarch/test/plug-in-PipoAdder.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.insert(0, '../utils') 3 | import ioManager 4 | import new 5 | sys.path.insert(0, '../connectors') 6 | import transport 7 | sys.path.insert(0,'../combinational') 8 | import arithmetics 9 | 10 | inputA = transport.wires(4) 11 | inputB = transport.wires(4) 12 | out = transport.wires(5) 13 | hware = arithmetics.PipoAdder(inputA,inputB,out) 14 | iohandler = ioManager.StringIO(hware) 15 | 16 | print iohandler.input('1111','1111') 17 | -------------------------------------------------------------------------------- /pyarch/test/plug-in-SRff.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.insert(0, '../utils') 3 | import ioManager 4 | import new 5 | sys.path.insert(0, '../connectors') 6 | import transport 7 | sys.path.insert(0,'../sequential') 8 | import ff 9 | 10 | inputS = transport.wires(1) 11 | inputR = transport.wires(1) 12 | out = transport.wires(2) 13 | clock = transport.wires(1) 14 | hware = ff.SRFlipFlop(inputS,inputR,out,clock) 15 | iohandler = ioManager.StringIO(hware) 16 | 17 | print iohandler.input('0','1','1') 18 | -------------------------------------------------------------------------------- /pyarch/test/plug-in-and.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.insert(0, '../utils') 3 | import ioManager 4 | import new 5 | sys.path.insert(0, '../connectors') 6 | import transport 7 | sys.path.insert(0,'../combinational') 8 | import gates 9 | 10 | inputA = transport.wires(1) 11 | inputB = transport.wires(1) 12 | inputC = transport.wires(1) 13 | 14 | hWare = gates.AndGate(inputA, inputB, inputC) 15 | ioHandler = ioManager.StringIO(hWare) 16 | 17 | print(ioHandler.input('1','1')) 18 | -------------------------------------------------------------------------------- /pyarch/test/plug-in-fulladder.py: -------------------------------------------------------------------------------- 1 | import sys 2 | sys.path.insert(0, '../utils') 3 | import ioManager 4 | import new 5 | sys.path.insert(0, '../connectors') 6 | import transport 7 | sys.path.insert(0,'../combinational') 8 | import arithmetics 9 | 10 | inputA = transport.wires(3) 11 | out = transport.wires(2) 12 | hware = arithmetics.FullAdder(inputA,out) 13 | iohandler = ioManager.StringIO(hware) 14 | 15 | print iohandler.input('1','1') 16 | -------------------------------------------------------------------------------- /pyarch/utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jumpip/pyarch/e78a62d1ddec4472171c141c5aef40a900b4f75b/pyarch/utils/__init__.py -------------------------------------------------------------------------------- /pyarch/utils/ioManager.py: -------------------------------------------------------------------------------- 1 | class StringIO: 2 | 3 | def __init__(self,ioMapping): 4 | self.i = ioMapping.input 5 | self.o = ioMapping.output 6 | 7 | def input(self,*args): 8 | inpIndex = len(args[0]) - 1 9 | totalInps = len(self.i) 10 | pos = 0 11 | while inpIndex >= 0: 12 | for inpNum in range(totalInps): 13 | self.i[inpNum][pos].propagateSignal(int(args[inpNum][inpIndex])) 14 | pos += 1 15 | inpIndex -= 1 16 | 17 | out = list() 18 | for i in self.o: 19 | out.append(str(i.getSignal())) 20 | outBuff = "".join(out) 21 | return outBuff 22 | -------------------------------------------------------------------------------- /pyarch/utils/new.py: -------------------------------------------------------------------------------- 1 | class Hardware: 2 | 3 | def __init__(self,io): 4 | self.ioMapping = io 5 | self.internalWiring = [] 6 | self.components = [] 7 | -------------------------------------------------------------------------------- /samples/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jumpip/pyarch/e78a62d1ddec4472171c141c5aef40a900b4f75b/samples/__init__.py -------------------------------------------------------------------------------- /samples/plug-in-2x4Decoder.py: -------------------------------------------------------------------------------- 1 | from pyarch.utils import ioManager 2 | from pyarch.connectors import transport 3 | from pyarch.combinational import decoders 4 | 5 | inputA = transport.wires(1) 6 | inputB = transport.wires(1) 7 | output = transport.wires(4) 8 | 9 | iohandler = ioManager.StringIO(decoders.Decoder2x4(inputA,inputB,output)) 10 | 11 | print iohandler.input('0','1') 12 | -------------------------------------------------------------------------------- /samples/plug-in-GrayCode.py: -------------------------------------------------------------------------------- 1 | from pyarch.utils import ioManager,new 2 | from pyarch.connectors import transport 3 | from pyarch.combinational import gates 4 | from functools import partial 5 | 6 | class GrayCode_CVT(new.Hardware,object): 7 | 8 | def __init__(self,inp,out): 9 | super(GrayCode_CVT,self).__init__([inp,out]) 10 | input=[] 11 | input.append(inp) 12 | self.input=input 13 | self.output=out 14 | inp[0].on('signal',partial(self.hardware,inp,out)) 15 | self.components.append(gates.XorGate([inp[0]],[inp[1]],[out[1]])) 16 | self.components.append(gates.XorGate([inp[1]],[inp[2]],[out[2]])) 17 | self.components.append(gates.XorGate([inp[2]],[inp[3]],[out[3]])) 18 | 19 | def hardware(self,inp,out): 20 | sig = inp[0].getSignal() 21 | if sig: 22 | out[0].propagateSignal(1) 23 | else: 24 | out[0].propagateSignal(0) 25 | 26 | 27 | inputA = transport.wires(4) 28 | out = transport.wires(4) 29 | hware = GrayCode_CVT(inputA,out) 30 | iohandler = ioManager.StringIO(hware) 31 | 32 | print iohandler.input('1001') 33 | -------------------------------------------------------------------------------- /samples/plug-in-PipoAdder.py: -------------------------------------------------------------------------------- 1 | from pyarch.utils import ioManager 2 | from pyarch.connectors import transport 3 | from pyarch.combinational import arithmetics 4 | 5 | inputA = transport.wires(4) 6 | inputB = transport.wires(4) 7 | out = transport.wires(5) 8 | hware = arithmetics.PipoAdder(inputA,inputB,out) 9 | iohandler = ioManager.StringIO(hware) 10 | 11 | print iohandler.input('1111','1111') 12 | -------------------------------------------------------------------------------- /samples/plug-in-SRff.py: -------------------------------------------------------------------------------- 1 | from pyarch.utils import ioManager 2 | from pyarch.connectors import transport 3 | from pyarch.sequential import ff 4 | 5 | inputS = transport.wires(1) 6 | inputR = transport.wires(1) 7 | out = transport.wires(2) 8 | clock = transport.wires(1) 9 | hware = ff.SRFlipFlop(inputS,inputR,out,clock) 10 | iohandler = ioManager.StringIO(hware) 11 | 12 | print iohandler.input('0','1','1') # S-bit R-bit Clock 13 | -------------------------------------------------------------------------------- /samples/plug-in-and.py: -------------------------------------------------------------------------------- 1 | from pyarch.utils import ioManager 2 | from pyarch.connectors import transport 3 | from pyarch.combinational import gates 4 | 5 | inputA = transport.wires(1) 6 | inputB = transport.wires(1) 7 | inputC = transport.wires(1) 8 | 9 | hWare = gates.AndGate(inputA, inputB, inputC) 10 | ioHandler = ioManager.StringIO(hWare) 11 | 12 | print(ioHandler.input('1','1')) 13 | -------------------------------------------------------------------------------- /samples/plug-in-fulladder.py: -------------------------------------------------------------------------------- 1 | from pyarch.utils import ioManager 2 | from pyarch.connectors import transport 3 | from pyarch.combinational import arithmetics 4 | 5 | inputA = transport.wires(3) 6 | out = transport.wires(2) 7 | hware = arithmetics.FullAdder(inputA,out) 8 | iohandler = ioManager.StringIO(hware) 9 | 10 | print iohandler.input('011') # 'carry bit + 2 sum bits' 11 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | description-file = README.md 3 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup, find_packages 2 | setup( 3 | name='pyarch', 4 | version='0.0.5', 5 | description='Hardware emulation library in python', 6 | url='https://github.com/jumpip/PyArch', 7 | author='Jeevesh Narang, Prachi Manchanda, Mansimar Kaur', 8 | author_email='jumpip.git@gmail.com', 9 | license='MIT', 10 | classifiers=[ 11 | 'Development Status :: 3 - Alpha', 12 | 'License :: OSI Approved :: MIT License', 13 | 'Programming Language :: Python :: 2', 14 | 'Programming Language :: Python :: 2.6', 15 | 'Programming Language :: Python :: 2.7', 16 | ], 17 | packages=find_packages(), 18 | install_requires=['pyee','hiatus'], 19 | ) 20 | --------------------------------------------------------------------------------