المنطق التوافقي (تتمة): الجامع، الطارح، والمقارن
تغطي هذه الوحدة تصميم وتنفيذ الدوائر التوافقية المتقدمة مثل الجامع الكامل، الجامع الثنائي، تأخير الانتشار، الجامع العشري (BCD)، الضارب الثنائي، ومقارن المقدار.
Combinational Logic (Cont.): Adders, Subtractors, and Comparators
This module covers the design and implementation of advanced combinational circuits such as Full Adders, Binary Adders, Propagation Delay, Decimal (BCD) Adders, Binary Multipliers, and Magnitude Comparators.
أهداف التعلم
- تصميم دائرة الجامع-الطارح الثنائي (الجامع الكامل).
- تنفيذ دائرة الجامع-الطارح الثنائي عملياً.
- فهم تأخير الانتشار وكيفية التخفيف من آثاره.
- فهم مبدأ عمل الجامع العشري (BCD Adder).
- وصف وظائف الضارب الثنائي ومقارن المقدار.
- Design Binary Adder-Subtractor (Full-adder).
- Implement Binary Adder-Subtractor.
- Understand the propagation delay and how to mitigate it.
- Understand Decimal Adder.
- Describe the functionality of Binary Multiplier and Magnitude Comparator.
1 الجامع الكامل (Full Adder)
1 Full Adder
دائرة توافقية تجمع ثلاثة بتات (بتين أساسيين وبت الحمل من المرحلة السابقة) وتنتج المجموع والحمل.
A combinational circuit that forms the arithmetic sum of three bits (two significant bits and a carry-in).
يتكون الجامع الكامل من ثلاثة مدخلات (x, y, z) ومخرجين (S للمجموع، و C للحمل).
- المدخل z يمثل الحمل القادم من المرحلة السابقة.
- المخرج S يمثل البت الأقل أهمية للمجموع، بينما C يمثل الحمل الناتج.
يمكن التعبير عن المخرجات بالمعادلات المنطقية: S = x ⊕ y ⊕ z و C = xy + xz + yz. يمكن بناء الجامع الكامل باستخدام جامعين نصفيين (Half Adders) وبوابة OR واحدة.
It consists of three inputs (x, y, z) and two outputs (S for sum, C for carry).
- The input z represents the carry from the previous lower significant position.
- The outputs are necessary because the sum of three bits ranges from 0 to 3, requiring two bits to represent.
The simplified Boolean expressions are S = x ⊕ y ⊕ z and C = xy + xz + yz. It can be implemented using two half adders and one OR gate.
تصميم الجامع الكامل باستخدام بوابات XOR يقلل من عدد البوابات المطلوبة مقارنة بتصميم (Sum of Products) المباشر.
استخدام جامعين نصفيين يوضح مبدأ إعادة الاستخدام الهرمي في التصميم الرقمي، حيث يقوم الجامع النصفي الأول بجمع x و y، ويقوم الثاني بجمع النتيجة مع z، وتُجمع الحمولات الناتجة باستخدام بوابة OR.
Implementing the Full Adder using XOR gates reduces the gate count compared to a direct Sum of Products implementation.
Using two half adders demonstrates hierarchical design reuse: the first HA adds x and y, the second HA adds the result to z, and their respective carries are ORed together to form the final carry out.
لماذا نحتاج إلى مخرجين (S و C) في الجامع الكامل؟ Why do we need two outputs (S and C) in a Full Adder?
لأن مجموع ثلاثة بتات (1+1+1) يمكن أن يصل إلى 3 (11 بالثنائي)، مما يتطلب بتين لتمثيل النتيجة.
Because the arithmetic sum of three binary digits ranges from 0 to 3, and the binary representation of 2 or 3 requires two bits.
2 الجامع الثنائي (Binary Adder)
2 Binary Adder
دائرة رقمية تنتج المجموع الحسابي لرقمين ثنائيين عن طريق توصيل عدة جوامع كاملة بشكل متسلسل.
A digital circuit that produces the arithmetic sum of two binary numbers by cascading full adders.
يتم بناء الجامع الثنائي (مثل جامع 4-بت) عن طريق توصيل جوامع كاملة (Full Adders) في سلسلة (Cascade).
يتم توصيل مخرج الحمل (Carry Out) من كل جامع كامل بمدخل الحمل (Carry In) للجامع الكامل الذي يليه. يبدأ الجمع من البت الأقل أهمية (Subscript 0) حيث يكون الحمل الابتدائي C0 عادة صفر.
تُعرف هذه البنية باسم Ripple Carry Adder لأن الحمل ينتقل (يتموج) عبر البوابات.
A binary adder is constructed with full adders connected in cascade, with the output carry from each full adder connected to the input carry of the next full adder in the chain.
For a 4-bit adder, the input carry C0 is typically 0. The bits are added starting from the least significant position.
This architecture is known as a Ripple Carry Adder because the carry signal ripples through the stages.
المشكلة الرئيسية في هذا التصميم هي أن كل جامع كامل يجب أن ينتظر الحمل من الجامع الذي يسبقه قبل أن يتمكن من حساب نتيجته النهائية.
هذا يخلق اعتمادية متسلسلة تؤدي إلى تأخير زمني يتناسب طردياً مع عدد البتات (n).
The main drawback of the Ripple Carry Adder is that each full adder must wait for the carry-out from the previous stage before it can compute its final sum and carry-out.
This creates a sequential dependency, leading to a propagation delay that scales linearly with the number of bits (n).
ما هي قيمة C0 عند استخدام الجامع الثنائي لجمع رقمين فقط؟ What is the value of C0 when using the binary adder to simply add two numbers?
يجب أن تكون قيمة C0 صفراً في البت الأقل أهمية.
The input carry C0 in the least significant position must be 0.
3 تأخير الانتشار ومنطق استباق الحمل
3 Propagation Delay & Carry Lookahead
الوقت الذي تستغرقه الإشارة للمرور عبر البوابات المنطقية؛ ويُحل باستخدام منطق استباق الحمل (Carry Lookahead) لتسريع العملية.
The time it takes for a signal to pass through logic gates; mitigated by Carry Lookahead logic to speed up addition.
تأخير الانتشار الكلي يساوي تأخير البوابة الواحدة مضروباً في عدد مستويات البوابات. في الجامع المتسلسل، أطول تأخير هو الوقت الذي يستغرقه الحمل للانتشار عبر جميع الجوامع. لتقليل هذا التأخير، نستخدم تقنية (Carry Lookahead Logic). نعرّف متغيرين:
- توليد الحمل (Carry Generate) Gi = AiBi،
- وتمرير الحمل (Carry Propagate) Pi = Ai ⊕ Bi.
باستخدام هذه المتغيرات، يمكن حساب جميع الحمولات (C1, C2, C3...) بشكل متوازٍ دون انتظار المرحلة السابقة.
Total propagation time equals the delay of a typical gate times the number of gate levels. The longest delay in an adder is the carry propagation time. To reduce this, Carry Lookahead logic is used. We define two variables:
- Carry Generate (Gi = AiBi)
- and Carry Propagate (Pi = Ai ⊕ Bi).
Using these, carries can be computed in parallel: C1 = G0 + P0C0, C2 = G1 + P1C1, etc., significantly reducing the delay.
منطق استباق الحمل يحول التأخير الخطي O(n) إلى تأخير ثابت O(1) من حيث مستويات البوابات (بافتراض بوابات ذات عدد مداخل غير محدود).
ومع ذلك، في الواقع، يقتصر هذا على 4 بتات عادةً بسبب قيود الـ Fan-in للبوابات، وتُجمع هذه الوحدات معاً لبناء جوامع أكبر.
Carry Lookahead logic flattens the linear O(n) delay into a constant O(1) gate-level delay (assuming infinite fan-in gates).
In practice, due to fan-in limitations, lookahead generators are typically built in 4-bit blocks, which are then cascaded or combined using higher-level lookahead blocks.
| Ripple Carry Adder | Carry Lookahead Adder | |
|---|---|---|
| السرعة (تأخير الانتشار) Speed (Propagation Delay) | بطيء (يتناسب مع عدد البتات O(n)) Slow (Proportional to n bits, O(n)) | سريع (تأخير ثابت تقريباً O(1)) Fast (Almost constant delay, O(1)) |
| تعقيد الدائرة (المساحة) Hardware Complexity (Area) | بسيط (جوامع كاملة متسلسلة) Simple (Cascaded Full Adders) | معقد (بوابات إضافية لتوليد الحمل) Complex (Extra logic gates for carry generation) |
لماذا لا نستخدم منطق استباق الحمل لجامع بحجم 64-بت ككتلة واحدة؟ Why don't we use a single Carry Lookahead block for a 64-bit adder?
لأن ذلك سيتطلب بوابات AND و OR ذات عدد مداخل (Fan-in) ضخم جداً، وهو أمر غير عملي فيزيائياً.
Because it would require AND and OR gates with an impractically large number of inputs (fan-in limit).
4 الطارح الثنائي والجامع-الطارح
4 Binary Subtractor & Adder-Subtractor
دائرة تدمج الجمع والطرح باستخدام المتمم الثاني (2's complement) وبوابات XOR للتحكم في العملية.
A circuit that combines addition and subtraction using 2's complement and XOR gates for mode control.
يتم إجراء الطرح (A - B) عن طريق أخذ المتمم الثاني للرقم B وجمعه مع A. المتمم الثاني هو المتمم الأول (عكس البتات) زائد 1.
يمكن دمج عمليتي الجمع والطرح في دائرة واحدة باستخدام بوابات XOR مع كل مدخل B، وإشارة تحكم (M).
- إذا كانت M=0، تعمل الدائرة كجامع.
- إذا كانت M=1، تقوم بوابات XOR بعكس بتات B، ويدخل 1 كحمل ابتدائي (C0=1)، مما يحقق المتمم الثاني ويقوم بعملية الطرح.
Subtraction A - B is done by taking the 2's complement of B and adding it to A. The 2's complement is the 1's complement plus 1.
Addition and subtraction can be combined into one circuit using an XOR gate with each B input and a mode control M.
- When M=0, it adds.
- When M=1, the XOR gates invert B (1's complement), and M is also fed to the input carry (C0=1) to add 1, completing the 2's complement subtraction.
بوابة XOR هنا تعمل كـ 'عاكس قابل للبرمجة'.
هذه الحيلة البسيطة توفر مساحة كبيرة في المعالجات (ALU) حيث لا نحتاج إلى دائرة طرح منفصلة، بل نعيد استخدام دائرة الجمع بالكامل.
The XOR gate acts as a 'programmable inverter'.
This elegant trick saves significant hardware area in ALUs because it eliminates the need for a dedicated subtractor circuit, fully reusing the adder logic.
ما هو دور إشارة التحكم M عندما تكون قيمتها 1؟ What is the role of the control signal M when its value is 1?
تقوم بعكس بتات B عبر بوابات XOR وتضيف 1 كحمل ابتدائي (C0)، مما يحول العملية إلى طرح.
It inverts the B bits via XOR gates and adds 1 as the initial carry (C0), turning the operation into subtraction.
5 الجامع العشري (BCD Adder)
5 Decimal (BCD) Adder
دائرة تجمع رقمين بنظام BCD، وتضيف 0110 (6) لتصحيح النتيجة إذا تجاوزت 9.
A circuit that adds two BCD digits and adds 0110 (6) to correct the result if it exceeds 9.
عند جمع رقمين عشريين بصيغة BCD (كل رقم من 0 إلى 9)، أقصى مجموع ممكن هو 9 + 9 + 1 (حمل) = 19.
إذا استخدمنا جامع ثنائي عادي 4-بت، فإنه سيعطي نتائج صحيحة من 0 إلى 9. لكن إذا كان المجموع أكبر من 9 (من 10 إلى 19)، فإن النتيجة الثنائية تكون غير صالحة كـ BCD.
لتصحيح ذلك، يجب إضافة الرقم الثنائي 0110 (6) إلى المجموع. شرط التصحيح هو: C = K + Z8Z4 + Z8Z2، حيث K هو الحمل النهائي، و Z هي بتات المجموع.
When adding two BCD digits (0-9), the maximum sum is 9 + 9 + 1 (carry) = 19.
A standard 4-bit binary adder gives correct BCD results for sums 0-9. However, for sums greater than 9 (10-19), the binary result is invalid in BCD.
To correct this, we must add binary 0110 (6) to the sum. The condition for correction and generating an output carry is C = K + Z8Z4 + Z8Z2, where K is the carry out and Z are the sum bits.
لماذا نضيف 6؟ لأن نظام BCD يستخدم 10 حالات فقط (0000 إلى 1001) من أصل 16 حالة ممكنة في 4 بتات. هناك 6 حالات غير مستخدمة.
عندما يتجاوز المجموع 9، نضيف 6 لتخطي هذه الحالات غير المستخدمة وإجبار الدائرة على توليد حمل صحيح للرقم العشري التالي.
Why add 6? Because BCD uses only 10 states (0000 to 1001) out of the 16 possible states in 4 bits. There are 6 unused states.
When the sum exceeds 9, adding 6 skips these unused states and forces the circuit to generate the correct carry for the next decimal digit.
ماذا يحدث إذا جمعنا 5 و 4 في جامع BCD؟ What happens if we add 5 and 4 in a BCD adder?
المجموع هو 9 (1001). شرط التصحيح لن يتحقق (C=0)، لذا لن يتم إضافة 0110، وستبقى النتيجة 1001.
The sum is 9 (1001). The correction condition is not met (C=0), so 0110 is not added, and the result remains 1001.
6 الضارب الثنائي (Binary Multiplier)
6 Binary Multiplier
دائرة تضرب الأرقام الثنائية باستخدام بوابات AND لتوليد النواتج الجزئية وجوامع لجمعها مع إزاحتها.
A circuit that multiplies binary numbers using AND gates for partial products and adders to sum them with shifts.
يتم ضرب الأرقام الثنائية بنفس طريقة ضرب الأرقام العشرية. يُضرب المضروب (Multiplicand) في كل بت من المضروب فيه (Multiplier) بدءاً من البت الأقل أهمية. كل عملية ضرب تشكل 'ناتجاً جزئياً' (Partial Product).
في النظام الثنائي، الضرب هو ببساطة عملية AND منطقية (إذا كان البت 1 ننسخ الرقم، وإذا كان 0 نضع أصفاراً).
يتم إزاحة النواتج الجزئية المتتالية موضعاً واحداً إلى اليسار، ثم تُجمع باستخدام جوامع نصفية وكاملة للحصول على الناتج النهائي.
Multiplication of binary numbers is performed similarly to decimal numbers. The multiplicand is multiplied by each bit of the multiplier, forming a partial product.
In binary, this multiplication is simply a logical AND operation.
Successive partial products are shifted one position to the left. The final product is obtained by summing these shifted partial products using half adders and full adders.
حجم الناتج النهائي لضرب رقمين بحجم n-bit و m-bit هو (n+m) بت.
في التنفيذ العتادي، بدلاً من تخزين جميع النواتج الجزئية ثم جمعها، يتم جمعها تراكمياً باستخدام مصفوفة من الجوامع (Multiplier Array) لتقليل المساحة وتسريع العملية.
The final product of an n-bit and m-bit number requires (n+m) bits.
In hardware implementation, instead of storing all partial products and adding them at the end, they are added cumulatively using an array of adders to optimize area and speed.
ما هي البوابة المنطقية التي تمثل عملية الضرب لبت واحد في النظام الثنائي؟ Which logic gate represents single-bit multiplication in binary?
بوابة AND.
The AND gate.
7 مقارن المقدار (Magnitude Comparator)
7 Magnitude Comparator
دائرة توافقية تقارن بين رقمين (A و B) وتحدد ما إذا كان A > B، أو A = B، أو A < B.
A combinational circuit that compares two numbers (A and B) and determines if A > B, A = B, or A < B.
مقارن المقدار يحدد العلاقات النسبية بين رقمين. للتحقق من المساواة (A = B)، يجب أن تكون كل أزواج البتات متساوية.
يتم التعبير عن تساوي بتين منطقياً باستخدام بوابة XNOR: xi = AiBi + Ai'Bi'. لكي يكون الرقمان متساويين، يجب أن تكون جميع متغيرات xi تساوي 1، مما يتطلب بوابة AND لجميع المتغيرات: (A=B) = x3x2x1x0.
لتحديد الأكبر أو الأصغر، نفحص البتات بدءاً من الأكثر أهمية (MSB). إذا كان Ai=1 و Bi=0، فإن A > B.
A magnitude comparator determines the relative magnitudes of A and B. For equality (A = B), all pairs of significant digits must be equal.
Equality of a pair of bits is expressed using an XNOR function: xi = AiBi + Ai'Bi'. For full equality, all xi must be 1, dictated by an AND operation: (A=B) = x3x2x1x0.
To determine greater or less than, we inspect bits from the most significant position. If Ai=1 and Bi=0 (and all higher bits are equal), then A > B.
المعادلة المنطقية لـ A > B في مقارن 4-بت هي: A>B = A3B3' + x3A2B2' + x3x2A1B1' + x3x2x1A0B0'.
هذا يعكس الفحص التسلسلي: نفحص البت التالي فقط إذا كانت البتات السابقة متساوية (xi=1).
The Boolean logic for A > B in a 4-bit comparator is: A>B = A3B3' + x3A2B2' + x3x2A1B1' + x3x2x1A0B0'.
This perfectly mirrors the sequential inspection process: we only care about the current bit pair if all more significant bit pairs are equal (represented by the xi terms).
لماذا نبدأ المقارنة لمعرفة الأكبر/الأصغر من البت الأكثر أهمية (MSB) وليس الأقل؟ Why do we start comparing for greater/less than from the Most Significant Bit (MSB) rather than the LSB?
لأن البت الأكثر أهمية له الوزن الأكبر؛ اختلاف واحد فيه يحسم النتيجة بغض النظر عن قيم البتات الأقل أهمية.
Because the MSB carries the most weight; a difference there definitively determines which number is larger, regardless of the lower bits.