📖 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 Slides 9-13 Verilog Structure & Overview

Verilog Basics — বেসিক স্ট্রাকচার

Before diving into code, let's understand the basic structure and building blocks of Verilog.

What is Verilog?

Verilog হল একটি Hardware Description Language (HDL) যা digital circuits describe করতে ব্যবহৃত হয়। এটি C programming এর মতো দেখতে, কিন্তু এটি hardware তৈরি করে, software নয়।

📝 Verilog Features

  • Case-sensitive: wireWire
  • Semicolon-terminated: প্রতিটা statement ; দিয়ে শেষ
  • Module-based: সব কিছু modules এ লেখা হয়
  • Comments: // single line বা /* multi line */

⚠️ Key Differences from C/C++

  • সব কিছু parallel execute হয় (sequential নয়)
  • Hardware তৈরি হয়, program run করে না
  • wire এবং reg — special data types
  • Time এবং delays important (#10 = 10 time units)
Basic Verilog Program Structure

প্রতিটা Verilog program এর তিনটা main parts:

basic_structure.v template
// 1. Module Declaration — কী তৈরি করছো module my_module ( input A, B, // Input ports output Y // Output ports ); // 2. Module Body — ভেতরে কী হবে // এখানে logic লেখো (gates, operations, etc.) assign Y = A & B; // AND gate // 3. Module End endmodule
বাংলায় বুঝি

module শুরু করো, তারপর input/output declare করো, ভেতরে logic লেখো, শেষে endmodule দিয়ে বন্ধ করো। এত সহজ!

📄 Lecture Slide 12-13 Verilog Keywords, Numbers & Identifiers

Keywords & Notation — শব্দ আর সংখ্যা

Verilog Keywords (Reserved Words)

এই words গুলো Verilog এর জন্য reserved — তুমি variable name হিসেবে ব্যবহার করতে পারবে না।

Module & Structure

module endmodule input output inout wire reg

Behavioral

always initial begin end if else case endcase

Data & Logic

assign parameter integer real time

Control

for while posedge negedge or and not
মনে রাখো

এই keywords lowercase এ লেখতে হয়। Module বা MODULE লিখলে error হবে। সবসময় module লেখো।

Number Notation — সংখ্যা লেখার নিয়ম

Verilog এ different number systems ব্যবহার করা যায়:

Format Syntax Example বাংলা
Binary 'b 4'b1010 4-bit binary: 1010
Hexadecimal 'h 8'hAF 8-bit hex: 10101111
Decimal 'd or none 8'd15 or 15 8-bit decimal: 15
Octal 'o 6'o77 6-bit octal: 111111
number_examples.v examples
// Binary numbers wire [3:0] bin = 4'b1010; // 4 bits: 1010 // Hexadecimal numbers wire [7:0] hex = 8'hFF; // 8 bits: 11111111 // Decimal numbers wire [31:0] dec = 32'd100; // 32 bits: decimal 100 // X (unknown) and Z (high impedance) wire [3:0] unknown = 4'bXXXX; // Don't know value wire [3:0] floating = 4'bZZZZ; // High-Z (disconnected)
Real Example

তুমি যদি 8'hA5 লেখো, এর মানে 8-bit hexadecimal number A5 = binary 10100101। Size (8) + Format ('h) + Value (A5)।

Identifiers — Variable Names

Verilog এ variable/signal names লেখার নিয়ম:

✅ Valid Identifiers

  • wire data_bus;
  • wire clk_50MHz;
  • reg counter_8bit;
  • wire _reset_n; (underscore শুরুতে OK)

❌ Invalid Identifiers

  • wire 8bit; (number দিয়ে শুরু নয়)
  • wire data-bus; (hyphen allowed নয়)
  • wire module; (keyword ব্যবহার নয়)
  • wire clk#; (special characters নয়)
Best Practice

Meaningful names ব্যবহার করো: clk (clock), rst (reset), data_in (input data)। Single letters (a, b) avoid করো except loops এ।

Comments — মন্তব্য লেখার নিয়ম

Code বোঝার জন্য comments খুব গুরুত্বপূর্ণ:

comments.v examples
// Single-line comment — এক লাইনের comment wire clk; // Clock signal /* Multi-line comment একাধিক লাইনের comment Hardware description এখানে */ module adder ( input A, B, output Sum ); assign Sum = A + B; // Addition operation endmodule
📄 Critical Concept wire vs reg — Most Important!

Data Types — wire আর reg

Verilog এর দুটো main data types: wire এবং reg। এই difference টা না বুঝলে কিছুই বুঝবে না!

wire vs reg — কী পার্থক্য?

🔌 wire

Physical wire মতো — সরাসরি connect

  • Continuous assignment (assign)
  • Combinational logic এর জন্য
  • Value সবসময় update হয়
  • Input কে wire declare করো
উদাহরণ

AND gate: output সরাসরি inputs এর উপর depend করে — wire ব্যবহার করো।

💾 reg

Storage element — value store করে

  • Procedural assignment (always block)
  • Sequential logic এর জন্য
  • Value hold করতে পারে
  • always block এ reg ব্যবহার করো
উদাহরণ

Flip-flop: output previous state + clock এর উপর depend করে — reg ব্যবহার করো।

⚠️ Common Confusion

reg মানে এই নয় যে এটা সবসময় physical register! এটা শুধু বলে যে value always block এ assign হবে। Hardware synthesis করার সময় compiler decide করে এটা wire হবে না register হবে।

wire Examples — Continuous Assignment
wire_examples.v combinational logic
module wire_demo; // Declare wires wire A, B, Y; // Continuous assignment — সরাসরি connect assign Y = A & B; // AND gate // A বা B change হলে Y instantly update হয় endmodule // Multi-bit wires module multi_bit; wire [7:0] data_bus; // 8-bit wire wire [3:0] addr; // 4-bit wire assign data_bus = 8'hFF; // Assign value endmodule
মনে রাখো

wire সবসময় assign দিয়ে value পায়। always block এ wire assign করতে পারবে না।

reg Examples — Procedural Assignment
reg_examples.v sequential logic
module reg_demo; reg Q; // Register variable wire clk, D; // Inputs // Procedural assignment — always block এ always @(posedge clk) begin Q = D; // D flip-flop behavior end endmodule // Multi-bit registers module counter; reg [7:0] count; // 8-bit counter wire clk, rst; always @(posedge clk or posedge rst) begin if (rst) count = 8'd0; // Reset to 0 else count = count + 1; // Increment end endmodule
মনে রাখো

reg সবসময় always বা initial block এ assign হয়। assign দিয়ে reg এ value দিতে পারবে না।

Quick Decision Guide

কখন wire আর কখন reg ব্যবহার করবে?

Situation Use বাংলা
Module input/output (most cases) wire Input/output সাধারণত wire
Continuous assignment (assign) wire Combinational logic
Inside always block reg Sequential logic
Storage (flip-flop, counter, FSM) reg Value store করতে হবে
📄 Lecture Slides 18-20 All Verilog Operators

Operators — সব অপারেটর

Verilog এ অনেক ধরনের operators আছে। C/C++ এর মতো, কিন্তু কিছু unique ones also।

1. Bitwise Operators — বিট-লেভেল অপারেশন

প্রতিটা bit এর উপর operation করে:

Operator Name Example Result বাংলা
~ NOT ~4'b1010 4'b0101 প্রতিটা bit উল্টা
& AND 4'b1010 & 4'b1100 4'b1000 Bit-by-bit AND
| OR 4'b1010 | 4'b1100 4'b1110 Bit-by-bit OR
^ XOR 4'b1010 ^ 4'b1100 4'b0110 Bit-by-bit XOR
~^ or ^~ XNOR 4'b1010 ~^ 4'b1100 4'b1001 XOR এর opposite
bitwise_ops.v examples
module bitwise; wire [3:0] A = 4'b1010; wire [3:0] B = 4'b1100; wire [3:0] not_A = ~A; // 0101 wire [3:0] A_and_B = A & B; // 1000 wire [3:0] A_or_B = A | B; // 1110 wire [3:0] A_xor_B = A ^ B; // 0110 endmodule
2. Logical Operators — লজিক্যাল অপারেশন

পুরো expression true/false check করে (C/C++ এর মতো):

Operator Name Example Result বাংলা
! Logical NOT !5 0 Not zero → 0
&& Logical AND (A > 0) && (B < 10) 1 or 0 দুটোই true
|| Logical OR (A == 0) || (B == 0) 1 or 0 যেকোনো একটা true

⚠️ Bitwise vs Logical — পার্থক্য

  • A & B (bitwise) — প্রতিটা bit আলাদাভাবে AND
  • A && B (logical) — পুরো A এবং B দুটোই non-zero? → 1, else 0

Example: 4'b0001 && 4'b00101 (both non-zero)
But: 4'b0001 & 4'b00104'b0000 (bitwise result)

3. Arithmetic Operators — গাণিতিক অপারেশন
Operator Name Example বাংলা
+ Addition A + B যোগ
- Subtraction A - B বিয়োগ
* Multiplication A * B গুণ
/ Division A / B ভাগ
% Modulus A % B ভাগশেষ
arithmetic.v examples
module calc; wire [7:0] A = 8'd10; wire [7:0] B = 8'd3; wire [7:0] sum = A + B; // 13 wire [7:0] diff = A - B; // 7 wire [15:0] prod = A * B; // 30 (double width!) wire [7:0] quot = A / B; // 3 wire [7:0] rem = A % B; // 1 endmodule
4. Relational Operators — তুলনা অপারেশন
Operator Name Example Result
== Equal A == B 1 if equal, else 0
!= Not Equal A != B 1 if not equal
< Less Than A < B 1 if A less than B
> Greater Than A > B 1 if A greater than B
<= Less or Equal A <= B 1 if A ≤ B
>= Greater or Equal A >= B 1 if A ≥ B
5. Special Operators — Concatenation & Replication

Concatenation {}

Multiple signals একসাথে join করো:

wire [1:0] A = 2'b10; wire [1:0] B = 2'b01; wire [3:0] AB = {A, B}; // 4'b1001 wire [5:0] ABC = {A, B, 2'b11}; // 6'b100111

Replication {n{...}}

Same pattern repeat করো:

wire [7:0] all_ones = {8{1'b1}}; // 11111111 wire [5:0] repeat_A = {3{2'b10}}; // 101010
Real Use Case

Replication sign extension এর জন্য useful: {8{sign_bit}, data} — sign bit 8 বার repeat করে data এর আগে বসায়।

📄 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
📄 Lecture Slides 25-26 Continuous Assignment — assign Statement

Dataflow Modeling — assign দিয়ে logic

Dataflow modeling হল সবচেয়ে সহজ way combinational logic describe করার। assign statement ব্যবহার করে continuous assignment করা হয়।

What is Dataflow Modeling?

Dataflow modeling-এ আমরা describe করি data কীভাবে flow করে inputs থেকে outputs এ। Structural modeling এর চেয়ে higher-level।

Structural (আগে শিখেছো)

Gate-by-gate বলে দাও:

and g1(w, a, b); or g2(y, w, c);

Dataflow (এখন শিখছো)

Expression লেখো সরাসরি:

assign y = (a & b) | c;
মনে রাখো

Dataflow = "কী করতে হবে" বলো (what), Structural = "কীভাবে করতে হবে" বলো (how)। Dataflow সহজ এবং পড়তে সুবিধা।

assign Statement — Continuous Assignment
dataflow_examples.v basic examples
// Simple gates using assign module gates_dataflow( input A, B, C, output Y1, Y2, Y3 ); assign Y1 = A & B; // AND gate assign Y2 = A | B | C; // 3-input OR assign Y3 = ~(A & B); // NAND gate endmodule // Full Adder using dataflow module fulladder_dataflow( input A, B, Cin, output Sum, Cout ); // Directly write Boolean equations assign Sum = A ^ B ^ Cin; assign Cout = (A & B) | (B & Cin) | (A & Cin); endmodule // MUX 2-to-1 using dataflow module mux2( input A, B, S, output Y ); assign Y = S ? B : A; // Conditional operator endmodule
বাংলায় বুঝি

assign মানে "continuously assign" — input change হলে output automatically update হয়। এটা একটা real wire connection মতো।

Dataflow Adder Example — আগের Full Adder আরো সহজভাবে

তুমি আগে structural modeling এ full adder দেখেছো (5টা gate instantiate করে)। Dataflow এ সেই একই logic মাত্র ২ লাইনে:

comparison.v structural vs dataflow
// Structural (verbose) — 5 gates module fulladder_structural(A, B, Cin, Sum, Cout); input A, B, Cin; output Sum, Cout; wire w1, w2, w3; xor g1(w1, A, B); xor g2(Sum, w1, Cin); and g3(w2, A, B); and g4(w3, w1, Cin); or g5(Cout, w2, w3); endmodule // Dataflow (concise) — just 2 lines! module fulladder_dataflow(A, B, Cin, Sum, Cout); input A, B, Cin; output Sum, Cout; assign Sum = A ^ B ^ Cin; assign Cout = (A & B) | (B & Cin) | (A & Cin); endmodule
কোনটা ভালো?

Dataflow অনেক cleaner! Boolean expression সরাসরি লেখো, intermediate wires declare করতে হয় না। Combinational logic এর জন্য dataflow preferred।

Multiple assign Statements

তুমি একই module এ একাধিক assign statements লিখতে পারো:

multi_assign.v example
module alu_4bit( input [3:0] A, B, input [1:0] op, output [3:0] Y ); wire [3:0] and_result, or_result, add_result, sub_result; // Parallel assignments — সব একসাথে evaluate হয় assign and_result = A & B; assign or_result = A | B; assign add_result = A + B; assign sub_result = A - B; // Select based on opcode assign Y = (op == 2'b00) ? and_result : (op == 2'b01) ? or_result : (op == 2'b10) ? add_result : sub_result; endmodule
📄 Lecture Slides 27-32 Behavioral Modeling — Procedural Blocks

Behavioral Modeling — always ব্লক

Behavioral modeling সবচেয়ে powerful! এখানে আমরা কী হবে (behavior) describe করি, hardware details নয়। always এবং initial blocks ব্যবহার করে।

always Block — সবসময় চলতে থাকে

always block একটি procedural block যা simulation/synthesis এর সময় সবসময় active থাকে। Sensitivity list দেখে কখন execute হবে।

always_syntax.v basic syntax
// Syntax always @(sensitivity_list) begin // Procedural statements এখানে // Executed যখন sensitivity list এ কিছু change হয় end // Combinational logic — সব inputs এ sensitive always @(*) begin Y = A & B; end // Sequential logic — clock edge এ sensitive always @(posedge clk) begin Q = D; end // Multiple conditions always @(posedge clk or posedge rst) begin if (rst) Q = 0; else Q = D; end
Sensitivity List
  • @(*) — সব inputs (combinational logic এর জন্য)
  • @(posedge clk) — Clock এর rising edge (0→1)
  • @(negedge clk) — Clock এর falling edge (1→0)
  • @(A or B or C) — A, B, অথবা C change হলে
initial Block — শুধু একবার

initial block simulation start এ শুধু একবার execute হয়। Hardware synthesis হয় না — শুধু simulation/testing এর জন্য।

initial_example.v testbench
module test; reg clk, rst; // Initialize signals at start initial begin clk = 0; rst = 1; #10 rst = 0; // After 10 time units, reset off end // Generate clock — toggle every 5 time units always #5 clk = ~clk; endmodule
কখন ব্যবহার করবে?

initial শুধু testbenches এ — signals initialize করতে, test values set করতে। Real hardware এ initial synthesize হয় না।

Behavioral vs Dataflow Comparison
Aspect Dataflow Behavioral
Statement assign always
Data Type wire reg
Best For Combinational logic Sequential & complex logic
Abstraction Medium level High level (like C code)
বাংলা সহজ combinational logic Complex behavior, state machines
📄 Lecture Slide 32 — CRITICAL CONCEPT Most Common Beginner Mistake!

Blocking vs Non-blocking — সবচেয়ে গুরুত্বপূর্ণ!

এটা Verilog এর সবচেয়ে confusing এবং important topic! = (blocking) এবং <= (non-blocking) এর difference না বুঝলে sequential logic এ ভুল হবে।

⚠️ THIS IS CRITICAL — Must Understand!

Blocking (=) আর non-blocking (<=) assignments এর মধ্যে পার্থক্য hardware behavior পুরোপুরি change করে দেয়। Wrong assignment ব্যবহার করলে circuit ভুল হবে!

1. Blocking Assignment (=)

"এক্ষুণি করো, পরের line-এ যাওয়ার আগে" — Sequential execution (C code এর মতো)

blocking.v example
always @(*) begin a = b; // Step 1: a gets b's value c = a; // Step 2: c gets NEW value of a (which is b) end // Suppose b = 2 // After execution: a = 2, c = 2
বাংলায়

Blocking = step-by-step execution। প্রথম line execute হওয়ার পরে পরের line। a = b হওয়ার পরে c = a — তাই c নতুন a পায় (যা b)।

2. Non-blocking Assignment (<=)

"একসাথে করো, parallel এ" — Concurrent execution (real hardware এর মতো)

nonblocking.v example
always @(posedge clk) begin a <= b; // Schedule: a gets b's value c <= a; // Schedule: c gets OLD value of a end // Suppose b = 2, a = 5 initially // After clock edge: a = 2, c = 5 (not 2!)
বাংলায়

Non-blocking = parallel execution। সব statements একসাথে schedule হয়, তারপর clock edge এ একসাথে update হয়। c <= a তে a-র পুরনো value যায় (নতুন নয়)।

Side-by-Side Comparison — দেখো পার্থক্য

Blocking =

always @(posedge clk) begin a = 1; b = a; // b gets 1 end

Result: a = 1, b = 1 (sequential)

Non-blocking <=

always @(posedge clk) begin a <= 1; b <= a; // b gets OLD a end

Result: a = 1, b = (old value of a)

🎯 Golden Rules — মুখস্থ করো!

Rule 1: Combinational Logic

always @(*) → Use = (blocking)

Combinational logic এ blocking assignment ব্যবহার করো। সব operations sequential order এ হবে।

Rule 2: Sequential Logic (Registers/Flip-flops)

always @(posedge clk) → Use <= (non-blocking)

Sequential logic এ non-blocking assignment ব্যবহার করো। সব registers একসাথে parallel এ update হবে (real hardware এর মতো)।

Real Example: Shift Register

Shift register এ blocking vs non-blocking এর impact দেখো:

❌ WRONG (Blocking)

always @(posedge clk) begin q1 = d; // q1 gets d q2 = q1; // q2 gets NEW q1 (which is d!) q3 = q2; // q3 gets d too! end // All flip-flops get same value! 😱

✅ CORRECT (Non-blocking)

always @(posedge clk) begin q1 <= d; // q1 gets d q2 <= q1; // q2 gets OLD q1 q3 <= q2; // q3 gets OLD q2 end // Perfect shift register! ✅
কেন এই পার্থক্য?

Blocking (=) এ প্রতিটা assignment immediately হয়। তাই q1 = d হওয়ার পরে q2 = q1 নতুন q1 পায়।

Non-blocking (<=) এ সব assignments একসাথে schedule হয়, তারপর clock edge এ একসাথে update হয়। তাই q2 <= q1 তে q1-এর পুরনো value যায়। Real hardware এভাবেই কাজ করে!

📄 Lecture Slide 30 if-else & case Statements

Conditional Constructs — শর্ত যুক্ত logic

Hardware-এও condition check করতে হয় (C/C++ এর মতো)। Verilog এ if-else এবং case statements আছে।

if-else Statement

Condition true হলে একটা block execute হয়, else আরেকটা:

if_else.v basic syntax
// Simple if-else always @(*) begin if (A > B) max = A; else max = B; end // Multi-way if-else-if always @(*) begin if (op == 2'b00) result = A & B; // AND else if (op == 2'b01) result = A | B; // OR else if (op == 2'b10) result = A + B; // ADD else result = A - B; // SUB end
মনে রাখো

if সবসময় always block এর ভেতরে। Module body-তে directly if লেখতে পারবে না।

case Statement — Multiple Options

case cleaner যখন অনেক গুলো conditions check করতে হয়:

case_example.v ALU design
module alu( input [3:0] A, B, input [2:0] opcode, output reg [3:0] result ); always @(*) begin case (opcode) 3'b000: result = A & B; // AND 3'b001: result = A | B; // OR 3'b010: result = A + B; // ADD 3'b011: result = A - B; // SUB 3'b100: result = A ^ B; // XOR 3'b101: result = ~A; // NOT default: result = 4'b0000; // Default case endcase end endmodule
case vs if-else কখন কী?
  • case — যখন একটা variable-এর multiple fixed values check করবে (ALU opcodes, state machines)
  • if-else — যখন complex conditions (A > B && C == 0) check করবে
Complete MUX Example with if-else
mux4to1.v 4-to-1 multiplexer
module mux4to1( input [3:0] D, // 4 data inputs input [1:0] S, // 2-bit selector output reg Y // Output (reg because always block) ); always @(*) begin case (S) 2'b00: Y = D[0]; // Select D[0] 2'b01: Y = D[1]; // Select D[1] 2'b10: Y = D[2]; // Select D[2] 2'b11: Y = D[3]; // Select D[3] endcase end endmodule
📄 Lecture Slides 36-37 D Flip-Flop & Registers

Sequential Logic — ফ্লিপ-ফ্লপ আর রেজিস্টার

Sequential logic-এ memory/state থাকে — output শুধু current input নয়, previous state-এরও উপর depend করে। Clock signal এর সাথে synchronize হয়।

D Flip-Flop (Positive Edge-Triggered)

D flip-flop সবচেয়ে basic storage element — clock এর rising edge এ input (D) কে output (Q) তে copy করে এবং hold করে রাখে।

dff.v D flip-flop
module dff( input clk, D, output reg Q ); // Positive edge-triggered D flip-flop always @(posedge clk) begin Q <= D; // Non-blocking! (Sequential logic) end endmodule
কীভাবে কাজ করে?
  1. Clock এর rising edge (0 → 1 transition) detect করো
  2. সেই moment এ D এর value Q তে copy করো
  3. Next rising edge পর্যন্ত Q এর value hold করো
D Flip-Flop with Reset

Real circuits এ reset signal থাকে — chip power on হওয়ার সময় known state এ আসার জন্য:

dff_reset.v with asynchronous reset
module dff_reset( input clk, rst, D, output reg Q ); // Asynchronous reset — rst high হলে তৎক্ষণাৎ reset always @(posedge clk or posedge rst) begin if (rst) Q <= 1'b0; // Reset to 0 else Q <= D; // Normal operation end endmodule
8-bit Register

একাধিক flip-flops একসাথে = register:

register.v 8-bit register
module reg8bit( input clk, rst, load, input [7:0] data_in, output reg [7:0] data_out ); always @(posedge clk or posedge rst) begin if (rst) data_out <= 8'd0; // Reset else if (load) data_out <= data_in; // Load new data // else: keep old value (hold) end endmodule
বাংলায়

এই register load signal high থাকলে data_in store করে। load low থাকলে আগের value hold করে (else condition নেই মানে previous value রাখো)।

Counter Example — Sequential Logic
counter.v 4-bit counter
module counter4bit( input clk, rst, enable, output reg [3:0] count ); always @(posedge clk or posedge rst) begin if (rst) count <= 4'd0; // Reset to 0 else if (enable) count <= count + 1; // Increment // else: hold current value end endmodule
Counter কীভাবে কাজ করে?

প্রতিটা clock rising edge এ (যদি enable = 1): count নিজের value নিয়ে 1 যোগ করে এবং নতুন value store করে। এভাবে 0 → 1 → 2 → ... → 15 → 0 (wrap around) চলতে থাকে।

📄 Lecture Slides 38-43 Finite State Machines (FSM)

State Machines — FSM ডিজাইন

Finite State Machine (FSM) হল sequential circuit যেখানে discrete states আছে। State transitions clock এবং inputs এর উপর নির্ভর করে। খুব powerful concept!

FSM Basics — কী এবং কেন?

FSM একটি abstract model যা behavior describe করে states এবং transitions দিয়ে:

  • States: Discrete conditions (e.g., IDLE, RUNNING, DONE)
  • Transitions: State changes based on inputs/events
  • Outputs: Depend on current state (and/or inputs)
Real-World Example

Traffic Light: States = {RED, YELLOW, GREEN}। Timer শেষ হলে state change (RED → GREEN → YELLOW → RED)। প্রতিটা state এ different output (lights)।

FSM Types

Moore Machine

Output শুধু current state এর উপর depend করে

Output = f(state)

✅ Outputs stable (glitch-free)
❌ May need more states

Mealy Machine

Output state + inputs দুটোর উপর depend করে

Output = f(state, inputs)

✅ Fewer states needed
❌ Outputs may glitch

Simple FSM Example — Traffic Light
traffic_light.v Moore machine
module traffic_light( input clk, rst, output reg [2:0] lights // {RED, YELLOW, GREEN} ); // State encoding parameter RED = 2'b00, GREEN = 2'b01, YELLOW = 2'b10; reg [1:0] state, next_state; // State register — sequential always @(posedge clk or posedge rst) begin if (rst) state <= RED; else state <= next_state; end // Next state logic — combinational always @(*) begin case (state) RED: next_state = GREEN; // RED → GREEN GREEN: next_state = YELLOW; // GREEN → YELLOW YELLOW: next_state = RED; // YELLOW → RED default: next_state = RED; endcase end // Output logic — combinational (Moore) always @(*) begin case (state) RED: lights = 3'b100; // Only RED on GREEN: lights = 3'b001; // Only GREEN on YELLOW: lights = 3'b010; // Only YELLOW on default: lights = 3'b000; endcase end endmodule
FSM Structure

সব FSM-এ তিনটা always blocks:

  1. State Register: @(posedge clk) — non-blocking (<=)
  2. Next State Logic: @(*) — blocking (=)
  3. Output Logic: @(*) — blocking (=)
Complete FSM Example — Sequence Detector

এই FSM sequence 1011 detect করে (serial input থেকে):

sequence_detector.v 1011 detector
module seq_detector( input clk, rst, X, // X = serial input output reg Z // Z = 1 when 1011 detected ); // States: S0 (idle), S1 (got 1), S2 (got 10), S3 (got 101) parameter S0 = 2'b00, S1 = 2'b01, S2 = 2'b10, S3 = 2'b11; reg [1:0] state, next_state; // State register always @(posedge clk or posedge rst) begin if (rst) state <= S0; else state <= next_state; end // Next state logic always @(*) begin case (state) S0: next_state = (X == 1) ? S1 : S0; // Wait for 1 S1: next_state = (X == 0) ? S2 : S1; // Got 1, wait for 0 S2: next_state = (X == 1) ? S3 : S0; // Got 10, wait for 1 S3: next_state = (X == 1) ? S1 : S0; // Got 101, check last bit default: next_state = S0; endcase end // Output logic (Moore) — শুধু state দেখে always @(*) begin case (state) S3: Z = (X == 1); // Output 1 if got 1011 default: Z = 0; endcase end endmodule
বাংলায় বুঝি

S0 = শুরু, S1 = "1" পেয়েছি, S2 = "10" পেয়েছি, S3 = "101" পেয়েছি। যদি S3 তে থাকা অবস্থায় X=1 আসে, তাহলে পুরো sequence "1011" complete — Z = 1 output!

📄 Lecture Slide 11, 15 Module Instantiation & Hierarchy

Hierarchical Design — মডিউল দিয়ে মডিউল

বড় circuits ছোট modules দিয়ে তৈরি করতে হয়। একটা module আরেকটা module instantiate করতে পারে — এটাই hierarchical design

Why Hierarchical Design?

✅ Benefits

  • Reusability: একবার লিখে বারবার ব্যবহার
  • Maintainability: ছোট modules সহজে fix করা যায়
  • Readability: Code organized এবং clean
  • Testing: প্রতিটা module আলাদাভাবে test করো

📊 Example Hierarchy

Processor (top)
  ├── ALU
  │   ├── Adder
  │   └── Comparator
  ├── Register File
  └── Control Unit
      ├── Decoder
      └── FSM
                
Module Instantiation — আগে দেখেছো

তুমি আগে full adder এবং 4-bit adder এ module instantiation দেখেছো। আরো examples:

hierarchy_example.v building blocks
// Low-level module — 2-to-1 MUX module mux2( input a, b, sel, output y ); assign y = sel ? b : a; endmodule // Mid-level module — 4-to-1 MUX using three 2-to-1 MUXes module mux4( input [3:0] d, input [1:0] sel, output y ); wire w0, w1; // Instantiate three mux2 modules mux2 m0(d[0], d[1], sel[0], w0); // First pair mux2 m1(d[2], d[3], sel[0], w1); // Second pair mux2 m2(w0, w1, sel[1], y); // Final selection endmodule // Top-level module — Register file using mux4 module reg_file_simple( input clk, input [1:0] read_addr, input [3:0] write_data, output [3:0] read_data ); reg [3:0] regs [0:3]; // 4 registers // Use mux4 to select register mux4 read_mux( .d({regs[3], regs[2], regs[1], regs[0]}), .sel(read_addr), .y(read_data) ); endmodule
Hierarchy Benefits

mux2 একবার লিখে mux4 তে ৩ বার ব্যবহার করেছি। mux4 আরো বড় design এ ব্যবহার করেছি। এভাবে reusability আর clarity দুটোই পাও!

📄 Lecture Slide 42-43 + Extra Testing Your Verilog Modules

Testbenches — মডিউল টেস্ট করো

একটা module লেখার পরে এটা test করতে হয় — input values দিয়ে output check করতে হয়। এই test code-কে বলে testbench

What is a Testbench?

Testbench একটি special Verilog module যা:

  • Design-under-test (DUT) module instantiate করে
  • Test inputs generate করে
  • Outputs monitor করে
  • Expected values এর সাথে compare করে
  • Results display করে
Important

Testbench hardware synthesize হয় না — এটা শুধু simulation এর জন্য। তাই initial blocks, $display, delays ইত্যাদি freely ব্যবহার করতে পারো।

Basic Testbench Structure
testbench_template.v basic template
module testbench; // 1. Declare signals — inputs as reg, outputs as wire reg A, B, Cin; // Test inputs (we drive these) wire Sum, Cout; // Test outputs (module drives these) // 2. Instantiate DUT (Device Under Test) fulladder dut( .A(A), .B(B), .Cin(Cin), .Sum(Sum), .Cout(Cout) ); // 3. Generate test vectors initial begin // Display header $display("Testing Full Adder..."); $display("A B Cin | Sum Cout"); $display("-------------------"); // Test case 1 A = 0; B = 0; Cin = 0; #10; $display("%b %b %b | %b %b", A, B, Cin, Sum, Cout); // Test case 2 A = 0; B = 0; Cin = 1; #10; $display("%b %b %b | %b %b", A, B, Cin, Sum, Cout); // Test all 8 combinations... A = 0; B = 1; Cin = 0; #10; $display("%b %b %b | %b %b", A, B, Cin, Sum, Cout); // ... $finish; // End simulation end endmodule
System Tasks for Testing

Verilog এ testing এর জন্য special system tasks আছে ($ দিয়ে শুরু):

Task Purpose Example
$display Print to console $display("Sum = %d", sum);
$monitor Auto-print on change $monitor("Q = %b", Q);
$finish End simulation $finish;
$time Current simulation time $display("Time = %t", $time);
Complete Testbench Example — Counter Test
counter_tb.v testbench
module counter_tb; // Testbench signals reg clk, rst, enable; wire [3:0] count; // Instantiate counter counter4bit dut( .clk(clk), .rst(rst), .enable(enable), .count(count) ); // Generate clock — toggle every 5 time units initial begin clk = 0; forever #5 clk = ~clk; // 100 MHz (10ns period) end // Test stimulus initial begin // Initialize rst = 1; enable = 0; #15 rst = 0; // Release reset after 15ns // Enable counting #10 enable = 1; // Let it count for 200ns #200; // Disable counting enable = 0; #50; // Test reset during count enable = 1; #30 rst = 1; #10 rst = 0; #100; $display("Test completed!"); $finish; end // Monitor output initial begin $monitor("Time=%0t rst=%b enable=%b count=%d", $time, rst, enable, count); end endmodule
বাংলায় বুঝি

এই testbench clock generate করে (forever #5 clk = ~clk), reset/enable signals control করে, এবং counter output monitor করে। প্রতিটা clock cycle এ count value print হয়।

How to Run Testbench

Verilog simulator (Icarus Verilog, ModelSim, ইত্যাদি) ব্যবহার করে:

Terminal compilation & simulation
# Compile (Icarus Verilog) iverilog -o sim counter.v counter_tb.v # Run simulation vvp sim # View waveform (if using $dumpfile/$dumpvars) gtkwave counter.vcd
📄 Advanced Topics Reusable & Scalable Design

Advanced Verilog — Parameters, Generate, Tasks

এই concepts industry-grade design এর জন্য essential। Code reusability এবং scalability dramatically increase করে।

📄 Real-World Topics Memories, Design Patterns, Best Practices

Real-World Verilog — Industry Best Practices

Professional Verilog design এর জন্য memory modeling, common design patterns, এবং synthesis best practices শিখো।

🎯 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!

🎯 Comprehensive Assessment

Comprehensive Quizzes — সব Topics Test

70+ questions covering ALL topics from basics to advanced। প্রতিটা quiz 10 questions। সব quizzes complete করলে তুমি Verilog expert!

📊 Quiz Structure

  • Quiz 1: Verilog Basics & Keywords (10 questions)
  • Quiz 2: Data Types — wire vs reg (10 questions)
  • Quiz 3: All Operators (10 questions)
  • Quiz 4: Modeling Styles (10 questions)
  • Quiz 5: Blocking vs Non-blocking — CRITICAL! (10 questions)
  • Quiz 6: Advanced Topics (10 questions)
  • Quiz 7: FSM & Sequential Logic (10 questions)

Total: 70 Questions covering ENTIRE Verilog curriculum!

Quiz 1: Verilog Basics & Keywords

Quiz 2: Data Types — wire vs reg

Quiz 3: All Operators

Quiz 4: Modeling Styles

Quiz 5: Blocking vs Non-blocking — CRITICAL!

Quiz 6: Advanced Topics

Quiz 7: FSM & Sequential Logic

🎉 Congratulations!

তুমি যদি সব quizzes complete করো এবং 80%+ score পাও, তুমি Verilog HDL expert! এখন তুমি industry-grade digital designs করতে পারবে।

  • ✅ Combinational এবং Sequential logic design
  • ✅ FSM implementation
  • ✅ Parameterized, reusable modules
  • ✅ Professional coding standards
  • ✅ Testbench development