The Hardware/Software Interface Programming assignment——缓存

这篇博客介绍了Coursera上关于硬件/软件接口的编程作业,特别是关于缓存的部分。作业中包含四个神秘的缓存几何配置(mystery0到mystery3),需要通过编程来确定每个缓存的块大小、缓存总大小和关联性。提供了包含get_block_size、get_cache_size和get_cache_assoc函数的代码示例,这些函数用于发现缓存的几何参数。最后,main函数调用这些函数并打印出缓存的属性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
Coursera HW/SW Interface
Lab 4 - Mystery Caches

Mystery Cache Geometries (for you to keep notes):
mystery0:
    block size =
    cache size =
    associativity =
mystery1:
    block size =
    cache size =
    associativity =
mystery2:
    block size =
    cache size =
    associativity =
mystery3:
    block size =
    cache size =
    associativity =
*/

#include <stdlib.h>
#include <stdio.h>

#include "mystery-cache.h"

/*
 * NOTE: When using access_cache() you do not need to provide a "real"
 * memory addresses. You can use any convenient integer value as a
 * memory address, you should not be able to cause a segmentation
 * fault by providing a memory address out of your programs address
 * space as the argument to access_cache.
 */

/*
   Returns the size (in B) of each block in the cache.
*/
int get_block_size(void) {
  /* YOUR CODE GOES HERE */
  flush_cache();
  int i=0;
  access_cache(i++);
  while(access_cache(i))
	++i;
  return i;
}

/*
   Returns the size (in B) of the cache.
*/
int get_cache_size(int size) {
  /* YOUR CODE GOES HERE */
int i=0;
int cache_check=size;
flush_cache();
access_cache(i);
while(access_cache(0)){
	i=size;
	while(i<=cache_check){
		i+=size;
		access_cache(i);
		}
	cache_check +=size;
	}
 return i;
}

/*
   Returns the associativity of the cache.
*/
int get_cache_assoc(int size) {
  /* YOUR CODE GOES HERE */
  int i=0;
int cache_check=1;
int assoc=0;
flush_cache();
access_cache(i);
while(access_cache(0)){
i=size;
assoc=0;
while(i<=cache_check){
	
			i+=size;
	
			++assoc;
	
			access_cache(i);
	
		}
	
		cache_check +=size;
	
	}	
  return assoc;
}

 DO NOT CHANGE ANYTHING BELOW THIS POINT
int main(void) {
  int size;
  int assoc;
  int block_size;

  /* The cache needs to be initialized, but the parameters will be
     ignored by the mystery caches, as they are hard coded.  You can
     test your geometry paramter discovery routines by calling
     cache_init() w/ your own size and block size values. */
  cache_init(0,0);

  block_size=get_block_size();
  size=get_cache_size(block_size);
  assoc=get_cache_assoc(size);

  printf("Cache block size: %d bytes\n", block_size);
  printf("Cache size: %d bytes\n", size);
  printf("Cache associativity: %d\n", assoc);

  return EXIT_SUCCESS;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值