3-to-8 Decoder in Python

From dftwiki3
Revision as of 12:21, 14 February 2016 by Thiebaut (talk | contribs) (Created page with " --~~~~ ---- ::<source lang="python"> # hw2.py # Based on Li Chai's program, edited by D. Thiebaut # # Simulates a 3-to-8 decoder with an active-high enable, # and an active-...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

--D. Thiebaut (talk) 12:21, 14 February 2016 (EST)


# hw2.py
# Based on Li Chai's program, edited by D. Thiebaut
#
# Simulates a 3-to-8 decoder with an active-high enable,
# and an active-high outputs.
#
import math

def decoder( enable, a, b, c):
    L = 8*[0]
    if enable==1:
        decimal = c + b*2 + a*4
        L[decimal] = 1
    return L

def OR( a, b ):
    if a==1 or b==1: return 1
    return 0

def AND( a, b ):
    if a==1 and b==1: return 1
    return 0

def NOR( a, b ):
    return 1-OR( a, b )

def NAND( a, b ):
    return 1-AND( a, b )

def majority( a, b, c ):
    Y0, Y1, Y2, Y3, Y4, Y5, Y6, Y7 = decoder( 1, a, b, c )
    return OR( OR( Y3, Y5), OR( Y6, Y7 ) )

def main1():
    print( "e", "a", "b", "c", "|", "7", "6", "5", "4", "3", "2", "1", "0" )
    print( "--------------------------" )
    for enable in [0,1]:
        for a in [0,1]:
            for b in [0,1]:
                for c in [0,1]:
                    Y0,Y1,Y2,Y3,Y4,Y5,Y6,Y7 = decoder( enable, a, b, c )
                    print( enable, a, b, c,"|", Y7, Y6, Y5, Y4, Y3, Y2, Y1, Y0 )


def main2():
    print( "a", "b", "c", "|", "majority" )
    print( "--------------------------" )
    for a in [0,1]:
        for b in [0,1]:
            for c in [0,1]:
                m = majority( a, b, c )
                print( a, b, c,"|", m )

# main1()
main2()


Output


e a b c | 7 6 5 4 3 2 1 0
--------------------------
0 0 0 0 | 0 0 0 0 0 0 0 0
0 0 0 1 | 0 0 0 0 0 0 0 0
0 0 1 0 | 0 0 0 0 0 0 0 0
0 0 1 1 | 0 0 0 0 0 0 0 0
0 1 0 0 | 0 0 0 0 0 0 0 0
0 1 0 1 | 0 0 0 0 0 0 0 0
0 1 1 0 | 0 0 0 0 0 0 0 0
0 1 1 1 | 0 0 0 0 0 0 0 0
1 0 0 0 | 0 0 0 0 0 0 0 1
1 0 0 1 | 0 0 0 0 0 0 1 0
1 0 1 0 | 0 0 0 0 0 1 0 0
1 0 1 1 | 0 0 0 0 1 0 0 0
1 1 0 0 | 0 0 0 1 0 0 0 0
1 1 0 1 | 0 0 1 0 0 0 0 0
1 1 1 0 | 0 1 0 0 0 0 0 0
1 1 1 1 | 1 0 0 0 0 0 0 0

a b c | majority
--------------------------
0 0 0 | 0
0 0 1 | 0
0 1 0 | 0
0 1 1 | 1
1 0 0 | 0
1 0 1 | 1
1 1 0 | 1
1 1 1 | 1