الذاكرة والمنطق القابل للبرمجة
دراسة أنواع الذاكرة (RAM و ROM)، هيكلية الذاكرة، اكتشاف الأخطاء وتصحيحها باستخدام كود هامينغ، والمنطق القابل للبرمجة مثل PLA و FPGAs.
Memory and Programmable Logic
Study of memory types (RAM and ROM), memory architecture, error detection and correction using Hamming code, and programmable logic such as PLA and FPGAs.
أهداف التعلم
- التعرف على أنواع وخصائص الذاكرة (RAM، ROM).
- فهم عملية القراءة والكتابة في الذاكرة.
- تعلم كيفية اكتشاف الأخطاء وتصحيحها أثناء القراءة من الذاكرة باستخدام كود هامينغ.
- فهم أساسيات عمل مصفوفات المنطق القابل للبرمجة (PLA، FPGA).
- Learn the types and characteristics of memory (RAM, ROM).
- Learn the memory read/write process.
- Learn how errors are detected and corrected while reading from memory.
- Learn the basics of how programmable logic arrays (PLA, FPGA) work.
1 هرمية الذاكرة وأنواعها
1 Memory Hierarchy and Types
الذاكرة تشبه مساحات التخزين في مكتبك؛ بعضها سريع وصغير (كدرج المكتب) وبعضها بطيء وكبير (كخزانة الملفات)، وتنقسم إلى متطايرة وغير متطايرة.
Memory is like storage spaces in an office; some are fast and small (desk drawer) and some are slow and large (filing cabinet), divided into volatile and non-volatile.
تصنف الذاكرة إلى:
- متطايرة (Volatile) تفقد بياناتها عند انقطاع التيار مثل RAM،
- وغير متطايرة (Non-volatile) تحتفظ بالبيانات مثل ROM و Flash.
تنقسم RAM إلى:
- SRAM (سريعة ومكلفة، تستخدم للـ Cache)
- و DRAM (أبطأ وأرخص، تستخدم للذاكرة الرئيسية).
هرمية الذاكرة (Memory Hierarchy) تحل معضلة التصميم بين السعة الكبيرة، السرعة العالية، والتكلفة المنخفضة، حيث تتدرج من المسجلات (Registers) السريعة جداً إلى الأقراص المغناطيسية البطيئة.
Memory is classified into:
- Volatile (loses data when power is off, e.g., RAM)
- and Non-volatile (retains data, e.g., ROM, Flash).
RAM is divided into:
- SRAM (fast, expensive, used for Cache)
- and DRAM (slower, cheaper, used for Main Memory).
The Memory Hierarchy solves the design dilemma of large capacity, high speed, and low cost, ranging from very fast Registers down to slow Magnetic Disks.
يتم تقييم الذاكرة بناءً على مقاييس التصميم الأساسية:
- كثافة الذاكرة (عدد البتات لكل ميكرومتر مربع)،
- وقت الوصول (Access Time)،
- واستهلاك الطاقة.
استخدام SRAM للذاكرة الرئيسية غير عملي بسبب التكلفة والحجم، لذا يُستخدم DRAM للذاكرة الرئيسية بينما يُحتفظ بـ SRAM للذاكرة المخبئية (Cache) القريبة من المعالج.
Memory is evaluated based on key design metrics:
- Memory Density (bits/μm²),
- Access Time,
- and Power Dissipation.
Using SRAM for main memory is impractical due to cost and size, so DRAM is used for main memory while SRAM is reserved for Cache memory close to the CPU.
| RAM | ROM | |
|---|---|---|
| الاحتفاظ بالبيانات (Volatility) Data Retention (Volatility) | متطايرة (تفقد البيانات) Volatile (loses data) | غير متطايرة (تحتفظ بالبيانات) Non-Volatile (retains data) |
| العمليات Operations | قراءة وكتابة Read/Write | قراءة فقط Read Only |
| الاستخدام الشائع Common Application | المتغيرات، الذاكرة الديناميكية، الكومة (Heaps) Variables, Dynamic memory, Heaps | برامج الإقلاع (Booting)، الثوابت Booting programs, Constants |
لماذا لا نصنع كل ذاكرة الحاسوب من نوع SRAM لتكون بأقصى سرعة ممكنة؟ Why don't we build all computer memory out of SRAM to achieve maximum speed?
لأن SRAM مكلفة جداً وتستهلك مساحة أكبر لكل بت مقارنة بـ DRAM، مما يجعل بناء ذاكرة رئيسية بسعات كبيرة (مثل جيجابايت) أمراً غير عملي اقتصادياً وهندسياً.
Because SRAM is very expensive and takes up more physical space per bit compared to DRAM, making it economically and physically impractical to build large capacity main memory (e.g., Gigabytes) entirely from SRAM.
2 إشارات الذاكرة وعملياتها
2 Memory Signals and Operations
للتحدث مع الذاكرة، تحتاج إلى عنوان (أين؟)، بيانات (ماذا؟)، وإشارات تحكم (اقرأ أم اكتب؟).
To talk to memory, you need an address (where?), data (what?), and control signals (read or write?).
تتكون إشارات الذاكرة من ثلاث مجموعات:
- ناقل العناوين (Address bus) لتحديد الموقع،
- ناقل البيانات (Data bus) لنقل المعلومات،
- وإشارات التحكم (Control Signals) لتحديد العملية.
أهم إشارات التحكم هي:
- CS (Chip Select) لتفعيل الشريحة،
- OE (Output Enable) للسماح بظهور البيانات على الناقل،
- WR (Write) للكتابة،
- و R (Read) للقراءة.
دورة القراءة (Read cycle) عادة ما تكون أقصر من دورة الكتابة (Write cycle).
Memory signals fall into three groups:
- Address bus (selects location),
- Data bus (transfers info),
- and Control Signals (specifies operation).
Key control signals are:
- CS (Chip Select) to activate the chip,
- OE (Output Enable) to allow data on the bus,
- WR (Write),
- and R (Read).
The Read cycle is usually shorter than the Write cycle.
إشارة CS (Chip Select) ضرورية جداً لأنها تسمح بتوصيل عدة شرائح ذاكرة على نفس ناقل البيانات باستخدام بوابات ثلاثية الحالة (Tri-state buffers).
في دورة الكتابة، يجب تثبيت العنوان والبيانات قبل تفعيل إشارة الكتابة لضمان استقرار الإشارات (Setup time). دورة الكتابة هي أقصى وقت من تطبيق العنوان حتى اكتمال التخزين الداخلي.
The CS (Chip Select) signal is crucial because it allows multiple memory chips to share the same data bus using tri-state buffers.
In a write cycle, the address and data must be stable before the write signal is activated (Setup time). The write cycle time is the maximum time from address application to the completion of internal storage operations.
لماذا تُستخدم بوابات ثلاثية الحالة (Tri-state) في مخرجات الذاكرة؟ Why are Tri-state buffers used in memory outputs?
لمنع حدوث تعارض (Short circuit) عندما تتشارك عدة شرائح ذاكرة نفس ناقل البيانات؛ حيث تسمح لشريحة واحدة فقط بوضع بياناتها على الناقل بينما تكون البقية في حالة ممانعة عالية (High Impedance).
To prevent short circuits when multiple memory chips share the same data bus; it allows only one chip to drive the bus while the others are in a High Impedance (disconnected) state.
3 الاختيار المتزامن (Coincident Selection)
3 Coincident Selection
بدلاً من استخدام مفتاح واحد عملاق لفتح مليون باب، نستخدم شبكة من الصفوف والأعمدة لتقليل عدد المفاتيح المطلوبة بشكل هائل.
Instead of using one giant switch to open a million doors, we use a grid of rows and columns to drastically reduce the hardware needed.
في الذواكر الكبيرة، استخدام مفكك شفرة (Decoder) واحد ببعد واحد (1D) يصبح غير عملي. على سبيل المثال، ذاكرة بحجم 32K تحتاج إلى مفكك شفرة بـ 15 مدخل و 32,768 مخرج (يتطلب 32,800 بوابة AND).
الحل هو إعادة ترتيب الذاكرة في مصفوفة ثنائية الأبعاد (2D) واستخدام مفككي شفرة:
- واحد للصفوف (Row Decoder)
- وآخر للأعمدة (Column Decoder).
هذا يسمى الاختيار المتزامن (Coincident Selection).
In large memories, using a single 1D decoder becomes impractical. For example, a 32K memory needs a 15-to-32768 decoder (requiring 32,800 AND gates).
The solution is to rearrange the memory into a 2D matrix and use two decoders:
- a Row Decoder
- and a Column Decoder.
This is called Coincident Selection.
لجعل عدد الصفوف والأعمدة متساوياً تقريباً في ذاكرة 32Kx8 (إجمالي 262,144 بت)، نأخذ الجذر التربيعي لـ 256K، مما يعطي 512 صفاً. نستخدم مفكك شفرة 9-إلى-512 للصفوف.
للأعمدة، نقسم 512 على 8 (حجم الكلمة) لنحصل على 64، ونستخدم مفكك شفرة 6-إلى-64.
إجمالي البوابات يصبح 512 + 64 = 576 بوابة AND فقط، مما يقلل حجم العتاد بأكثر من 50 ضعفاً مقارنة بالتصميم أحادي البعد.
To make rows and columns roughly equal in a 32Kx8 memory (262,144 bits total), we take the square root of 256K, giving 512 rows. We use a 9-to-512 row decoder.
For columns, 512/8 (word size) = 64, so we use a 6-to-64 column decoder.
Total gates = 512 + 64 = 576 AND gates, reducing hardware by over 50x compared to a 1D layout.
إذا كان لدينا ذاكرة بحجم 1 ميجابت (1Mbit)، ما هو حجم مفككات الشفرة المطلوبة باستخدام الاختيار المتزامن (بافتراض مصفوفة مربعة 1024x1024)؟ If we have a 1 Mbit memory, what size decoders are needed using coincident selection (assuming a square 1024x1024 array)?
سنحتاج إلى مفكك شفرة 10-إلى-1024 للصفوف، ومفكك شفرة 10-إلى-1024 للأعمدة.
We would need a 10-to-1024 row decoder and a 10-to-1024 column decoder.
4 كود هامينغ لاكتشاف وتصحيح الأخطاء
4 Hamming Code for Error Detection and Correction
كود هامينغ يضيف بتات فحص (Parity) في مواقع محددة (مضاعفات الرقم 2) لتمكين الذاكرة من اكتشاف أي بت خاطئ وتصحيحه تلقائياً.
Hamming code adds parity bits at specific positions (powers of 2) to enable memory to detect a flipped bit and automatically correct it.
التفاعلات الفيزيائية قد تسبب أخطاء في الذاكرة. لتحسين الموثوقية، يُستخدم كود هامينغ (Hamming Code). يتم إضافة k بتات تماثل (Parity bits) إلى n بتات بيانات.
تُحجز المواقع التي تمثل مضاعفات الرقم 2 (1، 2، 4، 8...) لبتات التماثل، والباقي للبيانات. يتم حساب بتات التماثل باستخدام دالة XOR أثناء الكتابة، ويتم فحصها أثناء القراءة لإنشاء كود الفحص (Syndrome C).
- إذا كان C = 0000، فلا يوجد خطأ.
- إذا كان C ≠ 0، فإن قيمة C تدل على موقع البت الخاطئ.
Physical interactions can cause memory errors. To improve reliability, Hamming Code is used. k parity bits are added to an n-bit data word.
Positions that are powers of 2 (1, 2, 4, 8...) are reserved for parity bits, the rest for data. Parity bits are calculated using XOR during write, and checked during read to generate a syndrome C.
- If C = 0000, there is no error.
- If C ≠ 0, the value of C indicates the position of the erroneous bit.
لحساب P1، نستخدم XOR لجميع المواقع التي يحتوي تمثيلها الثنائي على 1 في البت الأقل أهمية (1, 3, 5, 7, 9, 11). لحساب P2، نستخدم المواقع التي تحتوي على 1 في البت الثاني (2, 3, 6, 7, 10, 11)، وهكذا.
كود هامينغ الأساسي يكتشف ويصحح خطأً واحداً فقط (SEC). لاكتشاف خطأين (DED)، يجب إضافة بت تماثل إضافي يغطي الكلمة بأكملها.
To calculate P1, XOR all positions with a 1 in the least significant bit of their binary representation (1, 3, 5, 7, 9, 11). For P2, use positions with a 1 in the second bit (2, 3, 6, 7, 10, 11), etc.
Standard Hamming code detects and corrects only a single error (SEC). To detect double errors (SEC-DED), an extra overall parity bit must be added.
إذا قرأنا الكلمة المشفرة بهامينغ وحسبنا C فكانت النتيجة C = 1010 (بالثنائي)، فأي بت يحتوي على الخطأ؟ If we read a Hamming-coded word and calculate C to be 1010 (binary), which bit is in error?
البت رقم 10 (لأن 1010 بالثنائي تساوي 10 بالعشري).
Bit position 10 (since 1010 in binary equals 10 in decimal).
5 الأجهزة المنطقية القابلة للبرمجة (PLDs)
5 Programmable Logic Devices (PLDs)
بدلاً من توصيل بوابات منطقية ثابتة، تتيح لك هذه الأجهزة برمجة التوصيلات بين مصفوفات AND و OR لإنشاء أي دائرة تريدها.
Instead of wiring fixed logic gates, these devices let you program the connections between AND and OR arrays to create any circuit you want.
يمكن تنفيذ أي دائرة توافقية باستخدام مجموع المضروبات (SOP). الأجهزة المنطقية القابلة للبرمجة (PLDs) توفر مصفوفات من بوابات AND و OR يمكن التحكم بتوصيلاتها. الأنواع الرئيسية هي:
- PROM (مصفوفة AND ثابتة كمفكك شفرة، ومصفوفة OR قابلة للبرمجة)،
- PAL (مصفوفة AND قابلة للبرمجة، ومصفوفة OR ثابتة)،
- و PLA (مصفوفة AND قابلة للبرمجة، ومصفوفة OR قابلة للبرمجة).
PLA هي الأكثر مرونة.
Any combinational circuit can be implemented with Sum of Products (SOP). Programmable Logic Devices (PLDs) provide arrays of AND and OR gates with controllable connections. The main types are:
- PROM (Fixed AND array/decoder, Programmable OR array),
- PAL (Programmable AND array, Fixed OR array),
- and PLA (Programmable AND array, Programmable OR array).
PLA is the most flexible.
في PLA، يمكن برمجة كل من مستويي AND و OR، مما يسمح بمشاركة حدود الضرب (Product terms) بين مخرجات متعددة، وهذا يقلل من عدد البوابات المطلوبة.
كما يمكن استخدام المجمعات (Multiplexers) لتنفيذ الدوال البوليانية كجداول بحث (Look-up tables)، وهو المبدأ الأساسي المستخدم في FPGAs الحديثة.
In a PLA, both AND and OR planes are programmable, allowing product terms to be shared across multiple outputs, minimizing required gates.
Additionally, Multiplexers can be used to realize Boolean functions as look-up tables, which is the foundational concept used in modern SRAM-based FPGAs.
| PROM | PAL | PLA | |
|---|---|---|---|
| مصفوفة AND AND Array | ثابتة (Fixed) Fixed | قابلة للبرمجة (Programmable) Programmable | قابلة للبرمجة (Programmable) Programmable |
| مصفوفة OR OR Array | قابلة للبرمجة (Programmable) Programmable | ثابتة (Fixed) Fixed | قابلة للبرمجة (Programmable) Programmable |
لماذا نعتبر ROM نوعاً من الأجهزة المنطقية القابلة للبرمجة (PROM)؟ Why is ROM considered a type of Programmable Logic Device (PROM)?
لأن ROM يتكون داخلياً من مفكك شفرة (يمثل مصفوفة AND ثابتة تولد كل الاحتمالات) وبوابات OR يمكن برمجتها (توصيلها) لتوليد أي دالة منطقية مطلوبة.
Because a ROM internally consists of a decoder (acting as a fixed AND array generating all minterms) and OR gates that can be programmed (wired) to produce any desired Boolean function.
6 مصفوفات البوابات المنطقية القابلة للبرمجة ميدانياً (FPGA)
6 Field-Programmable Gate Arrays (FPGA)
الـ FPGA هي شريحة سيليكون فارغة تحتوي على آلاف المكعبات المنطقية (CLBs) التي يمكنك إعادة توصيلها برمجياً لتصبح أي معالج أو دائرة رقمية تتخيلها.
An FPGA is a blank silicon canvas with thousands of logic blocks (CLBs) that you can wire via software to become any processor or digital circuit you imagine.
الـ FPGAs هي دوائر يمكن برمجتها من قبل المستخدم النهائي. تتكون معمارية FPGA النموذجية من مصفوفة من الكتل المنطقية القابلة للتهيئة (CLBs) محاطة بكتل إدخال وإخراج (I/O pads)، وترتبط ببعضها عبر قنوات توجيه قابلة للبرمجة (Routing Channels) وكتل تحويل (Switch Blocks).
تتكون الكتلة المنطقية الأساسية (BLE) من:
- جدول بحث (LUT) لتنفيذ المنطق التوافقي،
- ومسجل (Flip-flop) للمنطق التتابعي،
- ومجمع (Multiplexer).
FPGAs are circuits programmable by the end user. A typical FPGA architecture consists of an array of Configurable Logic Blocks (CLBs) surrounded by I/O pads, connected via programmable Routing Channels and Switch Blocks.
A Basic Logic Element (BLE) inside a CLB consists of:
- a Look-Up Table (LUT) for combinational logic,
- a Flip-flop for sequential logic,
- and a multiplexer.
تعتمد FPGAs الحديثة على SRAM، حيث لا تستخدم بوابات AND/OR فيزيائية لتنفيذ المنطق، بل تستخدم جداول البحث (LUTs).
الـ LUT هو في الأساس مجمع (Multiplexer) صغير يعمل كذاكرة ROM صغيرة؛ حيث تعمل مدخلات الدالة كخطوط اختيار (Select lines) للمجمع، وتُخزن قيم جدول الحقيقة (Truth table) في خلايا SRAM المتصلة بمدخلات البيانات للمجمع.
Modern SRAM-based FPGAs do not use physical AND/OR gates for logic; they use Look-Up Tables (LUTs).
A LUT is essentially a small multiplexer acting as a tiny ROM; the function inputs act as select lines, and the truth table values are stored in SRAM cells connected to the multiplexer's data inputs.
كيف يمكن لـ LUT بـ 3 مداخل (3-input LUT) تنفيذ أي دالة منطقية بـ 3 متغيرات؟ How can a 3-input LUT implement ANY 3-variable Boolean function?
لأن دالة بـ 3 متغيرات لها 8 احتمالات في جدول الحقيقة. الـ LUT بـ 3 مداخل يحتوي على 8 خلايا ذاكرة (SRAM) تخزن مخرجات هذه الاحتمالات، وتعمل المداخل الثلاثة كعناوين لاختيار المخرج الصحيح.
Because a 3-variable function has 8 possible truth table outcomes. A 3-input LUT contains 8 SRAM cells storing these outcomes, and the 3 inputs act as address lines to select the correct output.