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

Microcontrollers and DSP - Mock Exam 5

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 it is not a copy of any real test. 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 the exercises use the ARM programming, the memory systems and the external memories that the notes describe.

Conventions: numbers with the prefix 0x are hexadecimal; a space inside a binary number only separates groups of four bits; in the assembly code r0-r12 are registers, labels end with a colon and comments start with @. Memory is byte-addressed and little endian.

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)

MIPS instruction formats. (a) Give the three 32-bit MIPS formats R, I and J, with the fields and their widths, and say what each format is used for. (b) Why do the register fields have 5 bits, and how many registers can be named? Which range has the 16-bit immediate of format I, and how is it extended to 32 bits? Give the value of the field 0xFF00 after the extension. (c) The address field of a jump instruction contains 0x4C30: what does it represent, and which byte address does it designate? How many bytes can the 26-bit field reach?

Show the result
  • R: op 6, rs 5, rt 5, rd 5, shamt 5, funct 6 (register operations); I: op 6, rs 5, rt 5, immediate 16 (immediates, load/store, conditional branches); J: op 6, address 26 (unconditional jump); each is 3232 bits.
  • 25=322^5=32 registers; the immediate is sign-extended: range −32768…32767-32768\dots32767; 0xFF00 becomes 0xFFFFFF00, that is −256-256.
  • The J address is a word address, multiplied by 44 (shifted left by 22): 0x4C30 designates the byte address 0x130C0.
  • 2262^{26} words, that is 2282^{28} bytes =256=256 MB.
Show the full solution

The three MIPS formats are all 3232 bits wide (Instruction Formats and Addressing ModesAddressing modes (immediate, direct, indirect, register, register indirect, displacement, PC-relative, scaled indexed, stack) with effective address and a numeric example, fixed versus variable instruction formats, the MIPS R, I and J formats and the 32-bit ARM data-processing format with the encodable-constant rule.Instruction Formats and Addressing Modes →). Format R (register operations such as add $1, $2, $3): op 66, rs 55, rt 55, rd 55, shamt 55, funct 66 bits, and 6+5+5+5+5+6=326+5+5+5+5+6=32. Format I (immediates, load/store, conditional branches): op 66, rs 55, rt 55 and a 1616-bit immediate. Format J (unconditional jump): op 66 and a 2626-bit address. A register field has 55 bits because there are 25=322^5=32 registers. The immediate is sign-extended to 3232 bits, so it ranges from −32768-32768 to 3276732767; 0xFF00 has sign bit 11 and becomes 0xFFFFFF00, that is −256-256. The J address is a word address, multiplied by 44 (shifted left by 22) because instructions are 44-byte aligned: 0x4C30 designates 0x130C0, and the field reaches 226×4=2282^{26}\times4=2^{28} bytes =256=256 MB.

Typical mistakes: using the jump field as a byte address without multiplying by 44; zero-extending the immediate and getting 6528065280 instead of −256-256; saying that a register field with 55 bits names 55 registers.

Question 2 (points 3)

The N-bit ALU. (a) Starting from the 1-bit ALU (AND, OR, full adder, multiplexer), say how the N-bit ALU performs subtraction, NOR, set less than and zero detection, and how overflow is detected. (b) With N=8N=8 the ALU computes a−ba-b for a=0011 0100a=0011\,0100 and b=0101 0111b=0101\,0111: give the second operand of the adder, the carry in, the result and the flags N, Z, C, V, with their meaning. (c) What is the result of NOR on the 4-bit patterns 1010 and 0110, and which signals produce it?

Show the result
  • Subtraction: Binvert passes the complement of bb and sets c0=1c_0=1, so a−b=a+b‾+1a-b=a+\overline b+1; NOR: Ainvert and Binvert invert both inputs of the AND (a‾⋅b‾=a+b‾\overline a\cdot\overline b=\overline{a+b}); set less than: sign bit of a−ba-b (corrected for overflow) in bit 0; zero: NOR of all the result bits; overflow V=cn⊕cn−1V=c_n\oplus c_{n-1}.
  • b‾=1010 1000\overline b=1010\,1000, carry in 11: 0011 0100+1010 1000+1=1101 11010011\,0100+1010\,1000+1=1101\,1101, that is −35-35.
  • N =1=1, Z =0=0, C =0=0 (no carry out: a borrow, 52<8752<87 unsigned), V =0=0 (operands of the same sign in a subtraction).
  • NOR of 1010 and 0110 is 0001.
Show the full solution

In the N-bit ALU (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 →) NN one-bit ALUs are chained by the carries. For subtraction the signal Binvert passes the complement of bib_i and sets c0=1c_0=1, so a−b=a+b‾+1a-b=a+\overline b+1. For NOR, Ainvert and Binvert both invert the inputs of the AND gate: a‾⋅b‾=a+b‾\overline a\cdot\overline b=\overline{a+b} (De Morgan). Set less than computes a−ba-b and puts the sign bit, corrected for overflow, in bit 00. The zero detection is the NOR of all the result bits, and overflow is V=cn⊕cn−1V=c_n\oplus c_{n-1}. For a=0011 0100a=0011\,0100 (5252) and b=0101 0111b=0101\,0111 (8787): b‾=1010 1000\overline b=1010\,1000, and 0011 0100+1010 1000+1=1101 11010011\,0100+1010\,1000+1=1101\,1101, that is −35-35. N =1=1 (the most significant bit), Z =0=0, C =0=0 (no carry out: a borrow occurred, 52<8752<87 as unsigned numbers), V =0=0 (a subtraction of two numbers of the same sign cannot overflow). NOR of 1010 and 0110 is 0001.

Typical mistakes: forgetting the carry in c0=1c_0=1 (the result would be 1101 11001101\,1100, one less); reading C =0=0 in a subtraction as an error instead of a borrow; computing the zero flag as an AND of the result bits.

Question 3 (points 3)

ROM and non-volatile memories. (a) For ROM, PROM, EPROM, EEPROM and flash say how each one is written and how it is erased. (b) Which type do you choose for: firmware that is identical in every device and never changes; a memory that the user programs once, electrically; a program that is erased with ultraviolet light before being rewritten; a table of constants that is rewritten electrically one byte at a time; a program that is updated by erasing whole blocks quickly? (c) Which of these technologies is used in SSDs and USB sticks, and what are the typical uses of non-volatile memories?

Show the result
  • ROM: written at the factory with the chip mask, cannot be erased; PROM: written once electrically by the user, cannot be erased; EPROM: written electrically, erased with ultraviolet light (whole chip); EEPROM: written and erased electrically byte by byte (slow); flash: written electrically, erased electrically by blocks (fast).
  • Choices: ROM, PROM, EPROM, EEPROM, flash in the order of the list of part b.
  • SSDs and USB sticks use flash; uses: firmware and BIOS, microprograms, function tables, programs of embedded systems.
Show the full solution

A ROM is written at the factory with the chip mask and cannot be erased (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 →). A PROM is written once, electrically, by the user, and cannot be erased. An EPROM is written electrically and erased with ultraviolet light, the whole chip at once. An EEPROM is written and erased electrically, byte by byte (slow). Flash is written electrically and erased electrically by blocks (fast). So the firmware identical in every device is a ROM, the memory programmed once is a PROM, the program erased with ultraviolet light is an EPROM, the table rewritten byte by byte is an EEPROM, the program updated by blocks is flash. Flash is the technology of SSDs and USB sticks. Typical uses: firmware and BIOS, microprograms, function tables, programs of embedded systems.

Typical mistakes: exchanging EEPROM and flash (byte by byte against blocks); saying that an EPROM is erased electrically; saying that a PROM can be rewritten.

Question 4 (points 3)

The status register and the supervisor bit. (a) Which are the control and status registers of a processor, and are they usually directly accessible to the programmer? (b) What does the program status word (PSW) contain? Explain the role of the supervisor bit and what happens when a user program tries to execute a privileged instruction. (c) At 88 bits in two's complement the processor adds 0x64 and 0x32: give the result and the flags N, Z, C, V, and explain the value of V.

Show the result
  • PC, IR, MAR, MBR and the PSW (called CPSR in ARM): usually not directly accessible.
  • PSW: flags N, Z, C, V; interrupt enable bits; supervisor bit (privileged mode: the CPU is running operating-system code); privileged instructions (memory management, I/O, changing the PSW) are allowed only in that mode, and a user program that attempts them raises an exception.
  • 0x64 ++ 0x32 =100+50=150==100+50=150= 0x96 =1001 0110=1001\,0110: N =1=1, Z =0=0, C =0=0, V =1=1.
  • V =1=1: two positive numbers give a negative result (the sum 150>127150>127 does not fit in two's complement); C =0=0 because no carry leaves the most significant bit.
Show the full solution

The control and status registers are the PC, the IR, the MAR, the MBR and the program status word (PSW, called CPSR in ARM); they are usually not directly accessible (CPU Structure and RegistersArchitecture versus organization, the stored-program (von Neumann) machine, the internal components of a CPU (ALU, control unit, register file, internal bus), user-visible and control/status registers, the status word with flags and supervisor bit, and how many registers are worth having.CPU Structure and Registers →). The PSW contains the flags N (the most significant bit of the result), Z (zero), C (carry) and V (overflow), the interrupt enable bits and the supervisor bit, which says whether the CPU is running operating-system code. Only in this privileged mode are privileged instructions allowed (memory management, I/O, changing the PSW itself); a user program that attempts them raises an exception. For the sum, 100+50=150=100+50=150= 0x96 =1001 0110=1001\,0110: the most significant bit is 11, so N =1=1; Z =0=0; 150<256150<256, so no carry leaves the most significant bit and C =0=0; two positive operands give a negative result, so V =1=1 (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 →).

Typical mistakes: believing that a user program can freely change the supervisor bit; reading C =0=0 as "no error" (V is the flag of the signed overflow); treating the PC as a general register.

Exercise 1 (points 6)

Load and store in ARM. In parts a and b r1 = 0x2400, r2 = 5, r3 = 0x0000003F, and the memory contains the words 0x23F8 →\to 0x0000AA01, 0x240C →\to 0x00000B02, 0x2410 →\to 0x000CC003, 0x2414 →\to 0x0000D004.

a) The following six instructions are executed in this order. For each one give the effective address, the value loaded (or stored) and the value of r1 afterwards (2 points).

armasm
        LDR r0, [r1, #12]
        LDR r4, [r1, #-8]
        LDR r5, [r1, r2, LSL #2]
        LDR r6, [r1, #16]!
        LDR r7, [r1], #4
        STR r3, [r1, #-4]!

b) The word 0x8C73F2A5 is stored at address 0x2500 and r5 = 0x2500. Give the contents of the destination register r4 (in hexadecimal and in decimal) after LDR r4, [r5], LDRB r4, [r5], LDRSB r4, [r5, #1], LDRSB r4, [r5, #3], LDRH r4, [r5, #2] and LDRSH r4, [r5, #2] (2 points).

c) The function copysum copies nn words from the array whose address is in r0 to the array whose address is in r1 and returns in r0 the sum of the words; the count nn is in r2. Complete the three missing parts, marked with underscores (2 points).

armasm
        LDR    r0, =src
        ______ r1, =dst              @ (3) address of dst
        MOV    r2, #4
        BL     copysum
        ...
copysum: MOV   r3, #0                @ sum
loop:   CMP    r2, #0
        BEQ    end
        LDR    r12, ______           @ (1) read a word of src and advance the pointer
        ______ r12, [r1], #4         @ (2) write it in dst and advance the pointer
        ADD    r3, r3, r12
        SUB    r2, r2, #1
        B      loop
end:    MOV    r0, r3
        BX     lr
        .data
src:    .word 12, -5, 40, 7
dst:    .word 9, 0, 0, 0

Suppose that the assembler places src at 0x2000 and dst at 0x2010. Fill a table with the address read, the value, the address written and the sum for each iteration, and give the final values of r0 (on return), r1 (just before the MOV) and of the four words of dst. Finally, if the programmer wrote by mistake LDR r12, [r0, #4]! in part (1), which addresses would be read, and what would the function return?

Show the result

a) (1) EA 0x240C, r0 = 0x00000B02, r1 = 0x2400; (2) EA 0x23F8, r4 = 0x0000AA01, r1 = 0x2400; (3) EA 0x2400+5×4=0\text{x}2400+5\times4= 0x2414, r5 = 0x0000D004, r1 = 0x2400; (4) EA 0x2410, r6 = 0x000CC003, r1 = 0x2410; (5) EA 0x2410 (the old r1), r7 = 0x000CC003, r1 = 0x2414; (6) EA 0x2410, the word 0x0000003F is stored there, r1 = 0x2410.

b) LDR: 0x8C73F2A5; LDRB: 0x000000A5 =165=165; LDRSB [r5, #1]: 0xFFFFFFF2 =−14=-14; LDRSB [r5, #3]: 0xFFFFFF8C =−116=-116; LDRH [r5, #2]: 0x00008C73 =35955=35955; LDRSH [r5, #2]: 0xFFFF8C73 =−29581=-29581.

c) (1) [r0], #4; (2) STR; (3) LDR. Iterations: (0x2000, 12, 0x2010, 12)(0\text{x}2000,\ 12,\ 0\text{x}2010,\ 12), (0x2004, −5, 0x2014, 7)(0\text{x}2004,\ -5,\ 0\text{x}2014,\ 7), (0x2008, 40, 0x2018, 47)(0\text{x}2008,\ 40,\ 0\text{x}2018,\ 47), (0x200C, 7, 0x201C, 54)(0\text{x}200C,\ 7,\ 0\text{x}201C,\ 54). Final: r0 = 54, r1 = 0x2020 (r0 was 0x2010 before the MOV), dst =12,−5,40,7=12,-5,40,7. With [r0, #4]!: addresses 0x2004, 0x2008, 0x200C, 0x2010; the last word is the one written by the first iteration (−5-5), so the sum is −5+40+7−5=37-5+40+7-5=37.

Show the full solution

a) The addressing modes (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 →). A pre-indexed access with ! computes the address, accesses and then updates the base; a post-indexed access uses the base as it is and updates it afterwards; the other forms leave r1 unchanged. The scaled index LSL #2 multiplies the index by 44.

# instruction effective address value r1 afterwards
1 LDR r0, [r1, #12] 0x2400+12=0\text{x}2400+12= 0x240C r0 = 0x00000B02 0x2400
2 LDR r4, [r1, #-8] 0x2400−8=0\text{x}2400-8= 0x23F8 r4 = 0x0000AA01 0x2400
3 LDR r5, [r1, r2, LSL #2] 0x2400+5×4=0\text{x}2400+5\times4= 0x2414 r5 = 0x0000D004 0x2400
4 LDR r6, [r1, #16]! 0x2410 r6 = 0x000CC003 0x2410
5 LDR r7, [r1], #4 0x2410 (the base before the update) r7 = 0x000CC003 0x2414
6 STR r3, [r1, #-4]! 0x2414−4=0\text{x}2414-4= 0x2410 memory 0x2410 ←\leftarrow 0x0000003F 0x2410

In instruction 3, 5×4=20=5\times4=20= 0x14. Instructions 4 and 5 read the same word, but they leave different values in r1 (0x2410 after the first, 0x2414 after the second, because the second starts from 0x2410). In instruction 6 the first register of STR is the source: it is read, and its value 0x0000003F overwrites the word at 0x2410, which was 0x000CC003.

b) Bytes and half-words. The memory is little endian (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 →): the least significant byte of the word is at the lowest address, so the four bytes are 0xA5 at 0x2500, 0xF2 at 0x2501, 0x73 at 0x2502 and 0x8C at 0x2503.

  • LDR r4, [r5] reads the whole word: 0x8C73F2A5.
  • LDRB r4, [r5] reads the byte at 0x2500 and extends it with zeros: 0x000000A5 =10×16+5=165=10\times16+5=165.
  • LDRSB r4, [r5, #1] reads the byte 0xF2 at 0x2501, whose sign bit is 11, and extends the sign: 0xFFFFFFF2, that is 242−256=−14242-256=-14.
  • LDRSB r4, [r5, #3] reads 0x8C at 0x2503: 0xFFFFFF8C, that is 140−256=−116140-256=-116.
  • LDRH r4, [r5, #2] reads the half-word at 0x2502, made of the bytes 0x73 (low) and 0x8C (high), that is 0x8C73, zero-extended: 0x00008C73 =35955=35955.
  • LDRSH r4, [r5, #2] reads the same half-word with the sign extension: 0xFFFF8C73, that is 35955−65536=−2958135955-65536=-29581.

Check on the sign: in a signed load the bit 77 (byte) or the bit 1515 (half-word) is copied into all the upper bits; with LDRB or LDRH the upper bits are always zeros.

c) The copy loop (Arrays, Strings and Structures in ARM AssemblyArrays in memory and the address of an element; walking with an index or with a pointer; worked examples (sum, maximum, copy); zero-terminated strings and length computation; row-major matrices; structs with field offsets and alignment.Arrays, Strings and Structures in ARM Assembly → for the walk with a pointer). Part (1) must read the word at the address held in r0 and then advance the pointer by 44 bytes, a word: LDR r12, [r0], #4 (post-indexed). Part (2) writes the word with the same step in dst: STR r12, [r1], #4. Part (3) must put in r1 the address of dst: LDR r1, =dst, the pseudo-instruction that loads an address; LDR r1, dst would read the contents.

Trace, with src at 0x2000 and dst at 0x2010:

iteration r2 at the start address read value address written r3 after the ADD
1 44 0x2000 1212 0x2010 1212
2 33 0x2004 −5-5 0x2014 77
3 22 0x2008 4040 0x2018 4747
4 11 0x200C 77 0x201C 5454

After the fourth iteration r2 =0=0, so CMP r2, #0 sets Z and BEQ end is taken. The pointer r0 has advanced by 4×4=164\times4=16 bytes, from 0x2000 to 0x2010 (it now points to dst, which follows src in memory); r1 is 0x2010 +16=+16= 0x2020; the function returns r0 =54=54 (12−5+40+712-5+40+7); the words of dst are 12,−5,40,712,-5,40,7 (the initial 99 was overwritten).

With the pre-indexed LDR r12, [r0, #4]! the address is updated before the access: the reads are at 0x2004, 0x2008, 0x200C and 0x2010. The first element is skipped and one word is read after the end of src, which is the first word of dst; but the first iteration has already written −5-5 at 0x2010, so that word is −5-5 and not the initial 99. The sum is −5+40+7−5=37-5+40+7-5=37. Typical mistakes: using step 11 instead of 44 for an array of words; confusing the pre-indexed and post-indexed forms (the address read is different); writing LDR r1, dst to get the address; answering 99 for the last word read in the pre-indexed case, forgetting that dst has just been modified.

Exercise 2 (points 6)

A byte-addressed memory has 16-bit addresses (a word is one byte). A cache of 6464 bytes has blocks of 88 bytes, is 22-way set associative with LRU replacement and is initially empty. Ten accesses are made (R = read, W = write, addresses in hexadecimal):

# 1 2 3 4 5 6 7 8 9 10
op R W W W W R W R W R
address 1208 1209 120A 120B 3A0C 5A08 3A0D 1208 3A0E 3A0C

a) Compute the number of lines and of sets and the widths of the fields tag, set and word. What is the maximum memory that a cache with the same set and word fields and a 99-bit tag could manage? For each access give tag, set and word. How many different blocks are accessed, and how many of them compete for the same set? (2 points)

b) The cache uses write-through and no-write-allocate (a write to memory is one word). Trace the accesses: hit or miss, content of the set (from the least recently used), accesses to memory. Give hits, block loads and writes to memory. (2 points)

c) Repeat the trace with write-back and write-allocate (dirty blocks are written back as a whole block). Compare the two policies on the number of accesses to memory and on the bytes exchanged. Finally, classify each miss of this trace as compulsory or conflict, and say what removes the conflict miss and what that remedy costs. (2 points)

Show the result

a) 88 lines, 44 sets; word 33 bits, set 22 bits, tag 16−2−3=1116-2-3=11 bits; with a 99-bit tag the memory is 29+2+3=214=163842^{9+2+3}=2^{14}=16384 bytes. All ten accesses are in set 11: 1208, 1209, 120A, 120B have tag 0x090, word 0,1,2,30,1,2,3; 3A0C, 3A0D, 3A0E, 3A0C have tag 0x1D0, word 4,5,6,44,5,6,4; 5A08 has tag 0x2D0, word 00. Three different blocks, all in set 11, which has only 22 lines.

b) Write-through, no-write-allocate: M, H, H, H, M, M, M, H, M, M; 44 hits; 33 block loads (accesses 11, 66, 1010); 66 writes of one word to memory (accesses 22, 33, 44, 55, 77, 99). Final set 11: 0x090, 0x1D0 (most recent last).

c) Write-back, write-allocate: M, H, H, H, M, M, H, M, H, H; 66 hits; 44 block loads (accesses 11, 55, 66, 88); 11 write-back of a block (0x090 at access 66). Accesses to memory: 3+6=93+6=9 against 4+1=54+1=5 (a dirty block 0x1D0 is still in the cache). Bytes: 24+6=3024+6=30 against 32+8=4032+8=40. Misses 11, 55, 66 are compulsory (first access to the blocks 0x090, 0x1D0, 0x2D0); miss 88 is a conflict miss (0x090 was replaced at access 66 although only 33 of the 88 lines are in use): more ways remove it (it disappears with full associativity), at the cost of more tags compared in parallel and a longer access cycle.

Show the full solution

a) Fields (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 →). With capacity C=64C=64 bytes and blocks of K=8K=8 bytes:

lines=CK=648=8,sets=linesn=82=4,word=log⁡28=3,set field=log⁡24=2,\text{lines}=\frac{C}{K}=\frac{64}{8}=8,\qquad \text{sets}=\frac{\text{lines}}{n}=\frac{8}{2}=4,\qquad \text{word}=\log_2 8=3,\qquad \text{set field}=\log_2 4=2,

tag=16−2−3=11 bits.\text{tag}=16-2-3=11\ \text{bits}.

If the tag had 99 bits, the address would have 9+2+3=149+2+3=14 bits and the memory managed would be 214=163842^{14}=16384 bytes =16=16 KB. Example of decomposition: 0x1209 =0001 0010 0000 10012=0001\,0010\,0000\,1001_2; the last 33 bits are 001001 (word 11), the next 22 bits are 0101 (set 11), the first 1111 bits are 000 1001 0000=000\,1001\,0000= 0x090 (tag). The block address is the address divided by 88 (without the word field), and the set is the block address modulo 44:

# address block tag set word
1 1208 0x241 0x090 1 0
2 1209 0x241 0x090 1 1
3 120A 0x241 0x090 1 2
4 120B 0x241 0x090 1 3
5 3A0C 0x741 0x1D0 1 4
6 5A08 0xB41 0x2D0 1 0
7 3A0D 0x741 0x1D0 1 5
8 1208 0x241 0x090 1 0
9 3A0E 0x741 0x1D0 1 6
10 3A0C 0x741 0x1D0 1 4

The blocks are 33 (0x241, 0x741, 0xB41), all in set 11 (they have the same set bits and different tags), and set 11 has only 22 lines: the conflict is built into the sequence. Accesses 11 to 44 and accesses 55, 77, 99, 1010 share a block: the word field does not decide hit or miss.

b) Write-through, no-write-allocate. The list shows set 11 from the least recently used block to the most recently used (the other sets stay empty).

# access result set 1 after memory
1 R 1208 miss 090 load block
2 W 1209 hit 090 write 1 word
3 W 120A hit 090 write 1 word
4 W 120B hit 090 write 1 word
5 W 3A0C miss 090 write 1 word only (no allocation)
6 R 5A08 miss 090, 2D0 load block
7 W 3A0D miss 090, 2D0 write 1 word only
8 R 1208 hit 2D0, 090 none
9 W 3A0E miss 2D0, 090 write 1 word only
10 R 3A0C miss 090, 1D0 load block, replaces 2D0 (the least recently used)

On a write hit the datum goes to the cache and to memory, so memory is always up to date and a replaced block is never written back. On a write miss with no-write-allocate the write goes only to memory and the block is not brought in: that is why accesses 77, 99 and 1010 to the block 0x1D0 all miss. Result: 44 hits (accesses 22, 33, 44, 88), 66 misses, 33 block loads and 66 writes of one word.

c) Write-back, write-allocate. A write hit changes only the cache and sets the dirty bit (marked ∗*); on a write miss the block is first loaded and then written.

# access result set 1 after memory
1 R 1208 miss 090 load block
2 W 1209 hit 090* none
3 W 120A hit 090* none
4 W 120B hit 090* none
5 W 3A0C miss 090*, 1D0* load block, then write in the cache
6 R 5A08 miss 1D0*, 2D0 the set is full and the least recently used is 090*, dirty: write back the whole block, then load
7 W 3A0D hit 2D0, 1D0* none
8 R 1208 miss 1D0*, 090 the least recently used is 2D0, clean: no write-back; load block
9 W 3A0E hit 090, 1D0* none
10 R 3A0C hit 090, 1D0* none

Result: 66 hits (accesses 22, 33, 44, 77, 99, 1010), 44 block loads and 11 write-back; the dirty block 0x1D0 stays in the cache and would be written back only when it is replaced.

Comparison. Accesses to memory: write-through 33 loads + 6+\ 6 writes =9=9; write-back 44 loads + 1+\ 1 write-back =5=5: the three writes to 0x090 and the three to 0x1D0 become one single transfer each time the block is replaced. The price is that a write-back moves a whole block: bytes exchanged are 3×8+6×1=303\times8+6\times1=30 for write-through and 4×8+1×8=404\times8+1\times8=40 for write-back (as a count of transfers write-back is better, in bytes it is not in this short sequence: the pending dirty block would add 88 more). Write-allocate is also why accesses 77, 99 and 1010 hit.

Kinds of misses (write-back trace). A miss is compulsory when it is the first access to the block: accesses 11, 55 and 66 (blocks 0x090, 0x1D0, 0x2D0). Access 88 asks again for 0x090, which was already in the cache and was replaced at access 66; but the cache has 88 lines and has held only 33 blocks, so the block was lost because too many blocks compete for the same set: it is a conflict miss. With full associativity it would not occur (the three blocks fit in the 88 lines, and access 88 would be a hit). The remedy is higher associativity (more ways, or a victim cache); its cost is that more tags are compared in parallel and the access cycle gets longer.

Typical mistakes: using the word field to decide hit or miss; computing the tag as 16−316-3 and forgetting the set field; allocating the block on a write miss with write-through; forgetting that a replaced dirty block is written back whole (all 88 bytes), and writing back the clean ones.

Exercise 3 (points 6)

External memories (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 →).

a) A storage system needs n=8n=8 disks of data. For RAID levels 00, 11, 33, 55 and 66 give the total number of disks and the number of failures tolerated. Which level has a dedicated parity disk with a write bottleneck, and how does RAID 55 remove it? Which level can survive the failure of any two disks? (2 points)

b) In a RAID level 44 with four data disks, the blocks in the same position are D1=1011 0110D_1=1011\,0110, D2=0110 1001D_2=0110\,1001, D3=1110 0011D_3=1110\,0011, D4=0101 1100D_4=0101\,1100. Compute the parity block PP. The third disk fails: reconstruct D3D_3 from the other blocks and the parity. (2 points)

c) A disk turns at 10 00010\,000 revolutions per minute; a track holds 384384 KB and the average seek time is 6.06.0 ms. Compute the revolution time and the average rotational latency. Compute the access time to read 9696 KB stored in consecutive sectors of one track. Then compute it if the same 9696 KB are spread over the tracks of a cylinder of 66 surfaces read in parallel, and the fraction of the time due to positioning and latency in the two cases. (2 points)

Show the result

a) RAID 00: 88 disks, 00 failures; RAID 11: 1616, 11 per mirrored pair; RAID 33 and RAID 55: 99, 11; RAID 66: 1010, 22. RAID 44 has the dedicated parity disk, a write bottleneck; RAID 55 distributes the parity over all the disks. Only RAID 66 survives any two failures.

b) P=0110 0000P=0110\,0000; D3=D1⊕D2⊕D4⊕P=1110 0011D_3=D_1\oplus D_2\oplus D_4\oplus P=1110\,0011.

c) Trev=6T_{rev}=6 ms, latency 33 ms. One track: transfer 96384×6=1.5\tfrac{96}{384}\times6=1.5 ms, T=6+3+1.5=10.5T=6+3+1.5=10.5 ms; cylinder of 66 surfaces: transfer 1.5/6=0.251.5/6=0.25 ms, T=9.25T=9.25 ms. Positioning and latency: 9/10.5≈869/10.5\approx86 per cent and 9/9.25≈979/9.25\approx97 per cent.

Show the full solution

a) RAID levels. RAID needs the following disks for nn disks of data (the table of the note): level 00 (striping, no redundancy) n=8n=8, no failure tolerated; level 11 (mirroring) 2n=162n=16, one failure per pair; levels 33, 44, 55 (one parity disk, or parity distributed) n+1=9n+1=9, one failure; level 66 (two independent redundancy computations) n+2=10n+2=10, two failures. The level with block striping and a dedicated parity disk is level 44: every write must also update the parity disk, which becomes the bottleneck. Level 55 is like level 44 but with the parity distributed over all the disks, so the parity writes are spread. The failure of any two disks is survived only by level 66 (level 11 survives two failures only if they are not in the same mirrored pair, so it does not guarantee it).

b) Parity and reconstruction. The parity is the XOR of the data blocks, bit by bit:

D1⊕D2=1011 0110⊕0110 1001=1101 1111,D_1\oplus D_2=1011\,0110\oplus0110\,1001=1101\,1111,

1101 1111⊕D3=1101 1111⊕1110 0011=0011 1100,1101\,1111\oplus D_3=1101\,1111\oplus1110\,0011=0011\,1100,

0011 1100⊕D4=0011 1100⊕0101 1100=0110 0000=P.0011\,1100\oplus D_4=0011\,1100\oplus0101\,1100=0110\,0000=P.

If the third disk fails, each of its blocks is the XOR of the corresponding blocks of the other disks, parity included:

D1⊕D2=1101 1111,1101 1111⊕D4=1101 1111⊕0101 1100=1000 0011,D_1\oplus D_2=1101\,1111,\qquad 1101\,1111\oplus D_4=1101\,1111\oplus0101\,1100=1000\,0011,

1000 0011⊕P=1000 0011⊕0110 0000=1110 0011=D3.1000\,0011\oplus P=1000\,0011\oplus0110\,0000=1110\,0011=D_3.

Check: this is exactly the lost block. The same procedure reconstructs any other single failed disk (the parity disk too).

c) Access time. One revolution lasts Trev=6010 000 s=6T_{rev}=\dfrac{60}{10\,000}\ \text{s}=6 ms, and the average latency is half a revolution:

Tlatency=12×6=3 ms.T_{latency}=\frac12\times6=3\ \text{ms}.

Reading D=96D=96 KB on a track of N=384N=384 KB takes a fraction D/N=96/384=1/4D/N=96/384=1/4 of a revolution:

Ttransfer=DN Trev=14×6=1.5 ms,Taccess=Tseek+Tlatency+Ttransfer=6.0+3+1.5=10.5 ms.T_{transfer}=\frac{D}{N}\,T_{rev}=\frac14\times6=1.5\ \text{ms},\qquad T_{access}=T_{seek}+T_{latency}+T_{transfer}=6.0+3+1.5=10.5\ \text{ms}.

With the data spread over a cylinder of 66 surfaces read in parallel the transfer is divided by 66: 1.5/6=0.251.5/6=0.25 ms, so Taccess=6.0+3+0.25=9.25T_{access}=6.0+3+0.25=9.25 ms. The fraction due to positioning and latency is (6.0+3)/10.5=0.857(6.0+3)/10.5=0.857, about 8686 per cent, in the first case, and 9/9.25=0.9739/9.25=0.973, about 9797 per cent, in the second: positioning and latency dominate, so reading contiguous data is far more efficient than reading scattered data.

Typical mistakes: using the time of a whole revolution (66 ms) as latency instead of half; forgetting that the dedicated parity disk belongs to levels 33 and 44 and that RAID 55 distributes it; computing the parity with OR or AND instead of XOR; forgetting that the seek time is paid once whatever the amount of data.

Self-assessment grid
Item Points What must be there for full points Typical mistakes
Question 1 3 formats R, I, J with all field widths and uses (1); 3232 registers, sign extension and −256-256 (1); word address times 44, 0x130C0, 256256 MB (1) jump field taken as a byte address; zero extension of the immediate
Question 2 3 Binvert with carry in 11, NOR with Ainvert and Binvert, slt, zero, overflow (1); b‾\overline b, result −35-35 and flags N, Z, C, V (1.5); NOR result 0001 (0.5) carry in forgotten; borrow read as error; zero as AND of the bits
Question 3 3 how each of the five types is written and erased (1); the five choices (1); flash in SSDs and USB sticks, typical uses (1) EEPROM and flash exchanged; PROM rewritable
Question 4 3 the five registers, usually not directly accessible (0.5); contents of the PSW, supervisor bit, exception (1.5); result and flags N, Z, C, V with the reason for V (1) supervisor bit changed by a user program; carry read as overflow
Exercise 1 6 a) 2 points: six effective addresses, values and r1; b) 2 points: six register contents in hexadecimal and decimal; c) 2 points: three missing parts, trace table, final values, pre-indexed case with 3737 pre-indexed and post-indexed confused; LDRB for a signed byte; step 11 instead of 44; LDR r1, dst
Exercise 2 6 a) 2 points: 88 lines, 44 sets, fields 11/2/311/2/3, 1616 KB, table, three blocks in set 11; b) 2 points: write-through trace, 44 hits, 33 loads, 66 writes; c) 2 points: write-back trace, 66 hits, 44 loads, 11 write-back, comparison 99 and 55 accesses, 3030 and 4040 bytes, compulsory and conflict misses word field used to decide a hit; allocation on a write miss in write-through; missing write-back
Exercise 3 6 a) 2 points: disks and failures of the five levels, RAID 44 against RAID 55, level 66; b) 2 points: P=0110 0000P=0110\,0000 and D3=1110 0011D_3=1110\,0011; c) 2 points: 66 ms, 33 ms, 10.510.5 ms, 9.259.25 ms, 8686 and 9797 per cent revolution time as latency; OR instead of XOR; RAID 55 with a dedicated parity disk
Total 30