Coursera:從頭自製計算機1--用Nand門搭建15個基本芯片(附全部芯片實現)

簡介

項目1的要求是用Nand門搭建15個基本芯片.其理論基礎是:

  • 用And, Or和Not三種Boolean操作符就可以表示所有的Boolean函數
    • 用德摩根定律可以證明Or可以用And和Not表示
      • And和Not可以僅用Nand表示

根據以上結論,只需要使用Nand操作符,就可以表示所有的Boolean函數.

講師在整個課程中"扮演"系統架構師(System architect)的角色,他們提供需要實現芯片的框架文件(stub file),測試文件和比較文件. 框架文件包含了需要實現的芯片的名稱,該芯片引脚的名稱,以及簡單的注釋 (其中不包含具體的實現). 測試文件和比較文件則是相當於定義了這個芯片的接口表現, 具體來説就是輸入和輸出的表現.

學員"充當"開發者的角色,根據講師提供的框架和輸入輸出表現,來搭建相應的芯片,完成整個課程大致需要實現30多個芯片,最基本的組件只有Nand門(内部可以由晶體管實現,唯一不需要學員自己實現的部分).最終將這些芯片組合在一起,便可以構建出一個Hack計算機.

在項目1中有個至關重要的概念,即Boolean Function和Digital Logic Circuit之間可以相互表示. 另外,根據真值表也可以設計出對應的數字邏輯電路(通過sum of product或者product of sum方法), 或者寫出相應的Boolean Function. Boolean Algebra也是一種代數, 對代數而言, 有三個要素(常數/變量/運算),在Boolean Algebra中具體表現為: constants(0/1), variable(用來代表0/1的字母)和operation(AND,OR,NOT).

項目1需要實現的15個芯片分類

Elementary logic gates 16-bit variants Multi-way variants
Not Not16
And And16
Or Or16 Or8way
Xor
Mux Mux16 Mux4Way16, Mux8Way16
DMux DMux4Way, DMux8Way

Mux簡介:
最簡情況有三個輸入端a,b,和Sel,以及一個輸出端out. 其邏輯的效果是:

if sel = 0
  // 此時無論b為何值,均不影響out的結果,只和a相關
  if a = 0
    out = 0
  else if a = 1
    out = 1

else if sel = 1
  // 此時無論a為何值,均不影響out的結果,只和b相關
  if b = 0
    out = 0
  else if b = 1
    out = 1

具體實現 (Implementation)

Not

And (Not Nand)

Or

\overline{x \lor y} = \overline{x}\land\overline{y}


Xor

Mux

a b sel out
0 0 0 0
0 0 1 0
0 1 0 0
0 1 1 1
1 0 0 1
1 0 1 0
1 1 0 1
1 1 1 1

f(a,b,Sel) = \overline{a}bSel + a\overline{b}\overline{Sel} + ab\overline{Sel}+ abSel

if (sel = 0)
  out = a
else
  out = b
Sel=0, a=0, b=0/1(whatever),out=0

Sel=0, a=1, b=0/1(whatever),out=1

Sel=1,a=0/1(whatever),b=0,out=0

Sel=1,a=0/1(whatever),b=1,out=1

DMux

if (sel = 0)
  {a,b}={in,0}
else
  {a,b}={0,in}
in sel a b
0 0 0 0
0 1 0 0
1 0 1 0
1 1 0 1

a=in\overline{sel}
b=insel




Not16, And16, Or16, Mux16

Example

CHIP Foo {
  IN in[8] // 8-bit input
  OUT out[8] // 8-bit output
  // Foo's body (irrelevant to the example)
}

The implementation of Not16, And16,Or16,and Mux16 is similar.



    // ...
    PARTS:
    // Put your code here:
    Not(in=in[0],out=out[0]);
    // ...
    Not(in=in[15],out=out[15]);

Or8Way

Ref:And4way

/*
 * Ands together all 4 bits of the input
 */
CHIP And4way{
  IN a[4];
  OUT  out;
  
  PARTS:
    AND(a = a[0], b = a[1], out = t01);
    AND(a = t01, b = a[2], out = t012);
    AND(a = t012,b = a[3], out = out);
    // ...
}

Mux4Way16, Mux8Way16

Example

/*
 * Adds three 16-bit values
 */
CHIP Add3Way16{
  IN first[16], second[16], third[16];
  OUT  out[16];
  
  PARTS:
    Add16(a = first, b = second, out = temp);
    Add16(a = temp, b = third, out = out);
    // ...
}


// ...
    Mux16(...,sel=sel[0],...);
    Mux16(...,sel=sel[0],...);
    Mux16(...,sel=sel[1],...);

Mux4Way16 can be implemented by three Mux16 chips and Mux8Way16 can be implemented by two Mux4Way16 chips and one Mux16 chip.

DMux4Way, DMux8Way

DMux4Way

in sel a b c d
0 00 0 0 0 0
0 01 0 0 0 0
0 10 0 0 0 0
0 11 0 0 0 0
1 00 1 0 0 0
1 01 0 1 0 0
1 10 0 0 1 0
1 11 0 0 0 1



After sel[0] works, sel[0]=0 \rightarrow choose a,c; sel[0]=1 \rightarrow choose b,d
We need to further choose one of a and c, and one of b and d by using sel[1].

DMux8Way
After sel[0..1] works, we need to use sel[2] to separate a/e,b/f,c/g,d/h. (recursively apply the rule of DMux4Way)
In comparison, we only need to separate a/c and b/d in the situation of DMux4Way.

Coursera課程部分筆記

All chips can be made from the same building blocks: elementary logic gates and these gates can further be made from one primitive gate: Nand. The mathematical foundation of logic gates is Boolean algebra so that in this chapter, professors first present Boolean functions and their basic rules and then introduce how to implement Boolean gates in a hardware description language.

Unit 1.1: Boolean Logic

Boolean functions can be represented by either formulas or truth tables.

1." Every Boolean function, no matter how complex, can be expressed using three Boolean operators only: And, Or, and Not."
2." Every Boolean function can be expressed using at least one Boolean expression called the canonical representation."

f(x,y,z) = \overline{x} \overline{y}z + \overline{x} yz + xy\overline{z} + xyz

There are some laws associated with boolean logic as follows.

  • commutative laws
    x \land y = y \land x
    x \lor y = y \lor x
  • associative laws
    x \land (y \land z) = (x \land y) \land z
    x \lor (y \lor z) = (x \lor y) \lor z
  • distributive laws
    x \land (y \lor z) = (x \land y)\lor(x \land z)
    x \lor (y \land z) = (x \lor y)\land(x \lor z)
  • De Morgan laws
    \overline{x \land y} = \overline{x}\lor\overline{y}
    \overline{x \lor y} = \overline{x}\land\overline{y}

Unit 1.2: Boolean Functions Synthesis

Truth table to Boolean Expression

f(x,y,z) = \overline{x}\overline{y}\overline{z} + \overline{x}y\overline{z} + x\overline{y}\overline{z}

"Just with ANDs and NOTs, we can construct any Boolean function."

Proof:
a \lor b = \overline{\overline{a} \land \overline{b}}

"Any Boolean Functions can be represented using an expression containing only NAND operations."

Nand (Not And) x NAND y = Not(x AND y)

x y Nand
0 0 1
0 1 1
1 0 1
1 1 0

Proof: NOT and AND can be represented just by NAND

Not(X) = (X NAND X)
(X AND Y) = NOT(X NAND Y)

Unit 1.3: Logic Gates

Gate: A gate is a physical device that implements a Boolean function.

"The chip interface tells us What the chip is doing, and chip implementation tells us How the chip is doing it. There are many possible implementations for every interface. The builders of a chip need to worry about the implementation and the users only need to know the interface."

Primitive and Composite Gates: Composite gates are composed of more elementary gates.


Unit 1.4: Hardware Description Language

XOR gate

a \oplus b = (\neg a \land b)\lor (a \land \neg b)

Steps:

  1. Understand what the interface does (focus on inputs and outputs)
  2. Draw a diagram in mind or physically
  3. Name the connections(a,b,in,out)
  • Common HDLs
    • Verilog
    • VHDL

This course provides a simple and minimal HDL language and there are 2 useful documents for us to nail it.
Appendix A: Hardware Description Language (HDL)
HDL Survival Guide

Unit 1.5: Hardware Simulation

  • Simulation options
    • interactive
    • Script-based
    • with/without output and compare files
  • Process:
    • load the HDL file into the hardware simulator
    • enter 0/1 into the chip's input pins
    • evaluate the logic of the chip (whether the logic is correct or not)
    • inspect the values of
      • output pins
      • internal pins

Interactive simulation

Script-Based simulation
The advantage of script-based simulation is that we do not need to do tedious interactive works (play with input pins and inspect the output) and things can be done automatically with the test scripts.


icon: load script

This pane is used to display either test script or the output.

Script-Based simulation with compare files
Compare our output with a well-behaved output file to see if there is a difference. If two lines in Xor.out and Xor.cmp are not the same, then the simulator will throw a comparison error.

Alternative HDL software
Icarus Verilog for Windows
Icarus Verilog和GTKwave使用简析

Unit 1.6: Multi-Bit Buses

Addition of two 16-bit integers

Buses in HDL

/*
 * Adds two 16-bit values
 */
CHIP Add16{
  IN a[16], b[16];
  OUT  out[16];
  
  PARTS:
    // ...
}

Using Buses

/*
 * Adds three 16-bit values
 */
CHIP Add3Way16{
  IN first[16], second[16], third[16];
  OUT  out[16];
  
  PARTS:
    Add16(a = first, b = second, out = temp);
    Add16(a = temp, b = third, out = out);
    // ...
}

Working with Bits in Buses

/*
 * Ands together all 4 bits of the input
 */
CHIP And4way{
  IN a[4];
  OUT  out;
  
  PARTS:
    AND(a = a[0], b = a[1], out = t01);
    AND(a = t01, b = a[2], out = t012);
    AND(a = t012,b = a[3], out = out);
    // ...
}
/*
 * Computes a bit-wise and of its two 4-bit input buses
 */
CHIP And4{
  IN a[4], b[4];
  OUT  out[4];
  
  PARTS:
    AND(a = a[0], b = b[0], out = out[0]);
    AND(a = a[1], b = b[1], out = out[1]);
    AND(a = a[2], b = b[2], out = out[2]);
    AND(a = a[3], b = b[3], out = out[3]);
    // ...
}

Sub-buses

// ...
  IN lsb[8], msb[8] // least significant bytes and most significant bytes
// ...
  Add16(a[0..7]=lsb, a[8..15]=msb, b=..., out=...);
  Add16(..., out[0..3]=t1, out[4..15]=t2);

Example

IN c, Bus1[16], Bus2[16];
OUT out, out2[16];

PARTS:
  Add16(a=Bus1[0..15], b=Bus2[0..15], out[0..14]=out2[0..14]); // ✔
  Add16(a=true, b=false, out=out2); // ✔
  And(a=c, b=Bus2[7], out=out); // ✔

Unit 1.7: Project 1 Overview

Chip Name Description
Nand Nand gate (primitive)
Not Not gate
And And gate
Or Or gate
Xor Xor gate
Mux Multiplexer
DMux Demultiplexer
Not16 16-bit Not
And16 16-bit And
Or16 16-bit Or
Mux16 16-bit Multiplexer
Or8Way Or (in0,in1,in2,...in7)
Mux4Way16 16-bit, 4-way mux
Mux8Way16 16-bit, 8-way mux
DMux4Way 4-way demultiplexer
DMux8Way 8-way demultiplexer

Multiplexer


Demultiplexer

It looks like the inverse of a multiplexer.



The Hack chip-set API

  Add16(a= ,b= ,out= ); 
  ALU(x= ,y= ,zx= ,nx= ,zy= ,ny= ,f= ,no= ,out= ,zr= ,ng= ); 
  And16(a= ,b= ,out= ); 
  And(a= ,b= ,out= ); 
  ARegister(in= ,load= ,out= ); 
  Bit(in= ,load= ,out= ); 
  CPU(inM= ,instruction= ,reset= ,outM= ,writeM= ,addressM= ,pc= ); 
  DFF(in= ,out= ); 
  DMux4Way(in= ,sel= ,a= ,b= ,c= ,d= ); 
  DMux8Way(in= ,sel= ,a= ,b= ,c= ,d= ,e= ,f= ,g= ,h= ); 
  DMux(in= ,sel= ,a= ,b= ); 
  DRegister(in= ,load= ,out= ); 
  FullAdder(a= ,b= ,c= ,sum= ,carry= );  
  HalfAdder(a= ,b= ,sum= , carry= ); 
  Inc16(in= ,out= ); 
  Keyboard(out= ); 
  Memory(in= ,load= ,address= ,out= ); 
  Mux16(a= ,b= ,sel= ,out= ); 
  Mux4Way16(a= ,b= ,c= ,d= ,sel= ,out= ); 
  Mux8Way16(a= ,b= ,c= ,d= ,e= ,f= ,g= ,h= ,sel= ,out= ); 
  Mux(a= ,b= ,sel= ,out= ); 
  Nand(a= ,b= ,out= ); 
  Not16(in= ,out= ); 
  Not(in= ,out= ); 
  Or16(a= ,b= ,out= ); 
  Or8Way(in= ,out= ); 
  Or(a= ,b= ,out= ); 
  PC(in= ,load= ,inc= ,reset= ,out= ); 
  RAM16K(in= ,load= ,address= ,out= ); 
  RAM4K(in= ,load= ,address= ,out= ); 
  RAM512(in= ,load= ,address= ,out= ); 
  RAM64(in= ,load= ,address= ,out= ); 
  RAM8(in= ,load= ,address= ,out= ); 
  Register(in= ,load= ,out= ); 
  ROM32K(address= ,out= ); 
  Screen(in= ,load= ,address= ,out= ); 
  Xor(a= ,b= ,out= ); 

Unit 1.8: Perspectives

NMOS Nand Gate (left)
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,324评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,303评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,192评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,555评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,569评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,566评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,927评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,583评论 0 257
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,827评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,590评论 2 320
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,669评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,365评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,941评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,928评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,159评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,880评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,399评论 2 342

推荐阅读更多精彩内容