Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบ >> Mac

ภายใน CPU: ทรานซิสเตอร์สร้าง Instruction Set Architecture อย่างไร

CPU (Central Process Unit) เป็นสมองของคอมพิวเตอร์ และการเชื่อมต่อหลักระหว่างซอฟต์แวร์และฮาร์ดแวร์ มันทำให้เป็นไปได้ที่จะรันซอฟต์แวร์บนฮาร์ดแวร์

แต่มันทำงานยังไงในรายละเอียดลึก? และมันเชื่อมโยงโปรแกรมกับฮาร์ดแวร์คอมพิวเตอร์เฉพาะได้อย่างไร?

บทความนี้มุ่งทำให้คุณเข้าใจการเชื่อมต่อนี้ โดยอธิบายอย่างลึกซึ้งว่า CPU ทำงานยังไง หัวข้อนี้มักคุ้นเคยเฉพาะคนที่มีพื้นฐาน computer hardware design จากวิทยาลัย

บ่อยครั้ง นักวิทยาศาสตร์คอมพิวเตอร์หลายคน ไม่มีคลาส advanced digital logic แม้โปรแกรมเมอร์มืออาชีพมาก็อาจขาดความเข้าใจว่า CPU ประมวลผลข้อมูลจริงๆ ยังไง

แม้เราจะไม่ได้ออกแบบ logic gates จาก transistors หรือ CPU components จาก logic gates เราจะครอบคลุม key concepts ที่จำเป็นเพื่อเข้าใจว่า CPU ประมวลผล data ที่สร้างโดยโปรแกรมที่เขียนในภาษาโปรแกรมมิ่งอย่างไร

เราจะเห็น:

  • Analogy: บทนำสิ่งที่ทำให้ CPU ทำงาน
  • The Memory Hubs: เข้าใจ RAM และ ROM
  • The Roadways of Data: นำทาง CPU Data Path
  • Traffic Controllers: บทบาท State Machines ใน CPU
  • Daily Routines: Fetch-Execute Cycle อธิบาย
  • The Rulebook: ถอดรหัส Instruction Set Architecture (ISA)
  • จากภาษาโปรแกรมมิ่งสู่ machine code
  • City Challenges: จัดการปัญหา CPU
  • สรุป: Control units และ data parts ที่ดีกว่า

ผมจะใช้ Intel 8008 เป็น reference

Analogy: บทนำสิ่งที่ทำให้ CPU ทำงาน

เพื่อเข้าใจลึกๆ ว่าคอมพิวเตอร์ทำงานยังไง มาจินตนาการเมืองเป็น real-life scenario เราจะเปรียบเทียบ computer elements กับส่วนต่างๆ ของเมือง

วิธีนี้ให้มุมมองชัดเจนกว่าของ CPU parts ต่างๆ และว่าทำไมมันสำคัญ หลังจากนั้น เราจะลงลึกแต่ละ component

The Memory Hubs: เข้าใจ RAM และ ROM

RAM (Random access memory) เหมือนห้องสมุดสาธารณะของเมือง: เก็บหนังสือและข้อมูลให้คนยืมและคืนได้ตามต้องการ

ในคอมพิวเตอร์ RAM โหลด data และ instructions จาก computer memory ที่ CPU ต้องการเพื่อประมวลผล data

ROM (Read Only Memory) เหมือนหอจดหมายเหตุประวัติศาสตร์ในเมือง: เก็บบันทึกที่จะไม่เปลี่ยนแปลงและไม่ได้ยืมให้สาธารณะ

The Roadways of Data: นำทาง CPU Data Path

CPU data path คือ network ของถนนในเมือง Buses และ registers ของ CPU data path ทำหน้าที่เหมือนเครือข่ายถนนของเมือง

เช่นเดียวกับถนนช่วยให้รถและคนเคลื่อนที่ CPU data path รับรองว่า data เดินทางอย่างมีประสิทธิภาพใน CPU

Traffic Controllers: บทบาท State Machines ใน CPU

State machines ทำหน้าที่เป็น traffic control systems

ระบบควบคุมจราจรจัดการ flow ของยานพาหนะ และ state machines จัดการ flow ของ data ตาม instructions ที่ให้กับ CPU

Daily Routines: Fetch-Execute Cycle อธิบาย

Fetch-execute cycle คือ daily commute สำหรับชาวเมือง

ทุกวัน คนตัดสินใจว่าจะไปไหน เดินทางไป ทำหน้าที่ และกลับบ้าน กระบวนการนี้ซ้ำตลอด

เช่นเดียวกัน CPU fetch instructions, decode และ execute ใน cycle ที่ซ้ำ

The Rulebook: ถอดรหัส Instruction Set Architecture (ISA)

Instruction set architecture เหมือนกฎหมายการขนส่งของเมือง

กฎหมายการขนส่งเมืองระบุว่าอะไรถูกกฎหมายในเมือง เกี่ยวกับการขนส่งคน

Instruction set architecture คือชุดกฎและ instructions ที่ CPU สามารถ execute ได้

The Memory Hubs: เข้าใจ RAM และ ROM

ภายใน CPU: ทรานซิสเตอร์สร้าง Instruction Set Architecture อย่างไร Photo by Valentine Tanasovich: https://www.pexels.com/photo/black-and-gray-computer-motherboard-2588757/

RAM ย่อมาจาก Random Access Memory และสามารถ read และ write data ได้

CPU ได้ data จาก computer memory ไป RAM ก่อน เพื่อหลีกเลี่ยง long waiting times

จากนั้น ใช้ data จาก RAM เพื่อทำ instructions ให้เสร็จ

พวกเขาใช้ในคอมพิวเตอร์และอุปกรณ์อิเล็กทรอนิกส์มาก เนื่องจาก memory เป็น volatile หมายถึง data อยู่เฉพาะตอนคอมพิวเตอร์เปิด ทำให้เหมาะสำหรับ temporary storage ขณะอุปกรณ์ทำงาน

ROM ย่อมาจาก Read Only Memory ที่นั่น มีแค่ data ที่เพิ่มระหว่าง manufacturing ของคอมพิวเตอร์

พวกเขาใช้ใน firmware สำหรับอุปกรณ์, BIOS และ small embedded systems อย่างแพร่หลาย

เพราะ ROM เป็น non-volatile memory หมายถึงมันคงอยู่ใน memory เมื่ออุปกรณ์ปิด ทำให้มันสำคัญมากสำหรับ permanent data storage

The Roadways of Data: นำทาง CPU Data Path

ภายใน CPU: ทรานซิสเตอร์สร้าง Instruction Set Architecture อย่างไร Photo by Rogeer Marques: https://www.pexels.com/photo/close-up-shot-of-a-chip-processor-11272008/

CPU data path เป็น complex digital circuit มีหลาย components ทำงานร่วมกัน เช่น:

  • Arithmetic Logic Unit (ALU): ทำ arithmetic และ logical operations ภายใน CPU data part
  • Registers: สถานที่เก็บข้อมูลเล็ก เร็ว สำหรับ temporary data ที่ดึงจาก RAM
  • Buses: Data, control และ address buses เป็น wires ที่ใช้ใน CPU data path ถ่ายโอน information

แม้ CPUs จะเปลี่ยนแปลงมากตั้งแต่ Intel 8008 นี่คือ components บางส่วนที่ยังคงเป็น foundation สำหรับ CPUs ทั้งหมด

ขอบคุณพวกเขา ทำให้ data ไหลได้ แตไม่ควบคุม actual flow นี่คือหน้าที่ของ control unit ใน CPU ที่สร้างใน Intel 8008 เป็น state machines

Traffic Controllers: บทบาท State Machines ใน CPU

State machine คือระบบที่ transition ระหว่าง states ต่างๆ เพื่อทำ tasks

พวกเขาประกอบด้วย states และ transitions จำนวนหนึ่ง พวกเขาถูกใช้ใน Intel 8008 เพื่อสร้าง control unit เนื่องจาก structure และ effective way ในการจัดการ sequence of operations ที่จำเป็นในการประมวลผล instructions

แต่ละ state สามารถ activate หนึ่งหรือหลาย CPU components เพื่อประมวลผล assembly instruction เฉพาะ

ด้วยวิธีนี้ CPU data path parts บางส่วนถูก activate สำหรับ instruction ให้เสร็จ

เพิ่มเติม ขอบคุณ state machines เหล่านี้ CPU จึงสมบูรณ์ และสามารถ perform instructions ทั้งหมดที่ user ต้องการ ใน continuous loop ที่เรียกว่า fetch-execute cycle

Daily Routines: Fetch-Execute Cycle อธิบาย

State machine ใน CPU ควบคุมว่า CPU data path ทำงานร่วมกันยังไง เพื่อ perform instruction ที่ให้มา

ทุกวันนี้ คอมพิวเตอร์ทุกเครื่องรับ instructions หลายล้านต่อวินาที ดังนั้น state machines ทำหน้าที่เป็น loop เพื่อ get instructions และ execute

กระบวนการนี้รู้จักว่า fetch-execute cycle ที่ CPU retrieves และ executes instructions:

  • Fetch: CPU ดึง instruction จาก memory
  • Decode: Instruction ที่ดึงมา ถูก decode เพื่อกำหนด action ที่จำเป็น
  • Execute: Decoded instruction ถูก execute ด้วย CPU components ที่เหมาะสม
  • Write-back: ผลลัพธ์ของ execution ถูกเขียนกลับ memory หรือ register

ใน fetch stage control unit บอก RAM ให้ instruction ถัดไปแก่ CPU

ใน decode stage CPU ตีความ instruction และใน execution stage มัน perform operation หลังจากนั้น write-back stage รับรองว่าผลลัพธ์ถูกเก็บถูกต้อง

Cycle นี้ดำเนินต่อไปตลอดที่ PC เปิดอยู่ ดังนั้นใน modern processors ประมวลผล instructions หลายพันล้านต่อวินาที

แตข้อมูลจาก Keyboard หรือ Mouse ล่ะ?

Data นี้ไม่ได้มาจาก RAM แตจัดการผ่าน mechanism ที่เรียกว่า interrupts ในขณะที่ CPU รัน instructions มันสามารถ detect ได้ว่า data มาจาก peripherals

ถ้าเกิดขึ้น CPU หยุด current task และ prioritize instructions จาก peripherals หลังจากนั้น CPU resume previous tasks

มีหลายวิธีจัดการ interrupts บางอย่างที่นิยมคือ:

  1. Polled Interrupts: CPU ตรวจสอบเป็นระยะว่ามี interrupt เกิดขึ้นหรือไม่
  2. Vectored Interrupts: อุปกรณ์ที่ interrupt ส่ง CPU ไป interrupt service routine ที่เหมาะสม
  3. Prioritized Interrupts: Interrupts ถูก assign priority levels ต่างกัน รับรองว่า critical tasks ถูกจัดการก่อน

ด้วย mechanisms เหล่านี้ CPU รักษาประสิทธิภาพขณะโต้ตอบ peripherals

The Rulebook: ถอดรหัส Instruction Set Architecture (ISA)

ด้วย control unit, complete CPU และ RAM สามารถ perform instructions หลายอย่างได้

แต instructions อะไรที่ perform ได้บน CPU ที่กำหนด? และกี่อัน? นี่คือสิ่งที่ Instruction Set Architecture (ISA) แก้

ISA กำหนดชุด instructions ที่ CPU 某个 สามารถ execute ได้ มันคือสิ่งที่ให้ programmers เข้าใจว่า processor ทำได้อะไรและทำไม่ได้ โดยไม่ต้องเข้าใจ digital logic hardware ทั้งหมดภายใน

ด้วยวิธีนี้ มันทำหน้าที่เป็น interface ระหว่าง software และ hardware

Key Aspects of ISA:

  • Instruction Types: รวม arithmetic, logical, control และ data transfer instructions
  • Addressing Modes: Methods สำหรับ specify operands ของ instructions
  • Registers: ชุด registers ที่พร้อมใช้โดย instructions

Common ISAs:

  • x86: ใช้กันแพร่หลายใน desktop และ server processors
  • ARM: ครองชีพใน mobile และ embedded devices เนื่องจาก power efficiency
  • RISC-V: Open standard ISA ออกแบบสำหรับ wide range of applications

CPU แต่ละตัวมักมี version ของ instruction-set architecture ของตัวเอง และ ISA บ่อยครั้งถูกกำหนดด้วย assembly programming languages

นี่คือเหตุผลว่าทำไมมี versions เยอะของ assembly programming language

เพราะแต่ละ CPU มี hardware specifications ของตัวเอง แต่ละตัวจะมี components คล้าย CPU อื่นๆ และดังนั้น assembly programming languages ที่คล้ายกัน associated

การเลือก ISA ส่งผลต่อ CPU design, performance และ compatibility กับ software

ตัวอย่าง ความซับซ้อนของ x86 อนุญาต powerful desktop applications ในขณะที่ความเรียบง่ายของ ARM ช่วย energy-efficient mobile devices

จากภาษาโปรแกรมมิ่งสู่ Machine Code

ภายใน CPU: ทรานซิสเตอร์สร้าง Instruction Set Architecture อย่างไร Photo by luis gomes: https://www.pexels.com/photo/close-up-photo-of-programming-of-codes-546819/

แม้แต่ละ processor จะมี assembly language ของตัวเอง การจัดการ code ใน assembly และเขียน code ใน assembly เพื่อทำ big programs อาจซับซ้อน

มันซับซ้อนมาก และอาจนำไปสู่เวลาที่สูญเปล่าในการแก้ไขรายละเอียด แทนที่จะแบบรวดเร็วและง่าย จัดการ development ของโปรแกรมและพัฒนาจริง

เพื่อแก้ปัญหานี้ หลาย programming languages ถูกสร้างจาก assembly เราเขียน code ใน programming languages และมันจะถูก convert เป็น assembly

ด้วยวิธีนี้ แทนที่จะใช้เวลากับรายละเอียด สามารถโฟกัสที่สิ่งสำคัญกว่า เช่น system development และ algorithm design

นี่คือกระบวนการที่ programming languages ส่วนใหญ่ convert code ของพวกเขาเป็น assembly:

  1. Convert code เป็น assembly code ผ่าน compiler หรือ interpreter
  2. Assembly code ถูก convert เป็น raw machine code และ executed โดย CPU
  3. A specific loop ใน CPU's state machine เสร็จสิ้น
  4. หลังจากนั้น CPU fetch และ execute instruction ถัดไป

มา看例子ของ programming languages 2 ตัวที่ทำอย่างนี้!

C Programming Language

C programming language สร้างจาก assembly ในช่วงต้น 1970s มันถูกสร้างเพื่อให้ higher-level language สำหรับ efficient system-level programming ที่ยังอนุญาต hardware manipulation

ด้วย compiler C code ถูก convert เป็น assembly และประมวลผลโดย complete CPU

ขอบคุณ conversion นี้ ด้วยการเขียนโปรแกรมใน C programming language เราสามารถ address ปัญหาหลายอย่างอย่างมีประสิทธิภาพ เช่น:

  • Memory management errors
  • Buffer overflows
  • Manual optimization issues

ทุกวันนี้ แม้สำหรับ simpler tasks assembly code ที่ convert จาก C compiler มีประสิทธิภาพและน่าเชื่อถือกว่ามนุษย์เขียน assembly code

ถ้าอยากเรียนรู้เพิ่มเติมเกี่ยวกับ C compiler เช็คได้ที่:

Python Programming Language

Python programming language สร้างจาก C ในช่วงปลาย 1980s

เป้าหมายคือให้ user-friendly, high-level programming language ที่เน้น readability และ simplicity อนุญาต rapid application development

ใน Python interpreter convert Python code เป็น bytecode บรรทัดต่อบรรทัด

และ bytecode นี้ถูก convert เป็น machine code ใน CPU และประมวลผลใน fetch-execute cycle

ด้วยวิธีนี้ คนสามารถเขียนโปรแกรมได้ง่าย และโฟกัสที่ bigger programs เช่น:

  • Artificial intelligence models
  • Web apps
  • Data analysis
  • Scientific computing

แตท้าทายกับ CPUs ใน programming languages ทั้งหมด คือ มันประมวลผล data แบบ sequential

City Challenges: จัดการปัญหา CPU

ภายใน CPU: ทรานซิสเตอร์สร้าง Instruction Set Architecture อย่างไร Photo by Peng LIU: https://www.pexels.com/photo/timelapse-photography-of-vehicle-on-concrete-road-near-in-high-rise-building-during-nighttime-169677/

Traditional one core CPU ประมวลผล data แบบ sequential instruction ต่อ instruction นี่กลายเป็น limitation ถ้าเรามี instructions เยอะที่ต้องประมวลผล

นี่คือสิ่งที่ GPUs (Graphics processing units) มาแก้ ขอบคุณ GPUs เราสามารถประมวลผล instructions แบบ parallel ได้ โดยลด computing time อย่างมีนัยสำคัญ

ด้วย parallel processing capabilities นี้ สามารถ achieve faster computation และ improved efficiency ใน wide range of applications ได้

สรุป: Control Units และ Data Parts ที่ดีกว่า

ภายใน CPU: ทรานซิสเตอร์สร้าง Instruction Set Architecture อย่างไร Photo by Miguel Á. Padriñán: https://www.pexels.com/photo/green-circuit-board-343457/

นอกจาก modern CPUs เป็น multicore แล้ว advancements ใน control units และ data paths เล่นบทบาทสำคัญในการปรับปรุง processor performance

Control units มักออกแบบด้วย microprogramming หรือ hardwired control units

Microprogramming ให้ greater flexibility และ easier updates ของ control logic ในขณะที่ hardwired control units ให้ faster performance โดย implement control signals โดยตรง

อีก advancement สำคัญคือการสำรวจ new materials สำหรับ transistors ใน logic gates

แทนที่จะพึ่งพา silicon เพียงอย่างเดียว นักวิจัยกำลังสำรวจ alternative materials เพื่อสร้าง processors ที่เร็วกว่าและมีประสิทธิภาพมากกว่า

เมื่อเทคโนโลยีพัฒนาต่อไป การเข้าใจ fundamental concepts เหล่านี้จะยังคงจำเป็นสำหรับทั้ง enthusiasts และ professionals ในสาขานี้

การติดตาม developments เหล่านี้ รับรอง continued innovation และ improvement ของ CPU design และ functionality

เรียนรู้การเขียนโค้ดฟรี หลักสูตรโอเพนซอร์สของ freeCodeCamp ช่วยให้ผู้คน 40,000+ คนได้งานเป็นนักพัฒนา เริ่มต้นเลย