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.

PROGRAM over Old DataEach column is one clock cycle. prog shows the PROGRAM command. Byte 6 held 33 before.
0123clkprogfaddr06fdata00a5readya5 into byte 6
Scroll horizontally ↔
  1. 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.

  2. 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.

Each Family Adds One Way to Change the BitsArrows lead from a family to the next one, labelled with what the next one adds.
  • Storage
  • Structure
Each family adds one way to change the bitsArrows lead from a family to the next one, labelled with what the next one adds. storage Mask ROM bits set by thefactory mask storage PROM fuses blown once bythe user storage EPROM erased by ultravioletlight storage EEPROM erased electrically,byte by byte storage Flash erased electrically, ablock at a time written by the usercan be erasederased in the circuitblock erase: denser
  1. storage Mask ROMbits set by the factory mask
    • written by the user → PROM
  2. storage PROMfuses blown once by the user
    • can be erased → EPROM
  3. storage EPROMerased by ultraviolet light
    • erased in the circuit → EEPROM
  4. storage EEPROMerased electrically, byte by byte
    • block erase: denser → Flash
  5. storage Flasherased electrically, a block at a time
  1. Mask ROM

    The bits are part of the chip itself. They are set by a photo mask in the factory and can never change.

  2. PROM

    The user writes it once, by blowing tiny fuses. A blown fuse cannot be repaired.

  3. EPROM

    Each cell stores charge on a floating gate. Ultraviolet light through a window in the package removes the charge from the whole chip.

  4. EEPROM

    An electric pulse removes the charge, so the chip is erased in the circuit. Each byte can be erased on its own.

  5. 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.

FamilyWritten byErased byErase unit
Mask ROMthe factory masknevernone
PROMblowing fuses, oncenevernone
EPROMa programmer, electricallyultraviolet lightthe whole chip
EEPROMthe circuit, electricallythe circuit, electricallyone byte
Flashthe circuit, electricallythe circuit, electricallyone 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.

Erase the Block, Then Program One ByteEach column is one clock cycle. The left grid shows the bytes the ERASE sets to ff. The right grid shows the byte the PROGRAM writes.
  • I/O
  • Storage
ERASEPROGRAMWritersends commandsErased to fflane01230x00x40x80xcProgrammedlane01230x00x40x80xc
ERASE→ Erased to ffPROGRAM→ ProgrammedWritersends commandsErased to fflane01230x00x40x80xcProgrammedlane01230x00x40x80xc

Each grid row is one block of 4 bytes, from byte 0 at the top left.

0123456clkeraseprogfaddr6mask1111000001000000ready
Scroll horizontally ↔
  1. Cycle 0

    ERASE with address 6. The flash erases the whole block of that byte: bytes 4 to 7 become ff.

  2. Cycle 1

    The erase is slow. The flash is busy, and ready stays 0 for three cycles.

  3. Cycle 4

    PROGRAM a5 into byte 6. ff AND a5 is a5, so the byte is right.

  4. 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.

The Update of One ByteArrows show the order of the steps. The saved copy lives in the writer, not in the flash.
  • Step
  • Data
  • Structure
  • Control
The update of one byteArrows show the order of the steps. The saved copy lives in the writer, not in the flash. step Read the block all bytes of the block step Change one byte in the saved copy step Erase the block every bit becomes 1 step Wait until ready is 1 step Program eachbyte wait after each one copythenbusyready
  1. step Read the blockall bytes of the block
    • copy → Change one byte
  2. step Change one bytein the saved copy
    • then → Erase the block
  3. step Erase the blockevery bit becomes 1
    • busy → Wait
  4. step Waituntil ready is 1
    • ready → Program each byte
  5. step Program each bytewait after each one
  1. Read

    Copy the whole block into registers. The erase will destroy it.

  2. Modify

    Put the new byte into the copy, at its place.

  3. Erase

    Erase the block. Every byte is now ff.

  4. Wait

    The erase takes several cycles. A command sent now is ignored.

  5. 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 flashNAND flash
Cells on a bit lineside by side, in parallelin series strings
Readany byte, at randoma whole page at a time
Cost per bithigherlower: fewer contacts per cell
Typical usecode that runs straight from the chipfiles: memory cards and SSDs
Extra needsfewerror 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
  1. 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.

  2. 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.

  3. 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.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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 a5
Read 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

Predict the outputThis program models one block of four bytes. What exact line does it print?
module tb;
  logic [7:0] blk [4];
  initial begin
    blk[0] = 8'h11; blk[1] = 8'h22; blk[2] = 8'h33; blk[3] = 8'h44;
    blk[1] = blk[1] & 8'hf0;              // program byte 1 with f0
    foreach (blk[i]) blk[i] = 8'hff;      // erase the block
    blk[3] = blk[3] & 8'h3c;              // program byte 3 with 3c
    $display("%h %h %h %h", blk[0], blk[1], blk[2], blk[3]);
  end
endmodule
Spot the bugEach option updates byte 6 of block 1 to a5. The flash ignores a command while it is busy. Which schedule leaves the block wrong?
ConceptA flash byte holds 0f. With no erase, which value can a PROGRAM give it?
ConceptWhy is NAND flash cheaper per bit than NOR flash?
ConceptWhich memory can rewrite a single byte with no block erase?

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.

Before
localparam logic ERASE_FIRST = 1'b0;
After
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.

Practice

Report a problem with this page