C++ / a working model

148 / 163   ·   C11   ·   8 min

Flash-based SSDs

Keep this sentence

This chapter introduces how NAND flash forms modern solid-state drives, focusing on the physical requirement to erase an entire block before programming any page, cell wear-out, and how these traits shape storage-system design.

In this lesson
  1. How Transistors Hold Data
  2. Organization of Pages and Erase Blocks
  3. The Read, Erase and Program Operations
  4. Performance Numbers and Wear Lifetime
  5. Example
  6. Exercise

Official chapter PDF

How Transistors Hold Data

A flash cell traps electrons on a floating gate to encode information. Single-level cells distinguish only presence or absence of charge; multi- and triple-level cells use several voltage thresholds to store two or three bits, raising density at the cost of speed and endurance. Charge remains after power loss, making the technology suitable for persistent storage.

Organization of Pages and Erase Blocks

A chip is partitioned into planes that contain many erase blocks; each block is further divided into pages of a few kilobytes. Reads and programs operate on pages, yet charge can be reset only at block granularity. The mismatch forces higher-level software to treat flash as an append-only medium.

The Read, Erase and Program Operations

Any page can be read randomly in tens of microseconds. Programming is allowed only on an already-erased page, takes hundreds of microseconds, and changes selected 1s into 0s. Erasing a whole block takes milliseconds, restoring every cell to 1 so that pages may be programmed again. Once programmed, a page can be altered only by erasing its entire block.

Performance Numbers and Wear Lifetime

SLC flash reads in about 25 µs, programs in 200-300 µs and erases in 1.5-2 ms; MLC and TLC are slower. Every erase-program cycle slightly damages the insulator, eventually rendering a cell unusable. SSD firmware must therefore implement wear leveling and garbage collection to deliver acceptable lifetime and stable performance.

Pitfalls

  • Treating a flash page as an in-place overwritable disk sector
  • Forgetting to copy live pages out of a block before erasing it
  • Ignoring speed and endurance differences among SLC, MLC and TLC cells

Run an example

Minimum C11 · complete program · Download .c

#include <stdio.h>
#include <stdint.h>

#define NPAGES 4

typedef enum { ST_INVALID, ST_ERASED, ST_VALID } state_t;

typedef struct {
    uint8_t data;
    state_t st;
} page_t;

static page_t blk[NPAGES];

static void dump(void) {
    printf("  ");
    for (int i = 0; i < NPAGES; i++) {
        const char *s = (blk[i].st == ST_INVALID) ? "INV" :
                        (blk[i].st == ST_ERASED) ? "ERS" : "VAL";
        printf("[%s %02X] ", s, blk[i].data);
    }
    printf("\n");
}

static void do_erase(void) {
    printf("ERASE block\n");
    for (int i = 0; i < NPAGES; i++) {
        blk[i].data = 0xFF;
        blk[i].st = ST_ERASED;
    }
    dump();
}

static void do_program(int pg, uint8_t val) {
    printf("PROGRAM page %d <- %02X\n", pg, val);
    if (pg < 0 || pg >= NPAGES || blk[pg].st != ST_ERASED) {
        printf("  ERROR: page not erasable\n");
        return;
    }
    blk[pg].data = val;
    blk[pg].st = ST_VALID;
    dump();
}

static void do_read(int pg) {
    printf("READ page %d\n", pg);
    if (pg < 0 || pg >= NPAGES || blk[pg].st != ST_VALID) {
        printf("  (not valid)\n");
        return;
    }
    printf("  data = %02X\n", blk[pg].data);
}

int main(void) {
    for (int i = 0; i < NPAGES; i++) {
        blk[i].data = 0x00;
        blk[i].st = ST_INVALID;
    }
    printf("INIT\n");
    dump();
    do_erase();
    do_program(0, 0xA5);
    do_program(0, 0x5A);
    do_program(2, 0x3C);
    do_read(0);
    do_read(1);
    do_erase();
    return 0;
}

Compile locally

gcc -std=c11 -Wall -Wextra -Wpedantic -Werror ostep-44-ssd.c -o example && ./example

Expected result

INIT
  [INV 00] [INV 00] [INV 00] [INV 00] 
ERASE block
  [ERS FF] [ERS FF] [ERS FF] [ERS FF] 
PROGRAM page 0 <- A5
  [VAL A5] [ERS FF] [ERS FF] [ERS FF] 
PROGRAM page 0 <- 5A
  ERROR: page not erasable
PROGRAM page 2 <- 3C
  [VAL A5] [ERS FF] [VAL 3C] [ERS FF] 
READ page 0
  data = A5
READ page 1
  (not valid)
ERASE block
  [ERS FF] [ERS FF] [ERS FF] [ERS FF] 

CHECK YOUR UNDERSTANDING

Close the answer. Explain it.

What low-level steps are required to update data in an already-programmed page, and why can the page not simply be reprogrammed?

Show a reference answer

All still-needed pages in the block must first be copied elsewhere, the entire block erased, then the new page programmed. Programming can only change 1s to 0s, never 0s back to 1s, and is permitted solely on erased pages.

Check the sources

Drafts and official chapters change. The version mark is only the example’s minimum.

Back to the catalog