# 18-348 Lab #2

Spring 2015

IMPORTANT: Lab 2 consists of two independent components (Lab 2 Part A and Lab 2 Part B). Start early! We suggest each of the two students in your lab group be "in charge" of one of the two parts of the lab parts to help divide the work. BUT, we expect both of you to understand and participate in the execution of both labs. And, as always, you must do the pre-labs independently. Please see the course web page for due dates.

Relevant lectures:
- Part A: Lecture 2. Embedded Hardware
- Part B: Lectures 3-4. Microcontroller Instruction Set.

Links to all files referenced in the lab and prelab can be found in the Files section at the end of this document.

Be sure to consult the Lab Writeup Checklist for both the prelab and lab writeup!

## Pre-Lab Part A:

Goal: To familiarize you with the operation of the LS374 flip-flop and understand how it can be integrated into a circuit.

Discussion: This lab focuses on the operation of the ICs used in Lab 2.

• Consider the following figures when answering the questions below.  Also, refer to the LS374 datasheet.  Since the datasheet is a combined 373/374 datasheet, be sure you are looking at correct tables.
• For the timing diagram, assume that all timing requirements (e.g., setup time) are met.
• For D7-0, the value for the entire word is shown.  For example, if the value is 0xF0, then D7, D6, D5, and D4 would be high and D3, D2, D1, and D0 would be low.

Figure 1:  Timing Diagram for Inputs to 74LS374

Figure 2:  Circuit Output Connections (In the resistor network, each resistor is 470 Ohms. Pin 2 provides D0 to bus, Pin 5 provides D1, etc. Note that 74LS374 labeling of "Q" outputs is in terms of bus bits and not per data sheet for this diagram.)

Procedure:

None. This section only involves answering questions.

Part A Questions:
A1.  (10 points) For Figure 1, list the value of 74LS374 outputs Q7-0 at each of the times T1-T5 (ignore T**).  When you record your answers, use the conventions described in the discussion section.  For example, if Q7 is high, and Q6:0 are low, then your answer would be "0x80" or "\$80" (either notation indicating the number is hexadecimal is acceptable).  The value Z must be used to indicate high-impedance/disabled output. Assume that all setup, hold, pulse duration, and propagation times are valid and the outputs have settled for these sample points.

 Time Q7:0 T1 T2 T3 T4 T5

A2.  (BONUS: 2 points) If T** occurs 1 ns after the falling edge of CLK reaches zero, and 27 ns after the falling edge of OC reaches zero, what is the value of Q7-0 at T** in Figure 1?  Why is it valid or invalid?  Limit your answer to 40 words. (Note: you only get credit if you have a correct answer to the "why" question. Look carefully at the data sheet!)

A3.  (10 points) Assume the 74LS374 output is connected as shown in Figure 2.  If the inputs described in Figure 1 are applied,  compute the total amount of power consumed by the LED/resistor network (not the LS374) at each time T1 through T5.  Assume the voltage drop across the LED is 1.7 volts.  Assume that VOL=0.35V for the LS374.  Assume that the output voltage is quiescent (i.e., it has had time to settle to a constant value).

 Time Power consumed T1 T2 T3 T4 T5

## Pre-Lab Part B:

Goal: To familiarize yourself with the microcontroller assembly instructions and the process of assembly.

Discussion: This lab focuses on the operation of the CPU core and assembling/dissassembling instructions.

• Compilation - C and other high-level programming languages must be compiled.  Compilation is the process of translating keywords and statements into assembly instructions which perform the functions described by the language.  Typically, a single statement will translate into multiple assembly instructions.
• Assembly - Unlike high-level languages, in assembly code, each assembly instruction corresponds to a single CPU instruction.  Assembly is the process of translating the assembly instructions into the binary machine code.
• Disassembly - This is the reverse process of assembly.  Binary machine code is translated into the corresponding assembly instructions.

Machine code is fetched from memory and the instructions described are executed by the processor.  This includes performing operations (shift, add, subtract, clear) on registers and memory locations and copying values from one location to another.

Procedure:
For each question below, we suggest that you do the work by hand and check it with an assembler.  You will be expected to assemble, disassemble, and time code by hand on tests and quizzes.

Part B Questions:

B1.  (10 pts) Translate the following assembly code into machine code.    The first instruction is done as an example. Do this by hand using the reference materials. It is OK to use CodeWarrior to check your work and make corrections after you have done it by hand.

 Assembly instruction Machine Code (hexadecimal values) LDAA PTT  ; read from port T B6 02 40 BITA #\$80   ; check MSB BEQ msbUnset ; branch to msbUnset msbSet: LDAB #1   ;write 1 to port M STAB PTM msbUnset: LDAA #0   ;write 0 to port M STAA PTM

B2.  (10 pts) Disassemble the following machine code.  Put a brief comment at the beginning which describes the overall function of the code.  Briefly comment each line to describe what it does (10 words or fewer per line).  For branch instructions, insert labels at the appropriate locations and use the label values in your disassembled assembly code. Do this by hand using the reference materials. It is OK to use CodeWarrior to check your work and make corrections after you have done it by hand.

 CE 00 00 08 7E 02 40 8E 00 0A 26 F7 20 FE

; overall description of the function of the code
______ ______ ;comment
_____:
______        ;comment
______ ______ ;comment
______ ______ ;comment
______ ______ ;comment
_____:
______ ______ ;comment

B3.  (10 pts) Determine the values of registers and flags during the following program.  For each instruction, fill in the values that will be present AFTER the instruction is executed.  If an instruction is not executed, fill in N/A for that row. Do this by hand using the reference materials. It is OK to use CodeWarrior to check your work and make corrections after you have done it by hand.

 Assembly Instructions Register D Register A Register B Flags Z N C Initial Values 0 0 0 0 0 0 Value DS.W 1             LDD #1 STD Value LDAA #255 LDAB #255 ADDD Value BCC cFlagClear cFlagSet:             CLRA cFlagClear:             BEQ zFlagSet zFlagClear:             CLRB zFlagSet:             LDAB #20 LDAA #10 SBA BPL nFlagClear nFlagSet:             LDD #0; nFlagClear:             LDD #65535

B4.  (10 pts) For the program below , determine the best case execution time by hand, from the reference materials.  For each instruction, indicate the CUMULATIVE number of clock cycles that will have occurred AFTER each instruction has executed.  If an instruction is not executed, write N/A. Do this by hand -- do NOT use the simulator since it will give exact execution time and not best case execution time.

 Assembly Instructions Cumulative Clock Cycle Count Initial Value 0 LDAA #\$0E BITA #\$80   ; check MSB BEQ msbUnset ; branch to msbUnset msbSet: LDAB #1   ;write 1 to port M STAB PTM msbUnset: LDAB #0   ;write 0 to port M STAB PTM

B5.  (BONUS: 2 pts) Use the simulator to determine and state the exact executions time for the programs in both problems 3 & 4 above. Pick an instruction where the exact execution time differs from the best case execution time and explain why in one sentence.

B6.  (BONUS: 2 pts) Consider the Indexed Indirect example shown in lecture (Fig 2.6 from Valvano). Create a similar example using different, non-trivial (e.g., non-zero), data and addresses of your choosing to illustrate the operation of the instruction: "STX [D,Y]". Include source code listing and a boxes+arrows figure similar to Figure 2.6 illustrating your answer. Hand-drawn & scanned black & white figure is OK. Confirm the operation using the simulator to make sure you got it right, which you need document only by saying "checked with simulator" in your hand-in.

## Pre-Lab Part C:

Goal: To familiarize yourself with the generic writeup hand-in checklist.

Discussion: It is easy to make small mistakes when you are in a hurry. Using a checklist is your best defense against this in general, and in particular is extremely helpful for avoiding unnecessary point losses when handing in assignments for this course.

Procedure:

C1. Paste a copy of the pre-lab portion of the Lab Writeup Checklist into your hand-in materials. Put a "*" in front of each item to indicate you understand that step. Print a copy of the checklist and put a check-mark in ink next to the "*" when you actually do the hand-in. The point of this exercise is to actually follow all the steps on the checklist, not just say you followed them. Think of how embarrassed you will be if you get something wrong after saying you followed the checklist.

Hand-in Checklist: (66 points + 6)

Part A:

1. (20 pts) Answers to part A questions 1 & 3
2. (BONUS: 2 points) Answer to part A question 2

Part B:

1. (40 pts) Answers to the part B questions 1-4.
2. (BONUS: 4 points) Answer to part B questions 5-6.

Part C:

1. (6 points). Include a copy of the Lab Writeup Checklist appropriately marked with "*"s.

Refer to the LAB FAQ for more information on lab hand-in procedures and file type requirements.  You MUST follow these procedures or we will not accept your submissions.

## Lab 2 Part A

Goal: Demonstrate that you can read and understand the datasheet for an IC and use it in a circuit.

Discussion:

This lab uses a flip-flop to hold a data value on a bar graph LED.  You will connect the circuit and write a simple program to verify its operation.

• Note that only 8 of the 10 LEDs in the bar graph are used.  Be sure pin 1 of the resistor network does not overlap the unused LEDs.
• This circuit uses Port T as a data line, Port A as a control line, and Port P as an input.
• Study the datasheet to see how to propagate a value from the D inputs to the Q outputs on the LS374.
• Note that the LEDs are active-low the way they are connected.  Note that this schematic is different from the one in the prelab.
• When using the bar graph LED, note that there is a small black dot on the side of the device that denotes the location of pin 1.

For this lab, you will adapt the string hash program from lab 1 (lab1_prog1.c).  Your new program should be named lab2_prog1_gXX.c (according to the conventions in the Lab FAQ).  Your program shall meet the following requirements:

1. The program shall compute the hash value of the string in memory (according to the program from lab 1) and store the lowest byte of the value in memory.
2. When PB1 (on the project board) is pressed, the program shall display the stored hash value on the bar graph LED.
3. When PB1 is not pressed, all elements of the bar graph LED shall be turned off.
4. Values written to the display shall use the following convention:
• A "1" bit shall be indicated by the corresponding LED being lit.
• A "0" bit shall be indicated by the corresponding LED being unlit.
5. The program shall include a subroutine of the following form to write values to the display:
void writeValue( unsigned char value ) {
/* code here sets the outputs to propagate value to the output of the flipflop */
}
6. The program shall always call the subroutine described above to write a value to the display.

Procedure:

1. Make sure the project board is powered down and disconnected from the PC.
2. Disconnect the USER jumpers on the APS12C128 module.
3. Disconnect the UFEA jumpers on the project board.
4. Wire the schematic for lab 2.
5. Follow the lab safety procedures to check your circuit before powering the project board.
6. Program the lab2_prog1.c you have written using any method you prefer (USB on the module or project board).
7. Exercise the function of the program.

#### Part A Questions:

1. Record the string value and the computed hash value from your program:
• String Value:
• Hash Value:
2. Measure and record the voltages (with respect to ground) at the following locations.  Do this with PB1 pressed and unpressed:
 LS374 Resistor Network Pins State Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 1 2 3 4 5 6 7 8 9 PB1 unpressed PB1 pressed
3. Compute the power consumption of the diode / resistor network in each state.
 State Power Consumption (mW) PB1 unpressed PB1 pressed
4. Write a short paragraph comparing these results with those predicted by the computations in your prelab
• 4a) How did the actual circuit differ from the assumptions made in the prelab? (100 words maximum; fewer is better)
• 4b) How would this affect your calculations if you had been asked to compute the power consumed when writing the hash value? (100 words maximum; fewer is better)
.
5. BONUS: Why is it important to compute the power consumption in the "off" state (no LEDs lit)? (50 words maximum; fewer is better)

#### Part A Demo Checklist: (10 points)

1. (10 points) Demo to TA lab2_prog1.c.

## Lab 2 Part B

Goal: Demonstrate that you can write simple programs in HC12 assembly language.

Discussion:

This lab uses the same circuit as Lab 2 part A.  Refer above for details on wiring the circuit.

For this lab, you will write a program that uses a loop to add the numbers 1 through 125 and displays the results.  Your program shall meet the following requirements:

1. The program shall compute the cumulative sum of the numbers from 1 to 125, inclusive (i.e. 1 + 2 + ... + 125) and store this value in a 16-bit location in  RAM.
2. The sum computation shall be done using a loop.  Each iteration of the loop shall increment the sum with the loop counter value.  The loop counter shall be incremented in each iteration of the loop.
3. The loop counter shall be stored in the X register.
4. The sum computation may use the X, A, B, and D registers and the "Sum" RAM location only.  No other memory or RAM may be used.
5. When PB1 (on the project board) is pressed, the program shall display the upper 8 bits of the computed value on the bar graph LED.
6. When PB1 (on the project board) is unpressed, the program shall display the lower 8 bits of the computed value on the bar graph LED.
7. Values written to the display shall use the following convention:
• A "1" bit shall be indicated by the corresponding LED being lit.
• A "0" bit shall be indicated by the corresponding LED being unlit.

Procedure:

Part B.1:

For the questions in part B.1, you may compute the answers using any method you wish.  You do not have to use the assembly program or the simulator.  (Hint:  For the questions involving the running sum, you may find it faster to compute the sum using a formula for an arithmetic sequence, rather than doing the sum iteratively).  The term counter limit refers to the maximum value the counter runs to (e.g., 125 in the lab program).

Part B.2:

1. Wire the project board according to Part A of Lab 2.
2. Download the 348_stationery project.  Unzip it and follow the instructions to add it to the Code Warrior stationery directory (Note: You may need to create this directory).  You must use the 348 stationery for the program to assemble correctly.
3. Start a new project using the 348 stationery.
4. Download  the lab2 comment skeleton.  Rename it to lab2_prog2_gXX.asm and add it to the project source.  Remove main.asm.
5. Write the program according to the requirements above.
6. Execute the program on the APS12C128/project board using the project board USB port.  Exercise the function of the program.
7. Verify that the value matches the expected value from part 1.

BONUS: Part B.3:

1. Re-do part B.2 above with a new program (named lab2_prog3_gXX.asm) as follows. Write a new program that adds up an arbitrary list of one-byte unsigned numbers previously stored in memory, computing a 16-bit sum, and displaying the sum as for part B.2. This program must use a loop and an indexed addressing mode to retrieve the bytes (can't be in-line code -- must be a loop that iterates over the numbers). You can use assembler constant definitions to store the bytes in the list (no need to enter them at run time). Demo this program with 17 numbers in the list, but design it in a way that it is trivial to change the numbers in the list without having to change the program (e.g., a byte in data memory stores the number of items in the list).

#### Part B Questions:

Part B.1: for procedure part B.1, answer the following questions:

1. What do you expect the sum from the program described above to be?
2. Given the requirements above, what is the largest counter limit that will not overflow the running sum?
3. What is the maximum value that the counter register can hold?
4. How large would the storage location have to be to hold the running sum if the counter limit were set to the maximum from question 3? (Assume the computation is adapted so that it does not overflow.)
5. Given the conditions in question 4, what would the running sum value be?

Part B.2:

1. Briefly describe the method(s) you used to compute the running sums in this section.  Limit your answer to 50 words.

BONUS: Part B.3:

1. Briefly describe the method(s) you used to compute the running sums in this section.  Limit your answer to 50 words.

#### Part B Demo Checklist: (20 + 4 points) Demo lab2_prog1.asm :

1. (10 points) Demonstrate the loop function to the TA by stepping through two full iterations of the loop.
2. (10 points) Show the TA the final value from part B.2 and show that it matches the expected value from part B.1 (or satisfactorily explain why it doesn't)
3. (BONUS: 4 points) Show the TA the source code and final value for part B.3 and demonstrate that it is the correct value.

## Lab Part C:

Goal: To familiarize yourself with the generic writeup hand-in checklist.

Discussion: It is easy to make small mistakes when you are in a hurry. Using a checklist is your best defense against this in general, and in particular is extremely helpful for avoiding unnecessary point losses when handing in assignments for this course.

Procedure:

C1. Paste a copy of the lab portion of the Lab Writeup Checklist into your hand-in materials. Put a "*" in front of each item to indicate you understand that step. Print a copy of the checklist and put a check-mark in ink next to the "*" when you actually do the hand-in. Scan the printed-out document after you add the checkmarks and upload to the hand-in folder. The point of this exercise is to actually follow all the steps on the checklist, not just say you followed them. Think of how embarrassed you will be if you get something wrong after saying you followed the checklist.

### Lab Hand-in Checklist: (82 + 8 points)

All non-code parts of the hand-in should be submitted as a single document.  Code files should be submitted separately, per the lab FAQ.

Part A:

1. (5 points) List any problems you encountered in the lab and pre-lab, and suggestions for future improvement of this lab and pre-lab. If "none" then say so -- if you omit this section entirely you won't get the 5 points for it.
2. (15 points) Submit the measurements and writeup for part A questions 1-4.
3. (BONUS: 3 points) submit the answer for part A question 5.
4. (15 points) Submit a listing of the code for lab2_prog1.c module (as lab2_prog1_gXX.c). Code must be fully commented to receive full credit.

Part B:

1. (5 points) List any problems you encountered in the lab and pre-lab, and suggestions for future improvement of this lab. If none, then state so to get these points.
2. (15 points) Answer questions for part B.1 (3 points per question)
3. (5 points) Answer questions for part B.2.
4. (15 points) Submit a listing of the code for lab2_prog2.asm module for part B.2 (as lab2_prog2_gXX.asm). Code should be fully commented to receive full credit.
5. (BONUS: 5 points) Answer question for part B.3 and submit a listing of the code for lab2_prog3.asm module for part B.3. Code should be fully commented to receive full credit.

Part C:

1. (7 points). Include a copy of the Lab Writeup Checklist appropriately marked with "*"s.

Refer to the LAB FAQ for more information on lab hand-in procedures and file type requirements.  You MUST follow these procedures or we will not accept your submissions.

## Support Material

### Hints and Suggestions:

Lab 2 Part A

• Use the "+" and "-" rows on the proto-board to make it easier to distribute power and ground. But, realize that they are not already hooked up. You need to run a wire from ground to the "-" row, and then you can use the "-" row to feed ground to various parts of your circuit. Same idea for power and "+". If you are unsure of how to do this, ask a TA and it will save you a lot of wiring hassle.
• Be sure to look at the data sheet and drive the CLK input to the 74LS374 properly, including meeting setup and hold times for the data.
• Hint: Port A controls the clock -- it is not the same as the microprocessor clock. You might need to look at various documentation to figure this out (but doing so is important for understanding later labs)
• Hint: be sure to use the prepackaged DIP resistor network instead of discrete resistors.

Lab 2 Part B

• The simulator might not catch an attempt to write to a ROM location. If your code works in simulation but not on the real hardware, look for this error. Any memory location your program writes must be declared in the RAM section.
• Don't forget that the clock for the D register is controlled by a bit in Port A, and not the microcontroller clock signal.

Lab 2 Part A

Lab 2 Part B