Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Introduction to Embedded Microcomputer Systems Lecture 3.1 Appdx 1. Embedded system development using TExaS Assembler Source code PORTA equ DDRA equ org cnt rmb org main lds movb off bclr look ldd std loop ldaa anda cmpa bne ldx dex stx bne bset bra key fcb org fdb $0000 $0002 $0800 2 $F000 #$0C00 #$80,DDRA PORTA,#$80 #4444 cnt PORTA #$7F key off cnt cnt loop PORTA,#$80 look %00100011 $FFFE main Loader Microcomputer processor Object code $F000 $F003 $F008 $F00B $F00E $F011 $F013 $F015 $F018 $F01A $F01D $F01E $F021 $F023 $F026 $F028 $FFFE CF0C00 180B800002 4D0080 CC115C 7C0800 9600 847F B1F028 26EE FE0800 09 7E0800 26EE 4C0080 20E3 23 F000 RAM EPROM I/O External circuits and devices Figure 4.1. Assembly language development process. Editor Source code PORTA equ DDRA equ org cnt rmb org main lds movb off bclr look ldd std loop ldaa anda cmpa bne ldx dex stx bne bset bra key fcb org fdb Assembler $0000 $0002 $0800 2 $F000 #$0C00 #$80,DDRA PORTA,#$80 #4444 cnt PORTA #$7F key off cnt cnt loop PORTA,#$80 look %00100011 $FFFE main TExaS Simulated Loader Microcomputer processor Object code $F000 $F003 $F008 $F00B $F00E $F011 $F013 $F015 $F018 $F01A $F01D $F01E $F021 $F023 $F026 $F028 $FFFE CF0C00 180B800002 4D0080 CC115C 7C0800 9600 847F B1F028 26EE FE0800 09 7E0800 26EE 4C0080 20E3 23 F000 RAM ROM I/O Simulated External circuits and devices Figure 4.2. Assembly language development using TExaS. Mark W. Welker (From Jonathan W. Valvano) Introduction to Embedded Microcomputer Systems Lecture 3.2 A1.5. Tutorial A1. Getting started The purpose of this tutorial is to introduce the first time user to TExaS. 1) how to launch the simulator, 2) how to modify input switches, 3) how to edit, assemble, and run a 6812 program, 4) how to modify display format in the ViewBox, 5) how to move and resize windows, 6) how to get on-line help. visualize four places information can be stored on a computer. 1) external switches are input devices that hold information, 2) registers are high-speed temporary storage inside the processor, 3) global variables can hold information that is easy to access, 4) external LEDs are output devices that hold information. Action: Install and upgrade the TExaS application. www.ece.utexas.edu/~valvano Action: Copy the tutorial programs onto a hard drive. copy the Mc6812T subdirectory from the CD onto a hard drive. Action: How to launch the TExaS application. Figure A1.9. TExaS application icon. Question A1.1. List the names of the six files that are now open. Action: How to assemble a program. Mark W. Welker (From Jonathan W. Valvano) Introduction to Embedded Microcomputer Systems Lecture 3.3 Execute the command Assemble->Assemble, Question A1.2. What happened to the colors of the Start, ExecuteOne, and StepOver tools in the tool bar after the program was assembled? Why? Action: How to modify input switches. Question A1.3. Describe the relationship between the switch position (open or closed) and 1) the voltage across the switch (probe near the switch) 2) the voltage at the output of the 74HC14 not gate 3) the digital value of the input port Action: Observing the details of execution. TheList.rtf will highlight the current instruction details of the simulation are dumped into TheLog.rtf. Question A1.4. Explain the behavior of each of the following modes. Mode->FollowPC Mode->CycleView Mode->InstructionView Mode->LogRecord Observation: The simulation speed (real human time to run your program) is greatly improved by turning these four modes off. Question A1.5. Use the help system to answer this question, “Into which file are the parameters specified by the Mode menu commands (e.g., FollowPC CycleView InstructionView LogRecord) saved?” In particular, which file do you save when you want to remember these settings? To answer this question, execute Help->HelpTopics, double-click Menus, then double-click Mode menu. Action: Backdump. Debugging Tip: Execute the Backdump command after your software crashes. Action: Setting breakpoints. type the absolute address, and push the Add button. type the symbolic name, and push the Add button. Mark W. Welker (From Jonathan W. Valvano) Introduction to Embedded Microcomputer Systems Lecture 3.4 left click in the TheList.rtf, then right click and hold. Add a breakpoint on PORTC, and run the program. Action: Rearranging the windows. TheList.rtf ChapA1.uc and ChapA1.io. Question A1.6. What is the program counter, PC? How is its initial value (right after reset) established? Question A1.7. What does the value PORTC=$0A in the ViewBox window mean? Action: Changing the format of the ViewBox data. Single step (tool bar or F10) the program until the value in PORTB just changes to $0A. To change the format of a ViewBox entry 1) Select the entry Click on the PORTC=$0A line in the ViewBox 2) Give the new format Type d in the Format box 3) Enter the change Type <Enter> or click on the Enter button Question A1.8. Using this procedure, click on the PC and give the values for the following formats H 16-bit unsigned hexadecimal ($F00F on the 6812) +H 16-bit signed hexadecimal (+H and -H are the same format) D 16-bit unsigned decimal +D 16-bit signed decimal (+D and -D are the same format) B 16-bit unsigned binary +B 16-bit signed binary (+B and -B are the same format) Using this procedure, click on the Data=$0A and give the values for the following formats h 8-bit unsigned hexadecimal (should be $0A) +h 8-bit signed hexadecimal (+h and -h are the same format) d 8-bit unsigned decimal +d 8-bit signed decimal (+d and -d are the same format) b 8-bit unsigned binary +b 8-bit signed binary (+b and -b are the same format) 2d two unsigned decimal numbers Mark W. Welker (From Jonathan W. Valvano) Introduction to Embedded Microcomputer Systems Lecture 3.5 Action: Changing the value in the ViewBox data. The current state of the microcomputer can be changed using this window. To change the value of a ViewBox entry 1) Select the entry Click on the A=$0A line in the ViewBox 2) Give the new value Type 100 in the Data box 3) Enter the change Type <Enter> or click on the Enter button To add new entries in the ViewBox (step 2 is optional) 1) Give the Address For the 6812 type the address $0900 2) Give the new value Type "TExaS" in the Data box (including the " ") 3) Enter the new format Type 6d in the Format box (try also s and 6c) Debugging Tip: Choose the formats in the ViewBox to simplify debugging. Debugging Tip: While debugging software adjust the parameters in the ViewBox to include important information and exclude unimportant information. Action: Simulating the microcomputer hardware/software. ChapA1.io and ChapA1.scp Observation: The simulation speed (real human time to run your program) is greatly improved by closing (not hide) scope windows. Mark W. Welker (From Jonathan W. Valvano) Introduction to Embedded Microcomputer Systems Lecture 3.6 2.5. Programming numbers in assembly language w is signed 8-bit -128 to +127 or unsigned 8-bit 0 to 255 n is signed 8-bit -128 to +127 u is unsigned 8-bit 0 to 255 W is signed 16-bit -32787 to +32767 or unsigned 16-bit 0 to 65535 N is signed 16-bit -32787 to +32767 U is unsigned 16-bit 0 to 65535 =[addr] 8-bit read from addr ={addr} 16-bit read from addr [addr]= 8-bit write to addr {addr}= 16-bit write to addr ldaa #w ldaa u ldaa U staa u staa U bra RegA=w RegA=[u] RegA=[U] [u]=RegA [U]=RegA U PC=U 6811/6812 RAM processor Reg A ROM Reg PC I/O port Figure 2.10. The ldaa Data instruction loads Mark W. Welker (From Jonathan W. Valvano) Introduction to Embedded Microcomputer Systems Lecture 3.7 6811/6812 RAM processor Reg A ROM Reg PC I/O port Figure 2.11. The staa PTT instruction stores 7 0 S XH I NZ V C 8 15 Register A Register B CC 8 bit condition code D two 8 bit accumulators X 16 bit index register Y 16 bit index register SP 16 bit stack pointer PC 16 bit program counter ldaa #w ldaa u ldaa U staa u staa U bra RegA=w RegA=[u] RegA=[U] [u]=RegA [U]=RegA U PC=U DDRAD, DDRS, DDRM, DDRT, specify if corresponding pin 0 means input 1 means output *****start TExaS show Lab1.rtf**** Mark W. Welker (From Jonathan W. Valvano) Introduction to Embedded Microcomputer Systems Lecture 3.8 2.6. Logical operations A B A&B A|B 0 0 0 0 0 1 0 1 1 0 0 1 1 1 1 1 Table 2.14. Logical operations. AND Gate A B A&B A^B 0 1 1 0 OR Gate A B 74HC08 A|B 74HC32 EOR Gate A^B A B NOT Gate A 74HC86 Figure 2.12. implemented with discrete digital gates. A ~A 0 1 1 0 Table 2.15. Logical complement. anda #w anda u anda U oraa #w oraa u oraa U eora #w eora u eora U coma RegA=RegA&w RegA=RegA&[u] RegA=RegA&[U] RegA=RegA|w RegA=RegA|[u] RegA=RegA|[U] RegA=RegA^w RegA=RegA^[u] RegA=RegA^[U] RegA=~RegA Mark W. Welker (From Jonathan W. Valvano) A 74HC04 Introduction to Embedded Microcomputer Systems Lecture 3.9 The and operation to extract, or mask, individual bits Pressed = PTS&0x01; +5V +5V C q 10k Input port PS 0 10k C p 74H C14 q 2 2 Input port PS 0 5F Figure 2.13. Interface of a switch. ldaa PTS read input Port S anda #$01 clear bits except bit 0 staa Pressed true iff PS0 is high a7 a6 a5 a4 a3 a2 a1 a0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 a0 value of PTS $01 constant result of the anda instruction The or operation to set bits 4 and 5 of the register DDRT. The other six bits of DDRT remain constant. Friendly software modifies just the bits that need to be. DDRT |= 0x30; /*PT4,PT5 outputs */ ldaa DDRT oraa #$30 staa DDRT read previous value set bits 4 and 5 update c7 c6 c5 c4 c3 c2 c1 c0 0 0 1 1 0 0 0 0 c7 c6 1 1 c3 c2 c1 c0 value of DDRT $30 constant result of the oraa instruction Mark W. Welker (From Jonathan W. Valvano) Introduction to Embedded Microcomputer Systems Lecture 3.10 Maintenance Tip: When interacting with just some of the bits of an I/O register it is better to modify just the bits of interest, leaving the other bits unchanged. In this way, the action of one piece of software does not undo the action of another piece. The exclusive or operation can also be used to toggle bits. PTM ^= 0x08; /* toggle PM3 */ ldaa PTM eora #$08 staa PTM read output Port M toggle bit 3 update b7 b6 b5 b4 b3 b2 b1 b0 0 0 0 0 1 0 0 0 b7 b6 b5 b4 ~b3 b2 b1 b0 value of PTM $08 constant result of the eora instruction The output of an open collector gate, drawn with the ‘x’, has two states low (0V) and HiZ (floating.) +5V C 2 20 O u t p u t po r t p P M3 q LE D 7 40 6 Figure 2.16. LED interface. Mark W. Welker (From Jonathan W. Valvano) Introduction to Embedded Microcomputer Systems Lecture 3.11 The and operation can be used to clear bits. PTM &= 0xF7; /* PM3 becomes 0 */ ldaa PTM anda #$F7 staa PTM read output Port M clear just bit 3 update b7 b6 b5 b4 b3 b2 b1 b0 1 1 1 1 0 1 1 1 b7 b6 b5 b4 0 b2 b1 b0 value of PTM $F7 constant result of the anda instruction Checkpoint 2.33: Write assembly code that clears bit 1 of Port M. Checkpoint 2.34: Write assembly code that sets bit 7 of Port M. Mark W. Welker (From Jonathan W. Valvano) Introduction to Embedded Microcomputer Systems Lecture 3.12 2.7. Shift operations LSR 0 C Figure 2.19. 8-bit logical shift right. ASR C Figure 2.21. 8-bit arithmetic shift right. LSL/ASL C 0 Figure 2.22. 8-bit shift left. ROR C ROL C Figure 2.23. 8-bit roll right and 8-bit roll left. asla lsla asra lsra rola rora RegA=RegA*2 RegA=RegA*2 RegA=RegA/2 RegA=RegA/2 roll left RegA roll right RegA Maintenance Tip: Use the asla instruction when manipulating signed numbers, and use the lsla instruction when shifting unsigned numbers. High and Low are unsigned 4-bit components, which will be combined into a single unsigned 8-bit Result. Result = (High<<4)|Low; Mark W. Welker (From Jonathan W. Valvano) Introduction to Embedded Microcomputer Systems Lecture 3.13 The assembly code for this operation is ldaa High read value of High lsla shift into position lsla lsla lsla oraa Low combine the two parts staa Result save answer Lab 1. Logic Function 74LS14 Microcontroller +5 V 5k S 22 switch PS0 CPU 10 Input Port 74LS14 +5 V 5k RAM PM0 ROM 22 switch 10 Output Port M +5 V 7405 LED T PT0 Simulated hardware circuit for Lab 1. The specific function you will implement is T M &S This means the LED will be on if and only if the M switch is not pressed and the S switch is pressed, as shown belwo TExaS IO window showing the door is unlocked. Mark W. Welker (From Jonathan W. Valvano) Introduction to Embedded Microcomputer Systems void main(void){ DDRM = 0x00; // make Port M an input, DDRS = 0x00; // make Port S an input, DDRT = 0xFF; // make Port T an output, while(1){ PTT = (~PTS)&PTM; // LED on iff PS0=0 and } The first C program to illustrate Lab 1. Lecture 3.14 PM0 is M PS0 is S PT0 is T PM0=1 Approach -> start with template ;****************** Lab1.RTF *************** ; The overall objective of this system is a digital lock ; Hardware connections ; PM0 is switch input M ; PS0 is switch input S ; PT0 is LED output T (on means unlocked) ; The specific operation of this system ; unlock if M is pressed and S is not pressed ; Your name and date ;I/O port definitions PTM equ $0250 ;Port M I/O Register PTS equ $0248 ;Port S I/O Register PTT equ $0240 ;Port T I/O Register DDRM equ $0252 ;Port M Data Direction Register DDRS equ $024A ;Port S Data Direction Register DDRT equ $0242 ;Port T Data Direction Register org $3800 ;2k RAM ($3800-$3FFF) ;Global variables (none required for this lab) org $4000 ;32k EEPROM ($4000-$7FFF,$C000-$FFFF) main ;Software performed once at the beginning loop ;Software repeated over and over bra loop org $FFFE fdb main ;Starting address end Mark W. Welker (From Jonathan W. Valvano) Introduction to Embedded Microcomputer Systems Lecture 3.15 2.8. Arithmetic operations Checkpoint 2.37: How many bits does it take to store the result of two unsigned 8bit numbers added together? Checkpoint 2.39: How many bits does it take to store the result of two unsigned 8bit numbers multiplied together? adda #w adda u adda U suba #w suba u suba U RegA=RegA+w RegA=RegA+[u] RegA=RegA+[U] RegA=RegA-w RegA=RegA-[u] RegA=RegA-[U] condition code register (CC or CCR) C set after an unsigned add if the answer is wrong V set a signed add if the answer is wrong bit name N negative Z zero V overflow C carry Table 2.18. Condition code bits. meaning after add or sub result is negative result is zero signed overflow unsigned overflow Mark W. Welker (From Jonathan W. Valvano) Introduction to Embedded Microcomputer Systems Lecture 3.16 96+64 224+64 +64 255 0 255 0 224 32 64 192 160 64 192 96 128 128 +64 Figure 2.26. Unsigned number wheel. 160-64 32-64 255 0 255 0 224 64 192 160 -64 32 64 192 96 128 128 -64 Figure 2.28. Unsigned number wheel. Observation: The carry bit, C, is set after an unsigned addition or subtraction when the result is incorrect. Mark W. Welker (From Jonathan W. Valvano) Introduction to Embedded Microcomputer Systems Lecture 3.17 -32+64 96+64 +64 -1 0 -1 0 -32 32 64 -64 64 -64 -96 -128 127 96 -128 127 +64 Figure 2.29. Signed number wheel. 32-64 -96-64 -64 -1 0 -1 0 -32 32 64 -64 64 -64 -96 -128 127 -64 96 -128 127 Figure 2.30. Signed number wheel Observation: The overflow bit, V, is set after a signed addition or subtraction when the result is incorrect. Mark W. Welker (From Jonathan W. Valvano) Introduction to Embedded Microcomputer Systems Lecture 3.18 Let the result R be the result of the addition A+B. N bit is set if unsigned result is above 127 or if signed result is negative. N = R7 Z bit is set if result is zero. Z= R7 & R6 & R5 & R4 & R3 & R2 & R1 & R0 V bit is set after a signed addition if result is incorrect V= A7 & B7 & R7 A7 & B7 & R7 C bit is set after an unsigned addition if result is incorrect C= A7 & B7 A7 & R7 B7 & R7 Checkpoint 2.41: Assume Register A is initially -100. After executing the instruction adda #64 what is the value in Register A, and the NZVC bits? Checkpoint 2.42: Assume Register A is initially -100. After executing the instruction adda #-64 what is the value in Register A, and the NZVC bits? Let the result R be the result of the subtraction A-B. N bit is set if unsigned result is above 127 or if signed result is negative. N = R7 Z bit is set if result is zero. Z= R7 & R6 & R5 & R4 & R3 & R2 & R1 & R0 V bit is set after a signed addition if result is incorrect V= A7 & B7 & R7 A7 & B7 & R7 Mark W. Welker (From Jonathan W. Valvano) Introduction to Embedded Microcomputer Systems Lecture 3.19 C bit is set after an unsigned addition if result is incorrect C= A7 & B7 B7 & R7 A7 & R7 Common Error: Ignoring overflow (signed or unsigned) can result in significant errors. Observation: Microcomputers have two sets of conditional branch instructions (if statements) that make program decisions based on either the C or V bit. Promotion involves increasing the precision of the input numbers, and performing the operation at that higher precision. decimal 8-bit 16-bit 224 1110,0000 0000,0000,1110,0000 + 64 +0100,0000 +0000,0000,0100,0000 288 0010,0000 0000,0001,0010,0000 We can check the 16-bit intermediate result to see if the answer will fit back into the 8-bit result. unsigned add unsigned sub promote A to A16 promote B to B16 promote A to A16 promote B to B16 R16=A16+B 16 R16=A16-B 16 ok R16 < 255 R16 overflow R16 >255 R=255 R=R16 end ok R16 > 0 R16 underflow R16 < 0 R=0 R=R16 end Figure 2.31.Promotion to detect and correct unsigned arithmetic errors. Mark W. Welker (From Jonathan W. Valvano) Introduction to Embedded Microcomputer Systems Lecture 3.20 To promote a signed number, we duplicate the sign bit decimal 8-bit 16-bit -96 1010,0000 1111,1111,1010,0000 - 64 -0100,0000 -0000,0000,0100,0000 -160 0110,0000 1111,1111,0110,0000 signed add signed sub promote A to A16 promote B to B16 promote A to A16 promote B to B 16 R16=A16+B 16 R16=A16-B 16 underflow R16 < -128 R16 R = -128 overflow R16 >127 underflow R16 < -128 R=127 R = -128 R16 overflow R16 >127 R=127 R=R16 R=R16 end end Figure 2.32. Flowcharts showing how to use promotion to detect and correct signed arithmetic errors. Common Error: Even though most C compilers automatically promote to a higher precision during the intermediate calculations, they do not check for overflow when demoting the result back to the original format. bcc bcs bvc bvs bpl bmi beq bne l1 l2 l3 l4 l5 l6 l7 l8 jump jump jump jump jump jump jump jump to to to to to to to to l1 l2 l3 l4 l5 l6 l7 l8 if if if if if if if if C=0 C=1 V=0 V=1 V=0 N=1 Z=0 Z=1 Mark W. Welker (From Jonathan W. Valvano) Introduction to Embedded Microcomputer Systems Lecture 3.21 ceiling and floor unsigned add R=A+B unsigned sub R=A-B C=1 C=1 C C=0 end C R=255 C=0 R=0 end Figure 2.33. Flowcharts showing how to use overflow bits to detect and correct unsigned arithmetic errors. Assume A8 B8 and R8 are three 8-bit (1-byte) global variables defined in RAM. A8 ds 1 Input B8 ds 1 Input R8 ds 1 Output The following assembly language adds two unsigned 8-bit numbers, using the algorithm presented in Figure 2.33. ldaa A8 get first input adda B8 A8+B8 bcc OK1 if C=0, then no error, ldaa #255 overflow OK1 staa R8 The following assembly language subtracts two unsigned 8-bit numbers. ldaa A8 get first parameter suba B8 A8-B8 bcc OK2 if C=0, then no error, ldaa #0 underflow OK2 staa R8 Mark W. Welker (From Jonathan W. Valvano)