I tested all the connection on the socket side of the the flash socket. Everything is connected and the levels change as they should under the control of the Arduino.

I forgot to solder the flash power connection to by little power distribution strip, so I have to do that. I also forgot to pull up the ENABLE pin on the CPLD.

Next step is to start programming the flash.

For those who don’t know how to write and erase flash, here is a quick rundown. Reading from flash is easy. You just put the address on the address bus, pull CE (chip enable) and OE (output enable aka RD for read) down, and read the data on the databus.

Writing is not so simple. With RAM, you can just set the address and data, and while CE is pulled low, pull WE (aka WR) low and it will write on the rising edge. But flash doesn’t work that way. Instead, you enter a mode and while in the mode, tell the flash which byte to write to which address. Then you keep reading until databit 6 stops toggling.

The way you enter the mode is to write a sequence of bytes that would be unlikely to just be written at random.

To just write a byte, write 0xaa to 0x5555. Then write 0x55 to 0x2aaaa. Then write 0xa0 to 0x5555. Finally, write the byte to the address you want to program. Now read and when two consecutive reads have the same bit #6, it is over.

The thing about writing to flash is that you can write a 0 bit to a location where there is a 1 bit, but you cannot turn a 0 back into a 1 by writing a new byte. It will always be an &= operation, not an = operation.

But that doesn’t mean you can never turn a 0 back into a 1. You have to do it a sector at a time. The 39SF020A I chose has 4kB pages that can be erased. The entire chip can also be erased.

To erase a sector, write 0xaa to 0x5555. Then write 0x55 to 0x2aaaa. Next write 0x80 to 0x5555, then 0xaa to 0x5555. Next, write 0x55 to 0x2aaaa. Finally, write 0x30 to any byte in the sector to erase.

The chip erase is the same except if the last byte is 0x10 instead of 0x30, the whole chip is erased.

Again, as with a write, read the chip and watch the toggle bit until it stops toggling.