Salta al contenuto
Note per Studenti Microcontrollers and DSP - Mock Exam 4 Report

Microcontrollers and DSP - Mock Exam 4

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 not public: the official format, from the course description, is a written test with some theoretical questions and some application exercises (programming of microcontrollers and peripherals, DSP), and an additional oral exam is possible. This mock exam is a proposal of the site, built only on the notes of the course: the questions and the data are new and it is not a copy of any real test. The distribution of the points is a proposal too: 4 theory questions worth 3 points each and 3 exercises worth 6 points each, for a total of 30 points, in 2 hours. The notes of the course do not yet cover the peripheral units of a microcontroller and the DSP part, so this mock covers the topics that the notes treat.

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-r11 are registers, labels end with a colon and comments start with @.

Question 1 (points 3)

Recursion and the stack. The function fact below is the recursive factorial of the notes.

armasm
fact:   CMP   r0, #1
        MOVLE r0, #1
        BXLE  lr
        PUSH  {r4, lr}
        MOV   r4, r0
        SUB   r0, r0, #1
        BL    fact
        MUL   r0, r4, r0
        POP   {r4, pc}

a) Why does the function save r4 and lr with PUSH, and why does the base case return without any PUSH? What would happen if lr were not saved?

b) The function is called with n=5n=5 and sp = 0x9000 at the moment of the call. Give sp after the PUSH of each level, the lowest value reached by sp and the bytes of stack used. Then give the value of r0 returned by each level and sp after each POP.

c) The stack area has only 64 bytes. What is the largest nn that can be used, and what happens with a larger one?

Show the result
  • lr is overwritten by BL, and r4 (callee-saved) holds nn across the call, so both are saved; the base case calls nothing and uses no r4, so it needs no PUSH. Without lr the function returns to the wrong place.
  • sp after the PUSH for n=5,4,3,2n=5,4,3,2: 0x8FF8, 0x8FF0, 0x8FE8, 0x8FE0; lowest 0x8FE0, 3232 bytes.
  • Returns: n=1n=1 gives 11; then r0=2,6,24,120r0=2,6,24,120 and sp == 0x8FE8, 0x8FF0, 0x8FF8, 0x9000.
  • 88 bytes per level, 88 levels in 6464 bytes, so n≤9n\le9; beyond that, stack overflow.
Show the full solution

fact tests the base case first: for n≤1n\le1 it sets r0 to 11 and returns with BXLE lr before any PUSH, because no BL has changed lr and r4 has not been used, so there is nothing to save (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 →). For n≥2n\ge2 the function keeps nn in the callee-saved r4 and calls itself with BL, which overwrites lr: PUSH {r4, lr} saves both, 8 bytes per level, and POP {r4, pc} restores r4 and returns. Without lr the first return address would be lost and the function would return in the wrong place. With sp = 0x9000 the levels n=5,4,3,2n=5,4,3,2 leave sp at 0x8FF8, 0x8FF0, 0x8FE8, 0x8FE0: 32 bytes. On return r0 is 2,6,24,1202,6,24,120 and sp climbs back to 0x9000. A 64-byte area holds 8 levels, so n≤9n\le9.

Typical mistakes: saving only r4 and forgetting lr; thinking that the base case also pushes; giving sp after the POP of level 5 as 0x8FF8 instead of the initial value (every PUSH has its POP).

Question 2 (points 3)

Request scheduling on a magnetic disk.

a) Describe the three algorithms FCFS, SSTF and elevator, and the advantages and drawbacks of each.

b) The head is on cylinder 100100 and moves toward higher cylinders. The requests in the queue, in arrival order, are for the cylinders 35, 120, 98, 160, 2035,\ 120,\ 98,\ 160,\ 20. Give the order of service and the total number of cylinders travelled with each algorithm, and the number of reversals of the arm.

c) Why does the order of service matter so much for the time of a disk access?

Show the result
  • FCFS: arrival order, fair but often slowest; SSTF: nearest request, minimum movement but starvation of far requests and many reversals; elevator: keeps the direction, reverses when there are no more requests, few reversals.
  • FCFS: 35,120,98,160,2035,120,98,160,20, 374374 cylinders, 44 reversals.
  • SSTF: 98,120,160,35,2098,120,160,35,20, 204204 cylinders, 22 reversals; elevator: 120,160,98,35,20120,160,98,35,20, 200200 cylinders, 11 reversal.
  • Positioning (seek) and rotational latency dominate the access time, so less arm movement saves the largest part of it.
Show the full solution

FCFS serves the requests in arrival order: fair, but often the slowest. SSTF always takes the request nearest to the current cylinder: minimum movement at each step, but far requests may wait for a long time and the arm reverses often. The elevator continues in the current direction serving the requests it meets and reverses only when there are no more: few reversals, little mechanical wear, good performance (External Memory - Disks, RAID and Optical StorageMagnetic disk (platters, surfaces, tracks, sectors, cylinders); access time as the sum of seek, rotational latency and transfer, with formulas and an example; scheduling algorithms FCFS, SSTF and elevator; RAID levels 0 to 6; SSD; CD, DVD and tapes.External Memory - Disks, RAID and Optical Storage →). With the head on cylinder 100100 moving up, FCFS goes 35,120,98,160,2035,120,98,160,20 and travels 65+85+22+62+140=37465+85+22+62+140=374 cylinders; SSTF goes 98,120,160,35,2098,120,160,35,20 and travels 2+22+40+125+15=2042+22+40+125+15=204 with two reversals; the elevator goes 120,160,98,35,20120,160,98,35,20 and travels 20+40+62+63+15=20020+40+62+63+15=200 with one reversal. The order matters because positioning and rotational latency dominate the access time.

Typical mistakes: letting the elevator start downward when the text says upward; choosing the nearest request from the starting cylinder at every step of SSTF instead of from the current one; counting the total movement as the number of requests.

Question 3 (points 3)

Bus timing.

a) Explain the difference between a synchronous and an asynchronous bus: what paces the events and what each choice costs.

b) A synchronous bus runs at 8080 MHz and a read operation lasts 44 cycles, just enough for the slowest device, which needs 5050 ns. Compute the cycle and the duration of the operation.

c) A fast memory on the same bus could answer in 2020 ns. How much time is lost on each read with the synchronous bus? Compare the time of 10001000 reads with the synchronous bus and with an asynchronous bus, ignoring the delay of the handshake signals.

Show the result
  • Synchronous: a common clock paces the events, each operation lasts a whole number of cycles, simple but everyone adapts to the slowest device. Asynchronous: handshake signals (for example "read" and "data ready"), slow and fast devices coexist.
  • Cycle 12.512.5 ns; operation of 44 cycles =50=50 ns.
  • Lost time on each read: 50−20=3050-20=30 ns.
  • 10001000 reads: 5050 microseconds synchronous, about 2020 microseconds asynchronous (reading of the site: the handshake delays are ignored).
Show the full solution

In a synchronous bus a common clock paces every event and each operation lasts a whole number of cycles: simple, but every device must adapt to the slowest one (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 →). In an asynchronous bus each event follows the previous one through handshake signals, for example the master raises "read" and the slave answers "data ready": slow and fast devices coexist. At 8080 MHz the cycle is 1/80 MHz=12.51/80\ \text{MHz}=12.5 ns, so an operation of 44 cycles lasts 4×12.5=504\times 12.5=50 ns. The fast memory would answer in 2020 ns, but on the synchronous bus the read still takes 5050 ns: 3030 ns are lost each time. For 10001000 reads the synchronous bus needs 1000×50 ns=501000\times 50\ \text{ns}=50 microseconds; with handshakes, ignoring their delays, the time is about 1000×20 ns=201000\times 20\ \text{ns}=20 microseconds.

Typical mistakes: confusing the clock frequency with the cycle time (8080 ns instead of 12.512.5 ns); saying that an asynchronous bus has a common clock; forgetting that the notes do not give a number for the handshake delays, so the asynchronous time is only an estimate.

Question 4 (points 3)

The linker.

a) What are the three tasks of the linker?

b) The linker receives three object files in this order: main.o with 240240 bytes of text, util.o with 9696 bytes and io.o with 6464 bytes. At which offset of the text of the executable does each file start? A relocatable address that in io.o is 0x10 becomes which address? What happens to a BL printf in main.o?

c) Compare static and dynamic linking. What error does the linker report if a label of an assembly file is not declared with .global?

Show the result
  • Tasks: places the text and data segments one after the other; resolves the external symbols; corrects the relocatable addresses.
  • Starts: main.o at 00, util.o at 240=240= 0xF0, io.o at 336=336= 0x150; the address 0x10 of io.o becomes 16+336=352=16+336=352= 0x160; BL printf is linked to the definition of printf in the library.
  • Static: libraries copied into the executable; dynamic: only a reference, the shared library is loaded at run time and shared among processes.
  • Without .global the label is not visible: undefined reference.
Show the full solution

The linker has three tasks (From a C Program to an ARM ExecutableThe translation chain compiler, assembler, linker and loader; object files, symbol table and relocation; static and dynamic linking; memory layout of a process; calling assembly functions from C and vice versa, with an example.From a C Program to an ARM Executable →): it places the text and data segments of all the object files one after the other; it resolves the external symbols, so a BL printf in main.o is linked to the definition of printf in the library; it corrects the addresses marked as relocatable. With main.o of 240240 bytes, util.o of 9696 and io.o of 6464, the text segments start at offsets 00, 240=240= 0xF0 and 240+96=336=240+96=336= 0x150. The address 0x10 of io.o is corrected by its start: 16+336=352=16+336=352= 0x160. With static linking the libraries are copied into the executable; with dynamic linking the executable holds only a reference, and the shared library is loaded at run time and shared among processes. Without .global the label is not visible: undefined reference.

Typical mistakes: correcting the address of io.o by the size of io.o instead of by the offset where it starts; confusing the linker with the assembler (the assembler translates assembly into machine code, the linker joins the files); thinking that a dynamic library is copied into the executable.

Exercise 1 (points 6)

All the registers hold 32-bit values; in each part the registers have the values stated there.

a) With r1 = 25 and r2 = 7, the following instructions are executed in order. Give the final values of r3, r4, r5, r6 and r7 (negative results also in hexadecimal).

armasm
SUB    r3, r1, r2
RSB    r4, r1, r2
MVN    r5, r2
BIC    r6, r1, #0x18
MLA    r7, r1, r2, r3

Then, with r10 = -100000 and r11 = 70000, give r8 and r9 after SMULL r8, r9, r10, r11 (r8 is the low half), and the value of r8 after MUL r8, r10, r11. Say which of the two results is the true product (2 points).

b) Without MUL, translate in three instructions the C statement x = 15*b + 5*c, with x, b, c in r0, r1, r2 and r5, r6 as temporaries, and compute r0 for b = 13 and c = 9. Then, with r1 = -37, give r0 after MOV r0, r1, ASR #2 and after MOV r0, r1, LSR #2, and say which one divides by 4 (2 points).

c) The 64-bit number PP is held in r1:r0 (r1 is the high word) and QQ in r3:r2. With P=P= 0x00000005FFFFFF00 and Q=Q= 0x0000000300000300 write two instructions that put P+QP+Q in r5:r4 and give the result and the value of the flag C after the first. Then with P=P= 0x0000000500000100 and Q=Q= 0x0000000200000300 write two instructions that put P−QP-Q in r7:r6, give the flag C after the first and the result, and say what is obtained if the second instruction is a plain SUB (2 points).

Show the result

a) r3 =18=18; r4 =−18==-18= 0xFFFFFFEE; r5 =−8==-8= 0xFFFFFFF8; r6 =1=1; r7 =193=193. SMULL: r9 == 0xFFFFFFFE, r8 == 0x5EC47A00, the 64-bit value 0xFFFFFFFE5EC47A00 is −7 000 000 000-7\,000\,000\,000; MUL gives r8 == 0x5EC47A00 =1 589 934 592=1\,589\,934\,592, which is not the product: the true product is the one of SMULL.

b) RSB r5, r1, r1, LSL #4; ADD r6, r2, r2, LSL #2; ADD r0, r5, r6; r5 =195=195, r6 =45=45, r0 =240=240. ASR: r0 =−10==-10= 0xFFFFFFF6; LSR: r0 == 0x3FFFFFF6 =1 073 741 814=1\,073\,741\,814; ASR is the division for a signed number.

c) ADDS r4, r0, r2 then ADC r5, r1, r3: r4 == 0x00000200, C =1=1, r5 =9=9, so P+Q=P+Q= 0x0000000900000200. SUBS r6, r0, r2 then SBC r7, r1, r3: r6 == 0xFFFFFE00, C =0=0 (borrow), r7 =2=2, so P−Q=P-Q= 0x00000002FFFFFE00; with a plain SUB the high word is 33, wrong by 2322^{32}.

Show the full solution

a) Five instructions and two products (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 →). With r1 = 25 and r2 = 7:

  • SUB r3, r1, r2 computes 25−7=1825-7=18.
  • RSB r4, r1, r2 is the reverse subtraction, Rd=Op2−Rn=7−25=−18Rd=Op2-Rn=7-25=-18; in 32 bits 232−18=2^{32}-18= 0xFFFFFFEE.
  • MVN r5, r2 is the NOT of 77: the bits of 0x00000007 are inverted and give 0xFFFFFFF8, that is −8-8 (the NOT of xx is −x−1-x-1).
  • BIC r6, r1, #0x18 clears the bits indicated by the mask: 25=25= 0x19 =1 10012=1\,1001_2 and 0x18=1 100020x18=1\,1000_2, so the result is 0 00012=10\,0001_2=1.
  • MLA r7, r1, r2, r3 computes r1⋅r2+r3=25⋅7+18=175+18=193r1\cdot r2+r3=25\cdot7+18=175+18=193 (0xC1).

Products. −100000×70000=−7 000 000 000-100000\times70000=-7\,000\,000\,000, which does not fit in 32 bits. SMULL gives the whole signed 64-bit product: −7 000 000 000=−2⋅232+1 589 934 592-7\,000\,000\,000=-2\cdot2^{32}+1\,589\,934\,592, so the high word r9 is −2=-2= 0xFFFFFFFE and the low word r8 is 1 589 934 592=1\,589\,934\,592= 0x5EC47A00. MUL keeps only the low 32 bits, 0x5EC47A00: read as a signed number it is positive, 1 589 934 5921\,589\,934\,592, with the wrong sign and the wrong value. The true product is the 64-bit one of SMULL.

b) Multiplication by constants and division. The scaled operand makes the multiplication free of MUL (it is made in the barrel shifter in the same cycle). 15b=16b−b15b=16b-b, and RSB r5, r1, r1, LSL #4 computes Op2−Rn=16 r1−r1Op2-Rn=16\,r1-r1; 5c=c+4c5c=c+4c, and ADD r6, r2, r2, LSL #2; then the sum:

armasm
RSB r5, r1, r1, LSL #4      @ r5 = 16*b - b = 15*b
ADD r6, r2, r2, LSL #2      @ r6 = c + 4*c = 5*c
ADD r0, r5, r6              @ x = 15*b + 5*c

With b=13b=13 and c=9c=9: r5=208−13=195r5=208-13=195, r6=9+36=45r6=9+36=45, r0=195+45=240r0=195+45=240, and indeed 15⋅13+5⋅9=195+45=24015\cdot13+5\cdot9=195+45=240.

Division by 44 with r1=−37=r1=-37= 0xFFFFFFDB. MOV r0, r1, ASR #2 replicates the sign bit and gives 0xFFFFFFF6, that is −10-10: the quotient −37/4=−9.25-37/4=-9.25 rounded toward −∞-\infty. MOV r0, r1, LSR #2 brings in zeros: 0x3FFFFFF6, that is 1 073 741 8141\,073\,741\,814, a huge positive number. For signed numbers ASR is the right one.

c) Arithmetic on 64 bits (reading of the site: the notes define ADC and the flags but do not show a 64-bit example; the two words are chained through the flag C). First the low words with the instruction that updates C, then the high words with the instruction that uses it.

Sum: ADDS r4, r0, r2 adds 0xFFFFFF00 and 0x00000300: the true sum is 0x1 00000200, 33 bits, so r4 == 0x00000200 and there is a carry out of bit 31, C=1C=1 (Arithmetic Logic UnitRole of the ALU, its inputs and the N Z C V flags, the 1-bit ALU built from AND, OR, a full adder and a multiplexer, the N-bit ALU with subtraction, NOR, set-less-than, zero and overflow detection, comparators, bus sharing with tri-state outputs, and the other computation units.Arithmetic Logic Unit →). Then ADC r5, r1, r3 computes Rn+Op2+C=5+3+1=9Rn+Op2+C=5+3+1=9. The result is P+Q=P+Q= 0x0000000900000200; with a plain ADD the carry would be lost and the high word would be 88.

Difference: SUBS r6, r0, r2 computes 0x00000100 −- 0x00000300, that is 256−768=−512256-768=-512, 0xFFFFFE00 in 32 bits. As unsigned numbers 256<768256<768, so a borrow occurs, and in ARM a borrow means C=0C=0 (Binary ArithmeticBinary addition and two's complement subtraction, overflow for unsigned (carry) and signed (sign change) numbers, the N Z C V flags, multiplication by shifts and additions, Booth's algorithm with a worked example, division, and logical, arithmetic and rotate shifts.Binary Arithmetic →). Then SBC r7, r1, r3 computes Rn−Op2−C‾=5−2−1=2Rn-Op2-\overline{C}=5-2-1=2: the extra 11 is the borrow taken from the high word. The result is P−Q=P-Q= 0x00000002FFFFFE00. Check: 3⋅232−512=2⋅232+(232−512)3\cdot2^{32}-512=2\cdot2^{32}+(2^{32}-512). With a plain SUB the high word would be 5−2=35-2=3, which is wrong by 2322^{32}.

Typical mistakes: reading RSB as SUB with the operands in the same order; thinking that MUL gives the 64-bit product; writing MUL r0, r1, #15 (multiplication needs registers); using LSR for a signed division; forgetting that after SUBS the flag C=0C=0 means borrow.

Exercise 2 (points 6)

A computer has a cache with access time T1=2T_1=2 ns and a main memory with access time T2=80T_2=80 ns.

a) Compute the average access time with the rule "a miss costs the cache access plus the memory access" for the hit rates 0.900.90, 0.970.97 and 0.9950.995; with the rule of the parallel request for h=0.97h=0.97; and without cache. Which hit rate is needed to bring the average time (first rule) to 33 ns? For comparison, how many times slower than the main memory is a magnetic disk access of 66 ms? (2 points)

b) Another machine has two cache levels: TL1=1.5T_{L1}=1.5 ns with local miss rate mL1=0.08m_{L1}=0.08, TL2=12T_{L2}=12 ns with mL2=0.25m_{L2}=0.25, and main memory Tmem=90T_{mem}=90 ns. Compute the average access time. Compare it with a machine that has only the first level cache, with h=0.92h=0.92, and give the ratio (2 points).

c) A loop sums 200200 consecutive words of an array, twice. The cache is initially empty, has blocks of 44 words and is large enough for the whole array; the array starts at the beginning of a block, only the accesses to the array are counted and the times are those of part a (first rule). Give the number of misses, the hit rate and the average time for the first pass and for the two passes together, and say which kind of locality gives the hits of the first pass and which those of the second (2 points).

Show the result

a) First rule: 1010 ns, 4.44.4 ns, 2.42.4 ns; parallel rule with h=0.97h=0.97: 4.344.34 ns; without cache 8080 ns. For 33 ns: h≥0.9875h\ge0.9875. The disk is 75 00075\,000 times slower than the memory.

b) Tavg=4.26T_{avg}=4.26 ns; single level: 8.78.7 ns; ratio ≈2.04\approx2.04.

c) First pass: 5050 misses out of 200200, h=0.75h=0.75, Tavg=22T_{avg}=22 ns, hits due to spatial locality; two passes: 5050 misses out of 400400, h=0.875h=0.875, Tavg=12T_{avg}=12 ns, the hits of the second pass due to temporal locality.

Show the full solution

a) One level (Memory Hierarchy and LocalityCharacteristics of memories (location, capacity, transfer unit, access method, performance, volatility); the cost-capacity-speed trade-off and the levels of the hierarchy; temporal and spatial locality; hit, miss and average access time with worked calculations.Memory Hierarchy and Locality →). If a miss costs the cache access plus the memory access, then Tavg=T1+(1−h) T2T_{avg}=T_1+(1-h)\,T_2:

  • h=0.90h=0.90: 2+0.10⋅80=2+8=102+0.10\cdot80=2+8=10 ns;
  • h=0.97h=0.97: 2+0.03⋅80=2+2.4=4.42+0.03\cdot80=2+2.4=4.4 ns;
  • h=0.995h=0.995: 2+0.005⋅80=2+0.4=2.42+0.005\cdot80=2+0.4=2.4 ns.

If the request goes in parallel to the cache and to the memory, a miss costs only T2T_2 and Tavg=h T1+(1−h) T2T_{avg}=h\,T_1+(1-h)\,T_2: for h=0.97h=0.97, 0.97⋅2+0.03⋅80=1.94+2.4=4.340.97\cdot2+0.03\cdot80=1.94+2.4=4.34 ns. Without cache every access costs 8080 ns, 80/4.4≈1880/4.4\approx18 times more than with h=0.97h=0.97.

Hit rate for 33 ns: 2+(1−h)⋅80=32+(1-h)\cdot80=3 gives (1−h)⋅80=1(1-h)\cdot80=1, so 1−h=0.01251-h=0.0125 and h=0.9875h=0.9875: 98.7598.75 per cent of the accesses must hit. Check: 2+0.0125⋅80=32+0.0125\cdot80=3 ns.

Disk: 6 ms/80 ns=6⋅10−3/(80⋅10−9)=75 0006\ \text{ms}/80\ \text{ns}=6\cdot10^{-3}/(80\cdot10^{-9})=75\,000. This ratio of five orders of magnitude between levels is the reason for the hierarchy: the faster a memory, the more it costs per bit and the smaller it is.

b) Two levels. With the formula of the notes Tavg=TL1+mL1 (TL2+mL2 Tmem)T_{avg}=T_{L1}+m_{L1}\,(T_{L2}+m_{L2}\,T_{mem}) and the local miss rates:

Tavg=1.5+0.08 (12+0.25⋅90)=1.5+0.08⋅34.5=1.5+2.76=4.26 ns.T_{avg}=1.5+0.08\,(12+0.25\cdot90)=1.5+0.08\cdot34.5=1.5+2.76=4.26\ \text{ns}.

The inner bracket is the average time of a level-2 access, 12+22.5=34.512+22.5=34.5 ns. With the first level only, h=0.92h=0.92 means a miss rate 0.080.08 and Tavg=1.5+0.08⋅90=1.5+7.2=8.7T_{avg}=1.5+0.08\cdot90=1.5+7.2=8.7 ns. The ratio is 8.7/4.26≈2.048.7/4.26\approx2.04: the second level halves the average time because it absorbs three quarters of the misses of the first one.

c) Locality in a loop. The array has 200200 words and the blocks have 44 words, so it occupies 200/4=50200/4=50 blocks. First pass: the first access to each block is a miss and brings the 44 words, so the next 33 accesses hit. Misses: 5050 out of 200200; hits 150150; h=150/200=0.75h=150/200=0.75; Tavg=2+0.25⋅80=22T_{avg}=2+0.25\cdot80=22 ns. The hits of the first pass come from spatial locality (the block brings the neighbouring words). Second pass: the whole array is already in the cache, so all the 200200 accesses hit. Two passes: 400400 accesses, 5050 misses, h=350/400=0.875h=350/400=0.875, Tavg=2+0.125⋅80=12T_{avg}=2+0.125\cdot80=12 ns. The hits of the second pass come from temporal locality (the same data are used again).

Typical mistakes: using Tavg=h T1+(1−h) T2T_{avg}=h\,T_1+(1-h)\,T_2 when the text says that a miss includes the cache access (check which rule the text gives); using the global miss rate instead of the local ones in the two-level formula; counting a miss for every access of the first pass, forgetting that the block brings 44 words.

Exercise 3 (points 6)

A disk controller must transfer a block of 88 KB to main memory. The words have 44 bytes and the CPU clock is 200200 MHz.

a) With interrupt-driven I/O every word costs one interrupt of 180180 CPU cycles. With DMA the CPU needs 120120 cycles to program the module and 180180 cycles for the single interrupt at the end. Compute the number of words, the CPU cycles and the time in the two cases, and the ratio of the cycles. From how many words on is the DMA cheaper? What does the CPU tell the DMA module? (2 points)

b) The bus cycle lasts 88 ns (bus at 125125 MHz) and the transfer of one word across the bus takes one cycle. How many bus cycles and how much bus time does the block need if the DMA module is separate on a single bus, and if it is integrated with the device? In burst mode, for how long does the integrated DMA hold the bus, and what can the CPU do meanwhile? With cycle stealing, for how long at most is the CPU suspended each time? (2 points)

c) The destination buffer starts at the beginning of a block of a cache with blocks of 3232 bytes. How many cache blocks can hold a stale copy after the transfer, and what must be done? Then the same controller transfers a block of 44 KB: compute the cycles of the two methods and the ratio, and say which of the two costs grows with the size of the block (2 points).

Show the result

a) 20482048 words; interrupts 368 640368\,640 cycles (1843.21843.2 microseconds); DMA 300300 cycles (1.51.5 microseconds); ratio 1228.81228.8. DMA is cheaper from 22 words on (360>300360>300; for one word 180<300180<300). The CPU gives: read or write, device address, starting address in memory, number of words.

b) Separate DMA: each word crosses the bus twice, 40964096 cycles =32.768=32.768 microseconds; integrated: 20482048 cycles =16.384=16.384 microseconds. Burst: the bus is held for 20482048 consecutive cycles (16.38416.384 microseconds) and the CPU cannot access memory (it can work only if it does not need the bus, for example with data in the cache); cycle stealing: one cycle, 88 ns, each time.

c) 8192/32=2568192/32=256 blocks: the cached copies of the blocks written by the DMA must be invalidated. For 44 KB: 10241024 words, interrupts 184 320184\,320 cycles, DMA 300300 cycles, ratio 614.4614.4; the cost of the interrupts grows with the size of the block, that of the DMA (programming plus one interrupt) does not.

Show the full solution

a) Interrupts against DMA (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 →). The block has 8 KB/4 B=8192/4=20488\ \text{KB}/4\ \text{B}=8192/4=2048 words.

  • Interrupt-driven: one interrupt per word, 2048⋅180=368 6402048\cdot180=368\,640 cycles; at 200200 MHz a cycle lasts 55 ns, so the time is 368 640⋅5 ns=1.8432368\,640\cdot5\ \text{ns}=1.8432 ms =1843.2=1843.2 microseconds.
  • DMA: 120+180=300120+180=300 cycles, 300⋅5 ns=1.5300\cdot5\ \text{ns}=1.5 microseconds, plus any cycles taken on the bus.

The ratio of the cycles is 368 640/300=1228.8368\,640/300=1228.8. For nn words the interrupts cost 180 n180\,n cycles against 300300 for the DMA, so the DMA is cheaper when 180 n>300180\,n>300: for n=1n=1 it is not (180<300180<300), for n=2n=2 it is (360>300360>300), so from 22 words on. The CPU programs the DMA module with four data: read or write, the device address, the starting address in memory and the number of words; then it goes on with other work, while the module increments the address and decrements the counter for every word.

b) Use of the bus. The word is 44 bytes, so 20482048 transfers are needed. With a separate DMA on a single bus every word crosses the bus twice (device to DMA, DMA to memory): 2⋅2048=40962\cdot2048=4096 cycles, 4096⋅8 ns=32.7684096\cdot8\ \text{ns}=32.768 microseconds. With the DMA integrated with the device every word uses the bus once: 20482048 cycles, 2048⋅8 ns=16.3842048\cdot8\ \text{ns}=16.384 microseconds (reading of the site: one cycle for each crossing of the bus, as in the definition of cycle stealing).

In burst mode the integrated DMA holds the bus for 20482048 consecutive cycles, 16.38416.384 microseconds: the transfer is faster, but the CPU cannot access memory in the meantime; it is suspended at the points where it needs the bus (instruction fetch or operand fetch, result write), and with caches it can often keep working without the bus. With cycle stealing the DMA takes the bus for one cycle per word: the CPU is suspended for at most 88 ns each time and slows down slightly.

c) Cache and size of the block. The buffer has 81928192 bytes and the blocks 3232 bytes, so it covers 8192/32=2568192/32=256 blocks. If the DMA writes to memory a block that is also in the cache, the cached copy is out of date and must be invalidated: up to 256256 blocks here (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 →).

For 44 KB the words are 4096/4=10244096/4=1024. Interrupt-driven: 1024⋅180=184 3201024\cdot180=184\,320 cycles. DMA: still 120+180=300120+180=300 cycles. The ratio is 184 320/300=614.4184\,320/300=614.4, half of the previous one: the interrupts cost 180180 cycles per word, so their total is proportional to the size of the block (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 →), while the programming of the DMA and its single final interrupt do not depend on it (only the cycles taken on the bus do).

Typical mistakes: forgetting the interrupt at the end of the DMA transfer in the cycle count; counting 81928192 words instead of 20482048 (bytes instead of words); saying that the DMA interrupts the CPU at every bus cycle; forgetting the coherence with the cache.

Self-assessment grid
Item Points What must be there for full points Typical mistakes
Question 1 3 why r4 and lr are pushed and the base case needs no PUSH (1); sp values, 3232 bytes and the returns 2,6,24,1202,6,24,120 (1); 88 levels, n≤9n\le9, stack overflow (1) lr not saved; sp after the last POP wrong
Question 2 3 the three algorithms with advantages and drawbacks (1); orders and 374374, 204204, 200200 cylinders with the reversals (1.5); positioning dominates (0.5) elevator started in the wrong direction; wrong current cylinder in SSTF
Question 3 3 clock against handshake and the cost of each (1); 12.512.5 ns and 5050 ns (1); 3030 ns lost and 5050 against 2020 microseconds for 10001000 reads (1) frequency confused with cycle time; common clock in the asynchronous bus
Question 4 3 three tasks (1); offsets 00, 0xF0, 0x150, address 0x160, BL printf (1); static and dynamic, .global and the undefined reference (1) address corrected by the size instead of the start; dynamic library copied
Exercise 1 6 a) 2 points: 1818, 0xFFFFFFEE, 0xFFFFFFF8, 11, 193193, the two products; b) 2 points: three instructions, 240240, −10-10 and 0x3FFFFFF6; c) 2 points: ADDS/ADC with C =1=1 and 99, SUBS/SBC with C =0=0 and 22, the plain SUB RSB operands exchanged; MUL as 64-bit product; LSR on a signed number; C read as borrow
Exercise 2 6 a) 2 points: 1010, 4.44.4, 2.42.4, 4.344.34, 8080, h=0.9875h=0.9875, 75 00075\,000; b) 2 points: 4.264.26, 8.78.7, 2.042.04; c) 2 points: 5050 misses, 0.750.75 and 2222 ns, 0.8750.875 and 1212 ns, spatial and temporal locality wrong rule for the miss time; global miss rate in the two-level formula
Exercise 3 6 a) 2 points: 20482048 words, 368 640368\,640 and 300300 cycles, 1228.81228.8, from 22 words, four data; b) 2 points: 40964096 and 20482048 cycles, 32.76832.768 and 16.38416.384 microseconds, burst, cycle stealing; c) 2 points: 256256 blocks invalidated, 184 320184\,320 cycles and ratio 614.4614.4 for 44 KB bytes counted as words; missing final interrupt in the DMA cost; missing cache invalidation
Total 30