Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B:...

20
Module 1-D Control Structure Applications Tim Rogers 2017 [1.D]-1

Transcript of Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B:...

Page 1: Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B: E: None of these [1.D]-10. N equ ? org $800 macme

Module1-DControlStructureApplications

TimRogers2017[1.D]-1

Page 2: Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B: E: None of these [1.D]-10. N equ ? org $800 macme

LearningOutcome#1

“Anabilitytoprogramamicrocontrollertoperformvarioustasks”

How?

A. ArchitectureandProgrammingModelB. InstructionSetOverviewC. AssemblyControlStructuresD. ControlStructureApplicationsE. ParameterPassingF. TableLookupG. MacrosandStructuredProgramming

[1.D]-2

Page 3: Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B: E: None of these [1.D]-10. N equ ? org $800 macme

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

Page 4: Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B: E: None of these [1.D]-10. N equ ? org $800 macme

3Simpleapps

1. SoftwareDelay2. Extended-precisionbinaryadd/subtract3. Extended-precisiondecimaladd/subtract

[1.D]-4

Page 5: Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B: E: None of these [1.D]-10. N equ ? org $800 macme

SoftwareDelay

• Purpose:Donothing,butforaspecificamountoftime• Fineifyouneedanimprecise delay:keyde-bouncingforinstance• Whydoesthisendupbeingimprecise?

delay_func

Input:(A)=<DelayTimeinms>

Output:nothingPassingarguments

viaregisters

Parameter-dependentoverheadInterrupts

[1.D]-5

Page 6: Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B: E: None of these [1.D]-10. N equ ? org $800 macme

SoftwareDelay• How(andthisisjustonewaytodothis):doubly-nestedloop

• Innerlooptakes1ms(willtimeitbasedonnumberofcycles/instruction)• Outerloopcontrols#timeswegothroughinnerloop

Onequestion:Howmanytimesdoweneedtogoaroundtheinnerloopgetto1ms?

[1.D]-6

Page 7: Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B: E: None of these [1.D]-10. N equ ? org $800 macme

Reminder

• Canfindtheexactcyclecountforeachinsn inreferencemanual

[1.D]-7

Page 8: Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B: E: None of these [1.D]-10. N equ ? org $800 macme

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

Page 9: Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B: E: None of these [1.D]-10. N equ ? org $800 macme

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

Page 10: Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B: E: None of these [1.D]-10. N equ ? org $800 macme

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

Page 11: Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B: E: None of these [1.D]-10. N equ ? org $800 macme

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

Page 12: Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B: E: None of these [1.D]-10. N equ ? org $800 macme

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

Page 13: Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B: E: None of these [1.D]-10. N equ ? org $800 macme

Extended-Precision(EP)BinaryAdd

• Motivation:Wanttoaddnumbersthatarebiggerthan16-bits• How?

• Assume2largevaluesareinmemory• UseCFtopropagatethecarryof8-bitadds

• SortoflikebuildinglargeaddersinHWfromsmalladders• Storepointerstothe2numbers

• augend(topnumber),addend(bottomnumber)• Useautoincrement/decrementpointerstomoveadditionalong

[1.D]-13

Page 14: Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B: E: None of these [1.D]-10. N equ ? org $800 macme

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

Page 15: Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B: E: None of these [1.D]-10. N equ ? org $800 macme

CodeEPBinaryAdd

[1.D]-15

Page 16: Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B: E: None of these [1.D]-10. N equ ? org $800 macme

OurEPExamples

addnInput:Assumesmemory

labelspointtoinputs

Ineffect,therereallyisnoargumentpassingtothis

function

Output:Nothingexplicitlypassedout,

updatesmemoryatlabel

[1.D]-16

Page 17: Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B: E: None of these [1.D]-10. N equ ? org $800 macme

CodeEPBinarySubtract

sbca

[1.D]-17

Page 18: Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B: E: None of these [1.D]-10. N equ ? org $800 macme

CodeEPDecimalAdd

daa

[1.D]-18

Page 19: Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B: E: None of these [1.D]-10. N equ ? org $800 macme

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

Page 20: Module 1-D - Purdue Engineering 1-D Control Structure Applications ... Software Delay •How ... B: E: None of these [1.D]-10. N equ ? org $800 macme

EPDecimalSubtract

Add1togetradixcomplementandpropagatecarry

[1.D]-20