# Chapter 4 : Combinational And Sequential Logic Circuits¶

## Example4_1,pg 488¶

In [1]:
# find output and carry
# it is a half-adder circuit with the output 'a' and carry 'c' given by the boolean equations

import math
#Variable declaration
b1   = 1                       #input-1
b2   = 1                       #input-2

#Calculations
a=(b1 and (not b2))+((not b1) and b2)  #sum
c=(b1 and b2)                   #carry

#Result
print("sum:")
print("a = %d\n"%a)
print("carry:")
print("c = %.f"%c)

sum:
a = 0

carry:
c = 1


## Example4_2,pg 489¶

In [2]:
# find difference and borrow
#the circuit is that of a half subtractor

import math
#Variable declaration
b1   = 1                       #input-1, case-1
B1   = 0                       #input-2  case-1
b2   = 1                       #input-1, case 2
B2   = 1                       #input-2, case 2

#Calculations
#case-1
d1=(b1 and (not B1))+(B1 and (not b1)) #difference
r1=(b1 and (not B1))                    #borrow

#case-2
d2=(b2 and (not B2))+(B2 and (not b2))
r2=(b2 and (not B2))

#Result
print("difference case-1:")
print("d1 = %d"%d1)
print("borrow case-1:")
print("r1 = %.f\n"%r1)
print("difference case-2:")
print("d2 = %.f"%d2)
print("borrow case-2:")
print("r2 = %.f\n"%r2)

difference case-1:
d1 = 1
borrow case-1:
r1 = 1

difference case-2:
d2 = 0
borrow case-2:
r2 = 0



## Example4_3,pg 489¶

In [3]:
# find final output

import math
#Variable declaration
b=1                    #input-1
B=0                    #input-2

#Calculations
y=(not((b or B)) or (b and B))

#Result
print("final output:")
print("y = %d"%y)

final output:
y = 0


## Example4_4_a,pg 489¶

In [5]:
# decoder output

import math
#VAriable declaration
#initial conditions
b  = 0                # input
Bi = 0                # initial value
Bf = 1                # final value

#initial state of outputs

y1i=not(b or Bi)

y2i=not(b or (not Bi))

y3i=not(Bi or (not(b)))

y4i=not((not(Bi)) or (not(b)))

#final state of outputs

y1f=not(b or Bf)

y2f=not(Bf or (not(b)))

y3f=not(b or (not(Bf)))

y4f=not((not(Bf)) or (not(b)))

print("first: ")
print("y1 = %.f "%y1i)
print("y2 = %.f "%y2i)
print("y3 = %.f "%y3i)
print("y4 = %.f\n"%y4i)
print("next: ")
print("y1 = %.f "%y1f)
print("y2 = %.f "%y2f)
print("y3 = %.f "%y3f)
print("y4 = %.f"%y4f)

first:
y1 = 1
y2 = 0
y3 = 0
y4 = 0

next:
y1 = 0
y2 = 0
y3 = 1
y4 = 0


## Example4_4_b,pg 489¶

In [12]:
# decoder output

import math
#VAriable declaration
#initial conditions
b  = 1                # input
Bi = 0                # initial value
Bf = 1                # final value

#initial state of outputs

y1i=not(b or Bi)

y2i=not(Bi or (not(b)))

y3i=not(b or (not Bi))

y4i=not((not(Bi)) or (not(b)))

#final state of outputs

y1f=not(b or Bf)

y2f=not(b or (not(Bf)))

y3f=not(Bf or (not(b)))

y4f=not((not(Bf)) or (not(b)))

print("first: ")
print("y1=%.f "%y1i)
print("y2=%.f "%y2i)
print("y3=%.f "%y3i)
print("y4=%.f\n"%y4i)
print("next: ")
print("y1=%.f "%y1f)
print("y2=%.f "%y2f)
print("y3=%.f "%y3f)
print("y4=%.f"%y4f)

first:
y1=0
y2=1
y3=0
y4=0

next:
y1=0
y2=0
y3=0
y4=1


## Example4_5,pg 489¶

In [11]:
# convert 8421 to 2421 code

import math
#Variable declaration
#if A8,B8,C8,D8 is the binary in 8421 code, for 12 this would be 1100(DCBA)
#in 8421-code
A8 = 0
B8 = 0
C8 = 1
D8 = 1

#Calculations
#in 2421-code
D2 = D8
C2 =(C8 or D8)
B2 =(B8 or D8)
A2=A8

#Result
print("2421-code for 12(1100) is:")
print("%d%d%d%d "%(D2,C2,B2,A2))

2421-code for 12(1100) is:
1110


## Example4_6,pg 490¶

In [12]:
# Xcess 3 code

import math
#Variable declaration
x   = "0010"                 #binary-2
y   = "0100"                 #binary-4
z   = "0111"                 #binary-7

#Calculations
x   = int(x,2)
XS31=bin(XS31)[2:]
y=int(y,2)
XS32=bin(XS32)[2:]
z=int(z,2)
XS33=bin(XS33)[2:]

#Result
print("XS-3 for 2: %s"%XS31)
print("XS-3 for 4: %s"%XS32)
print("XS-3 for 7: %s"%XS33)

XS-3 for 2: 101
XS-3 for 4: 111
XS-3 for 7: 1010


## Example4_7,pg 490¶

In [14]:
# 8 to 1 MUX

# one can see from relations of AND gate outputs in terms of address bits and input bit
# that there are possibilities depending on which input is low
# if I7=0, by making all address bits s1,s2,s3 as 1 one can have the conditions satisfied

#Result
print("This requires all the outputs from AND gates should be low")

This requires all the outputs from AND gates should be low


## Example4_8,pg 490¶

In [2]:
# truth table of RS flip flop

import math
#Variable declaration
#enter binary 1-bit values only

S  = input("Enter value of S\n")
R  = input("Enter value of R\n")
Qn = input("Enter previous value of Q\n")
En = input("Enter enable value\n")
print("RS flip-flop truth table:")
if (En==0):
op=Qn
print("op = %d"%op)
elif(( S==0) and (R==0)):
op=Qn
print("op = %d"%op)
elif ((S==0) and (R==1)):
op=0
print("op = %d"%op)
elif((S==1) and (R==0)):
op=1
print("op = %d"%op)
elif ((S==1) and (R==1)):
print("output not determinable")

print("the relations are:")
print("Qn=(R+Qn*)*")              #Q* = not(Q)
print("Qn*=(S+Qn)*")

Enter value of S
1
Enter value of R
0
Enter previous value of Q
1
Enter enable value
1
RS flip-flop truth table:
op = 1
the relations are:
Qn=(R+Qn*)*
Qn*=(S+Qn)*


## Example4_9,pg 491¶

In [26]:
# JK fiip flop

# Q=(Q*+Q.K)* and Q*=(Q+Q*.J)*
# with J=K=0 Q=Q and Q*=Q*
# Q* is not(Q)

#Result
print("operational equations:\n")
print("Q=(Q*+Q.K)* and Q*=(Q+Q*.J)*\n")
print("holds good where Q and Q* should be given appropriate values")

operational equations:

Q=(Q*+Q.K)* and Q*=(Q+Q*.J)*

holds good where Q and Q* should be given appropriate values


## Example4_10,pg 491¶

In [27]:
# 3 bit binary counter

print("It remains positive from the falling edge of pulse-2, then falling edge of 4th, 6th and 8th pulses and so on....")

It remains positive from the falling edge of pulse-2, then falling edge of 4th, 6th and 8th pulses and so on....


## Example4_11,pg 491¶

In [30]:
# 6 modulo counter

print("All modulo counters are basically scalars.A 6-modulo counter is a 6-scaler")
print("so that after 6-input pulses the content of counter becomes 000(reset)")

All modulo counters are basically scalars.A 6-modulo counter is a 6-scaler
so that after 6-input pulses the content of counter becomes 000(reset)


## Example4_12,pg 491¶

In [32]:
# 3 bit 5 modulo counter

print("Normal count would be 2^3=8, while 5-modulo counter would limit it to 5, so that illegitimate states are 8-5=3")

Normal count would be 2^3=8, while 5-modulo counter would limit it to 5, so that illegitimate states are 8-5=3