背景:
這學期在香港大學交流學習,選了ELEC 2441 Microprocessor and Microcomputer這門課。老師講課深入淺出,把本來比較枯燥的東西講的非常容易理解,課程安排也很合理,可惜十一回來玩把港澳通行證丟了,無奈之下只能補辦。,今天來武大圖書館借一本書啃啃。
ps: 以下課件內容均來自于香港大學 Dr. Vincent
Computer Operating Cycle
下面我講一講計算機底層是如何處理一條指令的,計算機在執(zhí)行一條指令時會做以下兩件事情。
- fetching an instruction word from memory and interpreting that instruction.
- executing the operations called for by the instruction word.
其實就是做兩件事情,一件是獲取指令,一件是執(zhí)行指令,這兩個操作分別在instruction cycle和execution cycle中進行。
在介紹后面詳細過程之前我先簡單介紹一下指令,基本的Assembly Instructions有以下四類:
- Arithmetic or logic instructions
e.g. +?×÷, AND, OR, NEGATE. - Program control instructions
e.g. JMP/BEQ - skip over a block of instructions. - Internal data manipulation instructions
e.g. ASLA - shift the bits of a word - Input output instructions
e.g. read data from keyboard, output to printer.
Instruction的格式:
-
Three-address Instructions
Three-address Instructions Two-address Instructions

- One-address instructions
這種指令source和destination的地址都是accumulator,因此也叫做“accumulator-based”
Instruction cycle diagram
step 1:
要執(zhí)行指令的地址由PROGRAM COUNTER(PC)傳入到MEMORY ADDRESS REGISTER(MAR)中,PC傳入的地址決定了哪些指令會從內存中取出( pc put the address of the op-code byte onto the address bus, set R/W'=1 (i.e. a high voltage) to initiate a read from memory.),在instruction cycle結束后PC會加1。
- MAR (memory address register)
用來存儲instruction address和data address,MAR和address bus相連,這是cpu與bus通訊的唯一方式。在MAR output和address bus 之間有個32-bit tri-state buffer(D),當deactivated以后MAR可以和bus斷開允許其他io device使用bus,MAR存儲著instruction address和data address。 - MDR (memory data register)
It connects to the data bus and contains the data to be stored in the computer storage(e.g. RAM), or the data after a fetch from the computer storage. It acts like a buffer and holds anything that is copied from the memory ready for the processor to use it.

step 2:
Control unit generates a READ pulse, which cause the Memory unit to read the instruction word form the memory location specified by the MDR. MDR functions as a buffer register for all data read from or written into memory.
step 3:
instruction中op-code的部分傳入INSTRUCTION REGISTER(IR) ,同時操作數(shù)地址傳入MAR中取代原來的部分。(With the given address, the memory unit gets the op-code byte from the memory cells, puts the byte onto the data bus. The control unit generates signals to copy the byte into the Instruction Register (IR))
step4:
IR 中的op-code傳入decoder中,決定了使用哪一種op code,同時傳入control signal generating circuits中使cpu知道在execute cycle中執(zhí)行那種execute signal。
step5:
PC加一,準備下一個instruction cycle。
一個instruction cycle 執(zhí)行結束后是一個execute cycle:
step 1:
在instruction cycle中 control-signal-generating circuits 已經(jīng)產(chǎn)生了相應的control signal,如果一條指令需要從內存中取出數(shù)據(jù),Control unit產(chǎn)生一條READ pulse,which takes the data from the address specified by the MAR and places it in the MDR(MAR is holding the operand address from the instruction cycle)
step2:
數(shù)據(jù)從MDR到ALU中(可能存儲在register b或者accumulator中)control unit 發(fā)送control signals決定這次轉換并且決定了對數(shù)據(jù)進行什么樣的操作。
step3:
數(shù)據(jù)在ALU中執(zhí)行完畢后,如果需要將data存儲到memory中(如STA操作), control signals使數(shù)據(jù)從accumulator傳入MDR.
step 4:
control unit發(fā)送一個write pulse到MDR,MDR會將數(shù)據(jù)寫到MAR中的那個地址中。
The bus system:


Address bus
這是一個沒有方向的bus,數(shù)據(jù)從cpu到io elements的bus,一個cpu邏輯上可以有16條address bus,因此可以產(chǎn)生2的16次方個地址,每一個地址都綁定著內存中的地址或者io element,例如在 address 20A0,可能存儲著一個字節(jié)的數(shù)據(jù)在RAM/ROM,或者在interface circuitry的8-bit buffer register。
當cpu想要read或者write數(shù)據(jù)時,cpu將16位的address code放在他的16個address pin output上,A0->A15, 傳入address bus,地址被decode到相應的內存地址或者io device中。
RAM (Random access memory)
This section of memory is used to store programs and data that will change often during the course of operation. It is also used as storage for intermediate and final results of operations performed during execution of a program.ROM (Read only memory)
Store data and instruction that does not change.
Data bus
雙向的bus,D0->D8 表示databus pin,既可以作為input又可以作為output取決于cpu當前做的是read還是write操作。
Control bus
一系列的signals用來同步和激活microcomputer中的elements,既可以由cpu發(fā)給io device(R/W),又可以由io發(fā)給cpu(Interrupt input)
Timing signals:
在control bus中最重要的signal就是system clock signal,在系統(tǒng)操作產(chǎn)生的時候產(chǎn)生相應的time interval,不同的microcomputer采用不同的clock signals,以Motorola's 6800為例,他需要external circuit去產(chǎn)生CPU需要的signal,很多的系統(tǒng)采用一種two-phase clock system(non overlapping pulses)

The read operation

1、首先,設置R/W到信號到1,R/W是一條control bus,連接著所有的內存和io elements,同時cpu將16-bit 的address 放在port上,然后傳入address去定位一個要操作的內存地址或者I/O devices。
2、被選中的內存地址或者I/O devices將8-bit 的數(shù)據(jù)放到data bus 上其他沒有被選中的I/O devices將不會影響到data bus,因為他們的tristate outputs將會被disabled(high z)。
4、cpu在data bus pin(D0->D7)接受到由data bus 傳來的數(shù)據(jù),data pin在R/W為1時作為input pin,數(shù)據(jù)被寫入到cpu內部的register如accumulator。
