ECE 447: Lecture 16
description
Transcript of ECE 447: Lecture 16
ECE 447: Lecture 16
Common Errors
& Good Programming Style
Typical Assembly LanguageProgram Bugs
ECE 447: Typical Assembly Language Program Bugs
1. Improper transfer to subroutines
Correct: JSR, BSRIncorrect: JMP, BRA
2. Forgetting to initialize stack pointer
section .text
lds #stack_end-1
done by default by the startup code when you call your assembly language routine from the main() function written in C
section .bss rmb 128stack_end
ECE 447: Typical Assembly Language Program Bugs
3. Not allocating enough memory for the stack
data
stack
data
stack
programexecution
ECE 447: Typical Assembly Language Program Bugs
4. Unbalanced stack operations
immediately after JSR just before RTS
RTNRTNSP
SPvariables
ECE 447: Typical Assembly Language Program Bugs
5. Using subroutines that change registers
LDX #ADDRESS
JSR changer
LDAA 0,X
Example:
6. Transposed registers
TBA vs. TAB
PSHAPSHBPULX
Examples:
instead ofPSHBPSHAPULX
ECE 447: Typical Assembly Language Program Bugs
7. Not initializing pointer register
8. Not initializing registers and data areas
section .bssvar1 rmb 2
LDAA 0,X
section .text
LDD var1
Example:
Example:
ECE 447: Typical Assembly Language Program Bugs
9. Inadvertent modification of the condition code register
CPX #end_addressLDD resultBNE start
Examples: CLCstart LDAA 0,X ADCA 0,Y STAA 0,X INX CPX #end BNE start
modifies Z flag
modifies C flag
ECE 447: Typical Assembly Language Program Bugs
10. Using the wrong conditional branch instruction
BHI, BHSBLO, BLS
for unsigned numbers
Correct:
BGT, BGEBLT, BLE
for signed numbers
ECE 447: Branch Instructions (1)
REL N Z V C– – – –
after comparison register vs. memory
unsigned numbers signed numbers
BHI higher >BLO lower <
BHS higher or same BLS lower or same
BGT greater than >BLT less than <
BGE greater than or equal BLE less than or equal
BEQ equal =BNE not equal
ECE 447: Typical Assembly Language Program Bugs
11. Using the wrong addressing mode
Examples:
LDD INIT
INIT EQU 1 var1 fdb 5
LDD #var1
instead of
LDD #INIT
instead of
LDD var1
ECE 447: Typical Assembly Language Program Bugs
12. Using a 16-bit counter in memory
counter fdb 0,0
inc counter
Example:
increments only the more significantbyte of a 16-bit counter!
Good Programming Style
ECE 447: Do Not Use Magic Constants!
// set EGA
PIOC |= EGA;
// clear STAI
PIOC &= ~STAI;
PIOC |= 0x40;
PIOC &= 0xBF;
Bad style Good style
#include <hc11e9.h> #include <hc11e9.h>
ECE 447: Do Not Use Magic Constants!
Bad style Good style
#include <hc11e9.h> #include <hc11e9.h>
while (!(TFLG2 & TOF)) ;
while ((TFLG2 & 0x80)==0) ; while ((TFLG2 & TOF) == 0) ;
// do nothing while TOF equal to 0
or or
while (!(TFLG2 & 0x80)) ;
ECE 447: Do Not Use Magic Constants!
Bad style Good style
PIOC_STAI EQU $40PIOC_EGA EQU $20
// set EGA
LDAA PIOCORAA #EGASTAA PIOC
// clear STAI
LDAA PIOCANDA #~STAISTAA PIOC
PIOC EQU $1002
LDAA $1022ORAA #$02STAA $1022
LDAA $1022ANDA #$BFSTAA $1022
ECE 447: Initializing global variables
Bad practice Good practice
int counter = 0;char lookup[3];
int counter;
void main(void){ counter = 0;
}
const char lookup[] = {0xE6, 0xE4, 0xEA};const char string1 = “Menu:”;
main(){ lookup[0] = 0xE6; lookup[1] = 0xE4; lookup[2] = 0xEA;}
char string1[5]= {‘M’,’e’,’n’,’u’,’:’};
ECE 447: Clearing Timer Flags
TMSK 1 &= ~OC2F;Clears all flags in the samebyte except OC2F
or
TMSK1 |= OC2FClears all flags in the samebyte, including OC2F
Bad Good
#include <hc11e9.h> #include <hc11e9.h>
TMSK1 = OC2FClears only OC2F
ECE 447: Common errors regarding interrupts
C
ASM
• No __attribute__((interrupt)) in the declaration/prototype of an interrupt service routine
• Using RTS instead of RTI
• Using local auto variables to store information between interrupts• Calling an interrupt service routine in your program (allowed only for testing after commenting __attribute__((interrupt)) modifier)
ECE 447: Common errors regarding interrupts
C & ASM
• No CLI• No setting of the local interrupt enable flag• Waiting for a flag to be set INSIDE of an interrupt
ECE 447: Common errors regarding polling
C• Using if instead of while to wait for a flag to be set• No clearing the flag after it was set• No using of special procedure to clear the flag• Not disabling interrupts• Checking the wrong flag
ECE 447: Common errors regarding polling
ASMUsing too complicated procedure to check the flag
LOOP LDAA PIOC ANDA #STAF
CMPA #0 BEQ LOOP
LOOP TST PIOC BPL LOOP
Bad style Good style
ECE 447: Use but not abuse comments
LDAA #INIT ; load accumulator A with ; the constant INITSTAA COUNTER ; store accumulator A to the variable COUNTER
Useless and repetitive
Effective
; initialize COUNTER to INITLDAA #INITSTAA COUNTER
VHDL: - writing synthesizable RTL level code in VHDL - writing test benches
FPGAs: - architecture of FPGA devices - tools for the computer-aided design with FPGAs - current FPGA families & future trends
Topics
ECE 448, FPGA and ASIC Design with VHDL
Applications: - basics of computer arithmetic - applications from communications, cryptography, digital signal processing, bioengineering, etc.
- FPGA boards- microprocessor board–FPGA board interfaces: PCI, PCI-X, PCIe, JTAG, USB- reconfigurable computers
High-level ASIC Design: - standard cell implementation approach - logic synthesis tools - differences between FPGA & standard-cell ASIC design flow
New trends:- using high-level programming languages to design hardware- microprocessors embedded in FPGAs
Platforms:
Tasks of the course
Advancedcourse on digital
system designwith VHDL
Comprehensive introduction to
FPGA & front-end ASIC
technology
Testing equipment
- writing VHDL code for synthesis- design using finite state machines and algorithmic state machines- test benches
- hardware:Xilinx FPGAs,TSMC libraryof standard ASICcells
- software:VHDL simulatorsSynthesis toolsXilinx ISE
- oscilloscopes- logic analyzer
Celoxica RC10 Educational Board
Digital system design technologiescoverage in the CpE & EE programs at GMU
Microprocessors ASICsFPGAs
ECE 445
ECE 447
ECE 586
ECE 681
ECE 448
ECE 511
ECE 611
ECE 431Computer Organization
Single ChipMicrocomputers
FPGA and ASIC Design with VHDL
Digital Circuit Design
Microprocessors
Advanced Microprocessors
Digital Integrated Circuits
VLSI Design Automation
ECE 545 Introduction to VHDL
ECE 645 Computer Arithmetic