You are on page 1of 217

# Scilab Textbook Companion for

## Digital Principals And Applications

by D. P. Leach And A. P. Malvino1
Created by
Kapu Venkat Sayeesh
B.Tech (pursuing)
Electronics Engineering
NIT, Warangal
College Teacher
S.K.L.V Sai Prakash
Cross-Checked by
Giridharan, IITB
May 16, 2016

1 Funded

## by a grant from the National Mission on Education through ICT,

http://spoken-tutorial.org/NMEICT-Intro. This Textbook Companion and Scilab
codes written in it can be downloaded from the Textbook Companion Project
section at the website http://scilab.in

Book Description
Title: Digital Principals And Applications
Author: D. P. Leach And A. P. Malvino
Publisher: Tata McGraw - Hill, New Delhi
Edition: 6
Year: 2006
ISBN: 0-07-060175-5

Scilab numbering policy used in this document and the relation to the
above book.
Exa Example (Solved example)
Eqn Equation (Particular equation of the above book)
AP Appendix to Example(Scilab Code that is an Appednix to a particular
Example of the above book)
For example, Exa 3.51 means solved example 3.51 of this book. Sec 2.3 means
a scilab code whose theory is explained in Section 2.3 of the book.

Contents
List of Scilab Codes

1 Digital Principles

2 Digital Logic

25

43

## 5 Number Systems and Codes

52

6 Arithmetic Circuits

66

90

8 Flip Flops

105

9 Registers

114

10 Counters

122

138

145

13 Memory

151

153

15 Applications

156

161

Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa

1.1
1.2
2.1
2.2
2.3
2.4
2.9
2.10
2.11
2.12
2.13
2.14
2.15
3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.9
3.10
3.11
3.12
3.13
3.14
4.1

## Finding duty cycle . . . . . . . . . . . . .

Maximum decimal count for a counter . .
7404 waveform . . . . . . . . . . . . . . .
7404 waveform . . . . . . . . . . . . . . .
truth table for given figure . . . . . . . .
truth table for given figure . . . . . . . .
proving two circuits are logically equal . .
truth table for NOR NOR circuit . . . . .
timing diagram for NOR NOR . . . . . .
proving two circuits are logically equal . .
truth table for NAND NAND circuit . . .
timing diagram for NAND NAND circuit
detecting all bits low in a register . . . . .
Boolean Algebra . . . . . . . . . . . . . .
Boolean Algebra . . . . . . . . . . . . . .
Testing a circuit using logic clip . . . . . .
Sum of Products . . . . . . . . . . . . . .
Boolean Algebra . . . . . . . . . . . . . .
Gives a simplified Boolean equation . . .
simplest logic for given Truth table . . . .
simplest logic for given logic equation . .
Product of sums . . . . . . . . . . . . . .
sop for the karnaugh map . . . . . . . . .
POS form of karnaugh map . . . . . . . .
POS form of karnaugh map . . . . . . . .
Quine Mc clusky method . . . . . . . . .
Dynamic hard . . . . . . . . . . . . . . .
4 to 1 mux using 2 to 1 mux . . . . . . .
5

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

6
6
7
9
12
12
13
14
14
18
19
19
23
25
25
26
27
28
29
29
30
30
32
32
33
33
37
43

Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa

4.2
4.3
4.4
4.7
4.8
4.9
4.10
4.11
5.1
5.2
5.3
5.4
5.5
5.6
5.7
5.8
5.9
5.10
5.11
6.1
6.2
6.3
6.4
6.5
6.6
6.7
6.8
6.9
6.10
6.12
7.1
7.2
7.3
7.4
7.5
7.6
7.7
7.8

## Realizing boolean equation using 8 to 1 mux . . . . .

32 t0 1 mux using 16 to 1 and 2 to 1 muxes . . . . . .
74154 IC y12 . . . . . . . . . . . . . . . . . . . . . . .
realizing boolean equation using 3 to 8 decoder . . . .
current in LED . . . . . . . . . . . . . . . . . . . . . .
which LED lights up for given input conditions . . . .
output of 74147 when button 6 is pressed . . . . . . .
priority encoder . . . . . . . . . . . . . . . . . . . . .
Binary to decimal conversion . . . . . . . . . . . . . .
Binary to decimal conversion . . . . . . . . . . . . . .
decimal equivalent of 2 Mb . . . . . . . . . . . . . . .
Decimal to binary conversion . . . . . . . . . . . . . .
Binary number having all ones . . . . . . . . . . . . .
Decimal to binary conversion . . . . . . . . . . . . . .
binary to hexadecimal . . . . . . . . . . . . . . . . . .
hexadecimal to decimal . . . . . . . . . . . . . . . . .
decimal to hexadecimal and binary . . . . . . . . . . .
decimal to hexadecimal and binary . . . . . . . . . . .
decimal to hexadecimal and binary . . . . . . . . . . .
8bit binary adder . . . . . . . . . . . . . . . . . . . . .
16 bit binary adder . . . . . . . . . . . . . . . . . . . .
first generation microcomputers addition . . . . . . . .
binary subtraction . . . . . . . . . . . . . . . . . . . .
adding 8 bit unsigned numbers . . . . . . . . . . . . .
subtraction of unsigned numbers . . . . . . . . . . . .
overflow case . . . . . . . . . . . . . . . . . . . . . . .
2s compliment . . . . . . . . . . . . . . . . . . . . . .
2s compliment . . . . . . . . . . . . . . . . . . . . . .
2s compliment subtraction . . . . . . . . . . . . . . . .
final carry in a CLA . . . . . . . . . . . . . . . . . . .
clock cycle time . . . . . . . . . . . . . . . . . . . . .
maximum clock frequency . . . . . . . . . . . . . . . .
frequency limits of the clock . . . . . . . . . . . . . . .
Schmitt trigger . . . . . . . . . . . . . . . . . . . . . .
frequency of oscillation for 555 timer . . . . . . . . . .
finding Ra and C in 555 timer circuit . . . . . . . . . .
output pulse width for the timer . . . . . . . . . . . .
value of C necessary to change pulse width to given values
6

43
46
46
47
48
48
49
50
52
54
56
57
58
59
60
61
62
63
64
66
68
69
71
73
76
78
80
83
85
88
90
90
91
93
94
94
95
96

Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa

7.9
7.10
7.11
8.4
8.5
8.6
8.7
8.9
8.10
8.12
8.13
9.1
9.2
9.4
9.5
9.8
9.9
10.1
10.2
10.3

Exa 10.5
Exa 10.6

Exa 10.7
Exa 10.8
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa

10.9
10.10
10.12
10.13
10.14
10.15
11.1
11.2
11.5

monostable multivibrator . . . . . . . . . . . . . . . .
74123 . . . . . . . . . . . . . . . . . . . . . . . . . . .
finding timing capacitor values . . . . . . . . . . . . .
RS flipflop . . . . . . . . . . . . . . . . . . . . . . . .
positive edge triggred RS flip flop . . . . . . . . . . . .
negative edge triggred RS flip flop . . . . . . . . . . .
T flip flop . . . . . . . . . . . . . . . . . . . . . . . . .
JK master slave . . . . . . . . . . . . . . . . . . . . .
fictitious flip flop excitation table . . . . . . . . . . . .
state transition diagram for given circuit . . . . . . . .
D flip flop to RS flip flop . . . . . . . . . . . . . . . .
shift register serial input . . . . . . . . . . . . . . . . .
shift register serial input and output graph . . . . . .
54164 shift register . . . . . . . . . . . . . . . . . . . .
54164 shift register . . . . . . . . . . . . . . . . . . . .
74ls174 . . . . . . . . . . . . . . . . . . . . . . . . . .
7495A . . . . . . . . . . . . . . . . . . . . . . . . . . .
ripple counter clock frequency . . . . . . . . . . . . . .
number of flip flops required to construct a counter . .
Output waveforms for a 7493A connected as a mod 16
counter . . . . . . . . . . . . . . . . . . . . . . . . . .
Expression for AND gate connected to the leg of OR
gate that drives clock input to flip flop Qd in 74193 . .
Expression for 4 input AND gate connected to the leg
of OR gate that conditions the J and K inputs to the
Qd flip flop in a 74191 . . . . . . . . . . . . . . . . . .
number of flip flops required to construct a counter . .
what modulus counters can be constructed with given
number of flip flops e . . . . . . . . . . . . . . . . . .
mod 6 counter . . . . . . . . . . . . . . . . . . . . . .
Expression for a gate to decode count 8 in a 7492A . .
mod 12 counter . . . . . . . . . . . . . . . . . . . . . .
4 bit binary counter presettable . . . . . . . . . . . . .
self correcting modulo 6 counter . . . . . . . . . . . .
sequence generator . . . . . . . . . . . . . . . . . . . .
synchronous sequential logic circuit . . . . . . . . . . .
vending machine . . . . . . . . . . . . . . . . . . . . .
Reducing state transition diagrams . . . . . . . . . . .
7

96
100
103
105
106
106
107
107
110
112
113
114
115
120
120
121
121
122
122
124
126

127
127
128
128
132
132
133
135
137
138
140
141

Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa
Exa

11.6
11.7
11.8
12.1
12.2
12.3
12.4
12.5
12.6
12.8
12.9
12.10
12.11
12.13
13.2
13.3

## asynchronous sequential circuit . . . . . . . . . . . . .

asynchronous sequential circuit problem in operation .
asynchronous sequential circuit . . . . . . . . . . . . .
binary equivalent weight of each bit in a 4bit system .
5 bit resistive divider . . . . . . . . . . . . . . . . . . .
5 bit ladder . . . . . . . . . . . . . . . . . . . . . . . .
5 bit ladder . . . . . . . . . . . . . . . . . . . . . . . .
5 bit ladder . . . . . . . . . . . . . . . . . . . . . . . .
5 bit ladder . . . . . . . . . . . . . . . . . . . . . . . .
DAC0808 . . . . . . . . . . . . . . . . . . . . . . . . .
resolution of 9 bit D to A . . . . . . . . . . . . . . . .
resolution . . . . . . . . . . . . . . . . . . . . . . . . .
counter type A to D converter . . . . . . . . . . . . .
10 bit A to D converter . . . . . . . . . . . . . . . . .
structure of binary address . . . . . . . . . . . . . . .
address . . . . . . . . . . . . . . . . . . . . . . . . . .
Exa 14.1 diode forward or reverse . . . . . . . . . . . . . . . . .
Exa 14.2 Diode current . . . . . . . . . . . . . . . . . . . . . . .
Exa 14.3 current in the given circuit . . . . . . . . . . . . . . .
Exa 14.4 n channel MOSFET inverter . . . . . . . . . . . . . .
Exa 15.1 Timing of a six digit display . . . . . . . . . . . . . . .
Exa 15.4 Basic frequency counter . . . . . . . . . . . . . . . . .
Exa 15.5 4 decimal digit frequency counter . . . . . . . . . . . .
Exa 15.6 instrument to measure time period . . . . . . . . . . .
Exa 15.9 ADC0804 . . . . . . . . . . . . . . . . . . . . . . . . .
Exa 15.10 ADC3511 . . . . . . . . . . . . . . . . . . . . . . . . .
Exa 15.11 ADC3511 . . . . . . . . . . . . . . . . . . . . . . . . .
Exa 15.12 ADD3501 . . . . . . . . . . . . . . . . . . . . . . . . .
Exa 16.1 size of PC IR ACC MAR MDR . . . . . . . . . . . . .
Exa 16.6 Number of clock cycles needed to execute a program .
AP 1
3-variable kmap(abx) . . . . . . . . . . . . . . . . . .
AP 2
returns number of 1s in a matrix . . . . . . . . . . . .
AP 3
returns number of 0s in a matrix . . . . . . . . . . . .
AP 4
4-variable kmap with dont cares . . . . . . . . . . . .
AP 5
number of zeros and ones . . . . . . . . . . . . . . . .
AP 6
3-variable kmap(a) . . . . . . . . . . . . . . . . . . . .
AP 7
2-variable kmap . . . . . . . . . . . . . . . . . . . . .
8

142
143
144
145
146
146
147
147
148
148
149
149
149
150
151
151
153
154
154
155
156
157
157
158
158
159
160
160
161
162
163
167
168
168
176
176
181

AP
AP
AP
AP

8
9
10
11

3-variable
4-variable
4-variable
4-variable

kmap . . . .
kmap(sx1x2)
kmap . . . .
kmap pos . .

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

.
.
.
.

182
187
194
202

List of Figures
2.1
2.2
2.3
2.4

7404 waveform
7404 waveform
timing diagram
timing diagram

. . . . . . . . . .
. . . . . . . . . .
for NOR NOR . .
for NAND NAND

.
.
.
.

8
10
15
20

3.1

Dynamic hard . . . . . . . . . . . . . . . . . . . . . . . . . .

38

7.1
7.2
7.3
7.4
7.5

Schmitt trigger . . . . .
monostable multivibrator
monostable multivibrator
74123 . . . . . . . . . . .
74123 . . . . . . . . . . .

.
.
.
.
.

92
97
98
100
101

8.1

JK master slave . . . . . . . . . . . . . . . . . . . . . . . . .

108

9.1

116

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

. . . .
. . . .
. . . .
circuit

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.

.
.
.
.

.
.
.
.
.

## 10.1 Output waveforms for a 7493A connected as a mod 16 counter 123

10.2 mod 6 counter . . . . . . . . . . . . . . . . . . . . . . . . . . 129

10

Chapter 1
Digital Principles

## Scilab code Exa 1.1 Finding duty cycle

// Example 1 . 1
clc ;
clear ;
f = 5 * 10^6 ; // g i v e n
T =1/ f ; // c a c u l a t i n g t h e t i m e p e r i o d .
H = 0.05 * 10^ -6 / T ;
printf ( Time p e r i o d o f t h e waveform i s T = %f u s \n ,
T ) ; // d i s p l a y i n g r e s u l t s
8 printf ( Duty c y c l e H = %f %% ,H *100) ;

1
2
3
4
5
6
7

## Scilab code Exa 1.2 Maximum decimal count for a counter

1 // Example 1 . 2
2 clc ;
3 clear ;
4 n =8; // g i v e n no o f f l i p f l o p s
5 max_count = 2^ n -1 ;
6 printf ( Maximum c o u n t = %d , max_count ) ;

11

Chapter 2
Digital Logic

## Scilab code Exa 2.1 7404 waveform

1 // e x m a p l e 2 . 1
2 // 7 4 0 4
3 clc
4 close
5 clear
6 // f r q= i n p u t ( E n t e r t h e s q u a r e wave f r e q u e n c y i n KHz
7
8
9
10
11
12
13
14
15
16
17

: ) ;
frq =1 ; // f r e q u e n c y i n KHz
t =(1/ frq ) *100;
t = round ( t )
for r =1: t *10
inputc ( r ) =0;
outputc ( r ) =0;
end
p =1;
while p < t *10
to plot the curve
if p ==1 | modulo (p , t ) ==0 then
for k =1: t /2
12

// making a r r a y s

## Figure 2.1: 7404 waveform

13

18
inputc ( p + k ) =0;
19
outputc ( p + k ) =1;
20
end
21
p = p + t /2;
22
else
23
inputc ( p ) =1;
24
outputc ( p ) =0;
25
p = p +1;
26
end
27 end
28 y =[3 3];
29 subplot (2 ,1 ,1)
// p l o t i n g t h e c u r v e s
30 title ( i n p u t a t p i n 1 )
31 xlabel ( Time X 10 5 s e c o n d s ) ;
32 ylabel ( Magnitude )
33 plot ( inputc )
34 plot ( y )
35 subplot (2 ,1 ,2)
36 title ( o u t p u t a t p i n 2 )
37 xlabel ( Time X 10 5 s e c o n d s ) ;
38 ylabel ( Magnitude )
39 plot ( outputc )
40 plot ( y )

## Scilab code Exa 2.2 7404 waveform

1 // e x m a p l e 2 . 2
2 // 7 4 0 4
3 clc
4 close
5 clear
6 // f r q= i n p u t ( E n t e r t h e s q u a r e wave f r e q u e n c y i n KHz

: ) ;
14

15

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

## frq =0.5 ; //KHz

t =(1/ frq ) *100;
t = round ( t )
for r =1: t *10
inputc ( r ) =0;
outputc ( r ) =0;
end
p =1;
while p < t *10
making a r r a y s t p l o t t h e c u r v e
if p ==1 | modulo (p , t ) ==0 then
for k =1: t /2
inputc ( p + k ) =0;
outputc ( p + k ) =1;
end
p = p + t /2;
else
inputc ( p ) =1;
outputc ( p ) =0;
p = p +1;
end
end
y =[3 3];
subplot (2 ,1 ,1)
// p l o t i n g t h e g r a p h s
title ( i n p u t a t p i n 1 )
xlabel ( Time X 10 5 s e c o n d s ) ;
ylabel ( Magnitude )
plot ( inputc )
plot ( y )
subplot (2 ,1 ,2)
title ( o u t p u t a t p i n 2 )
xlabel ( Time X 10 5 s e c o n d s ) ;
ylabel ( Magnitude )
plot ( outputc )
plot ( y )

16

//

## Scilab code Exa 2.3 truth table for given figure

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

// exmple 2 . 3
clc
clear
close
a =[0 0 1 1];
b =[0 1 0 1];
for i =1:4
r ( i ) = bitor ( bitcmp ( a ( i ) ,1) , bitcmp ( b ( i ) ,1) )
// g i v e n e x p r e s s i o n
end
disp (
A
B
Y)
for i = 1 : 4
Y (i ,1) = a ( i ) ;
Y (i ,2) = b ( i ) ;
Y (i ,3) = r ( i ) ;
end
disp ( Y ) ; // d i s p l a y i n g t r u t h t a b l e
disp ( 1 r e p r e s e n t s a HIGH(H) and 0
r e p r e s e n t s a LOW( L ) )

## Scilab code Exa 2.4 truth table for given figure

1
2
3
4
5
6
7

// exmple 2 . 4
clear
clc
a =[0 0 1 1];
b =[0 1 0 1];
for i =1:4
r ( i ) = bitand ( bitcmp ( a ( i ) ,1) , bitcmp ( b ( i ) ,1) ) //
given expression
17

8 end
9 disp (
A
B
Y)
10 for i = 1 : 4
11
Y (i ,1) = a ( i ) ;
12
Y (i ,2) = b ( i ) ;
13
Y (i ,3) = r ( i ) ;
14
end
15 disp ( Y ) ;
// d i s p l a y i n g t r u t h t a b l e
16 disp ( 1 r e p r e s e n t s a HIGH(H) and

r e p r e s e n t s a LOW( L ) )

Scilab code Exa 2.9 proving two circuits are logically equal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

// Example 2 . 9
clc
clear
close
a =[0 0 0 0 0 0 0 0 1 1 1 1
b =[0 0 0 0 1 1 1 1 0 0 0 0
c =[0 0 1 1 0 0 1 1 0 0 1 1
d =[0 1 0 1 0 1 0 1 0 1 0 1
for i =1:16
// f i n d i n g
x = bitor ( a ( i ) ,b ( i ) ) ;
y = bitor ( c ( i ) ,d ( i ) ) ;
r ( i ) = bitand (x , y ) ;
x1 = bitcmp (x ,1) ;
y1 = bitcmp (y ,1) ;
z = bitor ( x1 , y1 ) ;
r1 ( i ) = bitcmp (z ,1) ;
end
disp (
Y
Y1 ) ;
for i = 1 : 16
Y (i ,1) = r1 ( i ) ;
Y (i ,2) = r ( i ) ;
end

1
1
0
0
Y

18

1 1
1 1
0 1
1 0
for

1];
1];
1];
1];
a l l 16 c a s e s

23
24

disp ( Y ) ; // d i s p l a y i n g r e s u l t
disp ( Both a r e l o g i c a l l y e q u i v a l e n t ) ;

Scilab code Exa 2.10 truth table for NOR NOR circuit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

// exmple 2 . 1 0
clc
clear
a =[0 0 0 0 0 0 0 0 1 1 1 1 1
b =[0 0 0 0 1 1 1 1 0 0 0 0 1
c =[0 0 1 1 0 0 1 1 0 0 1 1 0
d =[0 1 0 1 0 1 0 1 0 1 0 1 0
for i =1:16
x = bitor ( a ( i ) ,b ( i ) ) ;
y = bitor ( c ( i ) ,d ( i ) ) ;
r ( i ) = bitand (x , y ) ;
end
disp ( Truth t a b l e : ) ;
disp (
A
B
C
D
for i = 1 : 16 // d i s p l a y i n g
Y (i ,1) = a ( i ) ;
Y (i ,2) = b ( i ) ;
Y (i ,3) = c ( i ) ;
Y (i ,4) = d ( i ) ;
Y (i ,5) = r ( i ) ;
end
disp ( Y ) ;

1
1
0
1

1
1
1
0

1];
1];
1];
1];

Y)
truth table

## Scilab code Exa 2.11 timing diagram for NOR NOR

1

// exmple 2 . 1 1
19

## Figure 2.3: timing diagram for NOR NOR

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

clc
clear
close
a =[0 0 0 0 0 0 0 0 1 1 1 1 1 1 1
b =[0 0 0 0 1 1 1 1 0 0 0 0 1 1 1
c =[0 0 1 1 0 0 1 1 0 0 1 1 0 0 1
d =[0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
for i =1:16
x = bitor ( a ( i ) ,b ( i ) ) ;
y = bitor ( c ( i ) ,d ( i ) ) ;
r ( i ) = bitand (x , y ) ;
end
Y=r
ap =1;
bp =1;
cp =1;
dp =1; Yp =1;
for i =1:16
// Making a r r a y t o
20

1];
1];
1];
1];

## plot the timing

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

diagram
if a ( i ) ==1 then
for o =1:100
a1 ( ap ) =1;
ap = ap +1;
end
else
for o =1:100
a1 ( ap ) =0;
ap = ap +1;
end
end
if b ( i ) ==1 then
for o =1:100
b1 ( bp ) =1;
bp = bp +1;
end
else
for o =1:100
b1 ( bp ) =0;
bp = bp +1;
end
end
if c ( i ) ==1 then
for o =1:100
c1 ( cp ) =1;
cp = cp +1;
end
else
for o =1:100
c1 ( cp ) =0;
cp = cp +1;
end
end
if d ( i ) ==1 then
for o =1:100
d1 ( dp ) =1;
21

57
dp = dp +1;
58
end
59
else
60
for o =1:100
61
d1 ( dp ) =0;
62
dp = dp +1;
63
end
64
end
65 if Y ( i ) ==1 then
66
for o =1:100
67
Y1 ( Yp ) =1;
68
Yp = Yp +1;
69
end
70
else
71
for o =1:100
72
Y1 ( Yp ) =0;
73
Yp = Yp +1;
74
end
75
end
76 end
77 z =[2 2];
78 subplot (5 ,1 ,1) ;
// p l o t t i n g
79 title ( Timing Diagrm ) ;
80 plot ( z ) ;
81 plot ( a1 ) ;
82 ylabel ( A ) ;
83 subplot (5 ,1 ,2) ;
84 plot ( z ) ;
85 ylabel ( B ) ;
86 plot ( b1 ) ;
87 subplot (5 ,1 ,3) ;
88 plot ( z ) ;
89 ylabel ( C ) ;
90 plot ( c1 ) ;
91 subplot (5 ,1 ,4) ;
92 plot ( z ) ;
93 ylabel ( D ) ;
94 plot ( d1 ) ;

22

timing diagram

95
96
97
98
99

subplot (5 ,1 ,5) ;
plot ( z ) ;
ylabel ( Y ) ;
xlabel ( Time i n m i l l i s e c o n d s ) ;
plot ( Y1 ) ;

Scilab code Exa 2.12 proving two circuits are logically equal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

// Example 2 . 1 2
clc
clear
a =[0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1];
b =[0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1];
c =[0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1];
d =[0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1];
for i =1:16 // f i n d i n g Y and y1 f o r a l l p o s s i b l e i n p t
cases
x = bitand ( a ( i ) ,b ( i ) ) ;
y = bitand ( c ( i ) ,d ( i ) ) ;
r ( i ) = bitor (x , y ) ;
x1 = bitcmp (x ,1) ;
y1 = bitcmp (y ,1) ;
z = bitand ( x1 , y1 ) ;
r1 ( i ) = bitcmp (z ,1) ;
end
disp (
Y
Y1 ) ;
for i = 1 : 16 // d i s p l a y i n g r e s u l t
Y (i ,1) = r ( i ) ;
Y (i ,2) = r1 ( i ) ;
end
disp ( Y ) ;
disp ( Both a r e l o g i c a l l y e q u i v a l e n t ) ;

23

Scilab code Exa 2.13 truth table for NAND NAND circuit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

// exmple 2 . 1 3
//NAND NAND
clc
clear
close
a =[0 0 0 0 0 0 0 0 1 1 1 1 1 1
b =[0 0 0 0 1 1 1 1 0 0 0 0 1 1
c =[0 0 1 1 0 0 1 1 0 0 1 1 0 0
d =[0 1 0 1 0 1 0 1 0 1 0 1 0 1
for i =1:16
x = bitand ( a ( i ) ,b ( i ) ) ;
y = bitand ( c ( i ) ,d ( i ) ) ;
r ( i ) = bitor (x , y ) ;
end
disp ( Truth t a b l e : ) ;
disp (
A
B
C
D
for i = 1 : 16
// d i s p l a y i n g
Y (i ,1) = a ( i ) ;
Y (i ,2) = b ( i ) ;
Y (i ,3) = c ( i ) ;
Y (i ,4) = d ( i ) ;
Y (i ,5) = r ( i ) ;
end
disp ( Y ) ;

1
1
1
0

1];
1];
1];
1];

Y)
the truth t a b l e

Scilab code Exa 2.14 timing diagram for NAND NAND circuit
1 // exmple 2 . 1 4
2 clc
3 clear
4 close

24

## Figure 2.4: timing diagram for NAND NAND circuit

25

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

a =[0 0 0 0 0 0 0 0 1 1 1 1 1 1 1
b =[0 0 0 0 1 1 1 1 0 0 0 0 1 1 1
c =[0 0 1 1 0 0 1 1 0 0 1 1 0 0 1
d =[0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
for i =1:16
x = bitand ( a ( i ) ,b ( i ) ) ;
y = bitand ( c ( i ) ,d ( i ) ) ;
r ( i ) = bitor (x , y ) ;
end
Y=r
ap =1;
bp =1;
cp =1;
dp =1; Yp =1;
for i =1:16
// Making a r r a y s t o
diagram
if a ( i ) ==1 then
for o =1:100
a1 ( ap ) =1;
ap = ap +1;
end
else
for o =1:100
a1 ( ap ) =0;
ap = ap +1;
end

20
21
22
23
24
25
26
27
28
29
30
31 end
32 if b ( i ) ==1 then
33
for o =1:100
34
b1 ( bp ) =1;
35
bp = bp +1;
36
// z ( bp ) =3
37
end
38
else
39
for o =1:100
40
b1 ( bp ) =0;
41
bp = bp +1;

26

1];
1];
1];
1];

## plot the timing

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

// z ( bp ) =3
end
end
if c ( i ) ==1 then
for o =1:100
c1 ( cp ) =1;
cp = cp +1;
end
else
for o =1:100
c1 ( cp ) =0;
cp = cp +1;
end
end
if d ( i ) ==1 then
for o =1:100
d1 ( dp ) =1;
dp = dp +1;
end
else
for o =1:100
d1 ( dp ) =0;
dp = dp +1;
end
end
if Y ( i ) ==1 then
for o =1:100
Y1 ( Yp ) =1;
Yp = Yp +1;
end
else
for o =1:100
Y1 ( Yp ) =0;
Yp = Yp +1;
end
27

80
81
end
82
83 end
84 z =[2 2];
85 subplot (5 ,1 ,1) ;
// p l o t t i n g t i m i n g d i a g r a m
86 title ( Timing Diagrm ) ;
87 plot ( z ) ;
88 plot ( a1 ) ;
89 ylabel ( A ) ;
90 subplot (5 ,1 ,2) ;
91 plot ( z ) ;
92 ylabel ( B ) ;
93 plot ( b1 ) ;
94 subplot (5 ,1 ,3) ;
95 plot ( z ) ;
96 ylabel ( C ) ;
97 plot ( c1 ) ;
98 subplot (5 ,1 ,4) ;
99 plot ( z ) ;
100 ylabel ( D ) ;
101 plot ( d1 ) ;
102 subplot (5 ,1 ,5) ;
103 plot ( z ) ;
104 ylabel ( Y ) ;
105 xlabel ( Time i n m i l l i s e c o n d s ) ;
106 plot ( Y1 ) ;

## Scilab code Exa 2.15 detecting all bits low in a register

1 // e x a m p l e 2 . 1 5
2 clc
3 clear
4 s =0; // s from t h e r e g i s t e r
5 s (1) = input ( E n t e r t h e v a l u e a t S0 : )

28

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

s (2) = input ( E n t e r t h e v a l u e a t
s (3) = input ( E n t e r t h e v a l u e a t
s (4) = input ( E n t e r t h e v a l u e a t
s (5) = input ( E n t e r t h e v a l u e a t
s (6) = input ( E n t e r t h e v a l u e a t
s (7) = input ( E n t e r t h e v a l u e a t
s (8) = input ( E n t e r t h e v a l u e a t
count =0;
for i =1 :8 // l o o p t o d e t e c t a
if s ( i ) ==1 then
disp ( ZERO i s LOW ) ;
break ;
end
count = count +1;
end
if count ==8 then
disp ( ZERO i s HIGH ) ;
end ;

29

S1
S2
S3
S4
S5
S6
S7
1

:
:
:
:
:
:
:

)
)
)
)
)
)
)

Chapter 3
Combinational Logic Circuits

## Scilab code Exa 3.1 Boolean Algebra

1 // e x a m p l e 3 . 1
2 clc ;
3 clear ;
4 disp ( we can m i n i m i z e t h e g i v e n e q u a t i o n a s : ) ;
5 disp ( Y = AB + AB = A(B + B) ) ;
6 disp ( T h e r e f o r e ,
Y = A( 1 ) = A ) ;
7 disp ( t h i s s a y s t h a t o u t p u t Y e q u a l s t o A, s o a l l we

hve t o do i s c o n n e c t a w i r e b e t w e e n i n p u t A and
output Y. );

## Scilab code Exa 3.2 Boolean Algebra

1 // e x a m p l e 3 . 2
2
3 clc ;
4 clear ;
5 disp ( M u l t i p l t h e

f a c t o r s of the f o r e g o i n g equation

to g e t );
30

disp ( Y = A A + A B + BA +BB ) ;
disp ( i t becomes ,
Y = A B + AB + B ) ;
disp ( We can f a c t o r t h e f o r e g o o i n g e q u a t i o n a s
f o l l o w s : );
9 disp ( Y = B(A + A) + B = B + B = B ) ;
10 disp ( t h i s s a y s t h a t o u t p u t Y e q u a l s t o B , s o a l l we
have t o do i s c o n n e c t a w i r e b e t w e e n i n p u t B and
output Y. );
6
7
8

## Scilab code Exa 3.3 Testing a circuit using logic clip

1 // e x a m p l e 3 . 3
2 clc ;
3 clear ;
4 // d i s p ( E n t e r t h e i n p u t s f o r AND g a t e ) ;
5 // f o r i =1:7
6 // p r i n t f ( E n t e r w e t h e r t h e l e d %d i s on o r
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

o f f (1 or

0) : , i ) ;
// a ( i )=i n p u t ( ) ;
// end ;
// d i s p ( E n t e r t h e i n p u t s f o r OR g a t e )
// f o r i =1:7
// p r i n t f ( E n t e r w e t h e r t h e l e d %d i s on o r o f f ( 1 o r
0) : , i ) ;
// b ( i )=i n p u t ( ) ;
// end ;
a = [0 1 0 1 1 0 0]; // l i g h t s on a l o g i c c l i p
b = [0 0 0 0 1 1 0];
k = bitand ( a (1) ,a (2) ) ;
k2 = bitand ( a (4) ,a (5) ) ;
k3 = bitor ( b (1) ,b (2) ) ;
k4 = bitor ( b (4) ,b (5) ) ;
if a (3) ~= k then // c h e c k i n g which g a t e i s f a u l t y
disp ( The f i r s t AND g a t e i s d i f f e c t i v e ) ;
elseif a (6) ~= k2 then
31

23
disp ( The s e c o n d AND g a t e i s d i f f e c t i v e ) ;
24
elseif b (3) ~= k3 then
25
disp ( The f i r s t OR g a t e i s d i f f e c t i v e ) ;
26 elseif
b (6) ~= k4 then
27
disp ( The s e c o n d OR g a t e i s d i f f e c t i v e ) ;
28
else
29
disp ( A l l t h e g a t e s a r e w o r k i n g c o r r e c t l y ) ;
30 end

## Scilab code Exa 3.4 Sum of Products

1 // e x a m p l e 3 . 4
2 clc ;
3 clear ;
4 disp ( Given t h e t r u t h
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

## t a b l e has high output f o r

f o l l o w i n g c o n d i t o n s : );
a =[0 0 0 ; 0 1 0 ; 1 0 0 ; 1 1 0 ] // g i v e n i p u t
c o n d i t i o n s f o r which o u t p u t i s h i g h
disp ( a )
for ( i =1:4)
if a (i ,1) ==1 then
b (i ,1) = A
else
b (i ,1) = A
end
if a (i ,2) ==1 then
b (i ,2) = B
else
b (i ,2) = B
end
if a (i ,3) ==1 then
b (i ,3) = C
else
b (i ,3) = C
end
32

23 end
24 disp ( When you OR t h e s e p r o d u c t s you g e t

: ) //
d i s p l a y i n g sum o f p r o d u c t s
25 x = strcat ([ b (1 ,1) b (1 ,2) b (1 ,3) + b (2 ,1) b (2 ,2) b
(2 ,3) + b (3 ,1) b (3 ,2) b (3 ,3) + b (4 ,1) b
(4 ,2) b (4 ,3) ]) ;
26 disp ( x )

## Scilab code Exa 3.5 Boolean Algebra

1 // e x a m p l e 3 . 5
2 clc ;
3 clear ;
4 disp ( The b o o l e a n e q u a t i o n i s : ) ;
5 disp ( Y = A B C + A BC + AB C + ABC ) ;
6 disp ( S i n c e C i s common t o e a c h term , f a c t o r a s
7
8
9
10
11
12
13

f o l l o w s : );
disp ( Y = (A B + A B + AB AB)C ) ;
disp ( Again , f a c t o r t o g e t : ) ;
disp ( Y = [ A (B + B) + A(B + B) ] C ) ;
disp ( Now , s i m p l i f y t h e f o r e g o i n g a s f o l l o w s : ) ;
disp ( Y=[A ( 1 ) + A( 1 ) ] C = (A + A)C ) ;
disp ( o r
Y= C ) ;
disp ( T h i s f i n a l e q u a t i o n means t h a t you don t e v e n
n e e d a l o g i c c i r c u i t . A l l you n e e d i s a w i r e
c o n n e c t i n g i np u t C to output Y. );
check Appendix AP 10 for dependency:
kmap.sci
check Appendix AP 2 for dependency:
noof.sci

33

## Scilab code Exa 3.6 Gives a simplified Boolean equation

// e x a m p l e 3 . 6
// t h i s program n e e d s kmap . s c i and n o o f . s c i
clc
Y =[7 9 10 11 12 13 14 15]; // g i v e n l o g i c e q u a t i o n
k =[0 0 0 0;0 0 1 0 ; 1 1 1 1; 0 1 1 1]; //
m i n i m i z i n g i t u s i n g 4 v a r i a b l e kmap
6
disp ( The m i n i m a l e x p r e s s i o n o f Y from t h e
f o l l o w i n g Kmap i s : ) ;
7
kmap ( k ) ; // c a l l i n g t h e Kmap f u n c t i o n

1
2
3
4
5

## check Appendix AP 10 for dependency:

kmap.sci
check Appendix AP 2 for dependency:
noof.sci

Scilab code Exa 3.7 simplest logic for given Truth table
1 // e x a m p l e 3 . 7
2 // t h i s program n e e d s kmap . s c i and n o o f . s c i
3 clc ;
4 disp ( The k a n a u r g h map f o r g i v e n t r u t h t a b l e
5
6
7
8
9
10

11
12

w i l l be

: );
disp (
C D C D CD CD ) ; // d i s p l a y i n g
t h e g i v e n kmap
disp ( A B
1
0
0
0 );
disp ( A B
0
0
0
0 );
disp ( AB
x
x
x
x );
disp ( AB
0
0
x
x );
disp ( The t r u t h t a b l e h a s o u t p u t one o n l y f o r t h e
i n p u t c o n d i t i o n 0 0 0 0 . The c o r r e s p o n d i n g
f u n d a m e n t a l p r o d u c t i s A B C D ) ;
k =[1 0 0 0 ; 0 0 0 0; 0 0 0 0;0 0 0 0];
kmap ( k ) ; // c a l l i n g t h e Kmap f u n c t i o n
34

## check Appendix AP 10 for dependency:

kmap.sci
check Appendix AP 2 for dependency:
noof.sci

Scilab code Exa 3.8 simplest logic for given logic equation
1 // e x a m p l e 3 . 8
2 // t h i s program n e e d s kmap . s c i and n o o f . s c i
3 clc ;
4 disp (
C D C D CD CD ) ; // d i s p l a y i n g
5
6
7
8
9
10

11
12

t h e g i v e n kmap
disp ( A B
0
0
0
0 );
disp ( A B
0
0
1
0 );
disp ( AB
x
x
x
x );
disp ( AB
0
0
x
x );
k =[0 0 0 0;0 0 1 0;0 0 1 0;0 0 0 0];
disp ( I n a Karnaugh map i f don t c a r e c o n d i t i o n
e x i t s , we may c o n s i d e r them a s o n e s i f t h a t g i v e s
a l a r g e r group s i z e . );
disp ( The m i n i m a l e x p r e s s i o n from t h e g i v e n kmap i s
);
kmap ( k ) ; // c a l l i n g t h e kamp f u n c t i o n

## Scilab code Exa 3.9 Product of sums

1 // e x a m p l e 3 . 4
2 clc ;
3 clear ;

35

disp ( Given t h e t r u t h t a b l e h a s h i g h o u t p u t f o r
f o l l o w i n g c o n d i t o n s : );
5 a =[0 0 0 ; 0 0 1 ; 0 1 0 ] // g i v e n t r u t h t a b l e
6 disp ( a )
4

7
8 for ( i =1:3) // f i n d i n g t h e t e r m s i n p o s
9
if a (i ,1) ==0 then
10
b (i ,1) = A
11
else
12
b (i ,1) = A
13
end
14
if a (i ,2) ==0 then
15
b (i ,2) = B
16
else
17
b (i ,2) = B
18
end
19
if a (i ,3) ==0 then
20
b (i ,3) = C
21
else
22
b (i ,3) = C
23
end
24 end
25 disp ( b )
26 disp ( The p r o d u c t o f sums e q u a t i o n i s
: ) //

d i s p l a y i n g t h e POS
27 x = strcat ([ ( b (1 ,1) + b (1 ,2) + b (1 ,3) )

## ( b (2 ,1) + b (2 ,2) + b (2 ,3) ) ( b

(3 ,1) + b (3 ,2) + b (3 ,3) ) ]) ;
28 disp ( x )
check Appendix AP 10 for dependency:
kmap.sci
check Appendix AP 2 for dependency:
noof.sci

36

## Scilab code Exa 3.10 sop for the karnaugh map

1 // e x a m p l e 3 . 1 0
2 // t h i s program n e e d s kmappos . s c i and n o o f . s c i
3
4 k =[0 0 0 0;0 0 0 1; 1 1 1 1;1 1 1 1];
5 disp ( The m i n i m a l e x p r e s s i o n o f Y from t h e f o l l o w i n g
6
7
8
9

Kmap i s : ) ;
kmap ( k ) ;
d i s p ( A f t e r c o m p l i m e n t i n g and s i m p l i f y i n g t h e
Krarnugh map we g e t Y = : ) ;
k =[1 1 1 1 ; 1 1 1 0 ; 0 0 0 0 ; 0 0 0 0 ] ;
kmap ( k ) ; // c a l l i n g t h e Kmap f u n c t i o n
check Appendix AP 11 for dependency:
kmappos.sci
check Appendix AP 2 for dependency:
noof.sci

## Scilab code Exa 3.11 POS form of karnaugh map

1 // e x a m p l e 3 . 1 1
2 // t h i s program n e e d s kmappos . s c i and n o o f . s c i
3
4 clc
5 disp ( The g i v e n kmap i s ) ; // d i s p l a y i n g t h e g i v e n
6
7
8
9
10
11

kmap
disp (
C D C D CD CD ) ;
disp ( A B
0
0
0
0 );
disp ( A B
0
0
0
1 );
disp ( AB
1
1
1
1 );
disp ( AB
1
1
1
1 );
disp ( The s i m p l e s t POS form o f f o l l o w i n g Kmap i s : )
;
37

12
13 k= [ 0 0 0 0 ; 0 0 0 1 ; 1 1 1 1 ; 1 1 1 1 ] ;
14 kmappos ( k ) ; / / c a l l i n g t h e Kmappos f u n c t i o n

## check Appendix AP 11 for dependency:

kmappos.sci
check Appendix AP 2 for dependency:
noof.sci

## Scilab code Exa 3.12 POS form of karnaugh map

1 // e x a m p l e 3 . 1 2
2 clc
3 disp ( The g i v e n kmap i s
4
5
6
7
8
9

10

) ; // d i s p l a y i n g t h e g i v e n
kmap
disp (
C D C D CD CD ) ;
disp ( A B
0
0
1
0 );
disp ( A B
0
0
1
1 );
disp ( AB
x
x
x
1 );
disp ( AB
x
x
x
0 );
disp ( I n a Karnaugh map i f don t c a r e c o n d i t i o n
e x i t s , we may c o n s i d e r them a s z e r o s i f t h a t
g i v e s a l a r g e r group s i z e . );
disp ( The s i m p l e s t POS form o f f o l l o w i n g Kmap i s : )
;

11
12 k= [ 0 0 1 0 ; 0 0 1 1 ; 0 0 1 1 ; 0 0 1 0 ] ;
13 kmappos ( k ) ; // c a l l i n g t h e Kmappos f u n c t i o n

## Scilab code Exa 3.13 Quine Mc clusky method

38

1 // e x a m p l e 3 . 1
2 clc ;
3 clear ;
4 a =[0 0 0 0 1 1 1 1;0 0 1 1 0 0 1 1;0 1 0 1 0 1 0 1];
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

// from t h e t r u t h t a b l e g i v e n
y =[0 0 1 0 0 0 1 1];
j =1;
for i =1:8 // f i n d i n g f o r which i n p u t c o n d i t i o n s t h e
output i s high
if y ( i ) == 1 then
x (j ,:) = [ a (1 , i ) a (2 , i ) a (3 , i ) ];
j = j +1;
end
end
for i =1: j -1; // f i n d i n g t h e f i r s t s t a g e
f ( i ) =0;
c =0;
for m =3: -1:1
f ( i ) = f ( i ) + x (i , m ) *(2^ c ) ;
c = c +1;
end
end
disp ( s t a g e 1 ) ; // d i s p l a y i n g f i r s t s t a g e
x (: ,4) = f ;
disp (
A
B
C );
disp ( x )
count = zeros (j -2 ,j -2)
pos = count ;
for i =1: j -2 // f o r s e c o n d s t a g e c o m p a r i n g w i t h e a c h
other
for k =1: j -i -1
for m =1:3
if x (i , m ) == x ( i +k , m ) then
count (i , k ) = count (i , k ) +1;
else
pos (i , k ) = m
end
end
39

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

end
end
r =1;
for i =1: j -2 // making a l i s t o f s e c o n d s t a g e
elements
for m =1: j -2
if count (i , m ) ==2 then
posi ( r ) = pos (i , m ) ;
sest (r ,1) = x (i ,4) ;
sest (r ,2) = x ( i +m ,4) ;
r = r +1;
end
end
end
disp ( s t a g e 2 ) ; // d i s p l a y i n g s e c o n d s t a g e
disp ( sest ) ;
o = size ( sest ) ;
fin (1) = sest (1 ,1) ;
fin (2) = sest (1 ,2) ;
p =3;
for i =2: o (1 ,1) // r e m o v i n g r e d u n d a n c y i n s e c o n d s t a g e
t =0;
ts =0;
for w =1: p -1
if fin ( w ) == sest (i ,1) then
t =30;
end ;
if fin ( w ) == sest (i ,2) then
ts =40;
end
end
if t ==0 then
fin ( p ) = sest (i ,1) ;
finn (p -2) = i ;
p = p +1;
end
if ts ==0 then
fin ( p ) = sest (i ,2)
40

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

finn (p -2) = i ;
p = p +1;
end
end
ppp = size ( finn ) // s e l e c t i n g t h e p r i m e i m p l i c a n t s
l =1
fina ( l ) = finn ( l ) ;
for i =2: ppp (1 ,1)
q =0;
for b =1: l
if fina ( b ) == finn ( i ) then
q =89 ;
end
end
if q ==0 then
fina ( l +1) = finn ( i ) ;
l = l +1;
q =0;
end
end
kkk = size ( fina ) ;
i =1;
jj =0;
bi (1) = ;
x(i)
po =1;
for k =1: kkk (1 ,1) +1
for p =1:3
// a p p e n d i n g a s t r i n g t o make t h e
expression
if p ~= posi ( i ) then
if p == 1 & x (i , p ) ==1
then
bi ( po ) = strcat ([ bi ( po ) A ]) ;
elseif p == 1 & x (i , p ) ==0 then
bi ( po ) = strcat ([ bi ( po ) A ]) ;
end
if p == 2 & x (i , p ) ==1
then
bi ( po ) = strcat ([ bi ( po ) B ]) ;
elseif p == 2 & x (i , p ) ==0 then
41

110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

bi ( po ) = strcat ([ bi ( po ) B ]) ;
end
if p == 3 & x (i , p ) ==1
then
bi ( po ) = strcat ([ bi ( po ) C ]) ;
elseif p == 3 & x (i , p ) ==0 then
bi ( po ) = strcat ([ bi ( po ) C ]) ;
end
end
end
jj = jj +1;
if jj <= kkk (1 ,1) then
i = fina ( jj ) ;
bi ( po ) = strcat ([ bi ( po ) + ]) ;
end
end ;
disp ( The m i n i m i s e d e x p r e s s i o n i s
disp ( bi ) ;

);

## Scilab code Exa 3.14 Dynamic hard

1
2
3
4
5
6
7
8
9
10
11
12
13

// e x a m p l e 3 . 9
clc ;
clear ;
close ;
c = [1 1 0 0 0 0 0]; // g i v e n v a l u e s
a = [1 1 1 1 1 1 1];
b = [1 1 1 1 1 1 1] ;
for i =1:7
y1 ( i ) =0
y2 ( i ) =1
y3 ( i ) =0
y4 ( i ) =1
y ( i ) =0
42

## Figure 3.1: Dynamic hard

43

14 end
15 for ( i =1: 7) // f i n d i n g t h e Y v a l u e s
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

f o r next c l o c k
periods
y1 ( i +1) = bitcmp ( c ( i ) ,1) ;
y2 ( i +1) = bitand ( a ( i ) ,c ( i ) ) ;
end ;
for i =1: 5
y3 ( i +2) = bitand ( y1 ( i +1) ,b ( i ) ) ;
end ;
for i =1:4
y4 ( i +3) = bitor ( y3 ( i +2) , y2 ( i +2) ) ;
end ;
for i =1:3
y ( i +4)
= bitand ( y4 ( i +3) , y1 ( i +3) ) ;
end ;
y11p =1;
y22p =1;
y33p =1;
y44p =1;
cp =1;
yf1p =1;
for i =1:7 // p l o t i n g a l l o f them i n t o g r a p h
if y1 ( i ) ==1 then
for o =1:100
y11 ( y11p ) =1;
y11p = y11p +1;
end
else
for o =1:100
y11 ( y11p ) =0;
y11p = y11p +1;
end
end
if y2 ( i ) ==1 then
for o =1:100
y21 ( y22p ) =1;
y22p = y22p +1;
// z ( bp ) =3
44

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

end
else
for o =1:100
y21 ( y22p ) =0;
y22p = y22p +1;
// z ( bp ) =3
end
end
if y3 ( i ) ==1 then
for o =1:100
y31 ( y33p ) =1;
y33p = y33p +1;
// z ( bp ) =3
end
else
for o =1:100
y31 ( y33p ) =0;
y33p = y33p +1;
// z ( bp ) =3
end
end
if y4 ( i ) ==1 then
for o =1:100
y41 ( y44p ) =1;
y44p = y44p +1;
// z ( bp ) =3
end
else
for o =1:100
y41 ( y44p ) =0;
y44p = y44p +1;
// z ( bp ) =3
end
end
if c ( i ) ==1 then
for o =1:100
c1 ( cp ) =1;
45

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

cp = cp +1;
end
else
for o =1:100
c1 ( cp ) =0;
cp = cp +1;
end
end
if y ( i ) ==1 then
for o =1:100
yf1 ( yf1p ) =1;
yf1p = yf1p +1;
end
else
for o =1:100
yf1 ( yf1p ) =0;
yf1p = yf1p +1;
end
end
end
z =[2 2];
// p l o t i n g t h e r e s u l t s
subplot (6 ,1 ,1) ;
title ( Timing Diagrm ) ;
plot ( c1 ) ;
plot ( z ) ;
ylabel ( C ) ;
subplot (6 ,1 ,2) ;
plot ( y11 ) ;
ylabel ( Y1 ) ;
plot ( z ) ;
subplot (6 ,1 ,3) ;
plot ( y21 ) ;
ylabel ( Y2 ) ;
plot ( z ) ;
subplot (6 ,1 ,4) ;
plot ( z ) ;
ylabel ( Y3 ) ;
plot ( y31 ) ;
46

127
128
129
130
131
132
133
134
135
136

subplot (6 ,1 ,5) ;
plot ( z ) ;
ylabel ( Y4 ) ;
xlabel ( Time i n m i l l i s e c o n d s ) ;
plot ( y41 ) ;
subplot (6 ,1 ,6) ;
plot ( z ) ;
ylabel ( Y ) ;
xlabel ( Time i n m i l l i s e c o n d s ) ;
plot ( yf1 ) ;

47

Chapter 4
Data processing circuits

## Scilab code Exa 4.1 4 to 1 mux using 2 to 1 mux

1 // e x a m p l e 4 . 1
2 clc ;
3 clear ;
4 disp ( L o g i c e q u a t i o n f o r 2to 1 M u l t i p l e x e r : )
5 printf ( Y = A D0 + AD1\n ) ;
6 disp ( L o g i c e q u a t i o n f o r 4to 1 M u l t i p l e x e r : )
7 printf ( Y = A B D0 + A BD1 + AB D2 + ABD3\n ) ;
8 disp ( T h i s can be r e w r i t t e n as , )
9 printf ( Y= A (B D0 + BD1) + A(B D2 + BD3) \n ) ;
10 disp ( Compare t h i s w i t h e q u a t i o n o f 2to 1 mux . We

n e e d two 2to 1 m u l t i p l e x e r t o r e a l i z e t h e
b r a c k e t e d t e r m s where B s e r v e s a s s e l e c t i n p u t .
The o u t p u t o f t h e s e two m u l t i p l e x e r s can be s e n t
t o a t h i r d m u l t i p l e x e r a s d a t a i n p u t s where A
s e r v e s a s s e l e c t i n p u t and we g e t t h e 4to 1
m u l t i p l e x e r . );

## Scilab code Exa 4.2 Realizing boolean equation using 8 to 1 mux

48

1 // e x a m p l e 4 . 2
2 clc ;
3 clear
4 a (1 ,1) =0 // t a k i n g i n p u t i n
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

this
form 1 i f A, 0 i f A
and 2 i f no A i n t h e term
a (1 ,2) =1
a (1 ,3) =2
a (2 ,1) =2
a (2 ,2) =0
a (2 ,3) =0
a (3 ,1) =1
a (3 ,2) =1
a (3 ,3) =1
p =3;
for i =1:3 // f i n d i n g t h e m i n t e r m s h e r e
coun =0;
for j =1:3
if a (i , j ) ==2 then
coun = coun +1
end
end
if coun == 2 then
p = p +3
else if coun ==1 then
p = p +1
end
end
end
n =4;
for m =4: p
for l =1:3
a (m , l ) =0;
end
end
for i = 1: p
for j =1:3
if a (i , j ) ==2 then
for k =1:3
49

38
a (n , k ) = a (i , k )
39
end
40
a (i , j ) = 0;
41
a (n , j ) =1;
42
n = n +1;
43
end
44
end
45 end
46 for h =1: p
47
f ( h ) =0
48
c =2;
49 for m =1:3
// f i n d i n g e q u i v l e n t

## decimal values for

the minterms
f ( h ) = f ( h ) + a (h , m ) *(2^ c ) ;
c =c -1;

50
51
52 end
53 end
54 disp ( The min t e r m s a r e
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

: ) // d i s p l a y i n g t h e min

terms
disp (
A
B
C)
disp ( a )
l =1
o (1 , l ) = f ( l ) ; // r e m o v i n g t h e r e p e t a t i o n s i n
minterms
for i =2: p
q =0;
for b =1: l
if o (1 , b ) == f ( i ) then
q =89 ;
end
end
if q ==0 then
o (1 , l +1) = f ( i ) ;
l = l +1;
q =0;
end
end
disp ( The f o l l o w i n g d a t a l i n e s a r e t o be g i v e n 1
50

and r e m a i n i n g s h o u l d be g i v e n 0 ) ; //
d i s p l y i n g the d e c i m a l e q u i v l e n t o f minterms
73 disp ( o ) ;
74 disp ( For a 41 mux , we s h o u l d g i v e D0 =C , D1 = 1
, D2 = C and D3 = C w i t h A and B a s d a t a
s e l e c t o r i n p u t s );

## Scilab code Exa 4.3 32 t0 1 mux using 16 to 1 and 2 to 1 muxes

1
2
3
4
5
6
7
8

// e x a m p l e 4 . 3
clc ;
clear ;
m (1) =32; // t k i n g t h e g i v e n v a l u e s
m (2) = log2 ( m (1) ) // making n e c e s s a r y c a l c u l a t i o n s
m (3) = m (2) -1;
m (4) = m (1) /2;
printf ( A %dto 1 m u l t i p l e x e r r e q u i r e s ,m (1) ) ; printf
( %d s e l e c t l i n e s , The l o w e r ,m (2) ) ; printf ( %d
s e l e c t l i n e s c h o o s e ,m (3) ) ; printf ( %dto 1
m u l t i p l e x e r o u t p u t s . The 2to 1 m u l t i p l e x e r s
c h o o s e s one o f t h e o u t p t o f two ,m (4) ) ; printf (
%dto 1 m u l t i p l e x e r s d e p e n d i n g on what a p p e a r s i n
t h e ,m (4) ) ; printf ( %dth s e l e c t l i n e . , m (2) ) ;
// d i s p l a y i n g t h e r e s u l t

## Scilab code Exa 4.4 74154 IC y12

1 // e x a m p l e 4 . 4
2 clc ;
3 clear ;
4 r = input ( E n t e r t h e v a l u e o f R ( 0 o r 1 ) : ) ; //

a c c e p t i n g t h e i n p u t s from t h e u s e r
5 t = input ( E n t e r t h e v a l u e o f T ( 0 o r 1 ) : ) ;
51

## 6 sel = input ( E n t e r t h e v a l u e s o f ABCD : ) ;

7 strb = bitcmp ( bitand (r , t ) ,1) ;
8 if strb ==0
then // c h e c k i n g w h e t h e r s t r o b e
9
10
11
12

i s high

o r low
if sel ==1100 then
y = The two p u l s e s a r e s t e e r e d t o t h e Y12
output ;
else
y = The o u t p u t Y12 r e m a i n s i n t h e High s t a t e
;
end
else
y = The o u t p u t Y12 r e m a i n s i n t h e High s t a t e ;

13
14
15
16 end
17 disp ( y ) // d i s p l a y i n g

result

## Scilab code Exa 4.7 realizing boolean equation using 3 to 8 decoder

1 // e x a m p l e 4 . 7
2 clc ;
3 clear ;
4 n = input ( E n t e r t h e no . o f t e r m s i n u r e x p r e s s i o n
5
6
7
8

9
10

: );

// a c c e p t i n g i n p u t from u s e r
for i =1: n
a (1 , i ) = input ( E n t e r t h e term (0 9) : ) ;
end ;
disp ( S i n c e a t t h e d e c o d e r o u t p u t we g e t a l l t h e
m i n t e r m s we u s e them t o g e t t h e r e q u i r e d b o o l e a n
f u n c t i o n s by g i v i n g t h e o u t p u t l i n e s numbered )
;
disp ( a ) ;
// d i s p l y i n g t h e r e s u l t
disp ( t o a m l t i i n p u t OR g a t e . ) ;

52

## Scilab code Exa 4.8 current in LED

1 // e x a m p l e 4 . 8
2 clc ;
3 clear ;
4 // r=i n p u t ( E n t e r t h e v a l u e s
5
6
7
8
9

o f r e s i s t a n c e i n Kohms

: ) ;
// v=i n p u t ( E n t e r t h e f o r w a r d v o l t a g e d r o p o f LED( i n
volts ) : ) ;
r =1 // t a k i n g t h e g i v e n v a l u e s f o r r and v
v =2
i =5 - v / r ; // c a l c u l a t i n g I
printf ( The c u r r e n t t h r o u g h a LED i s : %f mA , i ) ;
// d i s p l a y i n g I

Scilab code Exa 4.9 which LED lights up for given input conditions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

// e x a m p l e 4 . 9
clc ;
clear ;
sel = input ( E n t e r t h e v a l u e s o f ABCD : ) ;
a = sel ;
q =1;
while (a >0) // f i n d i n g t h e d e c i m a l e q u i v l e n t
r = modulo (a ,10) ;
b (1 , q ) = r ;
a = a /10;
a = floor ( a ) ;
q = q +1;
end
f =0;
for m =1: q -1
c =m -1
f = f + b (1 , m ) *(2^ c ) ;
53

19 end
20 if f >9 then // c h e c k i n g t h e i n v a l i d c o n d i t i o n
21
disp ( I t s a i n v a l i d i n p u t . T h e r e f o r e , none o f t h e

LEDs i s on b e c a u s e a l l o u t p u t s l i n e s a r e
high );
22 else
// d i s p l a y i n g t h e LED no i f t h e i n p u t s a r e
valid
23
printf ( \n LED %d l i g h t s up a l l . A l l o t h e r LEDs
remain o f f because the o t h e r outputs are high
. ,f ) ;
24 end

## Scilab code Exa 4.10 output of 74147 when button 6 is pressed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

// e x a m p l e 4 . 1 0
clc ;
clear ;
// s e l = i n p u t ( E n t e r which i s p r e s s e d ( 1 9 ) : ) ;
sel =6;
aa = sel ;
for i =4: -1:1 // c o n v e r t i n g t h e s e l i n p u t t o b i n a r y
notation
a (1 , i ) = modulo ( aa ,2) ;
b (1 , i ) = bitcmp ( a (1 , i ) ,1) ;
aa = aa /2;
aa = floor ( aa ) ;
end
printf ( When s w i t c h %d i s p r e s s e d t h e ABCD o u t p u t i s
: , sel ) ;
disp ( b ) ; // d i s p l a y i n g t h e r e s u l t
printf ( Which i s e q u i v a l e n t t o %d when t h e o u t p u t
i s complimented . In the output above a 0
r e p r e s e n t s a LOW and a 1 r e p r e s e n t s a HIGH .
, sel ) ;

54

## check Appendix AP 9 for dependency:

kmapsx.sci
check Appendix AP 2 for dependency:
noof.sci

## Scilab code Exa 4.11 priority encoder

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

// e x a m p l e 4 . 1 1
// u s e s f u n c t i o n s kmap . s c i and n o o f . s c i s o run them
b e f o r e r u n n i n g t h i s program . .
clc ;
s =[0 1 1 1 1 ];
x1 =[0 1 0 0 0 ];
x2 =[0 0 1 0 0 ];
x3 =[0 0 0 1 0 ];
for i =1:5
if s ( i ) ==1 then
// f i n d i n g o u t p u t A and B
if x1 ( i ) == 1 then
a ( i ) =0;
b ( i ) =1;
elseif x2 ( i ) == 1 then
a ( i ) =1;
b ( i ) =0;
elseif x3 ( i ) == 1 then
a ( i ) =1;
b ( i ) =1;
else
a ( i ) =0;
b ( i ) =0;
end
else
a ( i ) =0;
b ( i ) =0;
55

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

43
44
45

end
end
for i = 1 : 5
// p r i n t i n t h e s t a t e t a b l e
Y (i ,1) = s ( i ) ;
Y (i ,2) = x1 ( i ) ;
Y (i ,3) = x2 ( i ) ;
Y (i ,4) = x3 ( i ) ;
Y (i ,5) = a ( i ) ;
Y (i ,6) = b ( i ) ;
end
disp (
Input
Output
);
disp (
S
X1
X2
X3
A
B );
disp ( Y ) ;
kmp =[0 0 0 0;0 0 0 1 ;0 0 0 1;0 0 0 1]; // f i n d i n g
m i n i m i z e d e x p r e s s i n u s i n g 4 v a r i b l e kmap
disp ( The m i n i m a l e x p r e s s i o n o f A from t h e
f o l l o w i n g Kmap i s : ) ;
kmapsx ( kmp ) ;
kmp=[0 0 1 0 ; 0 0 1 1 ; 0 0 1 0 ; 0 0 1 0 ] ; //
f i n d i n g m i n i m i z e d e x p r e s s i n u s i n g 4 v a r i b l e
kmap
p r i n t f ( \n ) ;
d i s p ( The m i n i m a l e x p r e s s i o n o f B from t h e
f o l l o w i n g Kmap i s : )
kmapsx ( kmp ) ;

56

Chapter 5
Number Systems and Codes

## Scilab code Exa 5.1 Binary to decimal conversion

1 // Example 5 . 1
2 clc

// c l e a r s t h e command window
3 clear
// c l e a r s

a l l the v a r i a b l e s

4 p =1;

5
6
7
8
9
10

11

// i n i t i a l i s i n g
q =1;
z =0;
b =0;
w =0;
f =0;
format ( v ,18) ;

variables

// i n c r e a s i n g t h e p r e c i s i o n t o 18 .
// b i n= i n p u t ( E n t e r t h e b i n a r y no t o be c o n v e r t e d
to i t s decimal e q u i v a l e n t : )
// a c c e p t i n g
t h e b i n a r y i n p u t from u s e r
57

12 bin =110.001;
13 d = modulo ( bin ,1) ;

// s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r
part
14 d = d *10^10;
15 a = floor ( bin ) ;

16

// r e m o v i n g t h e d e c i m a l p a r t
while (a >0)

17
18
19
20
21
22
23

// Loop t o t a k e t h e b i n a r y b i t s o f i n t e g e r i n t o a
matrix
r = modulo (a ,10) ;
b (1 , q ) = r ;
a = a /10;
a = floor ( a ) ;
q = q +1;
end
for m =1: q -1

24
25
26
27

// m u l t i p l i y i n g t h e b i t s o f i n t e g e r w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g
c =m -1;
f = f + b (1 , m ) *(2^ c ) ;
end
while (d >0)

28
29
30
31
32
33
34

// Loop t o t a k e t h e b i n a r y b i t s o f d e c i m a l i n t o a
matrix
e = modulo (d ,2)
w (1 , p ) = e
d = d /10;
d = floor ( d )
p = p +1;
end
for n =1: p -1

58

// m u l t i p l i y i n g t h e b i t s o f d e c i m a l w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g
35
z = z + w (1 , n ) *(0.5) ^(11 - n ) ;
36 end
37 z = z *10000;
// r o u n d i n g o f t o 4 d e c i m a l v a l u e s
38 z = round ( z ) ;
39 z = z /10000;
40 printf ( The D e c i m a l e q u i v a l e n t

g i v e n i s = %f ,f + z ) ;
result

o f t h e B i n a r y number
// D i s p l a y i n g t h e f i n a l

## Scilab code Exa 5.2 Binary to decimal conversion

1 // Example 5 . 2
2 clc

// c l e a r s t h e command window
clear

// c l e a r s
4 p =1;

5
6
7
8
9
10

a l l the v a r i a b l e s

// i n i t i a l i s i n g
q =1;
z =0;
b =0;
w =0;
f =0;
format ( v ,18) ;

variables

// i n c r e a s i n g t h e p r e c i s i o n t o 18 .
11 // b i n= i n p u t ( E n t e r t h e b i n a r y no t o be c o n v e r t e d
to i t s decimal e q u i v a l e n t : )
// a c c e p t i n g
59

t h e b i n a r y i n p u t from u s e r
12 bin =1011.11;
13 d = modulo ( bin ,1) ;
// s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r
part
14 d = d *10^10;
15 a = floor ( bin ) ;
// r e m o v i n g t h e d e c i m a l p a r t
16 while (a >0)

17
18
19
20
21
22
23

// Loop t o t a k e t h e b i n a r y b i t s o f i n t e g e r i n t o a
matrix
r = modulo (a ,10) ;
b (1 , q ) = r ;
a = a /10;
a = floor ( a ) ;
q = q +1;
end
for m =1: q -1

24
25
26
27

// m u l t i p l i y i n g t h e b i t s o f i n t e g e r w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g
c =m -1;
f = f + b (1 , m ) *(2^ c ) ;
end
while (d >0)

28
29
30
31
32
33
34

// Loop t o t a k e t h e b i n a r y b i t s o f d e c i m a l i n t o a
matrix
e = modulo (d ,2)
w (1 , p ) = e
d = d /10;
d = floor ( d )
p = p +1;
end
for n =1: p -1
60

// m u l t i p l i y i n g t h e b i t s o f d e c i m a l w i t h t h e i r
p o s i t i o n v a l u e s and a d d i n g
35
z = z + w (1 , n ) *(0.5) ^(11 - n ) ;
36 end
37 z = z *10000;
// r o u n d i n g o f t o 4 d e c i m a l v a l u e s
38 z = round ( z ) ;
39 z = z /10000;
40 printf ( The D e c i m a l e q u i v a l e n t o f t h e B i n a r y number
g i v e n i s = %f ,f + z ) ;
// D i s p l a y i n g t h e f i n a l

result

## Scilab code Exa 5.3 decimal equivalent of 2 Mb

1 // Example 5 . 3
2 clc

// c l e a r s t h e command window .
clear

// c l e a r s a l l t h e v a r i a b l e s .
format ( v ,18) ;

// i n c r e a s i n g t h e p r e c i s i o n t o 18 .
5 n =2; // / g i v e n 2 mb
6 dec = n * 2^20 ;
7 printf ( The d e c i m a l e q u i v a l e n t o f 2Mb i s = %f , dec )
;
// d i s p l a y i n g t h e v a l u e .

61

## Scilab code Exa 5.4 Decimal to binary conversion

1 // Example 5 . 3
2 clc

// c l e a r s t h e command window
3 clear

4
5
6
7

// c l e a r s a l l t h e v a r i a b l e s
q =0;
b =0;
s =0;
format ( v ,18) ;

// i n c r e a s i n g t h e p r e c i s i o n t o 18 .
// a=i n p u t ( E n t e r t h e d e c i m a l no t o be c o n v e r t e d t o
i t s binary e q u i v a l e n t : ) ;
// a c c e p t i n g
t h e d e c i m a l i n p u t from u s e r
9 a =23.6;
10 d = modulo (a ,1) ;
8

// s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r
part
11 a = floor ( a ) ;
// r e m o v i n g t h e d e c i m a l p a r t
12
13

14
15
16

while (a >0)
// t a k i n g i n t e g e r p a r t i n t o a m a t r i x and c o n v e r t
to e q u i v a l e n t binary
x = modulo (a ,2) ;
b = b + (10^ q ) * x ;
a = a /2;
62

17
a = floor ( a ) ;
18
q = q +1;
19 end
20
21
22
23 for i =1:10

24
25
26
27
28
29
30
31
32

// For v a l u e s a f t e r d e c i m a l p o i n t c o n v e r t i n g t o
binary
d = d *2;
q = floor ( d ) ;
s = s + q /(10^ i ) ;
if d >=1 then
d =d -1;
end
end
k=b+s;
printf ( The b i n a r y e q u i v a l e n t o f t h e g i v e n d e c i m a l
number i s = %f ,k ) ;
// d i s p l a y i n g t h e f i n a l

result .

## Scilab code Exa 5.5 Binary number having all ones

1 // Example 5 . 5
2 clc

// c l e a r s t h e command window
clear

// c l e a r s a l l t h e v a r i a b l e s
4 format ( v ,18)
// i n c r e a s i n g t h e p r e c i s i o n
63

5 n =32; // g i v e n 32 1 s
6 dec =2^ n - 1 ;
7 printf ( The d e c i m a l e q u i v a l e n t

a l l 1 s i s = %f , dec ) ;
result

o f 32 b i t number w i t h
// d i s p l a y i n g t h e

## Scilab code Exa 5.6 Decimal to binary conversion

1 // Example 5 . 6
2 clc

// c l e a r s t h e command window
3 clear

4
5
6
7

// c l e a r s a l l t h e v a r i a b l e s
q =0;
b =0;
s =0;
format ( v ,18) ;

// i n c r e a s i n g t h e p r e c i s i o n t o 18 .
// a=i n p u t ( E n t e r t h e d e c i m a l no t o be c o n v e r t e d t o
i t s binary e q u i v a l e n t : ) ;
// a c c e p t i n g
t h e d e c i m a l i n p u t from u s e r
9 a =363;
8

// t a k i n g t h e v a l u e g i v e n i n p r o b l e m
10 d = modulo (a ,1) ;
// s e p a r a t i n g t h e d e c i m a l p a r t and t h e i n t e g e r
part
11 a = floor ( a ) ;
// r e m o v i n g t h e d e c i m a l p a r t
12

64

13

while (a >0)
// t a k i n g i n t e g e r p a r t i n t o a m a t r i x and c o n v e r t
to e q u i v a l e n t binary
x = modulo (a ,2) ;
b = b + (10^ q ) * x ;
a = a /2;
a = floor ( a ) ;
q = q +1;
end

14
15
16
17
18
19
20
21
22
23 for i =1:10

24
25
26
27
28
29
30
31
32
33

// For v a l u e s a f t e r d e c i m a l p o i n t c o n v e r t i n g t o
binary
d = d *2;
q = floor ( d ) ;
s = s + q /(10^ i ) ;
if d >=1 then
d =d -1;
end
end
k=b+s;
disp ( The g i v e d e c i m a l number i s 363 )
printf ( The b i n a r y e q u i v a l e n t o f t h e g i v e n d e c i m a l
number i s = %f ,k ) ;
// d i s p l a y i n g t h e f i n a l

result .

## Scilab code Exa 5.7 binary to hexadecimal

1 // Example 5 . 7
2 clc

65

// c l e a r s t h e command window
clear

8
9

// c l e a r s a l l t h e v a r i a b l e s
q =1;
b =0;
f =0;
bin = input ( E n t e r t h e 8 b i t b i n a r y a d d r e s s : ) ;
// Taking t h e i n p u t
b i n a r y b i t s from t h e u s e r
a = floor ( bin )
while (a >0)

10
11
12
13
14
15
16

// Loop t o t a k e t h e b i n a r y b i t s i n t o a m a t r i x (
array )
r = modulo (a ,10) ;
b (1 , q ) = r ;
a = a /10;
a = floor ( a ) ;
q = q +1;
end
for m =1: q -1

4
5
6
7

// c o n v e r r t i n g t o d e c i m a l
17
c =m -1;
18
f = f + b (1 , m ) *(2^ c ) ;
19 end
20 c = dec2hex ( f ) ;
21 printf ( The h e x a d e c i m a l e q u i v a l e n t

b i n a r y number i s : %s ,c ) ;
d i s p l a y i n g the value

## Scilab code Exa 5.8 hexadecimal to decimal

66

of the given
//

1 // Example 5 . 8
2 clc

// c l e a r t h e command window
clear
// c l e a r t h e v a r i a b l e s

4 a = input ( E n t e r t h e h e x a d e c i m a l number t o be

## converted into decimal ( enter in a s i n g l e

quotation ) : )
// t a k i n g t h e i n p u t from
user
5 d = hex2dec ( a ) ;
6 printf ( The d e c i m a l e q u i v a l e n t i s : %d ,d ) ;
// d i s p l a y i n g t h e o u t p u t

## Scilab code Exa 5.9 decimal to hexadecimal and binary

1 // Example 5 . 9
2 clc
3
4
5
6
7
8
9
10
11
12

// c l e a r s t h e

command window
clear
// c l e a r s
the v a r i a b l e s
q =0;
b =0;
// a=i n p u t ( e n t e r t h e d e c i m a l no : )
a =65535;
//
g i v i n g the v a l u e s p e c i f i e d i n the problem
temp = a ;
format ( v ,18)
//
i n c r e a s i n g t h e p r e c i s i o n t o 18
a = floor ( a ) ;
h = dec2hex ( a ) ;
while (a >0)
//
converting to binary
67

13
x = modulo (a ,2) ;
14
b = b + (10^ q ) * x ;
15
a = a /2;
16
a = floor ( a ) ;
17
q = q +1;
18 end
19 printf ( Given d e c i m a l number i s : %d\n , temp )
20 printf ( The h e x a d e c i m a l e q u i v a l e n t i s = %s\n ,h ) ;

//
d i s p l a y i n g the r e s u l t s
21 printf ( The b i n a r y e q u i v a l e n t i s = %f ,b ) ;

## Scilab code Exa 5.10 decimal to hexadecimal and binary

1 // Example 5 . 1 0
2 clc
3
4
5
6
7
8
9
10
11
12
13
14
15
16

// c l e a r s t h e
command window
clear
// c l e a r s
the v a r i a b l e s
q =0;
b =0;
// a=i n p u t ( e n t e r t h e d e c i m a l no : )
a =56000;
//
g i v i n g the v a l u e s p e c i f i e d i n the problem
temp = a ;
format ( v ,18)
//
i n c r e a s i n g t h e p r e c i s i o n t o 18
a = floor ( a ) ;
h = dec2hex ( a ) ;
while (a >0)
//
converting to binary
x = modulo (a ,2) ;
b = b + (10^ q ) * x ;
a = a /2;
a = floor ( a ) ;
68

17
q = q +1;
18 end
19 printf ( Given d e c i m a l number i s : %d\n , temp )
20 printf ( The h e x a d e c i m a l e q u i v a l e n t i s = %s\n ,h )
21
// d i s p l a y i n g t h e r e s u l t s
22 printf ( The b i n a r y e q u i v a l e n t i s = %f\n ,b ) ;

## Scilab code Exa 5.11 decimal to hexadecimal and binary

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

// c h a p t e r 5
// Example 5 . 1 1
//Q . c o n v e r t d e c i m a l numbers t o i t s h e x a d e c i m a l and
binary equivalent ?
// s o l u t i o n :
clc
// c l e a r s t h e
command window
clear
// c l e a r s
the v a r i a b l e s
q =0;
b =0;
a = input ( E n t e r t h e d e c i m a l no : )
// E n t e r
the decimal nuber
format ( v ,18)
//
i n c r e a s i n g t h e p r e c i s i o n t o 18
a = floor ( a ) ;
h = dec2hex ( a ) ;
while (a >0)
//
converting to binary
x = modulo (a ,2) ;
b = b + (10^ q ) * x ;
a = a /2;
a = floor ( a ) ;
q = q +1;
end
printf ( The h e x a d e c i m a l e q u i v a l e n t i s = %s\n ,h )
69

//

21

displaying
the
results
22

printf ( The b i n a r y e q u i v a l e n t i s = %f ,b )

70

Chapter 6
Arithmetic Circuits

## Scilab code Exa 6.1 8bit binary adder

// exmple 6 . 1
clc ;
clear ;
// a=i n p u t ( e n t e r t h e f i r s t 8 b i t number : ) ;
// b=i n p u t ( e n t e r t h e s e c o n d 8 b i t number : ) ;
a =01010111; // t a k i n g g i v e n i n p u t s
b =00110101;
for i =1:8
a1 ( i ) = modulo (a ,10) ;
a = a /10;
a = round ( a ) ;
b1 ( i ) = modulo (b ,10) ;
b = b /10;
b = round ( b ) ;
end
car (1) =0;
for i =1:8
// a d d i n g b o t h t h e i n p u t s ( b i n a r y
18
c1 ( i ) = car ( i ) + a1 ( i ) + b1 ( i ) ;
19
if c1 ( i ) == 2 then
20
car ( i +1) = 1;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

71

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

c1 ( i ) =0;
elseif c1 ( i ) ==3 then
car ( i +1) = 1;
c1 ( i ) =1;
else
car ( i +1) =0;
end
end
c1 (9) = car (9) ;
re =0;
format ( v ,18) ;
for i =1:9
re = re +( c1 ( i ) *(10^( i -1) ) )
end
printf ( The sum o f g i v e n two b i n a r y numbers i s %d\n
, re ) ;
q =1;
b =0;
f =0;
a = re ;
while (a >0)
// c o n v e r t i n g t h e r e s u l t t o a
h e x a d e c i m a l no
r = modulo (a ,10) ;
b (1 , q ) = r ;
a = a /10;
a = floor ( a ) ;
q = q +1;
end
for m =1: q -1
c =m -1;
f = f + b (1 , m ) *(2^ c ) ;
end
hex = dec2hex ( f ) ;
printf ( The sum i n h e x a d e c i m a l n o t a t i o n i s %s \n ,
hex ) ; // d i s p l a y i n g r e s u l t

72

## Scilab code Exa 6.2 16 bit binary adder

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

// exmple 6 . 2
clc ;
clear ;
// a=i n p u t ( e n t e r t h e f i r s t 16 b i t b i n a r y number : ) ;
// b=i n p u t ( e n t e r t h e s e c o n d 16 b i t b i n a r y n u m b e r : ) ;
a =0000111110101100;
b =0011100001111111;
for i =1:16
a1 ( i ) = modulo (a ,10) ;
a = a /10;
a = round ( a ) ;
b1 ( i ) = modulo (b ,10) ;
b = b /10;
b = round ( b ) ;
end
car (1) =0;
for i =1:16
// / a d d i n g b o t h t h e 16 b i t i n p u t s (
c1 ( i ) = car ( i ) + a1 ( i ) + b1 ( i ) ;
if c1 ( i ) == 2 then
car ( i +1) = 1;
c1 ( i ) =0;
elseif c1 ( i ) ==3 then
car ( i +1) = 1;
c1 ( i ) =1;
else
car ( i +1) =0;
end
end
c1 (17) = car (17) ;
re =0;
format ( v ,25) ;
73

32 for i =1:17
33
re = re +( c1 ( i ) *(10^( i -1) ) )
34 end
35 printf ( The sum o f g i v e n two b i n a r y numbers i s %f\n
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

, re ) ;
q =1;
b =0;
f =0;
a = re ;
while (a >0)
// c o n v e r t i n g t h e r e s u l t t o a
h e x a d e c i m a l no
r = modulo (a ,10) ;
b (1 , q ) = r ;
a = a /10;
a = floor ( a ) ;
q = q +1;
end
for m =1: q -1
c =m -1
f = f + b (1 , m ) *(2^ c ) ;
end
hex = dec2hex ( f ) ;
printf ( Sum i n d e c i m a l n o t a t i o n i s %d\n ,f ) ;
printf ( Sum i n h e x a d e c i m a l n o t a t i o n i s %s \n , hex ) ;
// d i s p l a y i n g r e s u l t

## Scilab code Exa 6.3 first generation microcomputers addition

1 // e x a m p l e 6 . 3
2 clc ;
3 clear ;
4 // a=i n p u t ( e n t e r t h e f i r s t 8 b i t number : ) ;
5 // b=i n p u t ( e n t e r t h e s e c o n d 8 b i t number : ) ;
6 a =0000111110101100;
7 b =0011100001111111;

74

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

for i =1:16
a1 ( i ) = modulo (a ,10) ;
a = a /10;
a = round ( a ) ;
b1 ( i ) = modulo (b ,10) ;
b = b /10;
b = round ( b ) ;
end
car (1) =0;
for i =1:8
c1 ( i ) = car ( i ) + a1 ( i ) + b1 ( i ) ;
Higher bytes
if c1 ( i ) == 2 then
car ( i +1) = 1;
c1 ( i ) =0;
elseif c1 ( i ) ==3 then
car ( i +1) = 1;
c1 ( i ) =1;
else
car ( i +1) =0;
end
end
c1 (9) = car (9)
re =0;
format ( v ,18) ;
for i =1:9
re = re +( c1 ( i ) *(10^( i -1) ) )
end

// a d d i n g t h e

## printf ( The sum o f l o w e r b y t e s o f two b i n a r y numbers

i s %d\n , re ) ;
printf ( w i t h a c a r r y i s %d\n , car (9) ) ;
re = re -( c1 (9) *(10^(8) ) )
for i =9:16
// a d d i n g t h e H i g h e r b y t e s
c1 ( i ) = car ( i ) + a1 ( i ) + b1 ( i ) ;
if c1 ( i ) == 2 then
car ( i +1) = 1;
c1 ( i ) =0;
75

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

## elseif c1 ( i ) ==3 then

car ( i +1) = 1;
c1 ( i ) =1;
else
car ( i +1) =0;
end

end
c1 (17) = car (17) ;
format ( v ,25) ;
ree =0;
for i =9:17
ree = ree +( c1 ( i ) *(10^( i -9) ) ) ;
end
for i =9:17
re = re +( c1 ( i ) *(10^( i -1) ) )
end
printf ( The sum o f u p p e r b y t e s o f t h e g i v e n
numbers i s %d\n , ree ) ;
61 printf ( w i t h a c a r r y i s %d\n , car (17) ) ; //
displaying results
62 printf ( The t o t a l sum i s %f , re ) ;

## Scilab code Exa 6.4 binary subtraction

// exmple 6 . 4
clc ;
clear ;
a =0;
b =0;
q =0;
// bb=i n p u t ( E n t e r t h e f i r s t no ( i n d e c i m a l ) : ) ;
// aa=i n p u t ( E n t e r t h e number from which f i r s t no
has to s u b s t r a c t e d : ) ;
9 aa =200; // t a k i n g t h e g i v e n i n p u t
10 bb =125;
1
2
3
4
5
6
7
8

76

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

while ( aa >0)
// c o n v e r t i n g t h e i n p u t s i n t o b i n a r y
numbers
x = modulo ( aa ,2) ;
a = a + (10^ q ) * x ;
aa = aa /2;
aa = floor ( aa ) ;
q = q +1;
end
q =0;
while ( bb >0)
x = modulo ( bb ,2) ;
b = b + (10^ q ) * x ;
bb = bb /2;
bb = floor ( bb ) ;
q = q +1;
end
printf ( \ nThe b i n a r y e q u i v a l e n t o f f i r s t no i s %f\n
\n ,b ) ;
printf ( The b i n a r y e q u i v a l e n t o f s e c n d no i s %f\n\n
,a ) ;
for i =1:40
a1 ( i ) = modulo (a ,10) ;
a = a /10;
a = round ( a ) ;
b1 ( i ) = modulo (b ,10) ;
b = b /10;
b = round ( b ) ;
end
bro (1) =0;
for i =1:40
c1 ( i ) = a1 ( i ) - b1 ( i ) - bro ( i ) ; // f i n d i n g t h e
d i f f e r e n c e of the given inputs
if c1 ( i ) == -1 then
bro ( i +1) = 1;
c1 ( i ) =1;
elseif c1 ( i ) == -2 then
bro ( i +1) = 1;
c1 ( i ) =0;
77

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

else
bro ( i +1) =0;
end
end
re =0;
format ( v ,18) ;
for i =1:40
re = re +( c1 ( i ) *(10^( i -1) ) )
end
printf ( The d i f e r e n c e o f g i v e n two numbers i s %f\n\
n , re ) ;
q =1;
b =0;
f =0;
a = re ;
while (a >0)
r = modulo (a ,10) ;
b (1 , q ) = r ;
a = a /10;
a = floor ( a ) ;
q = q +1;
end
for m =1: q -1
c =m -1
f = f + b (1 , m ) *(2^ c ) ;
end
hex = dec2hex ( f ) ;
printf ( Sum i n d e c i m a l n o t a t i o n i s %d\n\n ,f ) ; //
d i s p l a y i n g the r e s u l t s
printf ( Sum i n h e x a d e c i m a l n o t a t i o n i s %s \n , hex ) ;

## Scilab code Exa 6.5 adding 8 bit unsigned numbers

1

// e x a m p l e 6 . 5
78

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

clc ;
clear ;
a =0;
b =0;
q =0;
// aa=i n p u t ( E n t e r t h e f i r s t no ( i n d e c i m a l ) : ) ;
// bb=i n p u t ( E n t e r t h e number from which f i r s t no
has to s u b s t r a c t e d : ) ;
aa =150;
bb =85;
while ( aa >0)
// c o n v e r t i n g t h e i n p u t s i n
t o b i n a r y numbers
x = modulo ( aa ,2) ;
a = a + (10^ q ) * x ;
aa = aa /2;
aa = floor ( aa ) ;
q = q +1;
end
q =0;
while ( bb >0)
x = modulo ( bb ,2) ;
b = b + (10^ q ) * x ;
bb = bb /2;
bb = floor ( bb ) ;
q = q +1;
end
printf ( \n The b i n a r y e q u i v a l e n t o f f i r s t no i s %f\
n\n ,a ) ;
printf ( The b i n a r y e q u i v a l e n t o f s e c n d no i s %f\n\n
,b ) ;
for i =1:40
a1 ( i ) = modulo (a ,10) ;
a = a /10;
a = round ( a ) ;
b1 ( i ) = modulo (b ,10) ;
b = b /10;
79

36
b = round ( b ) ;
37 end
38
39 car (1) =0;
40 for i =1:40
41
c1 ( i ) = car ( i ) + a1 ( i ) + b1 ( i ) ; // addng b o t h t h e
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

inputs
if c1 ( i ) == 2 then
car ( i +1) = 1;
c1 ( i ) =0;
elseif c1 ( i ) ==3 then
car ( i +1) = 1;
c1 ( i ) =1;
else
car ( i +1) =0;
end
end
c1 (41) = car (41) ;
re =0;
format ( v ,18) ;
for i =1:41
re = re +( c1 ( i ) *(10^( i -1) ) )
end
printf ( The sum o f g i v e n two b i n a r y numbers i s %f\n
\n , re ) ;
q =1;
b =0;
f =0;
a = re ;
while (a >0)
r = modulo (a ,10) ;
b (1 , q ) = r ;
a = a /10;
a = floor ( a ) ;
q = q +1;
end
for m =1: q -1
c =m -1;
80

72
f = f + b (1 , m ) *(2^ c ) ;
73 end
74 printf ( Sum i n d e c i m a l n o t a t i o n

i s %d\n\n ,f ) ;

//

displaying results
75 hex = dec2hex ( f ) ;
76 printf ( The sum i n h e x a d e c i m a l n o t a t i o n

i s %sH \n ,

hex ) ;

## Scilab code Exa 6.6 subtraction of unsigned numbers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

// exmple 6 . 6
clc ;
clear ;
a =0;
b =0;
q =0;
// bb=i n p u t ( E n t e r t h e f i r s t no ( i n d e c i m a l ) : ) ;
// aa=i n p u t ( E n t e r t h e number from which f i r s t no
has to s u b s t r a c t e d : ) ;
aa =150;
bb =85;
while ( aa >0) // // c o n v e r t i n g t h e i n p u t s i n t o b i n a r y
numbers
x = modulo ( aa ,2) ;
a = a + (10^ q ) * x ;
aa = aa /2;
aa = floor ( aa ) ;
q = q +1;
end
nn = a
q =0;
while ( bb >0)
x = modulo ( bb ,2) ;
b = b + (10^ q ) * x ;
bb = bb /2;
81

24
bb = floor ( bb ) ;
25
q = q +1;
26 end
27 printf ( \ nThe b i n a r y
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

e q u i v a l e n t o f f i r s t no i s %f\n
\n ,b ) ;
printf ( The b i n a r y e q u i v a l e n t o f s e c n d no i s %f\n\n
,a ) ;
for i =1:40
a1 ( i ) = modulo (a ,10) ;
a = a /10;
a = round ( a ) ;
b1 ( i ) = modulo (b ,10) ;
b = b /10;
b = round ( b ) ;
end
bro (1) =0;
for i =1:40
c1 ( i ) = a1 ( i ) - b1 ( i ) - bro ( i ) ;
if c1 ( i ) == -1 then
bro ( i +1) = 1;
c1 ( i ) =1;
elseif c1 ( i ) == -2 then
bro ( i +1) = 1;
c1 ( i ) =0;
else
bro ( i +1) =0;
end

end
re =0;
format ( v ,18) ;
for i =1:40
re = re +( c1 ( i ) *(10^( i -1) ) )
end
printf ( The d i f e r e n c e o f g i v e n two numbers i s %f\n\
n , re ) ;
58 q =1;
82

59 b =0;
60 f =0;
61 a = re ;
62 while (a >0) // c o n v e r t i n g t h e b i n a r y
63
64
65
66
67
68
69
70
71
72
73
74
75

r e s u l t to

r = modulo (a ,10) ;
b (1 , q ) = r ;
a = a /10;
a = floor ( a ) ;
q = q +1;
end
for m =1: q -1
c =m -1
f = f + b (1 , m ) *(2^ c ) ;
end
hex = dec2hex ( f ) ;
printf ( Sum i n d e c i m a l n o t a t i o n i s %d\n\n ,f ) ;
printf ( Sum i n h e x a d e c i m a l n o t a t i o n i s %s \n , hex ) ;

## Scilab code Exa 6.7 overflow case

1
2
3
4
5
6
7
8
9
10
11
12

// e x a m p l e 6 . 7
clc ;
clear ;
a =0;
b =0;
q =0;
// aa=i n p u t ( E n t e r t h e f i r s t no ( i n d e c i m a l ) : ) ;
// bb=i n p u t ( E n t e r t h e number from which f i r s t no
has to s u b s t r a c t e d : ) ;
aa =175;
bb =118;
while ( aa >0)
// c o n v e r t i n g t h e i n p u t s i n t o b i n a r y
numbers
x = modulo ( aa ,2) ;
83

13
a = a + (10^ q ) * x ;
14
aa = aa /2;
15
aa = floor ( aa ) ;
16
q = q +1;
17 end
18 q =0;
19 while ( bb >0)
20
x = modulo ( bb ,2) ;
21
b = b + (10^ q ) * x ;
22
bb = bb /2;
23
bb = floor ( bb ) ;
24
q = q +1;
25 end
26 printf ( \n The b i n a r y
27

e q u i v a l e n t o f f i r s t no i s %f\
n\n ,a ) ;
printf ( The b i n a r y e q u i v a l e n t o f s e c n d no i s %f\n\n
,b ) ;
for i =1:8
a1 ( i ) = modulo (a ,10) ;
a = a /10;
a = round ( a ) ;
b1 ( i ) = modulo (b ,10) ;
b = b /10;
b = round ( b ) ;
end

28
29
30
31
32
33
34
35
36
37 car (1) =0;
38 for i =1:8
39
c1 ( i ) = car ( i ) + a1 ( i ) + b1 ( i ) ; // a d d i n g t h e b i n a r y
40
41
42
43
44
45
46
47

numbers ( b i n a r y a d d t i o n )
if c1 ( i ) == 2 then
car ( i +1) = 1;
c1 ( i ) =0;
elseif c1 ( i ) ==3 then
car ( i +1) = 1;
c1 ( i ) =1;
else
car ( i +1) =0;
84

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

end
end
c1 (9) = car (9) ;
re =0;
format ( v ,18) ;
for i =1:8
re = re +( c1 ( i ) *(10^( i -1) ) )
end
printf ( I f o n l y 8 b i t s a r e t a k e n t h e r e s u l t w i l l be
a s shown b e l o w \n\n ) ;
printf ( and t h e sum o f g i v e n two b i n a r y numbers
w i l l be %f\n\n , re ) ;
q =1;
b =0;
f =0;
a = re ;
while (a >0) // c o n v e r t i n g t h e b i n a r y o u t p u t t o
r = modulo (a ,10) ;
b (1 , q ) = r ;
a = a /10;
a = floor ( a ) ;
q = q +1;
end
for m =1: q -1
c =m -1;
f = f + b (1 , m ) *(2^ c ) ;
end
printf ( Sum i n d e c i m a l n o t a t i o n i s %d\n\n ,f ) ;
hex = dec2hex ( f ) ;
printf ( The sum i n h e x a d e c i m a l n o t a t i o n i s %sH \n ,
hex ) ;
printf ( \n w i t h an o v e r f l o w o f %d\n\n , car (9) ) ;

## Scilab code Exa 6.8 2s compliment

85

1 // e x a m p l e 6 . 8
2 clc ;
3 clear ;
4 re =0;
5 aaa = input ( e n t e r t h e number ( i n d e c i m a l ) : ) ; //
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

t a k i n g t h e s i g n e d number
m = aaa ;
if aaa <0 then
aa = -1* aaa ;
else aa = aaa ;
end
a =0;
q =0;
while ( aa >0)
// c o n v e r t i n g from d e c i m a l t o
binary
x = modulo ( aa ,2) ;
a = a + (10^ q ) * x ;
aa = aa /2;
aa = floor ( aa ) ;
q = q +1;
end
mm = a ;
for i =1:8
a1 ( i ) = modulo (a ,10) ;
a = a /10;
a = round ( a ) ;
b1 ( i ) =0;
end
b1 (1) =1;
if aaa <0 then // making two s complement i f t h e
number i s l e s s t h a n z e r o
for i =1:8
a1 ( i ) = bitcmp ( a1 ( i ) ,1) ;
end
car (1) =0;
for i =1:8
c1 ( i ) = car ( i ) + a1 ( i ) + b1 ( i ) ; // a d d i n g one ( a s a
part o f f i n d i n g 2 s compliment
86

35
if c1 ( i ) == 2 then
36
car ( i +1) = 1;
37
c1 ( i ) =0;
38
elseif c1 ( i ) ==3 then
39
car ( i +1) = 1;
40
c1 ( i ) =1;
41
else
42
car ( i +1) =0;
43
end ;
44 end ;
45 c1 (9) = car (9) ;
46 re =0;
47 format ( v ,18) ;
48
for i =1:9
49
re = re +( c1 ( i ) *(10^( i -1) ) )
50
end ;
51
printf ( \ nThe b i n a r y c o n t e n t s a r e %d\n\n ,

re ) ;
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

else
re = mm ;
end ;
if ( aaa >0)
printf ( \ nThe b i a r y c o n t e n t s a r e %d\n\n , mm ) ;
end ;
q =1;
b =0;
f =0;
a = re ;
while (a >0) // c o n v e r t i n g t h e r e s u l t t o d e c i m a l t h e n
r = modulo (a ,10) ;
b (1 , q ) = r ;
a = a /10;
a = floor ( a ) ;
q = q +1;
end
for m =1: q -1
c =m -1
87

71
f = f + b (1 , m ) *(2^ c ) ;
72 end
73 hex = dec2hex ( f ) ;
74 printf ( The H e x a d e c i m a l c o n t e n t s a r e %sH , hex ) ; //

d i s p l a y i n the r e s u l t

## Scilab code Exa 6.9 2s compliment

// e x a m p l e 6 . 9
clc ;
clear ;
// aaa=i n p u t ( e n t e r t h e number ( i n d e c i m a l ) : ) ;
aaa = -19750 // g i v e n i n p u t
aa = -1* aaa ;
format ( v ,18) ;
a =0;
q =0;
while ( aa >0)
// c o n v e r t i n g
i t to binary
x = modulo ( aa ,2) ;
a = a + (10^ q ) * x ;
aa = aa /2;
aa = floor ( aa ) ;
q = q +1;
end
for i =1:16
a1 ( i ) = modulo (a ,10) ;
a = a /10;
a = round ( a ) ;
b1 ( i ) =0;
end
b1 (1) =1;
for i =1:16
// / f i n d i n g t h e 2 s
compliment
25
a1 ( i ) = bitcmp ( a1 ( i ) ,1) ;
26
end

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

88

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

## car (1) =0;

for i =1:16
c1 ( i ) = car ( i ) + a1 ( i ) + b1 ( i ) ;
if c1 ( i ) == 2 then
car ( i +1) = 1;
c1 ( i ) =0;
elseif c1 ( i ) ==3 then
car ( i +1) = 1;
c1 ( i ) =1;
else
car ( i +1) =0;
end ;
end ;
c1 (17) = car (17) ;
re =0;
for i =1:17
re = re +( c1 ( i ) *(10^( i -1) ) )
end ;
printf ( \n The 2 s c o m p l i m e n t i s ) ;
disp ( re ) ;
q =1;
b =0;
f =0;
a = re ;
while (a >0)
// c o n v e r t i n g t o h e x a d e c i m a l
r = modulo (a ,10) ;
b (1 , q ) = r ;
a = a /10;
a = floor ( a ) ;
q = q +1;
end
for m =1: q -1
c =m -1
f = f + b (1 , m ) *(2^ c ) ;
end
hex = dec2hex ( f ) ;
printf ( \n I n H e x a d e c i m a l n o t a t i o n i s %sH\n\n , hex )
; // d i s p l a y i n g t h e r e s u l t
89

64

disp ( As t h e memory o f a f i r s t g e n e r a t i o n
m i c r o c u m p u t e r i s o r g n i s e d i n b y t e s . The l o w e r
b y t e i s s t o r e d i n 2 0 0 0 a d d r e s s and t h e h i g h e r
byte i s s t o r e d i n 2001 a d d r e s s . );

## Scilab code Exa 6.10 2s compliment subtraction

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

// e x a m p l e 6 . 1 0
clc ;
clear ;
format ( v ,18) ;
// bb=i n p u t ( e n t e r t h e f i r s t number ( i n d e c i m a l ) : ) ;
// aaa=i n p u t ( e n t e r t h e s e c o n d number ( n e g a t i v e ) : ) ;
aaa = -12618
bb =18357;
aa = -1* aaa ;
a =0;
q =0;
while ( aa >0)
// f i n d i n g t h e b i n a r y
equivalents
x = modulo ( aa ,2) ;
a = a + (10^ q ) * x ;
aa = aa /2;
aa = floor ( aa ) ;
q = q +1;
end
r =0;
b =0;
while ( bb >0)
x = modulo ( bb ,2) ;
b = b + (10^ r ) * x ;
bb = bb /2;
bb = floor ( bb ) ;
r = r +1;
end
90

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

m=b
for i =1:16
a1 ( i ) = modulo (a ,10) ;
a = a /10;
a = round ( a ) ;
p1 ( i ) =0;
b1 ( i ) = modulo (b ,10) ;
b = b /10;
b = round ( b ) ;
end
p1 (1) =1;
for i =1:16
// f i n d i n g t h e 2 s c o m p l i m e n t
o f s e c o n d number
a1 ( i ) = bitcmp ( a1 ( i ) ,1) ;
end
car (1) =0;
for i =1:16
c1 ( i ) = car ( i ) + a1 ( i ) + p1 ( i ) ;
if c1 ( i ) == 2 then
car ( i +1) = 1;
c1 ( i ) =0;
elseif c1 ( i ) ==3 then
car ( i +1) = 1;
c1 ( i ) =1;
else
car ( i +1) =0;
end ;
end ;
re =0;
for i =1:16
re = re +( c1 ( i ) *(10^( i -1) ) )
end ;
printf ( The b i n a r y r e p r e s e n t a t i o n o f f i r s t
number i s ) ;
disp ( m ) ;
printf ( The 2 s c o m p l i m e n t o f s e c o n d
nmber i s ) ;
disp ( re ) ;
91

63 a1 = c1 ;
64 ar (1) =0;
65 for i =1:8
66
c1 ( i ) = ar ( i ) + a1 ( i ) + b1 ( i ) ;
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

nmbers ( b i n a r y a d d i t i o n )
if c1 ( i ) == 2 then
ar ( i +1) = 1;
c1 ( i ) =0;
elseif c1 ( i ) ==3 then
ar ( i +1) = 1;
c1 ( i ) =1;
else
ar ( i +1) =0;
end

// a d d i n b o t h t h e
// l o w e r b y t e

end
c1 (9) = ar (9)
re =0;
format ( v ,18) ;
for i =1:8
re = re +( c1 ( i ) *(10^( i -1) ) )
end
printf ( The sum o f l o w e r b y t e s o f two b i n a r y
numbers i s %d\n , re ) ;
printf ( w i t h a c a r r y i s %d\n , ar (9) ) ;
for i =9:16
c1 ( i ) = ar ( i ) + a1 ( i ) + b1 ( i ) ; // u p p e r b y t e
if c1 ( i ) == 2 then
ar ( i +1) = 1;
c1 ( i ) =0;
elseif c1 ( i ) ==3 then
ar ( i +1) = 1;
c1 ( i ) =1;
else
ar ( i +1) =0;
end
end
c1 (17) = ar (17) ;
format ( v ,25) ;
92

99
100
101
102
103
104
105
106
107
108
109
110

ree =0;
for i =9:16
ree = ree +( c1 ( i ) *(10^( i -9) ) ) ;
end
for i =9:16
re = re +( c1 ( i ) *(10^( i -1) ) )
end
printf ( The sum o f u p p e r b y t e s o f t h e g i v e n
numbers i s %d\n , ree ) ;
printf ( w i t h a c a r r y i s %d\n , ar (17) ) ; // d i s p l a y i n g
results
printf ( The t o t a l sum i s ) ;
disp ( re ) ;
printf ( w i t h a c a r r y %d , ar (17) ) ;

## Scilab code Exa 6.12 final carry in a CLA

// exmple 6 . 1 2
clc ;
clear ;
// a=i n p u t ( E n t e r t h e f i r s t number A ( 4 b i t ) : ) ;
// b=i n p u t ( E n t e r t h e f i r s t number B ( 4 b i t ) : ) ;
a =1111; // g i v e n v a l u e s f o r a and b
b =0001;
for i =1:4
a1 ( i ) = modulo (a ,10) ;
a = a /10;
a = round ( a ) ;
b1 ( i ) = modulo (b ,10) ;
b = b /10;
b = round ( b ) ;
end
for i =1:4
// f i n d i n g t h e g e n e r a t e and p r o p a g a t e
v a l u e s f o r ech b i t
17
g ( i ) = bitand ( a1 ( i ) , b1 ( i ) ) ;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

93

18
19
20
21
22
23
24

p ( i ) = bitor ( a1 ( i ) , b1 ( i ) ) ;
end
c (1) =0;
for i =1:4 // f i n d i n g t h e c a r r y
c ( i +1) = bitor ( g ( i ) , bitand ( p ( i ) ,c ( i ) ) ) ;
end
printf ( The f i n a l c a r r y i s C3 = %d , c (5) ) ; //
d i s p l a y i n g the r e s u l t

94

Chapter 7
Clocks and Timing Circuits

## Scilab code Exa 7.1 clock cycle time

1 // e x a m p l e 7 . 1
2 clear ;
3 clc ;
4
// f o r 50 kHz c l o c k
5
clk_frq1 = 50000;
6
c_t_500 = 1000/ clk_frq1 ;
7
// f o r 8MHz c l o c k
8
clk_frq2 = 8000000;
9
c_t_8 = 10000000/ clk_frq2 ;
10
printf ( C y c l e t i m e f o r 500kHz c l o c k
11

i s %f
m i l l i s e c o n d s \n , c_t_500 ) ; // d i s p l a y i n g r e s u l t s
printf ( C y c l e t i m e f o r 8MHz c l o c k i s %f m i c r o
s e c o n d s , c_t_8 ) ;

## Scilab code Exa 7.2 maximum clock frequency

1 // e x a m p l e 7 . 2
2 clc ;

95

3
4
5
6
7
8
9

clear ;
// p r o p d e l a y = i n p u t ( E n t e r t h e p r o p a g a t i o n d e l a y i n
nano s e c o n d s : ) ;
prop_dely =24; // t a k i n g t h e g i v e n i n p u t
format ( v ,18) ; // s e t t i n g t h e p r e c i s i o n
max_clk_frq = 1/ prop_delay ; // making n e c e s s a r y
calculations
max_clk_frq = max_clk_frq *10^3;
printf ( maximum c l o c k f r e q u e n c y i s %f KHz ,
max_clk_frq ) ; // d i s p l a y i n g t h e r e s u l t

## Scilab code Exa 7.3 frequency limits of the clock

1 // e x a m p l e 7 . 3
2 clc ;
3 clear ;
4 //ppm = i n p u t ( E n t e r t h e
5
6
7
8
9
10
11
12
13
14

s t a b i l i t y in parts per

m i l l i o n (PPM) : ) ;
// c l k f r q = i n p u t ( E n t e r t h e c l o c k f r e q u e n c y i n MHz
: );
ppm = 5 // t a k i n g
the given values
clk_frq = 5
mill = clk_frq ; // making n e c e s s a r y c a l c u l a t i o n s
pp = mill * ppm ;
pp = round ( pp ) ;
clk_frq = clk_frq *10^6;
o (1 ,1) = clk_frq - pp ;
o (1 ,2) = clk_frq + pp ;
printf ( The c l o c k f r e q u e n c y w i l l be somewhere
b e t w e e n %d and %d Hz ,o ) ; // d i s p l a y i n g t h e r e s u l t

96

97

## Scilab code Exa 7.4 Schmitt trigger

1 // e x a m p l e 7 . 4
2 // s c h m i t t t r i g g e r i n v e r t e r
3 clear ;
4 clc ;
5 close ;
6 // peak= i n p u t ( E n t e r t h e peak v o l t a g e
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

o f s i n e wave

in volts : ) ;
// utp = i n p u t ( E n t e r t h e u p p e r t r i g g e r p o i n t i n
volts : ) ;
// l t p = i n p u t ( E n t e r t h e l o w e r t r i g g e r p o i n t i n
volts : ) ;
peak =2; // t a k i n g g i v e n v a l u e s f o r i n p u t s
utp =1.7;
ltp =0.9;
for i =1:4000
sinn ( i ) = peak * sin ( i *2*3.1416/1000) ; // d r a w i n g
a s i n wave w i t h g i v e n a m p l i t u d e
end
for j =1:4000
// making c a l c u l a t i o n s t o p l o t
output
if modulo (j ,1000) < 250 then
if sinn ( j ) < utp then
result ( j ) =3.4;
else
result ( j ) =0.2;
end
elseif sinn ( j ) > ltp then
result ( j ) =0.2
else
result ( j ) =3.4;
end
end
subplot (2 ,1 ,1)
plot ( sinn ) ; // p l o t i n g t h e i n p u t and o u t p u t c u r v e s
xlabel ( n ) ;
title ( i n p u t )
98

32
33
34
35
36
37
38

ylabel ( v o l t s ) ;
subplot (2 ,1 ,2) ;
plot ( result , r ) ;
xlabel ( n ) ;
title ( o u t p u t )
ylabel ( v o l t s ) ;

## Scilab code Exa 7.5 frequency of oscillation for 555 timer

1 // e x a m p l e 7 . 5
2 clc
3 clear
4 //Ra = i n p u t ( E n t e r t h e v a l u e o f t h e
5
6
7
8
9
10
11
12

r e s i s t a n c e RA

i n Kohms : ) ;
//Rb = i n p u t ( E n t e r t h e v a l u e o f t h e r e s i s t a n c e RB
i n Kohms : ) ;
//C =i n p u t ( E n t e r t h e v a l u e o f t h e C a p a c i t a n c e C i n
micro f a r a d s : ) ;
Ra =1 // t a k i n g t h e g i v e n i n p u t
Rb =1
C = 1 *10^ -3
T = ( Ra + 2* Rb ) * C ;
frq = 1.44 * (1/ T ) ; // s u b s t i t u t i n g i n t h e e q u t i o n
printf ( F r e q e n c y o f o s c i l l a t i o n i s %f KHz , frq ) ; //
displaying result

## Scilab code Exa 7.6 finding Ra and C in 555 timer circuit

1 // e x a m p l e 7 . 6
2 clc
3 clear

99

4
5
6
7
8
9
10
11
12
13
14
15
16
17

// r b=i n p u t ( E n t e r t h e v a l u e o f t h e r e s i s t a n c e RB i n
Kohms : ) ;
// dc =i n p u t ( E n t e r r e q u i r e d duty c y c l e i n % : ) ;
// c l k = i n p u t ( E n t e r t h e p r o v i d e d c l o c k f r e q u e n c y i n
MHz : ) ;
rb =0.75 // t a k i n g t h e g i v e n v a l u e s f o r i n p u t
dc =25
clk =1
ra = ( rb *100/ dc ) - 2* rb ; // mking n e c c e s a r y
calculations
format ( v ,18) ;
t2 = dc /( clk *10^8) ;
C = t2 /(693* rb )
C = C *10^12;
//C=round (C) ;
printf ( The v a l u e o f RA i s %f Kohms\n , ra ) ; //
d i s p l a y i n g the output
printf ( The v a l u e o f C i s %f p i c o f a r a d s ,C ) ;

Scilab code Exa 7.7 output pulse width for the timer
1 // e x a m p l e 7 . 7
2 clc
3 clear
4 //Ra = i n p u t ( E n t e r t h e v a l u e o f t h e
5
6
7
8
9

r e s i s t a n c e RA

i n Kohms : ) ;
//C =i n p u t ( E n t e r t h e v a l u e o f t h e C a p a c i t a n c e C i n
micro f a r a d s : ) ;
Ra =10 // t a k i n g g i v e n v a l u e s
C =0.1
pw = 1.1* Ra * C // s u b s t i t u t i n g i n t h e e q u a t i o n
printf ( p u l s e w i d t h i s %f m i l l i s e c o n d s , pw ) ; //
displaying result

100

Scilab code Exa 7.8 value of C necessary to change pulse width to given
values
1 // e x a m p l e 7 . 8
2 clc
3 clear
4 //Ra = i n p u t ( E n t e r t h e v a l u e o f t h e
5
6
7
8
9

r e s i s t a n c e RA
i n Kohms : ) ;
//pw =i n p u t ( E n t e r t h e v a l u e o f r e q u i r e d p u l s e w i d t h
in millisecndseconds : ) ;
Ra =10 // t a k i n g g i v e n v a l u e s
pw =10
C = pw /(1.1* Ra ) ; // s u b s t i t u t i n g i n t h e e q u a t i o n
printf ( The r e q u i r e d v a l u e o f c a p a c i t a n c e i s %f
m i c r o f a r a d s ,C ) ; // d i s p l a y i n g r e s u l t

## Scilab code Exa 7.9 monostable multivibrator

1 // e x a m p l e 7 . 9
2 clear
3 clc
4 close
5 //R = i n p u t ( E n t e r t h e v a l u e o f t h e

r e s i s t a n c e R in

Kohms : ) ;
6 //C =i n p u t ( E n t e r t h e v a l u e o f t h e C a p a c i t a n c e C i n
micro f a r a d s : ) ;
7 sp = input ( E n t e r t h e s p a c i n g b e t w e e n two i n p u t
p u l s e s i n micro s e c o n d s : );
101

102

## Figure 7.3: monostable multivibrator

103

R =1; // t a k i n g g i v e v a l u e s
C =0.01;
t = 693* R * C ; // c a l c u l t i n g t i m e c o n s t a n t
tt = t *10;
p =1;
len = sp *60 -1;
q =1;
for j =1: len
// p l o t i n t h e g r a p h s
lo = sp *10;
f = modulo (j , lo ) ;
if f ==0 then
inpu ( j ) =1;
else
inpu ( j ) =0;
end
inpu (1) =1;
o ( j ) =2;
end
while q < len
result ( q ) =0;
q = q +1;
end
while p < len
if inpu ( p ) ==1 then
for k =1: tt
result ( p + k ) =1;
end
p = p + tt ;
else
result ( p ) =0;
p = p +1;
end
end
subplot (2 ,1 ,1) ; // p l o t i n g b o t h e g r a p h s i n same
window
42 plot ( o ) ;
43 plot ( inpu ) ;
44 xlabel ( t i m e X107 s e c o n d s ) ;

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

104

## Figure 7.4: 74123

45
46
47
48
49
50
51
52

ylabel ( Manitude ) ;
title ( i n p u t p u l s e s ) ;
subplot (2 ,1 ,2) ;
plot ( o ) ;
plot ( result ) ;
xlabel ( t i m e X107 s e c o n d s ) ;
ylabel ( Manitude ) ;
title ( Output ) ;

105

106

## Scilab code Exa 7.10 74123

1 // e x a m p l e 7 . 1 0
2 clear
3 clc
4 close
5 //R = i n p u t ( E n t e r t h e v a l u e o f t h e
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

r e s i s t a n c e R in
Kohms : ) ;
//C =i n p u t ( E n t e r t h e v a l u e o f t h e C a p a c i t a n c e C i n
micro f a r a d s : ) ;
sp = input ( E n t e r t h e s p a c i n g b e t w e e n two i n p u t
p u l s e s i n micro s e c o n d s : );
R =10;
C =0.01;
// s p =50;
// s p =10;
t = 330* R * C ; // c a l c u l a t i n g t i m e c o n s t a n t
printf ( The o u t p u t p u l s e w i d t h w i l l be a b o u t %f
m i c r o s e c o n d s ,t ) ;
tt = t *10;
p =1;
len = sp *60+1;
q =1;
for j =1: len
// making a r r a y s t o p l o t t h e g r a p h s
lo = sp *10;
f = modulo (j , lo ) ;
if f ==0 then
inpu ( j ) =1;
else
inpu ( j ) =0;
end
inpu (1) =1;
o ( j ) =5;
end
if sp <40 then
while q <2* len
result ( q ) =0;
q = q +1;
107

33 end
34 else
35 while q <1.2* len
36
result ( q ) =0;
37
q = q +1;
38 end
39 end
40 while p < len
41
if inpu ( p ) ==1 then
42
for k =1: tt
43
result ( p + k ) =3;
44
end
45
p = p +1;
46
else
47
p = p +1;
48
end
49
50
end
51 subplot (2 ,1 ,1) ; // p l o t i n g b o t h e g r a p h s i n same
52
53
54
55
56
57
58
59
60
61
62

window
plot ( o ) ;
plot ( inpu ) ;
xlabel ( t i m e X107 s e c o n d s ) ;
ylabel ( Manitude ) ;
title ( i n p u t p u l s e s ) ;
subplot (2 ,1 ,2) ;
plot ( o ) ;
plot ( result ) ;
xlabel ( t i m e X107 s e c o n d s ) ;
ylabel ( Manitude ) ;
title ( Output ) ;

## Scilab code Exa 7.11 finding timing capacitor values

1

// e x a m p l e 7 . 1 1
108

2 clc
3 clear
4 //R = i n p u t ( E n t e r t h e v a l u e o f t h e
5
6
7
8
9
10
11

r e s i s t a n c e R in

Kohms : ) ;
//pw =i n p u t ( E n t e r t h e v a l u e o f r e q u i r e d p u l s e w i d t h
in millisecndseconds : ) ;
R =0.5; // t k i n g t h e g i v e n v a l u e s
format ( v ,18) ;
pw = 1 * 10^ -3;
C = pw /(0.33* R ) ; // c a l c u l a t i n g C
printf ( The r e q u i r e d v a l u e o f c a p a c i t a n c e i s %f
m i c r o f a r a d s \n ,C ) ;
printf ( The p u l s e d e l a y c a p a c i t o r i s %f m i c r o f a r a d s
,(2* C ) ) ;

109

Chapter 8
Flip Flops

## Scilab code Exa 8.4 RS flipflop

1 // e x a m p l e 8 . 4
2 clc ;
3 clear ;
4 en = input ( E n t e r t h e e n a b l e i n p u t

l e v e l (1 or 0) : )

;
5 r = input ( e n t e r t h e R i n p u t

l e v e l ( 1 o r 0 ) : ) ; //
a c c e p t i n g t h e i n p u t s from t h e u s e r
6 s = input ( e n t e r t h e S i n p u t l e v e l ( 1 o r 0 ) : ) ;
7 qn = input ( E n t e r t h e p r e v i o u s o u t p u t v a l u e ( 1 o r 0 ) :
);
8
9 if en == 0 then // c l c u l a t i n g t h e o u t p u t
10
op = qn ;
11 elseif ( s ==0 & r ==0) then
12
op = qn ;
13 elseif ( s ==1& r ==1) then
14
disp ( The i n p u t s a r e i l l e g a l ) ;
15
return ;
16 else
17
op = s ;
18

110

19 end
20
21 printf ( \n \ n o u t p u t (Qn+1) = %d , op ) ; // d i s p l a y i n g

the output

## Scilab code Exa 8.5 positive edge triggred RS flip flop

1 // e x a m p l e 8 . 5
2 clc ;
3 clear ;
4 disp ( Here what h a p p e n s a t e a c h p o i n t i n t i m e ) ;
5 disp ( Time
t 0 : S = 0 , R = 0 , no c h a n g e i n Q (Q
6
7
8
9
10

remains 0) );
disp ( Time t 1 : S = 1 , R = 0 , Q c h a n g e s from 0 t o 1
);
disp ( Time t 2 : S = 0 , R = 1 , Q r e s e t s t o 0 ) ;
disp ( Time t 3 : S = 1 , R = 0 , Q s e t s t o 1 ) ;
disp ( Time t 4 : S = 0 , R = 0 , no c h a n g e i n Q (Q
remains 1) );
disp ( N o t i c e t h a t e i t h e r R o r S , o r both , a r e
a l l o w e d t o c h a n g e s t a t e a t any time , w h e t h e r C i s
h i g h o r low . The o n l y t i m e b o t h R and S must be
s t a b l e ( u n c h a n g i n g ) i s d u r i n g t h e s h o r t PTs o f
the c l o c k . );

## Scilab code Exa 8.6 negative edge triggred RS flip flop

1 // e x a m p l e 8 . 6
2 clc ;
3 clear ;
4 disp ( Here what h a p p e n s a t e a c h p o i n t i n t i m e ) ;
5 disp ( Time
t 0 : S = 0 , R = 0 , no c h a n g e i n Q (Q

remains 0) );
111

6
7
8
9
10

## disp ( Time t 1 : S = 1 , R = 0 , Q c h a n g e s from 0 t o 1

);
disp ( Time t 2 : S = 0 , R = 1 , Q r e s e t s t o 0 ) ;
disp ( Time t 3 : S = 1 , R = 0 , Q s e t s t o 1 ) ;
disp ( Time t 4 : S = 0 , R = 0 , no c h a n g e i n Q (Q
remains 1) );
disp ( N o t i c e t h a t e i t h e r R o r S , o r both , a r e
a l l o w e d t o c h a n g e s t a t e a t any time , w h e t h e r C i s
h i g h o r low . The o n l y t i m e b o t h R and S must be
s t a b l e ( u n c h a n g i n g ) i s d u r i n g t h e s h o r t NTs o f
the c l o c k . );

## Scilab code Exa 8.7 T flip flop

1 // e x a m p l e 8 . 7
2 clc ;
3 clear ;
4 close ;
5 printf ( For i n p u t J and K = 0 o t p u t Qn+1 = Qn i . e

o u t p u t d o e s n o t c h a n g e i t s s t a t e And f o r J = K =
1 , The Output Qn+1 = Qn i . e o u t p u t t o g g l e s ) ;

## Scilab code Exa 8.9 JK master slave

1 // e x a m p l e 8 . 9
2 clc
3 close
4 clear
5 disp ( s i n c e J=K=1 , t h e

f l i p f l o p s i m p l y t o g g l e s e c h
t i m e t h e c l o c k g o e s low , The waveform a t Q h a s a
p e r i o d t w i c e o f t h a t o f t h e wavefrm . I n o t h e r
112

## Figure 8.1: JK master slave

113

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

words , t h e f r e q u e n c y o f Q i d o n l y oneh a l f o f
that o f . This c i r c u i t a c t s as a frequency
d i v i d e r t h e o u t p u t f r e q u e n c y d i v i d e by 2 . Note
t h a t Q c h a n g e s s t a t e on NTs o f t h e c l o c k . The
w a v e f o r m s a r e a s shown i n t h e f i g u r e ) ;
t =50; // t a k e n t i m e p e r i o d
p =1;
while p < t *10 // t k i n g v a l u e s f o r p l o t i n g t h e g r a p h
if p ==1 | modulo (p , t ) ==0 then
for k =1: t /2
cin ( p + k ) =0;
end
p = p + t /2;
else
cin ( p ) =1;
p = p +1;
end
end
t =100;
p =1;
while p < t *5
if p ==1 | modulo (p , t ) ==0 then
for k =1: t /2
dout ( p + k ) =0;
end
p = p + t /2;
else
dout ( p ) =1;
p = p +1;
end
end
y =[3 3];
subplot (2 ,1 ,1) // p l o t i n b o t h t h e p l o t s i n a s i n g l e
window
title ( i n p u t a t p i n C )
plot ( cin )
plot ( y )
subplot (2 ,1 ,2)
114

38
39
40

title ( o u t put a t p i n D )
plot ( dout )
plot ( y )

## Scilab code Exa 8.10 fictitious flip flop excitation table

1 // Example 8 . 1 0
2 clc
3 clear
4 close
5 b =[0 1 0 1 0 1 0 1]; // g i v e n t r u t h
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

t b l e f o r the
fictitious flip flop
a = [0 0 1 1 0 0 1 1];
qn = [0 0 0 0 1 1 1 1];
for i =1:8
if ( a ( i ) ==0 & b ( i ) ==0) then
qn1 ( i ) = 0;
end ;
if ( a ( i ) ==1 & b ( i ) == 1) then
qn1 ( i ) = 1 ;
end ;
if ( a ( i ) ==1 & b ( i ) == 0) then
qn1 ( i ) = bitcmp ( qn ( i ) ,1) ;
end ;
if ( a ( i ) ==0 & b ( i ) == 1) then
qn1 ( i ) = ( qn ( i ) ) ;
end ;
end ;
for i = 1 : 8
// p r i n t i n t h e t r u t h t a b l e
Y (i ,1) = qn ( i ) ;
Y (i ,2) = a ( i ) ;
Y (i ,3) = b ( i ) ;
Y (i ,4) = qn1 ( i ) ;
end
disp ( The g i v e n t r u t h t a b l e
i s : );
115

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

disp ( Qn
A
B
Qn+1 ) ;
disp ( Y ) ;
disp ( The t r a n s i t i o n s a r e shown b e l o w ) ;
c1 =0;
c2 =0;
c3 =0;
c4 =0;
for j =1:8 // c h e c k i n g a l l p o s s i b l e c a s e s t o make a
transition table
if ( qn ( j ) ==0) then
if ( qn1 ( j ) == 0) then
if ( c1 ==0) then
disp ( t r a n s i t i o n from 0 > 0 ) ;
disp (
A
B );
c1 =1;
end ;
disp ( Y (j ,2:3) ) ;
end ;
end ;
end ;
for j =1:8
if ( qn ( j ) ==0) then
if ( qn1 ( j ) == 1) then
if ( c2 ==0) then
disp ( t r a n s i t i o n from 0 > 1 ) ;
disp (
A
B );
c2 =1;
end ;
disp ( Y (j ,2:3) ) ;
end ;
end ;
end ;
for j =1:8
if ( qn ( j ) ==1) then
if ( qn1 ( j ) == 0) then
if ( c3 ==0) then
disp ( t r a n s i t i o n from 1 > 0 ) ;
disp (
A
B );
116

66
c3 =1;
67
end ;
68
disp ( Y (j ,2:3) ) ;
69
end ;
70
end ;
71 end ;
72 for j =1:8
73
if ( qn ( j ) ==1) then
74
if ( qn1 ( j ) == 1) then
75
if ( c4 ==0) then
76
disp ( t r a n s i t i o n from 1 > 1 ) ;
77
disp (
A
B );
78
c4 =1;
79
end ;
80
disp ( Y (j ,2:3) ) ;
81
end ;
82
end ;
83 end ;

Scilab code Exa 8.12 state transition diagram for given circuit
1
2
3
4
5
6
7
8
9
10
11
12
13
14

// e x a m p l e 8 . 1 2
clc ;
clear ;
close ;
qn =[0 ,0 ,1 ,1];
x =[0 ,1 ,0 ,1];
for i =1:4 // c a l c u l a t i n g Y f o r a l l p o s s i b l e c a s e s
d ( i ) = bitxor ( x ( i ) , qn ( i ) ) ;
qn1 ( i ) = d ( i ) ;
y ( i ) = bitand ( x ( i ) , bitcmp ( qn ( i ) ,1) ) ;
end ;
for i = 1 : 4 // d i s p l a y i n g t h e s t a t e t a b l e
Y (i ,1) = qn ( i ) ;
117

15
16
17
18
19
20
21
22

Y (i ,2) = x ( i ) ;
Y (i ,3) = d ( i ) ;
Y (i ,4) = qn1 ( i ) ;
Y (i ,5) = y ( i ) ;
end
disp ( The s t a t e t a b l e
i s : );
disp (
Qn
X
D
Qn+1
disp ( Y ) ;

Y );

## Scilab code Exa 8.13 D flip flop to RS flip flop

1 // e x a m p l e 8 . 1 3
2 clc ;
3 clear ;
4 disp ( For SR f l i p

f l o p Qn+1 = S + R Qn and f o r D
f l i p f l o p Qn+1 = D . ) ;
5 disp ( Thus w i t h D = S + R Qn we g e t c i r c u i t which
b e h a v e s l i k e SR f l i p f l o p . ) ;

118

Chapter 9
Registers

## Scilab code Exa 9.1 shift register serial input

1 // e x a m p l e 9 . 1
2 clc ;
3 clear ;
4 close ;
5 // s = i n p u t ( E n t e r t h e number t o be
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

serially
s h i f t e d in to the s h i f t r e g i s t e r ) ;
s =0100; // g i v e n s e r i a l i n p u t
for i = 4: -1:1
se ( i ) = modulo (s ,10) ;
s = s /10;
s = round ( s ) ;
end
se ( i +4) =0;
k =0;
for i = 2:6 // making s t a t e t a b l e
clk ( k +1) = k ;
q ( i ) = se (i -1) ;
if i >1 then
r ( i ) = q (i -1) ;
else
r ( i ) =0;
119

21
end ;
22
if i >2 then
23
s ( i ) = r (i -1) ;
24
else
25
s ( i ) =0;
26
end ;
27
if i >3 then
28
t ( i ) = s (i -1) ;
29
else
30
t ( i ) =0;
31
end ;
32
k = k +1;
33 end
34 for i = 1 : 5 // p r i n t i n g t h e s t a t e
35
Y (i ,1) = clk ( i ) ;
36
Y (i ,2) = se ( i ) ;
37
Y (i ,3) = q ( i ) ;
38
Y (i ,4) = r ( i ) ;
39
Y (i ,5) = s ( i ) ;
40
Y (i ,6) = t ( i ) ;
41
end
42 disp ( The s t a t e t a b l e
i s : );
43 disp ( C l o c k
Input
Q
R
S
44 disp ( Y ) ;

table

T );

Scilab code Exa 9.2 shift register serial input and output graph
1 // e x a m p l e 9 . 2
2 clc
3 clear
4 close
5 t1 =100; // c l o c k p e r i o d
6 s =0100; // g i v e n s e r i a l i n p u t

120

## Figure 9.1: shift register serial input and output graph

121

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

for i = 4: -1:1
se ( i ) = modulo (s ,10) ;
s = s /10;
s = round ( s ) ;
end
se ( i +4) =0;
k =0;
for i = 2:6 // i n i t i a l l y making a s t a t e t a b l e
clk ( k +1) = k ;
q ( i ) = se (i -1) ;
if i >1 then
r ( i ) = q (i -1) ;
else
r ( i ) =0;
end ;
if i >2 then
s ( i ) = r (i -1) ;
else
s ( i ) =0;
end ;
if i >3 then
t ( i ) = s (i -1) ;
else
t ( i ) =0;
end ;
k = k +1;
end
for m =1:5
// d r a w i n g t h e g r a p h
if ( se ( m ) ==1) then
v = (( m -1) .* t1 )
for u = 1: t1
se1 ( u + v ) =1;
end
else
v = (( m -1) * t1 )
for u = 1: t1
se1 ( u + v ) =0;
end
122

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

end ;
if ( q ( m ) ==1) then
v = (( m -1) .* t1 )
for u = 1: t1
q1 ( u + v ) =1;
end
else
v = (( m -1) * t1 )
for u = 1: t1
q1 ( u + v ) =0;
end
end ;
if ( r ( m ) ==1) then
v = (( m -1) .* t1 )
for u = 1: t1
r1 ( u + v ) =1;
end
else
v = (( m -1) * t1 )
for u = 1: t1
r1 ( u + v ) =0;
end
end ;
if ( s ( m ) ==1) then
v = (( m -1) .* t1 )
for u = 1: t1
s1 ( u + v ) =1;
end
else
v = (( m -1) * t1 )
for u = 1: t1
s1 ( u + v ) =0;
end
end ;
if ( t ( m ) ==1) then
v = (( m -1) .* t1 )
for u = 1: t1
123

83
t11 ( u + v ) =1;
84
end
85
else
86
v = (( m -1) * t1 )
87
for u = 1: t1
88
t11 ( u + v ) =0;
89
end
90
end ;
91
end ;
92 p =1;
93 while p < t1 *5
94
if p ==1 | modulo (p , t1 ) == 1 then
95
for k =1: t1 /2
96
cin ( p + k ) =0;
97
end
98
p = p + t1 /2;
99
else
100
cin ( p ) =1;
101
p = p +1;
102
end
103 end
104 y =[3 3];
105 subplot (6 ,1 ,1)
// making s u b p l o t s t o draw a l l
106
107
108
109
110
111
112
113
114
115
116
117
118
119

g r a p h s i n a s i n g l e window
title ( C l o c k )
plot ( cin )
plot ( y )
subplot (6 ,1 ,2)
title ( S e r i a l i n p u t )
plot ( se1 )
plot ( y )
subplot (6 ,1 ,3)
title ( Q )
plot ( q1 )
plot ( y )
subplot (6 ,1 ,4)
title ( R )
plot ( r1 )
124

120
121
122
123
124
125
126
127
128

plot ( y )
subplot (6 ,1 ,5)
title ( S )
plot ( s1 )
plot ( y )
subplot (6 ,1 ,6)
title ( T )
plot ( t11 )
plot ( y )

## Scilab code Exa 9.4 54164 shift register

// e x a m p l e 9 . 4
clc ;
clear ;
// b= i n p u t ( E n t e r t h e number o f b i t s : ) ;
// c= i n p u t ( E n t e r t h e c l o c k f r e q u e n c y i n Mhz : ) ;
b = 8; // g i v e n v a l u e s
c =10;
t = 1000/ c ;
printf ( One c l o c k p e r i o d t a k e s %d n s \n ,t ) ; //
d i s p l y i n g the r e s u l t s
10 tt = t * b ;
11 printf ( Time r e q u i r e d by t o t a l b i t s r e q u i r e d i s %d
n s , tt ) ;
1
2
3
4
5
6
7
8
9

## Scilab code Exa 9.5 54164 shift register

1 // e x a m p l e 9 . 5
2 clc ;
3 clear ;
4 close ;
5 // c= i n p u t ( E n t e r t h e c l o c k

125

f r e q u e n c y i n Mhz : ) ;

6 c =10; // g i v e n c l o c k f r e q u e n c y
7 t = 1000/ c ;
8 printf ( The d a t a must be s t a b l e

f o r 30 n s \n ) ;

//

displaying results
9 tc = t -30;
10 printf ( The d a t a may be c h a n g i n g i n %d n s , tc ) ;

## Scilab code Exa 9.8 74ls174

1 // e x a p l e 9 . 8
2 clc ;
3 clear ;
4 // s=i n p u t ( E n t e r t h e s e t u p t i m e i n n s : ) ;
5 // h=i n p u t ( E n t e r t h e h o l d t i m e i n n s : ) ;
6 s =20;
// g i v e n i n p u t v a l u e s
7 h =5;
8 printf ( The d a t a i n p u t l e v e l s must be h e l d s t e a d y

f o o r a minimum o f %d n s ,( s + h ) ) ;
the r e s u l t s

// d i s p l a y i n

## Scilab code Exa 9.9 7495A

1 // e x a m p l e 9 . 9
2 clc ;
3 clear ;
4 disp ( The mode c o n t r o l

l i n e must be h i g h . The d a t a
l i n e s must be s t a b l e f o r more t h a n 10 n s p r i o r t o
t h e c l o c k NTs . I f t h e c l o c k i s s t o p p e d a f t e r
the t r a n s i t i o n time T, the l e v e l s n the input
d a t a l i n e s may be c h a n g e d . However , i f t h e c l o c k
i s n o t s t o p p e d , t h e i n p u t d a t a l e v e l s must be
mainted . )

126

Chapter 10
Counters

## Scilab code Exa 10.1 ripple counter clock frequency

1 // e x a m p l e 1 0 . 1
2 clc ;
3 clear ;
4 // c= i n p u t ( E n t e r t h e p e r i o d

micro seconds :

o f t h e waveform a t C i n

) ;

5 c =24; // g i v e n p e r i o d o f waveform
6 clk = c /8;
7 clkf = 1/( clk *10^ -3) ;
8 printf ( The c l o c k p e r i o d i s %f m i c r o s e c o n d s \n , clk
9

) ; // d i s p l a y i n g t h e r e s u l t s
printf ( The c l o c k f r e q u e n c must be %f

KHz , clkf ) ;

Scilab code Exa 10.2 number of flip flops required to construct a counter
1 // e x a m p l e 1 0 . 2
2 clc ;
3 clc
4 c =128; // g i v e n c o u n t e r s

127

## Figure 10.1: Output waveforms for a 7493A connected as a mod 16 counter

5
6
7
8
9
10
11
12
13

d =32;
e =64;
fc = log2 ( c ) ; // making n e c e s s a r c a l c u l a t i o n s
fd = log2 ( d ) ;
printf ( A mod128 c o n t e r s h o u l d have %d f l i p f l o p s \n
, fc ) ;
printf ( A mod32 c o n t e r s h o u l d have %d f l i p f l o p s \n
, fd ) ; // d i s p l a y i n g t h e r e s u l t s
fe = log2 ( e ) ;
n =2^ fe - 1 ;
printf ( The l a r g e s t d e c i m a l no t h a t can be s t o r e d
i n a mod64 c o u n t e r i s %d ,n ) ;

128

Scilab code Exa 10.3 Output waveforms for a 7493A connected as a mod
16 counter
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

clc ;
clear ;
close ;
t =320;
p =1;
while p < t *1 // making a r r a y s f o r p l o t i n g
if p ==1 | modulo (p , t ) ==0 then
for k =1: t /2
qd ( p + k ) =0;
end
p = p + t /2;
else
qd ( p ) =1;
p = p +1;
end
end
t =160;
p =1;
while p < t *2
if p ==1 | modulo (p , t ) ==0 then
for k =1: t /2
qc ( p + k ) =0;
end
p = p + t /2;
else
qc ( p ) =1;
p = p +1;
end
end
t =80;
p =1;
129

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

while p < t *4
if p ==1 | modulo (p , t ) ==0 then
for k =1: t /2
qb ( p + k ) =0;
end
p = p + t /2;
else
qb ( p ) =1;
p = p +1;
end
end
t =40;
p =1;
while p < t *8
if p ==1 | modulo (p , t ) ==0 then
for k =1: t /2
qa ( p + k ) =0;
end
p = p + t /2;
else
qa ( p ) =1;
p = p +1;
end
end
t =20;
p =1;
while p < t *16
if p ==1 | modulo (p , t ) ==0 then
for k =1: t /2
clk ( p + k ) =0;
end
p = p + t /2;
else
clk ( p ) =1;
p = p +1;
end
end
for i =320:350
130

70
clk ( i ) =0;
71
qa ( i ) =0;
72
qb ( i ) =0;
73
qc ( i ) =0;
74
qd ( i ) =0;
75 end ;
76 y =[3 3]; // p l o t i n g t h e g r a p h s
77 subplot (5 ,1 ,1)
78 title ( C l o c k )
79 plot ( clk )
80 plot ( y )
81 subplot (5 ,1 ,2)
82 title ( Qa )
83 plot ( qa )
84 plot ( y )
85 subplot (5 ,1 ,3)
86 title ( Qb )
87 plot ( qb )
88 plot ( y )
89 subplot (5 ,1 ,4)
90 title ( Qc )
91 plot ( qc )
92 plot ( y )
93 subplot (5 ,1 ,5)
94 title ( Qd )
95 plot ( qd )
96 plot ( y )

Scilab code Exa 10.5 Expression for AND gate connected to the leg of
OR gate that drives clock input to flip flop Qd in 74193
1 // e x a m p l e 1 0 . 5
2 clc ;
3 clear ;
4 printf ( The c o r r e c t

e x p r e s s i o n i s : ( count up c l o c k )
131

( Qa ) (Qb) ( Qc ) ) ;

Scilab code Exa 10.6 Expression for 4 input AND gate connected to the
leg of OR gate that conditions the J and K inputs to the Qd flip flop in a
74191
1 // e x a m p l e 1 0 . 6
2
3 clc ;
4 clear ;
5 printf ( The c o r r e c t

l o g i c e x p r e s s i o n i s : ( downup )
( Qa ) (Qb) ( Qc ) ( e n a b l e ) ) ;

Scilab code Exa 10.7 number of flip flops required to construct a counter
1 // e x a m p l e 1 0 . 7
2 clc ;
3 clear ;
4 mod = input ( E n t e r t h e n v a l u e i n y o u r d e s i r e d modn

c o u n t e r : ) ; // t a k i n g t h e i n p u t
5 m = mod ;
6 while 1
7
n = log2 ( mod ) ; // c h e c k i n g w h e t h e r t h e g i v e n number
8
9
10

11
12
13
14

i s a power o f 2
k = modulo (n ,1) ;
if k ==0 then
printf ( The number o f f l i p f l o p s u s e d i n mod%d
c o u n t e r a r e : ,m ) ; // i f y e s t h e p r i n t t h
outpu .
printf ( %d ,n ) ;
return ;
end
mod = mod +1;
132

15

end

Scilab code Exa 10.8 what modulus counters can be constructed with given
number of flip flops e
1
2
3
4
5
6
7
8
9
10
11
12

// e x a m p l e 1 0 . 8
clc ;
clear ;
// f f = i n p u t ( E n t e r t h e no o f f l i p f l o p s : ) ;
ff =4; // g i v e n i n p u t
k =2^ ff ;
if ( k ==2) then // o u t p u t d i s p l a y
printf ( With g i v e n f l i p f l o p we can o n l y c o u n t 2 ,
we can have a modulus 2 c o u n t e r ) ;
else
printf ( With g i v e n number o f f l i p f l o p s t h e c o u n t e r
w i l l have a n a t u r a l c o u n t o f %d\n ,k ) ;
printf ( We can t h u s c o n s t r u c t any c o u n t e r t h a t h a s a
modulus b e t w e e n %d and 2 ,k )
end ;

## Scilab code Exa 10.9 mod 6 counter

1 // e x a m p l e 1 0 . 9
2
3 clc ;
4 clear ;
5 close ;
6 c = [0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1

0]; // t a k i n g t h e v a l u e s f o r a mod 6 c o u n t e r
133

## Figure 10.2: mod 6 counter

134

7 q = [0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1

0];
8 a = [0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0

0];
9 b = [0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1

0];
y1 = q ;
y2 = a ;
y3 = b ;
y11p =1;
y22p =1;
y33p =1;
y44p =1;
cp =1;
yf1p =1;
for i =1:25
// making a r r a y s t o draw t h e o u t p u t
if y1 ( i ) ==1 then
for o =1:100
y11 ( y11p ) =1;
y11p = y11p +1;
end
else
for o =1:100
y11 ( y11p ) =0;
y11p = y11p +1;
end

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 end
32 if y2 ( i ) ==1 then
33
for o =1:100
34
y21 ( y22p ) =1;
35
y22p = y22p +1;
36
end
37
else
38
for o =1:100
39
y21 ( y22p ) =0;
40
y22p = y22p +1;
41
end

135

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

end
if y3 ( i ) ==1 then
for o =1:100
y31 ( y33p ) =1;
y33p = y33p +1;
end
else
for o =1:100
y31 ( y33p ) =0;
y33p = y33p +1;
end
end
if c ( i ) ==1 then
for o =1:100
c1 ( cp ) =1;
cp = cp +1;
end
else
for o =1:100
c1 ( cp ) =0;
cp = cp +1;
end
end
end
z =[2 2];
subplot (4 ,1 ,1) ; // p l o t i n g t h e o u t put
title ( Timing Diagram ) ;
plot ( c1 ) ;
plot ( z ) ;
ylabel ( C ) ;
subplot (4 ,1 ,2) ;
plot ( y11 ) ;
ylabel ( Q ) ;
plot ( z ) ;
subplot (4 ,1 ,3) ;
136

80
81
82
83
84
85
86

plot ( y21 ) ;
ylabel ( A ) ;
plot ( z ) ;
subplot (4 ,1 ,4) ;
plot ( z ) ;
ylabel ( B ) ;
plot ( y31 ) ;

Scilab code Exa 10.10 Expression for a gate to decode count 8 in a 7492A
1 // e x a m p l e 1 0 . 1 0
2 clc ;
3 clear ;
4 printf ( The c o r r e c t

e x p r e s i o n i s 8 = Qd Qc Qb

Qa ) ;

## Scilab code Exa 10.12 mod 12 counter

1 // e x a m p l e 1 0 . 1 2
2 clc
3 clear
4 // p r o= ( E n t e r t h e v a l u e t o w h i c c o u n t e r s h o u l d
5
6
7
8
9
10
11
12
13

progress : ) ;
pro =11; // g i v e n i n p u t
q =1;
aa = pro ;
for i =1:4
// c o n v e r t i n g t h e g i v e n number
in to binary
x = modulo ( aa ,2) ;
b(q)=x;
aa = aa /2;
aa = floor ( aa ) ;
q = q +1;
137

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

end
bi = ;
// t h e n p r i n t i n g t h e NAND g a t e i n p u t s
for i =1:4
if i ==1 & b ( i ) ==1 then
bi = strcat ([ bi Qa ]) ;
elseif i ==1 & b ( i ) == 0 ;
bi = strcat ([ bi Qa ]) ;
end
if i ==2 & b ( i ) ==1 then
bi = strcat ([ bi Qb ]) ;
elseif i ==2 & b ( i ) == 0 ;
bi = strcat ([ bi Qb ]) ;
end
if i ==3 & b ( i ) ==1 then
bi = strcat ([ bi Qc ]) ;
elseif i ==3 & b ( i ) == 0 ;
bi = strcat ([ bi Qc ]) ;
end
if i ==4 & b ( i ) ==1 then
bi = strcat ([ bi Qd ]) ;
elseif i ==4 & b ( i ) == 0 ;
bi = strcat ([ bi Qd ]) ;
end
end
disp ( The NAND g a t e i n p u t s must be : ) ;
disp ( bi )

## Scilab code Exa 10.13 4 bit binary counter presettable

1 // e x a m p l e 1 0 . 1 3
2 clc ;
3 clear
4 // p r e=i n p u t ( E n t e r t h e number where t h e c o u n t e r

138

is

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

p r e s e t ) ;
pre = 1001; // g i v e n p r e s e t v a l u e
q =1;
b =0;
f =0;
a = pre ;
while (a >0) // c o n v e r t i n g t o d e c i m a l
r = modulo (a ,10) ;
b (1 , q ) = r ;
a = a /10;
a = floor ( a ) ;
q = q +1;
end
for m =1: q -1
c =m -1
f = f + b (1 , m ) *(2^ c ) ;
end
disp ( The c o u n t e r w i l l c o u n t down t o 15 , Then
p r e s e t back t o %d , The r e s u l t i n g s t a t e d i a g r a m i s
shown b e l o w ) ;
for k =1:3
for i =9: -1:0 // t h i s w i l l p r i n t t h e s t a t e s
printf ( %d ,i ) ;
end ;
printf ( 15
);

22
23
24
25
26
27
28 end ;

## check Appendix AP 8 for dependency:

kmap3.sci
check Appendix AP 2 for dependency:
noof.sci
check Appendix AP 3 for dependency:
noof0.sci
139

## Scilab code Exa 10.14 self correcting modulo 6 counter

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

// e x a m p l e 1 0 . 1 4
// t h i s program u s e s t h e f o l l o w i n g f u n c t i o n s
// kmap3 . s c i
// n o o f . s c i and n o o f 0 . s c i
// t h e a b o v e p r o g r a m s s h o u l d be e x e c u t e d b e f o r e
e x e c u t i n g t h e s e programs
clc ;
n = [ 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 i = 1 : 5
n1 (i ,:) = n ( i +1 ,:)
end
for i =6:8
n1 (i ,:) =[0 0 0]
end ;
p =1;
for i = 1:3 // making t h e s t a t e t a b l e
for j = 1:8
if n (j , i ) == 0
jf (j , p ) = n1 (j , i ) ;
jf (j , p +1) = 2;
elseif n (j , i ) == 1
jf (j , p ) =2;
jf (j , p +1) = bitcmp ( n1 (j , i ) ,1) ;
end
end
140

32
p = p +2
33 end ;
34 disp ( S t a t e
35
36
37
38
39

t a b e l f o r mod 6 c o u n t e r : ) ; // d i s p l a y i n g
the s t a t e t a b l e
di = [ n n1 jf ];
disp (
Cn
Bn
An
Cn1
Bn1
An1
Jc
Kc
Jb
Kb
Ja
Ka ) ;
disp ( di ) ;
disp ( Here 2 r e p r e s e n t s a don t c a r e c o n d i t i o n
);
disp ( These b e l o w Karnaugh maps g i v e t h e d e s i g n
e q u a t i o n s );

40
41 jc =[0 0 1 0;2 2 2 2] // Krnaugh Maps f o r t h e d e s i g n

equations
42 JC = kmap3 ( jc ) ;
// c a l l i n g t h e 3 v a r i a b l e kmap
43 printf ( \n\nJC = %s \n , JC ) ; // d i s p l a y i n g t h e r e s u l t
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63

kc =[2 2 2 2;0 1 1 1]
KC = kmap3 ( kc ) ; // c a l l i n g t h e 3 v a r i a b l e kmap
printf ( \n\nKC = %s \n , KC ) ; // d i s p l a y i n g t h e r e s u l t
jb =[0 1 2 2;0 0 2 2]
JB = kmap3 ( jb ) ; // c a l l i n g t h e 3 v a r i a b l e kmap
printf ( \n\nJB = %s \n , JB ) ; // d i s p l a y i n g t h e r e s u l t
kb =[2 2 1 0;2 2 1 1]
KB = kmap3 ( kb ) ; // c a l l i n g t h e 3 v a r i a b l e kmap
printf ( \n\nKB = %s \n , KB ) ; // d i s p l a y i n g t h e r e s u l t
ja =[1 2 2 1;1 2 2 0]
JA = kmap3 ( ja ) ; // c a l l i n g t h e 3 v a r i a b l e kmap
printf ( \n\nJA = %s \n , JA ) ; // d i s p l a y i n g t h e r e s u l t
ka =[2 1 1 2;2 1 1 2]
KA = kmap3 ( ka ) ; // c a l l i n g t h e 3 v a r i a b l e kmap
printf ( \n\nKA = %s \n , KA ) ; // d i s p l a y i n g t h e r e s u l t

141

kmap2.sci

## Scilab code Exa 10.15 sequence generator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

// e x a m p l e 1 0 . 1 5
// t h i s program u s e kmap2 . s c i
// kmap2 . s c i s h o u l d be e x e c u t e d b e f o r e e x e c u t i n g t h i s
program
clc ;
a = [0 0 1 1]
b = [0 1 0 1]
y = [1 1 0 1]
k = [1 1 ; 0 1]
bi = kmap2 ( k ) ; // c a l l i n g 2 v a r i a b l e kmap
disp (
A
B
Y ) ;
for i =1:3
Y (i ,1) = a ( i ) ;
Y (i ,2) = b ( i ) ;
Y (i ,3) = y ( i ) ;
end
disp ( Y ) ;
disp ( The m i n i m i s e d e x p r e s s i o n from k a r n a u g h map i s
) ; // d i s p l a y i n g t h e r e s u l t
disp ( bi ) ;

142

Chapter 11
Design of Sequential Circuit
check Appendix AP 6 for dependency:
kmap3a.sci
check Appendix AP 2 for dependency:
noof.sci
check Appendix AP 3 for dependency:
noof0.sci

## Scilab code Exa 11.1 synchronous sequential logic circuit

1 // e x a m p l e 1 1 . 1
2 clc ;
3 // t h i s program r e q u i r e s
4 // kmap3a . s c i
t o f i n d t h e kmap
5 // n o o f . s c i f u n c t i o n u s e d i n s i d e kmap
6 // n o o f 0 . s c i f u n c t i o n u s e d i n s i d e kmap
7 n = [ 0 0 0;
8
0 0 1;
9
0 1 0;
10
0 1 1;

143

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

1
1
1
1

0 0;
0 1;
1 0;
1 1];
for i = 1:8
// p r i n t i n g t h e s t a t e s y n t h e s i s
table
an1 (i ,1) = n (i ,3) ;
dn (i ,1) = n (i ,3) ;
if n (i ,1) ==1 & n (i ,2) ==1 & n (i ,3) ==0 then
z (i ,1) =1;
else
z (i ,1) =0;
end

end ;
dis =[ n an1 dn z ];
disp ( S t a t e S y n t h e s i s t a b l e : ) ;
disp (
An
X
Y
An1
Dn
disp ( dis ) ;
printf ( \n\n D e s i g n e q u a t i o n s : \ n ) ;
Dn = [ 0 1 1 0;0 1 1 0];
Z = [ 0 0 0 0;0 0 0 1];
dn1 = kmap3a ( Dn ) ;
t h e 3 v a r i b l e kmap o f Dn
32 printf ( \n
Dn = %s \n\n , dn1 ) ;
the minimized e x p r e s s i o n
33 z1 = kmap3a ( Z ) ;
t h e 3 v a r i a b l e kmap o f Z
34 printf ( \n
Z = %s \n\n , z1 ) ;
the minimized e x p r e s s i o n
check Appendix AP 4 for dependency:
donkmapij.sci
check Appendix AP 5 for dependency:
noof1.sci

144

Z );

// f i n d i n g
// d i s p l a y i n g
// f i n d i n g
// d i s p l a y i n g

## Scilab code Exa 11.2 vending machine

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

// e x a m p l e 1 1 . 2
// t h i s c o d e n e e d s
// d o n k m a p i j . s c i // f u n c t i o n t o m i n i m i z e g i v e n
e x p r e s s i o n u s i n g a kmap
// n o o f 1 . s c i
// a b o v e two s h o u l b be e x e c u t e d b e f o r e e x e c u t i n g t h i s
code
clc ;
tt =[0 0 0 0 0 0 0 0 0 0; // g i v e n s t a t e s y n t h e s i s
table
0 0 0 1 0 0 0 0 0 0;
0 0 1 0 0 1 0 0 0 1;
0 0 1 1 1 0 0 0 1 0;
0 1 0 0 0 1 0 0 0 1;
0 1 0 1 0 1 0 0 0 1;
0 1 1 0 1 0 0 0 1 0;
0 1 1 1 0 0 1 0 0 0;
1 0 0 0 1 0 0 0 1 0;
1 0 0 1 1 0 0 0 1 0;
1 0 1 0 0 0 1 0 0 0;
1 0 1 1 0 0 1 1 0 0];
disp ( S t a t e s n t h e s i s t a b l e f o r Vending machine
p r o b l e m ) ; // p r i n t i n g t h e s t a a t e s y n t h e s i s t a b l e
disp ( P r e s e n t s t a t e
input
Next s t a t e
Output
Db
Da ) ;
disp (
Bn
An
I
J
Bn1
An1
X
Y
);
disp ( tt ) ;
printf ( \n\n D e s i g n e q u a t i o n s : \ n ) ;

22
23
24
25 printf ( \n D e s i g n e q u a t i o n f o r DB\n ) ;
26 db =[ 0 0 2 1 ; 0 0 2 1 ;1 0 2 0 ;0 1 2 0];
27 DB = donkmapij ( db ) ; // m i n i m i z i n g t h e e x p r e s i o n

using
4 v a r i a b l e kmap
28 printf ( \n
DB = %s \n\n , DB ) ; // d i s p l a i n g r e s u l t
29

145

30 printf ( \n D e s i g n e q u a t i o n f o r DA\n ) ;
31 da =[ 0 1 2 0;0 1 2 0 ;0 0 2 0;1 0 2 0];
32 DA = donkmapij ( da ) ; // m i n i m i z i n g t h e e x p r e s i o n

using

4 v a r i a b l e kmap
33 printf ( \n
DA = %s \n\n , DA ) ; // d i s p l a i n g r e s u l t
34
35 printf ( \n D e s i g n e q u a t i o n f o r X\n ) ;
36 x =[0 0 2 0;0 0 2 0;0 1 2 1; 0 0 2 1];
37 X = donkmapij ( x ) ; // m i n i m i z i n g t h e e x p r e s i o n u s i n g 4

v a r i a b l e kmap
38 printf ( \n
X = %s \n\n ,X ) ; // d i s p l a i n g r e s u l t
39
40 printf ( \n D e s i g n e q u a t i o n f o r Y\n ) ;
41 y =[0 0 2 0;0 0 2 0;0 0 2 1;0 0 2 0];
42 Y = donkmapij ( y ) ; // m i n i m i z i n g t h e e x p r e s i o n

using 4

v a r i a b l e kmap
43 printf ( \n
Y = %s \n\n ,Y ) ; // d i s p l a i n g r e s u l t

## Scilab code Exa 11.5 Reducing state transition diagrams

1 // e x a m p l e 1 1 . 5
2 clc ;
3 clear ;
4 disp ( O r i g i n a l
5
6
7
8
9
10
11
12

t a b l e : ) ; // d i s p l a y i n g o r i g i n a l
table
disp ( P r e s e n t S t a t e
Next S t a t e
P r e s e n t Output ) ;
disp (
X=0
X=1
);
disp (
a
a
b
0
);
disp (
b
c
d
0
);
disp (
c
d
e
1
);
disp (
d
c
b
0
);
disp (
e
b
c
1
);
disp ( For s t a t e s b and d e x c e p t f o r n e x t s t a t e X=1
r e s t a r e same . NOw b and d would have b e e n
e q u i v a l e n t i f these next s t a t e s are e q u i v a l e n t .
146

13
14
15
16
17
18
19
20

21
22
23
24
25
26
27
28

For b n e x t s t a t e i s d and d , n e x t s t a t e i s b .
Thus bd a r e e q u i v a l e n t i f n e x t s t a t e s db a r e
e q u i v a l e n t which can a l w a y s be t r u e . Thus b and d
a r e e q u i v l e n t and s t a t e b i s r e t a i n e d . )
disp ( T a b l e a f t e r f i r s t row e l i m i n a t i o n : ) ; // a f t e r
f i r s t row e l i m i n a t i o n
disp ( P r e s e n t S t a t e
Next S t a t e
P r e s e n t Output ) ;
disp (
X=0
X=1
);
disp (
a
a
b
0
);
disp (
b
c
b
0
);
disp (
c
b
e
1
);
disp (
e
b
c
1
);
disp ( Now r e p e a t i n g t h e same a b o v e s t e p f o r c and e
. R e t a i n i n g c and r e p l a c i n g a r l l c s w i t h e we
g e t the below t a b l e );
disp ( T a b l e a f t e r s e c o n d row e l i m i n a t i o n : ) ; // a f t e r
s e c o n d row e l i m i n a t i o n
disp ( P r e s e n t S t a t e
Next S t a t e
P r e s e n t Output ) ;
disp (
X=0
X=1
);
disp (
a
a
b
0
);
disp (
b
c
b
0
);
disp (
c
b
c
1
);

disp ( I m p l i c a t i o n t a b l e method ) ; // by i m p l i c a t i o n
method
29 printf ( d : d\ nc : d ( c e ) \ nb : d ( Ce ) ( bd ) \ na : ( c e ) ( bd ) a \nP=(
c e ) ( bd ) ( a ) ) ;

## Scilab code Exa 11.6 asynchronous sequential circuit

1 // e x a m p l e 1 1 . 6
2 clc ;
3 clear ;
4 disp ( To a n a l y e t h e

where

c i r c u i t we c o n s i d e r x = X( t )
i s t h e c u m m u l a t i v e p r o p a g a t i n d e l a y from
147

5
6
7
8
9
10

i n p u t s i d e up t o X . For a l l
possible
c o m b b i n a t i o n s o f xAB we g e t X and Y f o l l o w i n g t h e
l o g i c r e l a t i o n a s shown i n t h e c i r c u i t and
p r e p a r e t h e f o l l o w i n g Karnaugh map ) ;
disp ( Karnaugh map ) ; // d i s p l a y i n g t h e kmap
disp ( AB )
disp ( x
00
01
11
10 ) ;
disp ( 0
0 /0
0 /0
1/0
0 /0 ) ;
disp ( 1
0/0
0/1
1 /1
1 /0 ) ;
disp ( S t a t e where X = x a r e s t a b l e and p r i m e d .
Outputs c o r r e s p o n d i n g t o e c h s t a t e and i n p t
c o m b i n a t i o n a r e shown b e s i d e . ) ;

## Scilab code Exa 11.7 asynchronous sequential circuit problem in operation

1 // e x a m p l e 1 1 . 7
2 clc ;
3 clear ;
4 disp ( Given k a r n a u g h map ) ; // g i v e n kmap
5 disp (
00
01
11
10 ) ;
6 disp ( 00
11
00
11
00 ) ;
7 disp ( 01
01
11
11
01 ) ;
8 disp ( 11
10
11
11
10 ) ;
9 disp ( 10
10
10
11
11 ) ;
10 disp ( Yes , t h e c i r c u i t may f a c e p r o b l e m i n i t s

o p e r a t i o n . When t h e c i r c u i t i s a t s t a b l e s t a t e
xyAB = 1 1 1 1 and i n p u t AB c h a n g e s from 11 > 10
t h e c i r c u i t o s c i l l a t e s b e t w e e n xyAB = 1 1 1 0 and xy
AB = 1 0 1 0 . A l s o t h e r e can be a c r i t i c l r a c e
p r o b l e m i f a t s t a b l e s t a t e xyAB = 0 0 0 1 , i n p u t AB
c h n g e from 01 t o 0 0 . The c i r c u i t may s e t t l e a t
xyAB = 0 1 0 0 o r xyAB = 1 0 0 0 d e p e n d i n g on which o f
x and y c h a n g e s f i r s t a t t h e f e e d b c k p a t h . Non
c r i t i c a l race s i t u a t u i o n occurs i f at s t a b l e
148

s t a t e xyAB = 0 0 1 0 t h e i n p u t AB c h a n g e from 10 t o
0 0 . );
check Appendix AP 1 for dependency:
kmap3abx.sci
check Appendix AP 2 for dependency:
noof.sci
check Appendix AP 3 for dependency:
noof0.sci

## Scilab code Exa 11.8 asynchronous sequential circuit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

// e x a m p l e 1 1 . 8
// t h i s program r e q u i r e s
// kmp3abx . s c i
// n o o f . s c i
// n o o f 0 . s c i
// a b o v e t h r e e f u n c t i o n s a r e f i r s t e x e c u t e b e f o r e
e x e c u t i n g t h i s program
clc ;
disp ( S t a t e t a b l e t h r o u g h k a r n a u g h map ) ; // s t a t e
t a b l e t h r o u g h kmap
disp (
00
01
11
10 ) ;
disp ( a
a
a
b
b );
disp ( b
a
b
b
b );
disp ( I f we r e p r e s e n t c u r r e n t s t a t e a a s x = 0 and b
a s x =1 t h e n o u t p u t X can be e x p r e s s e d a s ) ;
j =[0 0 1 1 ; 0 1 1 1];
J = kmap3abx ( j ) ; // f i n d i n g t h e m i n i m i z e d e x p r e s i o n
u s i n g 3 v a r i a b l e kmap
disp ( The m i n i m i s e d e x p r e s s i o n J ) ;
disp ( J ) ; // d i s p l a y i n g t h e m i n i m i z e d e x p r e s s i o n

149

Chapter 12
D to A Conversion and A to D
conversion

Scilab code Exa 12.1 binary equivalent weight of each bit in a 4bit system
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

// c h a p t e r 12
// Example 1 2 . 1
//Q . Find t h e b i n a r y e q u i v a l e n t o f e a c h b i t i n a 4
b i t system
// s o l u t i o n :
clc ;
clear ;
LSB = 1/(2^4 -1) ; // c a l c u l a t i n g b i n a r y w e i g h t s
LSB2 = 2*1/(2^4 -1) ;
LSB3 = 4*1/(2^4 -1) ;
MSB = 8*1/(2^4 -1) ;
disp ( B i n a r y w e i g h t o f e a c h b i t i n a 4 b i t system ) ;
d i s p ( LSB = ) ; // d i s p l a i n g t h e r e s u l t
d i s p ( LSB ) ;
d i s p ( LSB2 = ) ;
d i s p ( LSB2 )
d i s p ( LSB3 = ) ;
d i s p ( LSB3 )
d i s p ( MSB = ) ;
150

19

d i s p (MSB) ;

## Scilab code Exa 12.2 5 bit resistive divider

1 // Example 1 2 . 2
2 clc ;
3 clear ;
4 LSB = 1/(2^5 -1) ;
// c a l c u l a t i n g w e i g h t s
5 LSB2 = 2*1/(2^5 -1) ;
6 LSB3 = 4*1/(2^5 -1) ;
7 change_LSB = 10 * LSB ;
8 change_LSB2 = 10 * LSB2 ;
9 change_LSB3 = 10* LSB3 ;
10 op_vol = (10*2^0 + 0*2^1 + 10*2^2 +0*2^3 +10*2^4)

/(2^5 -1) ; // c a l c u l a t i n g o u t p u t v o l t a g e
disp ( ( a ) LSB = ) ;
disp ( LSB ) ;
disp ( ( b ) S e c o n d LSB = ) ;
disp ( LSB2 )
disp ( T h i r d LSB3 = ) ;
disp ( LSB3 )
disp ( ( c ) c h a n g e i n o u t p u t v o l t a g e c a u s e d by ) ;
disp ( c h a n g e i n LSB = ) ;
disp ( change_LSB ) ;
disp ( c h a n g e i n s e c o n d LSB = ) ;
disp ( change_LSB2 )
disp ( c h a n g e i n t h i r d LSB = ) ;
disp ( change_LSB3 )
disp ( ( d ) o u t p u t v o l t a g e f o r a d i g i t a l i n p u t o f 1 0 1 0 1
= ) ;
25 disp ( op_vol ) ;

11
12
13
14
15
16
17
18
19
20
21
22
23
24

## Scilab code Exa 12.3 5 bit ladder

151

1 // Example 1 2 . 3
2 clc ;
3 clear ;
4 for i =1:5
5
op_v (1 , i ) = 10/2^ i ; // c a l c u l a t i n g

otput v o l t a g e s

c o r r e s p o n d i n g to each b i t
6 end
7 disp ( o u t p u t v o l t a g e s

c o r r e s p o n d i n g to each b i t are
) // d i s p l a y i n g r e s u l t
8 disp ( op_v ) ;

## Scilab code Exa 12.4 5 bit ladder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

// Example 1 2 . 4
clc
clear
V_A =0
// a=i n p u t ( E n t e r t h e b i n a r y d i g i t ( 5 b i t s ) : ) ;
a =11010
for i =1:5
r = modulo (a ,10) ;
b (1 , i ) = r ;
a = a /10;
a = floor ( a ) ;
end
for j =1:5
V_A = V_A + 10* b (1 , j ) *2^( j -1) ;
end ;
V_A = V_A /2^5;
disp ( The o u t p u t v o l t a g e i n v o l t s i s ) ;
d i s p ( V A ) ; // d i s p l a y i n g t h e v a l u e

## Scilab code Exa 12.5 5 bit ladder

152

1 // Example 1 2 . 5
2 clc ;
3 clear ;
4 ful_scale_voltage = 0 ;
5 for i =1:5
6
op_v (1 , i ) = 10/2^ i ;
7
ful_scale_voltage = ful_scale_voltage + op_v (1 , i

) ; // c a l c u l a t i n g t h e f u l l
8 end
9 disp ( f u l l s c a l e o u t p u t v o l t a g e
10 disp ( ful_scale_voltage )

scale voltage

i n v o l t s i s = ) ;

## Scilab code Exa 12.6 5 bit ladder

1 // Example 1 2 . 6
2 clc ;
3 clear ;
4 I =10/(3*10^3) ;
5 printf ( C u r r e n t e a c h i n p u t
6
7
8
9
10
11
12

d i g i t a l v o l t a g e must be
c a p a b l e o f s u p p l y i n g i s =%f mA\n ,I *1000)
ful_scale_voltage = 0 ;
for i =1:5
op_v (1 , i ) = 10/2^ i ;
ful_scale_voltage = ful_scale_voltage + op_v (1 , i
);
end
V_A = ful_scale_voltage * (2*1000) /(1000+(2*1000) ) ;
printf ( \n Output v o l t a g e Va = %f V , V_A ) ;

## Scilab code Exa 12.8 DAC0808

1 // Example 1 2 . 8
2 clc ;

153

3 clear ;
4 V0 = 10*((1/2) +(1/4) +(1/32) +(1/128) ) ; // c a l c u l a t i n g

v o l t a g e when 1 , 2 , 5 , 7 r e h i g h
5 printf ( Output v o l t a g e = %f V , V0 ) // d i s p l a y i n r e s u l t

## Scilab code Exa 12.9 resolution of 9 bit D to A

1 // Example 1 2 . 9
2 clc ;
3 clear
4 resol = 1/512 * 100 ; // c a l c u l a t i n g r e s o l
5 vol_resol = 1/512 *5 *1000;
6 printf ( R e s o l u i t i o n i n p e r c e n t a g e = %f , resol ) ;
7
8 printf ( \n\n V o l t a g e r e s o l u t i o n = %f mV , vol_resol ) ;

## Scilab code Exa 12.10 resolution

1 // Example 1 2 . 1 0
2 clc ;
3 clear ;
4 disp ( The LSB o f an 11 b i t s y s t e m h a s a r e s o l u t i o n

o f 1/2048 );
5 re = ceil (10000/2048) ; // c a l c u l t i n g t h e
6 printf ( \n\n 1 / 2 0 4 8 x 10 = %d mV , re ) ;

## Scilab code Exa 12.11 counter type A to D converter

1 // Example 1 2 . 1 1
2 clc ;

154

resolution

3
4
5
6
7
8
9
10
11
12
13
14
15
16

clear ;
// b i t = i n p u t ( S p e c i f y t h e c o n v e r t e r b i t l e n g t h : )
// f r e = i n p u t ( s p e c i f y t h e c l o c k f r e q u e n c y i n kHz
:)
bit =8;
fre = 500
max_conv_time = 2^ bit * (1/( fre *1000) ) ;
avg_conv_time = 0.5 * max_conv_time ;
max_conv_rate = 1/ max_conv_time ;
disp ( Maximum C o n v e r s i o n Time = ) ;
disp ( max_conv_time ) ;
disp ( A v e r a g e C o n v e r s i o n Time = ) ;
disp ( avg_conv_time ) ;
disp ( Maximum C o n v e r s i o n Rate = ) ;
disp ( max_conv_rate ) ;

## Scilab code Exa 12.13 10 bit A to D converter

1 // Example 1 2 . 1 3
2 clc
3 clear
4 quat_err = 1/1024 *100 ;
5 disp ( I f t h e a n a l o g p o r t i o n t o be c o n s t r u c t e d
6

t i an

accuracy of 0 .1 )
printf ( \ nThe o v e r a l l a c c u r a c y i s i n p e r c e n t a g e = %f
,0.1 + quat_err )

155

Chapter 13
Memory

## Scilab code Exa 13.2 structure of binary address

1 // e x a m p l e 1 3 . 2
2 clc ;
3 clear ;
4 close ;
5 // cp = i n p u t ( e n t e r t h e c a p a c i t y
6
7
8
9
10

o f t h e memory
system in b i t s : ) ;
cp =1024; // g i v e n c a p a c i t y
n = log2 ( cp ) ;
printf ( The no o f b i t s i n t h e a d d r e s s word a r e : %d\
n ,n ) ;
printf ( The number o f r e q u i r e d r o w s a r e : %d\n ,2^( n
/2) ) ;
printf ( The number o f r e q u i r e d c o l u m n s a r e : %d ,2^(
n /2) ) ;

156

1 // e x a m p l e 1 3 . 3
2 clc
3 clear
4 // b i n ( 1 , 1 ) = i n p u t ( E n t e r t h e
5
6
7
8
9
10
11
12

f i r s t half string of
b i n a r y number : ) ;
// b i n ( 1 , 2 ) = i n p u t ( E n t e r t h e s e c o n d h a l f s t r i n g o f
b i n a r y number : ) ;
bin =[ 1 0 1 1 0 0 1 1 0 1 ]; // g i v e n b i n r y a d d r e s s
dec = bin2dec ( bin ) ; // f i n d i n g d e c i m a l e q u i v l e n t
hex = dec2hex ( dec ) ; // f i n d i n h e x d e c i m a l e q u i v a l e n t
disp ( The d e c i m a l a d d r e s s i s : ) ;
disp ( dec ) ;
disp ( The h e x a d e c i m a l a d d r e s s i s : ) ;
disp ( hex ) ;

157

Chapter 14
Digital Integrated circuits

## Scilab code Exa 14.1 diode forward or reverse

1 // e x a m p l e 1 4 . 1
2 clc ;
3 clear ;
4 close ;
5 vdc = input ( E n t e r t h e v a l u e o f DC v o l t a g e Vdc i n

v o l t s : );
6 r = input ( E n t e r t h e v a l u e o f r e s i s t a c e i n K ohms :
);
7 v = input ( E n t e r t h e v a l u e o f v o l t a g e a c r o s s d i o d e
i n v o l t s : );
8
9 i = ( vdc - v ) / r ;
10 format ( v ,4) ;
11 if (i >0)
// c h e c k i n g w h e t h e r t h e d i o d e
12
13
14
15
16
17

i s forward or

r e v e r s e b i a s e d by c h e c k i n g c u r r e n t
disp ( The d i o d e i s i n f o r w a r d b i a s ) ;
disp ( The d i o d e c u r r e n t i n mA i s : ) ;
disp ( i ) ;
else
disp ( The d i o d e i s i n R e v e r s e b i a s ) ;
disp ( The d i o d e c u r r e n t i n mA i s : 0 . 0 ) ;
158

18 end ;

## Scilab code Exa 14.2 Diode current

1 // e x a m p l e 1 4 . 2
2 clc ;
3 clear ;
4 close ;
5 vdc = input ( E n t e r t h e v a l u e o f DC v o l t a g e Vdc i n
6
7
8
9
10
11

v o l t s : );
r = input ( E n t e r t h e v a l u e o f r e s i s t a c e i n K ohms :
);
// v = i n p u t ( E n t e r t h e v a l u e o f v o l t a g e a c r o s s
diode in v o l t s : ) ;
v = 1.6;
i = ( vdc - v ) / r ; // / c a l c u l a t i n g t h e c u r r e n t
disp ( The d i o d e c u r r e n t i n mA i s : ) ;
disp ( i ) ;

## Scilab code Exa 14.3 current in the given circuit

1 // e x a m p l e 1 4 . 3
2 clc ;
3 clear ;
4 close ;
5 v1 = input ( E n t e r t h e v a l u e o f V1 i n
6
7
8
9
10
11

## taking the inpt voltage

disp ( CASE a ) ; // c a s e a
if ( v1 ==0) then
disp ( V2 = 5 V ) ;
disp ( I = 0 mA ) ;
else
disp ( V2 = 0 V )
159

v o l t s : ) ; //

12
disp ( I = 5 mA )
13 end
14 disp ( CASE b ) ; // c a s e b
15 if ( v1 ==0) then
16
disp ( V2 = 5 V )
17
disp ( I = 5 mA )
18 else
19
disp ( V2 = 0 V ) ;
20
disp ( I = 0 mA ) ;
21
22 end

## Scilab code Exa 14.4 n channel MOSFET inverter

1 // e x a m p l e 1 4 . 4
2 clc ;
3 clear ;
4 v1 = input ( E n t e r t h e v a l u e o f V1 i n

p a r t a : v1 =0 ; p a r t b : v1 =5v
5 if ( v1 ==0) then // c h e c k i n g f o r V1
6
disp ( V2 = 5 V ) ;
7
disp ( I = 0 mA ) ;
8
else
9
disp ( V2 = 0 V ) ;
10
disp ( I = 0 . 5 mA ) ;
11 end

160

v o l t s : ) ; //

Chapter 15
Applications

## Scilab code Exa 15.1 Timing of a six digit display

1 // e x a m p l e 1 5 . 1
2 // t i m i n g f o r a s i x d i g i t d i s p l a y
3 clc ;
4 clear ;
5 // f=i n p u t ( E n t e r t h e r e p e t i t i o n r a t e i n Hz : ) ;
6 // d= i n p u t ( E n t e r t h e l e n g t h o f d i s p l a y : ) ;
7 f =125; // g i v e n i n p u t s
8 d =6;
9 format ( v ,5) ; // c h a n g i n g t h e p r e c i s i o n o f t h e

calculation
10 k =1000/ f ;
11 l =1000/( f * d ) ; // making n e c c e s a r y c a l c u l a t i o n s
12 m =k - l ;
13 printf ( A l l d i g i t s must be s e r v i c e d o n c e e v e r y %f

m i l l i s e c o n d s \n ,k ) ;
printf ( Each d i g i t w i l l be ON f o r : %f m i l l i s e c o n d s \
n ,l ) ;
15 printf ( and OFF f o r : %f m i l l i s e c o n d s ,m ) ; //
displaying results
14

161

## Scilab code Exa 15.4 Basic frequency counter

1 // e x a m p l e 1 5 . 4
2 clc ;
3 clear ;
4 // f=i n p u t ( E n t e r t h e i n p u t s q u a r e wave s i g n a l

f r e q u e n c y i n kHz : ) ;
5 // t=i n p u t ( E n t e r t h e g a t e e n b l e t i m e i n s e c o n d s ) ;
6 // f i r s t p a r t :
7 f =7.50;
8 t =0.1;
9 format ( v ,18) ;
10 m = t * f *1000; // making n e c c e s a r y c a l c u l a t i o n s
11 printf ( For t = %f s e c o n d s \n ,t ) ;
// d i s p l a y i n g
12
13
14
15
16
17
18
19
20
21
22

results
printf ( The c o u n t e r
// p a r t 2
t =1;
printf ( \n\ nFor t =
m = t * f *1000;
printf ( The c o u n t e r
// p a r t 3
t =10;
m = t * f *1000;
printf ( \n\ nFor t =
printf ( The c o u n t e r

w i l l c o u n t up t o : %f\n ,m ) ;

%f s e c o n d s \n ,t ) ;
w i l l c o u n t up t o : %f\n ,m ) ;

%f s e c o n d s \n ,t ) ;
w i l l c o u n t up t o : %f\n ,m ) ;

## Scilab code Exa 15.5 4 decimal digit frequency counter

1 // e x a m p l e 1 5 . 5
2 clc ;

162

## disp ( Assuming t h e c o u n t e r began a t 0 0 0 0 , t h e

d i s p l a y would r e a d 200 a t t h e end o f t h e f i r s t
measurment p e r i o d . I t w i l l r e a d 4 0 0 , t h e n 600 and
s o on a t t h e end o f s u c c e e d i n g p e r i o d s . T h i s i s
because the counter c a p a c i t y i s exceeded each
time , and i t s i m p l y r e c y c l e s t h r o u g h 0 0 0 0 . ) ;

## Scilab code Exa 15.6 instrument to measure time period

1 // e x a m p l e 1 5 . 6
2 clc ;
3 clear ;
4 // s= i n p u t ( E n t e r t h e c l k f r e q e n c y i n kHz : ) ;
5 // f=i n p u t ( E n t e r t h e f r e q u e n c y o f t h e unknown i n p u t

i n Hz : ) ;
s =100; // t a k i n g t h e i n p u t s
f =200;
g =1000000/ f ; // making n e c c e s a r y c a l c u l a t i o n s
c = g * s /1000;
c = round ( c ) ;
p = c *1000/ s ;
disp ( Assuming t h a t t h e c o n t e r and t h e d i s p l a y a r e
i n i t i a l l y a t 0 0 0 0 0 ) ; // d i s p l a y i n g r e s u l t s
13 printf ( E n a b l e g a t e t i m e i n m i c r o s e c n d s w i l l be :
%d\n ,g ) ;
14 printf ( D u r i n g t h e g a t e t i m e t h e c o u n t e r w i l l be
a d v c e d by ( number o f c o u n t s ) %d \n ,c ) ;
15 printf ( The t i m e p e r i o d o f t h e unknown i n p u t i n
micro s e c o n d s i s : %d ,p ) ;
6
7
8
9
10
11
12

## Scilab code Exa 15.9 ADC0804

1

// e x a m p l e 1 5 . 9
163

2 clc ;
3 clear ;
4 // p a r t ( a )
5 // an=i n p u t ( E n t e r t h e a n a l o g i n p u t i n v o l t s
6 format ( v ,12) ; // c h a n g i n g t h e p r e c i s i o n o f
7
8
9
10
11
12
13
14
15
16
17

: ) ;

calculation
an =2.5;
k = an *1000/19.53;
k = round ( k ) ;
m = dec2bin ( k ) ; // c o n v e r t i n g from d e c m a l t o b i n a r y
printf ( The d i g i t a l o u t p u t i s : %s\n ,m ) ;
// p a r t ( b )
// dg=i n p u t ( E n t e r t h e d i g i t a l o u t p u t a s a s t r i n g : ) ;
dg = 0 0 1 0 0 0 1 0 ;
f = bin2dec ( dg ) ; // c o n v e r t i n g b i n a r y t o d e c i m a l
y = f *19.53*10^ -3;
printf ( The a n a l o g i n p u t i n v o l t s i s : %f ,y ) ;

## Scilab code Exa 15.10 ADC3511

1 // e x a m p l e 1 5 . 1 0
2 clc ;
3 clear ;
4 // v = i n p u t ( E n t e r t h e
5
6
7
8
9
10
11
12

## reference voltage in volts

: ) ;
// an = i n p u t ( E n t e r t h e a n a l o g i n p u t v o l t a g e i n
volts : ) ;
v =2; // t a k i n g g i v e n i n p u t
an =1.25;
count = 2000* an / v ;
count = round ( count ) ;
printf ( The c c o u n t h e l d i n t h e c o u n t e r f o r g i v e n
a n a l o g i n p u t w i l l be : %f , count ) ;
d = an / v ;
printf ( \ nThe duty c y c l e i s : %f ,d ) ;
164

## Scilab code Exa 15.11 ADC3511

1 // e x a m p l e 1 5 . 1 1
2 clc ;
3 clear ;
4 disp ( The f u l l s c a l e c o u n t f o r ADC3511 i s 1 9 9 9 and

f o r t h e ADC3711 i s 3 9 9 9 . So , t h e l a r g e s t v a l u e
p o s s i b l e f o r t h e MSD i n e i t h e r c a s e i s 3 = 0 0 1 1 .
c l e a r l y t h e MSB i s n o t n e e d e d f o r t h m a g n i t u e o f
t h e MSD. I t i s t h u s c o n v e n i e n t t o s p e c i f
p o s i t i v e number when t h i s b i t i s a 0 and a
n e g t i v e number when t h i s b i t i s a 1 . ) ;

## Scilab code Exa 15.12 ADD3501

1 // exmple 1 5 . 1 2
2 clc ;
3 clear ;
4 disp ( These two c o m p on e n t s e s t a b l i s h

the i n t e r n a l
o s c i l l a t o r frequency used as the c l o c k frequency
in the c n v e r t e r according to the r e l a t i o n s h i p f i
=0.6/RC . I n t h i s c a s e f i =320 kHz . ) ;

165

Chapter 16
A Simple Computer Design

## Scilab code Exa 16.1 size of PC IR ACC MAR MDR

1 // e x a m p l e 1 6 . 1
2 clc
3 clear
4 // l e n = i n p u t ( E n t e r t h e l e n g t h
5
6
7
8
9
10
11
12
13
14
15
16
17

o f e a c h memory

location in bits : ) ;
// op = i n p u t ( E n t e r t h e l e n g t h o f Opcode : ) ;
len =16;
op =4;
nop = 2^4;
// c a l c u l a t i n g
nab = len - op ;
memloc = 2^ nab ;
memsize = memloc *16;
mem = memsize /1024;
printf ( ( a ) Maximum Number o f Opcodes = %d \n , nop ) ;
// d i s p l a y i n g
printf ( ( b ) S i z e o f memory i n K i l o b i t s = %d \n , mem
);
printf ( ( c ) S i z e o f PC and MAR = %d \n , nab ) ;
printf (
S i z e o f IR = %d\n , op ) ;
printf (
S i z e o f ACC and MDR = %d \n , len ) ;

166

Scilab code Exa 16.6 Number of clock cycles needed to execute a program
1 // e x a m p l e 1 6 . 6
2 lda = input ( E n t e r t h e number o f LDA i n s t r u c t i o n s

: )

; // a c c e p t i n g t h e i n p u t from t h e u s e r
3 add = input ( E n t e r t h e number o f ADD i n s t r u c t i o n s

: )

;
4 sub = input ( E n t e r t h e number o f SUB i n s t r u c t i o n s
5
6
7
8
9
10
11

: )

;
sta = input ( E n t e r t h e number o f STA i n s t r u c t i o n s :
;
shl = input ( E n t e r t h e number o f SHL i n s t r u c t i o n s :
;
hlt = input ( E n t e r t h e number o f HLT i n s t r u c t i o n s :
;
k = lda + add + sub + sta ;
l = shl + hlt ;
c = k *5 + l *4; // c a l c u l a t i n g t h e t o t a l no . o f c l c k
cycles required
printf ( T o t a l c l o c k c y c l e s r e q u i r e d t o e x e c u t e a r e
%d ,c ) ; // d i s p l a y i n g r e s u l t .

167

)
)
)

Appendix
Scilab code AP 1 3-variable kmap(abx)
//3VARIABLE KMAP
// u s e s n o o f . s c i and n o o f 0 . s c i
// a b o v e two f u n c t i o n s s h o u l d be e x e c u t e d b e f o r e
executing this function .
4 function bi = kmap3abx ( k )
5
n =4;
6
m =2

1
2
3

7
8 // k =[0 0 0 1 ;
9
// 0 1 1 1 ] ;
10 k (: ,: ,2) = zeros (m , n ) ;
11
var =[ x A B ];
12
// v a r = [ w x y z ] ;
13
p1 =[ x x ];
14
p2 =[ A B ; A B ; AB ; AB ];
15
cmn4 =4;
16
cmn2 =2;
17
temp =1;
18
// p r i n t f ( The m i n i m a l e c p r e s s i o n o f t h e g i v e n
19
20
21
22
23
24

Kmap ) ;
disp ( k (: ,: ,1) ) ;
// d i s p ( i s : ) ;
// p r i n t f ( f ) ;
// p r i n t f (=) ;
bi = ;
168

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

// 8 c e l l s
for i =1: m
for j =1: n
if ( k (i , j ) ~=1 & k (i , j ) ~=2)
temp =0;
break ;
end
end
end
if ( temp ==1)
bi = strcat ([ bi 1 ]) ;
return ;
end
// 4 c e l l s
z1 = ones (1 ,4) ;
z2 = ones (4 ,1) ;
z3 = ones (2 ,2) ;
temp1 =[ 0 1 ];
temp2 =[ 00 ; 01 ; 11 ; 10 ];
for t =1: m
z = k (t ,: ,1) ;
no = noof ( k (t ,: ,2) ) ;
if ( noof0 ( z ) ==0 & no < cmn4 & noof ( z ) >0)
k (t ,: ,2) = z1 ;
a = strsplit ( temp1 (1 , t ) ) ;
for in =1: max ( size ( a ) )
if ( a ( in ) == 0 )
bi = strcat ([ bi var ( in )
]) ;
end
if ( a ( in ) == 1 )
bi = strcat ([ bi var ( in ) ]) ;
end
end
bi = strcat ([ bi + ]) ;
end
end
for i =1: m -1
169

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

for j =1: n
t1 = i +1;
if ( j == n )
t2 =1;
else
t2 = j +1;
end
z4 =[ k (i ,j ,1) k (i , t2 ,1) ; k ( t1 ,j ,1) k ( t1 , t2
,1) ];
z5 =[ k (i ,j ,2) k (i , t2 ,2) ; k ( t1 ,j ,2) k ( t1 , t2
,2) ];
no = noof ( z5 ) ;
if ( noof0 ( z4 ) ==0 & no < cmn4 & noof ( z4 ) >0)
k (i ,j ,2) =1;
k (i , t2 ,2) =1;
k ( t1 ,j ,2) =1;
k ( t1 , t2 ,2) =1;
a = strsplit ( temp2 (j ,1) ) ;
b = strsplit ( temp2 ( t2 ,1) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
bi = strcat ([ bi var (1+ in )
]) ;
end
if ( c ( in ) ==0 & a ( in ) == 1 )
bi = strcat ([ bi var (1+ in ) ])
;
end
end
bi = strcat ([ bi + ]) ;
end
end
end
// 2 c e l l s
z6 =[1 1];
z7 = z6 ;
170

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

for i =1: m
for j =1: n
t1 = i +1;
if ( j == n )
t2 =1;
else
t2 = j +1;
end
z8 =[ k (i ,j ,1) k (i , t2 ,1) ];
z9 =[ k (i ,j ,2) k (i , t2 ,2) ];
no1 = noof ( z9 ) ;
if ( noof0 ( z8 ) ==0 & no1 < cmn2 & noof ( z8 ) >0)
k (i ,j ,2) =1;
k (i , t2 ,2) =1;
bi = strcat ([ bi p1 (1 , i ) ]) ;
a = strsplit ( temp2 (j ,1) ) ;
b = strsplit ( temp2 ( t2 ,1) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
bi = strcat ([ bi var (1+ in )
]) ;
bi = strcat ([ bi + ]) ;
end
if ( c ( in ) ==0 & a ( in ) == 1 )
bi = strcat ([ bi var (1+ in ) ])
;
bi = strcat ([ bi + ]) ;
end
end
end
end
end
for i =1: m -1
for j =1: n
t1 = i +1;
if ( j == n )
t2 =1;
171

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

else
t2 = j +1;
end
z10 =[ k (i ,j ,1) ; k ( t1 ,j ,1) ];
z11 =[ k (i ,j ,2) ; k ( t1 ,j ,2) ];
no2 = noof ( z11 ) ;
if ( noof0 ( z10 ) ==0 & no2 < cmn2 & noof ( z10 )
>0)
k (i ,j ,2) =1;
k ( t1 ,j ,2) =1;
bi = strcat ([ bi p2 (j ,1) ]) ;
bi = strcat ([ bi + ]) ;
end
end
end
// s i n g l e c e l l
for i =1: m
for j =1: n
if ( k (i ,j ,2) ==0 & k (i ,j ,1) ==1)
bi = strcat ([ bi p1 (1 , i ) ]) ;
bi = strcat ([ bi p2 (j ,1) ]) ;
bi = strcat ([ bi + ]) ;
end
end
end
bi = strcat ([ bi 0 ]) ;
// d i s p ( )
endfunction
Scilab code AP 2 returns number of 1s in a matrix

1
2
3
4
5

## function res = noof ( a ) // t h i s f u n c t i o n r e t u r n s t h e

no o f 1 s i n t h e g i v e n m a t r r i x
res =0;
for i =1: max ( size ( a (: ,1) ) )
for j =1: max ( size ( a (1 ,:) ) )
if ( a (i , j ) ==1)
172

6
7
8
9
10

## res = res +1;

end
end
end
endfunction
Scilab code AP 3 returns number of 0s in a matrix

1
2
3
4
5
6
7
8
9
10

## function res = noof0 ( a ) // t o f i n d t h e no o f z e r o s i n

given matrix
res =0;
for i =1: max ( size ( a (: ,1) ) )
for j =1: max ( size ( a (1 ,:) ) )
if ( a (i , j ) ==0)
res = res +1;
end
end
end
endfunction
Scilab code AP 4 4-variable kmap with dont cares

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

//4 V a r i a b l e KMAP
// r e t u r n s a s t r i n g o f t h e m i n i m i z e d e x p r e s s i o n
// r e q u i r e s n o o f 1 . s c i
// n o o f 1 . s c i s h o u l d be e x e c u t e d b e f o r e e x e c u t i n g t h i s
function
function bi = donkmapij ( k )
n =4;
k (: ,: ,2) = zeros (n , n ) ;
var =[ I J Bn An ];
p1 =[ I J I J I J I J ];
p2 =[ Bn An ; Bn An ; BnAn ; BnAn ];
cmn4 =4;
cmn2 =2;
temp =1;
bi = ;
disp ( k (: ,: ,1) ) ;
173

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

for i =1: n
for j =1: n
if ( k (i , j ) ~=1 | k (i , j ) ~=2)
temp =0;
break ;
end
end
end
if ( temp ==1)
printf ( 1 ) ;
abort ;
end
// c h e c k i n g t h e 8 c e l l s c a s e s
z1 = ones (2 ,4) ;
z2 = ones (4 ,2) ;
temp1 =[ 00 01 11 10 ];
temp2 = temp1 ;
for i =1: n
if ( i ==4)
t =1;
else
t = i +1;
end
z =[ k (i ,: ,1) ; k (t ,: ,1) ];
if ( noof1 (z ,0) ==0 & noof1 (z ,1) >1)
k (i ,: ,2) =[1 1 1 1];
k (t ,: ,2) =[1 1 1 1];
a = strsplit ( temp2 (i ,1) ) ;
b = strsplit ( temp2 (t ,1) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
bi = strcat ([ bi var ( in )
]) ;
bi = strcat ([ bi + ]) ;
break ;
else
if ( c ( in ) ==0 & a ( in ) == 1 )
174

53

bi = strcat ([ bi var ( in ) ])
;
bi = strcat ([ bi + ]) ;
break ;
end

54
55
56
57
end
58
end
59
end
60 end
61
for j =1: n
62
if ( j ==4)
63
t =1;
64
else
65
t = j +1;
66
end
67
z =[ k (: ,j ,1) k (: ,t ,1) ];
68
if ( noof1 (z ,0) ==0 & noof1 (z ,1) >0)
69
k (: ,j ,2) =[1;1;1;1];
70
k (: ,t ,2) =[1;1;1;1];
71
a = strsplit ( temp1 (1 , j ) ) ;
72
b = strsplit ( temp1 (1 , t ) ) ;
73
c = strcmp (a , b ) ;
74
for in =1: max ( size ( c ) )
75
if ( c ( in ) ==0 & a ( in ) == 0 )
76
bi = strcat ([ bi var (2+ in ) ])

;
bi = strcat ([ bi + ]) ;
break ;
else
if ( c ( in ) ==0 & a ( in ) == 1 )
bi = strcat ([ bi var (2+ in ) ])
;
bi = strcat ([ bi + ]) ;
break ;
end
end

77
78
79
80
81
82
83
84
85
86
87

end
end
175

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

end
// c h e c k i n g t h e 4 c e l l s c a s e s
z1 = ones (1 ,4) ;
z2 = ones (4 ,1) ;
z3 = ones (2 ,2) ;
temp1 =[ 00 01 11 10 ];
temp2 = temp1 ;
for t =1: n
z = k (t ,: ,1) ;
no = noof1 ( k (t ,: ,2) ,1) ;
if ( noof1 (z ,0) ==0 & no < cmn4 & noof1 (z ,1)
>0)
k (t ,: ,2) = z1 ;
a = strsplit ( temp1 (1 , t ) ) ;
for in =1: max ( size ( a ) )
if ( a ( in ) == 0 )
bi = strcat ([ bi var ( in )
]) ;
end
if ( a ( in ) == 1 )
bi = strcat ([ bi var ( in ) ]) ;
end
end
bi = strcat ([ bi + ]) ;
end
end
for t =1: n
z = k (: ,t ,1) ;
no = noof1 ( k (: ,t ,2) ,1) ;
if ( noof1 (z ,0) ==0 & no < cmn4 & noof1 (z ,1) >0)
k (: ,t ,2) = z2 ;
a = strsplit ( temp2 (t ,1) ) ;
for in =1: max ( size ( a ) )
if ( a ( in ) == 0 )
bi = strcat ([ bi var (2+ in ) ])
;
end
if ( a ( in ) == 1 )
176

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

## bi = strcat ([ bi var (2+ in ) ]) ;

end
end
bi = strcat ([ bi + ]) ;
end
end
for i =1: n
for j =1: n
if ( i == n )
t1 =1;
else
t1 = i +1;
end
if ( j == n )
t2 =1;
else
t2 = j +1;
end
z4 =[ k (i ,j ,1) k (i , t2 ,1) ; k ( t1 ,j ,1) k ( t1 , t2
,1) ];
z5 =[ k (i ,j ,2) k (i , t2 ,2) ; k ( t1 ,j ,2) k ( t1 , t2
,2) ];
no = noof1 ( z5 ,1) ;
if ( noof1 ( z4 ,0) ==0 & no < cmn4 & noof1 ( z4
,1) >0)
k (i ,j ,2) =1;
k (i , t2 ,2) =1;
k ( t1 ,j ,2) =1;
k ( t1 , t2 ,2) =1;
a = strsplit ( temp2 (i ,1) ) ;
b = strsplit ( temp2 ( t1 ,1) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
bi = strcat ([ bi , var ( in )
]) ;
end
if ( c ( in ) ==0 & a ( in ) == 1 )
177

157
158
159
160
161
162
163
164
165

bi = strcat ([ bi var ( in ) ]) ;
end
end
a = strsplit ( temp1 (1 , j ) ) ;
b = strsplit ( temp1 (1 , t2 ) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
bi = strcat ([ bi , var (2+ in )
]) ;
end
if ( c ( in ) ==0 & a ( in ) == 1 )
bi = strcat ([ bi var (2+ in ) ])
;
end
end
bi = strcat ([ bi + ]) ;

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

end
end
end
// 2 c e l l s
z6 =[1 1];
z7 = z6 ;
for i =1: n
for j =1: n
if ( i == n )
t1 =1;
else
t1 = i +1;
end
if ( j == n )
t2 =1;
else
t2 = j +1;
end
z8 =[ k (i ,j ,1) k (i , t2 ,1) ];
z9 =[ k (i ,j ,2) k (i , t2 ,2) ];
no1 = noof1 ( z9 ,1) ;
178

193

## if ( noof1 ( z8 ,0) ==0 & no1 < cmn2 & noof1 ( z8

,1) >0)
k (i ,j ,2) =1;
k (i , t2 ,2) =1;
a = strsplit ( temp1 (1 , j ) ) ;
b = strsplit ( temp1 (1 , t2 ) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
bi = strcat ([ bi p1 (1 , i ) ]) ;
bi = strcat ([ bi , var (2+ in )
]) ;
bi = strcat ([ bi + ]) ;
end
if ( c ( in ) ==0 & a ( in ) == 1 )
bi = strcat ([ bi p1 (1 , i ) ]) ;
bi = strcat ([ bi var (2+ in ) ])
;
bi = strcat ([ bi + ]) ;
end
end
end

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

end
end
for i =1: n
for j =1: n
if ( i == n )
t1 =1;
else
t1 = i +1;
end
if ( j == n )
t2 =1;
else
t2 = j +1;
end
z10 =[ k (i ,j ,1) ; k ( t1 ,j ,1) ];
z11 =[ k (i ,j ,2) ; k ( t1 ,j ,2) ];
179

228
229

## no2 = noof1 ( z11 ,1) ;

if ( noof1 ( z10 ,0) ==0 & no2 < cmn2 & noof1 (
z10 ,1) >0)
k (i ,j ,2) =1;
k ( t1 ,j ,2) =1;
a = strsplit ( temp2 (i ,1) ) ;
b = strsplit ( temp2 ( t1 ,1) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
bi = strcat ([ bi p2 (j ,1) ]) ;
bi = strcat ([ bi var ( in )
]) ;
bi = strcat ([ bi + ]) ;
end
if ( c ( in ) ==0 & a ( in ) == 1 )
bi = strcat ([ bi p2 (j ,1) ]) ;
bi = strcat ([ bi var ( in ) ]) ;
bi = strcat ([ bi + ]) ;
end
end
end

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

end
end
// c h e c k i n g t h e s i n g l e c e l l c a s e s
for i =1: n
for j =1: n
if ( k (i ,j ,2) ==0 & k (i ,j ,1) ==1)
a = strsplit ( temp1 (1 , j ) ) ;
b = strsplit ( temp2 (i ,1) ) ;
for in =1: max ( size ( a (: ,1) ) )
if ( a ( in ,1) == 1 )
bi = strcat ([ bi var ( in +2) ]) ;
else
if ( a ( in ,1) == 0 )
bi = strcat ([ bi var (2+ in
) ]) ;
end
180

263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279

end
end
for in =1: max ( size ( b (: ,1) ) )
if ( b ( in ,1) == 1 )
bi = strcat ([ bi var ( in ) ]) ;
else
if ( b ( in ,1) == 0 )
bi = strcat ([ bi var ( in )
]) ;
end
end
end
bi = strcat ([ bi + ]) ;
end
end
end
bi = strcat ([ bi 0 ]) ;
endfunction
Scilab code AP 5 number of zeros and ones

1
2
3
4
5
6
7
8
9
10

## function res = noof1 (a , z ) // t h i s f u n c t i o n r e t u r n s b o t h

t h e no o f z e r o s and o n e s i n
given matrix
res =0;
for i =1: max ( size ( a (: ,1) ) )
for j =1: max ( size ( a (1 ,:) ) )
if ( a (i , j ) == z )
res = res +1;
end
end
end
endfunction
Scilab code AP 6 3-variable kmap(a)

1
2

//3VARIABLE KMAP
181

// t h i s f u n c t i o n r e t u r n s t h e a s t r i n g c o n t a i n i n g t h e
minimized e x p r e s s i o n f o r the given 3 v a r i a b l e
kmap
// t h i s f u n c t i o n r e q u i r e s
// n o o f . s c i
// n o o f 0 . s c i
function bi = kmap3a ( k )
n =4;
m =2
k (: ,: ,2) = zeros (m , n ) ;
var =[ An X Y ];
p1 =[ An An ];
p2 =[ X Y ; X Y ; XY ; XY ];

4
5
6
7
8
9
10
11
12
13
14
15
cmn4 =4;
16
cmn2 =2;
17
temp =1;
18
disp ( k (: ,: ,1) ) ;
19 bi = ;
20 // c h e c k i n g a l l t h e 8 1 s c a s e s
21
for i =1: m
22
for j =1: n
23
if ( k (i , j ) ~=1 & k (i , j ) ~=2)
24
temp =0;
25
break ;
26
end
27
end
28
end
29
if ( temp ==1)
30
bi = strcat ([ bi 1 ]) ;
31
return ;
32
end
33 // c h e c k i n g a l l t h e 4 1 s c a s e s
34
z1 = ones (1 ,4) ;
35
z2 = ones (4 ,1) ;
36
z3 = ones (2 ,2) ;
37
temp1 =[ 0 1 ];
38
temp2 =[ 00 ; 01 ; 11 ; 10 ];

182

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

for t =1: m
z = k (t ,: ,1) ;
no = noof ( k (t ,: ,2) ) ;
if ( noof0 ( z ) ==0 & no < cmn4 & noof ( z ) >0)
k (t ,: ,2) = z1 ;
a = strsplit ( temp1 (1 , t ) ) ;
for in =1: max ( size ( a ) )
if ( a ( in ) == 0 )
bi = strcat ([ bi var ( in )
]) ;
end
if ( a ( in ) == 1 )
bi = strcat ([ bi var ( in ) ]) ;
end
end
bi = strcat ([ bi + ]) ;
end
end
for i =1: m -1
for j =1: n
t1 = i +1;
if ( j == n )
t2 =1;
else
t2 = j +1;
end
z4 =[ k (i ,j ,1) k (i , t2 ,1) ; k ( t1 ,j ,1) k ( t1 , t2
,1) ];
z5 =[ k (i ,j ,2) k (i , t2 ,2) ; k ( t1 ,j ,2) k ( t1 , t2
,2) ];
no = noof ( z5 ) ;
if ( noof0 ( z4 ) ==0 & no < cmn4 & noof ( z4 ) >0)
k (i ,j ,2) =1;
k (i , t2 ,2) =1;
k ( t1 ,j ,2) =1;
k ( t1 , t2 ,2) =1;
a = strsplit ( temp2 (j ,1) ) ;
b = strsplit ( temp2 ( t2 ,1) ) ;
183

74
75
76
77

c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
bi = strcat ([ bi var (1+ in )
]) ;
end
if ( c ( in ) ==0 & a ( in ) == 1 )
bi = strcat ([ bi var (1+ in ) ])
;
end
end
bi = strcat ([ bi + ]) ;

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

end
end
end
// c h e c k i n g a l l t h e 2 1 s c a s e s
z6 =[1 1];
z7 = z6 ;
for i =1: m
for j =1: n
t1 = i +1;
if ( j == n )
t2 =1;
else
t2 = j +1;
end
z8 =[ k (i ,j ,1) k (i , t2 ,1) ];
z9 =[ k (i ,j ,2) k (i , t2 ,2) ];
no1 = noof ( z9 ) ;
if ( noof0 ( z8 ) ==0 & no1 < cmn2 & noof ( z8 ) >0)
k (i ,j ,2) =1;
k (i , t2 ,2) =1;
bi = strcat ([ bi p1 (1 , i ) ]) ;
a = strsplit ( temp2 (j ,1) ) ;
b = strsplit ( temp2 ( t2 ,1) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
184

if ( c ( in ) ==0 & a ( in ) == 0 )
bi = strcat ([ bi var (1+ in )
]) ;
bi = strcat ([ bi + ]) ;
end
if ( c ( in ) ==0 & a ( in ) == 1 )
bi = strcat ([ bi var (1+ in ) ])
;
bi = strcat ([ bi + ]) ;
end

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

end
end
end
end
for i =1: m -1
for j =1: n
t1 = i +1;
if ( j == n )
t2 =1;
else
t2 = j +1;
end
z10 =[ k (i ,j ,1) ; k ( t1 ,j ,1) ];
z11 =[ k (i ,j ,2) ; k ( t1 ,j ,2) ];
no2 = noof ( z11 ) ;
if ( noof0 ( z10 ) ==0 & no2 < cmn2 & noof ( z10 )
>0)
k (i ,j ,2) =1;
k ( t1 ,j ,2) =1;
bi = strcat ([ bi p2 (j ,1) ]) ;
bi = strcat ([ bi + ]) ;
end
end
end
// c h e c k i n g i f any s i n g l e i s o l a t e d 1 s a r e l e f t
for i =1: m
for j =1: n
if ( k (i ,j ,2) ==0 & k (i ,j ,1) ==1)
185

145
146
147
148
149
150
151
152

bi = strcat ([ bi p1 (1 , i ) ]) ;
bi = strcat ([ bi p2 (j ,1) ]) ;
bi = strcat ([ bi + ]) ;
end
end
end
bi = strcat ([ bi 0 ]) ;
endfunction
Scilab code AP 7 2-variable kmap

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

// t h i s f u n c t i o n m i n i m i z e s a two v r i a b l e b o o l e a n
e x p r e s s i o n u s i n g kmap
function bi = kmap2 ( k )
var =[ A B A B AB AB ]
temp =1
for i =1:2 // i n t i a l l y c h e c k i n g f o r a l l 1 s
for j =1:2
if k (i , j ) ==1 then
temp = temp + 1;
end
end
end
v =0;
bi = ;
if temp == 5 then
disp ( The m i n i m a l e x p r e s s i o n i s : 1 ) ;
v =1;
else
f o r i = 1 : 2 // c o n s i d e r i n g a l l 2 1 s c a s e s
i f k ( i , 1 ) == 1 & k ( i , 2 ) == 1 t h e n
i f i== 1 t h e n
b i = s t r c a t ( [ b i A ] ) ; v =1;
else
b i = s t r c a t ( [ b i A ] ) ; v =1;
end
bi = strcat ( [ bi + ] ) ;
end
186

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

i f k ( 1 , i ) == 1
i f i==
bi
else
bi
end
end

& k ( 2 , i ) == 1 t h e n
1 then
= s t r c a t ( [ b i B ] ) ; v =1;
= s t r c a t ( [ b i B ] ) ; v =1;

end
end ;
one ( 1 )=k ( 2 , 1 ) ;
f =2;m=2; i =1;
f o r j =1:2
one ( f )=k ( i , j )
f=f +1;
end
i =2;
f o r j =2: 1:1
one ( f )=k ( i , j )
f=f +1;
end
one ( 6 )=k ( 1 , 1 ) ;
i f v==0 t h e n // f o r i s o l a t e d 1 s
f o r i =2:5
i f one ( i )==1 & one ( i +1)== 0 & one ( i 1) ==0
then
i f m>0
bi = strcat ( [ bi + ] ) ;
end ;
b i = s t r c a t ( [ b i v a r ( i 1) ] ) ;
m=m+1;
end
end
end
e n d f u n c t i o n // f i n a l r e s u l t w i l l be s t o r e d i n b i
Scilab code AP 8 3-variable kmap

//3VARIABLE KMAP
187

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

// t h i s f u n c t i o n r e t u r n s t h e a s t r i n g c o n t a i n i n g t h e
minimized e x p r e s s i o n f o r the given 3 v a r i a b l e
kmap
// t h i s f u n c t i o n r e q u i r e s
// n o o f . s c i
// n o o f 0 . s c i
function bi = kmap3 ( k )
n =4;
m =2
k (: ,: ,2) = zeros (m , n ) ;
var =[ Cn Bn An ];
p1 =[ Cn Cn ];
p2 =[ Bn An ; Bn An ; BnAn ; BnAn ];
cmn4 =4;
cmn2 =2;
temp =1;
disp ( k (: ,: ,1) ) ;
bi = ;
// c h e c k i n g a l l t h e 8 1 s c a s e s
for i =1: m
for j =1: n
if ( k (i , j ) ~=1 & k (i , j ) ~=2)
temp =0;
break ;
end
end
end
if ( temp ==1)
bi = strcat ([ bi 1 ]) ;
return ;
end
// c h e c k i n g a l l t h e 4 1 s c a s e s
z1 = ones (1 ,4) ;
z2 = ones (4 ,1) ;
z3 = ones (2 ,2) ;
temp1 =[ 0 1 ];
temp2 =[ 00 ; 01 ; 11 ; 10 ];
for t =1: m
188

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

z = k (t ,: ,1) ;
no = noof ( k (t ,: ,2) ) ;
if ( noof0 ( z ) ==0 & no < cmn4 & noof ( z ) >0)
k (t ,: ,2) = z1 ;
a = strsplit ( temp1 (1 , t ) ) ;
for in =1: max ( size ( a ) )
if ( a ( in ) == 0 )
bi = strcat ([ bi var ( in )
]) ;
end
if ( a ( in ) == 1 )
bi = strcat ([ bi var ( in ) ]) ;
end
end
bi = strcat ([ bi + ]) ;
end
end
for i =1: m -1
for j =1: n
t1 = i +1;
if ( j == n )
t2 =1;
else
t2 = j +1;
end
z4 =[ k (i ,j ,1) k (i , t2 ,1) ; k ( t1 ,j ,1) k ( t1 , t2
,1) ];
z5 =[ k (i ,j ,2) k (i , t2 ,2) ; k ( t1 ,j ,2) k ( t1 , t2
,2) ];
no = noof ( z5 ) ;
if ( noof0 ( z4 ) ==0 & no < cmn4 & noof ( z4 ) >0)
k (i ,j ,2) =1;
k (i , t2 ,2) =1;
k ( t1 ,j ,2) =1;
k ( t1 , t2 ,2) =1;
a = strsplit ( temp2 (j ,1) ) ;
b = strsplit ( temp2 ( t2 ,1) ) ;
c = strcmp (a , b ) ;
189

73
74
75

## for in =1: max ( size ( c ) )

if ( c ( in ) ==0 & a ( in ) == 0 )
bi = strcat ([ bi var (1+ in )
]) ;
end
if ( c ( in ) ==0 & a ( in ) == 1 )
bi = strcat ([ bi var (1+ in ) ])
;
end
end
bi = strcat ([ bi + ]) ;

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

end
end
end
// c h e c k i n g a l l t h e 2 1 s c a s e s
z6 =[1 1];
z7 = z6 ;
for i =1: m
for j =1: n
t1 = i +1;
if ( j == n )
t2 =1;
else
t2 = j +1;
end
z8 =[ k (i ,j ,1) k (i , t2 ,1) ];
z9 =[ k (i ,j ,2) k (i , t2 ,2) ];
no1 = noof ( z9 ) ;
if ( noof0 ( z8 ) ==0 & no1 < cmn2 & noof ( z8 ) >0)
k (i ,j ,2) =1;
k (i , t2 ,2) =1;
bi = strcat ([ bi p1 (1 , i ) ]) ;
a = strsplit ( temp2 (j ,1) ) ;
b = strsplit ( temp2 ( t2 ,1) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
190

## bi = strcat ([ bi var (1+ in )

]) ;
bi = strcat ([ bi + ]) ;

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

end
if ( c ( in ) ==0 & a ( in ) == 1 )
bi = strcat ([ bi var (1+ in ) ])
;
bi = strcat ([ bi + ]) ;
end
end
end
end
end
for i =1: m -1
for j =1: n
t1 = i +1;
if ( j == n )
t2 =1;
else
t2 = j +1;
end
z10 =[ k (i ,j ,1) ; k ( t1 ,j ,1) ];
z11 =[ k (i ,j ,2) ; k ( t1 ,j ,2) ];
no2 = noof ( z11 ) ;
if ( noof0 ( z10 ) ==0 & no2 < cmn2 & noof ( z10 )
>0)
k (i ,j ,2) =1;
k ( t1 ,j ,2) =1;
bi = strcat ([ bi p2 (j ,1) ]) ;
bi = strcat ([ bi + ]) ;
end
end
end
// c h e c k i n g i f any s i n g l e i s o l a t e d 1 s a r e l e f t
for i =1: m
for j =1: n
if ( k (i ,j ,2) ==0 & k (i ,j ,1) ==1)
bi = strcat ([ bi p1 (1 , i ) ]) ;
191

144
145
146
147
148
149
150

bi = strcat ([ bi p2 (j ,1) ]) ;
bi = strcat ([ bi + ]) ;
end
end
end
bi = strcat ([ bi 0 ]) ;
endfunction
Scilab code AP 9 4-variable kmap(sx1x2)

## function []= kmapsx ( k ) // t h i s f n c t i o n s p r i n t s t h e

m i n i m i e d e x p r e s s i o n f o r t h e g i v e n kmap .
2 // i t r e q u i r e s n o o f . s c i
3 // s o t h e a b o v e m e n t i o n e d f u n c t i o n shoub be e x e c u t e
before executing this function .

4
5
n =4;
6
k (: ,: ,2) = zeros (n , n ) ;
7
var =[ X2 X3 S X1 ];
8
9 p1 =[ X2 X3 X2 X3 X2X3 X2X3 ];
10 p2 =[ S X1 ; S X1 ; SX1 ; SX1 ];
11
cmn4 =4;
12
cmn2 =2;
13
temp =1;
14
disp ( k (: ,: ,1) ) ;
15
disp ( i s : ) ;
16
disp ( )
17
// c h e c k i n g t h e 16 c e l l s c a s e
18
for i =1: n
19
for j =1: n
20
if ( k (i , j ) ~=1)
21
temp =0;
22
break ;
23
end
24
end
25
end
26
printf (
);

192

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

if ( temp ==1)
printf ( 1 ) ;
abort ;
end
// c h e c k i n g t h e 8 c e l l s c a s e s
z1 = ones (2 ,4) ;
z2 = ones (4 ,2) ;
temp1 =[ 00 01 11 10 ];
temp2 = temp1 ;
for i =1: n
if ( i ==4)
t =1;
else
t = i +1;
end
z =[ k (i ,: ,1) ; k (t ,: ,1) ];
if ( z == z1 )
k (i ,: ,2) =[1 1 1 1];
k (t ,: ,2) =[1 1 1 1];
a = strsplit ( temp2 (i ,1) ) ;
b = strsplit ( temp2 (t ,1) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
printf ( %s , var ( in ) ) ;
printf ( + ) ;
break ;
else
if ( c ( in ) ==0 & a ( in ) == 1 )
printf ( var ( in ) ) ;
printf ( + ) ;
break ;
end
end
end
end
end
for j =1: n
193

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

if ( j ==4)
t =1;
else
t = j +1;
end
z =[ k (: ,j ,1) k (: ,t ,1) ];
if ( z == z2 )
k (: ,j ,2) =[1;1;1;1];
k (: ,t ,2) =[1;1;1;1];
a = strsplit ( temp1 (1 , j ) ) ;
b = strsplit ( temp1 (1 , t ) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
printf ( %s , var (2+ in ) ) ;
printf ( + ) ;
break ;
else
if ( c ( in ) ==0 & a ( in ) == 1 )
printf ( var (2+ in ) ) ;
printf ( + ) ;
break ;
end
end
end
end
end
// c h e c k i n g t h e 4 c e l l s c a s e s
z1 = ones (1 ,4) ;
z2 = ones (4 ,1) ;
z3 = ones (2 ,2) ;
temp1 =[ 00 01 11 10 ];
temp2 = temp1 ;
for t =1: n
z = k (t ,: ,1) ;
no = noof ( k (t ,: ,2) ) ;
if ( z == z1 & no < cmn4 )
k (t ,: ,2) = z1 ;
194

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

a = strsplit ( temp1 (1 , t ) ) ;
for in =1: max ( size ( a ) )
if ( a ( in ) == 0 )
printf ( %s , var ( in ) ) ;
end
if ( a ( in ) == 1 )
printf ( var ( in ) ) ;
end
end
printf ( + ) ;
end
end
for t =1: n
z = k (: ,t ,1) ;
no = noof ( k (: ,t ,2) ) ;
if ( z == z2 & no < cmn4 )
k (: ,t ,2) = z2 ;
a = strsplit ( temp2 (t ,1) ) ;
for in =1: max ( size ( a ) )
if ( a ( in ) == 0 )
printf ( %s , var (2+ in ) ) ;
end
if ( a ( in ) == 1 )
printf ( var (2+ in ) ) ;
end
end
printf ( + ) ;
end
end
for i =1: n
for j =1: n
if ( i == n )
t1 =1;
else
t1 = i +1;
end
if ( j == n )
t2 =1;
195

141
142
143
144

else
t2 = j +1;
end
z4 =[ k (i ,j ,1) k (i , t2 ,1) ; k ( t1 ,j ,1) k ( t1 , t2
,1) ];
z5 =[ k (i ,j ,2) k (i , t2 ,2) ; k ( t1 ,j ,2) k ( t1 , t2
,2) ];
no = noof ( z5 ) ;
if ( z4 == z3 & no < cmn4 )
k (i ,j ,2) =1;
k (i , t2 ,2) =1;
k ( t1 ,j ,2) =1;
k ( t1 , t2 ,2) =1;
a = strsplit ( temp2 (i ,1) ) ;
b = strsplit ( temp2 ( t1 ,1) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
printf ( %s , var ( in ) ) ;
end
if ( c ( in ) ==0 & a ( in ) == 1 )
printf ( var ( in ) ) ;
end
end
a = strsplit ( temp1 (1 , j ) ) ;
b = strsplit ( temp1 (1 , t2 ) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
printf ( %s , var (2+ in ) ) ;
end
if ( c ( in ) ==0 & a ( in ) == 1 )
printf ( var (2+ in ) ) ;
end
end
printf ( + ) ;
end

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

end
196

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

end
// c h e c k i n g a l l t h e 2 c e l l s c a s e s
z6 =[1 1];
z7 = z6 ;
for i =1: n
for j =1: n
if ( i == n )
t1 =1;
else
t1 = i +1;
end
if ( j == n )
t2 =1;
else
t2 = j +1;
end
z8 =[ k (i ,j ,1) k (i , t2 ,1) ];
z9 =[ k (i ,j ,2) k (i , t2 ,2) ];
no1 = noof ( z9 ) ;
if ( z8 == z6 & no1 < cmn2 )
k (i ,j ,2) =1;
k (i , t2 ,2) =1;
a = strsplit ( temp1 (1 , j ) ) ;
b = strsplit ( temp1 (1 , t2 ) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
printf ( p1 (1 , i ) ) ;
printf ( %s , var (2+ in ) ) ;
printf ( + ) ;
end
if ( c ( in ) ==0 & a ( in ) == 1 )
printf ( p1 (1 , i ) ) ;
printf ( var (2+ in ) ) ;
printf ( + ) ;
end
end
end
197

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

end
end
for i =1: n
for j =1: n
if ( i == n )
t1 =1;
else
t1 = i +1;
end
if ( j == n )
t2 =1;
else
t2 = j +1;
end
z10 =[ k (i ,j ,1) ; k ( t1 ,j ,1) ];
z11 =[ k (i ,j ,2) ; k ( t1 ,j ,2) ];
no2 = noof ( z11 ) ;
if ( z10 == z7 & no2 < cmn2 )
k (i ,j ,2) =1;
k ( t1 ,j ,2) =1;
a = strsplit ( temp2 (i ,1) ) ;
b = strsplit ( temp2 ( t1 ,1) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
printf ( p2 (j ,1) ) ;
printf ( %s , var ( in ) ) ;
printf ( + ) ;
end
if ( c ( in ) ==0 & a ( in ) == 1 )
printf ( p2 (j ,1) ) ;
printf ( var ( in ) ) ;
printf ( + ) ;
end
end
end
end
end
198

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

// c h e c k i n g a l l t h e s i n g l e c e l l c a s e s
for i =1: n
for j =1: n
if ( k (i ,j ,2) ==0 & k (i ,j ,1) ==1)
a = strsplit ( temp1 (1 , j ) ) ;
b = strsplit ( temp2 (i ,1) ) ;
for in =1: max ( size ( a (: ,1) ) )
if ( a ( in ,1) == 1 )
printf ( var ( in +2) ) ;
else
if ( a ( in ,1) == 0 )
printf ( %s , var (2+ in ) )
;
end
end
end
for in =1: max ( size ( b (: ,1) ) )
if ( b ( in ,1) == 1 )
printf ( var ( in ) ) ;
else
if ( b ( in ,1) == 0 )
printf ( %s , var ( in ) ) ;
end
end
end
if ( i ~=4 & j ~=4)
printf ( + ) ;
end
end
end
end
printf ( 0 ) ;
endfunction
Scilab code AP 10 4-variable kmap

199

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

// t h i s f u n t i o n p r i n t s t h e m i n i m a l e x p r e s s i o n o f a
g i v e n 4 v r i a b l e kmap
// t h i s program r e q u i r e s n o o f . s c i
function []= kmap ( k )
n =4;
k (: ,: ,2) = zeros (n , n ) ;
var =[ A B C D ];
p1 =[ A B A B AB AB ]
p2 =[ C D ; C D ; CD ; CD ];
cmn4 =4;
cmn2 =2;
temp =1;
disp ( k (: ,: ,1) ) ;
disp ( i s : ) ;
disp ( )
// c h e c k i n g f o r 16 c e l l s
for i =1: n
for j =1: n
if ( k (i , j ) ~=1)
temp =0;
break ;
end
end
end
printf (
);
if ( temp ==1)
printf ( 1 ) ;
abort ;
end
// c h e c k i n g 8 c e l l s c a s e s
z1 = ones (2 ,4) ;
z2 = ones (4 ,2) ;
temp1 =[ 00 01 11 10 ];
temp2 = temp1 ;
for i =1: n
if ( i ==4)
t =1;
else
200

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

t = i +1;
end
z =[ k (i ,: ,1) ; k (t ,: ,1) ];
if ( z == z1 )
k (i ,: ,2) =[1 1 1 1];
k (t ,: ,2) =[1 1 1 1];
a = strsplit ( temp2 (i ,1) ) ;
b = strsplit ( temp2 (t ,1) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
printf ( %s , var ( in ) ) ;
printf ( + ) ;
break ;
else
if ( c ( in ) ==0 & a ( in ) == 1 )
printf ( var ( in ) ) ;
printf ( + ) ;
break ;
end
end
end
end
end
for j =1: n
if ( j ==4)
t =1;
else
t = j +1;
end
z =[ k (: ,j ,1) k (: ,t ,1) ];
if ( z == z2 )
k (: ,j ,2) =[1;1;1;1];
k (: ,t ,2) =[1;1;1;1];
a = strsplit ( temp1 (1 , j ) ) ;
b = strsplit ( temp1 (1 , t ) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
201

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

if ( c ( in ) ==0 & a ( in ) == 0 )
printf ( %s , var (2+ in ) ) ;
printf ( + ) ;
break ;
else
if ( c ( in ) ==0 & a ( in ) == 1 )
printf ( var (2+ in ) ) ;
printf ( + ) ;
break ;
end
end
end
end
end
// c h e c k i n g a l l 4 c e l l s c a s e s
z1 = ones (1 ,4) ;
z2 = ones (4 ,1) ;
z3 = ones (2 ,2) ;
temp1 =[ 00 01 11 10 ];
temp2 = temp1 ;
for t =1: n
z = k (t ,: ,1) ;
no = noof ( k (t ,: ,2) ) ;
if ( z == z1 & no < cmn4 )
k (t ,: ,2) = z1 ;
a = strsplit ( temp1 (1 , t ) ) ;
for in =1: max ( size ( a ) )
if ( a ( in ) == 0 )
printf ( %s , var ( in ) ) ;
end
if ( a ( in ) == 1 )
printf ( var ( in ) ) ;
end
end
printf ( + ) ;
end
end
for t =1: n
202

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

z = k (: ,t ,1) ;
no = noof ( k (: ,t ,2) ) ;
if ( z == z2 & no < cmn4 )
k (: ,t ,2) = z2 ;
a = strsplit ( temp2 (t ,1) ) ;
for in =1: max ( size ( a ) )
if ( a ( in ) == 0 )
printf ( %s , var (2+ in ) ) ;
end
if ( a ( in ) == 1 )
printf ( var (2+ in ) ) ;
end
end
printf ( + ) ;
end
end
for i =1: n
for j =1: n
if ( i == n )
t1 =1;
else
t1 = i +1;
end
if ( j == n )
t2 =1;
else
t2 = j +1;
end
z4 =[ k (i ,j ,1) k (i , t2 ,1) ; k ( t1 ,j ,1) k ( t1 , t2
,1) ];
z5 =[ k (i ,j ,2) k (i , t2 ,2) ; k ( t1 ,j ,2) k ( t1 , t2
,2) ];
no = noof ( z5 ) ;
if ( z4 == z3 & no < cmn4 )
k (i ,j ,2) =1;
k (i , t2 ,2) =1;
k ( t1 ,j ,2) =1;
k ( t1 , t2 ,2) =1;
203

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

## a = strsplit ( temp2 (i ,1) ) ;

b = strsplit ( temp2 ( t1 ,1) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
printf ( %s , var ( in ) ) ;
end
if ( c ( in ) ==0 & a ( in ) == 1 )
printf ( var ( in ) ) ;
end
end
a = strsplit ( temp1 (1 , j ) ) ;
b = strsplit ( temp1 (1 , t2 ) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
printf ( %s , var (2+ in ) ) ;
end
if ( c ( in ) ==0 & a ( in ) == 1 )
printf ( var (2+ in ) ) ;
end
end
printf ( + ) ;
end
end
end
// c h e c k i n g a l l 2 c e l l s c a s e s
z6 =[1 1];
z7 = z6 ;
for i =1: n
for j =1: n
if ( i == n )
t1 =1;
else
t1 = i +1;
end
if ( j == n )
t2 =1;
204

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

else
t2 = j +1;
end
z8 =[ k (i ,j ,1) k (i , t2 ,1) ];
z9 =[ k (i ,j ,2) k (i , t2 ,2) ];
no1 = noof ( z9 ) ;
if ( z8 == z6 & no1 < cmn2 )
k (i ,j ,2) =1;
k (i , t2 ,2) =1;
a = strsplit ( temp1 (1 , j ) ) ;
b = strsplit ( temp1 (1 , t2 ) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
printf ( p1 (1 , i ) ) ;
printf ( %s , var (2+ in ) ) ;
printf ( + ) ;
end
if ( c ( in ) ==0 & a ( in ) == 1 )
printf ( p1 (1 , i ) ) ;
printf ( var (2+ in ) ) ;
printf ( + ) ;
end
end
end
end
end
for i =1: n
for j =1: n
if ( i == n )
t1 =1;
else
t1 = i +1;
end
if ( j == n )
t2 =1;
else
t2 = j +1;
205

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

end
z10 =[ k (i ,j ,1) ; k ( t1 ,j ,1) ];
z11 =[ k (i ,j ,2) ; k ( t1 ,j ,2) ];
no2 = noof ( z11 ) ;
if ( z10 == z7 & no2 < cmn2 )
k (i ,j ,2) =1;
k ( t1 ,j ,2) =1;
a = strsplit ( temp2 (i ,1) ) ;
b = strsplit ( temp2 ( t1 ,1) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
printf ( p2 (j ,1) ) ;
printf ( %s , var ( in ) ) ;
printf ( + ) ;
end
if ( c ( in ) ==0 & a ( in ) == 1 )
printf ( p2 (j ,1) ) ;
printf ( var ( in ) ) ;
printf ( + ) ;
end
end
end
end
end
// c h e c k i n g f o r i s o l a t e d c e l l
for i =1: n
for j =1: n
if ( k (i ,j ,2) ==0 & k (i ,j ,1) ==1)
a = strsplit ( temp1 (1 , j ) ) ;
b = strsplit ( temp2 (i ,1) ) ;
for in =1: max ( size ( a (: ,1) ) )
if ( a ( in ,1) == 1 )
printf ( var ( in +2) ) ;
else
if ( a ( in ,1) == 0 )
printf ( %s , var (2+ in ) )
;
206

264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283

end
end
end
for in =1: max ( size ( b (: ,1) ) )
if ( b ( in ,1) == 1 )
printf ( var ( in ) ) ;
else
if ( b ( in ,1) == 0 )
printf ( %s , var ( in ) ) ;
end
end
end
if ( i ~=4 & j ~=4)
printf ( + ) ;
end
end
end
end
printf ( 0 ) ;
endfunction
Scilab code AP 11 4-variable kmap pos

1
2
3
4
5
6
7
8
9
10
11
12
13
14

// t h i s f u n t i o n p r i n t s t h e m i n i m a l e x p r e s s i o n i n t h e
P r u d u c t o f sums form f o r a g i v e n 4 v r i a b l e kmap
// t h i s program r e q u i r e s n o o f . s c i
function []= kmappos ( k )
n =4;
k (: ,: ,2) = zeros (n , n ) ;
var =[ A B C D ];
p1 =[ A + B A + B A + B A + B ];
p2 =[ C + D ; C + D ; C + D ; C + D ];
cmn4 =4;
cmn2 =2;
temp =1;
disp ( k (: ,: ,1) ) ;
disp ( i s : ) ;
disp ( )
207

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

// c h e c k i n g f o r 16 c e l l s
for i =1: n
for j =1: n
if ( k (i , j ) ~=1)
temp =0;
break ;
end
end
end
printf (
);
if ( temp ==1)
printf ( 1 ) ;
abort ;
end
for i =1: n
for j =1: n
if ( k (i , j ) ~=0)
temp =0;
break ;
end
end
end
printf (
);
if ( temp ==1)
printf ( 0 ) ;
abort ;
end
// c h e c k i n g f o r 8 c e l l s c a s e s
z1 = zeros (2 ,4) ;
z2 = zeros (4 ,2) ;
temp1 =[ 00 01 11 10 ];
temp2 = temp1 ;
for i =1: n
if ( i ==4)
t =1;
else
t = i +1;
208

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

end
z =[ k (i ,: ,1) ; k (t ,: ,1) ];
if ( z == z1 )
printf ( ( ) ;
k (i ,: ,2) =[1 1 1 1];
k (t ,: ,2) =[1 1 1 1];
a = strsplit ( temp2 (i ,1) ) ;
b = strsplit ( temp2 (t ,1) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
printf ( var ( in ) ) ;
break ;
else
if ( c ( in ) ==0 & a ( in ) == 1 )
printf ( %s , var ( in ) ) ;
break ;
end
end
end
printf ( ) ) ;
end
end
for j =1: n
if ( j ==4)
t =1;
else
t = j +1;
end
z =[ k (: ,j ,1) k (: ,t ,1) ];
if ( z == z2 )
printf ( ( ) ;
k (: ,j ,2) =[1;1;1;1];
k (: ,t ,2) =[1;1;1;1];
a = strsplit ( temp1 (1 , j ) ) ;
b = strsplit ( temp1 (1 , t ) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
209

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

if ( c ( in ) ==0 & a ( in ) == 0 )
printf ( var (2+ in ) ) ;
break ;
else
if ( c ( in ) ==0 & a ( in ) == 1 )
printf ( %s , var (2+ in ) ) ;
break ;
end
end
end
printf ( ) )
end
end
// c h e c k i n g f o r 4 c e l l s c a s e s
z1 = zeros (1 ,4) ;
z2 = zeros (4 ,1) ;
z3 = zeros (2 ,2) ;
temp1 =[ 00 01 11 10 ];
temp2 = temp1 ;
for t =1: n
z = k (t ,: ,1) ;
no = noof ( k (t ,: ,2) ) ;
if ( z == z1 & no < cmn4 )
printf ( ( )
k (t ,: ,2) =[1 1 1 1];
a = strsplit ( temp1 (1 , t ) ) ;
for in =1: max ( size ( a ) )
if ( a ( in ) == 0 )
if in ~= 1 then
printf ( + ) ;
end
printf ( var ( in ) ) ;
end
if ( a ( in ) == 1 )
if in ~= 1 then
printf ( + ) ;
end
printf ( %s , var ( in ) ) ;
210

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

end
end
printf ( ) ) ;
end
end
for t =1: n
z = k (: ,t ,1) ;
no = noof ( k (: ,t ,2) ) ;
if ( z == z2 & no < cmn4 )
printf ( ( ) ;
k (: ,t ,2) =[1;1;1;1];
a = strsplit ( temp2 (t ,1) ) ;
for in =1: max ( size ( a ) )
if ( a ( in ) == 0 )
if in ~= 1 then
printf ( + ) ;
end
printf ( var (2+ in ) ) ;
end
if ( a ( in ) == 1 )
if in ~= 1 then
printf ( + ) ;
end
printf ( %s , var (2+ in ) ) ;
end
end
printf ( ) ) ;
end
end
for i =1: n
for j =1: n
if ( i == n )
t1 =1;
else
t1 = i +1;
end
if ( j == n )
t2 =1;
211

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

else
t2 = j +1;
end
z4 =[ k (i ,j ,1) k (i , t2 ,1) ; k ( t1 ,j ,1) k ( t1 , t2
,1) ];
z5 =[ k (i ,j ,2) k (i , t2 ,2) ; k ( t1 ,j ,2) k ( t1 , t2
,2) ];
no = noof ( z5 ) ;
if ( z4 == z3 & no < cmn4 )
printf ( ( )
k (i ,j ,2) =1;
k (i , t2 ,2) =1;
k ( t1 ,j ,2) =1;
k ( t1 , t2 ,2) =1;
a = strsplit ( temp2 (i ,1) ) ;
b = strsplit ( temp2 ( t1 ,1) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
printf ( var ( in ) ) ;
end
if ( c ( in ) ==0 & a ( in ) == 1 )
printf ( %s , var ( in ) ) ;
end
end
a = strsplit ( temp1 (1 , j ) ) ;
b = strsplit ( temp1 (1 , t2 ) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
printf ( + ) ;
printf ( var (2+ in ) ) ;
end
if ( c ( in ) ==0 & a ( in ) == 1 )
printf ( + ) ;
printf ( %s , var (2+ in ) ) ;
end
212

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

end
printf ( ) ) ;
end
end
end
// c h e c k i n g f o r 2 c e l l s
z6 =[0 0];
z7 = z6 ;
for i =1: n
for j =1: n
if ( i == n )
t1 =1;
else
t1 = i +1;
end
if ( j == n )
t2 =1;
else
t2 = j +1;
end
z8 =[ k (i ,j ,1) k (i , t2 ,1) ];
z9 =[ k (i ,j ,2) k (i , t2 ,2) ];
no1 = noof ( z9 ) ;
if ( z8 == z6 & no1 < cmn2 )
printf ( ( ) ;
k (i ,j ,2) =1;
k (i , t2 ,2) =1;
a = strsplit ( temp1 (1 , j ) ) ;
b = strsplit ( temp1 (1 , t2 ) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
printf ( p1 (1 , i ) ) ;
printf ( + ) ;
printf ( var (2+ in ) ) ;
printf ( ) ) ;
end
213

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

if ( c ( in ) ==0 & a ( in ) == 1 )
printf ( p1 (1 , i ) ) ;
printf ( + ) ;
printf ( %s , var (2+ in ) ) ;
printf ( ) ) ;
end
end
end
end
end
for i =1: n
for j =1: n
if ( i == n )
t1 =1;
else
t1 = i +1;
end
if ( j == n )
t2 =1;
else
t2 = j +1;
end
z10 =[ k (i ,j ,1) ; k ( t1 ,j ,1) ];
z11 =[ k (i ,j ,2) ; k ( t1 ,j ,2) ];
no2 = noof ( z11 ) ;
if ( z10 == z7 & no2 < cmn2 )
printf ( ( ) ;
k (i ,j ,2) =1;
k ( t1 ,j ,2) =1;
a = strsplit ( temp2 (i ,1) ) ;
b = strsplit ( temp2 ( t1 ,1) ) ;
c = strcmp (a , b ) ;
for in =1: max ( size ( c ) )
if ( c ( in ) ==0 & a ( in ) == 0 )
printf ( p2 (j ,1) ) ;
printf ( + ) ;
printf ( var ( in ) ) ;
214

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

printf ( ) ) ;
end
if ( c ( in ) ==0 & a ( in ) == 1 )
printf ( p2 (j ,1) ) ;
printf ( + ) ;
printf ( %s , var ( in ) ) ;
printf ( ) ) ;
end
end
end
end
end
// f o r s i n g l e c e l l
for i =1: n
for j =1: n
if ( k (i ,j ,2) ==0 & k (i ,j ,1) ==0)
printf ( ( ) ;
a = strsplit ( temp1 (1 , j ) ) ;
b = strsplit ( temp2 (i ,1) ) ;
for in =1: max ( size ( a (: ,1) ) )
if ( a ( in ,1) == 1 )
printf ( %s , var (2+ in ) ) ;
printf ( + ) ;
else
if ( a ( in ,1) == 0 )
printf ( var ( in +2) ) ;
printf ( + ) ;
end
end
end
for in =1: max ( size ( b (: ,1) ) )
if ( b ( in ,1) == 1 )
printf ( %s , var ( in ) ) ;
if ( in ~= max ( size ( b (: ,1) ) ) )
printf ( + ) ;
end
215

317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332

else
if ( b ( in ,1) == 0 )
printf ( var ( in ) ) ;
if ( in ~= max ( size ( b (: ,1) ) ) )
printf ( + ) ;
end
end
end
end
printf ( ) ) ;
end
end
end
endfunction

216