Loading a program into ROM/RAM of embedded hardware board
We have learned about the hardware used in an embedded system.
Now we know that, when power on an embedded hardware, the CPU will fetch
(i.e. read) instructions from the ROM or Flash and executes them.
We also learned that each instruction is a number (machine code) stored in the
ROM. Typical size of instruction is 16 bits or 32 bits. But our simple CPU uses
8 bit instructions. But few instructions are 16 bit in size. I hope
you remembered that, in each instruction, some bits will specify the
operation to perform. And other bits will specify the operands on which
operation to perform. In most of the instructions, operands will be register
numbers, but in some instructions operands also will specify memory
Now we wish to write a small program and run it on our embedded hardware
board. So first thing is that, we have to write a program first. We can
write a program in three possible ways:
- Directly write machine codes (i.e. instruction codes or numbers)
- Write in assembly language instructions and generate machine codes.
- Write in C language and again generate machine codes from C program
If we write a program in assembly language, then we need a software called
assembler. This assembler takes the assembly language program, we have
written and converts into an executable file containing machine codes.
In your microprocessor lab, if you have used a software called MASM, it
is an assembler software. It converts 8086 assembly instructions to machine
If we write a program in C language, then we need a software called
compiler. This compiler takes the C program file and converts it into
an executable file containing machine codes. Important point is that,
whichever way we write, finally we need machine codes to put in the
ROM/Flash of embedded hardware.
Let us assume that, we have written a program and stored the final machine
codes in a file with name 'program.hex'. Now we need to copy the contents
of this file to ROM/Flash chip.
Putting our program in ROM/Flash is a bit more involved, let us delay a bit.
Instead, let us put this program in RAM. It will be easy to put in RAM.
To put our program in RAM, there must a program in ROM/Flash, which will
do that. When we power on the hardware, the program in ROM will run and this
program allow us to copy our program to RAM. After copying the program to
RAM, ROM program will allow us to jump to this program loaded in RAM. Jumping
to RAM program is easy, it involve loading Program Counter (PC) register
with the RAM address. Now CPU fetches its instruction from RAM and executes.
The program present in ROM and allows us to load our program into RAM, is
called 'Loader' or 'Boot-loader' program. Sometimes it is also refered as
'Monitor' or 'Debug Monitor' program.
Now let us understand, how this 'Loader' in ROM, allow us to copy our program
to RAM. One simple way is through serial communication. We have learned that
embedded hardware will contain serial controller (UART) which will support
serial communication through RS-232 serial port. Our PC also should have
RS-232 serial port. All most all old PCs used to have this serial port. But now
a days, modern laptops are not having serial ports. But we have a solution for
this. We need to buy USB to RS-232 adapter. This adapter can be connected to
USB port and provides serial port. We need to connect PC's serial port and
embedded board's serial port through a serial cable.
On PC side, we have to run a software, which will transfer the program.hex
file over the serial port. On the embedded board side, the loader program in ROM
will receive the contents of this file and stores them in RAM. Finally we
will give a command to loader program, so that loader program in ROM jumps to
the RAM program and executes it.
Some embedded hardware boards, specifically microprocessor kits supplied
to engineering colleges, will have small LCD and key-pad or key board
interface. These LCD and Keypads are interfaced to the GPIO lines of
embedded hardware. The program running from ROM supports, loading of
program through keypad/keyboard.
Through keypad/keyboard program can be loaded in to RAM in two ways.
One way is directly entering the machine code numbers into RAM. Second
way is to enter assembly instructions like 'MOV R1, R2'. The ROM program
will have assembler capability. It takes assembly instruction, converts
it to machine code, and stores it in the RAM. Finally we can give command
to ROM program, so that ROM program jumps to the RAM address and executes
the RAM program.