4 ms·
Self-modifying code in a EPROM, written backwards. It was the start-up sequence of the boot ROM for my home computer, based on the RCA 1802 chip way back in the
by heikkilevanto 4y ago
Self-modifying code in a EPROM, written backwards. It was the start-up sequence of the boot ROM for my home computer, based on the RCA 1802 chip way back in the 1970's... It had to save the contents of (almost) all the registers for debugging. I was short of space - I had added 2.5 kb to the 2kb program, and the EPROM was only 2kb, so we needed to optimize everything we could. The shortest way to save the registers relied on self-modifying code. Such can not run from an EPROM, so it had to be copied in RAM. Luckily we had other stuff to copy, so copying a bit more did not cost extra bytes. And the shortest copy loop was using an auto-increment load instruction and a auto-decrement push instruction, so the stuff had to be written backwards in the rom.
There were other dirty tricks, for example the table of (one-letter) commands was at the end of the ROM, and was terminated by a letter with the 8th bit set, which we got by reading the address after the ROM. As a aside effect, you could extend the commands by installing a second ROM in the proper place.
That time I knew the instruction set for the 1802 by heart. I even dreamed directly in hex.