Timing Analysis - Ohio University
-
Upload
khangminh22 -
Category
Documents
-
view
1 -
download
0
Transcript of Timing Analysis - Ohio University
2/3/2019
1
Timing Analysis
Spring 2019EE3954: Microprocessors and Microcontrollers
Avinash Karanth
Professor, Electrical Engineering and Computer Science
Ohio University
E-mail: [email protected]
Acknowledgment: Harsha Chenji, Jim Goble, Maarten Uijt de Haag
1
Course Administration
• Lab 1 this week!• Submit prelab, in-lab and post-lab reports!
• Quiz 1• On Blackboard, multiple choice questions (10), timed
quiz, randomized solutions, cannot back trace, once started, have to finish within 25 minutes, single attempt, so make sure you have reliable connection.
• Availability: Friday Feb 8 between 9:00 AM – 9:00 PM.
• Topics include everything including Timing Analysis (this slides)
2
1
2
2/3/2019
2
3
Decision
Process A Process B
instructions toset up conditionbtfss …goto ProcessAgoto ProcessB
Example: execute code at LOC_1 if a “0” occurs at PORTB,0execute code at LOC_2 if a “1” occurs at PORTB,0
btfss PORTB,0goto LOC_1goto LOC_2
ORbtfsc PORTB,0goto LOC_2goto LOC_1
Branching – if … then …
‘for’ loop and ‘while/repeat’ loop
4
Counter0?
Set Counter
DecrementCounter
Process A
N
Y
Perform Process A“Counter” times
Condition False
Set up Condition
Process A
N
Y
Perform Process Awhile condition is true
Condition True
Set up Condition
Y
Process A
ContinueMain Program
ContinueMain Program
ContinueMain Program
N
3
4
2/3/2019
3
DECFSZ Command
5
A useful command for doing a task a set number of times.
Loops – ‘for’ loop
6
• Use ‘DECFSZ’
COUNT equ 0x20
; Set up the counter valuemovlw d’3’ ; SET COUNTER To “Three”movwf COUNT ; Store it in a GPR register.
NXT: …… ; PROCESS A…decfsz COUNT,F ; Decrement and check if zerogoto NXT…… ; Continue the main program…
5
6
2/3/2019
4
BTFSC Command
7
Loops – ‘while/repeat until’ loop
8
Example: Execute ProcessA subroutine/function until ‘w’ contains the value decimal 99
NXT: sublw d ’99’btfsc STATUS,Zgoto DONEcall ProcessAgoto NXT
DONE ………
NXT: call ProcessAsublw d ’99’btfss STATUS,Zgoto NXT
DONE ………
Always executes Process A at least once
Does not necessarilyexecute Process A
Recall: Sublw = literal – (W) -> W
7
8
2/3/2019
6
Loops – ‘repeat until’ example
11
movlw 0x20 ; set first address of the listmovwf FSR ; by setting FSR
NXT1: clrf INDF ; element of the listincf FSR,F ; increment the list pointermovlw 0x70 ; check if the end of the listsubwf FSR,W ; reached – here: 0x70btfss STATUS,Z ; skip next inst. If f-(w) = 0goto NXT1 ; goto next element in the listreturn
Question = will location 0x70 get cleared?
Timing Analysis
12
Remember:
1 Instruction Cycle takes 4 Oscillator Cycles
osccy TT 4=
Most instructions take 1 instruction cycle to execute
Some instructions take 2 instruction cycles to execute: GOTO, CALL, RETURN, RETLW, RETFIE
Some instructions take either 1 or 2 instruction cycles to execute: DECFSZ, INCFSZ, BTFSC, BTFSS
See instruction summary tables
11
12
2/3/2019
7
Timing Analysis
13
Execute 1
Fetch 2 Execute 2
Fetch 3
Fetch 4
Oops, the microcontroller just fetched the wrong instruction
Execute 4
Fetch 5
2 instruction cycles
DECFSZ, INCFSZ, BTFSC, BTFSS
if a SKIP occurs:if a SKIP does NOT occur:
Execute 1
Fetch 2 Execute 2
Fetch 3 Execute 3
Fetch 4
1 instruction cycle
Example 1 – Timing Analysis
14
org 0x000
NXT:movlw 0x28 ; 1 instruction cyclemovwf PORTB ; 1 instruction cyclenop ; 1 instruction cyclemovf PORTC,W ; 1 instruction cyclesublw 0xAA ; 1 instruction cyclebtfss STATUS,Z ; 2 if taken; 1 is not takengoto NXT ; 2 instruction cycles
A ……
Disregarding first fetch instruction cycle time:If the ‘skip’ is taken the first time the time it takes to get to point A is:delay = 1 + 1 + 1 + 1 + 1 + 2 = 7 instruction cycles
If the ‘skip’ is taken the second time the time it takes to get to point A is:delay = (1 + 1 + 1 + 1 + 1 + 1 + 2) + (1 + 1 + 1 + 1 + 1 + 2) = 15 instruction cycles
13
14
2/3/2019
8
Timing Analysis
15
With an external oscillator of 4MHz:
6
6
10.25 10 seconds 0.25 sec
4 10osc
T −= = =
Oscillator period
4 1.0 seccy osc
T T = =
instruction cycle
7 instruction cycles = 7 * 1.0 μsec = 7 μsec15 instruction cycles = 15 * 1.0 μsec = 15 μsec
Timing Analysis - Subroutines
16
…
call SUB_1 ; 2
…
…
SUB_1: andlw 0x0F ; 1
movwf TEMP ; 1
movlw 0x12 ; 1
subwf TEMP,F ; 1
return ; 2
A) Subroutine itself takes: 6 instruction cycles
B) Calling the subroutine costs:2 instruction cycles
C) Totally, it costs: 6+2 = 8 instruction cycles
to call and execute the subroutine
15
16
2/3/2019
9
Timing Analysis – Building Delays
17
1: A: movlw d’3’ ; 1 instruction cycle2: movwf COUNT ; 1 instruction cycle3: NXT:nop ; 1 instruction cycle4: decfsz COUNT,F ; 1 or 2 instruction cycles5: goto NXT ; 2 instruction cycles
B: …
Line: Instruction duration: Times Executed: Execution time:
1 1 1 1
2 1 1 1
3 1 COUNT 1*COUNT
4 1 COUNT-1 1*COUNT-1
4 2 1 2
5 2 COUNT-1 2*(COUNT-1)
Timing Analysis – Building Delays
18
Time to go from A to B is:
(1 + 1) + COUNT + COUNT – 1 + 2 + 2•(COUNT – 1) =1+1+3+3-1+2+2(2) =
13 instruction cycles
So with an oscillator of 4MHz: delay takes 13 * 1 μsec = 13 μsec
1: A: movlw d’3’ ; 1 instruction cycle2: movwf COUNT ; 1 instruction cycle3: NXT:nop ; 1 instruction cycle4: decfsz COUNT,F ; 1 or 2 instruction cycles5: goto NXT ; 2 instruction cycles
B: …
17
18
2/3/2019
10
Timing Loops
19
What is the general expression for this delay given any value for COUNT ?
COUNT*41nsInstructio# +=
So, what is the maximum delay we can do with this construction ?
COUNT is a byte so its maximum value is 255 !!Maximum value for delay is therefore: 1+4*255 = 1021 cycles, or 1021*1.0 μsec = 1021 μsec(0 gives 1+4*256) = 1025 cycles – because decfsz 0->255).
Timing Loops – Even Shorter
20
COUNT equ 0x20 ; assign memory for countorg 0x100
A: movlw d’10’ ; 1movwf COUNT ; 1
NXT:decfsz COUNT,F ; 1 (2)goto NXT ; 2 instruction cycles
B: …
Time to go from A to B is:
(1 + 1) + 9 + 2 + 2*(10-1)= 31 instruction cycles
COUNT*31nsInstructio# +=
19
20
2/3/2019
11
Timing Loops – Even Shorter
21
Counter0?
Set Counter
DecrementCounter
N
YContinueMain Program
Nested Loops
22
OuterCounter
0?
Set Outer Counter
DecrementOuter Counter
N
YContinueMain Program
Set Inner Counter
InnerCounter
0?
DecrementInner Counter
Y
N
InnerLoop
OuterLoop
21
22
2/3/2019
12
Nested Timing Loops
23
COUT equ 0x20 ; outer counter storageCIN equ 0x21 ; inner counter storage
org 0x0100 ; program location - start…
OUTER: movlw d’10’ ; 1movwf COUT ; 1
INNER: movlw d’255’ ; 1movwf CIN ; 1
NXT: decfsz CIN,F ; 1 (2)goto NXT ; 2decfsz COUT,F ; 1 (2)goto INNER ; 2
B: …
Nested Timing Loops - Computations
24
Inner loop: CINInner •+= 31
Outer loop:( )
( )
COUTCINCOUT
COUTCIN
COUT
COUTCOUT
Inner
InnerOuter
••+•+=
••++=
•++=
+•+=
341
341
31
31
For example, on the previous slide COUT = 10 and CIN = 255:
76625531Inner =•+=
(@ 4MHz -> 7.691 msec delay)
691,710)7663(1Outer =•++=
23
24
2/3/2019
13
Timing Loop Fine-tuning
25
• By inserting instructions (most of the time nopinstructions) the inner loop delay can be changed and as a result the outer loop. Similarly, instructions can be added to the outer loop.
Fine-tuning Example
26
COUT equ 0x20CIN equ 0x21
org 0x00…
OUTER: movlw 0x0A ; 1movwf COUT ; 1
INNER: movlw 0xFF ; 1movwf CIN ; 1nop ; 1
NXT: decfsz CIN,F ; 1 (2)goto NXT ; 2decfsz COUT,F ; 1 (2)goto INNER ; 2
B: …
2 instead of 1Since nop is insertedoutside the loop
( ) COUTInnerOuter •++= 31
CINInner •+= 32
Increment thisif nop is insertedinside the loop
25
26
2/3/2019
14
Delay Subroutines
27
DELAY : movlw d’10’ ; 1movwf COUT ; 1
INNER: movlw d’255’ ; 1movwf CIN ; 1
NXT: decfsz CIN,F ; 1 (2)goto NXT ; 2decfsz COUT,F ; 1 (2)goto INNER ; 2return ; 2
Add 2 instruction cycles to the delay for the ‘call’ statement, and 2 instruction cycles for the ‘return’ statement
call DELAY ;2
27