Electronic – how does program counter stores the instruction memory when program is loaded?

computer-architecturemips

it is mentioned in the book computer organization and design by Patterson/Hennessy page 252 that:

The instruction memory need only provide read access because the
datapath does not write instructions. Since the instruction memory
only reads, we treat it as combinational logic: the output at any time
reflects the contents of the location specified by the address input,
and no read control signal is needed. (We will need to write the
instruction memory when we load the program;
this is not hard to add,
and we ignore it for simplicity.)

my question is: how is storing instruction memory when the program is loaded possible?? should we add an enable to PC which activates by operating system and the operating system then writes the address in PC?? or there are other ways??

here is a simple figure of PC and instruction memory

pc and instruction memory

Best Answer

Essentially all the text is saying is that somehow the program gets loaded into memory before the CPU can start running it. Since the text only intends to discuss how the CPU executes instructions out of program memory, how the instructions end up in program memory in the first place is an implementation detail.

There are many ways of getting the instructions into program memory, and it is highly dependent on the architecture.

Systems based on read-only memory will have the instructions loaded into program memory either during manufacture of the chip itself (with a mask ROM) or with external programming hardware that writes to the memory array.

Systems that contain flash memory often have some self-programming mechanism where the processor can either execute special instructions or write to special registers to trigger an erase or write operation on program memory. The CPU may have to be stopped temporarily to allow the operation to complete.

A standard desktop computer that executes out of RAM and loads programs off of a hard drive has a much more complex process. In this case, the initial boot would be done out of a flash ROM of some sort, possibly copied into RAM by components on the motherboard before the main CPU is allowed to start running. Then this startup code will load more code into RAM from a hard drive or solid state drive. A desktop or server CPU actually doesn't even execute code out of RAM, it executes code out of on-chip cache memory, which is in turn loaded from RAM. Because of this, it is possible that the CPU to run software out of cache while memory operations take place to do other things, including copying other executable code from a hard drive and into RAM. The CPU also has stop and wait occasionally when the code it wants to run is not in the cache.