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.
| Question | |
|---|---|
| 1 | What can an erase do in a flash memory, and what can a write do? |
| 2 | Why does changing one byte of flash cost a whole block? |
| 3 | Why must a design save erases, and how does it do so? |
| 4 | What differs between flash on the chip and a flash chip beside it? |
| 5 | What 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
| Word | Meaning |
|---|---|
| AND | A way to combine two binary numbers, bit by bit. A result bit is 1 only where both numbers have a 1. |
| OR | Another way to combine two binary numbers. A result bit is 1 where at least one number has a 1. |
| Erase | The flash operation that sets every bit of one block to 1. |
| Erase block | The group of bytes that one erase covers. It is far larger than a word: often thousands of bytes. |
| Write to flash | The operation that clears chosen bits to 0. It is also called programming. It cannot set a bit to 1. |
| Endurance | The number of erases that a block survives before it stops holding its bits. |
| Embedded flash | Flash that is built into the same chip as the processor. |
| External flash | A 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 place | To run a program straight from the flash that holds it, with no copy in RAM. |
| Fuse | A tiny link inside a chip that a strong current can change for ever. One fuse holds one one-time bit. |
| Burn | To change a fuse. A burnt bit reads as 1 from then on. |
| Version floor | A 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
- Processor
- Memory
- Control / FSM
- An erase
Flash is divided into blocks. An erase works on a whole block, and leaves every bit of it at 1.
- 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.
- 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.
- 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.
- 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.
- 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.
| Step | Value given | Byte 0 before | Byte 0 after | Why |
|---|---|---|---|---|
| erase | anything | 1111 1111 (ff) | an erase sets every bit | |
| write | 0101 1010 (5a) | 1111 1111 | 0101 1010 (5a) | four bits cleared |
| write again | 1010 0101 (a5) | 0101 1010 | 0000 0000 (00) | the two values share no 1 |
| erase | 0000 0000 | 1111 1111 (ff) | the only way back, and it takes the other three bytes too | |
| write | 1010 0101 (a5) | 1111 1111 | 1010 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.
| Step | Value burnt | Bits after | Bits that are 1 |
|---|---|---|---|
| new chip | 0000 0000 | 0 | |
| burn | 0000 0001 | 0000 0001 | 1 |
| burn | 0000 0010 | 0000 0011 | 2 |
| burn, to go back to one bit | 0000 0001 | 0000 0011 | 2 |
The count of 1s can go up and can never go down. That makes it a version floor.
The Rules
Rule 1: An erase sets every bit of a block to 1.
It always covers the whole block. No smaller erase exists.
Rule 2: A write to flash can only clear bits.
The byte becomes the AND of what it held and what was written.
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.
Rule 4: Every erase wears the block.
A design counts its erases, and spreads them over many blocks.
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.
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.
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=2Rule 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 choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Erase by the block | Erase each byte alone | Far less erase circuit, so far more bits in the same area. One changed byte costs a block. |
| A flash chip beside the chip | Embedded flash | The main chip needs no extra manufacturing steps. Reads are slow, and the board has one more part. |
| Embedded flash | A flash chip beside the chip | The program runs in place, at once after reset. The chip costs more to make. |
| Copy the program to SRAM at start-up | Execute in place from external flash | Full speed after the copy. The chip needs enough SRAM for the whole program. |
| A secret in one-time bits | A secret in flash | No 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:
flash count in byte 0, 8 updates | 08 ff ff ff | erases=8
flash next free byte, 8 updates | 05 06 07 08 | erases=2The 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 section looks ahead. Real flash memories are far larger than one block of four bytes, and they hide most of this behind a controller.
Reading an external flash
The four wires and the commands of a flash chip on the board are in a later lesson.
Copy at start-up
The Boot ROM of ap_soc copies the program from flash into SRAM: The Boot Flow.
Spreading the wear
A flash controller moves data between blocks, so that no block is erased far more often than the others.
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.
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=2Check Yourself
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.