How the data loads in cache?

If you have any questions on programming, this is the place to ask them, whether you're a newbie or an experienced programmer. Discussion on programming in general is also welcome. We will help you with programming homework, but we will not do your work for you! Any porting requests must be made in Developmental Ideas.
Post Reply
User avatar
RyoDC
Mental DCEmu
Mental DCEmu
Posts: 366
https://www.artistsworkshop.eu/meble-kuchenne-na-wymiar-warszawa-gdzie-zamowic/
Joined: Wed Mar 30, 2011 12:13 pm
Has thanked: 2 times
Been thanked: 0

How the data loads in cache?

Post by RyoDC »

I mean, we have a data cache, for example, about of 16 Kbytes on SH4 processor.
How the processor knows which data and when (and how much of them) to load into cache?
How do I try to build a Dreamcast toolchain:
Image
nymus
DC Developer
DC Developer
Posts: 968
Joined: Tue Feb 11, 2003 4:12 pm
Location: In a Dream
Has thanked: 5 times
Been thanked: 6 times

Re: How the data loads in cache?

Post by nymus »

behold the mind
inspired by Dreamcast
TapamN
DC Developer
DC Developer
Posts: 109
Joined: Sun Oct 04, 2009 11:13 am
Has thanked: 2 times
Been thanked: 96 times

Re: How the data loads in cache?

Post by TapamN »

The SH4 loads data into the cache when the program tries to access cacheable memory and the data isn't already in the cache. Main RAM is usually accessed as cacheable, while hardware is usually accessed as uncacheable. When the SH4 load stuff into cache, it always loads a block of 32 bytes. The start of that block is the address of the memory access rounded down to a multiple of 32. So if the address accessed is between 0-31, the block loaded will contain the data from 0-31; if the address is between 32-63, the block loaded will contain the data from 32-63; etc.
User avatar
RyoDC
Mental DCEmu
Mental DCEmu
Posts: 366
Joined: Wed Mar 30, 2011 12:13 pm
Has thanked: 2 times
Been thanked: 0

Re: How the data loads in cache?

Post by RyoDC »

So there will be some delay, right? Is there's any way to pre-load that data?

What data are initially in the cache?
How do I try to build a Dreamcast toolchain:
Image
Ayla
DC Developer
DC Developer
Posts: 142
Joined: Thu Apr 03, 2008 7:01 am
Has thanked: 0
Been thanked: 4 times
Contact:

Re: How the data loads in cache?

Post by Ayla »

You can use GCC's __builtin_prefetch for that purpose.
User avatar
BlueCrab
The Crabby Overlord
The Crabby Overlord
Posts: 5666
Joined: Mon May 27, 2002 11:31 am
Location: Sailing the Skies of Arcadia
Has thanked: 9 times
Been thanked: 69 times
Contact:

Re: How the data loads in cache?

Post by BlueCrab »

Ayla wrote:You can use GCC's __builtin_prefetch for that purpose.
Or, of course, just use the pref instruction in assembly.
User avatar
GyroVorbis
Elysian Shadows Developer
Elysian Shadows Developer
Posts: 1874
Joined: Mon Mar 22, 2004 4:55 pm
Location: #%^&*!!!11one Super Sonic
Has thanked: 81 times
Been thanked: 64 times
Contact:

Re: How the data loads in cache?

Post by GyroVorbis »

RyoDC wrote:So there will be some delay, right? Is there's any way to pre-load that data?

What data are initially in the cache?
Yeah, there's considerable delay for a cache miss. That's why cache optimization is so important for time-critical algorithms.
BlueCrab wrote:Or, of course, just use the pref instruction in assembly.

Code: Select all

#define PREFETCH(addr) __asm__ __volatile__("pref @%0" : : "r" (addr));
Post Reply