activate cces project ‐‐ mistakestoavoid
TRANSCRIPT
CCES Demo project buildLab 0 – ENCM515
and preparation for Assignment 1
M. Smith – August 2013
Familiarization Laboratory 0Essentially the same as ENCM511 Lab 0
This laboratory is designed to allow you to• Familiar with the CrossCore Embedded Studio, CCES, development environment
• Build a CCES project with C++ code that calls assembly functions and other C++ functions
• Run the code on a ADSP‐21469 (SHARC)• Most of these details are ‘one time only’ provided you go back to the same laboratory station (may be needed) and use a ‘workspace’ on your H: drive
2/40
Activate CCES• Make ENCM515Directory on your H: Drive• Activate CCES
Analog Devices CrossCore Embedded Studio• Browse to H:\ENCM515 directory – Get Welcome Pane
3
ENCM515
Plan a Project ‐‐MistakesToAvoid
• CCES Project to contain• Main driver program main( ) which SHOULD
– have (gasp) a print statement (only time allowed)– Calls Assembly code routine Level1ASM( ) that
• Calls another assembly code routine Level2ASM( ) which returns the number 6 * 6
• Call a C++ function Level2CPP( ) which returns the number 6
• And returns the sum of the returned values from Level2ASM( ) and Level2CPP( )
4
Plan a TestProject
– Automated test to show that Level2ASM( ) works– Automated test to show that Level2CPP( ) works– Automated test to show that Level1ASM( ) works
• Demonstrate – TestLastApproach (as part of Waterfall life‐cycle) and
– TestFirstApproach (as part of Agile / Lean life‐cycle)
5
Build a CCES project
Select NEW
Select CCES Project
Select ENCM515Demo
Click OKSet nameSelect ExecutableBrowse
6
MistakesToAvoid515
ENCM515
Build 2Select CCES roject
Next
ENCM511 ENCM515
Processor Family Blackfin SHARC
Tool chain CCES Blackfin CCES SHARC
Processor type ADSP‐BF533 ADSP‐21469
Silicon revision 0.5 or 0.6 0.0 or any
or any
7
Build 3
Doing SHARC build
Next
8
Build 4 Select C++ then Finish
Project Pane
Click on src toExpand link
Tabs to 4 panes
9
Build 5 – Clicked on MistakesToAvoid.cpp
Code of main( )
Outline pane
Key details of main
Console pane active ‐‐ compile messages and ‘temporary system screen’ for board
Click Welcomeminimize button
10
Code a polite request
Fix error messages
Generated as CCESdoes syntax checking in background
Click Welcomemaximize button
11
Has deliberate code errors so you can see CCES behaviour
Build 7Syntax checker does not spot format statement error ******Click on project name in Explore pane ‐‐Mistakes to AvoidPress F7 to build projectPress F5 to run project
12
Deliberate code errors so you can see CCES behaviour
Build 8 – Problem after F5CCES does not know where to put the code
Right click here and selectRun asRun configurations
13
Build 9
Click this
Select 21469 then configurator
Select 21469 then copy
Change name to 21469 (515)then OK
14
Build 10
Click 515 name then Test
Click Start
Click Exit
15
Build 11 – Once done, no need to repeat if come back to same lab station
Click OKThen Next
16
Build 12
Click EmulatorThen Next
Click 515Then Finish
Click ApplyThen Run
17
Unexpected request
• Think this is because I have 2 projects available in my project
• Just clicked yes and the system loaded the executable – but never ran it
• So I clicked on MistakesToAvoid515 project name, typed F7 then F5 and selected 515 stuff and the system ran
18/40
Final result
VERY VERY VERY IMPCheck program finish
Printing with Some garbage Fix code so prints value of WHICHSHARC 19
Fix errors and Rebuild
Click here – then F7 (build)
Say yes to get code to run
20
Had to click F5 again to see this
Now add C++ call to Assembly codeFix error by adding prototype to .h file
Remains errorUntil save the .h file
21
Now add stub routine for Level1ASM( )
Right click SrcSelect NewSelect File from template
Type in name with extension .asmThen finish
22
Add in SHARC stub assembly code
Add in key features of the code.section/pm seg_pmco.global _functionName
_functionName: (colon)Return value (garbage)Return‐to‐C ‐‐ special
code_functionName.end: (colon)
Formatting is requirement for course
Useless output– don’t know if ASM worksor not
23
C++ code with ASM and CPPgenerated answers
24
Generate C++ version ofassembly code we want to generate
Cross‐Core editor gives 2 indications of errors if you bother to do a code review(The fact still there after I compiled the code mean I did not a code review)
25
Note this error appears not fixed till do F7 (rebuild) – CCES error reported
26
More intense ASM ‐‐ Cut and paste working C++ code as comments
27
Step N – do what you knowConvert Level2A_ASM, Level2B_ASMShould assemble,Link, run without crashing , but not give correct answer
Ran safely indicatorHow would you know I did not bother with code review before F7ing? 28
Complex ASM ‐‐ BlackfinAdd in working code as comments
Then simplify the C++ codeSo only one RISC operation (ROP) per line
Then apply ASM rulesReturn always in R0
So WIDFI
And the code goes into never‐never land(never returns like Peter Pan in that famous story)
29
Add break points to code to follow the code flow – F7 and then F5, F5
30
More F5, F5, F5, F5 (run, run, run)
Code just re‐executes RTS for ever
31
Understanding why
• CALL _Function– Save the address of the instruction after the CALL instruction (so code knows where to come back after the call). Jump and execute _Function
– Saved to the Blackfin register RETS with equivalent function as the MIPS link register
• RTS– Sent the processor (JUMP) to the instruction at the address stored in RETS and start executing
32
Add break points to code to follow the code flow – F7 and then F5, F5
CALL saves the address of this instruction’s address in RETS register
33
More F5, F5, F5, F5
Processor will now re‐execute RTS for ever
RTS uses the address inRETS register to jump to here
RTS uses the address inRETS register to jump to here ‐‐ OOPS
34
The link and unlink instructions fix the problem
LINK – remember the old RETS register value so RETS can be overwritten by the next CALL instruction
UNLINK – recover old RETS register value so it can be used by the next RTS instruction
35
Remember – LINK and UNLINK always occur as PEARS (sic) and never A LOAN (sic)‐‐ (Sic – means deliberately left incorrect – as in a bad pun)
Defect in SHARC code when do same thing (runs but with hidden problem)
Second call destroys the SHARC RETS equivalentBut since we use a special return‐to‐C return, then (by luck) the code works.It’s a DEFECT as this problem will come back to haunt us another day
36/40
CALL 2A returns R0which is then Overwritten byCALL 2B’s return
So save R0 from Level2A_ASM into another register – e.g. R1 ‐‐ Blackfin
• Works BY LUCKas can be seen bynext slide
37
You can’t save things in register R1
C++/ASM mixing rule
When you call a function you don’t know whether that function is using R0, R4, R8 or R12
Also SHARC C++ uses all other registers
38
R1 Value DESTROYED AFTER CA
Find out how C++ does it
Try rewriting C++ code more simply
39/40
Let’s discuss SHARC assembly code from rewritten C++ code
40/40
What happens if you need to call overloaded C++ from ASM?
• You are forgetting two key rules
• AGILE coding Rule 1 – KIS (Keep it simple)Why are you calling complex functions from ASM?
• First rule of Assembly language codingDON’T WRITE IN ASSEMBLY CODE
• Second rule of Assembly language codingWrite in assembly code when accessing hardware
• Third rule in Assembly language codingWrite in assembly code if C++ code is not fast enoughApply this rule in ENCM515
41
Plan a Project ‐‐MistakesToAvoid
• CCES Project to contain• Main driver program main( ) which SHOULD call
– with (gasp) a print statement– Assembly code routine Level1ASM( ) that
• Calls another assembly code routine Level2ASM( ) which returns the number 6 * 6
• Call a C++ function Level2CPP( ) which returns the number 6 • And returns the sum of the returned values from Level2ASM( ) and Level2CPP( )
42
Plan a TestProject (To come)
– Automated test to show that Level2ASM( ) works– Automated test to show that Level2CPP( ) works– Automated test to show that Level1ASM( ) works
• Demonstrate – TestLastApproach (as part of Waterfall life‐cycle) and
– TestFirstApproach (as part of Agile / Lean life‐cycle)
43