What prevents the CPU from directly accessing the secondary memory and what allows it to access the primary memory directly? How is the physical wiring of primary memory to CPU different from that of the secondary memory?
It's about convenience and practicality.
Primary
The CPU instruction set contains instructions that access memory directly like "load contents of address X into register R". When it executes such instructions it will output the address to be read on its address bus along with signals that tell the memory chips that a read is occuring. The memory chips respond by outputting the data at this address on the data bus, which the CPU can then read.
Practical implementations usually have a bit more complexity like address decoding (figuring out which memory chip(s) are concerned by the address currently on the bus), wait states to handle some memories being slower than the CPU, etc. The "memory chip" can be integrated into the physical chip that also contains the CPU, for example a microcontroller usually has on-chip RAM, ROM and NOR flash.
But overall, the important things are:
- Every word has its own address and is individually accessible
- Access is blocking
- This is a simple, fast, 100% hardware process. Primary memory and CPU communicate with very simple interfaces: buses for address and data, and a bunch of signal wires for read, write, etc. And the de
On the opposite end, secondary memory like a harddisk is accessed through a controller, and data words on these memories donot have an address that is directly accessible via the address bus: the CPU first has to tell the disk controller to access a sector and copy it into RAM, then it can read it. It's the same thing for writes, the CPU first builds a complete sector to write, and then gives an order to the disk controller.
Secondary
Now I guess you're asking why is it like that? Again, convenience, practicality, and historical reasons. So, on secondary memory:
- Every word does not have its own address on the address bus
Back in the day before 64 bit cpus, people with 16 bit CPUs wanted harddisks (or tapes) to hold more data that the CPU could access with only 16 bits of address. I mean the whole point of this kind of storage is to hold a lot of data. So the "address" of the data to be read can't be communicated via the address bus, which doesn't have enough bits. Instead it has to be communicated to the controller, for example by writing into its hardware registers.
In theory it would be possible to use a disk as primary memory if the controller supported direct access via the memory but, but...
When the CPU does a memory access via its bus, it blocks while waiting for the result: the CPU can't execute the next instruction until it has been read. Primary memory is supposed to be very fast, so this is not a problem. However, spinning disks are very slow and with huge latency. So it makes sense to delegate disk tasks to a controller: in a multitasking OS, when a process needs to read from disk, the OS orders the disk controller to do the operation, then puts that process on hold and executes other processes while the disk is working. When the data is ready, the disk controller signals the CPU via an interrupt, and the OS wakes up the process that was waiting for the data.
So even if, in theory it would be possible to use a disk as primary memory if the controller supported direct access via the memory but, no one would do it... because it is much better to delegate to a controller to free up the CPU to do other stuff while it is waiting.
- This is a complicated process involving software.
Due to the above, and secondary memory access being slow, you actually want it to go through software so the OS can handle things and execute other processes during the wait time.
The species problem
The quintessential primary memories are SRAM and the ROM/Flash types that support individual word access.
Some memory types are a bit "in between".
SDRAM and Flash have some properties similar to harddisks: data is organized in pages (just like disk sectors) ; there's no clear data/address bus but instead a protocol has to be used to access data ; there is latency to access a page ; burst access of consecutive addresses is much faster than random access. For Flash, writes usually need special care, like erasing and writing a whole page.
However, we need lots of fast RAM, so for these special cases, a pure hardware controller is justified. This controller makes these types of memories primary because it sits on the CPU's bus and allows direct access by memory address.
Likewise some microcontrollers use SPI flash as a primary memory via a controller that allows direct access by memory address, and translates that into SPI commands.
The funny thing is, if you take the exact same chips and put them into a SSD, then they'd be secondary memory because they're no longer accessible by memory address, but via a protocol like SATA.
Unless of course the protocol is PCI-Express, in which case some parts of the device's onboard memory may be memory-mapped and accessible by direct access!
Going back to the microcontroller executing code from SPI flash: when it does that, via a hardware controller, Flash is accessed by address so it is primary. But these controllers don't handle writes: that's done via software emitting SPI commands. So this type of SPI flash is a primary memory for reads, but a secondary memory for writes...