Файл: Single- and Multi-Chip Microcontroller Interfacing For the Motorola 68HC12 (G.J. Lipovski, 1999).pdf

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 15.06.2025

Просмотров: 784

Скачиваний: 2

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

1.2 The 6812 Instruction Set

The displacement used in the branch instruction, the last instruction in the program, is shown as xx. It can be determined as follows. When the branch is executed, the program counter has the value $20D, and we want to jump back to location $202. The difference, $20D - $202, is $OB, so the displacement should be -$OB. A safe way to calculate the displacement is to convert to binary, negate, and then convert to hexadecimal. $OB is 00001011, so the 2's complement negative is 11110101. In hexadecimal, this is SF5. That is not hard to see, but binary arithmetic gets rather tedious. A faster way takes the 16's complement of the hexadecimal number. Just subtract each digit from $F (15), digit by digit, then add 1 to the whole thing. -$OB is then ($F - 0),($F - B) + 1 or $F4 + 1, which is $F5. That's pretty easy, isn't it!

A branch instruction may direct the person in the analogy to branch, for instance, only if the number in the adder is positive. If that number is not positive, the next instruction is fetched and executed because the left hand is not moved. This is a conditional branch. The 6812 has only conditional branch instructions, rather than conditional jumps or conditional subroutine calls and conditional subroutine returns (as does the 8080). The conditional branch tests one or more condition codes, then branches to another location specified by the displacement if the condition is true, using relative addressing. For each conditional branch, which uses an 8-bit offset, there is also a conditional long branch, which uses a 16-bit offset. The instruction

BCC L

branches to location L if the carry bit is cleared, otherwise the instruction does nothing. A set of simple branches test any one of the condition codes, branching if the bit is

set or clear. For example, BCC L will branch to location L if the carry bit is clear, while BCS L will branch there if the carry bit is set. Other sets test combinations of condition codes (the Z, N, and V bits) that indicate 2's complement inequalities. The last set tests combinations of the Z and C bits that indicate unsigned number inequalities. Column 2 of Table 1.6 tests each condition code separately. The BMIand BPLinstructions check the sign bit and should be used after LDAA, STAA, and TST (or equivalent) to check the sign of a 2's complement number that was moved. The BCC and BCSinstructions test the carry bit, which indicates an overflow after adding unsigned numbers, or the bit shifted out after a shift instruction. The BVS and BVCinstruction set tests the V condition code, set if an overflow occurs on adding 2's complement numbers. The Z bit is also tested easily, but since we often compare two numbers to set the Z bit if the two numbers are equal, the instruction is called BEQand the complementary instruction is BNE. BEQ and BNE are also used in the 2's complement and unsigned number branches discussed next.

A 2's complement overflow will occur if the two numbers being added have the same sign and the result has a different sign. Have you ever added two positive numbers and gotten a negative number? That's an overflow. Or if you add two negative numbers and get a positive number, that too is an overflow. But if you add two numbers of different signs, an overflow cannot occur. In using these condition codes in branch instructions, we must be careful to test the carry bit, not the overflow bit, after an unsigned binary add, since the carry bit is set if an unsigned overflow occurs; and we must remember to test the overflow bit V after a 2's complement add, because it is set if the result is erroneous as a 2's complement number. The branches listed in the middle

30

Chapter 1

Microcomputer Architecture

I/O registers

in memory and locations in other programs

to jump or branch to. In an

EQU'S expression, the asterisk (*) is often used to indicate the current location counter. The ORG, DS . B, and EQUdirectives determine where areas of data are to be put but

do not fill those areas with initial values. The following directives not only provide room for variables but also initialize them with constants when the program is loaded.

The define constant (byte) directive DC. B will put a byte in memory for each operand of the directive. The value of an operand is put into memory when the location counter specifies the address and the location counter is incremented for each operand. DC. B 10 will put $0A in a word in memory. The directive

L: DC.B 1 , 2,3

will initialize 3 bytes in memory to be

01

02

03

and will tell the assembler that L is the symbolic address of the first word, whose initial value is $01. The location counter is incremented three times. ASCII characters can be inserted as DC.B arguments by putting them between matching quotes. Define constant (word) DC.Wwill initialize two consecutive 16-bit words for each argument. The value of each operand is put in two consecutive words and the location counter is incremented by two for each operand. For example, the directive

L: DC.W 1 , 2,3

will initialize six consecutive bytes in memory, as follows

00

01

00

02

00

03

and will tell the assembler that L is the address of the first word in this area, whose value is $00. The location counter is incremented six times. The DC .Wdirective is especially useful in putting addresses in memory so that they can be used in indirect addressing or picked up into an index register. If ALPHA is $100 because an EQUdirective set it to that value or because it is a label of an instruction or directive like DC. B that begins at location $100, then the directive

DC.W ALPHA

will generate the following 2 bytes in memory:

01

00



38

Chapter 1 Microcomputer Architecture

Problems

Problems 1 through 3 in this chapter and many problems in later chapters are paragraph correction problems. We use thefollowing guidelines for all these problems.

These paragraph correction problems have been proven useful in helping students understand concepts and definitions. Theparagraph in eachproblem has some correctand some erroneous sentences. Your task is to rewrite the paragraph so the wholeparagraph is correct, deleting any sentences that do not fit into the paragraph's theme. However, if a sentence is correct, you should not change it, and you cannot use the word "not "or its equivalent to correct the sentence. Consider the first sentence in problem 1: "The architecture is the block diagram of a computer." This is incorrect. It can be made correct by changing "architecture"to "organization,"or by changing "block diagram"to either "programmer's view"or "instructionset and I/O connection capabilities."Any of these corrections would be acceptable. The second sentence is correct, however, and should not be rewritten. Try to complete the problems without referring to the chapter, then check your answers by looking up the definitions. If you get a couple of sentences wrong, you're doing fine. But if you have more trouble, you should reread the sections the problem covers.

1. * The architecture is the block diagram of a computer. Von Neumann invented the architecture used on microcomputers. In it, the controller is analogous to the adding machine. We recall words from primary memory into the controller using the program counter (left hand). Symbolic addresses are used in assembly languages to represent locations in this memory. A macro is a program in another part of memory that is called by a program, so that when the macro is done, the calling program resumes execution at an instruction below the jump to macro. An I/O interrupt is like a subroutine that is requested by an I/O device. The latency time is the time needed to completely execute an interrupt. To optimize the speed of execution, choose a computer with good static efficiency. A microcomputer is a controller and data operator on a single LSI chip, or on a few LSI chips.

2, * Addressing modes are especially important because they affect the efficiency of the most common class of instructions, the arithmetic class. Direct addressing has the operand data in a part of the instruction called the displacement, and the displacement would be 8 bits long for an instruction using it to load an 8-bit accumulator. Indirect addressing allows programs to be position independent. The 6812 has direct page addressing, which is a "quick-and-dirty" index-addressing mode. Index addressing is especially useful for jumping to nearby locations. If we want to move data around in memory during execution of a program, indirect addressing is the only mechanism that can efficiently access single words as well as arrays.

40

Chapter 1 Microcomputer Architecture

8. Suppose a memory is filled, except for the program that follows, like this: the word at address $WXYZ is $YZ (for example, location $2538 has value $38).Assumingthat an address in X never points to the program, what will the value of X be after each instruction is executed in thisprogram?

LDX #$1

LDX 0,X

LDX 4,X

9. Suppose the condition code register is clear and the ADDA APLHA instruction is executed. Give the value in the condition code register if

a.Accumulator A is $77, ALPHA is$77.

b.Accumulator A is $C8, ALPHA is$77.

c.Accumulator A is $8C, ALPHA is$C8.

d.Repeat part c for SUBA ALPHA.

10.Explain under what conditions the H, N, Z, V, and C bits in the condition code register are set.Also explain the difference between overflow and carry.

11.If the accumulator Acontains the value $59 and ALPHA contains the value $6C,what will be the value of the condition code register after the following instructions? (Assume the condition code register is clear before each instruction.)

a.ADDA ALPHA

b. SUBA ALPHA

C, TSTA

d.COMA

e.BITA ALPHA f.EORA ALPHA

12.Repeat problem 11, assuming that the accumulator Acontains the value $C9, ALPHA contains the value $59,and the condition code register is set to the value $FF before each instruction.

13.Give the shortest 6812 instruction sequences that perform the same operation as the following nonexistent 16-bit 6812instructions. State whether the condition codes are set properly ornot.

a. ASRD (shift Dright arithmetically) c. NEGD (negate accumulator D)

b. INCD ALPHA (increment 16-bit ALPHA) d. DECD ALPHA (decrement 16-bit ALPHA)

e.MULS (multiply signed 8-bit A times 8-bit B to get 16-bit result in D)

14.Give the shortest 6812 instruction sequences to implement 32-bit arithmetic operations for each case given below. In each case, the data arrive in register Y(high 16 bits) and register D(low 16 bits), and are returned in the same way.


Problems

41

a. Complement b. Increment

c. Decrement

d. Shift right logical

e. Shift right arithmetic

f. Shift left (arithmetic or logical)

15. Explain, in terms of condition code bits, when the branch is taken for the following conditional branchinstructions:

a. BEQ

b.BGT C. BHI

d.BHS C. BLE

f.BPL

16.How many times does the following loop get repeated when the instruction CND is

a.BNE?

b.BPL?

C. BLT?

LDAA #200

LOOP : statement list

DECA

CND

LOOP

Show calculationsor explain your answers.

17. What is the value of accumulator B after the following program ends, when the instruction COND is

a.BEQ?

b.BMI?

C,BGT?

d. BVS?

LDAA #200

CLRB

LOOP: DECA

COND EXIT

INCB

BRA LOOP

EXIT: SWI

Show calculations or explain answers.

18. Convert the following high-level programming language construct into the shortest 6812 assembly-language instructions, assuming that the variable A is already assigned to the accumulator A. As long as the expression in the while statement is true, the statements inside braces are repeated.


2

Programming Microcomputers

We now consider programming techniques used in I/O interfacing. The interface designer must know a lot about them. As the industry matures, the problems of matching voltage levels and timing requirements, discussed in §3.2, are being solved by better-designed chips, but the chips are getting more complex, requiring interface designers to write more software to control them.

The state-of-the-art 6812 clearly illustrates the need for programming I/O devices in a high-level language as well as for programming them in object-oriented languages. The dozen I/O ports and its plethora of SPI, SCI, A-to-D, and timer ports may be a challenge to many assembler language programmers. But the 4K-byte EEPROM memory is large enough to support high-level language programs. Also, object-oriented features like modularity, information hiding, and inheritance will further simplify the task of controlling 6812 systems.

This book develops C and C++ interfacing techniques. Chapter 1, describing the architecture of a microcomputer, has served well to introduce assembler language, although a bit more will be done in this chapter. We introduce C in this chapter. The simplest C programming constructs are introduced in the first section. The handling of data structures is briefly covered in the next section. Programming styles, including the writing of structured, modular, and object-oriented programming, will be introduced in the last section. Subroutines will be further studied as an introduction to programming style. The use of classes in C++ will be introduced at the end of this chapter. While this introduction is very elementary and rather incomplete, it is adequate for the discussion of interfacing hi this text. Clearly, these concepts must be well understood before we discuss and design those interfaces.

For this chapter, the reader should have programmed in some high-level language. From it, he or she should learn general fundamentals of programming in C or C++ to become capable of writing and debugging tens of statements with little difficulty, and should learn practices specifically applicable to the 6812 microprocessor. If you have covered this material in other courses or absorbed it from experience this chapter should bring it all together. You may pick up the material just by reading this condensed version. Others should get an idea of the amount of background needed to read the rest of the book.

45