المنطق التوافقي (الجزء الثاني): مفككات التشفير، المشفرات، والمجمعات
تغطي هذه الوحدة المكونات الأساسية في مسار بيانات وحدة المعالجة المركزية، بما في ذلك مفككات التشفير (Decoders)، المشفرات (Encoders)، والمجمعات (Multiplexors)، وكيفية استخدامها لتنفيذ الدوال المنطقية.
Combinational Logic (Cont.): Decoders, Encoders, and Multiplexors
This module covers fundamental components in the CPU datapath, including Decoders, Encoders, and Multiplexors, and how to use them to implement Boolean logic functions.
أهداف التعلم
- فهم أساسيات مفككات التشفير (Decoders)، استخداماتها، وكيفية تصميمها.
- فهم أساسيات المشفرات (Encoders)، استخداماتها، وكيفية تصميمها.
- فهم أساسيات المجمعات (Multiplexers)، استخداماتها، وكيفية تصميمها.
- تصميم وتنفيذ الدوال المنطقية (Boolean functions) باستخدام مفككات التشفير والمجمعات.
- Learn the basics of decoders, what they are used for, how they work, and how to create a decoder.
- Learn the basics of encoders, what they are used for, how they work, and how to create an encoder.
- Learn the basics of multiplexers, what they are used for, how they work, and how to create a multiplexer.
- Learn how to design a Boolean function using decoders and multiplexers.
1 مسار البيانات ووحدة التحكم في المعالج
1 CPU Datapath and Control Unit
يتكون المعالج من مسار بيانات للعمليات الحسابية ووحدة تحكم لإرسال إشارات التوجيه.
A CPU consists of a Datapath for arithmetic/logic operations and a Control Unit that sends control signals.
أي وحدة معالجة مركزية (CPU) تتكون من جزأين رئيسيين:
- مسار البيانات (Datapath Unit) الذي ينفذ العمليات الحسابية والمنطقية،
- ووحدة التحكم (Control Unit) التي ترسل إشارات التحكم إلى مسار البيانات لتنفيذ عمليات معينة.
عند توصيل عدة أجهزة بناقل بيانات (Bus)، يجب أن يكون جهاز واحد فقط نشطاً في كل مرة لتجنب تضارب البيانات. تستخدم إشارات التحكم (Enable/Disable) لتفعيل الجهاز المطلوب.
Any CPU consists of two main parts:
- the Datapath Unit which performs Arithmetic/Logic Operations,
- and the Control Unit which sends control signals to the datapath to perform certain operations.
When multiple devices are attached to a Bus, only one device has to be active at a time (writing info on the bus). Control signals (Enable/Disable) from the Control Unit are needed to enable each device.
إذا كان لدينا العديد من الأجهزة المتصلة بالناقل، فإن استخدام سلك تحكم منفصل لكل جهاز يصبح غير عملي (يحتاج لأسلاك كثيرة جداً).
هنا تبرز الحاجة إلى 'فك تشفير العناوين' (Address Decoding) لتقليل عدد أسلاك التحكم المطلوبة من وحدة التحكم.
If more devices are to be attached to the bus, we would need many more wires coming out of the control unit if we used a 1-to-1 mapping.
This necessitates Address Decoding, where a smaller number of wires can select one of many devices using a decoder.
ماذا يحدث إذا حاولت وحدة التحكم تفعيل جهازين للكتابة على ناقل البيانات في نفس الوقت؟ What happens if the Control Unit enables two devices to write to the bus at the same time?
سيحدث تضارب في البيانات (Data Collision) مما يؤدي إلى تلف الإشارات وقراءة قيم غير صحيحة.
A data collision will occur on the bus, leading to corrupted signals and incorrect data being read.
2 مفككات التشفير (Decoders)
2 Decoders
دائرة منطقية تحول n من المدخلات إلى 2^n من المخرجات، حيث يتم تفعيل مخرج واحد فقط بناءً على القيمة الثنائية للمدخل.
A logic circuit with n inputs and 2^n outputs that drives high the single output corresponding to the binary code of the input.
مفكك التشفير هو دائرة توافقية تحتوي على n مدخلات و 2^n مخرجات. يقوم بتفعيل (Drive High) المخرج الذي يتوافق مع الرمز الثنائي للمدخل.
على سبيل المثال، مفكك تشفير 2-إلى-4 يأخذ مدخلين ويفعل واحداً من 4 مخرجات. المخرجات تمثل فعلياً الحدود الصغرى (Minterms) للمدخلات.
من تطبيقاته:
- فك تشفير العناوين (Address Decoding) لاختيار أجهزة أو بنوك ذاكرة،
- وتحويل الأنظمة العددية (مثل من ثنائي إلى ثماني).
A Decoder is a combinational circuit with n inputs and 2^n outputs. It drives high the output corresponding to the binary code of the input.
For example, a 2-to-4 line decoder takes 2 inputs and activates one of 4 outputs. Notice that the outputs are exactly the minterms of the inputs.
Applications include:
- Address Decoding (selecting memory banks or devices)
- and binary to octal/hex converters.
تحتوي العديد من مفككات التشفير على طرف تمكين (Enable). إذا كان التمكين غير مفعل، تكون جميع المخرجات 0 بغض النظر عن المدخلات.
يُستخدم طرف التمكين لتوسيع مفككات التشفير؛ مثلاً يمكن بناء مفكك تشفير 3-إلى-8 باستخدام جهازي مفكك تشفير 2-إلى-4، حيث يعمل المدخل الثالث (A2) كإشارة تمكين (مباشرة لأحدهما ومعكوسة للآخر).
Many decoders include an Enable (EN) pin. When EN is low, all outputs are 0 regardless of inputs.
The Enable pin is crucial for expansion. For instance, a 3-to-8 decoder can be realized using two 2-to-4 line decoders with an enable. The most significant bit (A2) is used to enable the top decoder when 0, and the bottom decoder when 1.
كم عدد مفككات التشفير 3-إلى-8 المطلوبة لبناء مفكك تشفير 4-إلى-16؟ How many 3-to-8 decoders are needed to build a 4-to-16 decoder?
نحتاج إلى مفككي تشفير (2) من نوع 3-إلى-8، مع استخدام المدخل الرابع كإشارة تمكين (Enable) لاختيار أحدهما.
Two 3-to-8 decoders are needed, using the 4th input bit as the Enable signal to select between them.
3 تنفيذ الدوال المنطقية باستخدام مفككات التشفير
3 Implementing Logic with Decoders
بما أن مفكك التشفير يولد جميع الحدود الصغرى (Minterms)، يمكننا تنفيذ أي دالة منطقية بتوصيل المخرجات المطلوبة ببوابة OR.
Since a decoder generates all minterms, any Boolean function can be implemented by connecting the required minterm outputs to an OR gate.
يمكن لمفكك تشفير بحجم n:2^n تنفيذ أي دالة منطقية تتكون من n متغيرات. تُستخدم المتغيرات كمدخلات تحكم (Control inputs) لمفكك التشفير، ويتم ربط طرف التمكين (Enable) بـ 1 (مفعل).
يقوم مفكك التشفير بتوليد جميع الحدود الصغرى (Minterms) الممكنة. لتشكيل الدالة المطلوبة، نأخذ المخرجات التي تقابل الحدود الصغرى الموجودة في الدالة ونجمعها باستخدام بوابة OR.
An n:2^n decoder implements any function of n variables. The variables are used as control inputs, and the Enable input is tied to 1.
The decoder generates the appropriate minterm based on the control signals. To form the function, the appropriate minterms are logically summed using an OR GATE.
يمكن لمفكك تشفير واحد تنفيذ دوال متعددة في نفس الوقت (مثل دالتي المجموع S والحمل C في الجامع الكامل) ببساطة عن طريق إضافة بوابة OR منفصلة لكل دالة.
يجب الانتباه إلى ترتيب المتغيرات؛ المتغير الأكثر أهمية (MSB) يجب أن يوصل بالمدخل الأكثر أهمية في مفكك التشفير لضمان تطابق أرقام الحدود الصغرى.
A single decoder can implement multiple functions simultaneously (e.g., both 'S' and 'C' for a full adder) by using multiple OR gates.
It is critical to map the variables correctly: the Most Significant Bit (MSB) of the function must be connected to the MSB input of the decoder to ensure the minterm indices match the truth table.
إذا كانت الدالة تحتوي على عدد كبير جداً من الحدود الصغرى (مثلاً 14 حد من أصل 16)، هل هناك طريقة أفضل لتنفيذها باستخدام مفكك التشفير؟ If a function contains a very large number of minterms (e.g., 14 out of 16), is there a better way to implement it using a decoder?
نعم، يمكننا استخدام بوابة NOR لجمع الحدود الصغرى غير الموجودة (الحدين المتبقيين)، مما يقلل من حجم البوابة المطلوبة.
Yes, we can use a NOR gate to sum the missing minterms (the 2 remaining ones) instead of an OR gate for the 14 present ones, simplifying the hardware.
4 المشفرات (Encoders)
4 Encoders
عكس مفكك التشفير؛ يأخذ 2^n مدخلات (واحد منها فقط نشط) ويخرج الرمز الثنائي n المقابل له.
The opposite of a decoder; it takes 2^n inputs (only one active at a time) and outputs the corresponding n-bit binary code.
المشفر (Encoder) هو الدائرة العكسية لمفكك التشفير. يحتوي على 2^n مدخلات (أو أقل) و n مخرجات. من أهم شروطه أن مدخلاً واحداً فقط يمكن أن يكون نشطاً في نفس الوقت.
من الأمثلة الشائعة: محول من النظام الثماني إلى الثنائي (Octal to Binary Encoder)، والذي يحتوي على 8 مدخلات (D0 إلى D7) و 3 مخرجات (A0, A1, A2).
An Encoder is the opposite of a decoder. It has 2^n inputs (or less, e.g., BCD in) and n outputs. A critical constraint is that only one input is active at a time.
Examples include:
- Octal to binary conversion
- and Hexadecimal to binary conversion.
An Octal to Binary Encoder has 8 inputs (D0-D7) and 3 outputs (A0, A1, A2).
لتصميم مشفر، ننظر إلى جدول الحقيقة ونستنتج معادلة كل مخرج. مثلاً، المخرج z (أو A0) يكون 1 عندما يكون المدخل D1 أو D3 أو D5 أو D7 نشطاً. لذلك، z = D1 + D3 + D5 + D7. يتم تنفيذ ذلك باستخدام بوابات OR.
المشكلة في المشفر العادي هي أنه إذا كانت جميع المدخلات 0، فإن المخرج يكون 0، وهو نفس مخرج تفعيل D0. تُحل هذه المشكلة عادةً باستخدام 'مشفر الأولوية' (Priority Encoder) الذي يضيف مخرجاً إضافياً لبيان صحة المدخلات.
To design an encoder, we derive the Boolean expression for each output from the truth table. For example, output z (or A0) is 1 when D1, D3, D5, or D7 is 1. Thus, z = D1 + D3 + D5 + D7. This is implemented using OR gates.
A limitation of a standard encoder is that if all inputs are 0, the output is 000, which is indistinguishable from D0 being active. This is typically resolved using a Priority Encoder with a 'Valid' output bit.
| Decoder | Encoder | |
|---|---|---|
| المدخلات والمخرجات Inputs and Outputs | n مدخلات إلى 2^n مخرجات n inputs to 2^n outputs | 2^n مدخلات إلى n مخرجات 2^n inputs to n outputs |
| الوظيفة الأساسية Primary Function | توسيع البيانات (تفعيل خط واحد بناءً على الرمز) Expands data (activates one line based on code) | ضغط البيانات (توليد رمز بناءً على الخط النشط) Compresses data (generates code based on active line) |
ماذا يحدث في المشفر العادي إذا تم تفعيل مدخلين (مثل D3 و D6) في نفس الوقت؟ What happens in a standard encoder if two inputs (e.g., D3 and D6) are active at the same time?
سيقوم المشفر بإجراء عملية OR للمخرجات، مما ينتج عنه قيمة خاطئة (D3=011 و D6=110، النتيجة ستكون 111 أي 7)، ولهذا نستخدم مشفر الأولوية.
The encoder will OR the outputs together, producing an incorrect binary code (D3=011, D6=110 -> output 111, which is 7). This is why Priority Encoders are used.
5 المجمعات (Multiplexors / Mux)
5 Multiplexors (Mux)
مفتاح إلكتروني يختار واحداً من عدة مدخلات بيانات ويمرره إلى مخرج واحد بناءً على خطوط الاختيار.
An electronic switch that selects one of several data inputs and passes it to a single output based on select lines.
المجمع (Multiplexer أو Mux) هو دائرة تختار واحداً من مجموعة مدخلات لتمريره إلى المخرج. لكل 2^n مدخل بيانات، نحتاج إلى n خطوط اختيار (Select lines).
يُستخدم بكثرة في مسار بيانات المعالج (CPU Datapath) لاختيار البيانات، مثل اختيار أي سجل (Register) سيتم إرسال بياناته إلى وحدة الحساب والمنطق (ALU).
يتكون داخلياً من مستوى من بوابات AND (للتفعيل بناءً على خطوط الاختيار) تليها بوابة OR لتجميع الناتج.
A Multiplexer (or Mux) selects one of a set of inputs to pass on to the output. For every 2^n inputs, we need n select lines.
Applications include choosing from sets of data, such as selecting which memory or register data goes to the ALU.
Structurally, it consists of a plane of AND gates (acting as enabling circuits based on select lines) followed by an OR gate.
يمكن تجميع المجمعات للتعامل مع كلمات بيانات متعددة البتات. على سبيل المثال، مجمع Quad 2-to-4 Line Mux يختار بين مجموعتين من 4 خطوط (A0-A3 أو B0-B3) بناءً على خط اختيار واحد (S).
إذا كان S=0، تمر المجموعة A إلى المخرج، وإذا كان S=1 تمر المجموعة B. هذا أساسي في توجيه البيانات (Buses) داخل المعالج.
Multiplexers can be banked to handle multi-bit data words. For example, a Quad 2-to-1 Line Mux selects one set of 4 lines.
When S=0, the 4-bits (A3-A0) are sent to the output (Y3-Y0). When S=1, the 4-bits (B3-B0) are sent. This is fundamental for routing multi-bit buses in a CPU architecture.
إذا كان لديك 32 سجل (Registers) في المعالج وتريد اختيار واحد منها، ما هو حجم المجمع المطلوب وكم عدد خطوط الاختيار؟ If you have 32 registers in a CPU and want to select one, what size MUX is needed and how many select lines?
نحتاج إلى مجمع 32-إلى-1، وسيتطلب 5 خطوط اختيار (لأن 2^5 = 32).
A 32-to-1 MUX is needed, which requires 5 select lines (since 2^5 = 32).
6 تنفيذ الدوال المنطقية باستخدام المجمعات
6 Implementing Logic with Multiplexers
يمكن استخدام المجمع كـ جدول بحث لتنفيذ أي دالة منطقية بتوصيل المتغيرات بخطوط الاختيار وقيم الدالة بمدخلات البيانات.
A MUX can act as a Lookup Table to implement any Boolean function by connecting variables to select lines and function values to data inputs.
يمكن لمجمع بحجم 2^n:1 تنفيذ أي دالة من n متغيرات. يتم استخدام المتغيرات كخطوط اختيار (Control inputs)، بينما يتم توصيل مدخلات البيانات (D0, D1...) بالقيم 0 أو 1 بناءً على جدول الحقيقة للدالة.
في جوهره، يعمل المجمع كـ جدول بحث (Lookup Table).
A 2^n:1 multiplexer implements any function of n variables. The variables are used as control (select) inputs, and the data inputs (D0-D...) are tied to 0 or 1 based on the function's truth table.
In essence, it acts as a lookup table.
التنفيذ المحسن (Optimized): يمكن لمجمع بحجم 2^(n-1):1 تنفيذ دالة من n متغيرات. نستخدم n-1 متغير كخطوط اختيار، ونربط مدخلات البيانات بالمتغير الأخير (أو معكوسه، أو 0، أو 1).
مثلاً، لدالة من 3 متغيرات (A,B,C)، نستخدم مجمع 4:1. نربط A و B بخطوط الاختيار، ونقارن المخرج F مع المتغير C لكل حالة لتحديد ما إذا كان المدخل سيكون C، C'، 0، أو 1.
Optimized Implementation: A 2^(n-1):1 mux can implement any function of n variables. We use n-1 variables as select lines, and tie the data inputs to the last variable, its complement, 0, or 1.
For a 3-variable function (A,B,C), we use a 4:1 MUX. A and B are select lines. We find the relationship between C and F for each combination of A and B to determine the data inputs.
| Decoder | Multiplexer (MUX) | |
|---|---|---|
| المكونات الإضافية المطلوبة Additional Components Needed | يتطلب بوابة OR خارجية لجمع الحدود الصغرى Requires external OR gate to sum minterms | لا يتطلب بوابات إضافية (يعمل كجدول بحث جاهز) No extra gates needed (acts as a ready LUT) |
| توصيل المتغيرات Variable Connection | المتغيرات توصل بالمدخلات Variables connected to inputs | المتغيرات توصل بخطوط الاختيار (Select lines) Variables connected to select lines |
لتنفيذ دالة من 4 متغيرات باستخدام الطريقة المحسنة، ما هو حجم المجمع الذي ستحتاجه؟ To implement a 4-variable function using the optimized method, what size MUX do you need?
سنحتاج إلى مجمع 8:1 (حيث 2^(4-1) = 8)، ونستخدم 3 متغيرات لخطوط الاختيار والمتغير الرابع لمدخلات البيانات.
An 8:1 MUX is needed (since 2^(4-1) = 8). 3 variables are used for select lines, and the 4th for data inputs.