Flash and One-Time Programmable Bits

Two kinds of memory keep their content with the power off and can still be written. Each can change a bit in one direction only.

You Will Be Able To

  • Work out what a flash byte holds after an erase and after one or two writes.
  • Compare flash on the chip with a flash chip beside it, and say where the program runs.
  • Say what one-time programmable bits can and cannot do, and what they are used for.
Lesson
S3.13
Time
24 min
Runs on
Your browser
Check
6 questions

A device saves the value 5a in its flash. Later it saves a5 in the same place. After the next power-up it reads back 00. Both writes were reported as done, and neither value is there.

Before You Start

You need S3.1: the six kinds of memory and the three questions. You need S0.7: what flash is for.

You also need binary numbers, and the patience to compare two of them bit by bit.

Five questions this page answers
Question
1What can an erase do in a flash memory, and what can a write do?
2Why does changing one byte of flash cost a whole block?
3Why must a design save erases, and how does it do so?
4What differs between flash on the chip and a flash chip beside it?
5What can one-time programmable bits do, and what are they for?

The Idea: Memory That Changes in One Direction

An SRAM bit can be set and cleared as often as you like, and it forgets everything at power-off. Memory that survives is different. Its bits are changed by force, with a high voltage, and the force works in one direction.

In flash, the easy direction is from 1 to 0. A write can clear any bits it likes. To get the 1s back, the chip must erase, and an erase is a blunt tool: it covers a whole block of bytes at once. So changing one byte can mean saving the block, erasing it, and writing everything back.

Each erase also wears the block a little. After some tens of thousands of erases, a block stops holding its bits. A design that erases once a second will kill its flash within days. So erases are counted and saved, like money.

Where the flash sits is a second choice. Flash needs extra steps when the chip is made. A chip can pay for those and carry its flash inside. Or it can use a cheap flash chip beside it on the board. The second way is slower to read, so the program is usually copied into SRAM at start-up.

The third idea is a memory that goes the other way and has no erase at all. A one-time programmable bit starts at 0. Burning it makes it 1 for ever. That sounds like a weakness, and it is exactly the point: nobody, not even the maker, can undo it.

New Words

New words on this page
WordMeaning
ANDA way to combine two binary numbers, bit by bit. A result bit is 1 only where both numbers have a 1.
ORAnother way to combine two binary numbers. A result bit is 1 where at least one number has a 1.
EraseThe flash operation that sets every bit of one block to 1.
Erase blockThe group of bytes that one erase covers. It is far larger than a word: often thousands of bytes.
Write to flashThe operation that clears chosen bits to 0. It is also called programming. It cannot set a bit to 1.
EnduranceThe number of erases that a block survives before it stops holding its bits.
Embedded flashFlash that is built into the same chip as the processor.
External flashA flash chip of its own on the board, outside the main chip. It is read one bit at a time, over a few wires.
Execute in placeTo run a program straight from the flash that holds it, with no copy in RAM.
FuseA tiny link inside a chip that a strong current can change for ever. One fuse holds one one-time bit.
BurnTo change a fuse. A burnt bit reads as 1 from then on.
Version floorA count kept in one-time bits. A device refuses any program with a version number below it.

One Picture: One Block, Two Homes, and the Fuses

Flash Clears Bits, and Fuses Set ThemStep through the picture. It starts with one block of flash, then shows where flash sits, and ends with one-time bits.
  • Processor
  • Memory
  • Control / FSM
Flash Clears Bits, and Fuses Set ThemStep through the picture. It starts with one block of flash, then shows where flash sits, and ends with one-time bits. component A program wants to keep somebytes component Erase block bytes that areerased together component Flash on the chip read like any memory component Flash chip on theboard read bit by bit, over afew wires component SRAM a copy of theprogram, made atstart-up component One-time bits each can be set once erase: every bit to 1write: clears bitsonlymany blocksmany blockscopied onceburn: sets bits only
  1. component A programwants to keep some bytes
    • erase: every bit to 1 → Erase block
    • write: clears bits only → Erase block
    • burn: sets bits only → One-time bits
  2. component Erase blockbytes that are erased together
    • many blocks → Flash on the chip
    • many blocks → Flash chip on the board
  3. component Flash on the chipread like any memory
  4. component Flash chip on the boardread bit by bit, over a few wires
    • copied once → SRAM
  5. component SRAMa copy of the program, made at start-up
  6. component One-time bitseach can be set once
  1. An erase

    Flash is divided into blocks. An erase works on a whole block, and leaves every bit of it at 1.

  2. A write

    A write can change a 1 into a 0. It cannot change a 0 into a 1. So a byte can be written once after each erase.

  3. A second write to the same byte

    The bits that are already 0 stay 0. The byte ends up with the 1s that both values share, which is neither value.

  4. Flash on the chip

    Some chips carry their flash inside. The processor reads it like a ROM, and can run the program straight from it.

  5. A flash chip beside the chip

    Other chips, like ap_soc, use a separate flash chip. It is read bit by bit over a few wires, so the program is copied into SRAM first.

  6. One-time bits

    A one-time bit is the opposite. Burning it sets it to 1, and nothing can clear it. There is no erase at all.

Read the diagram as text
  • A program: wants to keep some bytes
  • Erase block: bytes that are erased together
  • Flash on the chip: read like any memory
  • Flash chip on the board: read bit by bit, over a few wires
  • SRAM: a copy of the program, made at start-up
  • One-time bits: each can be set once
  • A program → Erase block: erase: every bit to 1
  • A program → Erase block: write: clears bits only
  • Erase block → Flash on the chip: many blocks
  • Erase block → Flash chip on the board: many blocks
  • Flash chip on the board → SRAM: copied once
  • A program → One-time bits: burn: sets bits only

The lab below models one erase block of four bytes, and eight one-time bits.

The Smallest Example, Worked by Hand

One block of four bytes. Follow byte 0 through an erase and two writes. A write leaves a 1 only where the old byte and the new value both have a 1. That is the AND of the two.

Byte 0 of a flash block, step by step
StepValue givenByte 0 beforeByte 0 afterWhy
eraseanything1111 1111 (ff)an erase sets every bit
write0101 1010 (5a)1111 11110101 1010 (5a)four bits cleared
write again1010 0101 (a5)0101 10100000 0000 (00)the two values share no 1
erase0000 00001111 1111 (ff)the only way back, and it takes the other three bytes too
write1010 0101 (a5)1111 11111010 0101 (a5)now the value is stored

This is the device from the top of the page. Its second save needed an erase first.

Now eight one-time bits, all 0 in a new chip. A burn leaves a 1 wherever the old bits or the new value have a 1. That is the OR of the two.

Eight one-time bits, step by step
StepValue burntBits afterBits that are 1
new chip0000 00000
burn0000 00010000 00011
burn0000 00100000 00112
burn, to go back to one bit0000 00010000 00112

The count of 1s can go up and can never go down. That makes it a version floor.

The Rules

  1. Rule 1: An erase sets every bit of a block to 1.

    It always covers the whole block. No smaller erase exists.

  2. Rule 2: A write to flash can only clear bits.

    The byte becomes the AND of what it held and what was written.

  3. Rule 3: So a byte can be written once after each erase.

    To change it again, save the block, erase it, and write everything back.

  4. Rule 4: Every erase wears the block.

    A design counts its erases, and spreads them over many blocks.

  5. Rule 5: Flash on the chip is read like a ROM. A flash chip beside it is read bit by bit.

    So a program in embedded flash can run in place. A program in external flash is usually copied to RAM first.

  6. Rule 6: A one-time bit can be set and never cleared.

    The bits become the OR of what they held and what was burnt. There is no erase.

Sources for These Rules

The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.2, "Flash Memory": flash is an electrically erasable ROM, writes wear its bits out, and a controller spreads the writes.

The textbook "CMOS VLSI Design" by Weste and Harris, section 12.4.3. It covers the erase of a block at a time, the erased bit that reads as 1, and endurance. It also covers flash built into a chip with extra manufacturing steps. Fuses and antifuses are in section 3.4.3.8.

The textbook "Digital Design and Computer Architecture" by Harris and Harris, section 5.5.6: ROM that is programmed once by fuses, and flash erased in blocks.

Rules 1 and 2 describe the usual flash, in which an erased bit reads as 1. The books give a typical endurance of 100,000 erases, and far less for some dense parts.

Rule 6 describes a fuse that reads as 0 when new and as 1 when burnt. Some parts read the other way round. The idea is the same: one direction, once.

The block of four bytes and the eight fuses are our own teaching example. ap_soc has no flash and no one-time bits on the chip.

Now Run It

The lab at the end of this page is test code. It models one erase block of four bytes and eight one-time bits. Each row shows the four bytes after one step, and the number of erases so far.

Printed by the lab on Icarus. The four bytes are printed as byte 0, 1, 2, 3.
flash erase the block          | ff ff ff ff | erases=1
flash write 5a to byte 0       | 5a ff ff ff | erases=1
flash write 3c to byte 1       | 5a 3c ff ff | erases=1
flash write a5 to byte 0       | 00 3c ff ff | erases=1
flash erase the block          | ff ff ff ff | erases=2
flash write a5 to byte 0       | a5 ff ff ff | erases=2
flash write 3c to byte 1       | a5 3c ff ff | erases=2
otp   burn 00000001            | fuses=00000001 | count=1
otp   burn 00000010            | fuses=00000011 | count=2
otp   burn 00000001            | fuses=00000011 | count=2

Rule 1 at work: the first row, where all four bytes are ff. Rule 2 at work: the fourth row. Byte 0 held 5a, the write gave a5, and the byte now holds 00.

Rule 3 at work: the fifth row. The second erase brings byte 0 back to ff. It takes byte 1 with it, so 3c must be written again in the last flash row.

Rule 6 at work: the three rows that start with "otp". Two burns give a count of 2. The third burn tries to go back to one bit, and the count stays at 2.

This lab leaves out time. A real erase takes many milliseconds, and a write takes far longer than a write to SRAM. The lab also has no wires: it does not model how an external flash is read.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Erase by the blockErase each byte aloneFar less erase circuit, so far more bits in the same area. One changed byte costs a block.
A flash chip beside the chipEmbedded flashThe main chip needs no extra manufacturing steps. Reads are slow, and the board has one more part.
Embedded flashA flash chip beside the chipThe program runs in place, at once after reset. The chip costs more to make.
Copy the program to SRAM at start-upExecute in place from external flashFull speed after the copy. The chip needs enough SRAM for the whole program.
A secret in one-time bitsA secret in flashNo program can ever change it. A wrong value can never be corrected.

The Trap: The Count That Wore Out the Flash

A meter must remember a count through a power cut. So its program writes the new count to flash once a second, always to byte 0 of one block. Each new count needs an erase first. Here are eight updates done that way, and then done another way:

The same model with other steps. Each row shows the block after eight updates.
flash count in byte 0, 8 updates | 08 ff ff ff | erases=8
flash next free byte, 8 updates | 05 06 07 08 | erases=2

The first row needed 8 erases for 8 updates. At one update a second, a block with an endurance of 100,000 erases is worn out after about 27 hours.

The second row writes each new count into the next free byte, and erases only when the block is full. The same 8 updates cost 2 erases. With a block of thousands of bytes, the saving is a thousand times larger. Rule 4 asks for exactly this.

At Scale

This page1 block of 4 bytes, 8 fuses
A productthousands of blocks, thousands of fuses

This section looks ahead. Real flash memories are far larger than one block of four bytes, and they hide most of this behind a controller.

  1. Reading an external flash

    The four wires and the commands of a flash chip on the board are in a later lesson.

  2. Copy at start-up

    The Boot ROM of ap_soc copies the program from flash into SRAM: The Boot Flow.

  3. Spreading the wear

    A flash controller moves data between blocks, so that no block is erased far more often than the others.

  4. Keys and locks

    One-time bits hold the key that checks the program, and the lock on the debug port: Secure Boot.

Try It

Run the lab. Check byte 0 in each flash row against the hand table.

Remove the second erase_block. Predict what byte 0 and byte 1 hold at the end.

Add a fourth burn that sets the top bit. Predict the fuses and the count.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: flash erase the block | ff ff ff ff | erases=1 flash write 5a to byte 0 | 5a ff ff ff | erases=1 flash write 3c to byte 1 | 5a 3c ff ff | erases=1 flash write a5 to byte 0 | 00 3c ff ff | erases=1 flash erase the block | ff ff ff ff | erases=2 flash write a5 to byte 0 | a5 ff ff ff | erases=2 flash write 3c to byte 1 | a5 3c ff ff | erases=2 otp burn 00000001 | fuses=00000001 | count=1 otp burn 00000010 | fuses=00000011 | count=2 otp burn 00000001 | fuses=00000011 | count=2

Check Yourself

Predict the outputThe block is erased. Byte 2 is written with f0, and then with 3c, with no erase in between. What does the last row say?
// One erase block of a flash memory (four bytes), and eight one-time programmable bits.
module tb;
  // Flash. An erase sets every bit of the block to 1. A write can only clear bits: it can never set one.
  logic [7:0] flash [0:3];
  int erases = 0;
  task automatic show(input string what);
    $display("flash %-24s | %02h %02h %02h %02h | erases=%0d", what, flash[0], flash[1], flash[2], flash[3], erases);
  endtask
  task automatic erase_block;
    for (int i = 0; i < 4; i++) flash[i] = 8'hff;
    erases++;
    show("erase the block");
  endtask
  task automatic write_byte(input int at, input logic [7:0] value);
    flash[at] = flash[at] & value;            // a bit that is 0 stays 0
    show($sformatf("write %02h to byte %0d", value, at));
  endtask

  // One-time bits. A write can only set bits, and nothing can clear one.
  logic [7:0] fuses = 8'b0000_0000;
  int count;
  task automatic burn(input logic [7:0] value);
    fuses = fuses | value;                    // a bit that is 1 stays 1
    count = $countones(fuses);
    $display("otp   burn %08b            | fuses=%08b | count=%0d", value, fuses, count);
  endtask

  initial begin
    erase_block;
    write_byte(2, 8'hf0);
    write_byte(2, 8'h3c);
  end
endmodule
ConceptWhat does an erase do in a flash memory?
ConceptA program must change one byte in a block of flash that is full of data. What must it do?
ConceptHow does a careful design keep a value that changes often?
ConceptA chip has its program in an external flash on the board. What does it usually do at start-up?
ConceptEight one-time bits hold 0000 0011. A program burns 0000 0001. What do they hold now?

Common Mistakes

Wrong Write a new value over an old one in flash.

A write only clears bits, so the result is the AND of the two.

Fix Erase the block first, or write to a byte that is still ff.

Wrong Keep a value that changes every second in one flash byte.

Each change costs an erase, and erases wear the block out.

Fix Write each new value to the next free place, and erase only a full block.

Wrong Keep a secret that must never change in flash.

A program can erase flash and write it again.

Fix Use one-time programmable bits for content that is written once.

Remember

  • An erase sets a whole block of flash to 1. A write can only clear bits.
  • A flash byte can be written once after each erase, and every erase wears the block.
  • Embedded flash runs a program in place. An external flash is usually copied to SRAM first.
  • A one-time bit can be set and never cleared: right for keys and for counts that only go up.

Practice

Report a problem with this page