Regarding CPU cache and RAM and Main memory

Started by
13 comments, last by Ryan_001 10 years, 2 months ago

Why does cache on CPU require both data and address of program's instruction?

Why does loading an instruction from a program sequential require RAM or main memory?

It seems every a load instruction takes place it consume main memory but I am not sure why.

I know the facts. But I do not know the reasons for why the facts hold.

Advertisement
1) It needs the data to decode the instruction. It needs the address to figure out which instruction is next.

2) A program is stored in RAM.

3) I have no clue what you mean by a load consuming memory.

1) It needs the data to decode the instruction. It needs the address to figure out which instruction is next.2) A program is stored in RAM.3) I have no clue what you mean by a load consuming memory.


I mean the load operation the one that has 5 steps: fetch encode execute memory read. The load operation requires RAM in order to operate right?

I don't know if it speaks directly to your questions, but there seems to be some confusion about the relationship between cache, RAM, and main memory.

Conceptually, your program only knows about address space -- that address space might be mapped to the RAM modules installed in your computer, it might be mapped to memory or registers on a peripheral device (like a graphics card), or it might be mapped to virtual memory that's currently swapped out to sectors on your hard disk drive.

Cache isn't separate, it just mirrors the contents of the address space in a small amount of fast memory that's closer to the CPU itself (on the CPU silicon, even). Caches aren't magic, all they do is operate on the principle that if you access word 0x0FFFFF04, you're more likely to access 0x0FFFFF08 soon than any other memory. When you read just one byte from the address space, it isn't just one byte that comes back -- to the CPU, you only see the byte you asked for, but the data path between the cache and RAM is an entire cache-line wide (usually 32 bytes or more, IIRC) and it all goes into the cache -- so when you ask for the next byte, you don't walk all the way out to main memory and back, you just walk to the cache and back. Of course, the cache is smaller than the address space, so eventually it gets full and you need to start over-writing what's in the cache. There's different structures and ways a cache can work, but that's the basic principle of it. All of its automatic and you don't usually have to worry about it. All you really need to care about, if memory bandwidth or latency begins to bottleneck your application, is arranging your data structures in such a way that memory that gets used together in next to each other (that oversimplifies a bit, but is the basic idea) so that its all in the cache at the same time.

throw table_exception("(? ???)? ? ???");

First off, you've asked a lot of hardware questions and mostly they've been for personal learning, but if I find out it is for homework I'll hunt you down. :-)


Most CPU instructions require a register, a memory address, a constant, or a combination of them.

opcode register
opcode address
opcode address register
opcode register address
opcode register constant
and so on.

Older machines (and sometimes modern microprocessors) did not have any memory cache on the processor. If the opcode required a memory address then everything stops and waits for the memory's content to be moved to or from the processor.

So now moving on to the questions.

>>Why does cache on CPU require both data and address of program's instruction?

A cache is just a local copy so the wait doesn't take as long. You need the address (where the copy is supposed to be from) and the data (the stuff at that address).

Many modern processors include caches dedicated to the data and instructions separately because they have different access patterns. Instruction caches make sense to keep the contents of functions in place. Data caches make sense to keep source and target buffers in place. Different access patterns mean different algorithms are helpful.

>> Why does loading an instruction from a program sequential require RAM or main memory?

I'm going to have to guess at the meaning because either some words were left out or there is an English translation issue.

Instructions are generally run sequentially. The first instruction is run, then the second, then the third. There are a few instructions that cause it to jump to a different place; some are conditional jumps and others are unconditional jumps. You write conditional jumps in high level languages with if statements, loops, and anything that says "if a condition is true, do something different". An unconditional jump simply moves to a new location, like a function call or a function return; no questions asked, you just go there.

>> It seems every a load instruction takes place it consume main memory but I am not sure why.

I want to give an answer but do not understand the question.



Because of all the questions you keep asking, you might enjoy looking at some old 6502 programs. There are quite a few games for the Atari 2600, the most popular game console at the end of the 1970s, that could be fun to study. Google turns up hundreds of good emulators, and also a large number of good tutorial sites that can help build simple games fairly easily. You won't be writing million-copy blockbusters, but just learning simple things like how to cycle through the screen colors on an old 6502-chip system is both very easy and very instructive.

I've seen people pick up some of the old 6502-chip systems where the first week is trying to figure out what is going on, the second week is experimenting with crazy ideas, and the third week is pushing out minimalist clones of pong, ski and other ancient games. Considering programmers of that era often only had 40 to 60 working days to create blockbuster titles, with today's shared information and super-large storage there are hobby developers who put together some amazing games on that generation's hardware. I had a co-worker who was very into those, he had programmable Atari 2600 cartridges, programmable Vectrex cartridges, and a few others with rather amazing homebrew games on them.


It needs the address to figure out which instruction is next.

I thought that was the job of PC(Program Counter)

1) It needs the data to decode the instruction. It needs the address to figure out which instruction is next.2) A program is stored in RAM.3) I have no clue what you mean by a load consuming memory.


I mean the load operation the one that has 5 steps: fetch encode execute memory read. The load operation requires RAM in order to operate right?

In case the steps of executing an instruction are the part that is confusing you, the steps are based on the most complex instruction's needs and all instructions use the same steps. This is because the CPU is pipelined, so one instruction is executing its fetch while another instruction is executing its encode, etc.

Fetch is just getting the instruction (from cache, or RAM if not in cache, or generating a fault that results in a page load from disk if not in RAM).

Encode (usually referred to as decode) is decoding the instruction so the CPU knows what to do and assigning the registers that will get used.

Execute is where math and such happens, perhaps calculating the full address from an offset for a load operation.

Memory is where reads and writes to memory occur (again, from cache when possible, RAM if not in cache, etc.). This is where the actual loading of data from memory (RAM) into a register happens.

Read I assume is supposed to be write-back. There is no reading that occurs at that point, it's just saving the virtual registers back to the physical register file. I don't think this stage even exists on CPUs that lack register renaming.

It's worth noting that loading a constant (when such instructions exist) does not require data loaded from RAM as the constant is encoded in the instruction. So the "memory" stage of the pipeline would be a noop when loading a constant.


First off, you've asked a lot of hardware questions and mostly they've been for personal learning, but if I find out it is for homework I'll hunt you down. :-)

I am not sure if hardware questions are allowed in the programming section. I only post these questions because the knowledge of what goes underneath the hood of a computer might benefit me as a programmer and a self taught game programmer. It is not for homework but more to fill the gaps of what I am not sure of or have doubts about. I am taking a Computer Architecture class and these questions came up when I was typing up the notes for this class.

I'll just say: Don't get discouraged if you get downvotes for your questions, its okay to ask things.

Think of it as trading reputation points for knowledge :D

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

I'll just say: Don't get discouraged if you get downvotes for your questions, its okay to ask things.

Think of it as trading reputation points for knowledge biggrin.png

Its sad but true, I find Gamedev to be a rather hostile forum.

This topic is closed to new replies.

Advertisement