Microcontrollers and DSP - Mock Exam 1
Questa pagina non ha ancora la versione ripasso: qui sotto c'è il testo completo.
In questa pagina 8
Official format (from the course description): a written test with some theoretical questions and some application exercises (programming of microcontrollers and peripherals, DSP); an additional oral exam may follow. The tests of this course are not public, so this mock exam is built only on the notes of the course; the questions and the data are new, and the distribution of the points is a proposal of this site: 4 theory questions of 3 points and 3 exercises of 6 points, for a total of 30 points, in 2 hours. The notes of the course do not yet cover the peripheral units of a microcontroller (timers, converters, serial modules) and the DSP part, so this mock covers the topics that the notes treat: processor, arithmetic circuits, control, number representations, ARM programming, memory and input-output.
How to use it: work with a timer, write the formulas in letters before the numbers, and look at the "Result" only when a question is finished. In the assembly code r0-r3 are registers, labels end with a colon and comments start with @.
Question 1 (points 3)
Describe the ripple-carry adder. (a) How are the full adders connected and what is the first carry in? (b) Give the formula of the worst-case delay as a function of the carry delay and of the sum delay of a full adder, and say when the worst case occurs. (c) Why can the delay for given operands be much shorter? Finally compute the worst-case delay for , ps and ps.
Show the result
- Cascade of full adders, the carry out of stage is the carry in of stage , the first carry in is .
- , linear in ; worst case: all stages propagate and the carry crosses all of them.
- A generate or delete stage breaks the carry chain: delay (longest chain of one generate or delete stage followed by propagate stages).
- For : ps.
Show the full solution
A ripple-carry adder is a cascade of full adders: the carry out of stage is the carry in of stage , and the first carry in is (AddersHalf adder and full adder, generate-propagate-delete decomposition, the ripple-carry adder and its linear worst-case delay, the adder-subtractor, and the fast structures carry-lookahead, carry-bypass, carry-select and square-root carry-select with their delay formulas and optimal block sizes.Adders →). The worst case occurs when every stage is a propagate stage, so that the carry crosses all the stages; the last sum bit then waits carry delays and one sum delay:
linear in . For , ps and ps: ps. For real operands the delay is shorter: a generate stage () or a delete stage () produces its carry out after whatever its carry in, so it breaks the chain. The delay is plus times the longest chain made of a generate or delete stage followed by consecutive propagate stages.
Typical mistakes: writing ( ps here) or ( ps) instead of , because the sum is computed only once, at the end; using the worst-case formula as if it held for every pair of operands; confusing propagate () with generate ().
Question 2 (points 3)
Define a RISC architecture. (a) List its main characteristics (at least five). (b) Explain how register windows and register allocation by graph colouring both aim at keeping the operands in registers, and say which of the two is a hardware solution. (c) Four variables , , , are such that the pairs -, -, - and - are live at the same time. How many registers does the colouring need? What does the compiler do if only 2 registers are available?
Show the result
- RISC: one instruction per cycle in a pipeline, register-register operations with only load and store accessing memory, few simple addressing modes, fixed 32-bit format, many registers, hardwired control, optimization left to the compiler.
- Register windows: hardware, overlapping windows, parameters passed without copying, oldest windows saved to memory; graph colouring: software, compiler, nodes are variables and edges join variables live together.
- , , form a triangle: 3 registers are needed (for example , , ).
- With 2 registers one variable of the triangle is kept in memory (spill).
Show the full solution
A RISC executes one instruction per cycle in a pipeline; only load and store access memory, the other operations work between registers; it has few simple addressing modes, a fixed 32-bit format, many registers and a hardwired control unit, and it leaves the optimization to the compiler (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 →, Hardwired and Microprogrammed ControlWhat the control unit does, control signals of a single-cycle datapath with the table for each instruction class, hardwired control (combinational or finite-state machine), microprogrammed control (control memory, horizontal and vertical microinstructions, microsequencer), and a comparison.Hardwired and Microprogrammed Control →). Register windows are a hardware solution: many physical registers are divided into windows, each procedure sees one, and the windows of caller and callee overlap, so parameters pass without copying; when nested calls exhaust the windows the oldest are saved to memory. Graph colouring is a software solution: nodes are variables, an edge joins two variables live at the same time, and registers mean colours. Here form a triangle: 3 registers are needed; with 2, one variable is kept in memory (spill).
Colouring for 3 registers: , , ; is adjacent only to , so it can take (or ).
Typical mistakes: saying that register windows are managed by the compiler, or that graph colouring needs special hardware; joining by an edge two variables that are not live at the same time; forgetting that a variable that cannot be coloured is kept in memory.
Question 3 (points 3)
A byte-addressed memory has 20-bit addresses and a direct-mapped cache of lines with blocks of 8 bytes. (a) Explain where a block is placed and give the three address fields with their widths. (b) Explain how the cache decides between hit and miss and what the word field is used for. (c) The addresses 0x2A4C8 and 0x2A6C8 are used alternately: give their fields and say what happens. (d) At equal capacity, how does a larger block size change the number of lines and the misses?
Show the result
- Line (block address) ; fields tag bits, line bits, word bits.
- The line field selects the line: empty means miss; otherwise compare tags (equal hit, different miss and replacement); the word field only selects the word to send to the CPU.
0x2A4C8: tag , line , word ;0x2A6C8: tag , line , word : same line, different tags, they evict each other (thrashing).- Larger blocks: better spatial locality but fewer lines, more conflicts, longer transfer per miss; an intermediate optimum exists.
Show the full solution
In a direct-mapped cache each block can go into one line only: line (block address) . Here the fields, from the most significant, are tag bits, line bits (), word bits (blocks of bytes) (Cache MemoryBlocks, lines and tags; address decomposition; direct, fully associative and set-associative mapping with field calculation; replacement policies (LRU, FIFO, random); write policies (write-through, write-back with dirty bit, write-allocate); block size; multilevel and split caches; reducing misses; typical address-field questions.Cache Memory →). The line field selects the line: empty means miss; otherwise the line tag is compared with the address tag, equal meaning hit, different meaning miss and replacement. The word field plays no role in this decision; it only selects the word to send to the CPU. 0x2A4C8 has tag , line , word ; 0x2A6C8 has tag , line , word : same line, different tags, so used alternately they evict each other (thrashing). Larger blocks at equal capacity exploit spatial locality better, but there are fewer lines and more conflicts, and each miss takes longer.
Typical mistakes: using the word field to decide hit or miss; confusing the width of the word field (it depends on the number of bytes in the block) with the size of the word; thinking that blocks with different tags can never compete for the same line.
Question 4 (points 3)
Several devices share one interrupt request line. (a) Name the techniques that identify the source of the interrupt. (b) Describe the daisy chain: how the acknowledge travels and what the module does. (c) Three modules , , are connected in a daisy chain in this order; and request an interrupt together, does not. Which module is served and what does it put on the bus? (d) What does the CPU do with the vector? Why is software polling slow?
Show the result
- Multiple interrupt lines, software polling, daisy chain (hardware polling), bus arbitration.
- Daisy chain: the acknowledge passes from module to module; the first with a pending request blocks it and puts its vector on the bus.
- is served and puts its own vector on the bus; does not receive the acknowledge.
- The vector selects the routine through the vector table in memory; polling is slow because one routine interrogates the status registers of all the modules.
Show the full solution
The source of an interrupt can be identified with multiple interrupt lines (one per device), with software polling, with a daisy chain, or with bus arbitration (the module must obtain the bus before requesting the interrupt) (Interrupts and ExceptionsClasses of interrupts; interrupt-driven I/O; hardware and software handling sequence with context saving on the stack; identifying the source (multiple lines, polling, daisy chain, vector); priorities and nested interrupts; exceptions and modes in ARM.Interrupts and Exceptions →). In software polling a single routine interrogates the status registers of all the modules, which is slow. In the daisy chain (hardware polling) the acknowledge signal passes from one module to the next; the first module with a pending request blocks it and puts its own vector, a number identifying the routine, on the bus. With and requesting, the acknowledge crosses and stops at , which puts its vector on the bus; does not receive the acknowledge. With the vector the CPU jumps directly to the right routine through the vector table in memory.
Typical mistakes: thinking that polling is done by hardware or that all the modules answer the acknowledge together; confusing the vector (a number) with the routine itself; forgetting that the order in the chain decides which module is served first.
Exercise 1 (points 6)
An 8-bit register contains the pattern .
a) Give the value of the pattern when it is read as an unsigned integer, in sign-magnitude, in one's complement, in two's complement and in excess 128 (2 points).
b) Write on 8 bits in sign-magnitude, in one's complement, in two's complement (with the rule "invert and add 1", and check it with the shortcut of copying up to the first 1) and in excess 128. Then extend the two's complement pattern to 16 bits, and say which value would be obtained if the extension were made by mistake with zeros (2 points).
c) A 12-bit register uses two's complement: give the minimum and the maximum values, and the opposite of the pattern . In sign-magnitude on 12 bits, how many different values can be represented? Finally a 6-bit code uses excess 31: give the codes of , and , decode the code , and say whether the codes of and are in the same order as the values (2 points).
Show the result
a) Unsigned ; sign-magnitude ; one's complement ; two's complement ; excess 128 .
b) Sign-magnitude ; one's complement ; two's complement (0xA6); excess 128 (stored value ). Extension to 16 bits: (0xFFA6); with zeros: .
c) Two's complement on 12 bits: from to ; the opposite of is again ( is not representable). Sign-magnitude on 12 bits: different values (from to , with two zeros). Excess 31: , , ; is ; yes, the order is the same ( as unsigned).
Show the full solution
a) One pattern, five values. The weights of the bits are , and the pattern has ones in the positions of weight . The value depends on the convention used to read it (Signed Integer RepresentationsSigned integers on n bits: sign-magnitude, one's complement, two's complement and excess K, with representable ranges, computing the opposite, sign extension and a 4-bit comparison table.Signed Integer Representations →).
- Unsigned: .
- Sign-magnitude: the most significant bit is the sign, and it is , so the number is negative; the magnitude is the other seven bits, . The value is .
- One's complement: the most significant bit is , so the number is negative; its opposite is obtained by inverting all the bits: . The value is .
- Two's complement: the most significant bit has negative weight, so . Check: a negative is the unsigned number , and .
- Excess 128: the stored number is , so .
b) The number in four conventions. First write .
- Sign-magnitude: change the sign bit and keep the magnitude: .
- One's complement: invert all the bits of : .
- Two's complement: invert and add : . Check with the weights: . Check with the shortcut: from the right copy the bits up to and including the first (they are ), and invert all the others (): the result is , the same.
- Excess 128: .
Sign extension. To pass from 8 to 16 bits the sign bit is replicated on the left, eight more copies of : (0xFFA6), still . If the extension is made with zeros the pattern is : a positive number, equal to , not .
c) Ranges and excess 31. In two's complement on bits the range is (asymmetric). The opposite of : invert, , and add , : the same pattern, because is not representable (edge case of the asymmetry).
In sign-magnitude on 12 bits the range is . There are patterns but two of them are zeros ( and ), so the different values are .
Excess stores as an unsigned number; here and , so the codes stand for the values .
- .
- .
- .
- Decoding , so .
The order of the codes equals the order of the values: as unsigned numbers , as values . This is why the exponent of a floating-point number uses excess (Fixed-Point and Floating-Point NumbersFixed-point numbers as an integer divided by 2 to the F with resolution and a sensor example, floating point in base 2, the IEEE 754 single and double formats, conversions in both directions with worked examples, special values and denormals, range, precision, rounding and floating-point operations.Fixed-Point and Floating-Point Numbers →).
Typical mistakes: inverting the bits without adding (that gives one's complement); extending a negative number with zeros; forgetting the asymmetry of two's complement ( has no opposite); reading an excess code as if it were two's complement.
Exercise 2 (points 6)
A routine receives in r0 a signed 32-bit integer and in r1 a signed 32-bit integer . It must leave in r2 the sum of the absolute values of for all the values with . In C:
s = 0; i = a;
while (i < b) {
t = i;
if (t < 0) t = -t;
s = s + t;
i = i + 3;
}The variables , , , are in r0, r1, r2, r3. The assembly translation has three missing parts, marked with underscores:
MOV r2, #0
loop: CMP r0, r1
______ out @ (1) exit when i >= b
MOV r3, r0
CMP r3, #0
______ r3, r3, #0 @ (2) t = |t|, without branches
ADD r2, r2, r3
ADD r0, r0, #3
______ loop @ (3) repeat
out:a) Write the three missing parts (2 points).
b) Run the code with and (the registers hold and on entry): give, for each iteration, the value of r0 at the start, of r3 and of r2 at the end. Then give the final values of r0 and r2, the number of iterations, in how many of them the instruction (2) changes r3, and the flags N, Z, C, V produced by the last CMP r0, r1 (2 points).
c) A programmer, thinking of addresses, replaces the instruction (1) with BHS out and runs the code again with r0 0xFFFFFFF9 and r1 . Give the result and the flags N, Z, C, V of the first CMP r0, r1, say what the code does and the final value of r2, and say which family of conditions must be used for signed data (2 points).
Show the result
a) (1) BGE; (2) RSBLT; (3) B.
b) Iterations: r0 ; r3 ; r2 . Final r0 , r2 ; iterations; the instruction (2) changes r3 in of them; last CMP r0, r1 (): , , , .
c) First CMP: with , , , . BHS is taken because : the loop never runs, r2 and r0 stays 0xFFFFFFF9. For signed data the signed conditions (GE, GT, LT, LE) must be used.
Show the full solution
a) The missing parts. The while loop goes on as long as , so the exit test is the negated condition , and the numbers are signed: the instruction (1) is BGE out (it tests after the CMP r0, r1; see Control Structures in ARM AssemblyBranch B and conditional branches, signed and unsigned condition codes; translation of if, if-else, while, for and do-while from C to ARM; conditional execution to remove short branches; switch with a jump table.Control Structures in ARM Assembly →). The absolute value is computed as in the note: CMP r3, #0 sets the flags on the sign of , and RSBLT r3, r3, #0 computes only if : the instruction (2) is RSBLT, which also avoids a branch and a pipeline flush. At the end of the body the loop starts again with the unconditional branch: the instruction (3) is B.
MOV r2, #0
loop: CMP r0, r1
BGE out
MOV r3, r0
CMP r3, #0
RSBLT r3, r3, #0
ADD r2, r2, r3
ADD r0, r0, #3
B loop
out:b) Trace with , . At each iteration CMP r0, r1 finds and BGE is not taken; MOV copies into r3; CMP r3, #0 sets the flags on ; RSBLT changes r3 only if is negative; then r2 grows by and r0 by .
| iteration | r0 at the start |
r3 after RSBLT |
RSBLT changes r3? |
r2 at the end |
|---|---|---|---|---|
| 1 | yes | |||
| 2 | yes | |||
| 3 | yes | |||
| 4 | no | |||
| 5 | no | |||
| 6 | no |
After the sixth iteration r0 . The next CMP r0, r1 computes : the result is positive (), non zero (), there is no overflow (), and because also as unsigned numbers. BGE tests , that is : true, so the branch to out is taken. Final values: r0 , r2 , after iterations; the instruction (2) changes r3 in iterations (those with ). In the other three it is still fetched and executed, but its condition is false and it does nothing.
c) Signed and unsigned conditions. With r0 and r1 , CMP r0, r1 computes , which is negative as a signed number (), so ; it is not zero, ; the signed result is representable, so . Read as an unsigned number 0xFFFFFFF9 is , which is greater than , so the condition HS (, unsigned "greater or equal") is true: . BHS out is therefore taken at the first test: the body never runs, r2 stays (instead of ) and r0 stays 0xFFFFFFF9. With BGE the test is , that is , false, and the loop goes on correctly. This is the reason why two families of conditions exist (Control Structures in ARM AssemblyBranch B and conditional branches, signed and unsigned condition codes; translation of if, if-else, while, for and do-while from C to ARM; conditional execution to remove short branches; switch with a jump table.Control Structures in ARM Assembly →): for signed data GE, GT, LT, LE; for unsigned data and addresses HS, HI, LO, LS.
Typical mistakes: using BHS/BHI (unsigned) for signed numbers, or BGT/BLT for addresses; omitting CMP r3, #0 before RSBLT, so that the flags still come from the comparison of with and not from the sign of ; putting an instruction with the suffix S between a CMP and the branch that uses its flags.
Exercise 3 (points 6)
A system has 32-bit virtual addresses, 28-bit physical addresses and pages of 8 KB; every page-table entry takes 4 bytes.
a) Compute the number of bits of the offset, of the virtual page number and of the frame number, the number of pages and of frames, and the size of the page table of one process (2 points).
b) The entry of virtual page 0x051EA is valid and contains frame 0x1B5C. Translate the virtual address 0x0A3D5E7C into a physical address. The virtual address 0x0A3D7123 belongs to a page whose entry is not valid: say what happens (2 points).
c) A memory access takes ns, the TLB access takes ns and the TLB hit rate is per cent; there are no page faults. Compute the average time of an access with the TLB and without it, and the ratio between the two (2 points).
Show the result
a) Offset bits; virtual page number bits, so pages; frame number bits, so frames; page table bytes MB.
b) Page 0x051EA, offset 0x1E7C; physical address 0x36B9E7C. For 0x0A3D7123 (page 0x051EB, offset 0x1123) the entry is not valid: page fault, an exception; the operating system loads the page from disk into a free or freed frame, updates the table and restarts the instruction.
c) With the TLB ns; without ns; ratio about .
Show the full solution
a) Fields and sizes (Virtual MemoryVirtual and physical addresses, pages and frames; translation with the page table and field calculation; page fault and page replacement; TLB and effective access time; protection and sharing; comparison with the cache.Virtual Memory →). The offset has bits: bytes, so the offset has bits and it does not change in the translation. The virtual page number has bits, so there are pages. The frame number has bits, so there are frames; check: frames of bytes make bytes MB, the whole physical memory. The page table has one entry for each virtual page: bytes MB for each process. It depends on the virtual space, not on the physical memory; this is why multi-level tables are used.
b) Translation. The address 0x0A3D5E7C in binary is . The last bits are , that is the offset 0x1E7C; the first bits are , that is the page number 0x051EA, the page of the given entry. The entry gives the frame 0x1B5C, which on bits is . The physical address is the frame number followed by the same offset:
which has bits, as required.
The address 0x0A3D7123 has page number 0x051EB and offset 0x1123. The entry is not valid: the MMU raises an exception (page fault) and the operating system chooses a free frame, or a page to replace (an approximation of LRU with the reference bit; if the replaced page is dirty it is first written to disk); reads the page from disk, which takes millions of cycles while another process runs; updates the table and restarts the instruction that caused the fault.
c) Average access time. With the TLB a hit costs the TLB access plus the memory access; a miss also needs the access to the page table in memory:
Without the TLB every access needs two memory accesses (table and datum): ns. The ratio is : the TLB almost halves the time.
Typical mistakes: translating the offset too (only the page number changes); computing the table size from the physical memory instead of the virtual space; forgetting the TLB access in the miss case, or counting only one memory access in the hit case.
Self-assessment grid
| Question or exercise | Points | What is needed for full marks | Typical mistakes |
|---|---|---|---|
| Question 1 | 3 | cascade of full adders and first carry in (0.5); and worst case (1); generate or delete breaks the chain (1); ps (0.5) | or ; worst case taken as general |
| Question 2 | 3 | at least five RISC characteristics (1); windows as hardware and colouring as software, with the mechanism (1); 3 registers and spill with 2 (1) | windows and colouring exchanged; edge between variables not live together |
| Question 3 | 3 | placement and fields (1); hit or miss decision and role of the word field (0.75); the two addresses in line and thrashing (0.75); block-size trade-off (0.5) | word field used for hit or miss; tags confused |
| Question 4 | 3 | the four techniques (0.75); daisy chain mechanism (0.75); and its vector (0.75); vector table and polling cost (0.75) | polling seen as hardware; vector confused with the routine |
| Exercise 1 | 6 | five values of the pattern (2); four codes of and the extension (2); ranges, opposite of the minimum, values, excess 31 (2) | inverting without ; zero extension; reading excess as two's complement |
| Exercise 2 | 6 | BGE, RSBLT, B (2); the table, , , and , the flags (2); flags, BHS taken, , signed conditions (2) |
unsigned conditions for signed data; missing CMP r3, #0 |
| Exercise 3 | 6 | bits, , , 2 MB (2); 0x36B9E7C and the page-fault steps (2); ns, ns, (2) |
offset translated; table size from physical memory |
| Total | 30 |