TactivergeLite

Lite / Engineering Complex Systems / Week 33

Registers and bit masks: where software touches silicon

One line of C turns on a motor driver. Which single bit, at which address, did it change, and what else could it break?

About 40 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

Every optimisation in software eventually becomes bits in a register: a clock divider, a sleep mode, a PWM duty. Integration bugs live at this boundary, where a correct algorithm meets a wrongly set bit.

Predict

A control register holds 0b1010_0011. You want to set bit 2 without changing the others. Which operation?

  1. reg = 0b0000_0100
  2. reg = reg | 0b0000_0100
  3. reg = reg & 0b0000_0100
  4. reg = reg ^ 0b1111_1011
Guess first, then reveal

OR with a mask that has only bit 2 set: 1010_0011 | 0000_0100 = 1010_0111. Plain assignment would wipe the other seven bits, which may have been turning on clocks or pins elsewhere.

Discover

In Silicon Studio, build or inspect a small register made of flip-flops with an enable. Write a value, then change a single bit using a mask built from gates. Notice that a 'register' in firmware is exactly this: storage whose outputs drive real hardware.

What the interactive shows (text version)

In Silicon Studio the learner works with a register built from flip-flops and changes one bit by combining the stored value with a mask through logic gates: OR to set, AND with an inverted mask to clear, XOR to toggle. The point: a firmware register is real storage whose outputs control hardware.

You can also open the interactive on the full site when you have a faster connection.

Understand

Memory-mapped I/O

Microcontrollers give each peripheral (GPIO, timers, ADC, UART) a block of addresses. Writing to an address doesn't store data in RAM; it sets flip-flops whose outputs drive the hardware: a pin's direction, a timer's prescaler, an interrupt enable. Reading may return live hardware state, such as whether a conversion has finished.

The four mask operations

Set bit n: reg |= (1 << n). Clear: reg &= ~(1 << n). Toggle: reg ^= (1 << n). Test: (reg & (1 << n)) != 0. Multi-bit fields (a 3-bit prescaler at bits 4–6) are cleared with a field mask, then the new value is ORed in, shifted into place. Datasheets give each bit's meaning; the code should name them, not use magic numbers.

Read–modify–write hazards

reg |= mask is really three steps: read, OR, write. If an interrupt changes the same register between the read and the write, its change is lost. Fixes: disable interrupts briefly around the update, or use hardware 'set' and 'clear' registers that change only the bits you write as 1. Some status bits clear when read, so even reading has side effects.

Integration means checking both sides

A firmware unit test can't see a wrongly configured clock. Integration tests run real code on real (or accurately simulated) hardware and check observable behaviour: the pin toggles at the expected frequency, the ADC reads a known voltage correctly. Write down what each register should contain and check it in a debugger.

Explain it another way

Intuitive

A register is a row of light switches on a wall you can't see behind. Each switch turns something on in the building. Flip the one you need without brushing the others.

Technical

Volatile access: registers are declared volatile so the compiler doesn't cache or reorder accesses. Bit-band or atomic set/clear registers avoid read–modify–write races on some architectures.

Mathematical

Field update: reg = (reg & ~(m << s)) | ((v & m) << s), where m = 2^w − 1 for a w-bit field at shift s. Example: w = 3, s = 4, v = 5: clear mask ~0x70, value 0x50.

Applied

Low-power modes are entered by setting a few register bits; a mistaken bit can leave a peripheral clock running and quietly double a sensor node's sleep current, ruining a battery-life budget.

Worked example 1

reg = 0x5A (0101_1010). Clear bit 3, then toggle bit 0. Result?

Clear bit 3: 0101_1010 & 1111_0111 = 0101_0010 (0x52). Toggle bit 0: 0101_0011 = 0x53.

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

Practice 1. Which C expression clears bit 5 of reg and leaves every other bit unchanged?

Practice 4. Main code does PORT |= LED_BIT while an interrupt handler sets MOTOR_BIT in the same register. Occasionally the motor bit is lost. Why?

Apply it

Write a short configuration routine for a hypothetical timer (enable bit 0, 3-bit prescaler at bits 4–6, interrupt enable bit 7) using named masks. Show the register value after each line in hex and binary.

Reflect

Why do datasheets matter more than code comments when debugging at this level?

Write your answer on paper or in your own notes. Lite never stores what you write.

← Light-time and the data budget: why rovers decide for themselvesPolling or interrupts? Latency, energy and shared data →