Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B:...
Transcript of Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B:...
Module1-DControlStructureApplications
TimRogers2017[1.D]-1
LearningOutcome#1
“Anabilitytoprogramamicrocontrollertoperformvarioustasks”
How?
A. ArchitectureandProgrammingModelB. InstructionSetOverviewC. AssemblyControlStructuresD. ControlStructureApplicationsE. ParameterPassingF. TableLookupG. MacrosandStructuredProgramming
[1.D]-2
Objective“ControlStructureApplications”
Why?
unsigned int I;
for(I=1;I<=10;I++) {<statements>;}
Loopsarenice
N equ ?
org $800macme
pshb ; (2)pshx ; (2)pshy ; (2)ldx #XA ; (2)ldy #YA ; (2)movw #0,ACM ; (5)movw #0,ACM+2 ; (5)ldab #N ; (1)
loopemacs ACM ; (13)leax 2,x ; (2)leay 2,y ; (2)dbne b,loop ; (3)
puly ; (3)pulx ; (3)pulb ; (3)rts ; (5)
ACM rmb 4XA fdb ?,?,?YA fdb ?,?,?
Whole“apps”arebetter…
[1.D]-3
3Simpleapps
1. SoftwareDelay2. Extended-precisionbinaryadd/subtract3. Extended-precisiondecimaladd/subtract
[1.D]-4
SoftwareDelay
• Purpose:Donothing,butforaspecificamountoftime• Fineifyouneedanimprecise delay:keyde-bouncingforinstance• Whydoesthisendupbeingimprecise?
delay_func
Input:(A)=<DelayTimeinms>
Output:nothingPassingarguments
viaregisters
Parameter-dependentoverheadInterrupts
[1.D]-5
SoftwareDelay• How(andthisisjustonewaytodothis):doubly-nestedloop
• Innerlooptakes1ms(willtimeitbasedonnumberofcycles/instruction)• Outerloopcontrols#timeswegothroughinnerloop
Onequestion:Howmanytimesdoweneedtogoaroundtheinnerloopgetto1ms?
[1.D]-6
Reminder
• Canfindtheexactcyclecountforeachinsn inreferencemanual
[1.D]-7
pshx [2]psha [2]pshc [2]
delay_func
loopo
loopi
ldx #_______ [2]
dbnex,loopi[3]
dbne a,loopo [3]pulc [3]pula[3]pulx [3]rts [5]
[X]=cycles
Total Cycles = (A) * [ (X)*3 + 5] + 20
Note:Needtoknowclockspeed ofCPU– here,assumeitis8MHz,i.e.,eachcycle is125ns
8000 = (1) * [ (X)*3 + 5] + 20
Here,X~265810
Set(A)=1ms
Set(A)=100ms
800,000 = (100) * [ (X)*3 + 5] + 20
Here,X~266410z
2661
[1.D]-8
N equ ?
org $800macme
pshb ; (2)pshx ; (2)pshy ; (2)ldx #XA ; (2)ldy #YA ; (2)movw #0,ACM ; (5)movw #0,ACM+2 ; (5)ldab #N ; (1)
loopemacs ACM ; (13)leax 2,x ; (2)leay 2,y ; (2)dbne b,loop ; (3)
puly ; (3)pulx ; (3)pulb ; (3)rts ; (5)
ACM rmb 4XA fdb ?,?,?YA fdb ?,?,?
SomeTimingAnalysis
1. If N=0, the total number of cycles consumed by “macme” is:
A: 55B: 235C: 5135D: 5155 PE: None of these
1. If N=0, the total number of cycles consumed by “macme” is:
A: 55B: 235C: 5135D: 5155E: None of these
[1.D]-9
N equ ?
org $800macme
pshb ; (2)pshx ; (2)pshy ; (2)ldx #XA ; (2)ldy #YA ; (2)movw #0,ACM ; (5)movw #0,ACM+2 ; (5)ldab #N ; (1)
loopemacs ACM ; (13)leax 2,x ; (2)leay 2,y ; (2)dbne b,loop ; (3)
puly ; (3)pulx ; (3)pulb ; (3)rts ; (5)
ACM rmb 4XA fdb ?,?,?YA fdb ?,?,?
SomeTimingAnalysis
1. If N=1, the total number of cycles consumed by “macme” is:
A: 55 PB: 235C: 5135D: 5155E: None of these
2. If N=1, the total number of cycles consumed by “macme” is:
A: 55B: 235C: 5135D: 5155E: None of these
[1.D]-10
N equ ?
org $800macme
pshb ; (2)pshx ; (2)pshy ; (2)ldx #XA ; (2)ldy #YA ; (2)movw #0,ACM ; (5)movw #0,ACM+2 ; (5)ldab #N ; (1)
loopemacs ACM ; (13)leax 2,x ; (2)leay 2,y ; (2)dbne b,loop ; (3)
puly ; (3)pulx ; (3)pulb ; (3)rts ; (5)
ACM rmb 4XA fdb ?,?,?YA fdb ?,?,?
SomeTimingAnalysis
1. If N=10, the total number of cycles consumed by “macme” is:
A: 55B: 235 PC: 5135D: 5155E: None of these
3. If N=10, the total number of cycles consumed by “macme” is:
A: 55B: 235C: 5135D: 5155E: None of these
[1.D]-11
N equ ?
org $800macme
pshb ; (2)pshx ; (2)pshy ; (2)ldx #XA ; (2)ldy #YA ; (2)movw #0,ACM ; (5)movw #0,ACM+2 ; (5)ldab #N ; (1)
loopemacs ACM ; (13)leax 2,x ; (2)leay 2,y ; (2)dbne b,loop ; (3)
puly ; (3)pulx ; (3)pulb ; (3)rts ; (5)
ACM rmb 4XA fdb ?,?,?YA fdb ?,?,?
SomeTimingAnalysis
4. If N=255, the total number of cycles consumed by “macme” is:
A: 55B: 235C: 5135 PD: 5155E: None of these
4. If N=255, the total number of cycles consumed by “macme” is:
A: 55B: 235C: 5135D: 5155E: None of these
[1.D]-12
Extended-Precision(EP)BinaryAdd
• Motivation:Wanttoaddnumbersthatarebiggerthan16-bits• How?
• Assume2largevaluesareinmemory• UseCFtopropagatethecarryof8-bitadds
• SortoflikebuildinglargeaddersinHWfromsmalladders• Storepointerstothe2numbers
• augend(topnumber),addend(bottomnumber)• Useautoincrement/decrementpointerstomoveadditionalong
[1.D]-13
EPBinaryAdd
14
RegistersValueName
Y 100t
X 50t
1t
2t
ValueAddr
50t
51t
100t
Memory
101t 20t
10t
(augend) ¬ (augend) + (addend)
Storingresultoveroneoftheinputs
(augend)¬ (X)+(addend)¬ (Y)---------------(result)¬ (X)
A 0t
1 ldaa 0,x
1t
2 adca 1,y+11t
51t
101t
3 staa 1,x+
11t
Repeatforsizeofnumbers
X
Y
[1.D]-14
CodeEPBinaryAdd
[1.D]-15
OurEPExamples
addnInput:Assumesmemory
labelspointtoinputs
Ineffect,therereallyisnoargumentpassingtothis
function
Output:Nothingexplicitlypassedout,
updatesmemoryatlabel
[1.D]-16
CodeEPBinarySubtract
sbca
[1.D]-17
CodeEPDecimalAdd
daa
[1.D]-18
EPDecimalSubtract
• Q:Isthisjustaseasy? No:Thereisnodas(decimaladjustsubtraction)
• Sohow? Usedaa,takethe10’scomplimentofsubtrahendfirst(flippingitssign).Thendonormaladca +daa.
[ 99BCD – (subtrahend)BCD + 1]
radix (10’s) complement of subtrahend
DAA
(minuend)BCD +(result)BCD ¬
[1.D]-19
EPDecimalSubtract
Add1togetradixcomplementandpropagatecarry
[1.D]-20