ebook img

C for the Microprocessor Engineer PDF

433 Pages·1994·7.706 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview C for the Microprocessor Engineer

Contents Part I Target Processors 1 1 The6809Microprocessor: ItsHardware 2 1.1 Architecture 3 1.2 Outsidethe6809 6 1.3 MakingtheConnection 9 2 The6809Microprocessor: ItsSoftware 19 2.1 ItsInstructionSet 19 2.2 AddressModes 30 2.3 ExamplePrograms 41 3 The68000/8Microprocessor: ItsHardware 56 3.1 Insidethe68000/8 57 3.2 Outsidethe68000/8 64 3.3 MakingtheConnection 71 4 The68000/8Microprocessor: ItsSoftware 86 4.1 ItsInstructionSet 86 4.2 AddressModes 106 4.3 ExamplePrograms 114 5 Subroutines,ProceduresandFunctions 122 5.1 TheCall-ReturnMechanism 123 5.2 PassingParameters 129 6 InterruptsplusTrapsequalsExceptions 141 6.1 HardwareInitiatedInterrupts 143 6.2 InterruptsinSoftware 161 Part II C 167 7 SourcetoExecutableCode 168 7.1 TheAssemblyProcess 170 7.2 LinkingandLoading 178 7.3 TheHigh-LevelProcess 189 v vi Contents 8 NakedC 199 8.1 ATutorialIntroduction 200 8.2 VariablesandConstants 202 8.3 Operators,ExpressionsandStatements 213 8.4 ProgramFlowControl 224 9 MoreNakedC 236 9.1 Functions 236 9.2 ArraysandPointers 245 9.3 Structures 258 9.4 HeadersandLibraries 271 10ROMableC 278 10.1MixingAssemblyCodeandStartingUp 278 10.2ExceptionHandling 286 10.3InitializingVariables 291 10.4Portability 297 Part III Project in C 309 11Preliminaries 310 11.1Specification 313 11.2SystemDesign 315 12TheAnalogWorld 323 12.1Signals 323 12.2DigitaltoAnalogConversion 329 12.3AnalogtoDigitalConversion 337 13TheTargetMicrocomputer 345 13.16809–TargetHardware 345 13.268008–TargetHardware 350 14SoftwareinC 355 14.1DataStructureandProgram 355 14.26809–TargetCode 359 14.368008–TargetCode 370 15LookingForTrouble 383 15.1Simulation 384 15.2ResidentDiagnostics 397 15.3In-CircuitEmulation 408 16C'estlaFin 416 16.1Results 416 16.2MoreIdeas 420 Contents vii A AcronymsandAbbreviations 423 List of Figures 1.1 Internal6809/6309structure. 4 1.2 6809pinout. 7 1.3 Asnapshotofthe6809MPUreadingdatafromaperipheraldevice. 10 1.4 Sendingdatatotheoutsideworld. 11 1.5 Thestructureofasynchronouscommon-busmicrocomputer. 12 1.6 Anelementaryaddressdecodingscheme. 14 1.7 Asimplebyte-sizedoutputport. 15 1.8 Talkingtoa61162kbytestaticRAMchip. 15 1.9 Interfacinga6821PeripheralInterfaceAdaptertothe6809. 17 2.1 Postbyteforpushingandpulling. 20 2.2 Moving16-bitdataatònego'. 22 2.3 Stackingregistersinmemory. 23 2.4 16-bitbinarytodecimalstringconversion. 48 2.5 Evaluatingfactorialn. 51 2.6 Amemorymapofthefactorialprocess. 52 3.1 Internalstructurepfthe68000. 58 3.2 Internal68008structure. 63 3.3 68000and68008DILpackages. 65 3.4 MemoryOrganizationforthe68000. 67 3.5 Thestructureofanasynchronouscommon-busmicro-computer. 72 3.6 The68000/8Readcycle. 73 3.7 The68000/8Writecycle. 75 3.8 Asimpleaddressdecoderwithno-waitfeedbackcircuitry. 77 3.9 ADTACKgeneratorforslowdevices. 78 3.10 Asimpleword-sizedoutputport. 80 3.11 Interfacing6264RAMICstothe68000MPU. 81 3.12 FastEPROMinterface. 82 3.13 Interfacingthe68230PI/Ttothe68000'sbuses. 83 3.14 Interfacinga6821PeripheralInterfaceAdaptertothe68000. 84 4.1 Multiplemovestoandfrommemory. 90 4.2 Multipleprecisionaddition. 93 4.3 UsingDBcctoimplementaloopstructure. 101 4.4 Twoexamplesofmachinecoding. 114 5.1 Subroutinecalling. 124 5.2 SavingthereturnaddressontheStack. 126 5.3 ThestackwhenexecutingthecodeofTable5.3(b),viewedasword-oriented. 128 viii LISTOFFIGURES ix 5.4 TheStackcorrespondingtoTable5.6. 132 5.5 TheStackusedfortheBLOCK_COPYsubroutine. 134 5.6 The6809Systemstackorganizedbythearrayaveragingsubroutine. 136 5.7 The68000Systemstackorganizedbythearray-averagingsubroutine. 138 6.1 Detectingandmeasuringanasynchronousexternalevent. 142 6.2 Interruptlogicforthe6809and68000processors. 145 6.3 Usingapriorityencodertocompress7linesto3-linecode. 146 6.4 Howthe6809respondstoaninterruptrequest 149 6.5 Howthe68000respondstoaninterruptrequest 151 6.6 Usinganexternalinterruptflagtodrivealevel-sensitiveinterruptline. 153 6.7 Servicingfourperipheralswithoneinterrupt. 157 6.8 Externalinterrupthardwareforthe68000MPU. 158 7.1 Onionskinviewofthestepsleadingtoanexecutableprogram. 170 7.2 Assembly-levelmachinecodetranslation. 172 7.3 Assemblyenvironment. 188 7.4 Syntaxtreeforsum = (n+1) * n/2; 191 7.5 TheWhitesmithsCcompilerprocess. 194 8.1 StructureofCprograms. 203 8.2 Propertiesofsimpleobjecttypes. 204 8.3 BasicsetofCdatatypes. 205 8.4 Typepromotions. 222 8.5 Simple2-waydecisions. 224 8.6 Usingelse-iftomakeamulti-waydecision. 227 8.7 switch-casemulti-waydecision. 229 8.8 Loopconstructs. 231 9.1 LayoutofCprograms. 237 9.2 TheSystemstackasseenfromwithinpower(),lines21–38. 243 9.3 Arraystorageinmemory. 249 9.4 Asimplewrite-onlyportat0x9000. 255 9.5 Registerstructureofa6821PIA. 262 11.1 Atypicallong-persistencedisplay. 311 11.2 Characteristicscrollingdisplayofatime-compressedmemory. 312 11.3 Blockdiagramoftheelectrocardiographtimecompressedmemory. 316 11.4 Abroadoutlineofsystemdevelopment. 318 11.5 Fundamentalchip-leveldesign. 320 11.6 Acostversusproductioncomparison. 322 12.1 Thequantizationprocess. 325 12.2 Theanalog–digitalprocess. 328 12.3 Illustratingaliasing. 329 12.4 A4th-orderanti-aliasingfilter. 330 12.5 TheR-2RcurrentD/Aconverter. 331 12.6 ConversionrelationshipsforthenetworkofFig.12.5. 333 12.7 Areal-worldtransfercharacteristic. 334 x LISTOFFIGURES 12.8 TheAD7528dualD/Aconverter. 335 12.9 InterfacingtheAD7528toamicroprocessor. 336 12.10A3-bitflashA/Dconverter. 338 12.11AsoftwarecontrolledsuccessiveapproximationD/Aconverter. 339 12.12FunctionaldiagramoftheAD7576A/Dconverter. 340 12.13InterfacingtheAD7576toamicroprocessor. 342 12.14Apertureerror. 343 13.1 The6809-basedembeddedmicroprocessorimplementation. 347 13.2 APAL-based6809addressdecoderimplementation. 349 13.3 The68008-basedembeddedmicroprocessorimplementation. 352 13.4 APAL-based68008addressdecoderimplementation. 353 14.1 Datastoredasacirculararray. 356 15.1 Tracingfunctionsum_of_n(). 392 15.2 Illustratingthefunctionpathinreachingline27. 393 15.3 Simulatingthetime-compressedmemorysoftware. 394 15.4 Simulatinganinterruptentryintoupdate(). 395 15.5 Mixed-modesimulationusingXRAY68K. 396 15.6 Free-runningyourmicroprocessor. 398 15.7 Onefree-runcycle,showingRAM,A/DandDIG_O/PEnables. 399 15.8 Theoutput_test()traces. 404 15.9 AtypicalPC-basedICEconfiguration. 410 16.1 TypicalXandYwaveforms,showingtwoECGtracescovering2s. 420 List of Tables 2.1 Moveinstructions. 21 2.2 Arithmeticoperations 24 2.3 ShiftingInstructions. 26 2.4 Logicinstructions. 27 2.5 Datatestoperations. 28 2.6 OperationswhichaffecttheProgramCounter. 29 2.7 TheM6809instructionset 33 2.8 Initializinga256-bytearray. 34 2.9 Sourcecodeforsumofnintegersprogram. 45 2.10ObjectcodegeneratedfromTable2.9. 46 2.11Asuperiorimplementation. 47 2.1216-bitbinarytoanequivalentASCII-codeddecimalstring. 49 2.13Fundamentalfactorial-ncode. 53 2.14Factorialusingalook-uptable. 54 4.1 Moveinstructions. 88 4.2 Arithmeticoperations. 91 4.3 Shiftinginstructions. 95 4.4 LogicInstructions. 97 4.5 Bit-levelinstructions. 98 4.6 Datatestinginstructions. 99 4.7 InstructionswhichaffecttheProgramCounter. 100 4.8 Summaryof68000instructions. 105 4.9 Asummaryof68000addressmodes. 113 4.10Objectcodeforsumofnintegersprogram. 115 4.11Asuperiorimplementation. 116 4.12Binarytodecimalstringconversion. 118 4.13Mathematicalevaluationoffactorialn. 119 4.14Factorialusingalook-uptable. 120 5.1 Subroutineinstructions. 125 5.2 Asimplesubroutinegivingafixeddelayof100mswhencalled. 127 5.3 Transparent100msdelaysubroutine. 129 5.4 Usingaregistertopassthedelayparameter. 130 5.5 Usingastaticmemorylocationtopassthedelayparameter. 131 5.6 Usingthestacktopassthedelayparameter. 132 5.7 Makingacopyofablockofdataofarbitrarylength. 133 5.8 Usingaframetoacquiretemporarydata;6809code. 137 5.9 UsingaFrametoacquiretemporarydata;68000code. 139 xi xii LISTOFTABLES 6.1 6809codedisplayingheartrateonanoscilloscope. 155 6.2 68000codedisplayingheartrateonanoscilloscope. 160 6.3 Exceptionrelatedinstructions. 162 7.1 Sourcecodefortheabsoluteassembler. 173 7.2 Atypicalerrorfile. 173 7.3 ListingfileproducedfromthesourcecodeinTable7.1. 174 7.4 SymbolfileproducedfromtheabsolutesourceofTable7.1. 174 7.5 Somecommonabsoluteobjectfileformats. 176 7.6 Asimplemacrocreatingthemodulusofthetargetoperand. 177 7.7 AssemblingtheDisplaymodulewiththeMicrotecRelocatableassembler. 181 7.8 Module2afterassembly. 183 7.9 Module3afterassembly. 184 7.10Linkingthethreesourcemodules. 185 7.11OutputfromtheMicroteclinker. 187 7.12ApossibleLexicalanalysisofsum = (n+1)*n/2; 190 7.136809targetcodeforsum = (n+1) * n/2; 193 7.14PassingasimpleprogramthroughthecompilerofFig.7.5. 197 8.1 Definitionoffunctionsum_of_n(). 200 8.2 Variablestorageclass 208 8.3 Initializingvariables. 210 8.4 Coperators,theirprecedenceandassociativity. 215 8.5 BitwiseANDandShiftoperations. 218 8.6 AnestedifReal-TimeClockinterruptserviceroutine. 225 8.7 Anelse-ifReal-TimeClockinterruptserviceroutine. 226 8.8 Generatingfactorialsusingtheelse-ifconstruct. 228 8.9 Generatingfactorialsusingtheswitch-caseconstruct. 230 8.10Generatingfactorialsusingawhileloop. 232 8.11Generatingfactorialsusingaforloop. 234 9.1 TheCprogramasacollectionoffunctions. 240 9.2 Generatingfactorialsusingalook-uptable. 247 9.3 Alteringanarraywithafunction. 250 9.4 Sendingoutadigittoa7-segmentport. 256 9.5 Displayingandupdatingheartbeat. 260 9.6 ThePIAasastructureofpointers. 265 9.7 Sendingpointerstostructurestoafunction. 267 9.8 Unions. 270 9.9 Using#definefortextreplacement. 272 9.10Atypicalmath.hlibraryheader(withaddedcomments). 276 10.1Elementarystartupfora6809-basedsystem. 280 10.2Usingarraysofpointerstofunctionstoconstructavectortable. 281 10.3AsimpleStartup/Vectorroutinefora68000-basedsystem. 282 10.4AC-compatibleassemblerfunctionevaluatingthesquareroot. 283 10.5Usingin-lineassemblycodetosetuptheSystemstack. 284 10.6Callingaresidentfunctionataknownaddress. 286 10.76809startupforthesystemofTable9.5. 287 LISTOFTABLES xiii 10.868000startupforthesystemofTable9.5. 288 10.9clock()configuredasaninterruptfunction. 290 10.10AstartupfortheAzteccompilerinitializingstatics/globals. 294 10.11Atypicallod68kfiletoproduceanimageofinitializeddatainROM 295 10.12Astartupinitializingstatics/globalsandsettinguptheDPRforzeropage. 296 10.13Zero-pagestoragewiththeCosmic6809compiler. 297 10.14AportableCprogramusingANSIIlibraryI/Oroutines. 299 10.15CompilingthesamesourcewithaspectrumofCPUs. 303 10.16TailoringtheANSIII/Ofunctionstosuitanembeddedtarget. 305 12.1Quantizationparameters. 326 12.2CdriverforFig.12.11. 340 14.1ThefundamentalCcoding. 357 14.2Thehard_09.hheaderfile. 359 14.36809coderesultingfromTables14.1and14.2. 362 14.4The6809TimeCompressedMemoryStartup. 363 14.5Themachine-codefileforthe6809-basedtime-compressedmemory. 364 14.6The@portdirective. 365 14.7Using_asm()toterminateaNMI/IRQtypeinterruptservicefunction. 366 14.8Optimized6809code. 370 14.968000coderesultingfromTables14.1and14.2. 373 14.10The68000TimeCompressedMemoryStartup. 375 14.11Machine-codefilefromTables14.9and14.10. 376 14.12The@portdirective. 377 14.13Using_asm()toterminateaninterruptservicefunction. 378 14.14Optimized68000basedcode. 381 15.1SimulatingtheprogramofTable4.10. 386 15.2TracingtheprogramofTable2.9. 387 15.3TracingaCfunction. 389 15.4Areportonthevariablesusedinthe68008TCMsystemofTable15.5. 390 15.5Complete68008package,includingresidentdiagnostics. 403 15.6Codeforthe68008implementation. 407 15.7AnalternativeRAMtestingmoduleforthe6809system. 408 15.8MemoryMappingandTesting. 412 15.9AwindowintothehardwareusinganICE. 413 16.1A6809-basedassembly-levelcoding. 417 16.2A68008-basedassembly-levelcoding. 419 PART I Target Processors A major advantage of the use of a high-level language is its independence of the hardware its generated code will eventually run on; that is, its portability. However,oneofthemainstrandsofthisbookistheinteractionofsoftwarewith its hardware environment, and thus it is essential to use real products in both domains. For clarity, rather than describing a multitude of devices, most of the examples are based on just two microprocessors. Two, rather than one, not to loosesightoftheportabilityaspectsofhigh-levelcode. In this part I describe the Motorola 6809 and 68000/8 microprocessors, the chosendevices. Thisgivesusahardwaretargetspectrumrangingfrom8through 32-bitarchitecture. Asbothmicroprocessorsshareacommonancestor,thecom- plexityisreducedcomparedwithanon-relatedselection. Wherenecessary,other processors are used as examples, but in general the principles are similar irre- spective of target. If the hardware detail seems excessive to a reader with a softwarebackground,muchmaybeignoredifbuildingtheminiprojectcircuitry ofPart3istobeomitted.

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.