BCD To Binary
BCD To Binary
BCD To Binary
CONVERTER
ABSTRACT
INTRODUCTION
BCD's main virtue is its more accurate representation and rounding of decimal
quantities as well as an ease of conversion into human-readable representations, in
comparison to binary positional systems. BCD's principal drawbacks are a small
increase in the complexity of the circuits needed to implement basic arithmetic and a
slightly less dense storage.
BCD was used in many early decimal computers. Although BCD is not as widely
used as in the past, decimal fixed-point and floating-point formats are still important
and continue to be used in financial, commercial, and industrial computing, where
subtle conversion and fractional rounding errors that are inherent in floating point binary
representations cannot be tolerated.
BCD takes advantage of the fact that any one decimal numeral can be
represented by a four bit pattern. The most obvious way of encoding digits is "natural
BCD" (NBCD), where each decimal digit is represented by its corresponding four-bit
binary value, as shown in the following table. This is also called "8421" encoding.
Decimal BCD
Digit 8421
0 0000
1 0001
2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
Other encodings are also used, including so-called "4221" and "7421" — named
after the weighting used for the bits — and "excess-3". For example the BCD digit 6,
'0110'b in 8421 notation, is '1100'b in 4221 (two encodings are possible), '0110'b in 7421,
and '1001'b (6+3=9) in excess-3.
As most computers deal with data in 8-bit bytes, it is possible to use one of the
following methods to encode a BCD number:
Uncompressed: each numeral is encoded into one byte, with four bits representing
the numeral and the remaining bits having no significance.
Packed: two numerals are encoded into a single byte, with one numeral in the least
significant nibble (bits 0 through 3) and the other numeral in the most significant
nibble (bits 4 through 7).
The 4-bit BCD system is usually employed by the computer systems to represent and
process numerical data only. In the 4-bit BCD system, each digit of the decimal number
is encoded to its corresponding 4-bit binary sequence. The two most popular 4-bit BCD
systems are:
The weighted 4-bit BCD code is more commonly known as 8421 weighted code.It
is called weighted code because it encodes the decimal system into binary system by
using the concept of positional weighting into consideration.In this code, each decimal
digit is encoded into its 4-bit binary number in which the bits from left to right have the
weights 8, 4, 2, and 1, respectively.
The Excess-3 (XS-3) BCD code does not use the principle of positional weights
into consideration while converting the decimal numbers to 4-bit BCD system. Therefore,
we can say that this code is a non-weighted BCD code.The function of XS-3 code is to
transform the decimal numbers into their corresponding 4-bit BCD code.In this code, the
decimal number is transformed to the 4-bit BCD code by first adding 3 to all the digits of
the number and then converting the excess digits, so obtained, into their corresponding
8421 BCD code. Therefore, we can say that the XS-3 code is strongly related with 8421
BCD code in its functioning.
Binary System
The binary system uses base 2 to represent different values. Therefore, the
binary system is also known as base-2 system. As this system uses base 2, only two
symbols are available for representing the different values in this system. These
symbols are 0 and 1, which are also known as bits in computer terminology.Using binary
system, the computer systems can store and process each type of data in terms of 0s
and 1s only.
The following are some of the technical terms used in binary system:
Bit: It is the smallest unit of information used in a computer system. It can either
have the value 0 or 1. Derived from the words Binary ditIT.
Nibble: It is a combination of 4 bits.
Byte: It is a combination of 8 bits.
Word: It is a combination of 16 bits.
Double word: It is a combination of 32 bits.
Kilobyte (KB): It is used to represent the 1024 bytes of information.
Megabyte (MB): It is used to represent the 1024 KBs of information.
Gigabyte (GB): It is used to represent the 1024 MBs of information.
We can determine the weight associated with each bit in the given binary number in the
similar manner as we did in the decimal system.In the binary system, the weight of any
bit can be determined by raising 2 to a power equivalent to the position of bit in the
number.
1011.101
BINARY CODE
In computing and telecommunication, binary codes are used for various methods
of encoding data, such as character strings, into bit strings. Those methods may use
fixed-width or variable-width strings. In a fixed-width binary code, each letter, digit, or
other character is represented by a bit string of the same length; that bit string,
interpreted as a binary number, is usually displayed in code tables
in octal, decimal or hexadecimal notation. There are many character sets and
many character encodings for them.
BCD IN ELECTRONICS
The same argument applies when hardware of this type uses an embedded
microcontroller or other small processor. Often, smaller code results when representing
numbers internally in BCD format, since a conversion from or to binary representation
can be expensive on such limited processors. For these applications, some small
processors feature BCD arithmetic modes, which assist when writing routines that
manipulate BCD quantities.
LITERATURE SURVEY
CHAPTER
PROPOSED ALGORITHM
The main objective of the proposed algorithm is to performhighly efficient fixed bit
binary to BCD conversion in terms of delay, power and area. As mentioned earlier, most
of the recently proposed multipliers use 7-bit binary to 8-bit/2-digit BCD converters. The
proposed algorithm has been specifically designed for such converters.
Let p6p5p4p3p2p1p0 be the seven binary bits to be converted intotwo BCD digits.
To convert these binary bits into 2-digit BCD we split the binary number into two parts,
the first part contains the lower significant bits (LSBs) p3, p2, p1 and p0 while the
second part contains the remaining higher significant bits (HSBs) p6, p5 and p4. The
lower significant part (LSBs) has the same weight as that of a BCD digit and can be
directly used to represent a BCD digit. The only exception arrives when p3p2p1p0
exceeds (1001)2 or (9)10. To convert the LSBs into a valid BCD number we check
whether p3p2p1p0 exceeds (1001)2, and if it does, we add (0110)2 to it. This procedure
of adding (0110)2 whenever the number exceeds (1001)2 is called correction in BCD
arithmetic. The carry obtained from this procedure is added to the higher significant
BCD digit calculated from the HSBs of the original binary number. The HSBs not only
contribute to the higher significant BCD digit but also to the lower significant BCD digit.
These contributions of HSBs towards the lower significant digit are added after BCD
correction. The resulting sum is then checked for the case (1001)2 and correction is
done if needed to obtain the final lower significant BCD digit.
A possible carry from the above operation is added to the higher significant digit
resulting in the final higher significant BCD digit. When two BCD digits are multiplied
only six combinations of p6, p5 and p4 (HSBs) are possible, which are 000, 001, 010,
011, 100 and 101. Each of these combinations have a different contribution towards the
lower and higher significant BCD digits. This contribution can be easily calculated by
evaluating the weights of the patterns which are p6x27 + p5x26+ p4x25. Contribution of
each of these patterns towards the lower and higher BCD digits is shown in table.
Maximum utilization of the fact that only limited and small numbersof outcomes
are possible for conversion has been made in designing the architecture to reduce delay,
power and area. Figure shows the proposed architecture.
Figure: Proposed Architecture
p6p5p4p3p2p1p0 are the binary bits to beconverted into BCD bits z7z6z5z4
z3z2z1z0. p6, p5 and p4 are the HSBs while p3, p2, p1 and p0 are the LSBs. z0 is same
as p0 and hence no operation is done on p0. {p3, p2 and p1} are used to check whether
the LSBs are greater than (1001)2 or not using equation (1) and are sent to the BCD
Correction block.
C1 = p3. (p2 + p1) - (1)
Whenever C1 is high, BCD Correction block adds 011 to the input bits. Figure
shows the implementation of BCD Correction block.
C1 is the carry from the lower significant digit, so it is added to the higher significant
digit t3t2t1t0. It is found that very few cases lead to the propagation of the incoming
carry from t1 to t2. Hence, we take advantage of this situation and implement {t3, t2} in
combinational logic thus removing the need to add C1 to these terms, thus saving
hardware and complexity. 2-bit One Adder, as shown in Figure, isused to add C1 to t0
and t1. There is a possibility of a carrygeneration, when the contributions of HSBs are
added to the corrected LSBs (a3, a2 and a1). This carry is calculated beforehand by a
Carry Generator block using C1 and input bits p6 to p1. The logic implemented by Carry
Generator is given by the equation below
C2 = C1‟ (p4 (p3+p2) +p3p5) + p6p3 + p4p3p1 -(6)
Figure: 2-bit One Adder
C2 is also added to result of the first 2-bit One Adder using another 2-bit One Adder and
the final higher significant digit is Higher Significantobtained. {t3 and t2} are equal to z7
and z6 respectively and aredirectly available from the Contribution Generator block.
Contribution of HSBs towards lower significant BCD digit is fixed and unique and
is known once HSBs are known. We have implemented four distinct adder units which
add only specified values to the inputs in parallel according to the contributions in Table.
The different adder blocks, +1, +2, +3 and +4 (shown in Figure) add 001, 010, 011 and
100 to the input bits respectively. Adder blocks take the corrected LSBs (a3, a2, a1) as
inputs and add specific numbers to them. The appropriate result is then obtained
through a multiplexer whose selection bits are p6, p5 and p4 (HSBs). The result from
the multiplexer is then fed to BCD
Correction block which takes C2 as input to decide whether correction has to be
done or not. The results obtained from the BCD Correction block are z3, z2 and z1 which,
along with z0, form the final lower significant BCD digit.
Figure: +1, +2, +3, +4 adder blocks
CHAPTER
VLSI TECHNOLOGY
Gone are the days when huge computers made of vacuum tubes sat humming in
entire dedicated rooms and could do about 360 multiplications of 10 digit numbers in a
second. Though they were heralded as the fastest computing machines of that time,
they surely don’t stand a chance when compared to the modern day machines. Modern
day computers are getting smaller, faster, and cheaper and more power efficient every
progressing second. But what drove this change? The whole domain of computing
ushered into a new dawn of electronic miniaturization with the advent of semiconductor
transistor by Bardeen (1947-48) and then the Bipolar Transistor by Shockley (1949) in
the Bell Laboratory.
Fig.: A comparison: first planar IC(1961) and Intel nehalem quad core die
Since the invention of the first IC (Integrated Circuit) in the form of a Flip Flop by
Jack Kilby in 1958, our ability to pack more and more transistors onto a single chip has
doubled roughly every 18 months, in accordance with the Moore’s Law. Such
exponential development had never been seen in any other field and it still continues to
be a major area of research work.
The development of microelectronics spans a time which is even lesser than the
average life expectancy of a human, and yet it has seen as many as four generations
Early 60’s saw the low density fabrication processes classified under Small Scale
Integration (SSI) in which transistor count was limited to about 10. This rapidly gave
way to Medium Scale Integration in the late 60’s when around 100 transistors could be
placed on a single chip.
It was the time when the cost of research began to decline and private firms
started entering the competition in contrast to the earlier years where the main burden
was borne by the military. Transistor-Transistor logic (TTL) offering higher integration
densities outlasted other IC families like ECL and became the basis of the first
integrated circuit revolution. It was the production of this family that gave impetus to
semiconductor giants like Texas Instruments, Fairchild and National Semiconductors.
Early seventies marked the growth of transistor count to about 1000 per chip called the
Large Scale Integration.
By mid-eighties, the transistor count on a single chip had already exceeded 1000
and hence came the age of Very Large Scale Integration or VLSI. Though many
improvements have been made and the transistor count is still rising, further names of
generations like ULSI are generally avoided. It was during this time when TTL lost the
battle to MOS family owing to the same problems that had pushed vacuum tubes into
negligence, power dissipation and the limit it imposed on the number of gates that
could be placed on a single die.
The second age of Integrated Circuits revolution started with the introduction of
the first microprocessor, the 4004 by Intel in 1972 and the 8080 in 1974. Today many
companies like Texas Instruments, Infineon, Alliance Semiconductors, Cadence,
Synopsys, Celox Networks, Cisco, Micron Tech, National Semiconductors, ST
Microelectronics, Qualcomm, Lucent, Mentor Graphics, Analog Devices, Intel, Philips,
Motorola and many other firms have been established and are dedicated to the various
fields in "VLSI" like Programmable Logic Devices, Hardware Descriptive Languages,
Design tools, Embedded Systems etc.
CHALLENGES
Stricter design rules – Due to lithography and etch issues with scaling, design
rules for layout have become increasingly stringent. Designers must keep ever more of
these rules in mind while laying out custom circuits. The overhead for custom design is
now reaching a tipping point, with many design houses opting to switch to electronic
design automation (EDA) tools to automate their design process.
Timing/design closure – As clock frequencies tend to scale up, designers are finding
it more difficult to distribute and maintain low clock skew between these high frequency
clocks across the entire chip. This has led to a rising interest in multicore and
multiprocessor architectures, since an overall speedup can be obtained by lowering the
clock frequency and distributing processing.
CHAPTER
Introduction of HDL’s
The most satisfying step is simulation or verification. The HDL simulator allows
to define and apply the inputs to the design and to observe its outputs without ever
having to build the physical circuit. There are at least two dimensions to verification. In
timing verification, the circuit operation including estimated delays, the setup, hold and
other timing requirements for sequential devices like flip flops are met. In the functional
verification the circuits logical operation independent of timing considerations; gate
delays and other timing parameters are considered to be zero.
After verification step, the synthesis process is done in the back end stage. There
are three basic steps, the first synthesis, converting the HDL description into a set of
primitive or components that can be assembled in the target technology and it may
generate a list of gates and a net list that specifies how they are interconnected.
In the fitter step, a fitter maps the synthesized components on to available device
resources. It may mean selecting microcells or laying down individual gates in a pattern
and finding ways to connect them within the physical constraints of the FPGA or ASIC
die, is called as place and route process.
The final step is post fitting verification of the fitted circuit. It is only at the stage
that the actual circuit delays due to wire lengths, electrical loading, and other factors
can be calculated with reasonable precision.
HDL Tool Suites
HDL tool suite really has several different tools with their own names and
purposes:
A text editor allows to write, edit and save an HDL program. It often contains HDL
specific features, such as recognizing specific file name extensions and
recognizing HDL reserved and comments and displaying them in different colors.
The compiler is responsible for parsing the HDL program, finding syntax errors
andfiguring out what the program really says.
A synthesizer or synthesis tools targets the design to a specific hardware
technology,such as FPGA, ASIC etc...
The simulator runs the specified input sequence on the described hardware
anddetermines the values of the hardware’s internal signals and its outputs over
a specifiedperiod of time.
The output of the simulator can be include waveforms to be viewed usingthe
waveform editor.
A schematic viewer may create a schematic diagram corresponding to an HDL
program,based on the intermediate-language output of the compiler.
A translator targets the compilers intermediate language output to a real device
such as
A timing analysercalculates the delays through some or all of the signal paths in
thefinal chip and produces a report showing the worst case paths and their
delays.
VHDL
VHDL Hardware Description Language
VHDL Advantages
The key advantage of VHDL, when used for systems design, is that it allows the
behavior of the required system to be described (modelled) and verified (simulated)
before synthesis tools translate the design into real hardware (gates and wires).
VHDL project is portable. Being created for one element base, a computing
device project can be ported on another element base, for example VLSI with various
technologies. Concurrency, timing and clocking can be modelled.
VHDL allows for various design methodologies, both the top-down, bottom-up
and is very flexible in its approach to describing hardware.
VHDL History and Features
In the mid-1980s, the U.S. Department of Defence (DOD) and the IEEE sponsored
the development of a highly capable hardware description language called VHDL and
this was got extended in 1993 and again 2002. And some of the features of the VHDL
are:
composed of one entity and one of more architecture. The entity represents the
interface
Types of Architectures
There are three general types of architectures. A VHDL Model can be created at
different abstraction levels (behavioral, dataflow, structural), according to a refinement
of starting specification.
Dataflow Modeling
Structural Modeling
Behavioral Modeling
VERILOG
Verilog is one of the two major Hardware Description Languages (HDL) used by
hardware designers in industry and academia. VHDL is the other one. The industry is
currently split on which is better. Many feel that Verilog is easier to learn and use than
VHDL. As one hardware designer puts it, "I hope the competition uses VHDL." VHDL was
made an IEEE Standard in 1987, while Verilog is still in the IEEE standardization process.
History of Verilog
Importance of HDLs
Designs can be described at a very abstract level by use of HDLs. Designers can
write their RTL description without choosing a specific fabrication technology.
Logic synthesis tools can automatically convert the design to any fabrication
technology. If a new technology emerges, designers do not need to redesign their
circuit. They simply input the RTL description to the logic synthesis tool and
create a new gate-level netlist, using the new fabrication technology. The logic
synthesis tool will optimize the circuit in area and timing for the new technology.
By describing designs in HDLs, functional verification of the design can be done
early in the design cycle. Since designers work at the RTL level, they can optimize
and modify the RTL description until it meets the desired functionality. Most
design bugs are eliminated at this point. This cuts down design cycle time
significantly because the probability of hitting a functional bug at a later time in
the gate-level netlist or physical layout is minimized.
Designing with HDLs is analogous to computer programming. A textual
description with comments is an easier way to develop and debug circuits. This
also provides a concise representation of the design, compared to gate-level
schematics. Gate-level schematics are almost incomprehensible for very
complex designs.
XILINX ISE
The Xilinx ISE tools allow you to use schematics, hardware description language
(HDLs), and specially designed modules in number of ways. Schematics are drawn by
using symbols for components and lines for wires. Xilinx tools is a suite of software
tools used for the design of digital circuits implemented using Xilinx Field
Programmable Gate Array(FPGA) or Complex programmable logic Device (CPLD).
The design procedure consists of (a) design entry, (b) synthesis and
implementation of the design,(c) functional simulation and (d) testing and verification.
Digital designs can be entered in various ways using the above CAD tools: using a
schematic hardware description language (HDL) – Verilog or VHDL or a combination of
both. In this lab we will only use the design flow that involves the use of Verilog HDL.
DESIGN ENTRY
Design entry is the first step in the ISE design flow. During design entry, you
create your source files based on your design objectives. You can create your top-level
design file using a Hardware Description Language (HDL), such as VHDL, Verilog, or
ABEL, or using schematic. You can use multiple formats for the lower-level source files
in your design.
SYNTHESIS
After design entry and optimal simulation, you run synthesis. During this step,
VHDL, Verilog, or mixed language designs become net list files that are accepted as
input to the implementation step.
IMPLEMENTATION
After synthesis, you run design implementation, which converts the logical
design into a physical file format that can be downloaded to selected target device.
From project navigator, you can run the implementation process in one step, or you can
run each of the implementation separately. Implementation processes vary depending
on whether you are targeting a Field Programmable Gate Array (FPGA) or a Complex
Programmable Logic Device (CPLD).
VERIFICATION
You can verify the functionality of your design at several points in the design
flow. You can use simulator software to verify the functionality and timing of your
design or a portion of your design. The simulator interprets VHDL or Verilog code into
circuit functionality and displays logical results of described HDL to determine correct
circuit operation. Simulation allows you to create and verify complex functions in a
relatively small amount of time. You can also run in-circuit verification after
programming your device.
DEVICE INSTALLATION
ISE
Xilinx ISE is a Hardware Description Language (HDL) simulator that enables you
to perform functional and timing simulations for VHDL, Verilog and mixed VHDL/Verilog
designs.
Now that you have a test bench in your project, you can perform behavioral
simulation on the design using ISE. The ISE software has full integration with ISE. The
ISE software enables ISE to create the work directory, compile the source files, load
the design, and perform simulation based on simulation properties.
In the Hierarchy pane of the Project Navigator Design Panel, right-click the
device line (xc3s100E-5tq114), and select “Design Properties”.
In the Design Properties dialog box, set the simulator field to “ISE
(VHDL/Verilog)”.
The simulation processes in the ISE software enable you to run simulation on
the design using ISE.
To locate the ISE processes, do the following:
In the View Pane of the Project Navigator Design Panel, select “Simulation”, and
select “Behavioral” from the drop-down list.
In the Hierarchy Pane, select the test bench files (ex: stopwatch_tb).
In the Processes Pane, expand “ISE Simulator” to view the process hierarchy.
Check “Syntax” .This process checks for syntax errors in the test bench.Simulate
“Behavioral Model” .This process starts the design simulation.
You will perform a behavioral simulation on the stopwatch design after you set
process properties for simulation.
The ISE software allows you to set several ISE properties in addition to the
simulation net list properties. To see the behavioral simulation properties and to modify
the properties for this tutorial, do the following:
In the Process Properties dialog box, set the property display level to
“Advanced”. This global setting enables you to see all available properties.
After the process Properties have been set, you are ready to run ISE to simulate
the design. To start the behavioral simulation, double-click “Simulate Behavioral Model”.
ISE creates the work directory, compiles the source files, loads the design, and performs
simulation for the time specified.
CHAPTER
CONCLUSION
All the architectures have been described using Verilog HDL. Delay, power and
area values for the designs are obtained by synthesizing the Verilog HDL description.
The proposed converteris flexible and can be plugged into any homogeneous
multiplicationarchitectures to achieve better performance irrespective of the method
used to generate binary partial products.
APPLICATIONS
1. The BIOS in many personal computers stores the date and time in BCD because
the MC6818 real-time clock chip used in the original IBM PC AT motherboard
provided the time encoded in BCD. This form is easily converted into ASCII for
display.
3. Early models of the PlayStation 3 store the date and time in BCD. This led to a
worldwide outage of the console on 1 March 2010. The last two digits of the year
stored as BCDwere misinterpreted as 16 causing an error in the unit's date,
rendering most functions inoperable.
ADVANTAGES
Scaling by a factor of 10 (or a power of 10) is simple; this is useful when a decimal
scaling factor is needed to represent a non-integer quantity (e.g., in financial
calculations)
Alignment of two decimal numbers (for example 1.3 + 27.08) is a simple, exact, shift.
REFERENCES
[1] IEEE standard for floating-point arithmetic. IEEE SC, Oct. 2006 at
http://754r.ucbtest.org/drafts/754r.pdf
[2] Erle, M.A.; Schwarz, E.M.; Schulte, M.J., "Decimal multiplication with efficient partial
product generation," 17thIEEE Symposium on Computer Arithmetic, 2005. ARITH-17
2005, 27-29 June 2005, pages21-28.
[3] Erle, M.A.; Schulte, M.J., "Decimal multiplication via carrysaveaddition," Proceedings.
IEEE International Conference on Application-Specific Systems, Architectures, and
Processors,
2003, 24-26 June, 2003 Page(s):348 - 358.
[4] A; Antelo, E M"i Vazquez A;Antelo, E; Montuschi P, "A New Family of High-
Performance Parallel Decimal Multipliers" in the 18th IEEE Symposium on Computer
Arithmetic 25-27
June 2007.
[6] Decimal multiplication using compact BCD multiplier James, R.K.; Shahana, T.K.;
Jacob, K.P.; Sasi, S.; Electronic Design, 2008. ICED 2008. International Conference on 1-
3 Dec. 2008
Page(s):1 – 6.
[7] Binary-coded decimal digit multipliers Jaberipur, G.; Kaivani, A. Computers and
Digital Techniques, IET Volume 1, Issue 4, July 2007 Page(s):377 - 381.
[8] Novel High-Speed Architecture for 32-Bit Binary Coded Decimal (BCD) Multiplier
Veeramachaneni, S.; Srinivas, M.B. Communications and Information Technologies,
2008. ISCIT 2008. International Symposium on 21-23 Oct. 2008 Page(s):543 – 546.
[9] Improving the Speed of Parallel Decimal Multiplication Jaberipur, Ghassem; Kaivani,
Amir; Computers, IEEE Transactions on Volume 58, Issue 11, Nov. 2009 Page(s):1539 -
1552.
[10] Schmookler, M.: „High-speed binary-to-decimal conversion‟, IEEETrans. Comput.,
1968, 17, (5), pages. 506–508.
[11] Rhyne, V.T.: „Serial binary-to-decimal and decimal-to-binary conversion‟, IEEE Trans.
Comput., 1970, 19, (9), pages. 808–812.
[12] Arazi, B., and Naccache, D.: „Binary-to-decimal conversion based on the 28 2 1 by 5‟,
Electron. Lett., 1992, 28, (23), pages. 2151–2152.
SIMULATION RESULTS
RTL Schematic:
Internal Schematic:
Simulation:
RTL CODE:
module final_binary_bcd_mul(
input [6:0] p,
output [7:0] z
);
//WIRE DECLARATION
wire c1,c2;
wire t0,t1,t2,t3;
wire [2:0] a,e,f,g,h;
wire [2:0] y;
wire o1,o2;
wire [2:0]s_1,s_2,s_3,s_4;
wire q1,q2,q3,q4,out1;
// CONTRIBUTION GENERATOR
assign c2 = ((~c1)&(p[4]&(p[3]|p[2])))|(p[3]&p[5])|(p[6]&p[3])|(p[4]&p[3]&p[2]);
assign t0 = ((~p[6])&(~p[5])&p[4])|(p[5]&(~p[4]));
assign t1 = (p[5]|p[6])&(~p[4]);
assign t2 = (p[5]&(p[4]|p[3]))|(p[6]&(~p[4]));
assign t3 = p[6] & p[4];
//BCD CORRECTION
assign c1 = p[3]&(p[2]|p[1]);
// always@(*)
// begin
// if(c1==1'b1)
// adds = p[3:1] + 3'b011;
// else
// adds = p[3:1];
// end
//
//bcd_correction BCD(adds[0],adds[1],adds[2],c1,a[0],a[1],a[2]);
add1_block add1(a[0],a[1],a[2],e[0],e[1],e[2]);
add2_block add2(a[0],a[1],a[2],f[0],f[1],f[2]);
add3_block add3(a[0],a[1],a[2],g[0],g[1],g[2]);
add4_block add4(a[0],a[1],a[2],h[0],h[1],h[2]);
//MULTIPLEXER ARRAY
// multiplexer_array mux(g,f,e,h,a,3'b0,3'b0,3'b0,p[6:4],y);
mux21 m1(e,h,p[4],s_1);
mux21 m2(f,g,p[4],s_2);
mux21 m3(s_2,s_1,p[5],s_3);
//xor u0(p[4],p[6],t_1);
//
// not u1(q2,q3);
// not u2(p[5],t_3);
// and u4(t,t_3,t_4);
assign q1 = p[4] ^ p[6];
assign q2 = ~ q1;
assign q3 = ~ p[5];
assign q4 = q2 & q3;
mux21 m4(s_3,a,q4,s_4);
//BCD CORRECTION CIRCUIT
bcd_correction BCD1(s_4[0],s_4[1],s_4[2],c2,z[1],z[2],z[3]);
twobit_one_adder bit2(t0,t1,c1,o1,o2);
twobit_one_adder bit1(o1,o2,c2,z[4],z[5]);
endmodule
module bcd_correction(
input s1,s2,s3,
input c,
output o1,o2,o3
);
wire y1,y2,y3;
assign o1 = (s1&(~c))|((~s1)&c);
assign y1 = (s2&s1)|((~s2)&(~s1));
assign o2 = (s2&(~c))|(y1&c);
assign y2 = s1 | s2;
assign y3 = (s3&(~y2))|((~s3)&y2);
assign o3 = (s3&(~c))|(y3&c);
endmodule
module add1_block(
input s1,s2,s3,
output o1,o2,o3
);
wire y;
assign o1 = ~s1;
assign o2 = (s2&(~s1)) | ((~s2)&s1);
assign y = s1&s2;
assign o3 = (s3&(~y)) | ((~s3)&y);
endmodule
module mux21(
input [2:0] a,b,
input s,
output [2:0] y
);
assign y = (s==1'b0) ? a : b;
endmodule
module twobit_one_adder(
input s1,s2,
input c,
output o1,o2
);
wire y1;
assign o1 = (s1&(~c))|((~s1)&c);
assign y1 = (s2&(~s1))|((~s2)&s1);
assign o2 = (s2&(~c))|(y1&c);
endmodule