📖 Lecture 4 — CSE 532

Computer Architecture
& Verilog HDL

তোমার পুরো lecture টা এখানে — diagram, code, বাংলা explanation, আর exam practice সব একসাথে। Lecture দেখতে দেখতে এটা খোলা রাখো।

Lecture Slides Covered Bangla + English Exact Lecture Code Interactive Quiz
📌 Prerequisites

Lecture 1 (logic gates, transistor context) recommended.

📄 Lecture Slides 1–4 Traditional Design → Technology Advancement

Why do we need HDL? কেন HDL দরকার?

Before learning Verilog, we need to understand the PROBLEM it solves. Your lecture starts here.

The Old Way — Traditional Design Approaches

আগের দিনে engineers দুইভাবে circuit design করত। তোমার lecture এর slide 2 এ এই দুইটার কথা আছে:

🔌 Gate Level Design (LogiSim)

প্রতিটা AND, OR, NOT gate হাতে আঁকতে হতো। ছোট circuit এর জন্য ঠিকই আছে — কিন্তু বড় circuit এ হাজার হাজার gate!

উদাহরণ

একটা calculator বানাতে হলে হাজারো gate। হাতে আঁকলে কত সময় লাগবে? ভুল হলে খুঁজে বের করা আরো কঠিন।

📐 Schematic Design (PSpice)

Computer software দিয়ে circuit আঁকা হতো। কিছুটা ভালো — কিন্তু তবুও একটা একটা করে component বসাতে হয়। Complex design এ scale করে না।

সমস্যা

40 million transistor এর chip কি এভাবে আঁকা সম্ভব? একদমই না।

Technology Advancement — Lecture Slide 4

From Lecture 1 you know Moore's Law and transistor counts. তোমার lecture এ দুইটা processor compare করা হয়েছে। এটা দেখো — এটাই বোঝায় কেন HDL এত জরুরি:

Intel 4004 Processor (1971)

Transistors: 2,300
Clock Speed: 108 KHz

2,300 transistor এখনো হাতে design করা possible ছিল — কিন্তু অনেক কষ্টের।

Intel P4 Processor (2000)

Transistors: 40,000,000
Clock Speed: 1.5 GHz

40 million transistor! হাতে আঁকা কল্পনাতেও সম্ভব না।

🇧🇩 Key Insight — এটাই পুরো lecture এর মূল কারণ

মাত্র ৩০ বছরে transistor সংখ্যা ২,৩০০ থেকে ৪ কোটিতে চলে গেছে। এই জটিলতা সামলানোর জন্যই HDL এসেছে। Verilog দিয়ে তুমি code লিখবে, আর computer সেটাকে hardware circuit এ convert করবে — ঠিক যেমন C++ code কে computer executable এ convert করে!

1971
2,300
2000
40,000,000
Bottom Line: HDL (Hardware Description Language) allows engineers to describe hardware using code — just like C++ describes software behavior. The computer then synthesizes that code into actual circuits. এটা না থাকলে আধুনিক processor বানানো অসম্ভব।
📄 Lecture Slide 5 System Design Pyramid

System Design Pyramid

Hardware design হয় বিভিন্ন level এ। Pyramid দিয়ে সেই levels বোঝানো হয়।

The Pyramid — Abstraction Levels
SYSTEM
CHIP
REGISTER
GATE
CIRCUIT
SILICON

⬆ More Abstract  |  More Physical ⬇

প্রতিটা Level কী মানে?
SILICON

Physical chip এর actual material। Transistor বানানোর raw layer। তুমি এটা নিয়ে কাজ করবে না।

CIRCUIT

Transistor গুলো কীভাবে connect → বিদ্যুৎ প্রবাহ। Resistor, Capacitor ইত্যাদি।

GATE

AND, OR, NOT — Logic gates। এই level এ Verilog এর gate-level modeling কাজ করে।

REGISTER

Flip-flops, Registers — Data store করতে পারে। Verilog এর behavioral modeling এখানে।

CHIP

Multiple components মিলে একটা chip (যেমন CPU)।

SYSTEM

পুরো computer system — CPU, RAM, I/O সব মিলিয়ে।

🇧🇩 Pyramid কেন জানতে হবে?

Verilog দিয়ে তুমি যেকোনো level এ design করতে পারো। Lecture এর pyramid এ দেখো — Structure side এ Verilog আছে, মানে Verilog দিয়ে gate থেকে শুরু করে পুরো system পর্যন্ত describe করা যায়। তোমার course এ মূলত Gate এবং Register level নিয়ে কাজ করবে।

Verilog এর তিনটা Modeling Style — Pyramid এর সাথে মিল রাখো

Gate Level

AND, OR, NOT gate এর instance create করো। Pyramid এর GATE level।

and g1(Y, A, B); or g2(Z, Y, C);

Dataflow (assign)

Boolean equations দিয়ে। তোমার lecture এর main focus। সবচেয়ে বেশি ব্যবহার হয়।

assign Y = A & B; assign Z = Y | C;

Behavioral (always)

if/else, case দিয়ে behavior describe করো। REGISTER level এবং উপরে।

always @(*) if (A) Y = B; else Y = C;
📄 Lecture Slides 7–8 HDL: Verilog & VHDL

Two HDL Languages: Verilog vs VHDL

তোমার lecture এ দুইটা HDL compare করা হয়েছে। আমরা Verilog শিখছি।

Feature ⚡ Verilog 🏛️ VHDL
Full Name Verilog HDL VHSIC Hardware Description Language
Syntax feels like C / C++ — তোমার জানা! Ada / Pascal — কঠিন
Ease of learning ✅ সহজ ❌ কঠিন
Popular where North America, Asia (তোমার region!) Europe (academia তে বেশি)
Lower-level design ✅ খুব ভালো (lecture অনুযায়ী) তুলনামূলক দুর্বল
Simulation speed ✅ দ্রুত ধীর
Industry use ✅ ASIC design এ dominant Academic এবং European industry
তোমার course ✅ এটা শিখবে ❌ এই course এ না
🇧🇩 Lecture থেকে key points
  • Verilog এর syntax C এর মতো — তুমি C++ জানো, তাই তোমার জন্য অনেক সহজ হবে
  • Lecture এ বলা হয়েছে Verilog "results in fast simulations, relatively simple, easy in first contacts" — মানে শিখতে সহজ
  • VHDL "also difficult, complex character" — আমাদের দরকার নেই এখন
  • Verilog North America এবং Asia তে most popular — তোমার job market এর জন্যও কাজে লাগবে
📄 Lecture Slide 14 The Module Interface — Port List & Port Declaration

The Module Interface: Port List & Port Declaration

এটা Verilog এর সবচেয়ে গুরুত্বপূর্ণ concept। প্রতিটা hardware component কে "module" হিসেবে লেখা হয়।

C++ vs Verilog — পাশাপাশি বোঝো

C++ (তুমি জানো):

analogy.cpp comparison
// C++ তে একটা class: class Calculator { public: // Input: data ঢোকে int A, B; // Output: result বের হয় int Result; // Function: কী করবে int add() { return A + B; } };

Verilog (শিখছো):

calculator.v Verilog
// Verilog এ একটা module: module Calculator (A, B, Result); // Input ports input [3:0] A, B; // Output port output [3:0] Result; // Logic: কী করবে assign Result = A + B; endmodule
🇧🇩 ৩টা মিল মনে রাখো
  • class = module — একটা component কে define করে
  • C++ এর parameter = Verilog এর input/output ports
  • C++ এর return = Verilog এর output wire
Lecture এর ALU Example — Slide 14 থেকে হুবহু

তোমার lecture এ ALU (Arithmetic Logic Unit) এর module interface দেখানো হয়েছে। এটাই lecture এর exact code:

input → In1 [3:0] // first operand
input → In2 [3:0] // second operand (when 1, In1=In2)
input → OpSel [3:0] // operation select
input → Mode // mode arithm/logic
input → CIn // carry in
ALU
→ output Result [3:0] // operation result
→ output Equal // (when 1, In1 = In2)
→ output COut // carry out
ALU.v 📄 Lecture Slide 14 — Exact Code
module ALU (Result, Equal, Cout, In1, In2, OpSel, CIn, Mode); // ── Outputs ────────────────────────────── output [3:0] Result; // operation result output Cout; // carry out output Equal; // when 1, In1 = In2 // ── Inputs ─────────────────────────────── input [3:0] In1; // first operand input [3:0] In2; // second operand input [3:0] OpSel; // operation select input CIn; // carry in input Mode; // mode: arithm when 0 // ... (internal logic would go here) endmodule
🇧🇩 Port List vs Port Declaration — দুইটা আলাদা part!

Port List (প্রথম line এ): module এর নাম এবং সব ports এর নাম একসাথে লেখা → module ALU (Result, Equal, Cout, In1, In2, ...)

Port Declaration (ভেতরে): প্রতিটা port কি input নাকি output, আর কত bit সেটা declare করো → output [3:0] Result;

মনে রাখো: Port list এ নাম, declaration এ type। দুইটাই লাগবে, একটা বাদ দিলে error।

wire vs reg — দুইটা Data Type তোমার জানা দরকার

wire — Physical তার

Hardware এর actual wire এর মতো। কোনো value নিজে store করে না — শুধু signal বহন করে। assign statement এর সাথে ব্যবহার হয়।

wire Y; // 1-bit wire [3:0] Bus; // 4-bit wire [7:0] Data; // 8-bit assign Y = A & B; // ✅
মনে রাখো

তার = wire। তার কারেন্ট store করে না, বহন করে। input/output by default wire type।

reg — Register (Store করে)

Value store করে রাখতে পারে। always block এর ভেতরে যে signals assign হয় তারা reg type হতে হবে।

reg Y; // 1-bit reg [7:0] Count; // 8-bit always @(posedge clk) Count = Count + 1; // ✅
মনে রাখো

reg = register = ছোট memory। Clock এর সাথে update হয়। Sequential circuit এ দরকার।

📄 Background Knowledge Logic Gates — Full Adder বোঝার আগে

Logic Gates — Hardware এর LEGO Blocks

From Lecture 1 you know AND, OR, NOT, NAND, NOR. Here we describe them in Verilog.

Lecture এর full adder বোঝার আগে এই ৫টা gate ভালো করে বুঝতে হবে। প্রতিটা দেখো, truth table মুখস্থ করো।

AND Gate — A & B

কী করে?

দুইটা input নেয়। Output 1 হয় শুধুমাত্র যখন দুইটাই 1। একটাও 0 হলে output 0।

বাংলায় মনে রাখো

AND = "এবং" — "A এবং B দুটোই সত্য হলে Y সত্য।" যেমন: বৃষ্টি আসে এবং ছাতা নেই → ভিজবো।

A
B
AND
Y

Truth Table

A B Y = A & B
0 0 0
0 1 0
1 0 0
1 1 1 ← only here!
and_gate.v
module and_gate (A, B, Y); input A, B; output Y; assign Y = A & B; endmodule
OR Gate — A | B

কী করে?

Output 1 হয় যখন যেকোনো একটা বা উভয় input 1 হয়। শুধু দুইটাই 0 হলে output 0।

বাংলায় মনে রাখো

OR = "অথবা" — "A অথবা B যেকোনো একটা সত্য হলেই Y সত্য।"

A
B
OR
Y

Truth Table

A B Y = A | B
0 0 0 ← only here 0
0 1 1
1 0 1
1 1 1
or_gate.v
module or_gate (A, B, Y); input A, B; output Y; assign Y = A | B; endmodule
NOT Gate — ~A

একটাই input। Output সবসময় উল্টো।

মনে রাখো

NOT = "না" — A সত্য হলে Y মিথ্যা। A মিথ্যা হলে Y সত্য।

A Y = ~A
0 1
1 0
assign Y = ~A; // NOT
XOR Gate — A ^ B

Output 1 যখন inputs আলাদা। দুটো same হলে 0।

মনে রাখো

XOR = "ভিন্নতা detector" — inputs আলাদা → 1, একই → 0।
Full Adder এ XOR দিয়ে Sum বানানো হয়!

A B Y = A ^ B
0 0 0
0 1 1 ← different
1 0 1 ← different
1 1 0
assign Y = A ^ B; // XOR
Quick Reference — Exam এ দরকার হবে: & = AND  |  | = OR  |  ~ = NOT  |  ^ = XOR  |  ~^ বা ^~ = XNOR
📄 Lecture Slide 33 One-bit Full Adder — Exact Lecture Code

One-bit Full Adder — তোমার Lecture এর Main Example

এটা তোমার lecture এর সবচেয়ে important circuit। ভালো করে বোঝো — 4-bit adder বানাতে এটাই 4 বার ব্যবহার হবে।

Full Adder কী? — Step by Step বোঝো

Problem: দুইটা single-bit binary number যোগ করতে হবে, সাথে আগের ঘর থেকে আসা carry।

🇧🇩 সহজ উদাহরণ — হাতে যোগ

মনে করো তুমি হাতে 1 + 1 যোগ করছো binary তে:

    A = 1
  + B = 1
  ─────────
  Sum = 0  (কারণ 1+1=10 binary তে, unit digit = 0)
 Cout = 1  (carry = 1, পরের ঘরে যাবে)

Cin হলো আগের ঘর থেকে আসা carry। Full Adder তিনটা জিনিস যোগ করে: A + B + Cin।

Ports বুঝি:

INPUT
A
First bit to add
INPUT
B
Second bit to add
INPUT
Cin
Carry from previous bit
OUTPUT
Sum
Result bit (LSB)
OUTPUT
Cout
Carry to next bit
Full Adder Truth Table — সব 8 টা cases
A B Cin Sum Cout Decimal: A+B+Cin
0 0 0 0 0 0+0+0 = 0
0 0 1 1 0 0+0+1 = 1
0 1 0 1 0 0+1+0 = 1
0 1 1 0 1 0+1+1 = 2 = 10₂
1 0 0 1 0 1+0+0 = 1
1 0 1 0 1 1+0+1 = 2 = 10₂
1 1 0 0 1 1+1+0 = 2 = 10₂
1 1 1 1 1 1+1+1 = 3 = 11₂
🇧🇩 Table পড়ার উপায়

Highlighted rows গুলো দেখো — Sum=1 কারণ result এর unit digit 1। যখন যোগফল ≥ 2 (binary 10), তখন Sum=0 এবং Cout=1 (carry হয়)।

📄 Lecture Slide 33 — Exact Verilog Code

এই exact code তোমার lecture এ আছে। প্রতিটা line বোঝো:

full_adder.v 📄 Lecture Slide 33 — Exact
module full_adder (A, B, Cin, Sum, Cout); input A, B, Cin; // 3 single-bit inputs output Sum, Cout; // 2 single-bit outputs // ── Sum equation ───────────────────────────────────── // Sum=1 when an ODD number of inputs are 1 assign Sum = (A & B & Cin) // 1+1+1 | (~A & ~B & Cin) // 0+0+1 | (~A & B & ~Cin) // 0+1+0 | (A & ~B & ~Cin); // 1+0+0 // ── Cout equation ──────────────────────────────────── // Cout=1 when 2 or more inputs are 1 assign Cout = (A & B) // both A and B are 1 | (A & Cin) // A and carry are 1 | (B & Cin); // B and carry are 1 endmodule
🇧🇩 Sum Equation বোঝো — Line by Line

Sum equation দেখতে জটিল — কিন্তু logic সহজ। Truth table থেকে Sum=1 হওয়ার সব row গুলো OR করা হয়েছে:

  • (A & B & Cin) → row: A=1, B=1, Cin=1 → Sum=1 ✓
  • (~A & ~B & Cin) → row: A=0, B=0, Cin=1 → Sum=1 ✓
  • (~A & B & ~Cin) → row: A=0, B=1, Cin=0 → Sum=1 ✓
  • (A & ~B & ~Cin) → row: A=1, B=0, Cin=0 → Sum=1 ✓

এই pattern কে Sum of Products (SOP) বলে — "যে যে conditions এ output 1, সেগুলো AND করো, তারপর সব OR করো।"

Cout Equation কেন ছোট? কারণ Cout=1 হওয়ার condition সহজ — যখনই দুইটা বা তিনটা input 1 হয়। (A&B), (A&Cin), (B&Cin) — এই তিনটার যেকোনো একটা সত্য হলেই carry হয়।
🔬 Interactive — Full Adder Test করো

A, B, Cin এর value দাও, দেখো Sum আর Cout কী হয়:

Sum = 0 Cout = 0
📄 Lecture Slide 34 Four-bit Adder — Structural Modeling

Four-bit Adder — Module Reuse & Structural Modeling

এখানে একটা নতুন concept — আগে বানানো module কে বারবার ব্যবহার করা। ঠিক C++ এ class instance তৈরির মতো।

Concept: 4টা Full Adder Chain করা
🇧🇩 Analogy — কারখানার Assembly Line

কল্পনা করো একটা কারখানায় ৪টা worker আছে। প্রথম worker কাজ করে, তার output দ্বিতীয়কে দেয়, দ্বিতীয় তৃতীয়কে, এভাবে চলে। চার-bit adder এ carry ঠিক এভাবে FA1 থেকে FA2, FA2 থেকে FA3, FA3 থেকে FA4 তে যায়।

A[0]
B[0]
Cin
Full
Adder
FA1 bit 0
Sum[0]
C0
Full
Adder
FA2 bit 1
Sum[1]
C1
Full
Adder
FA3 bit 2
Sum[2]
C2
Full
Adder
FA4 bit 3
Sum[3]
Cout
← Carry ripples from bit 0 to bit 3 →
📄 Lecture Slide 34 — Exact Verilog Code
four_bit_adder.v 📄 Lecture Slide 34 — Exact
module four_bit_adder (A, B, Cin, Sum, Cout); input [3:0] A; // 4-bit first number input [3:0] B; // 4-bit second number input Cin; // initial carry in output [3:0] Sum; // 4-bit result output Cout; // final carry out // Internal carry wires between the adders wire C0, C1, C2; // Instantiate 4 full adders — each feeds carry to next full_adder FA1(A[0], B[0], Cin, Sum[0], C0); full_adder FA2(A[1], B[1], C0, Sum[1], C1); full_adder FA3(A[2], B[2], C1, Sum[2], C2); full_adder FA4(A[3], B[3], C2, Sum[3], Cout); endmodule
🇧🇩 প্রতিটা line বোঝো
  • input [3:0] A — 4-bit input। A[3] হলো MSB (বাম দিক), A[0] হলো LSB (ডান দিক)
  • wire C0, C1, C2 — FA গুলোর মধ্যে carry বহন করার জন্য internal wires। এগুলো বাইরে থেকে দেখা যায় না।
  • full_adder FA1(...) — এটা C++ এ FullAdder fa1 = new FullAdder() এর মতো! Module এর একটা instance তৈরি করছি।
  • Port connection order: full_adder(A, B, Cin, Sum, Cout) এই order অনুযায়ী values দিতে হয়
  • FA1 এর Cout (C0) যায় FA2 এর Cin হিসেবে — এটাই "carry ripple"
Common Exam Trap: Port connection এর order ভুল হলে circuit কাজ করবে না। full_adder FA1(A[0], B[0], Cin, Sum[0], C0) — এখানে পঞ্চম argument C0 হলো Cout, Cin না। Module definition এর order মিলিয়ে দেখো।
Bit Width [3:0] — ঠিকঠাক বোঝো
A[3:0] — 4 bit bus — Example: A = 4'b1010
A[3]
1
MSB
value = 8
A[2]
0
 
value = 4
A[1]
1
 
value = 2
A[0]
0
LSB
value = 1
Binary: 1010
Decimal: 8+0+2+0 = 10
Hex: 0xA
🇧🇩 N-bit formula — মুখস্থ করো
  • N-bit bus = [N-1 : 0]
  • 4-bit = [3:0] → A[3], A[2], A[1], A[0]
  • 8-bit = [7:0] → 256 possible values (0 to 255)
  • 16-bit = [15:0] → 65536 possible values
🎯 Exam Practice

Exam Practice — Code পড়া ও লেখা

তোমার instructor দুই ধরনের question করতে পারেন। দুইটাই practice করো।

Strategy: Code দেখলে সবসময় ৪টা প্রশ্ন করো → (1) কী নাম? (2) কী input? (3) কী output? (4) logic কী করছে?
📋 Type 1: Code → বোঝো এবং Truth Table বানাও
unknown.vExam Question
module mystery (X, Y, Z, Out); input X, Y, Z; output Out; assign Out = ~(X | Y) & Z; endmodule

❓ এই code টা কী করছে? Truth table বানাও।

✍️ Type 2: Description → Code লেখো
Question

"Design a 2-to-1 Multiplexer with inputs A, B (data) and Sel (select). When Sel=0 output Y=A, when Sel=1 output Y=B."

🔥 Type 3: Lecture Code — Explain করো (Slide 34)
four_bit_adder.vExam — From Lecture
module four_bit_adder (A, B, Cin, Sum, Cout); input [3:0] A; input [3:0] B; input Cin; output [3:0] Sum; output Cout; wire C0, C1, C2; full_adder FA1(A[0], B[0], Cin, Sum[0], C0); full_adder FA2(A[1], B[1], C0, Sum[1], C1); full_adder FA3(A[2], B[2], C1, Sum[2], C2); full_adder FA4(A[3], B[3], C2, Sum[3], Cout); endmodule

❓ If A=4'b0011 (3) and B=4'b0101 (5), Cin=0, what are Sum and Cout?

🧠 Self Assessment Quiz

Quiz — নিজেকে Test করো

Lecture শেষ হলে এই quiz দাও। সব ঠিক হলে তুমি ready!