CS 31: Intro to Systems Digital Logic
Total Page:16
File Type:pdf, Size:1020Kb
CS 31: Intro to Systems Digital Logic Martin Gagné Swarthmore College January 31, 2017 You’re going to want scratch papr today … borrow some if needed. Quick Announcements • Late Policy Reminder • 3 late days total for the whole semester • Lab work • Start early, especially multi-week labs • Readings • Only the page linked, no further links Today (and Thursday) • Hardware basics • Machine memory models • Digital signals • Logic gates • Manipulating/Representing values in hardware • Adders • Storage & memory (latches) The system stack c program compiler software shell operating system Starting this week This class memory CPU hardware circuits gates transistors electrical engineering wires Hardware Models (1940’s) • Harvard Architecture: CPU Program Data (Control and Memory Memory Arithmetic) Input/Output • Von Neumann Architecture: CPU (Control and Program Arithmetic) and Data Memory Input/Output Von Neumann Architecture Model • Computer is a generic computing machine: • Based on Alan Turing’s Universal Turing Machine Von Neumann Architecture Model • Computer is a generic computing machine: • Based on Alan Turing’s Universal Turing Machine ➔ The hardware only ever executes one program: Initialize program counter Repeat forever: { fetch instruction indicated by program counter increment the program counter execute the instruction } Von Neumann Architecture Model • Computer is a generic computing machine: • Based on Alan Turing’s Universal Turing Machine • Stored program model: computer stores program rather than encoding it (feed in data and instructions) • No distinction between data and instructions memory • 5 parts connected by buses (wires): • Memory, Control, Processing, Input, Output Memory Cntrl Unit | Processing Unit Input/Output addr bus cntrl bus data bus Memory • Stores instructions and data. • Addressable, like array indices. • addr 0, 1, 2, … (In CPU:) • Memory Address Register: address to read/write • Memory Data Register: value to read/write Memory Cntrl Unit | Processing Unit Input/Output addr bus cntrl bus data bus Central Processing Unit (CPU) • Processing Unit: executes instructions selected by the control unit • ALU (arithmetic logic unit): simple functional units: ADD, SUB, AND, OR, etc. • Registers: temporary storage directly accessible by instructions • Control unit: determines the order in which instructions execute • PC: program counter: address of next instruction • IR: instruction register: holds current instruction • clock-based control: clock signal+IR trigger state changes Input/Output • Keyboard • Files on the hard drive • Network communication Memory Cntrl Unit | Processing Unit Input/Output addr bus cntrl bus data bus Digital Computers • All input is discrete (driven by periodic clock) • All signals are binary (0: no voltage, 1: voltage) data, instructions, control signals, arithmetic, clock • To run program, need different types of circuits Circuits to Circuits to CPU RAM execute store program program ALU, Cntrl, Cntrl & Storage data and instructions instructions Storage and support reading that act on and writing program data addressable storage bus locations Goal: Build a CPU (model) Three main classifications of HW circuits: 1. ALU: implement arithmetic & logic functionality (ex) adder to add two values together 2. Storage: to store binary values (ex) Register File: set of CPU registers, Also: main memory (RAM) 3. Control: support/coordinate instruction execution (ex) fetch the next instruction to execute Abstraction User / Programmer Wants low complexity Applications Specific functionality Software library Reusable functionality Operating system Manage resources Complex devices Compute & I/O Abstraction Hardware Circuits Logic Gates Transistors Here be dragons. Complex devices (Electrical Engineering) Compute & I/O … (Physics) Logic Gates Input: Boolean value(s) (high and low voltages for 1 and 0) Output: Boolean value result of boolean function Always present, but may change when input changes And Or Not a a out out a out b b out = a & b out = a | b out = ~a A B A & B A | B ~A 0 0 0 0 1 0 1 0 1 1 1 0 0 1 0 1 1 1 1 0 Note the circle on the More Logic Gates output. This means “negate it.” NAND NOR a a out out b b out = ~(a & b) out = ~(a | b) A B A NAND B A NOR B 0 0 1 1 0 1 1 0 1 0 1 0 1 1 0 0 Combinational Logic Circuits • Build up higher level processor functionality from basic gates Acyclic Network of Gates Inputs Outputs Outputs are boolean functions of inputs Outputs continuously respond to changes to inputs What does this circuit output? And Or Not X Output Y Clicker Choices X Y OutA OutB OutC OutD OutE 0 0 0 1 0 1 0 0 1 0 1 0 0 1 1 0 1 0 1 1 1 1 1 0 0 1 1 0 What can we do with these? • Build-up XOR from basic gates (AND, OR, NOT) A B A ^ B 0 0 0 Q: When is A^B ==1? 0 1 1 1 0 1 1 1 0 Which of these is an XOR circuit? And Or Not Draw an XOR circuit using AND, OR, and NOT gates. I’ll show you the clicker options after you’ve had some time. Which of these is an XOR circuit? A: B: A A B B C: D: A A B B E: None of these are XOR. Checking the XOR circuit A^B == (~A & B) | (A & ~B) A B out = A^B A:0 B:0 A^B: 0 A:1 B:0 A^B: 1 A:0 B:1 A^B: 1 A:1 B:1 A^B: 0 Abstracting the XOR circuit A^B == (~A & B) | (A & ~B) A B out = A^B A B A ^ B 0 0 0 0 1 1 XOR 1 0 1 1 1 0 A out = A^B = B Digital Circuits - Building a CPU Three main classifications of HW circuits: 1. ALU: implement arithmetic & logic functionality (ex) adder to add two values together 2. Storage: to store binary values (ex) Register File: set of CPU registers 3. Control: support/coordinate instruction execution (ex) fetch the next instruction to execute HW Circuits Logic Gates Transistor Digital Circuits - Building a CPU Three main classifications of HW circuits: 1. ALU: implement arithmetic & logic functionality (ex) adder to add two values together Start with ALU components (e.g., adder, logic circuits) Combine (with multiplexer) into ALU! HW Circuits Logic Gates Transistor Arithmetic Circuits • 1 bit adder: A+B • Two outputs: 1. Obvious one: the sum 2. Other one: ?? A B Cout Sum(A+B) 0 0 0 0 0 1 0 1 1 0 0 1 1 1 1 0 Which of these circuits is a one-bit adder? A B Sum(A+B) Cout 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0 1 A: B: A Sum A Sum B B Cout Cout C: D: A Sum A Sum B B Cout Cout More than one bit? • When adding, sometimes have carry in too 0011010 + 0001111 One-bit (full) adder A B Cin Sum Cout Need to include: 0 0 0 0 0 0 1 0 1 0 Carry-in & Carry-out 1 0 0 1 0 1 1 0 0 1 0 0 1 1 0 0 1 1 0 1 1 0 1 0 1 1 1 1 1 1 Cin A 1-bit B Sum = adder Cout One-bit (full) adder A B Cin Sum Cout Need to include: 0 0 0 0 0 0 1 0 1 0 Carry-in & Carry-out 1 0 0 1 0 1 1 0 0 1 0 0 1 1 0 0 1 1 0 1 1 0 1 0 1 1 1 1 1 1 Cin cout A cout A B HA sum cout 1-bit B Sum adder HA sum cin = Cout Multi-bit Adder (Ripple-carry Adder) 0 A A 0 1-bit 3 1-bit B Sum B Sum 0 adder 0 3 adder 3 Cout Cout A 1 1-bit B Sum 1 adder 1 … C out A N-1 1-bit A BN-1 SumN-1 2 1-bit adder B Sum 2 adder 2 Cout Cout Three-bit Adder (Ripple-carry Adder) 0 0 Carry in 1-bit 1 adder A0 A 010 (2) 1 A 1 2 Sum 1-bit 3-bit 0 + 011 (3) 1 Sum adder adder 1 B Sum = 0 2 B1 B2 0 1-bit 0 adder Carry out Arithmetic Logic Unit (ALU) • One component that knows how to manipulate bits in multiple ways • Addition • Subtraction • Multiplication / Division • Bitwise AND, OR, NOT, etc. • Built by combining components • Take advantage of sharing HW when possible (e.g., subtraction using adder) Simple 3-bit ALU: Add and bitwise OR 3-bit inputs A and B: A0 A1 A 3-bit Sum 2 Sum0 adder 1 Sum2 B0 B 1 At any given time, we B 2 only want the output from ONE of these! Or0 Or1 Or2 Simple 3-bit ALU: Add and bitwise OR 3-bit inputs Extra input: control signal to select Sum vs. OR A and B: A0 A1 A 3-bit Sum 2 Sum0 adder 1 Sum2 B0 B1 Circuit that takes B2 in Sum0-2 / Or0-2 Or and only outputs 0 one of them, based on control Or1 signal. Or2 Which of these circuits lets us select between two inputs? A: B: Input 1 Input 1 Control Control Signal Signal Input 2 Input 2 C: Input 1 Control Signal Input 2 Multiplexor: Chooses an input value N Inputs: 2 data inputs, N signal bits N Output: is one of the 2 input values s 1 bit 2-way MUX b out a out = (s & a)|(~s &b) • Control signal s, chooses the input for output • When s is 1: choose a, when s is 0: choose b Word Multiplexor s s B MUX Out A Input: b31 out31 two 32 bit values (a, b) a31 one 1bit signal s b30 out30 Each corresponding bit of 32 bit input values, a30 fed through 1 bit mux Output: One 32 bit value b 0 (either a or b) out0 a0 38 N-Way Multiplexor Choose one of N inputs, need log2 N select bits s1 s0 choose s1 4-Way Multiplexor 0 0 D0 s0 0 1 D1 1 0 D2 1 1 D3 D0 s1 s0 D1 S Input to Out choose D0 MUX4 D2 D3 D0 .