Using Structs in C Write a C program to simulate a simple 64

Using Structs in C

Write a C program to simulate a simple 64B direct-mapped cache with 4B blocks (i.e., 16 cache sets). This program builds upon many of the topics covered in this course and will require a comprehensive knowledge of caching theory (see chapter 6.4). Your program should:

• Define a struct to represent a cache block. Your struct should define a bit that represents whether or not the block is valid (you may use an unsigned char), a 26-bit tag (you may use an unsigned int), and a 4-byte value (you may again use an unsigned int).

• Allocate enough memory to hold 16 cache blocks (1 per set). Initialize your cache such that all blocks are invalid.

• Repeatedly prompt the user to either: o Write a value – prompt the user for a 32-bit hex address and a 32-bit hex value. Write the 4-byte value at the appropriate location in the cache for the given address, evicting an existing block if necessary. You may assume that the user enters an address that is a multiple of 4 so that the beginning of the value aligns with the beginning of a cache block. This is to simulate moving an entire block from memory into the cache. If a valid block will be evicted, print the set index, tag, and value of that block. Either way, print the set index, tag, and value of the new block being written. o Read a byte – prompt the user for a 32-bit hex address.

Print the set index and tag corresponding to the address. Also print “hit” and the hex value of the byte at that address if the cache contains a valid value corresponding to the address. Otherwise, print “miss” and indicate the reason for the miss (i.e., invalid block or tag mismatch). o Print values – print the set index, tag, and value for all valid blocks in the cache. o Quit – quit the simulation.

• Use only bitwise operators for address -translation arithmetic (i.e., computing the set index, block offset, and/or tag for a given address); do not use division or modulo arithmetic.

• Hint: use scanf(“ %x”) and printf(“%x”) to input and output hex values directly.

Solution

Executable code in C :

#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void 16_Byte(void *space, char byte, size_t nbytes)
{
assert((nbytes & 0x0F) == 0);
assert(((uintptr_t)space & 0x0F) == 0);
memset(space, byte, nbytes); // Here memset() is Not a custom implementation.
}

static void test_mask(size_t align)
{
uintptr_t mask = ~(uintptr_t)(align - 1);
void *mem = malloc(1024+align-1);
void *ptr = (void *)(((uintptr_t)mem+align-1) & mask);
assert((align & (align - 1)) == 0);
printf(\"0x%08\" PRIXPTR \", 0x%08\" PRIXPTR \"\ \", (uintptr_t)mem, (uintptr_t)ptr);
16_Byte(ptr, 0, 1024);
free(mem);
}

int main(void)
{
test_mask(16);
test_mask(32);
test_mask(64);
test_mask(128);
return(0);
}

Using Structs in C Write a C program to simulate a simple 64B direct-mapped cache with 4B blocks (i.e., 16 cache sets). This program builds upon many of the top
Using Structs in C Write a C program to simulate a simple 64B direct-mapped cache with 4B blocks (i.e., 16 cache sets). This program builds upon many of the top

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site