逐字稿 · CC BY 4.0

電腦如何運作:從矽晶與 NAND 閘到 RAM 與應用程式

本頁標題、字幕、摘要與閱讀筆記為已儲存的 AI 譯文,可能有誤。時間戳與來源一致,可展開查看原始字幕,並對照原影片核對。

從頭開始探索電腦的運作方式,循著矽電晶體與通用 NAND 閘,一路了解暫存器、快取、以電容為基礎的 RAM 及現代應用程式。

Improbable Matter · 發佈於 · 43 分鐘 · 英文 · 授權:CC BY 4.0 · 來源:在 YouTube 觀看

逐字稿來源:創作者上傳至 YouTube 的字幕。AI 標點由 Vidleaf 加入;原始措辭與時間戳錨點均保留。原始逐字稿可在下方取得。

AI 生成摘要

這部影片刻意以簡化的方式,從最基礎開始概述電腦的運作原理,每個段落結尾都有摘要,並在說明欄中連結進一步的解說;講者指出,大概沒有人知道電腦如何被建造與運作的每一個細節,而貝多芬第 5 號交響曲的音效提示則警告,整部影片充滿了大幅度的過度簡化。影片從電池、開關與燈泡組成的電路,以及二進位的開/關電壓講起,將 MOSFET 呈現為電壓控制的開關(N 通道在高閘極電壓時導通,P 通道則相反),並將兩者配對成 CMOS 以產生反相輸出,據稱這構成了所有現代處理器的基礎。從摻雜矽進行的製造、微影、沉積與蝕刻,可在 P 摻雜晶片上建構數十億個電晶體。影片解說了布林代數與邏輯閘(NOT、OR、AND、XOR、NAND),並將 NAND 描述為通用閘;邏輯閘可實現二進位算術、位元位移、相等比較,並透過 D 閂鎖與暫存器實現儲存,由時脈同步。影片接著涵蓋記憶體階層(暫存器、快取、RAM、硬碟)、32 位元與 64 位元位址、將指令以二進位數字儲存的馮紐曼架構,以及可實現迴圈與條件分支、達成圖靈完備性的跳躍指令。影片也描述了高階語言與編譯器、共享函式庫、LED 顯示器與像素、Bresenham 畫線演算法、透過音效卡產生聲音,以及鍵盤與滑鼠輸入。

由逐字稿生成。請對照原始影片檢查連結的段落。

含時間戳的章節摘要

電腦如何運作:從基礎開始的概述

概述的範圍與方法

  • [0:10] 本影片旨在從最基礎開始,廣泛概述電腦的運作原理,內容略帶技術性,同時盡可能保持簡單。
  • [0:22] 每個段落結尾都有摘要,進一步的解說則連結於說明欄中。
  • [0:32] 講者指出,大概沒有人知道電腦如何被建造與運作的每一個細節。
  • [0:37] 貝多芬第 5 號交響曲的音效提示警告,內容正進行大幅度的過度簡化,而這項警告適用於整部影片。

電晶體作為電壓控制的開關

基本電路與二進位電壓狀態

  • [1:04] 簡單的電路可以由電池經開關與燈泡連接,再回到電池負極所組成。
  • [1:13] 現代電腦元件通常在正極與接地或負極之間提供 3.3 或 5 伏特。
  • [1:24] 電腦根據電壓是高或低,將資訊視為二進位——開或關、真或假、一或零。

按壓開關:常開與常閉

  • [1:35] 範例電路中的關鍵元件是按壓開關,它分為兩種形式。
  • [1:37] 按下即導通的開關在有人按下時,會接通其兩端之間的電路,使燈泡發亮。
  • [1:43] 按下即斷開的開關則恰好相反。

MOSFET 的端子與通道類型

  • [1:48] 圖中所示的電晶體符號具體來說是金屬氧化物半導體場效電晶體,也就是 MOSFET。
  • [1:56] MOSFET 的源極與汲極作用如同開關的兩端,但它還有第三個端子稱為閘極。
  • [2:05] N 通道類型在閘極施加高電壓時接通電路,如同按下即導通;而 P 通道類型在施加高電壓時斷開電路,如同按下即斷開。
  • [2:19] 因此電晶體的作用如同開關,只是改以施加電壓來取代人為按下。

CMOS 配對與反相電路

  • [2:24] 現代電腦將這些電晶體成對使用,稱為 CMOS,也就是互補式金屬氧化物半導體,一對電晶體的閘極端子總是連接在一起以接收相同的輸入。
  • [2:39] 當輸入為高電壓或 1 時,N 通道 MOSFET 會開啟高電壓與輸出之間的通路,而 P 通道則關閉通往接地端的路徑。
  • [2:52] 當輸入為低電壓或 0 時,P 通道會開啟通往接地端的路徑。
  • [2:58] 第一種配置看似毫無意義,因為輸出與輸入相同,但將兩個 MOSFET 對調後,高輸入會開啟通往低電壓或 0 的路徑,而 0 輸入則在輸出產生 1。
  • [3:17] 無論輸入什麼,輸出都會出現相反的結果,據說這構成了所有現代處理器的基礎。

CMOS 電晶體與晶片的製造方式

摻雜矽以形成 P 型與 N 型區域

  • [3:40] 矽是一種具有四個價電子的半導體,會與其他元素進行摻雜,也就是混合。
  • [3:47] 鎵有三個價電子,比矽少一個,使該區域更偏正電,也就是 P 型。
  • [3:54] 砷有五個價電子,比矽多一個,會增加負電荷,使該區域成為 N 型。
  • [4:03] 這些摻雜物的微量,通常約為每百萬個矽原子一個,會透過化學擴散或由粒子加速器射入的方式植入。

場效電晶體與 MOSFET 的結構

  • [4:14] 電晶體的製作方式是將一種摻雜物的區域夾在另一種摻雜物的兩個區域之間,其中 N 通道配置會被更仔細地檢視。
  • [4:22] N 通道配置本身並不導電,但施加的電場會形成導電通道,因此得名場效電晶體。
  • [4:31] 這可以想成電場將電子從有多餘電子的 N 摻雜區吸引到有不足的 P 摻雜區。
  • [4:44] 半導體堆疊附近的導電端子在高電壓下形成電場,使電晶體導通,但需要絕緣層來防止短路。
  • [4:53] 接觸處是金屬,絕緣層是氧化矽,而摻雜半導體完成整個堆疊,因此稱為 MOSFET。

微影:在晶粒上進行沉積與蝕刻

  • [5:03] 電腦需要數百萬個互相連接的 CMOS 對,它們既小又彼此靠近,讓電訊號能快速在它們之間傳遞。
  • [5:13] 處理器製造通常從一塊稱為晶粒的 P 摻雜矽固體開始,並在其上透過微影(意即「在石頭上書寫」)製作電晶體。
  • [5:29] 沉積會以化學方式在整個晶粒上建立一層厚度受控且預先決定的層,例如在 P 摻雜晶粒上塗佈 N 摻雜矽。
  • [5:43] 蝕刻會對晶粒頂部施加化學品或高溫電漿,以移除不需要的表面材料,並透過多個步驟確保材料被選擇性地移除。

光阻遮罩與選擇性蝕刻

  • [6:02] 一種稱為光阻的化學品被沉積到晶粒上,並讓光線穿過遮罩照射,以曝光預先決定的區域。
  • [6:12] 曝光區域的大小必須等於或大於所用光的波長,因此微影使用波長極短的紫外光。
  • [6:23] 一種蝕刻方法只移除光阻的曝光區域,打開間隙,接著另一種蝕刻方法則針對未曝光光阻以外的所有部分。
  • [6:39] 最後,剩餘的光阻被蝕刻掉,因此凡是遮罩上有間隙的地方,材料都已被移除。

同時建立並連接所有電晶體

  • [6:48] 沉積與蝕刻搭配使用,意味著晶粒上所有電晶體的 N 型摻雜物可以同時加入。
  • [6:58] 接著沉積並蝕刻氧化矽,最後沉積並蝕刻金屬,在晶粒頂部留下金屬通道。
  • [7:06] 這些金屬結構不僅構成電晶體的導電端子,也構成相當於導線的走線,用來依需求連接電晶體。
  • [7:16] 每個循環都會沉積光阻、透過遮罩曝光、打開間隙並蝕刻晶粒,藉由這種方法,數十億個電晶體被組裝並連接在一起。

電腦如何表示與處理資訊

透過電壓狀態進行二進位表示

  • [7:40] 電腦以壹與零處理資訊,對應於電路中的高電壓或低電壓。

電晶體作為互補開關

  • [7:48] 電晶體扮演電子開關的角色,在兩種電壓狀態之間切換。
  • [7:52] 電晶體總是以互補成對的方式出現,因此當配對中的一半導通時,另一半就會關閉。

用於建構電晶體電路的微影製程

  • [7:58] 微影製程是在一塊矽晶片上建構大量互連電晶體的製程。

布林邏輯閘與 CMOS 實作

布林代數與單輸入邏輯閘

  • [8:10] 布林代數是數理邏輯的一個分支,用來解釋電晶體網路為何有用,這裡以輕鬆的例子加以說明。
  • [8:28] 以餐廳為例,供應漢堡時 Alice 會很開心,而 Bob 則是在不供應漢堡時才開心。
  • [8:36] 邏輯閘接收二進位輸入(真或假、1 或 0)並傳回二進位輸出;這裡的輸入是是否供應漢堡,輸出則是某人是否開心。
  • [8:54] Alice 的邏輯閘只是輸出與輸入相同的值,以電子術語來說就只是一條連線;而 Bob 的邏輯閘則是 NOT 閘,圖中顯示其慣用符號。

多輸入邏輯閘:OR、AND 與 XOR

  • [9:14] 邏輯閘可以有多個輸入,而它們的名稱淺顯易懂。
  • [9:22] 以披薩和麵條為選項,只要至少供應其中一種或兩種都供應,Alice 就會開心,這就是 OR 閘。
  • [9:35] Bob 希望菜單上披薩和麵條兩者都有,這就是 AND 閘。
  • [9:42] 若供應披薩或麵條但不同時供應兩者,Charlie 就會開心,因為她認為不夠專精的餐廳做不好;這就是互斥或,也就是 XOR 閘。

互補邏輯閘與 NAND 行為

  • [9:53] 每個邏輯閘都有互補版本,基本上就是同一個邏輯閘後接一個 NOT 閘,並以輸出端帶圓圈的慣用符號表示。
  • [10:01] NAND 閘在所有輸入皆為 0 時輸出 1,否則輸出 1。
  • [10:12] 對電腦而言,這些以及其他所有可能的邏輯閘都必須透過 CMOS 配對來實作。

CMOS NAND 的建構與通用性

  • [10:18] 如圖所示,將兩對 CMOS 連接起來即可構成 NAND 閘,其中 N 通道與 P 通道電晶體仍成對運作。
  • [10:26] 唯有當兩對輸入都施加高電壓或 1 時,通往高電壓的路徑才會關閉、通往接地的路徑才會開啟,輸出 0,這正是 NAND 的行為。
  • [10:40] NAND 閘具有通用性:只要有足夠的 NAND 閘,任何其他閘都能由 NAND 閘構成,不過製作某些閘還有更有效率的方式。
  • [10:48] 本質上,這表示透過 CMOS 對來實作邏輯閘的問題已經解決。

超過兩個輸入的閘

  • [10:56] 閘可以有三個、四個,或任意所需數量的輸入。
  • [11:00] 例如,如圖所示將兩個 AND 閘連接起來,就單純成為一個三輸入 AND 閘,依此類推。

邏輯閘與 CMOS 實作

邏輯閘的定義

  • [11:10] 邏輯閘是概念性的數學物件,可根據二元的真或假敘述進行推論。
  • [11:18] 例如,若「今天是星期六或今天是星期日」為真,則「今天是週末」也為真。

以 CMOS 對進行實際實作

  • [11:24] 前一節的 CMOS 對可用來以電子二元邏輯實際實作這些閘。

電腦中的二進位算術與邏輯閘

以 2 為基底的表示法與位元軌

  • [11:39] 電腦處理數字,並使用邏輯閘網路來執行算術。
  • [11:46] 以 10 為基底時,使用數字 0–9 來表示 10 的冪次倍數(1、10、100、1000),而現代電腦使用以 2 為基底,以數字 0 與 1 表示 2 的冪次(1、2、4、8)。
  • [12:10] 在以 10 為基底時,953 等於 9×100 + 5×10 + 3×1;在二進位中,101 等於 1×4 + 0×2 + 1×1,也就是以 10 為基底時的 5,而任何以 10 為基底的數都能以 2 為基底表示,反之亦然。
  • [12:28] 二進位數 101 可由三條電氣軌表示,其電壓分別為開、關、開;每條軌承載一個位元,因此三條軌構成一個 3 位元數,4 位元與 8 位元數的定義亦類似。
  • [12:48] 4 位元數 0101 等於 3 位元數 101。

以 XOR 與 AND 相加 1 位元數

  • [12:56] 將 1 位元數 A 與 B 相加可得 0+0=0 與 0+1=1,而 1+1 則需要寫下 0 並進位 1,正如以 10 為基底時 5+5 的情形。
  • [13:18] 為了儲存 A 和 B 所有可能值運算後的結果,C 必須是 2 位元的數字。
  • [13:26] C 的右位元在 A 或 B 為 1 但非兩者皆為時為 1,這是 XOR 閘的行為;而左位元只有在 A 和 B 皆為 1 時才為 1,這是 AND 閘的行為。
  • [13:51] 相加超過 1 位元的數字較為複雜,因為需要三輸入閘來處理來自 A 的位元、來自 B 的位元以及進位位元;NAND 閘具有通用性,因此只要數量足夠,任何所需的閘網路都能以 NAND 閘實作。

透過位元移位進行二進位乘法

  • [14:20] 在 2 進位制中,乘以 10(二進位)或 2(10 進位制)只是在尾端加上一個 0,因此在二進位中乘以 10、100 或 1000 會將每個位元左移並在尾端補上一個 0,這稱為位元移位。
  • [14:44] 在二進位中乘以任意數字(例如 11),等同於分別乘以 10 和 1 再將兩者相加。
  • [14:53] 將 A 乘以任意 B 的二進位乘法運作方式為:從零開始,若 B 的最右位元為 1 則加上 A,接著將 A 位元左移一次,若 B 的下一個位元為 1 則將其加入總和,並對 B 的每個位元重複此流程。

使用 XNOR 與 AND 進行相等比較

  • [15:11] 電腦依賴各種比較,例如 A 是否等於 B 或 A 是否大於 B,例如將以二進位 A 儲存的目前時間(秒)與以 B 儲存的鬧鐘時間進行比較。
  • [15:30] 為了檢查相等性,電腦必須驗證 A 的每個位元都與 B 的對應位元相符;XOR 在其輸入不同時回傳 1,而其補集 XNOR 則在輸入相同時回傳 1。
  • [15:50] 對 A 和 B 的每一對位元套用多個 XNOR 閘,可指出這些位元是否相等;再對 XNOR 的輸出套用 AND 閘,即可回傳單一位元,若且唯若 A 等於 B 時該位元為 1。

其他運算與數字範圍

  • [16:08] 減法、除法及其他比較同樣能以閘網路實作;例如往另一個方向位元移位即可將數字除以二,而這些全都是使用蝕刻在晶片上的電晶體來實作。
  • [16:20] 在本影片中,「數字」指的是整數或完整數;分數可以儲存在稱為浮點數(floats)的二進位版科學記號中,其一般原理仍然適用,但其運作方式不在本影片涵蓋範圍內。
  • [16:36] 總結來說,電腦使用一連串二進位的開/關電訊號,以 2 進位制而非人們日常使用的 10 進位制來表示數字;邏輯閘網路可以對二進位數字進行加法、減法與比較,再透過幾個額外步驟即可實現乘法與除法。

電路除法與組合

除法電路

  • [16:53] 講者表示這些項目也應該被分割。

電路的組成

  • [17:00] 講者指出由特定元件組成的電路已經被檢視過了。

位元、位元組與有號數表示法

商用電腦中的固定位元寬度

  • [17:01] 電晶體可以為具有任意二進位位數或位元的數字實現加法及其他代數運算。
  • [17:09] 打造一台電腦需要精確選擇有多少條電氣線路與閘可用來表示一個數字,類似於一份印有固定數量方格供填寫年齡的表格。
  • [17:22] 對商用電腦而言,這個數字始終是 8 的倍數,而 8 個位元稱為一個位元組。
  • [17:27] 加法器電路以 1 位元組(8 位元)、2 位元組(16 位元)或 4 位元組(32 位元)的數字作為輸入。

符號位元慣例

  • [17:40] 無論一個數字由多少位元組組成,都可以保留其中一個位元來表示正號或負號。
  • [17:47] 當數字以此方式表示符號時,前導位元為 0 表示該數字為正,前導位元為 1 表示該數字為負。

儲存輸入與輸出

  • [17:55] 就像數學考試一樣,保存你的計算過程很重要。
  • [18:00] 無論電腦執行何種運算,輸入與輸出都必須以某種方式儲存。

閂鎖、暫存器與時脈同步

閂鎖電路與資料儲存

  • [18:06] 閂鎖是一種電路,一旦閂鎖後便會長時間維持在該狀態,因此可以儲存一個位元的資料;8 個閂鎖可儲存一個位元組。
  • [18:18] 由反及閘建構的 D 型閂鎖具有回饋至輸入的輸出,這種互連讓閘網路得以保持並保留其 0 或 1 的輸出值。
  • [18:44] 除非 E 或致能輸入為 1,否則不會有任何變化;當致能為 1 時,施加於 D 的任何位元都會傳遞至輸出。

加法電路中的暫存器

  • [18:55] 電腦執行加法等運算,致能閂鎖電路以儲存輸出,然後再次關閉致能,使結果在所需時間內保持不變。
  • [19:07] 個 8 閂鎖可組成一個暫存器,只要有必要,就能儲存一個位元組的資料。
  • [19:11] 加法電路會有暫存器在相加前儲存輸入,並有一個暫存器擷取結果。

時脈週期與同步

  • [19:23] 由於電訊號傳播和電晶體完成切換都需要一些雖小卻顯著的時間,所有電腦都有時脈,以確保在讀取答案之前,運算已完全執行完畢。
  • [19:36] 振盪晶體以非常特定的速率切換電壓的開與關;在影片拍攝當時,處理器典型的時脈速度為 2 GHz,也就是每秒 2 億次切換。
  • [19:55] 一次完整的加法至少需要 3 個時脈週期:數字在第一個週期載入輸入暫存器,加法電路在接下來運作,輸出暫存器則在第三個週期收到致能訊號並儲存結果。
  • [20:18] 大多數運算所需的時間遠超過一個、甚至三個時脈週期,例如需要數個加法步驟的乘法,但時脈仍會同步運算從開始到輸出就緒的過程。

算術邏輯的位置

  • [20:38] 所有所謂的算術邏輯,也就是執行加法和減法的邏輯閘陣列,在實體上都與處理器的其餘部分位於同一處。

電腦中的記憶體與儲存裝置

處理器暫存器與快取

  • [20:46] 晶片上的暫存器稱為處理器或中央處理器。
  • [20:51] 稱為快取的電路如今可儲存數十 KB 的資料,作為運算的中間階段。

RAM 電容儲存

  • [21:01] 需要一種更大且更長期的方式,來儲存遠多於暫存器和快取所能容納的位元組。
  • [21:08] 電腦有獨立的實體隨機存取記憶體(RAM)晶片,利用電容來儲存資料。
  • [21:16] 充電的電容具有高電壓,值為 1,而放電的電容相對於接地為 0 伏特,值為 0。
  • [21:24] 電荷會隨時間流失,但只要電腦有電,記憶體電路就會將其補足。

RAM 容量與定址

  • [21:31] 典型的筆記型電腦有數十 GB,也就是數十億位元組的 RAM。
  • [21:37] RAM 中的每個位元組都有自己的二進位位址,就像公寓大樓裡的郵箱一樣。
  • [21:44] RAM 中的電晶體構成閘極網路,會回傳儲存在處理器所傳來的位址上的位元組。

32 位元與 64 位元位址

  • [21:56] 直到不久之前,電腦的記憶體通常不超過 4 GB,因此記憶體位址是 32 位元的數字。
  • [22:04] 32 位元的數字最大只能到略超過 4 億,這在過去已經足夠。
  • [22:10] 當記憶體變大之後,位址實際上就不夠用了,因此現在記憶體位址改用 64 位元的數字。
  • [22:17] 這就是 32 位元與 64 位元程式的差別。

錢包範例與硬碟

  • [22:22] 在電玩遊戲中,遊戲會記錄錢包總額所存放的位址。
  • [22:31] 找到額外的八枚金幣時,遊戲會從該位址取出錢包數字,將它與 8 的二進位數字載入暫存器,相加後再把結果寫回。
  • [22:50] 電腦也會將大量資料存放在硬碟上,一放就是數十年。

電腦記憶體、儲存與處理的限制

硬碟的磁性儲存機制

  • [22:55] 硬碟表面佈滿微小的磁域,以朝上或朝下代表 1 或 0,即使沒有供電也能保留。
  • [23:04] 當磁碟旋轉時,感測頭可以被動讀取磁鐵的朝向作為位元,或主動翻轉磁域來寫入資料。
  • [23:15] 每個位元組也各有自己的位址,而筆記型電腦或個人電腦可以在硬碟上儲存數百 GB。
  • [23:23] 快閃記憶體與 DVD 等其他技術也存在,但這裡不涵蓋。

光速限制與記憶體的取捨

  • [23:29] 運算速度的根本限制是光速,光在一個典型時脈週期內大約前進 15 公分或半英尺,因此較大的電腦需要多個週期才能取得資料。
  • [23:43] 實際上,電晶體的切換速度遠比這慢,而硬碟也需要時間轉起來並掃描才能讀取資料。
  • [23:54] 快取很小但存取快速,記憶體能存放更多位元組但讀取較久,硬碟耗時最長但能幾乎永久地儲存大量資料。
  • [24:04] 因此,資料取得的速度與可儲存的容量之間存在取捨。

簡化的單一記憶體池模型

  • [24:09] 在影片接下來的部分,電腦會被解釋成彷彿只有一個能瞬間讀取的記憶體池,儘管實際上情況更為複雜。

時脈、位元組、暫存器與位址的摘要

  • [24:26] 時脈會同步各項操作,並確保在開始下一項操作之前,有足夠的時間完成目前這項操作。
  • [24:36] 電腦會以 8 個位元為一組來處理二進位數字,這一組稱為位元組。
  • [24:41] 暫存器會在運算過程中儲存數字,而在其他時候,數字則儲存在快取、隨機存取記憶體與硬碟中。
  • [24:49] 記憶體中的每個位元組都有自己唯一的位址,而位址本身也是一個二進位數字。

操作控制的需求

  • [25:02] 二進位運算與結果儲存都已實作,但電腦仍需要一種方式來判斷更進一步的事情。

指令與記憶體中的程式執行

指令編碼與馮紐曼架構

  • [25:09] 指令會指定要使用哪項操作以及何時使用,例如將兩個數字相加或複製一個數字。
  • [25:16] 在馮紐曼架構中,指令會以二進位數字的形式,與其他資料一起儲存在記憶體中。
  • [25:25] 例如,以 10 為基底的指令編號 16,或二進位中的某個指定位元組,可能代表將兩個 8 位元數字相加並儲存結果,而指令 17 則可能對 16 位元數字執行相同的操作。
  • [25:41] 當電腦遇到指令時,比較電路會開啟所需操作的邏輯電路,例如當位元組等於 16 時,就會開啟加法電路。

依位元組順序區分指令

  • [25:54] 像 16 這樣的位元組,可能是一道加法指令,也可能是電玩遊戲中的金幣數量,其區別取決於位元組在記憶體中出現的精確順序。
  • [26:05] 指令位元組會排在最前面,接著是它所需的記憶體位址,例如加法所需的兩個輸入位址與一個輸出位址。
  • [26:22] 在現代的 64 位元系統中,位址長度為 8 個位元組,因此一道加法指令總共是 25 個位元組(一個指令位元組加上 24 個位元組的位址),而一道複製指令總共則需要 17 個位元組。
  • [26:45] 每種指令類型總是佔用固定數量的位元組,而下一道指令會緊接在前一道指令之後。

循序執行與錯誤的後果

  • [26:54] 電腦遇到加法指令後會執行它,並知道下一道指令正好在 25 個位元組之後。
  • [27:01] 電腦一開始會將記憶體開頭的第 0 個位元組解讀為指令,之後便一道指令接著一道指令地執行。
  • [27:11] 如果發生錯誤,導致下一道指令不在它應該在的位置,電腦很可能就會當機。

執行加法與多位元組運算

  • [27:20] 進行加法時,第一個位址的數字會載入一個輸入暫存器,第二個位址的數字載入另一個輸入暫存器,接著啟用加法電路,經過足夠的時脈週期後,結果便從輸出暫存器寫入所需的記憶體位址。
  • [27:38] 若要相加兩個 16 位元的數字,電腦會取出數字 A 所在的位元組及其緊接的下一個位元組,並對數字 B 與結果 C 做同樣的處理。

程式範例:遊戲中的錢包金幣

  • [27:54] 電腦程式是一組依序執行的指令與位址集合,電腦會從一個指令走到下一個指令來執行它。
  • [28:13] 以遊戲為例,位元組 0 是一道加法指令,後面接著錢包總額的位址、剛找到的金幣數量,以及再次作為輸出用的錢包位址,藉此覆寫錢包總額。
  • [28:26] 接著電腦會將位元組 25 解讀為另一道加法指令,把販售物品所得的金幣加到錢包中,而下一個指令則位於再往後 25 個位元組處,即位元組 49。
  • [28:51] 由於錢包總額經常被使用,它很可能除了存放在隨機存取記憶體外,也會長期保存在處理器的快取中;而當玩家儲存遊戲時,它會被寫入硬碟上的存檔檔案。

跳躍與條件指令以達成圖靈完備

圖靈完備與線性指令的限制

  • [29:36] 目前看到的指令都是線性運作——複製一個數字、將兩個數字相加——因此程式總是產生相同的結果,最終走到記憶體末端便停止。
  • [29:48] 還需要再多幾道指令,才能讓電腦達到圖靈完備,也就是說它最終能執行任何可能的運算,例如載入網頁或渲染電子遊戲。
  • [30:01] 兩台圖靈完備電腦之間唯一的差別,就在於它們執行這些運算的速度有多快。

跳躍(前往)指令與迴圈

  • [30:07] 首先需要的指令是跳躍,有時稱為前往,其後會跟著一個記憶體位址。
  • [30:13] 程式不會直接前往記憶體中的下一個指令,而是跳至指定的記憶體位址,並執行該處的指令。
  • [30:24] 這讓程式得以執行迴圈;在電子遊戲的例子中,將金幣加到錢包總額、更新遊戲世界與圖形的指令之後,會接著一道跳回起始處的跳躍指令,讓遊戲能持續不斷地執行下去。

分支(條件)指令

  • [30:48] 另一種關鍵的指令類型是分支或條件指令:若符合特定條件,例如兩個數字相等,就跳至某個特定位址。
  • [31:00] 比較可以用邏輯閘實作,而加入這樣的邏輯會讓跳躍變成有條件的,允許根據目前為止的計算結果,或根據鍵盤、滑鼠等輸入,執行不同的指令集。
  • [31:17] 有了條件跳躍,電腦便具備圖靈完備性:只要有足夠的時間,它就能進行任何運算。

高階語言、編譯器與函式

高階語言隱藏低階細節

  • [31:41] 程式設計師通常使用高階程式語言,這類語言隱藏了指令與記憶體位址的細節,讓程式設計師的工作更輕鬆。
  • [31:53] 在 C 或 C++ 中,像是「若 A 大於 B 則將 Q 設為 A,否則設為 B」這樣的程式碼是人類可讀的,並讓 Q 保留 A 與 B 中較大者。

編譯器將程式碼轉譯為指令

  • [32:18] 編譯器會接收這段程式碼,並將其安排成正確的指令集,決定變數要存放在記憶體何處、要在何處跳躍,以及要使用 16 位元或 32 位元的數字。
  • [32:30] 這個過程稱為編譯程式碼。

函式與方法作為可重複使用的程式碼

  • [32:39] 函式或方法的概念是程式設計師最重要的簡化之一;平方根是數學上的例子,但函式也可以是任意用途,例如擷取電子郵件。
  • [32:55] 像平方根這類重複使用的函式,其指令只需在記憶體中儲存一次;被呼叫時,電腦會跳躍到函式所在位置、執行指令,然後帶著結果跳回。
  • [33:16] 函式與方法可以編譯成函式庫,讓不同時空的程式設計師共享有用的程式碼,就像 Windows 上的 DLL 檔案一樣。

程式中的跳躍與迴圈

  • [33:36] 電腦具有跳躍到某個記憶體位址並執行該處任何指令的指令,這讓程式內可以形成迴圈。
  • [33:45] 有些跳躍只在符合條件時才會發生。

電腦如何以 LED 與位元組驅動顯示器

跳躍機器的計算普遍性

  • [33:47] 任何具備此類跳躍能力的電腦,只要有足夠的記憶體與時間,就能執行任何運算。

由電壓控制的 LED 亮度

  • [34:10] 燈泡對顯示器來說太大,但發光二極體(LED)更小且更有效率。
  • [34:18] LED 兩端的電壓越高,它就越亮。
  • [34:20] 顯示器會根據來自電腦的資料,以電子方式改變每個 LED 兩端的電壓,進而改變其亮度。

以位元組為基礎的亮度刻度

  • [34:30] 由於電腦以位元組(即 8 個位元所組成的群組)來處理數字,因此亮度會合理地從 0 縮放到 255,也就是一個位元組所能表示的最大值。

紅、綠、藍三色 LED

  • [34:42] 人眼具有對紅光、綠光和藍光敏感的細胞。
  • [34:46] 使用紅、綠、藍三個 LED 組成的組合,並改變每一個的相對亮度,就能產生顏色。

電腦如何呈現像素、圖形與聲音

像素色彩混合與顯示記憶體大小

  • [34:51] 人眼可以透過混合顏色而被騙,以為看見了完整的光譜:紅與綠產生黃色和棕色,紅與藍產生粉紅色,而三色全亮產生白色,部分亮度則產生灰色。
  • [35:07] 由三種顏色組成的小點,無論採用何種技術,都稱為像素,而顯示器會將像素排列成列與行,形成 2 維網格。
  • [35:19] 最大解析度的範例影片寬為 1920 像素、高為 1080 像素,而在記憶體中填滿一個顯示器需要寬度乘以高度再乘以 3 個位元組。
  • [35:34] 最簡單地說,電腦必須每秒在記憶體中更新這麼多位元組 30 或 60 次,並將結果傳送到顯示器。

像素座標對應到記憶體位址

  • [35:49] 雖然顯示圖形看似純粹是藝術,但對電腦而言,它只不過是目前為止所涵蓋的閘極邏輯與指令。
  • [36:00] 每個像素都有一個 x 座標和一個 y 座標,對應其在記憶體中的位置。
  • [36:08] 在寬度恰好為一千像素的顯示器上,從左上角像素往前移動三千個位元組,就會超出第一列的末端,來到第二列最左邊的像素。

以迴圈與算術繪製桌面元素

  • [36:25] 許多演算法或一長串指令會透過算術以及操作記憶體中的位元組,來繪製視窗、按鈕、文字與電腦圖形。
  • [36:37] 迴圈會反覆複製定義顏色的三個位元組序列,將每個像素填上背景色;工作列則透過在具有特定 y 座標的列上迴圈來繪製;而開始按鈕則是以另一種顏色填滿的 x 與 y 座標矩形。
  • [36:59] 文字則是透過複製每個字母預先決定的像素圖案,或透過其他演算法,疊加在最上層。
  • [37:06] 顯示視窗需要它的寬度、高度以及左上角像素的位置;將左側的 x 值與寬度相加即可得到最右緣的像素,而若使用者移動或調整視窗大小,所有這些運算都會重新執行,視窗也會重新繪製。

由簡單指令繪製的線條、三角形與 3D 形狀

  • [37:29] 最簡單的電腦圖形是兩點之間的一條線,兩點各有自己的 (x,y) 座標,而 bresenham 演算法會迴圈繪製這樣的線,沿著學校數學中熟悉的直線 x-y 曲線前進。
  • [37:44] 三條線構成一個三角形,可以用紋理填滿、由模擬光源照亮,並與數千個其他三角形結合,形成 3D 形狀。
  • [37:53] 無論變得多複雜,歸根究柢都是編譯成函式的加法、乘法與跳躍指令。

聲音即揚聲器發出的壓力波

  • [38:00] 聲音是電腦另一種主要的輸出形式,由揚聲器與耳機產生、透過空氣傳遞的高壓與低壓波組成。

揚聲器與麥克風的運作

揚聲器透過線圈與磁鐵運作的機制

  • [38:09] 聲音是藉由前後移動振膜而產生。
  • [38:11] 附著於振膜的永久磁鐵位於固定的導電線圈內。
  • [38:15] 通過線圈的振盪電流會將磁鐵拉進拉出,使振膜振盪並產生聲音。

數位轉類比的聲音產生

  • [38:20] 電腦將時間劃分為短暫的步階,並透過傳送二進位數字給音效卡,指定每個步階的電流。
  • [38:29] 電子元件將數位二進位值轉換為適當的電流。

麥克風即反向的過程

  • [38:33] 麥克風的運作方式恰好相反。

電腦如何處理鍵盤與滑鼠輸入

記憶體中的輸入訊息

  • [38:41] 電腦處理輸入的方式通常相同:輸入會在記憶體中留下訊息,這些訊息是長度預設的位元組群組,用以識別裝置與輸入內容。
  • [38:53] 訊息佇列可能存放三個各為八位元組的訊息,解碼後分別為按下按鍵、滑鼠向右移動 30 像素,以及放開 Shift 鍵。

鍵盤矩陣與按鍵訊息

  • [39:10] 按鍵排列成格狀,按下某個按鍵時,會在水平列與垂直列之間形成電氣接觸,藉此辨識出是哪一個按鍵。
  • [39:19] 在鍵盤與主機板之間,宣告按鍵被按下或放開的訊息會被轉送到電腦記憶體,讓電腦能據此採取動作。

遊戲動作的條件跳躍

  • [39:29] 以電玩遊戲為例,「B」鈕代表購買、「S」鈕代表販售,在 Windows 中分別對應到十進位數字 66 與 83。
  • [39:42] 遊戲會依據最後一次按鍵進行條件跳躍:值為 66 時跳至購買指令,值為 83 時跳至販售指令。

滑鼠追蹤與按鈕點擊測試

  • [39:56] 滑鼠會沿著平面追蹤左右與上下移動,以及哪些按鈕被按下。
  • [40:05] 通常作業系統會藉由加減從滑鼠接收到的位置變化量,來追蹤滑鼠的 x 與 y 像素座標。
  • [40:17] 當使用中視窗內的左鍵被按下時,程式會先檢查游標的 x 座標是否落在按鈕的左緣與右緣之間,再檢查 y 座標是否垂直落在按鈕範圍內;若兩者皆成立,就判定該按鈕被按下。
  • [40:43] 當畫面上有 10 個按鈕時,程式會逐一檢查每個按鈕是否被按下。

從像素與聲音到分層運算

顯示器與聲音在記憶體中的表示方式

  • [40:53] 電腦顯示器是由像素構成的格狀陣列,每個像素由紅、綠、藍三種光組成,其亮度分別由電腦記憶體中對應的位元組設定。
  • [41:05] 因此每個像素都能呈現任意顏色,而一群像素合在一起便構成影像。
  • [41:10] 聲音的錄製方式是測量聲波的運動並將其存成記憶體中的二進位數字,播放時則採用相反的過程。

輸入裝置與運算的分層抽象

  • [41:18] 外部裝置會將訊息傳送給電腦並放入記憶體,讓電腦能辨識按鍵按下或滑鼠移動等輸入,進而據此採取動作。
  • [41:31] 講者覺得很神奇:在矽中摻入雜質元素,並執行相對簡單的邏輯運算,竟能讓人們觀看與製作影片、處理資訊,甚至置身虛擬世界。
  • [41:44] 運算將較簡單的概念層層堆疊成更複雜的概念:CMOS 對構成 NAND 閘,NAND 閘可組成任何邏輯閘,邏輯閘組成指令,指令將演算法化為函式,函式再建構出任意複雜度的程式。

電腦是僵化、不直覺的執行者

  • [42:03] 這也意味著電腦非常笨:它們沒有直覺,必須嚴格遵循程式,而程式中的每一種可能情況都必須詳細寫明。

免裝外掛讀取附時間戳記的現有字幕,匯出 TXT、SRT 或 Markdown。免費 YouTube 字幕擷取工具。

前 3 段雙語對照預覽 · 免消耗額度

以下展示前 3 段已儲存繁體中文譯文與英文原版字幕的逐段對照(零額度消耗):

[0:00]

[音樂] 在這部影片中,我將嘗試從最基礎的層面,廣泛概述電腦如何運作。影片提供字幕。內容會稍微偏技術性,但我會盡量讓說明保持簡單。每個段落結束時都會有摘要,說明欄中也有進一步解說的連結。希望這份概述能讓你學到新東西,或讓你對事物有新的看法。

[Music] In this video I will try to give a broad overview of how a computer works from the ground up. Subtitles are available. It will get a little bit technical, but I will try to keep things as simple as possible. There will be a summary at the end of every section and links to further explanations in the description. Hopefully this overview will teach you something new or give you a new perspective on things.

[0:32]

世界上大概沒有人知道電腦如何建造與運作的每一個細節。這就是為什麼當你聽到這個聲音 [貝多芬第5號交響曲] 時,那是個警告,表示我正做出大幅簡化,我相信你會在留言中指出來。[貝多芬第5號交響曲] 那是整部影片的警告。[音樂] 現代電腦的核心是電路。

Probably nobody in the world knows every single nuance of how a computer is built and operates. This is why when you hear this sound [Beethoven's 5th symphony] that is a warning that I am making massive oversimplifications which I'm sure you'll point out to me in the comments. [Beethoven's 5th symphony] That was the warning for the entire video. [Music] Modern computers at their core are electrical circuits.

[1:04]

一個非常簡單的電路可能由電池連接到開關,再連接到燈泡,然後回到電池的負極端子。現代電腦元件通常在正極端子與接地或負極端子之間提供 3.3 或 5 伏特的電壓,如果你喜歡這樣想的話。電腦將資訊視為二進位:開或關、真或假、一或零,取決於電壓是高或低。我們這裡電路中的重要元件是按壓開關,它有兩種形式。

A very simple circuit might consist of a battery connected to a switch, then a light bulb, then back to the negative terminal of the battery. Modern computer components typically supply a voltage of 3.3 or 5 volts between the positive terminal and the ground or negative terminal if you like to think of it that way. Computers treat information as binary: on or off, true or false, one or zero, based on if the voltage is high or low. The important component in our circuit here is the push switch which. comes in two flavors.

[0:00] [音樂] 在這部影片中,我將嘗試從最基礎的層面,廣泛概述電腦如何運作。影片提供字幕。內容會稍微偏技術性,但我會盡量讓說明保持簡單。每個段落結束時都會有摘要,說明欄中也有進一步解說的連結。希望這份概述能讓你學到新東西,或讓你對事物有新的看法。

[0:32] 世界上大概沒有人知道電腦如何建造與運作的每一個細節。這就是為什麼當你聽到這個聲音 [貝多芬第5號交響曲] 時,那是個警告,表示我正做出大幅簡化,我相信你會在留言中指出來。[貝多芬第5號交響曲] 那是整部影片的警告。[音樂] 現代電腦的核心是電路。

[1:04] 一個非常簡單的電路可能由電池連接到開關,再連接到燈泡,然後回到電池的負極端子。現代電腦元件通常在正極端子與接地或負極端子之間提供 3.3 或 5 伏特的電壓,如果你喜歡這樣想的話。電腦將資訊視為二進位:開或關、真或假、一或零,取決於電壓是高或低。我們這裡電路中的重要元件是按壓開關,它有兩種形式。

[1:37] 按下導通,也就是當人按下時,兩個端子之間的電路被接通,燈泡便亮起。按下斷開開關則恰好相反。這是電晶體的符號:更精確地說,它是金屬氧化物半導體場效電晶體,或稱 MOSFET。它的源極和汲極就像開關的兩個端子,但它有第三個端子稱為閘極。就像開關一樣,它有兩種型態:N 通道型在閘極施加高電壓時接通電路,就像按下導通。P 通道型在施加高電壓時斷開電路,就像按下斷開。

[2:19] 所以,電晶體的作用就像開關,只是不是由人按下,而是施加電壓。在現代電腦中,這類電晶體成對使用,稱為 CMOS,或互補式金屬氧化物半導體。給定一對的閘極端子總是連接在一起,因此它們接收相同的輸入。這裡有個簡單的例子:如果我們對輸入施加高電壓或 1,N 通道 MOSFET 會開啟高電壓與輸出之間的路徑。P 通道則關閉通往接地的路徑。

[2:52] 如果在輸入端施加低電壓或 0,P 通道就會開啟一條通往接地的路徑。好,所以這個電路看起來沒什麼用,輸出端得到的結果和輸入端一樣,但讓我們把這兩個 MOSFET 對調。現在,當我們在輸入端施加高電壓或 1 時,N 型會開啟一條通往低電壓或 0 的路徑。當我們在輸入端施加 0 時,輸出端會得到 1。無論我們在輸入端輸入什麼,輸出端都會得到相反的結果。之後我們會再回來討論這如何構成所有現代處理器的基礎。要理解,正是像這樣龐大的 CMOS 對網路,讓電腦得以思考。

[3:33] [貝多芬第 5 號交響曲] 現在,讓我們大致了解電晶體網路是如何製成的。矽是一種半導體,具有四個價電子,會摻雜或混合其他元素。例如鎵有三個價電子——比矽少一個,因此較偏正電或 P 型。而像砷這樣的元素有五個價電子——比矽多一個,因此帶負電,所以是 N 型。

[4:03] 這些元素的微量,通常大約每百萬個矽原子對一個,會透過化學擴散或從粒子加速器射出後植入。電晶體的製作方式,是將其中一種摻雜物的區域夾在另外兩種之間。讓我們更仔細看看 N 通道的組態。它本身不導電,但當對它施加電場時,就會形成導電通道,因此得名:場效電晶體。簡單來說,可以把它想成電場將電子從有多餘電子的 N 摻雜區吸引到有電子不足的 P 摻雜區。

[4:40] 在半導體堆疊附近,我們有一個導電的電氣端子。當它處於高電壓時,會形成電場,電晶體就會導通,但為了防止短路,我們需要在它們之間加入一層絕緣層。接點是金屬,絕緣體是氧化矽,最後我們有摻雜半導體,因此稱為 MOSFET。對電腦而言,我們需要數百萬個互相連接的 CMOS 對,而且它們必須又小又靠近,這樣電訊號才能快速在它們之間傳遞。

[5:13] 製作處理器通常從一塊稱為晶粒的固態 P 型摻雜矽開始。接著透過稱為微影的製程,在晶粒上方製作電晶體:「在石頭上寫字」。微影主要有兩種技術。第一種是沉積:以化學方式在整個晶粒上建立一層厚度受控、預先決定的材料。這意味著,例如 N 型摻雜矽可以塗佈在 P 型摻雜晶粒的整個頂部。接著再以第二種稱為蝕刻的技術移除不需要的表面材料。做法是在晶粒頂部施加化學品或高溫電漿。過程中會採取數個步驟,確保蝕刻只針對特定區域,從而選擇性地移除材料。

[6:02] 一種稱為光阻的化學品會沉積到晶粒上。光線穿過光罩照射,使光阻的預定區域曝光。曝光區域的尺寸必須等於或大於所用光線的波長,因此微影採用波長極短的紫外光。會使用特殊的蝕刻方法,只移除已曝光的光阻區域,而不影響其他部分。這會在光阻中開出間隙,接著再使用另一種蝕刻方法,針對未曝光光阻以外的所有材料進行蝕刻。

[6:39] 最後,剩餘的光阻會被蝕刻掉。結果是,只要光罩上有任何間隙,該處的材料就會被移除。沉積與蝕刻這兩種技術搭配使用,意味著晶粒上所有電晶體的 N 型摻雜物可以同時添加。接著,沉積並蝕刻氧化矽,最後再沉積金屬並蝕刻掉,在晶粒頂部留下金屬通道。這些金屬特徵不僅構成電晶體的導電端子,也形成相當於電路中導線的走線,依需求連接各電晶體。

[7:16] 每一次,都會沉積光阻、透過光罩曝光、開出間隙並蝕刻出晶粒。最後,數十億個電晶體便以這種方式組裝並連接在一起。可能有三對 CMOS 搭配三種不同輸入,然後再接到別處的四對不同 CMOS,如此這般不斷下去。[貝多芬第 5 號交響曲] 總結來說:電腦以壹與零來處理資訊,也就是電路中的電壓處於高準位或低準位。

[7:48] 電晶體扮演電子開關的角色,在這兩種狀態之間切換。電晶體總是以互補成對的方式出現,因此當這對開關中的一半導通時,另一半就會關閉。微影製程是將大量互相連接的電晶體建構在一塊矽晶片上的製程。[音樂] 為了理解電晶體網路為何有用,我們需要談談數學邏輯中一個稱為布林代數的分支。

[8:18] 與其深入探討完整而繁複的數學細節,我只會用一些不太嚴肅的例子來說明基本概念。假設有幾位朋友正在挑選餐廳。愛麗絲在餐廳供應漢堡時會感到滿意。鮑勃在餐廳不供應漢堡時會感到滿意。在這個情況下,我們可以把評估餐廳想成一種稱為閘的東西:一個接收二元輸入真或假、1 或 0,並回傳二元輸出的物件。在這個例子中,輸入是餐廳是否有供應漢堡,輸出則是某人是否對這家餐廳感到滿意。

[8:54] 對愛麗絲而言,這個閘很簡單:輸出與輸入相同。以電子術語來說,這個電路就只是一條連線。對鮑勃而言,這個閘就是所謂的 NOT 閘。這是通常用來表示它的符號。在上一節中,我們已經看到這對 CMOS 如何作為 NOT 閘運作。讓我們考慮一個更有趣的例子,也就是邏輯閘可以有多個輸入的情況。這類閘的名字相當直白,我們接下來就會看到。現在朋友們正在挑選可能供應披薩、麵條、兩者都有或兩者都沒有的餐廳。愛麗絲在餐廳至少供應這兩種選項之一或兩者都供應時會感到滿意。這個概念由所謂的 OR 閘表示。這是它的符號。

[9:35] 鮑勃想要有選擇,所以他希望菜單上同時有披薩和麵條,因此是 AND 閘。查理喜歡餐廳供應披薩或麵條,但不能兩者都有:假設她認為如果餐廳不夠專精,就做不好。對應的閘稱為互斥或,簡稱 XOR。這些閘每一個也都有互補的閘:基本上就是同一個閘後面再接一個 NOT 閘。例如,NAND 閘在所有輸入皆為 1 時輸出 0,否則輸出 1。

[10:08] 這些互補閘會以一般符號在輸出端加上一個圓圈來表示。對電腦而言,這些以及所有其他可能的閘都必須透過 CMOS 對來實作。將兩對如下連接,即可建立一個 NAND 閘。N 通道與 P 通道電晶體仍然成對運作。唯有當高電壓或 1 同時施加於兩對輸入時,通往高電壓的路徑才會關閉,而通往接地的路徑則會開啟,因此輸出一個 0。這正是 NAND 閘的行為。

[10:40] NAND 閘之所以重要,是因為它被稱為通用閘:這表示只要有足夠多的 NAND 閘,任何其他閘都可以由 NAND 閘建構出來。當然,製作某些閘有更有效率的方式,但本質上這表示透過 CMOS 對實作邏輯閘的問題已經解決。也有具備三個、四個或任意所需輸入數的閘。例如,取兩個 AND 閘如圖所示連接,它們就簡單地變成一個三輸入 AND 閘,依此類推。[貝多芬第 5 號交響曲] 總結來說:邏輯閘是概念性的數學物件,讓我們能根據二元的真或假陳述進行推論。

[11:18] 例如,如果「今天是星期六」或「今天是星期日」為真,那麼「今天是週末」也為真。我們現在已經看到上一節的 CMOS 對如何用來以電子二元邏輯實際實作這些閘。[音樂] 現在我們將看到電腦如何處理數字,並使用邏輯閘網路來進行算術運算。我們都習慣於 10 進位數字,意思是從 0 到 9 共有 10 個數字。

[11:51] 這些數字代表 10 的冪次倍數,即 1、10、100、1000 等等。現代電腦使用 2 進位數字,只有 2 個數字:0 和 1。二進位數字是 2 的冪次倍數,即:1、2、4、8 等等。例如,在 10 進位中,數字 953 是 9 乘以 100 加上 5 乘以 10 加上 3 乘以 1。在二進位中,數字 1 0 1 是 1 乘以 4 加上 0 乘以 2 加上 1 乘以 1,也就是 10 進位中的 5。

[12:25] 10 進位中的任何數字都可以用 2 進位的數字表示,反之亦然。要表示數字 1 0 1 3,依序的電氣軌道其電壓會是開、關、開。這些軌道每一條都承載一個二進位數字或位元。有三條電氣軌道時,這稱為 3 位元數字。同樣地,也有 4 位元數字、8 位元數字等等。當然,4 位元數字 0 1 0 1 等同於 3 位元數字 1 0 1。

[12:56] 讓我們考慮將一對 1 位元的數字 A 和 B 相加得到 C。0 + 0 = 0 0 + 1 = 1。當我們嘗試在二進位中相加 1 + 1 時會發生什麼事?就跟我們在一般的 10 進位中相加 5 + 5 時一樣。沒有足夠大的數字,所以我們在該欄位放一個 0,並將 1 進位到下一個欄位。因此,為了保證能儲存 A 和 B 任何可能值的結果,結果 C 必須是 2 位元的數字。最右邊的位元在 A 或 B 為 1 時為 1,但不會同時兩者皆是:如果你還記得的話,這就是 XOR 閘的行為。上一節的 Charlie。

[13:35] 左邊的位元只有在 A 和 B 都是 1 時才是 1,因此是 AND 閘。所以 C 是透過像這樣將 A 和 B 連接到閘來得到的。當相加比 1 位元更大的數字 A 和 B 時,由於現在有三個輸入閘,事情會變得更複雜,因為現在有來自 A 的一個位元、來自 B 的一個位元以及一個進位位元。如我在上一節所說:NAND 閘是通用的,意思是任何所需的閘網路都可以用足夠多的 NAND 閘來實現。

[14:17] [貝多芬第 5 號交響曲] 在 10 進位中,乘以 10 很簡單:只要在你的數字末尾加上一個 0。在 2 進位中,乘以 10(二進位)或 10 進位中的 2 也是如此。所以,在二進位中乘以數字 10、100、1000 只是將每個位元向左移並在末尾放一個 0,這被難以想像地稱為位元移位。那麼乘以任意數字呢,比如說二進位中的 11?

[14:48] 這等同於乘以 10、乘以 1,然後將兩者相加。所以,將 A 乘以任意數字 B 的二進位乘法運作如下:從零開始。如果 B 的最右位元是 1,加上 A。將 A 位元移位一次,如果 B 的下一個位元是 1,將此加到總和中。對 B 的每個位元繼續進行。電腦也依賴於對數字進行比較,例如:A 等於 B 嗎?A 大於 B 嗎?

[15:18] 諸如此類。電腦可能會將當前時間以秒為單位儲存為二進位數字 A,每秒更新一次,並將設置鬧鐘的時間儲存為 B。當 A 等於 B 時,鬧鐘就會響起。為此,電腦必須檢查數字 A 的每個位元是否與 B 的對應位元相同。所以,當一個輸入是 1 而另一個輸入是 0 時,也就是當其輸入不同時,XOR 閘會返回 1。XNOR 閘是 XOR 的補數,意味著它返回與 XOR 相反的結果,因此當其輸入相同時會返回 1。將一堆 XNOR 閘應用於 A 和 B 的每一對位元,會返回這些位元是否相等。

[15:57] 當 AND 閘的所有輸入皆為 1 時,它會回傳 1。因此,將它套用到 XNOR 閘上,就會回傳單一位元;若且唯若 A 等於 B 時,該位元為 1。也有其他方式可以實作減法、除法及其他比較,全都使用閘網路。例如,往另一個方向進行位元移位,就能將一個數字除以二。這些全都是使用蝕刻在晶片上的電晶體來實作。附帶一提:當我在這部影片中提到「數字」這個詞時,我指的是整數或完整數。還有一種方式可以儲存分數,也就是以二進位版本的科學記號來表示,稱為浮點數。

[16:32] 我不會深入說明它們的運作方式,但這裡概述的一般原理仍然適用。總結來說:電腦使用一連串二進位的開關電氣訊號,來表示以 2 為基底的數字,而不是人們日常使用的 10 基底。邏輯閘網路可以用來加、減及比較二進位數字,再多幾個步驟,也能乘除它們。[音樂] 我們已經看到,由電晶體組成的電路如何用來對任意二進位位數或位元實作加法及其他代數運算。

[17:09] 如果你想打造一部電腦,我們就必須確切選擇要用多少條電氣軌道和閘來表示一個數字。這就像你在填寫表格時,表格上實際印有固定數量的欄位,讓你填入年齡。對商用電腦而言,這始終是 8 的倍數。8 位元稱為一個位元組。電腦會有加法器電路等等,其類型如我在上一節所述,可接受 1 位元組或 8 位元的數字、2 位元組或 16 位元的數字、4 位元組或 32 位元的數字作為輸入。

[17:40] 無論一個數字由多少位元組組成,都可以保留其中一個位元來表示正負號。當一個數字以這種方式帶有正負號時,若首位元為 0,則該數字為正數。若首位元為 1,則該數字為負數。任何考過數學考試的人都知道:儲存你的計算過程很重要。無論電腦執行哪種運算,輸入和輸出都必須以某種方式儲存起來。為此,會使用一種稱為閂鎖的電路。

[18:10] 一旦它鎖定,就會長時間維持該狀態,因此可用來儲存一個位元的資料。8 個閂鎖可儲存一個位元組,依此類推。這是所謂 D 型閂鎖(由 NAND 閘組成)的範例。你可能會注意到這看起來相當奇怪:先前我們看到的閘都是嚴格依序排列,一個閘的輸出直接接到另一個閘。現在,一個閘的輸出接到另一個閘的輸入,但輸出又繞回來。正是這種互連方式,讓這組閘網路能夠保持並保留其輸出值 0 或 1。

[18:44] 除非 E 或啟用輸入為 1,否則不會有任何變化。當啟用為 1 時,施加到 D 的任何位元都會傳播到輸出。電腦會執行一項運算(例如加法),啟用一堆閂鎖電路來儲存輸出,然後再關閉啟用。結果便會依需要一直保持在那裡。8 個這樣的閂鎖可組成暫存器,依需要儲存一個位元組的資料。我們在上一節看到的那種加法電路,會有這類暫存器在加法進行前儲存輸入,並有一個暫存器擷取加法的結果。

[19:23] 電氣訊號傳播、電晶體完成切換等,都需要一些雖小但顯著的時間。為了保證在嘗試讀出答案之前運算已完全執行完畢,所有電腦都有時脈。簡單來說,振盪晶體以非常特定的速率將電壓開啟、關閉、開啟、關閉。在本影片製作時,電腦處理器的典型時脈速度為 2 GHz,意即時脈每秒切換 2 十億次。

[19:55] 規律的切換讓事件得以同步。要完整執行一次加法運算,至少需要 3 個時脈週期。在第一個時脈週期,數字會載入輸入暫存器,接著加法電路可在下一個時脈週期運作。最後在第三個時脈週期,輸出暫存器收到啟用訊號並儲存結果。實際上,大多數運算所需的時間遠比一個、甚至三個時脈週期長得多。我們在上一節已看到,乘法需要好幾個加法步驟才能完成。

[20:30] 儘管如此,無論它需要一個還是上百個週期,時脈仍然被用來同步操作開始的時機,直到輸出準備就緒。所有所謂的算術邏輯,也就是:所有執行加減法的邏輯閘陣列,在實體上都與暫存器一起位於稱為處理器或中央處理單元的晶片上。那裡還有稱為快取的電路,如今可以儲存數十千位元組的資料,作為計算的中間階段,例如。

[21:01] 需要一種更大且更長期的方式來儲存比暫存器和快取更多的位元組資料。電腦有獨立的實體隨機存取記憶體或 RAM 晶片,利用電容器來儲存這些資料。如果電容器充電,它就有高電壓,因此值為 1。如果它放電,相對於地它就有 0 伏特,因此是 0。電荷會隨時間洩漏或流失,但只要電腦有供電,記憶體電路就會將其補充。

[21:31] 典型的筆記型電腦有數十 GB 或數十億位元組的 RAM。儲存在 RAM 中的每個位元組都有自己的位址,就像公寓大樓裡的郵箱一樣。位址本身是一個二進位數字。RAM 有許多電晶體組成閘極網路,確保當它從處理器接收到二進位格式的位址時,就會回傳儲存在該位址的位元組。直到最近,電腦通常不會有超過 4 GB 的 RAM。每個位元組都需要一個唯一的位址,所以記憶體位址是 32 位元的數字。這樣的 32 位元數字最高可達略超過 4 億,所以以前這樣就夠了。

[22:10] 當 RAM 變大時,實際上就沒有足夠的位址了,所以現在記憶體位址使用 64 位元的數字。這就是 32 位元和 64 位元程式的差異。所以,假設你在玩一個電子遊戲,錢包裡有一些硬幣。電子遊戲會追蹤錢包總額儲存的位址。如果你又找到了八枚硬幣,遊戲會從該位址取得目前錢包裡的數字,載入到暫存器,將對應於以 10 為基底的 8 的二進位數字載入到另一個暫存器,將它們相加,並將結果放回錢包總額的位址。

[22:47] 我們會在下一節更詳細地探討這一點。電腦也能將巨量的資料儲存在硬碟上長達數十年,即使沒有電力也一樣。硬碟表面佈滿微小的磁域,這些磁域可以朝上或朝下,分別對應到 1 或 0。當磁碟旋轉時,感測頭可以被動讀取磁鐵的方位作為位元,或主動翻轉磁域的方位,將資料寫入磁碟。每個位元組也都有各自獨立的位址。

[23:19] 筆記型電腦或個人電腦可以在硬碟上儲存數百 GB 的資料。還有其他技術,例如快閃記憶體、DVD 等等,我在此不深入探討。電腦執行操作速度的根本極限是光速,光在一個典型的電腦時脈週期內大約能前進 15 公分或半英尺,因此對於比這更大的電腦,要取得任何資料都需要多個週期。實際上,電晶體的切換速度遠比這慢,而硬碟則需要時間轉起來並掃描以讀取資料。就電腦記憶體的類型而言:快取很小,但存取快速;RAM 儲存更多位元組的資料,但讀取時間較長;硬碟讀取時間最長,但能儲存大量資料,幾乎可永久保存;因此,資料能被取回的速度與能儲存的容量之間存在取捨。從現在起,在這部影片中,我會把電腦當作只有單一記憶體池、可以瞬間讀取來解釋事情。實際上情況更為複雜,

[24:20] 正如我剛才所描述的。[貝多芬第 5 號交響曲]。總結來說:電腦有一個時脈,用來同步正在執行的操作,並確保在開始下一個操作之前,有足夠的時間完成目前的操作。電腦以 8 個位元為一組來處理二進位數字,這一組稱為一個位元組。暫存器用於在運算期間儲存數字,其他時候數字則儲存在快取、隨機存取記憶體和硬碟中。記憶體中的每個位元組都有自己唯一的位址,而位址本身也是一個二進位數字。

[24:56] [音樂] 我們有辦法實作二進位運算並儲存結果,但電腦需要一種方式來決定要使用哪個操作以及何時使用。這稱為指令。指令例如:將兩個數字相加,以及複製這個數字。電腦採用所謂的馮紐曼架構,這表示指令和其他資料一樣,以二進位數字的形式儲存在記憶體中。例如,指令編號 16 和基底 10,或這個二進位位元組,可能代表:將兩個 8 位元的數字相加並儲存結果。

[25:34] 指令編號 17 可能代表相同意義,但用於 16 位元數字,依此類推。當電腦遇到指令時,會使用我們在前一節看過的比較電路,來開啟必要運算的邏輯電路。如果該位元組等於 16,就開啟加法電路。電腦如何知道某個位元組是不是指令?16 可能是加法指令,也可能是電玩遊戲中的金幣數量。這一切都與位元組在記憶體中出現的精確順序有關。首先出現的是指令位元組。

[26:08] 如果指令是加法,電腦會需要兩個被加數的記憶體位址,以及儲存結果的記憶體位址。請記住,記憶體就像一棟大型公寓大樓的信箱,每個位元組都有唯一的位址。在現代 64 位元系統中,位址長度為 8 個位元組。因此,整個加法指令包含一個指令位元組和 24 個位元組的位址,總共 25 個位元組。另一方面,如果指令是複製數字,它只需要後面跟著來源位址和目的位址,也就是總共佔 17 個位元組。

[26:45] 訣竅在於,每種類型的指令總是佔用固定數量的位元組,而且關鍵是,下一個指令緊接在前一個指令之後。因此,當電腦遇到加法指令時,不僅會執行它,還知道下一個指令正好在 25 個位元組之後。一開始,電腦會將編號 0 的位元組(也就是記憶體最開頭的位元組)解讀為指令,之後便一個指令接一個指令地進行。相反地,如果發生錯誤,下一個指令不在它應該在的位置,那就倒楣了!電腦大概會當機。電腦實際上如何執行加法指令?

[27:22] 第一個位址的數字會被載入到一個輸入暫存器,第二個位址的數字則載入到另一個輸入暫存器。加法電路被啟用並執行加法。接著,經過足夠的時脈週期後,結果便可從輸出暫存器寫回記憶體中所需的位址。如果指令是要把兩個 16 位元的數字相加呢?電腦會直接前往數字 A 的位址,而且不只取出一個位元組,而是取出該位元組以及緊接在後的下一個位元組。數字 B 也是如此,結果 C 也一樣。[貝多芬第 5 號交響曲] 電腦程式是一組依序執行的指令(與位址)集合。電腦會從一個指令走到下一個指令,讓程式得以運行。讓我們看一個具體例子:遊戲中的某個部分(畢竟遊戲本身也不過就是個程式),玩家有一個裝著金幣的錢包。

[28:10] 記憶體的內容可能長這樣。位元組編號 0 是執行加法的指令。接著是兩個位址:錢包總額的位址,以及玩家剛找到的金幣數量。第三個位址又是錢包。加法完成後,錢包總額會被覆寫。無論如何,電腦現在會把記憶體中編號 25 的位元組解讀為指令。這是另一個加法指令。輸入是錢包的位址,以及玩家販售物品所賺得金幣數量的位址。

[28:40] 錢包位址同樣是輸出。下一個指令在位元組 49,也就是再往前 25 個位元組的位置。程式會繼續執行下去,還需要數百萬個其他指令才能讓遊戲運作。在這個例子中,錢包總額被頻繁使用,因此它很可能會長期存放在處理器的快取以及 RAM 中。當玩家儲存遊戲時,它會被寫入電腦硬碟上的存檔檔案。總結來說:電腦會把記憶體中的某些位元組解讀為指令,以決定要執行哪項運算。

[29:15] 電腦程式儲存在記憶體中。它們就只是一長串的指令、記憶體位址與數字。電腦會像時鐘一樣精準地從一個指令走到下一個指令,並總是依據目前指令的類型得知下一個指令距離多遠。[音樂] 到目前為止,我們看過的指令都是以線性方式運作:複製一個數字、把兩個數字相加等等。程式總是會產生相同的結果,最終它會走到記憶體的盡頭然後停止。

[29:48] 我們還需要再幾道指令,才能讓電腦達到所謂的圖靈完備。圖靈完備意味著它最終能執行任何可能的運算:載入網頁、渲染電玩遊戲等等。兩台圖靈完備電腦之間唯一的差別,就是它們執行這些運算的速度有多快。電腦需要的第一種指令叫做跳躍,有時也稱為前往。接在指令位元組之後的是一個記憶體位址。顧名思義,程式不會直接前往記憶體中的下一道指令,而是跳躍到指定的記憶體位址,並在該處執行指令。

[30:24] 這意味著程式現在可以進行迴圈。在我們的電玩遊戲範例中,我們已經看到指令如何用來把金幣數量加到錢包中保存的總數。這些指令以及更新遊戲世界、渲染圖形等所有其他必要指令之後,都會接上一道跳躍指令,跳回起點。這樣一來,遊戲就能無限持續執行。電腦需要的另一種關鍵指令是分支或條件指令。如果符合某個給定條件,例如兩個數字相等,就跳躍到特定位址。

[31:00] 我們已經看過如何用邏輯閘實作比較。加入這樣的邏輯,就能讓跳躍變成條件式,根據目前為止執行的計算,或根據鍵盤、滑鼠等電腦輸入,執行不同的指令集。有了條件跳躍,電腦就是圖靈完備的:只要時間足夠,它就能執行任何計算。到這裡值得提一下,真實的程式實際上可能怎麼寫。如果你覺得目前為止的東西相當令人困惑,你並沒有錯。要記住哪道指令做什麼、哪些記憶體位址存放什麼資料、跳躍要跳到哪裡等等。

[31:38] 這些全都很難追蹤。程式設計師通常使用所謂的高階程式語言來工作。這意味著指令與記憶體位址的所有細節都被隱藏起來,反而讓程式設計師的生活更輕鬆。例如,這是 C 或 C++ 語言中的一小段程式碼。這段程式碼人類可以閱讀。如果 A 大於 B,就把 Q 設為 A;否則,當 B 大於或等於 A 時,就把 Q 設為 B。換句話說,數字 Q 最後會是 A 或 B 中較大的那個數。

[32:14] 這對程式設計師和任何試圖閱讀它的人來說都很容易理解。一個稱為編譯器的程式會接收這段程式碼,並將其安排成正確的指令集,處理變數儲存在記憶體中的位置、要跳轉到哪裡、我們需要使用 16 位元還是 32 位元的數字等等。這就是所謂的編譯程式碼。程式語言有許多方面可以簡化程式設計師的工作,但其中最重要的一個是函式或方法的概念。在數學中,平方根是一個函式,它接收一個數字並傳回另一個數字。當然,電腦程式碼中也有平方根函式的實作,但也有更任意的函式和方法,例如用於擷取電子郵件等等。

[32:55] 假設一個程式需要重複計算不同數字的平方根。組成平方根函式的指令只需要在記憶體中儲存一次。每當需要使用或呼叫該函式時,電腦會跳轉到該函式在記憶體中的位置,執行指令,然後帶著結果跳回程式中它離開的地方。函式和方法可以編譯成函式庫,以便跨越時間和空間的程式設計師可以分享他們所撰寫的有用程式碼。如果你曾在 Windows 上看過 DLL 檔案,那就是這個東西。一旦一個函式或方法被有效率地撰寫出來,其他程式設計師就可以使用它,而不需要花太多時間。

[33:34] [貝多芬第 5 號交響曲] 總結來說:電腦有一條指令可以跳轉到某個記憶體位址並執行那裡的任何指令。這允許程式內有迴圈。有些跳轉只在滿足條件時才會發生。任何能夠執行此類跳轉的電腦,只要有足夠的記憶體和時間,就能執行任何運算。[音樂] 你很可能正在某種顯示器上觀看這部影片。過去和現在有許多技術被用於電腦螢幕,所以我會概括地說明。

[34:10] 像影片開頭那樣的燈泡太大了,但發光二極體或 LED 更小且更有效率。LED 兩端的電壓越高,它就越亮。顯示器能夠根據來自電腦的資料,以電子方式改變其每個 LED 兩端的電壓,從而改變它們的亮度。由於電腦以位元組處理數字,換句話說就是 8 位元的群組,因此亮度以 0 到 255 的刻度遞增是合理的——這是位元組能取的最大值。

[34:42] 人類的眼睛具有對紅、綠、藍光敏感的細胞。透過使用一組三個 LED:紅、綠、藍,改變每一個的相對亮度,就可以欺騙人類的眼睛,讓它感知到完整的光譜。紅與綠的混合會產生黃色和棕色。紅與藍的混合會產生粉紅色。三種顏色全亮度會產生白色,部分亮度則會產生灰色,依此類推。像這樣由三種顏色組成的小點,無論使用什麼技術,都稱為像素。

[35:14] 顯示器上的像素排列成列與行,形成一個 2 維網格。例如,這部影片在最大解析度下寬度為 1920 像素、高度為 1080 像素。在記憶體中填滿一個顯示器所需的位元組數,是寬度乘以高度再乘以 3。最簡單來說,電腦必須每秒在記憶體中更新這麼多位元組 30 或 60 次,並將結果傳送到顯示器。所以,舉例來說,如果你以最大解析度全螢幕播放這部影片,每一幀就會在記憶體中佔用這麼多位元組。

[35:49] 雖然顯示圖形和影像看似純粹是藝術工作,某種程度上也確實如此,但實際上對電腦而言,這不過是我們目前為止看過的閘極邏輯與指令。每個像素都有一個水平或 x 座標,以及一個垂直或 y 座標,對應到它在記憶體中的位置。例如,假設你的顯示器寬度正好是一千像素,而你從左上角的像素開始,如果你在記憶體中向前移動三千個位元組,或相當於一千個像素,你就會離開第一列的末端,來到第二列最左邊的像素。

[36:25] 有許多演算法或一長串指令可以繪製視窗、按鈕、文字和電腦圖形,全都透過這類算術以及操作記憶體中的位元組來完成。要顯示一個簡單的桌面環境,會用一個迴圈反覆複製能唯一定義該顏色的三個位元組序列,將每個像素填滿背景色。工作列則是只對具有特定 y 座標的像素列進行迴圈來繪製。開始按鈕則是將給定 x 與 y 座標範圍內的矩形填上另一種顏色來建立。

[36:59] 文字是透過為每個字母複製預先決定的像素樣式,或透過其他演算法疊加在上方。若要顯示視窗,就必須知道其特定的寬度與高度,以及其左上角像素的目前位置。將左側像素的 x 值與寬度以簡單的算術相加,即可得出最右緣的像素。接著便能繪製標題列,以及邊框文字等等。若使用者移動或調整視窗大小,所有這些算術都必須重新計算,並重繪視窗。

[37:29] 最簡單的電腦圖形涉及在兩點之間繪製一條線,這兩點各自擁有自己的座標組(x,y)。bresenham 演算法涉及一個迴圈,透過沿著學校數學中熟悉的直線 x-y 曲線來繪製這樣一條線。繪製三條線便得到一個三角形,可以為其填上紋理、以模擬光源照亮,並與數千個其他三角形結合,構成 3D 形狀。無論多麼複雜,歸根究柢都不外乎加法、乘法與跳躍指令,並編譯成函式。

[38:00] 聲音是電腦另一種主要的輸出形式。聲音是在空氣中傳遞的高壓與低壓波。喇叭與耳機透過前後移動振膜來產生聲音。在固定導電線圈內部,有一塊永久磁鐵附著於振膜上。通過線圈的振盪電流會將磁鐵拉進拉出,使振膜振盪並產生聲音。電腦將時間切分為短暫的步階,並透過將二進位數字傳送至音效卡,來指定每個時間步階的電流。電子元件會將數位二進位值轉換為適當的電流。

[38:33] 麥克風的運作方式恰好相反。市面上有許多輸入裝置,但電腦處理輸入的方式通常都相同。輸入會在記憶體中留下所謂的訊息,這些訊息是預設長度的位元組群組,用以識別裝置以及輸入的確切內容。例如,一個訊息佇列可能有三則各為八位元組的訊息:解碼後,第一則表示某個按鍵已被按下;第二則表示滑鼠已向右移動 30 個像素;第三則表示鍵盤上的 shift 鍵已被放開。

[39:10] 按鍵排列成一個網格。當按下某個按鍵時,它會在水平列與垂直列之間形成電氣接觸,讓系統能精確辨識出是哪個按鍵。在鍵盤與電腦主機板之間,會傳遞按鍵被按下或放開的訊息到電腦記憶體,讓電腦能據以反應。在我們的電玩範例中,想像「B」按鈕用來購買東西,「S」按鈕用來販售。在 Windows 中,這些按鍵按下動作有對應的十進位數字:分別是 66 和 83。就指令而言,遊戲會根據最後一次按鍵的值進行條件跳躍。

[39:46] 如果按鍵值是 66,電腦就會跳躍到對應購買的一組指令;如果是 83,就會跳躍到對應販售的指令。電腦滑鼠會追蹤它在平面上向左或向右、向上或向下移動了多遠,以及按下了哪些按鈕。程式(通常是電腦的作業系統)會藉由加減從滑鼠接收到的位置變化,持續追蹤滑鼠的 x 與 y 像素座標。

[40:17] 假設當使用者按下滑鼠左鍵時,某個帶有按鈕的視窗目前處於作用中,程式會將滑鼠游標的 x 座標與按鈕的尺寸進行比對。如果 x 大於或等於按鈕的左側,且小於或等於按鈕的右側,那麼這次點擊在水平方向上就位於按鈕內;接著程式會再檢查 y 座標是否也在垂直方向上位於按鈕內。如果兩者皆成立,就視為按鈕被按下。如果在任何時刻畫面上有 10 個按鈕,程式就會逐一檢查每個按鈕是否被按下。

[40:51] [貝多芬第 5 號交響曲] 總結來說:電腦顯示器是由像素構成的網格。每個像素都是一組紅光、綠光和藍光。每種光的亮度由電腦記憶體中對應的位元組設定。因此像素具有任意顏色,而一群像素合在一起就構成影像。聲音的錄製方式是測量聲波的運動,並將其存成記憶體中的二進位數字。播放聲音時則使用相反的過程。外部裝置會傳送訊息給電腦,這些訊息會被放入記憶體,讓電腦能辨識輸入,例如某個按鍵被按下或滑鼠被移動,然後據以反應。

[41:29] 最後,我想對整部影片做個總結。我認為這很了不起:透過在矽中摻雜雜質元素並執行相對簡單的邏輯運算,我們就能觀看和製作影片、處理資訊,甚至棲居於虛擬世界。運算的本質就是堆疊較簡單的概念,以達成更複雜的概念。如果你有 CMOS 對,就能做出 NAND 閘;如果你有 NAND 閘,就能做出任何邏輯閘;如果你有邏輯閘,就能做出指令;如果你有指令,就能把演算法變成函式;從函式出發,你就能建構出任意複雜的程式。

[42:03] 這也意味著電腦非常笨:它們沒有直覺,必須嚴格遵循程式,而程式中的每一種可能情況都必須極其詳細地寫明。

查看英文原始字幕

[0:00] [Music] In this video I will try to give a broad overview of how a computer works from the ground up. Subtitles are available. It will get a little bit technical, but I will try to keep things as simple as possible. There will be a summary at the end of every section and links to further explanations in the description. Hopefully this overview will teach you something new or give you a new perspective on things.

[0:32] Probably nobody in the world knows every single nuance of how a computer is built and operates. This is why when you hear this sound [Beethoven's 5th symphony] that is a warning that I am making massive oversimplifications which I'm sure you'll point out to me in the comments. [Beethoven's 5th symphony] That was the warning for the entire video. [Music] Modern computers at their core are electrical circuits.

[1:04] A very simple circuit might consist of a battery connected to a switch, then a light bulb, then back to the negative terminal of the battery. Modern computer components typically supply a voltage of 3.3 or 5 volts between the positive terminal and the ground or negative terminal if you like to think of it that way. Computers treat information as binary: on or off, true or false, one or zero, based on if the voltage is high or low. The important component in our circuit here is the push switch which comes in two flavors.

[1:37] push to make that's when a person presses down and the circuit is completed between the two terminals and the bulb lights up. The push to break switch is exactly the opposite. This is the symbol for a transistor: more specifically, it's a Metal Oxide Semiconductor Field Effect Transistor or MOSFET. Its source and drain are like the two terminals of a switch, but it has a third terminal called a gate. just like switches there are two types: the N-channel type makes a circuit when a high voltage is applied to the gate, like a push to make. The P-channel breaks the circuit when a high voltage is applied like a push to break.

[2:19] So, transistors act like switches except instead of a person pressing it down a voltage is applied. In modern computers, these types of transistors are used in pairs referred to as CMOS or Complementary Metal Oxide Semiconductor. The gate terminals of a given pair are always connected together so they receive the same input. Here is a simple example: if we apply a high voltage or 1 to the input the N-channel MOSFET opens a route between high voltage and the output. The P-channel closes the root to ground.

[2:52] If we apply a low voltage or 0 at the input the P-channel opens a path to ground. OK, so this circuit seems pointless we got the same thing at the output as we did at the input, but let's swap around the two MOSFETs. Now, when we apply a high voltage or 1 to the input the N-type opens a route to low voltage or 0. When we apply a 0 to the input we get a 1 at the output. Whatever we put in at the input, we get the opposite at the output. Later we will come back to how this forms the basis of all modern processors. Understand that it is vast networks of CMOS pairs like these which allow computers to think.

[3:33] [Beethoven's 5th symphony] For now, let's see how transistor networks are made in basic terms. Silicon is a semiconductor with four valence electrons which is doped, or mixed with, other elements. Gallium, for example, has three valence electrons - one less than silicon, hence more positive or P-type. Meanwhile, an element like arsenic has five valence electrons - one more than silicon, therefore adding a negative charge, hence N-type.

[4:03] Trace amounts of these elements typically one for every million silicon atoms or so are implanted by chemical diffusion or by firing them out of a particle accelerator. Transistors are made by sandwiching a region of one of the dopants between two of the other. Let's look more closely at the N-channel configuration. This by itself does not conduct electricity, but when an electric field is applied to it a conducting channel forms, hence the name: Field Effect Transistor. This can be thought of in simple terms as the electric field attracting electrons from the N-doped region, which has a surplus, to the P-doped region, which has a deficit.

[4:40] Near the semiconductor stack we have a conducting electrical terminal. When it's at high voltage, an electric field forms and the transistor conducts, but to stop a short circuit we need to add an insulating layer between them. The contact is metal, the insulator is silicon oxide and finally we have the doped semiconductor, hence the term MOSFET. For a computer we need millions of interconnected CMOS pairs and they must be small and close together so that electrical signals can travel between them quickly.

[5:13] Making a processor typically begins with a solid block of P-doped silicon called a die. Transistors are then created on top of the die by a process called lithography: "writing on stone". There are two main techniques used in lithography. The first is deposition: chemically building up a layer of controlled, predetermined thickness across the entire die. This means that, for example N-doped silicon can be coated all over the top of the P-doped die. Unwanted surface material is then removed by a second technique called etching. This is done by applying chemicals or hot plasma to the top of the die. several steps are taken to ensure that the etching is targeted at specific areas and therefore material is removed selectively.

[6:02] A chemical termed a photoresist is deposited onto the die. light is shined through a mask exposing predetermined areas of the photoresist. The exposed areas must be the same size or larger than the wavelength of light used, hence very short wavelength ultraviolet light is used for the lithography. A special etching method is used to remove only regions of photoresist which were exposed and nothing else. This opens up gaps in the photoresist and then another etching method is used which targets everything except the unexposed photoresist.

[6:39] Finally, the remaining photoresist is etched away. As a result, material has been removed wherever there are any gaps in the mask. The two techniques of deposition and etching used in conjunction mean that the N-type dopants for all the transistors on the die can be added simultaneously. Next, the silicon oxide is deposited and etched and finally metal is deposited and etched away to leave metal channels on top of the die. These metal features form not just the conducting terminals of the transistors, but also the tracks equivalent to wires in a circuit which connect the transistors as required.

[7:16] Every time, photoresist is deposited, exposed through a mask, gaps are made and a die is etched. Finally, the billions of transistors are assembled and connected together by this method. There may be three CNOS pairs with three different inputs then going out to four different CMOS pairs somewhere else and so on and so on. [Beethoven's 5th symphony] To summarize: computers handle information as ones and zeros when voltage in an electrical circuit is either high or low.

[7:48] Transistors act as electronic switches, changing between these two states. The transistors always come in complementary pairs, so when one half of the pair opens, the other closes. Lithography is the process by which huge numbers of interconnected transistors are built up on a block of silicon. [Music] To understand why transistor networks are useful, we need to talk about a branch of mathematical logic called Boolean algebra.

[8:18] Rather than go into the full gory mathematical details, I will just illustrate the basic concepts with some not too serious examples. Let's say some friends are choosing a restaurant. Alice is happy when the restaurant serves burgers. Bob is happy when the restaurant does not serve burgers. We can think of evaluating restaurants in this case as something called a gate: an object which takes a binary input true or false, 1 or 0 and returns a binary output. In this case, the input is whether the restaurant has burgers and the output is if someone is happy with the restaurant.

[8:54] For Alice the gate is simple: the output is the same as the input. In electronic terms the circuit is just a connection. For Bob the gate is what's unimaginably called a NOT gate. This is a symbol typically used to denote it. In the previous section we have seen how this CMOS pair acts as a NOT gate. Let's consider a more interesting example, when the logic gates can have multiple inputs. Such gates have pretty straightforward names as we'll see. Now the friends are choosing restaurants which might serve pizza, noodles, both or neither. Alice is happy when the restaurant serves at least one of the two options or both. This idea is represented by what is called an OR gate. This is the symbol.

[9:35] Bob wants choice, so he wants both pizza and noodles on the menu, hence the AND gate. Charlie likes it if the restaurant serves pizza or noodles but not both: let's say she thinks that they wouldn't do a good job if they weren't specialized enough. The corresponding gate is called exclusive or XOR for short. Each of these gates also has a complementary gate: basically the same gate with a NOT gate after it. For example the NAND gate gives a 0 when all its inputs are 1, and gives a 1 otherwise.

[10:08] These complementary gates are denoted by the usual symbol with a circle at the output. For a computer, these and all other possible gates must be implemented through CMOS pairs. Connecting two pairs as follows creates a NAND gate. The N-channel and P-channel transistors still work in pairs. Only when a high voltage or 1 is applied to both pairs of inputs do the paths to high voltage close and the path to ground opens so a 0 is output. This is exactly the behavior of a NAND gate.

[10:40] The NAND gate is important because it's what's called universal: this means that any other gate can be created out of NAND gates as long as there are enough of them. Now, there are more efficient ways to make certain gates, but in essence this means that the problem of implementing logic gates through CMOS pairs is solved. There are also gates with three, four, or as many inputs as necessary. For example, take two AND gates connected as shown here and they simply become a three input AND gate and so on and so forth. [Beethoven's 5th symphony] To summarize: logic gates are conceptual mathematical objects which allow us to make deductions based on binary true or false statements.

[11:18] For example if it's true that today is Saturday or today is Sunday, then it's also true that today is the weekend. We have now seen how the CMOS pairs from the last section can be used to practically implement these gates in terms of electronic binary logic. [Music] Now we shall see how computers handle numbers and use logic gate networks to do arithmetic. We are all used to base 10 numbers meaning that there are 10 digits from 0 to 9.

[11:51] The digits represent multiples of powers of 10 which are 1, 10, 100, 1000, and so on. Modern computers use base 2 numbers with just 2 digits: 0 and 1. Binary digits are multiples of powers of 2 which are: 1, 2, 4, 8 and so on. For example, in base 10 the number 953 is 9 times 100 plus 5 times 10 plus 3 times 1. In binary, the number 1 0 1 is 1 times 4 plus 0 times 2 plus 1 times 1 which is 5 in base 10.

[12:25] Any number in base 10 can be represented by a number in base 2 and vice versa. To represent the number 1 0 1 3 electrical tracks in order would have their voltage on off on. Each of these tracks carries a binary digit or bit. With three electrical tracks this is called a 3-bit number. There are similarly 4-bit numbers 8-bit numbers and so on. Of course the 4-bit number 0 1 0 1 is equivalent to the 3-bit number 1 0 1.

[12:56] Let's consider adding together a pair of 1-bit numbers A and B to get C. 0 + 0 = 0 0 + 1 = 1. What happens when we try to add 1 + 1 in binary? The same thing as happens when we add 5 + 5 in regular base 10. There is no digit big enough, so we put a 0 in that column and carry the 1 to the next column. so to guarantee being able to store the result for any possible values of A and B the result C must be a 2-bit number. The bit on the right is 1 when A or B is 1, but not both: this is the behavior of the XOR gate if you recall Charlie from the last section.

[13:35] The bit on the left is 1 only if A and B are 1, ergo the AND gate. Therefore C is obtained by connecting A and B to gates like this. When adding numbers A and B larger than 1-bit things get more complicated with three input gates because there is now a bit from A, a bit from B and a carried bit. As I said in the previous section: NAND gates are universal, meaning any required gate network can be implemented using enough of them.

[14:17] [Beethoven's 5th symphony] In base 10, multiplying by 10 is easy: just add a 0 onto the end of your number. In base 2 the same is true for multiplying by 10 in binary or 2 in base 10. So, multiplying by the numbers 10, 100, 1000 in binary just involves shifting each bit to the left and putting a 0 on the end, which is unimaginably called a bit shift. What about multiplying by an arbitrary number, say for example 11 in binary?

[14:48] This is the same as multiplying by 10 multiplying by 1 and adding the two together. So, binary multiplication of A by an arbitrary number B works as follows: begin with zero. If the rightmost bit of B is 1, add A. Bit shift A once and if the next bit of B is 1, add this to the total. Continue for every bit of B. Computers also rely on making comparisons of numbers, such as: Is A equal to B? Is A greater than B?

[15:18] And so on. The computer might have the current time in seconds stored as a binary number A updated every second and the time to set up an alarm stored as B. The alarm would be made to go off when A is equal to B. To do this, the computer must check that every bit of the number A is the same as the corresponding bit of B. So, the XOR gate returns 1 when an input is 1 and the other input is 0, in other words when its inputs are different. The XNOR gate which is the complement of the XOR meaning it returns the opposite to the XOR therefore returns a 1 when its inputs are the same. A bunch of XNOR gates applied to each pair of bits of A and B return whether those bits are equal or not.

[15:57] The and gate returns a 1 when all its inputs are 1. So applying it to the XNOR gates returns a single bit which is 1 if and only if A is equal to B. There are also ways to implement subtraction, division and other comparisons all using gate networks. For example bit shifting the other way divides a number by two. These are all implemented using transistors etched on a chip. Just a note: when I say the word number in this video I am referring to an integer or whole number. There is also a way to store fractions in a binary version of scientific notation called floating point numbers or floats.

[16:32] I won't go into how they work but the general principle outlined here still applies. To summarize: a sequence of binary on off electrical signals is used by computers to represent a number in base 2 as opposed to base 10 which is used by people day to day. Networks of logic gates can be used to add, subtract and compare binary numbers, and with a few extra steps multiply and divide them too. [Music] We have seen how circuits composed of transistors can be used to implement addition and other algebra for any number with arbitrary binary digits or bits.

[17:09] If you want to build a computer we would have to choose exactly how many electrical tracks and gates to have available for representing a number. This is like when you're filling in a form and it has a set number of boxes physically printed on it for putting in your age. For commercial computers this is always a multiple of 8. 8 bits are called a byte. The computer will have adder circuits and so on with the types I described in the last section which take 1 byte or 8-bit numbers, 2 byte or 16-bit numbers, 4 byte or 32-bit numbers as inputs.

[17:40] No matter of how many bytes a number is comprised one of its bits can be set aside to denote a plus or minus sign. When a number is signed in this way, if the leading bit is 0 then the number is positive. If the leading bit is 1 then the number is negative. As anyone who has ever taken a mathematics exam knows: it's important to store your workings. Whichever operation a computer undertakes, the inputs and outputs must be somehow stored. To do this, a type of circuit called a latch is used.

[18:10] Once it has latched, it will stay that way for a long time and can therefore be used to store a bit of data. 8 latches store a byte and so on. This is an example of what's called a D-latch comprised of NAND gates. You may notice that this looks rather strange: before we've seen gates in a strictly sequential manner where the output of one gate goes directly to another. Now there is an output from one going to the input of another, but then the output comes right back. It is this kind of interconnection that allows this network of gates to hold and retain its output value 0 or 1.

[18:44] Nothing will change unless the E or enable input is 1. When enable is 1, whatever bit is applied to D will propagate to the output. The computer will carry out an operation, addition for example, enable a bunch of latch circuits to store the output and then switch the enable back off. The result will then be held there as long as is required. 8 of these latches can form register which will store a byte of data as long as it's required. An add circuit of the type we looked at in the previous section would have such registers to store the inputs before the addition takes place and a register to capture the result of the addition.

[19:23] It takes some small but significant amount of time for electrical signals to propagate, for transistors to finish switching and so on. To guarantee that the operation is fully carried out before attempting to read out the answer, all computers have a clock. In basic terms an oscillating crystal switches the voltage on and off, on and off, at a very specific rate. At the time when this video was made computer processors have typical clock speeds of 2 Gigahertz, meaning that the clock switches at 2 billion times a second.

[19:55] The regular switching allows events to be synchronized. To fully carry out an addition operation it will take at least 3 clock cycles. On the first clock cycle numbers would be loaded into the input registers, then the addition circuit can work over the next clock cycle. Finally on the third clock cycle the output register gets an enable signal and stores the result. Actually, most operations take much longer than one or even three clock cycles to carry out. We have seen in the previous section that multiplication takes several adding steps to accomplish.

[20:30] Nonetheless, whether it takes one or a hundred cycles the clock is still used to synchronize when an operation begins until the output is ready. All the so-called arithmetic logic, that is: all the arrays of logic gates which do addition and subtraction is physically located together with the registers on a chip called the processor or Central Processing Unit. There are also circuits in there called the cache, which nowadays can store tens of kilobytes of data as the intermediate stages of calculations for example.

[21:01] There is a need for a larger and longer term way to store many more bytes of data than the registers and the cache. Computers have separate physical chips of random access memory or RAM which makes use of capacitors to store this data. If a capacitor is charged, it has a high voltage and therefore a value of 1. If it's discharged it has 0 volts relative to ground and so is 0. The charge leaks or is lost over time, but is topped up by the memory circuits as long as the computer is powered.

[21:31] A typical laptop computer has tens of gigabytes or billions of bytes of RAM. Every byte stored in RAM has its own address like a post box in an apartment block. The address is itself a binary number. The RAM has many transistors forming gate networks which ensure that when it receives the address in binary format from the processor, it then returns the byte stored at that address. Until recently, computers wouldn't usually have more than 4 gigabytes of RAM. Each of those bytes requires a unique address, so memory addresses were 32-bit numbers. Such 32-bit numbers go up to just over 4 billion so that used to be fine.

[22:10] When RAM got bigger there effectively weren't enough addresses, so now 64-bit numbers are used for memory addresses. This is the difference between 32 and 64-bit programs. So, let's say you're playing a video game where you have a number of coins in your wallet. The video game will keep track of the address where the wallet total is stored. If you found eight more coins, the game will fetch the number currently in your wallet from that address, load it into the register, load a biary number corresponding to 8 in base 10 into the other register, add them together and put the results back into the address with the wallet total.

[22:47] We will look at this in more detail in the next section. Computers also store huge volumes of data on hard disk for decades at a time even without power. A hard disk is covered in tiny magnetic domains which can point up or down corresponding to 1 or 0. As the disk spins, a sensor head can either passively read off the orientation of the magnets as bits, or actively flip the orientation of the magnetic domains to write data onto the disk. Each of the bytes also has its own separate address.

[23:19] A laptop or personal computer can store hundreds of gigabytes on a hard disk. there are other technologies like flash memory, DVDs and so on that I won't go into here. The fundamental limit on how fast a computer can do operations is the speed of light, which can go about 15 centimeters or half a foot during a typical computer clock cycle so it would take multiple cycles for a computer larger than that to fetch any data. Realistically, transistors switch much slower than that, while a hard disk takes time to spin up and scan to read the data. In terms of types of computer memory: the cache is small, but quick to access; the RAM stores many more bytes of data, but takes longer to read; the hard disk takes the longest to read, but stores a lot of data almost permanently; so, there is a trade-off between how quickly the data can be retrieved and how much can be stored. From now on in this video, I will explain things as if the computer just has a single pool of memory which it can instantly read In reality things are more complicated,

[24:20] as I've just described [Beethoven's 5th symphony] To summarize: computers have a clock which synchronizes the operations being performed and ensures that there is enough time to finish performing one operation before starting another. Computers process binary numbers in groups of 8 bits called a byte. Registers are used to store numbers during computations and at other times numbers are stored in the cache, in random access memory and on a hard disk. Each byte in memory has its own unique address which is itself a binary number.

[24:56] [Music] We have a way of implementing binary operations and storing the results, but the computer needs a way to determine which operation to use and when. This is called an instruction. There are instructions such as: add two numbers, and copy this number. Computers use what's known as the von Neumann architecture, which means that the instructions are stored in memory as binary numbers amongst other data. For example, the instruction number 16 and base 10 or this byte in binary might mean: add together two 8-bit numbers and store the result.

[25:34] Instruction number 17 might mean the same thing but for 16-bit numbers and so on. When the computer encounters an instruction, it will use a comparison circuit which we looked at in a previous section to switch on the logic circuit for the necessary operation. If the byte is equal to 16, switch on the add circuit. How does the computer know when a byte is an instruction or not? 16 might be the instruction to add, or it might be the number of coins in a video game. Well, everything has to do with a precise order in which the bytes appear in memory. First comes the instruction byte.

[26:08] If the instruction is to add, the computer will need the two memory addresses of the things being added and the memory address where the result is to be stored. Remember that memory is like post boxes for a big apartment building with a unique address on each byte. In the case of modern 64-bit systems addresses are 8 bytes long. Therefore, the entire add instruction has one instruction byte and 24 bytes of addresses; 25 in total. On the other hand, if the instruction is to copy a number, it just needs to be followed by the origin and destination addresses, meaning the whole thing takes 17 bytes in total.

[26:45] The trick is that each type of instruction always takes up a set number of bytes and - crucially - the next instruction immediately follows on after the previous one. So, when the computer encounters the add instruction not only does it carry it out, but it knows that the next instruction is precisely 25 bytes along. To start with, the computer interprets byte number 0 - the one at the very start of memory - as an instruction and thereafter goes from instruction to instruction. Conversely, if there has been an error and the next instruction is not where it's supposed to be - tough luck! The computer will probably crash. How does the computer actually carry out an add instruction?

[27:22] The number from the first address is loaded into one input register and the second address into another input register. The add circuit is enabled and it does the addition. Then, after a sufficient number of clock cycles, the result can be written from the output register to the required address in memory. What happens if the instruction is to add two 16-bit numbers together? The computer simply goes to the address of number A and instead of getting just one byte, it gets that byte and then one immediately after. The same for number B and the result C. [Beethoven's 5th symphony] A computer program is a collection of instructions (and addresses) to be done in order. The computer goes from instruction to instruction to make the program run. Let's look at a specific example: part of the game, which is just a program after all, where the player has a wallet with coins in.

[28:10] This is what the contents of memory might look like. Byte number 0 is the instruction to add. Then come two addresses: the address of the wallet total and the number of coins the player has just found. The third address is the wallet again. after the addition the wallet total will be overwritten. No matter what, the computer will now interpret byte number 25 in memory as an instruction. It is another add instruction. The addresses of the wallet and the amount of coins the player has made from selling items are the inputs.

[28:40] The wallet address is the output again. The next instruction is 25 bytes further along at byte 49. The program keeps going on with millions of other instructions required to make the game function. In this case, the wallet total is being used a lot, so it will probably be held long term in the processor's cache as well as the RAM. When the player saves the game it will be written to a save file on the computer's hard disk. To summarize: the computer interprets certain bytes in memory as instructions for which operation to carry out.

[29:15] Computer programs are stored in memory. They are just large sequences of instructions, memory addresses and numbers. The computer goes from instruction to instruction like clockwork, always knowing based on the type of the current instruction how far along the next one is. [Music] So far, we've looked at instructions which work in a linear manner: copy a number, add two together and so on. The program would always produce the same result and eventually it would just get to the end of memory and stop.

[29:48] We need a few more instructions to make a computer what's known as Turing complete. Being Turing complete means that it can eventually carry out any possible computation: load a web page, render video game and so on. The only difference between two Turing complete computers is how fast they are at doing those computations. The first type of instruction a computer needs is what's called a jump or sometimes called a go to. Following the instruction byte is a memory address. As the name suggests, instead of going directly to the next instruction in memory, the program jumps to the specified memory address and carries out the instruction there.

[30:24] This means that the program can now do a loop. In our video game example, we have seen how instructions are used to add numbers of coins to a total kept in a wallet. These instructions and all the others required to update the game world, render the graphics and so on are followed by a jump instruction back to the start. This way the game can keep running indefinitely. The other crucial type of instruction that a computer needs is a branching or conditional instruction. If a given condition is met, for example if two numbers are equal, then jump to a particular address.

[31:00] We have already seen how a comparison can be implemented with logic gates. Adding such logic makes a jump conditional, allowing different sets of instructions to be executed based on calculations performed so far, or inputs to the computer such as a keyboard or mouse. With conditional jumps the computer is Turing complete: given enough time it can do any calculation. At this point it's worth mentioning how a real program might actually be written. If you thought that things were quite confusing so far, you're not wrong. Remembering which instruction does what, which memory addresses have what data stored in them, where jumps go and so on.

[31:38] This is all hard to keep track of. Programmers typically work with what are referred to as high level programming languages. This means that all the nuts and bolts of instructions and memory addresses are hidden away and instead life is made easier for the programmer. For example this is a bit of code in the C or C++ language. This code is readable by humans. If A is greater than B, set Q to be A; otherwise, when B is greater or equal to A, set Q to be B. In other words, the number Q will end up with whichever number is larger out of A or B.

[32:14] This is easy to understand for the programmer and anyone who tries to read it. A program called a compiler will take this code and arrange it into the correct set of instructions, sort out where the variables are stored in memory, where to jump, do we need to use 16-bit or 32-bit numbers and so on. This is what's referred to as compiling the code. There are many aspects of programming languages which simplify the job of the programmer, but one of the most important is the idea of a function or method. In mathematics, the square root is a function which takes a number and returns another. Certainly there are implementations of the square root function in computer code, but there are also more arbitrary functions and methods such as for fetching emails and so on.

[32:55] Say that a program needs to evaluate the square root of different numbers repeatedly. The instructions which comprise the square root function need only be stored in memory once. Whenever the function needs to be used, or called, the computer will jump to the function's location in memory, carry out the instructions and jump back with the result to the place in the program where it left off. Functions and methods can be compiled into libraries so that programmers across time and space can share useful code that they've written. If you've ever seen a DLL file on Windows, that's what that is. Once a function or method has been written efficiently, other programmers can use it without needing to spend much time on it.

[33:34] [Beethoven's 5th symphony] To summarize: computers have an instruction to jump to a memory address and carry out whatever instruction is there. This allows loops within programs. Some jumps only happen when a condition is met any computer that is capable of such jumps can carry out any computation as long as it has enough memory and time to do so. [Music] Chances are, you're watching this video on some sort of display. There are many technologies past and present which have been used for computer monitors, so I will speak in generalities.

[34:10] Light bulbs like at the start of the video are too large, but light emitting diodes or LEDs are smaller and more efficient. The higher the voltage across an LED, the brighter it is. A display is capable of electronically changing the voltage across each of its LEDs and therefore their brightness according to data from the computer. Since computers handle numbers in bytes, in other words groups of 8 bits, it makes sense to have the brightness go up on a scale from 0 to 255 - the largest value a byte can take.

[34:42] The human eye has cells sensitive to red green and blue light. By using a set of three LEDs: red, green and blue, varying the relative brightness of each one, the human eye can be tricked into perceiving the full spectrum of light. A mix of red and green gives yellows and browns. Red and blue gives pinks. All three colors at full brightness give white and at partial brightness give grey and so on. A little spot composed of three colors like this, no matter what technology is used, is called a pixel.

[35:14] A display has pixels arranged in rows and columns to form a 2-dimensional grid. For example, this video at maximum resolution is 1920 pixels in width by 1080 pixels in height. The number of bytes it takes to fill a display in memory is 3 times the width times the height. In simplest terms, the computer must update this many bytes in memory 30 or 60 times per second and send the result to the display. So, for example, if you full screen this video in its maximum resolution, each frame would take up this many bytes in memory.

[35:49] While displaying graphics and images may seem like a purely artistic undertaking, and in some sense it is, in actual fact for a computer it is nothing more than gate logic and instructions we have seen so far. Each pixel has a horizontal or x coordinate and a vertical or y coordinate which corresponds to its position in memory. For example, let's say your display is exactly a thousand pixels wide and you start with the top left pixel, if you move forward three thousand bytes in memory or the equivalent of a thousand pixels, you'll go off the end of the top row and onto the left pixel of the second row.

[36:25] There are many algorithms or long lists of instructions to draw windows, buttons, text and computer graphics all by using this kind of arithmetic and manipulating bytes in memory. To display a simple desktop environment, a loop fills every pixel with the background color by repeatedly copying the sequence of three bytes that uniquely defines the color. The taskbar is drawn by looping over only rows of pixels with specific y coordinates. A start button is created by filling a rectangle with a given range of x and y coordinates with another color.

[36:59] Text is added on top by copying pre-determined patterns of pixels for each letter or through some other algorithm. To display a window, it's necessary to know its specific width and height and the current position of its top left pixel. Adding the x value of the left pixel and the width by simple arithmetic gives the rightmost edge pixel. A title bar can then be drawn, along with a border text and so on. If the user moves or resizes the window, all of this arithmetic has to be redone and the window redrawn.

[37:29] The simplest computer graphic involves drawing a line between two points both with their own set of coordinates (x,y). The bresenham algorithm involves a loop which draws just such a line by following a straight x-y curve familiar from school mathematics. Draw three lines and you have a triangle which can be filled with textures, illuminated by simulated light sources and combined with thousands of others to make a 3D shape. No matter how complicated it all comes down to addition, multiplication and jump instructions compiled into functions.

[38:00] Sound is the other major form of output from a Computer. Sounds are waves of high and low pressure moving through the air. Speakers and headphones produce sounds by moving a membrane back and forth. There is a permanent magnet attached to the membrane inside a fixed conducting coil. An oscillating current through the coil pulls the magnet in and out making the membrane oscillate and creating a sound. The computer breaks up time into short steps and specifies the current at every step in time by sending a binary number to a sound card. Electronics turn the digital binary values into the appropriate current.

[38:33] A microphone works in exactly the opposite way. There are many input devices out there, but the way a computer deals with inputs is usually the same. The inputs leave so-called messages in memory, which are groups of bytes of preset length, identifying the device and what exactly the input consists of. For example, a message queue may have it three messages of eight bytes each: when decoded, the first one states that the a key has been pressed down; the second states that the mouse has moved 30 pixels to the right; and the third that the shift key has been released on a keyboard.

[39:10] The keys are arranged into a grid. When a key is pressed, it makes an electrical contact between a horizontal and vertical row, allowing the precise key to be identified. Between the keyboard and the computer's motherboard, the message announcing when a key is pressed down or released is relayed to computer memory, allowing the computer to act on it. In our video game example, imagine that the 'B' button is used to buy things and the 'S' button is used to sell. In Windows, these key presses have corresponding decimal numbers 66 and 83 respectively. In terms of instructions, the game would have conditional jumps based on the value of the last keystroke.

[39:46] If the keystroke value is 66, then a computer would jump to a set of instructions corresponding to buy; if 83, it would jump to instructions corresponding to sell. A computer mouse tracks how far left or right and up or down it has moved along a flat surface and also which buttons have been pressed. A program, typically the computer's operating system, keeps track of the x and y pixel coordinates of the mouse by adding or subtracting any changes in position received from the mouse.

[40:17] Say that a window with a button is currently active when a user presses down the left mouse button the program will check the x coordinate of the mouse cursor against the dimensions of the button. If x is greater or equal to the left side of the button and less than or equal to the right side of the button then the click is horizontally inside the button the programs then check the y-coordinate is vertically inside the button too. If both are true the button is considered pressed. If at any moment you have 10 buttons on screen, the program will check every button in turn if it's been pressed down or not.

[40:51] [Beethoven's 5th symphony] To summarize: a computer display is a grid of pixels. Each one a group of a red, a green and a blue light. The brightness of each light is set by a corresponding byte in computer memory. A pixel therefore has an arbitrary color and a group of them together makes an image. Sound is recorded by measuring the motion of sound waves and saving them as binary numbers in memory. The reverse process is used to play back the sounds. External devices send messages to the computer, which are placed into memory allowing the computer to identify inputs, such as when a key has been pressed or the mouse has been moved, and then act upon them.

[41:29] Finally, I want to take stock of the entire video. I think it's amazing how doping silicon with impurity elements and performing relatively simple logical operations allow us to watch and create videos process information and even inhabit virtual worlds. Computing is all about building up simpler concepts to achieve more complicated ones. If you've got CMOS pairs, you can make a NAND gate; if you've got NAND gates, you can make any logic gate; if you have logic gates, you can make instructions; if you have instructions, you can make algorithms into functions; from functions you can build up programs as complicated as you like.

[42:03] This also means that computers are very dumb: they have no intuition and must rigidly follow programs which have every possible eventuality spelled out in great detail.

在 Vidleaf 工作台中開啟

搜尋逐字稿、選取行、複製帶時間戳的引文、翻譯。

署名

《電腦如何運作——從矽到應用程式》,作者為 Improbable Matter(https://www.youtube.com/@improbablematter),依 CC BY 4.0 授權(https://creativecommons.org/licenses/by/4.0/)。來源影片:https://www.youtube.com/watch?v=5f3NJnvnk7k。本頁為該影片的文字轉錄稿,並加入了段落分隔與時間戳記;創作者與 Vidleaf 無任何關聯,亦不為其背書。Vidleaf 加入了 AI 標點與 AI 生成的摘要;原始措辭仍可於本頁取得。

你是創作者或權利持有人嗎?請求更正或移除:copyright@vidleaf.app(參見 關於這些頁面)。

最後更新