Microcontrollers and DSP - Mock Exam 3
Questa pagina non ha ancora la versione ripasso: qui sotto c'è il testo completo.
In questa pagina 8
The written test of this course is described in the course sheet as a written test with some theory questions and some application exercises (programming of microcontrollers and peripherals, DSP), with a possible additional oral exam: this part is official. No past tests are public, so this mock exam is built only on the notes of the course. The split into 4 theory questions worth 3 points each and 3 exercises worth 6 points each (total 30 points) and the time of 2 hours are a proposal of this site. The questions and the data are new: they are not a copy of any real test. The notes of the course contain no sections on DSP and no register-level description of the peripherals, so the exercises use the instruction formats, the ARM assembly and the semiconductor memories that the notes do describe.
Conventions: numbers written with the prefix 0x are hexadecimal; memory is byte-addressed and a word has bytes; the ARM syntax is the one of the notes (comments after @).
How to use it: work with a timer, write the formula or the idea in words before the numbers, and look at the "Result" only when a question is finished.
Question 1 (points 3)
(a) List the registers PC, IR, MAR, MBR and AC of a simple machine and say what each one holds. (b) Describe the path of a load instruction (lw rt, off(rs)) through the datapath of a simple RISC core and say why it is the longest instruction. (c) Which multiplexers are needed and what does each one choose?
Show the result
- PC: address of the next instruction; IR: instruction being executed; MAR: address sent to memory; MBR: datum read from or written to memory; AC: operand and result in accumulator machines.
- Load: read
rs, ALU computesrs + sext(off), data memory read, writert. - It is the longest because it uses all the elements of the datapath.
- Multiplexers: ALUSrc (
rtor extended immediate), MemtoReg (ALU result or memory data), RegDst (rdorrt), PCSrc (PC + 4 or branch target, Branch AND Zero).
Show the full solution
PC holds the address of the next instruction, IR the instruction being executed, MAR the address sent to memory on the address bus, MBR the datum read from or to be written to memory, AC the operand and result in accumulator machines (see Fetch-Execute Cycle and DatapathThe registers PC, IR, MAR and MBR, the fetch and execute phases and the four kinds of action, the full instruction-cycle state diagram with interrupt check, a worked accumulator-machine example, and the datapath of a simple RISC core with single-cycle and multicycle timing.Fetch-Execute Cycle and Datapath →). In a load the register rs is read, the ALU computes rs + sext(off), the data memory is read and the datum is written into rt. The load is the longest instruction because it uses all the elements of the datapath. The multiplexers choose: the second ALU input, register rt or the extended immediate (ALUSrc); the data written back, ALU result or memory data (MemtoReg); the destination register, rd or rt (RegDst); the new PC, PC + 4 or the branch target (PCSrc, that is Branch AND Zero).
Typical mistakes: confusing PC with IR; forgetting the sign extension of the offset, which goes from 16 to 32 bits; saying that the load does not use the ALU.
Question 2 (points 3)
(a) What does "number of addresses" of an instruction mean? Distinguish the -address and the -address forms. (b) Write the statement , with all the variables in memory, with -address instructions and with -address instructions (accumulator), and count the instructions of each program. (c) What is the general trade-off when the number of addresses decreases, and which form do RISC architectures use?
Show the result
- Number of addresses: how many explicit operands an arithmetic instruction has; addresses
OP dest, src1, src2; addressesOP dest, srcwith the destination also first operand. - addresses:
MUL T, B, C;ADD T, T, D;SUB X, E, F;DIV X, T, X: instructions. - address:
LOAD E;SUB F;STOR X;LOAD B;MUL C;ADD D;DIV X;STOR X: instructions. - Fewer addresses: shorter instructions but more numerous; RISC uses register addresses.
Show the full solution
The number of addresses is how many explicit operands an arithmetic instruction has (see Instruction Set ArchitectureWhat an instruction set defines, the elements of an instruction, operand and operation types, 0-, 1-, 2- and 3-address machines on the same calculation, load/store architectures, and the RISC and CISC design philosophies with register windows and register allocation by graph coloring.Instruction Set Architecture →). With addresses, SUB Y, A, B names the destination and the two sources; with addresses, SUB A, B has the destination coinciding with the first operand. For the -address program is MUL T, B, C, ADD T, T, D, SUB X, E, F, DIV X, T, X: instructions. The -address program uses the implicit accumulator: LOAD E, SUB F, STOR X, LOAD B, MUL C, ADD D, DIV X, STOR X: instructions. Fewer addresses make each instruction shorter but the instructions more numerous. RISC architectures use register addresses.
Typical mistakes: believing that fewer addresses give a shorter program, when the instructions are shorter but more numerous; forgetting the STOR of the intermediate result in the accumulator form; writing a -address instruction with only two operands.
Question 3 (points 3)
(a) List the visible registers of the 32-bit ARM core, grouped by role. (b) What does a read of r15 give? (c) What does the CPSR contain, and what happens to some registers when the processor enters an exception mode?
Show the result
r0-r3arguments, return value, temporaries;r4-r11variables (preserved by called functions);r12IP temporary;r13SP;r14LR;r15PC.- Reading
r15gives the address of the current instruction plus . - CPSR: flags N, Z, C, V in bits -, interrupt-disable bits, bits of the current mode.
- Entering an exception mode: at least SP and LR are replaced by private (banked) copies, the CPSR is saved into the SPSR of the mode.
Show the full solution
The 32-bit ARM core has visible -bit registers (see The ARM Core ArchitectureThe 32-bit ARM core as the reference microcontroller core: registers r0-r15 with SP, LR and PC, the status register CPSR with flags NZCV, processor modes, the load/store model, conditional execution, the flexible second operand, Thumb, and the main differences of 64-bit ARMv8.The ARM Core Architecture →). r0-r3 hold the arguments and the return value of functions and temporaries; r4-r11 hold variables and must be preserved by the called functions; r12 (IP) is a temporary; r13 is the stack pointer SP, r14 the link register LR, which holds the return address after BL, and r15 is the program counter PC. Reading r15 gives the address of the current instruction plus , an effect of the original -stage pipeline. The CPSR holds the flags N, Z, C, V in bits -, the interrupt-disable bits and the bits of the current mode. On entering an exception mode at least SP and LR are replaced by private (banked) copies and the CPSR is saved into the SPSR of that mode.
Typical mistakes: using r13-r15 as ordinary registers; believing that r15 gives the address of the instruction being executed; expecting ADD to update the flags.
Question 4 (points 3)
(a) Why does a single bus with many devices become a bottleneck, and how is a bus hierarchy organised? (b) What is the role of the bridge? (c) How did the I/O module evolve beyond the DMA module, and what do disk, network and GPU controllers do today?
Show the result
- Single bus with many devices: propagation delay and contention grow, the bus is the bottleneck.
- Hierarchy: local bus CPU-cache, system bus to main memory, expansion bus for I/O.
- Bridge: interface acting as a buffer, slow peripherals do not slow down the CPU-memory traffic.
- I/O channel: a processor with its own instruction set executing I/O programs in memory without CPU intervention; then an I/O processor with local memory.
- Today controllers have their own DMA (bus mastering on PCI Express).
Show the full solution
With many devices on a single bus, propagation delay and contention grow and the bus becomes the bottleneck (see Buses and InterconnectionThe system bus divided into data, address and control lines; bus width and address space; arbitration; single bus and bus hierarchies; synchronous and asynchronous timing; point-to-point interconnections.Buses and Interconnection →). The solution is a bus hierarchy: a local bus between CPU and cache, a system bus between cache or controller and main memory, and an expansion bus for I/O. The expansion bus is connected through an interface, the bridge, which acts as a buffer: slow peripherals do not slow down the CPU-memory traffic. The I/O module evolved later (see Direct Memory AccessThe DMA module and the information it receives from the CPU; block transfer with a single final interrupt; cycle stealing and burst mode, points where the CPU may be suspended; bus configurations; channels and I/O processors; quantitative comparison with interrupt-driven I/O.Direct Memory Access →): first an I/O channel, a processor with its own instruction set that executes I/O programs in memory without CPU intervention, then a true I/O processor with a local memory. Today disk, network and GPU controllers have their own DMA, with bus mastering on PCI Express.
Typical mistakes: saying that the bridge makes the CPU faster instead of acting as a buffer; thinking that an I/O channel needs the CPU for every word; putting the I/O devices on the local bus.
Exercise 1 (points 6)
a) The memory contains , , , (decimal addresses and values) and the register contains . An instruction has address field . Give the operand for the immediate, direct, indirect, register (), register indirect () and displacement () modes, and the number of memory accesses needed for the operand in each mode. Then compute the effective address of a scaled-index access with , , , . (2 points)
b) In MIPS the instruction lw $8, 0x1200($9) has format I. Give the width of each field, the value of the immediate in decimal and the effective address if the register $9 contains 0x00400000. What changes if the offset is (16-bit pattern 0xFFF8)? A format J instruction has the -bit field 0x1000003: give the byte address that it denotes and explain the multiplication. (2 points)
c) A beq is at address 0x00400020. Compute the branch target (taken) for an offset field of and for . What would the target be for if the programmer forgot the advanced PC? How far, in bytes, can a branch with a -bit offset reach in each direction? (2 points)
Show the result
a) Immediate (no memory access); direct (1 access); indirect (2 accesses); register (none); register indirect (1 access); displacement (1 access). Scaled index: .
b) Fields: op , rs , rt , immediate bits. Immediate ; effective address 0x00401200. Offset : the pattern 0xFFF8 is sign-extended to 0xFFFFFFF8, so the address is 0x00400000 - 8 = 0x003FFFF8. Format J: op , address bits; the address is a word address, multiplied by (shift left by ): 0x1000003 times gives 0x400000C.
c) Target : for : 0x0040000C; for : 0x0040004C. Without the the target for would be 0x00400008. Reach: offset from to instructions, that is from to bytes around PC + 4.
Show the full solution
a) Effective addresses (see the table of the modes in the notes). The notation is for the address field, for the named register and for the content of .
- Immediate: the operand is itself, ; no memory access.
- Direct: the operand is ; EA ; one access.
- Indirect: the operand is : first is the address, then ; two memory accesses.
- Register: the operand is ; no memory access.
- Register indirect: EA , operand ; one access (the register must have been loaded first).
- Displacement: EA , operand ; one access, plus the addition.
Scaled index: EA (in hexadecimal 0x201C). Typical mistake: confusing direct and immediate addressing: in the direct mode the field is an address, in the immediate mode it is the value.
b) Format I and J. In format I the fields are op ( bits), rs (), rt () and the -bit immediate: bits. In lw $8, 0x1200($9) the base register $9 goes in rs, the destination $8 in rt, as in the datapath lw rt, off(rs), and the immediate is . The effective address is (\9)+4608=0\text{x}00400000+0\text{x}1200=0\text{x}00401200$.
With the offset the -bit pattern is 0xFFF8; the immediate is sign-extended to bits, 0xFFFFFFF8, which is , so the address is . Extending with zeros (0x0000FFF8) would give : wrong. Typical mistake: forgetting the sign extension of the immediate in negative displacements.
In format J the fields are op ( bits) and address ( bits). The address is a word address: instructions are -byte aligned, so the byte address is obtained by multiplying by , that is a left shift by : 0x1000003 (a -bit byte address).
c) Branch target. The PC is already advanced in the fetch, so the target is relative to PC + 4 and the offset counts instructions of bytes (see Fetch-Execute Cycle and DatapathThe registers PC, IR, MAR and MBR, the fetch and execute phases and the four kinds of action, the full instruction-cycle state diagram with interrupt check, a worked accumulator-machine example, and the datapath of a simple RISC core with single-cycle and multicycle timing.Fetch-Execute Cycle and Datapath →): . For : , a backward branch. For : . If the is forgotten the target for would be , four bytes too low. Typical mistake: forgetting that a branch target is relative to PC + 4.
The -bit offset is signed, from to instructions; in bytes, multiplying by , from to around PC + 4.
Exercise 2 (points 6)
ARM loads and stores (ARM Memory-Access InstructionsLDR and STR for words, bytes and half-words with and without sign; addressing modes with immediate or scaled-register offset, pre-indexed with write-back and post-indexed, with effective address examples; loading addresses and constants; LDM and STM.ARM Memory-Access Instructions →). The word array starts at address 0x2000 and contains (decimal values) , , , , .
a) The register r1 contains 0x2000 and the following code is executed.
MOV r2, #0
MOV r3, #4
loop: LDR r0, [r1], #4
ADD r2, r2, r0
SUBS r3, r3, #1
BNE loopGive, for each iteration, the address read and the value loaded in r0, then the final values of r2 and r1. Then replace the LDR instruction with LDR r0, [r1, #4]! and answer again. (2 points)
b) The byte at address 0x3000 contains 0xC8, and the half-word at address 0x3002 contains 0x8123. With r4 = 0x3000, give the value (hexadecimal and decimal) of the destination register after LDRB r0, [r4], LDRSB r0, [r4], LDRH r0, [r4, #2] and LDRSH r0, [r4, #2]. (2 points)
c) With r1 = 0x2000 and r2 = 5 give the effective address and the value of r1 after the access for these operands: [r1, #12], [r1, #-8], [r1, r2], [r1, r2, LSL #2], [r1, #16]!, [r1], #16. Which one reads the element a[r2] of the word array? With sp = 0x7FF0 the instruction STMDB sp!, {r4-r6, lr} is executed: give the new sp and the address where each register is saved. (2 points)
Show the result
a) With LDR r0, [r1], #4: addresses 0x2000, 0x2004, 0x2008, 0x200C; values ; r2 , r1 . With LDR r0, [r1, #4]!: addresses 0x2004, 0x2008, 0x200C, 0x2010; values ; r2 , r1 .
b) LDRB: 0x000000C8 ; LDRSB: 0xFFFFFFC8 ; LDRH: 0x00008123 ; LDRSH: 0xFFFF8123 .
c) [r1, #12]: 0x200C, r1 unchanged; [r1, #-8]: 0x1FF8, unchanged; [r1, r2]: 0x2005, unchanged; [r1, r2, LSL #2]: 0x2014, unchanged; [r1, #16]!: 0x2010, r1 ; [r1], #16: 0x2000, r1 . The element a[r2] is read with [r1, r2, LSL #2] (0x2014). STMDB: sp ; r4 at 0x7FE0, r5 at 0x7FE4, r6 at 0x7FE8, lr at 0x7FEC.
Show the full solution
a) Post-indexed and pre-indexed. With post-indexed addressing, LDR r0, [r1], #4 accesses the address held in r1 and then adds to r1. The loop runs times (r3 counts and SUBS sets Z when it reaches , see ARM Data-Processing InstructionsAdd, subtract, logical, move and multiply instructions of ARM, the immediate and shifted-register second operand, flag updates with S, CMP and TST, and translation of C expressions into ARM code.ARM Data-Processing Instructions →):
| iteration | address read | r0 |
r2 after the ADD |
r1 after the load |
|---|---|---|---|---|
| 1 | 0x2000 |
7 | 7 | 0x2004 |
| 2 | 0x2004 |
-3 | 4 | 0x2008 |
| 3 | 0x2008 |
12 | 16 | 0x200C |
| 4 | 0x200C |
5 | 21 | 0x2010 |
Final: r2 , r1 ().
With pre-indexed with write-back, LDR r0, [r1, #4]! first computes the address , accesses it, and then writes it back into r1. The first access is already at 0x2004:
| iteration | address read | r0 |
r2 after the ADD |
r1 after the load |
|---|---|---|---|---|
| 1 | 0x2004 |
-3 | -3 | 0x2004 |
| 2 | 0x2008 |
12 | 9 | 0x2008 |
| 3 | 0x200C |
5 | 14 | 0x200C |
| 4 | 0x2010 |
9 | 23 | 0x2010 |
Final: r2 , r1 . The element is skipped and , beyond the four elements meant, is read: with the pre-indexed form the pointer must start one element before the array. Typical mistake: confusing pre-indexed (the base is updated, then used) with post-indexed (used, then updated).
b) Loads of bytes and half-words. LDRB zero-extends the byte to bits: . LDRSB sign-extends it: the most significant bit of is , so the result is 0xFFFFFFC8, which is . LDRH zero-extends the half-word: 0x00008123 . LDRSH sign-extends it: the half-word has the most significant bit , the result is 0xFFFF8123 . Typical mistake: reading a signed byte with LDRB.
c) Effective addresses. With r1 = 0x2000 and r2 = 5:
| Operand | Effective address | r1 afterwards |
|---|---|---|
[r1, #12] |
unchanged | |
[r1, #-8] |
unchanged | |
[r1, r2] |
unchanged | |
[r1, r2, LSL #2] |
unchanged | |
[r1, #16]! |
0x2010 |
|
[r1], #16 |
0x2000 |
0x2010 |
The index must become a byte offset for an array of words: LDR r0, [r1, r2, LSL #2] reads at 0x2014; [r1, r2] would step by one byte. Typical mistake: walking an array of words with step instead of .
STMDB sp!, {r4-r6, lr} saves registers, so it subtracts from sp: . The registers go to memory in register-number order, the lowest register at the lowest address: r4 at 0x7FE0, r5 at 0x7FE4, r6 at 0x7FE8 and lr (that is r14) at 0x7FEC. This is the usual way to save registers at the start of a function (see Stack and Function Calls in ARMCall with BL and return with BX LR; the AAPCS calling convention (arguments in r0-r3, result in r0, registers to preserve r4-r11); full-descending stack with PUSH and POP; activation record; leaf and non-leaf functions; recursive factorial example with the evolution of the stack.Stack and Function Calls in ARM →).
Exercise 3 (points 6)
Semiconductor memories (Semiconductor MemoryMemory cell and operations; dynamic RAM (capacitor, refresh) and static RAM (flip-flop) compared; ROM, PROM, EPROM, EEPROM and flash; organisation of chips and modules with calculation of address lines and number of chips; synchronous DRAM and DDR; errors and correcting codes.Semiconductor Memory →). Memories are built with chips ( words of bits); the word of the memory is the unit that is addressed.
a) A byte-addressed memory of MB (word of bits) is built with bit chips. Compute the number of chips in a bank, the capacity of a bank, the number of banks, the total number of chips, the number of address bits of the memory, the number of address lines of each chip and the number of bits that enter the decoder. (2 points)
b) A memory of words of bits is built with bit chips and the address selects a word. Give the same data as in part a. If the chips are DRAM with row and column sent in two steps on the same lines (RAS and CAS), how many address pins does each chip need? (2 points)
c) A cache of KB is built with SRAM cells of about transistors per bit. How many transistors do the cells need, and how many would a DRAM of the same capacity need (one transistor and one capacitor per cell)? A student counts the chips of part a by dividing only the number of words, : which number does he get and why is it wrong? What must be done after reading a DRAM cell? (2 points)
Show the result
a) chips per bank; bank KB; banks; chips; address bits; address lines per chip; bits into a -to- decoder.
b) chips per bank; bank bits; banks; chips; address bits; address lines per chip; bits into a -to- decoder. With RAS and CAS: pins.
c) bits: SRAM transistors (about million); DRAM transistors and as many capacitors. Dividing only the words gives chips instead of , because the width of the chip ( bits instead of ) is ignored. After reading a DRAM cell the bit must be rewritten (the read is destructive).
Show the full solution
a) Byte-addressed memory. A chip has address lines and data lines. Each chip gives bits, so for a word of bits we need chips in parallel, forming a bank of bits, that is bytes KB. The memory is banks, so chips. The address has bits. The low bits go to all the chips (address lines of each chip), the high bits enter a -to- decoder that enables one of the banks. Typical mistake: counting chips by dividing only the capacities, without the word width of the chip.
b) Memory of words. Here the address selects a word of bits, so words need address bits. Each chip gives bits: a bank needs chips in parallel and has bits, that is words. The number of banks is and the chips are (check: MB of memory and chips of KB each). Each chip has address lines, the high bits enter a -to- decoder. In a DRAM row and column arrive in two steps on the same lines, which halves the pins: address pins.
c) Cells, count of chips and refresh. KB bits. With about transistors per SRAM cell: transistors. A DRAM cell has one transistor and one capacitor: transistors and capacitors, six times fewer transistors; this is why the DRAM holds more bits in the same area and is used as main memory, while the faster SRAM is used for the cache.
Dividing only the number of words gives chips: it is the number of banks, and it forgets that each chip supplies only of the bits of the word, so the right number is . Finally, the read of a DRAM cell is destructive: it discharges the capacitor, so the bit must be rewritten (and the charge leaks anyway, so a periodic refresh is needed). Typical mistake: thinking that a DRAM cell keeps its charge after a read.
Self-assessment grid
| Part | Points | Criteria |
|---|---|---|
| Question 1 | 3 | 1 for the five registers, 1 for the path of the load and why it is the longest, 1 for the multiplexers |
| Question 2 | 3 | 1 for the meaning of the number of addresses and the and address forms, 1 for the two programs, 1 for the trade-off and the RISC choice |
| Question 3 | 3 | 1 for the register roles, 1 for r15 plus , 1 for CPSR and banked registers |
| Question 4 | 3 | 1 for the bottleneck and the hierarchy, 1 for the bridge, 1 for I/O channel and today's controllers |
| Exercise 1 | 6 | 2 for part a (operands, accesses, scaled index), 2 for part b (I and J formats, sign extension), 2 for part c (branch target and reach) |
| Exercise 2 | 6 | 2 for part a (post-indexed and pre-indexed), 2 for part b (signed and unsigned loads), 2 for part c (effective addresses and STMDB) |
| Exercise 3 | 6 | 2 for part a (chips, banks, address bits), 2 for part b (words, decoder, RAS and CAS pins), 2 for part c (cell counts, counting error, refresh) |
| Total | 30 |
Criteria for each point: the idea is correct, the formula is written before the numbers, units are present, the result is checked.