Nonvolatile Memory: ROM, EPROM, EEPROM and Flash
Tell the nonvolatile memories apart by how they are written and erased, and update one byte of flash without losing its neighbours.
You Will Be Able To
- Order mask ROM, PROM, EPROM, EEPROM and flash by how they are written and erased.
- Explain why a flash program can only clear bits and an erase sets a whole block.
- Plan the read, erase and program steps that change one byte of flash.
- Compare NOR flash with NAND flash.
- Lesson
- G8.5
- Time
- 30 min
- Runs on
- Your browser
- Check
- 5 questions
A sensor stored its calibration byte in flash. The new value was a5, but the chip read back 21. The writer had programmed the new byte over the old one, 33, with no erase.
prog shows the PROGRAM command. Byte 6 held 33 before.- Cycle 1
PROGRAM sends a5 to byte 6. The flash can only clear bits, so it keeps the bits that are 1 in both 33 and a5.
- Cycle 3
The flash is ready again. Byte 6 now holds 21, not a5.
Measured on Icarus Verilog: the example of this lesson as it ships, cycles 1 to 4.
Five Ways to Keep Bits Without Power
A nonvolatile memory keeps its data when the power is off. The families differ in how the bits get in, and in how they can be removed again.
- Storage
- Structure
- Mask ROM
The bits are part of the chip itself. They are set by a photo mask in the factory and can never change.
- PROM
The user writes it once, by blowing tiny fuses. A blown fuse cannot be repaired.
- EPROM
Each cell stores charge on a floating gate. Ultraviolet light through a window in the package removes the charge from the whole chip.
- EEPROM
An electric pulse removes the charge, so the chip is erased in the circuit. Each byte can be erased on its own.
- Flash
The erase circuits are shared by a whole block. Fewer circuits mean smaller cells and more bits per chip.
Read the diagram as text
- Mask ROM: bits set by the factory mask
- PROM: fuses blown once by the user
- EPROM: erased by ultraviolet light
- EEPROM: erased electrically, byte by byte
- Flash: erased electrically, a block at a time
- Mask ROM → PROM: written by the user
- PROM → EPROM: can be erased
- EPROM → EEPROM: erased in the circuit
- EEPROM → Flash: block erase: denser
Lesson G9.17 shows the floating-gate cell in transistors.
| Family | Written by | Erased by | Erase unit |
|---|---|---|---|
| Mask ROM | the factory mask | never | none |
| PROM | blowing fuses, once | never | none |
| EPROM | a programmer, electrically | ultraviolet light | the whole chip |
| EEPROM | the circuit, electrically | the circuit, electrically | one byte |
| Flash | the circuit, electrically | the circuit, electrically | one block |
A block holds many bytes. In the model of this lesson a block is 4 bytes. In a real flash, a program and an erase each take many clock cycles, and the erase is the slowest step.
Program Clears, Erase Sets
In flash, an erased bit is 1. A program can change a 1 to a 0, and nothing else. Only an erase brings bits back to 1, and it works on a whole block.
- I/O
- Storage
Each grid row is one block of 4 bytes, from byte 0 at the top left.
- Cycle 0
ERASE with address 6. The flash erases the whole block of that byte: bytes 4 to 7 become ff.
- Cycle 1
The erase is slow. The flash is busy, and
readystays 0 for three cycles. - Cycle 4
PROGRAM a5 into byte 6. ff AND a5 is a5, so the byte is right.
- Cycle 6
Done. Block 1 holds ff ff a5 ff. The old bytes 11, 22 and 44 are gone.
More context
The erase is why the neighbours are lost. Problem G8.5.1 reads them first and programs them back.
Measured on Icarus Verilog: the example with ERASE_FIRST set to 1, cycles 2 to 8.
Why this rule? A cell stores a bit as trapped charge on a floating gate. Programming and erasing both need a high voltage. The erase circuits are shared by a whole block, which keeps the cells small.
Changing One Byte
To change one byte, a writer cannot simply program it. The steps form a read-modify-write of the whole block.
- Step
- Data
- Structure
- Control
- Read
Copy the whole block into registers. The erase will destroy it.
- Modify
Put the new byte into the copy, at its place.
- Erase
Erase the block. Every byte is now ff.
- Wait
The erase takes several cycles. A command sent now is ignored.
- Program
Program every byte of the copy, and wait for ready after each one.
Read the diagram as text
- Read the block: all bytes of the block
- Change one byte: in the saved copy
- Erase the block: every bit becomes 1
- Wait: until ready is 1
- Program each byte: wait after each one
- Read the block → Change one byte: copy
- Change one byte → Erase the block: then
- Erase the block → Wait: busy
- Wait → Program each byte: ready
A shortcut: when the new byte only clears bits of the old one, a program alone is enough.
Program only
localparam logic ERASE_FIRST = 1'b0;
Icarus: byte 6 holds 21, wanted a5. The neighbours are untouched.
Erase, then program
localparam logic ERASE_FIRST = 1'b1;
Icarus: byte 6 holds a5, but block 1 is ff ff a5 ff. The neighbours are lost.
Neither version is a full update. The fix of the lab makes byte 6 right and shows the price of the erase. The full update is problem G8.5.1.
NOR Flash and NAND Flash
Flash comes in two layouts. They differ in how the cells sit on a bit line.
| NOR flash | NAND flash | |
|---|---|---|
| Cells on a bit line | side by side, in parallel | in series strings |
| Read | any byte, at random | a whole page at a time |
| Cost per bit | higher | lower: fewer contacts per cell |
| Typical use | code that runs straight from the chip | files: memory cards and SSDs |
| Extra needs | few | error correction and a list of bad blocks |
Both erase in blocks and program by clearing bits. Lesson G8.9 shows the error correction.
NOR flash acts like a slow ROM for reads, so a processor can run code from it. NAND flash is read and written in pages, like a disk. A controller hides that from the system.
Asked in interviews
Why must flash be erased before it is written?
A program can only change a 1 to a 0. To get a 1 back, the cell must be erased. The erase circuits are shared by a block, so the whole block is erased together.
How do you change one byte in flash?
Read the whole block into RAM and change the byte there. Erase the block, wait until it is done, then program every byte back. If the new value only clears bits, one program is enough.
NOR or NAND flash?
NOR gives random-access reads, so code can run in place. NAND is denser and cheaper per bit and is read by page. It needs error correction and bad-block handling, so it suits bulk storage.
Try It
Block 1 starts as 11 22 33 44. The bench asks the writer to store a5 in byte 6, the third byte of the block.
Line e2 sends command 1, PROGRAM, with no erase. The run ends with byte 6 holds 21, wanted a5. 21 is 33 AND a5.
Set ERASE_FIRST to 1 and run again. Line e2 now sends ERASE, ready is 0 for three lines, and PROGRAM follows on line e6.
Byte 6 now holds a5. Look at the block line: ff ff a5 ff. The erase cleared the other three bytes as well.
block 1: 11 22 33 44 e0 cmd=0 faddr=x fdata=xx ready=1 e1 cmd=0 faddr=0 fdata=00 ready=1 e2 cmd=1 faddr=6 fdata=a5 ready=1 e3 cmd=0 faddr=6 fdata=a5 ready=0 e4 cmd=0 faddr=6 fdata=a5 ready=1 block 1: 11 22 21 44 byte 6 holds 21, wanted a5Read included file: tb.sv
// tb.sv: the bench (read-only). Block 1 (bytes 4 to 7) holds 11 22 33 44. The bench stores a5 in byte 6.
`timescale 1ns/1ps
// flash16: a small flash memory (a model, read-only). 4 blocks of 4 bytes.
// PROGRAM the byte becomes (old byte AND data): a program can only change a 1 to a 0. Busy 1 cycle.
// ERASE every byte of the block of addr becomes ff. Busy 3 cycles.
// A command that arrives while the flash is busy is ignored.
module flash16 (
input logic clk,
input logic [1:0] cmd, // 0 NOP, 1 PROGRAM, 2 ERASE
input logic [3:0] addr,
input logic [7:0] data,
output logic ready
);
logic [7:0] mem [16];
int busy = 0;
int ignored = 0;
assign ready = (busy == 0);
always @(posedge clk) begin
if (busy > 0) begin
busy <= busy - 1;
if (cmd != 2'd0) begin
ignored++;
$display(" FLASH: command %0d while busy: ignored", cmd);
end
end else if (cmd == 2'd1) begin
mem[addr] <= mem[addr] & data;
busy <= 1;
end else if (cmd == 2'd2) begin
for (int i = 0; i < 4; i++) mem[{addr[3:2], 2'(i)}] <= 8'hff;
busy <= 3;
end
end
endmodule
module tb;
logic clk = 0, rst = 0, start = 0;
logic [3:0] addr = 4'd0;
logic [7:0] data = 8'h00;
wire [1:0] cmd;
wire [3:0] faddr;
wire [7:0] fdata;
wire fready, done;
int edge_n = -1;
always #5 clk = ~clk; // a 10 ns clock
always @(posedge clk) edge_n = edge_n + 1;
flash_store dut (.clk(clk), .rst(rst), .start(start), .addr(addr), .data(data),
.cmd(cmd), .faddr(faddr), .fdata(fdata), .fready(fready), .done(done));
flash16 flash (.clk(clk), .cmd(cmd), .addr(faddr), .data(fdata), .ready(fready));
// One cycle: drive the inputs, show the command of this cycle, then take the edge.
task automatic tick(input logic do_rst, input logic do_start);
rst = do_rst; start = do_start;
#4 $display("e%0d cmd=%0d faddr=%0d fdata=%h ready=%b", edge_n + 1, cmd, faddr, fdata, fready);
@(posedge clk); #1;
endtask
task automatic show_block();
$display("block 1: %h %h %h %h", flash.mem[4], flash.mem[5], flash.mem[6], flash.mem[7]);
endtask
initial begin
for (int i = 0; i < 16; i++) flash.mem[i] = 8'hff; // an erased flash
flash.mem[4] = 8'h11; flash.mem[5] = 8'h22; flash.mem[6] = 8'h33; flash.mem[7] = 8'h44;
show_block();
tick(1, 0); // edge 0: reset
addr = 4'd6; data = 8'ha5;
tick(0, 1);
while (!done) tick(0, 0);
show_block();
$display("byte 6 holds %h, wanted a5", flash.mem[6]);
$finish(0);
end
endmodule
Check Yourself
Common Mistakes
Wrong Program a new byte over old data with no erase.
A program can only change a 1 to a 0. On Icarus, a5 over 33 leaves 21, which is 33 AND a5.
Fix Erase the block first, so that every bit is 1 again. Then program.
localparam logic ERASE_FIRST = 1'b0;localparam logic ERASE_FIRST = 1'b1;Wrong Erase the block to change one byte and forget the other bytes of the block.
The erase sets all four bytes to ff: the lab ends with block 1 holding ff ff a5 ff.
Fix Read the whole block first. After the erase, program every byte back, with the new byte in its place.
Wrong Send the next command while the flash is still busy.
The model ignores it: it prints command 1 while busy: ignored, and byte 6 stays ff.
Fix Wait until the ready flag is 1 again before each command.
Wrong Use flash like RAM for a value that changes all the time.
Every change needs an erase, and each block survives only a limited number of erases.
Fix Keep fast-changing values in RAM. Spread the writes that must go to flash over many blocks.
Remember
- Nonvolatile memories differ in how they are written and erased: mask ROM, PROM, EPROM, EEPROM and flash.
- In flash, a program can only clear bits. An erase sets a whole block to ff.
- Changing one byte means read the block, erase it, wait, and program every byte back.
- Send each command only when the flash is ready.
- NOR flash reads any byte at random. NAND flash is denser and reads by page.