Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ฮาร์ดแวร์ >> CPU

เข้าใจการทำงานของ CPU: หัวใจและสมองของคอมพิวเตอร์

CPU หรือ Microprocessor เป็นหัวใจและ/หรือสมองของคอมพิวเตอร์ Deep Dive เข้าสู่แก่นกลาง เพื่อช่วยเขียนโปรแกรมได้มีประสิทธิภาพมากขึ้น

"เครื่องมือมักเรียบง่ายกว่าเครื่องจักร ใช้กับมือ ขณะที่เครื่องจักรขับเคลื่อนด้วยพลังสัตว์หรือไอน้ำ" – Charles Babbage

คอมพิวเตอร์ เป็น เครื่องจักร ขับเคลื่อนด้วยไฟฟ้า แต่ความยืดหยุ่นและโปรแกรมได้ ทำให้เรียบง่าย כמו เครื่องมือ

CPU เป็นหัวใจและ/หรือสมอง คอมพิวเตอร์ รันคำสั่งที่ให้มา หน้าที่หลัก: คำนวณคณิตศาสตร์/ตรรกะ และประสานคำสั่ง

สองส่วนประกอบหลักของ Processor

  • Control Unit (CU)
  • Arithmetic and Logic Unit (ALU)

Control Unit (CU)

CU = ส่วนของ CPU ประสานการ Execute คำสั่ง บอกว่าทำอะไร ตามคำสั่ง เปิดใช้งานสายสัญญาณเชื่อม CPU กับส่วนอื่นๆ รวม ALU CU เป็นส่วนแรกที่รับคำสั่งมา Process

2 ประเภท CU:

  • Hardwired Control Units - ฮาร์ดแวร์ ต้องเปลี่ยนฮาร์ดแวร์ถึงจะแก้ไขได้
  • Microprogrammable Control Units - โปรแกรมได้ เปลี่ยนพฤติกรรมได้

Hardwired CU = เร็วกว่า Microprogrammable = ยืดหยุ่นกว่า

Arithmetic and Logic Unit (ALU)

ALU ทำคำนวณคณิตศาสตร์และตรรกะ: บวก ลบ ประกอบด้วย Logic Circuitry/Logic Gates

Logic Gates ส่วนใหญ่รับ Input 2 ตัว ผล Output 1 ตัว

ตัวอย่าง Half Adder Circuit: Input A, B Output S (Sum) และ C (Carry)

เข้าใจการทำงานของ CPU: หัวใจและสมองของคอมพิวเตอร์
Half Adder Source: Wikipedia

ที่เก็บข้อมูล - Registers และ Memory

CPU รันคำสั่ง ต้องการ Data (Input, Intermediate, Output) เก็บใน:

Registers

Register = ชุดที่เก็บ Data เล็กๆ ประกอบจาก Latches (Flip-flops) = Logic Gates รวมกันเก็บ 1 Bit

Latch มี 2 Input Wire (Write, Input) และ 1 Output Wire Enable Write -> เปลี่ยนข้อมูล Disable Write -> Output คงที่

เข้าใจการทำงานของ CPU: หัวใจและสมองของคอมพิวเตอร์
SR Latch จาก NOR Gates คู่ขวาง

CPU มี Registers เก็บ Output ส่ง RAM ช้า (Intermediate Data) ส่ง Register อื่นผ่าน BUS Register เก็บ Instruction, Output, Address, หรือ Data อะไรก็ได้

Memory (RAM)

RAM = Registers จำนวนมาก จัดเรียง Compact เก็บ Data มากขึ้น Volatile (ข้อมูลหายตอนปิดไฟ) อ่าน/เขียน: Input Address 8-bit, Data Input, Read/Write Enable

คำสั่ง (Instructions) คืออะไร?

Instruction = Granular Computation ที่ CPU ทำได้ มีหลายประเภท:

  • Arithmetic: ADD, SUBTRACT
  • Logic: AND, OR, NOT
  • Data: MOVE, INPUT, OUTPUT, LOAD, STORE
  • Control Flow: GOTO, IF...GOTO, CALL, RETURN
  • End Program: HALT

ส่งคำสั่งผ่าน Assembly Language หรือ Compiler/Interpreter Hardwired ใน CPU: ALU จัดการ Arithmetic/Logic, CU จัดการ Control Flow

1 Clock Cycle = 1 Instruction (สมัยใหม่ทำได้หลายคำสั่ง)

กลุ่มคำสั่งที่ CPU ทำได้ = Instruction Set

CPU Clock

Clock Cycle = ความเร็วคอมพิวเตอร์ = จำนวน Clock Periods ต่อวินาที

Clock Cycle เดียวเล็กมาก ~250 × 10⁻¹² วินาที Clock Cycle มาก = Processor เร็ว

วัดเป็น GHz (Gigahertz) 1 GHz = 10⁹ Hz (Cycles/Second)

Clock Cycle เร็ว = CPU รัน Instruction มากขึ้น

Clock Cycle = 1/Clock Rate

CPU Time = Instruction Count / Clock Rate

ปรับปรุง CPU Time: เพิ่ม Clock Rate หรือ ลด Instruction Count (Optimize Code) บาง Processor เพิ่ม Clock ได้ แต่อาจ Overheat/Smoke/Fire

คำสั่ง Execute อย่างไร?

Instructions เก็บใน RAM ต่อเนื่อง CPU สมมติ: Instruction = OP Code + Memory/Register Address

CU มี 2 Registers: Instruction Register (IR) โหลด OP Code, Instruction Address Register โหลด Address คำสั่งปัจจุบัน Register อื่นเก็บค่าที่ Address (4 บิตท้ายคำสั่ง)

ตัวอย่าง: บวก 2 จำนวน

STEP 1 — LOAD_A 8: Instruction ใน RAM = <1100 1000> 4 บิตแรก = OP Code = LOAD_A Decode -> โหลด Data ที่ Address 1000 (4 บิตท้าย) ไป Register A

STEP 2 — LOAD_B 2: โหลด Data ที่ Address 0010 ไป Register B

STEP 3 — ADD B A: CU สั่ง ALU บวก บันทึกผลกลับ Register A

STEP 4 — STORE_A 23: บันทึก Register A ไป Address 23

บวก 2 จำนวนสำเร็จ!

BUS

Data ระหว่าง CPU, Register, Memory, I/O Devices ถ่ายโอนผ่าน Bus

CPU ใส่ Address บน Address Bus, ผลบวกบน Data Bus, Enable Signal บน Control Bus -> โหลดข้อมูลเข้ Memory

เข้าใจการทำงานของ CPU: หัวใจและสมองของคอมพิวเตอร์
System Bus Architecture

Cache

CPU Prefetch Instructions ไป Cache รู้ไหม? ล้านคำสั่ง/วินาที -> ใช้เวลา Fetch จาก RAM มากกว่า Execute Cache Prefetch Instructions/Data บ้าง -> Execute เร็วขึ้น

Data ใน Cache และ RAM ต่างกัน = Dirty Bit

Instruction Pipelining

CPU สมัยใหม่ใช้ Instruction Pipelining สำหรับ Parallelization: Fetch, Decode, Execute พร้อมกัน

คำสั่งตัวนึงอยู่ Decode ตัวถัดไป Fetch ได้พร้อมกัน

เข้าใจการทำงานของ CPU: หัวใจและสมองของคอมพิวเตอร์
4-Stage Pipeline

ปัญหา: คำสั่งอาศัยกัน -> Processor Execute Out-of-order (คำสั่งที่ไม่ Dependent)

Multi-core Computer

หลาย CPU แชร์ Resource บางอย่าง (Cache)

Performance (ประสิทธิภาพ)

Performance = 1 / Execution Time

โปรแกรมรัน 20ms -> Performance = 1/20 = 0.05

Relative Performance = Exec Time 1 / Exec Time 2

ปัจจัย CPU Performance: Instruction Execution Time + Clock Speed

เพิ่ม Performance: เพิ่ม Clock Speed หรือ ลด Instruction Count (Optimize Code)

Processor Speed มีขีดจำกัด Multi-core รันล้านคำสั่ง/วินาที แต่โค้ดเยอะ -> Performance ต่ำ

Big O Notation บอก Given Input -> Performance แทนไง

CPU Optimize มาก เขียนโปรแกรมต้องคิด: ลด Instruction Count -> เพิ่ม Performance

Also Posted on Milap Neupane Blog: How Does a CPU work