98. Clock-Dependent Peripheral Design

In embedded firmware, hardware peripherals must never be accessed unless their clock is enabled.
Accessing a peripheral before enabling its clock can result in bus faults, undefined behavior, or system resets.

Your task is to encode this rule directly into C++ class relationships using dependency-aware composition, such that invalid usage is impossible by design.

The following conditions must be enforced structurally, not by comments or runtime checks:

  • The clock is enabled before the peripheral becomes usable
  • The peripheral cannot exist without a valid clock
  • The driver owns and controls the lifetime of both the clock and the peripheral
  • Shutdown occurs in the reverse-safe order

Any solution that violates these rules is incorrect, even if it compiles.

Program Flow

  1. Read two integers:
    • Register offset
    • Value to write
  2. Create a Driver object inside a local scope
  3. Clock is enabled
  4. Peripheral becomes ready using the clock
  5. Driver performs one register write
  6. Scope ends
  7. Driver shuts down
  8. Peripheral shuts down
  9. Clock shuts down

Input

Two space-separated integers:

  • offset — register offset
  • value — value to write

Output

The program must print exactly the following lines, in order:

Clock enabled
Peripheral ready
Driver started
Clocked write: <offset> <value>
Driver stopped
Peripheral stopped
Clock disabled

Constraints

  • offset and value are non-negative integers
  • 0 ≤ offset ≤ 1024
  • 0 ≤ value ≤ 65535
  • Exactly one write operation must occur
  • ClockController must not be created in main()
  • Peripheral must not create or own a clock
  • Peripheral must depend on an existing clock instance
  • The clock must outlive the peripheral
  • No dynamic allocation
  • No smart pointers
  • No inheritance
  • Output format must match exactly

 

 

Loading...

Input

8 55

Expected Output

Clock enabled Peripheral ready Driver started Clocked write: 8 55 Driver stopped Peripheral stopped Clock disabled