Microprocessors and Microcontrollers CPU & ALU EE3954 by Maarten Uijt de Haag, Tim Bambeck, Harsha Chenji CPU & ALU.1
So far Oscillator (Section 2) To generate the device clock which is necessary for the device to execute instructions and for the peripherals to perform their functions Architecture (Section 4) CPU & ALU.2
Program Bus EPROM Program Memory Up to k x 14 14 13 Program Counter -Level Stack (13-bit) RAM File Registers Up to 36 x Data Bus PORTA RA0 RA1 RA2 RA3 RA4 RA5 PORTB OSC1/CLKIN OSC2/CLKOUT Instruction Register Instruction Decode & Control Timing Generation Internal RC Clock 7 Power-Up Timer Oscillator Startup Timer Power-On Reset Watchdog Timer Brown-out Reset Address MUX FSR Register STATUS Register 3 MUX ALU W Register PORTC RB0 RB1 RB2 RB3 RB4 RB5 RB6 RB7 RC0 RC1 RC2 RC3 RC4 RC5 RC6 RC7 CPU & ALU.3
Now CPU & ALU (Reference Manual - Section 5) Instruction Set Instruction Format Arithmetic and Logic Unit (ALU) STATUS Central Processing Unit (CPU) Assembly Language Programming using MPLAB CPU & ALU.4
Instruction Set Instruction Set: the language you use to talk to a specific computer or a set of instructions used to tell a computer what functions/procedures to perform Instruction Set Architecture: what kind of instructions does the instruction set consist of. CPU & ALU.5
Instruction Set PICMicro Microcontrollers Byte-oriented File Register* Operations Program Bus EPROM Program Memory Up to k x 14 14 13 Bit-oriented File Register Operations Program Counter -Level Stack (13-bit) RAM File Registers Up to 36 x Data Bus Literal and Control Operations PORTA RA0 RA1 RA2 RA3 RA4 RA5 Instruction Register * Part of data memory 7 Address MUX PORTB RB0 RB1 CPU & ALU.6
Instruction Set - Byte Oriented CPU & ALU.7
Instruction Set Bit Oriented CPU & ALU.
Instruction Set Literal and Control Operations CPU & ALU.9
Arithmetic and Logic Unit (ALU) Addition Subtraction Shift Logical Clear addwf, addlw, incf subwf, sublw, decf rlf, rrf andwf, xorwf, iorwf, andlw, xorlw, iorlw, comf clrf CPU & ALU.10
Arithmetic and Logic Unit (ALU) -bit literal -bit register value W Register Special function registers (SFRs) for example PORTS And d bit or from instruction ALU MUX General Purpose Registers (GPR) from RAM If d = 0 result goes to W. For literal instructionsliteral goes to W. If d =1 result goes to file register. CPU & ALU.11
Program Bus EPROM Program Memory Up to k x 14 14 13 Program Counter -Level Stack (13-bit) RAM File Registers Up to 36 x Data Bus PORTA RA0 RA1 RA2 RA3 RA4 RA5 PORTB OSC1/CLKIN OSC2/CLKOUT Instruction Register Instruction Decode & Control Timing Generation Internal RC Clock 7 Power-Up Timer Oscillator Startup Timer Power-On Reset Watchdog Timer Brown-out Reset Address MUX FSR Register STATUS Register 3 MUX ALU W Register PORTC RB0 RB1 RB2 RB3 RB4 RB5 RB6 RB7 RC0 RC1 RC2 RC3 RC4 RC5 RC6 RC7 CPU & ALU.12
Arithmetic and Logic Unit (ALU) addlw 22h -bit literal: 22h -bit register value addlw 22h means w ß (w) + 22h d bit or from instruction W Register ALU MUX Special function registers (SFRs) And General Purpose Registers (GPR) d = 0 or literal instructions d =1 CPU & ALU.13
Destination Bit f: address of register in RAM (Register File) d: destination bit addwf 0x23, 1 means 0x23 ß (0x23) + W 1 F (register) 0 - W addwf 0x23, 0 means W ß (0x23) + W CPU & ALU.14
Arithmetic and Logic Unit (ALU) addwf 22h,0 Data Memory: -bit register value addwf 22h,0 means w ß (w) + (22h) d bit or from instruction W Register ALU MUX Special function registers (SFRs) And General Purpose Registers (GPR) d = 0 or literal instructions d =1 CPU & ALU.15
STATUS Register (0x03 in all banks) Contains the arithmetic status of the ALU, the RESET and the bank select bits (to be discussed in Section 6) ALU-related CPU & ALU.16
STATUS Register ALU Related bits CPU & ALU.17
STATUS Register CPU & ALU.1
Suppose w contains 34h. addlw 52h STATUS Example -addition 34h + 52h =? 0011 0100 0101 0010 -------------- 0 1000 0110 Not equal to zero, thus Z=0 Carry equal to zero, thus C=0 Carry from bit 3 to bit 4 is 0, thus DC=0 CPU & ALU.19
STATUS Example - subtraction Suppose w contains 34h. = 0011 0100 binary sublw 52h (subtract w from literal) 52h - 34h = 52h + (twos complement of 34h) 1100 1011 = ones complement of 34h 1 = add 1 ---------- 1100 1100 = twos complement of 34h 0101 0010 = 52h (2d) 1100 1100 = - 34h (-52d) -------------- 1 0001 1110 1Eh (30d) Z = 0 C = 1 DC = 0 Or, in decimal: 2-52 = 30 CPU & ALU.20
Assembly Example Suppose we want to perform the operation: 34h + 52h = 6h movlw 34h ; you need to use the w register!! addlw 52h ; result (6h) is stored in w Now using a temporary register called TEMP: TEMP equ 20h ; entry in register file (location 20h) movlw 34h ; you need to use the w register!! movwf TEMP ; move the value to Register TEMP movlw 52h ; store 52h in w addwf TEMP,1 ; add w and TEMP, put the result at TEMP CPU & ALU.21
Instruction Set Suppose there is an LED connected to pin RD1 (PORTD bit 1) as follows: RD1 R (470) bsf 0h,1 or, PORTD equ 0h bsf PORTD,1 CPU & ALU.22
Arithmetic and Logic Unit (ALU) bsf TEMP,5 (TEMP equ 20h) -bit literal -bit register value bsf TEMP,5 means TEMP<5> ß 1 d bit or from instruction W Register ALU MUX Special function registers (SFRs) And General Purpose Registers (GPR) d = 0 or literal instructions d =1 CPU & ALU.23
Another Example Suppose there is an LED connected to pin RC2 (PORTC bit 2) and a switch connected to RB0 (PORTB bit 0) as follows: V DD (5VDC ) R= 4.7 KΩ RB0 RC2 R (470) Write a program that, if the switch is closed, turns on the LED, and, if the switch is open, turns it off. CPU & ALU.24
Example Step 1: Need to check if the switch (RB0) is open or closed: If it is open V =? +5 V or Logic 1 If it is closed V =? 0 V or Logic 0 Step 2: How to check for this with an instruction? Hint: we need to check bit 0 of PORTB! Step 3: How to turn on or off the LED? If LED must be on RC2 must be V =? If LED must be off RC2 must be V =? +5V or Logic 1 0V or Logic 0 Step 4: Write the program. CPU & ALU.25
Example FLOW-CHART* Main if ( switch closed ) { turn on LED } else { turn off LED } Switch closed? Yes Turn on LED No Turn off LED *See the flow-chart guide! CPU & ALU.26
Example Alternative Flowchart Main Main Switch closed? No Switch closed? Yes No Yes Turn on LED Turn on LED Turn off LED Turn off LED CPU & ALU.27
Example Label Fields Mnemonic Fields Argument Fields Comment Fields TST btfss PORTB,0 ; port B bit 0 is tested goto LED_ON ; switch is closed (RB0=0) goto LED_OFF ; switch is open (RB0=1) LED_OFF bcf PORTC,2 ; turn LED off goto TST ; return to bit testing statement LED_ON bsf PORTC,2 ; turn on LED goto TST ; return to bit testing statement First example of branching (if..then )! CPU & ALU.2
Example Can it get any smaller? TST btfss PORTB,0 ; port B bit 0 is tested goto LED_ON ; switch is closed (RB0=0) goto LED_OFF ; switch is open (RB0=1) LED_OFF bcf PORTC,2 ; turn LED off goto TST ; return to bit testing statement LED_ON bsf PORTC,2 ; turn on LED goto TST ; return to bit testing statement TST btfss PORTB,0 ; port B bit 0 is tested goto LED_ON ; switch is closed (RB0=0) LED_OFF bcf PORTC,2 ; turn LED off goto TST ; return to bit testing statement LED_ON bsf PORTC,2 ; turn on LED goto TST ; return to bit testing statement CPU & ALU.29
Instructions 14-bit in Program Memory Program Bus EPROM Program Memory Up to k x 14 14 13 Program Counter -Level Stack (13-bit) RAM File Registers Up to 36 x Data Bus PORTA RA0 RA1 RA2 RA3 RA4 RA5 PORTB Instruction Register 7 Address MUX RB0 RB1 RB2 CPU & ALU.30
Instruction Format Conversion to Machine Code CPU & ALU.31
Instruction Set CPU & ALU.32
Instruction Set CPU & ALU.33
Arithmetic and Logic Unit (ALU) addlw 22h Instruction in Program Memory 1 1 1 1 1 0 0 0 1 0 0 0 1 0 -bit literal: 22h -bit register value literal k 22h = 0010 0010 b W Register Special function registers (SFRs) addlw 22h means w ß (w) + 22h d bit or from instruction ALU MUX And General Purpose Registers (GPR) d = 0 or literal instructions d =1 CPU & ALU.34
Arithmetic and Logic Unit (ALU) bsf TEMP,5 (TEMP equ 20h) Instruction in Program Memory 0 1 0 1 1 0 1 0 1 0 0 0 0 0 -bit literal -bit register value bit b 5 = 101b register f 20h = 0100000b W Register Special function registers (SFRs) And d bit or from instruction ALU MUX General Purpose Registers (GPR) d = 0 or literal instructions d =1 CPU & ALU.35
Instruction Format Examples movlw 44h addwf TEMP,1 btfsc TEMP,6 sleep NOTE: TEMP equ 20h CPU & ALU.36
Central Processing Unit (CPU) Uses the Arithmetic and Logic Unit (ALU), Responsible for Fetch, Decode, and Execute, Control Unit and Datapath, Controls memory address bus, Controls memory data bus, Controls stack. CPU & ALU.37