Question
OTHER CHEGG ANSWER DOES NOT WORK IT CRASHES EVERYTIME I TRY TO BUILD THE MEMORY. DO NOT POST THAT AS A REPLY IT IS USELESS
OTHER CHEGG ANSWER DOES NOT WORK IT CRASHES EVERYTIME I TRY TO BUILD THE MEMORY. DO NOT POST THAT AS A REPLY IT IS USELESS
Computer Organization: Cache Memory Simulator:
Objective: To simulate reading and writing to a custom-sized direct-mapped cache, involving a custom-sized main memory. Inputs: The total size of accessible main memory (in words) The total size of the cache (in words) The block size (words/block) A signal to read (r) or write (w) to the cache The main memory address to read from/write to The contents of the address for writing to the cache Outputs: The corresponding cache tag, block, and word for a main memory address The contents of the address resulting from reading/writing to the cache A message indicating either a hit or a miss to the cache Specification: The program simulates reading from and writing to a cache based on choosing from a menu of choices, where each choice calls the appropriate procedure, where the choices are: a) Enter parameters b) Access cache for reading/writing and transfer data c) Quit program Notes: Use a structure (struct) to represent a cache line consisting of a tag (integer) and a block (integer pointer). Define the cache to be a pointer to the struct. Upon entering the parameters, the main memory and cache are to be dynamically allocated based on their respective total sizes. Each word i of main memory is initialized with the value Mi, where M is the size of main memory in words. For example, if the memory size is 16384, then word 10 will initially contain the value 16374 (which is 16384-10). Reading/writing from/to a new block in the cache results in dynamically allocating the block based on the block size.
This is my code so far:
#include
//declare cache
struct node{ int tag; int *block; //dynamic array } *cache= NULL;
typedef struct node line; //rename
//define dynamic array for Main Memory
int *mm= NULL;
//Declare Global Vars int numLines, blockSize, cacheSize, mmSize;
void Enter_Params(){ //local vars
int index =0; mmSize=0;
printf(" Enter main memory size (words): "); scanf("%d",&mmSize);
printf("Enter cache size (words): "); scanf ("%d",&cacheSize);
printf("Enter block size (words/block): "); scanf("%d",&blockSize);
//build Main Memory
mm=(int*)malloc(mmSize * sizeof(int));
for(index=0;index mm[index]=mmSize-index; } numLines= (cacheSize/blockSize); //Allocate memory for Cache cache=(line*)malloc(numLines * sizeof(line)); for (index=0;index cache[index].tag=-1; cache[index].block=NULL; } return; } void Access_Cache(){ int index, data, mmAddress, tag, block, word, base; char signal; printf("Select read (r) or write (w): "); scanf(" %c", &signal); if(signal=='w'){ printf(" Enter main memory address to write to: "); scanf("%d",&mmAddress); printf(" Enter value to write to: "); scanf("%d", &data); } else if (signal=='r'){ printf(" Enter main memory address to read from: "); scanf("%d", &mmAddress); } tag=mmAddress/cacheSize; block=(mmAddress%cacheSize)/blockSize; word=mmAddress%blockSize; base =(mmAddress/blockSize)*blockSize; //CASES //Case 1: Miss if(cache[block].tag==-1){ cache[block].block=(int*)malloc(blockSize*sizeof(int)); } //Case 2: Tags don't match if(cache[block].tag!=tag){ if(signal=='w'){ printf("Write Miss!"); } else if(signal=='r'){ printf("Read Miss!"); } for(index=0;index } //Case 3: Hit due to matching tags else if(cache[block].tag==tag){ if(signal=='w'){ printf("Write Hit!"); cache[block].block[word]=data; mm[mmAddress]=data; } else if(signal=='r'){ printf("Read Hit!"); data=cache[block].block[word]; } } printf(" ------------------------------------"); printf(" | Tag: %d\t\tBlock: %d\t\tWord: %d (%d)", tag, block, word, data); printf("------------------------------------"); return; } int main() { char choice='z'; printf("Cache memory allocation and mapping: "); printf(" ------------------------------------"); while(choice!='c'){ printf(" a) Enter parameters"); printf(" b) Access cache for reading/writing and transfer data"); printf(" c) Quit "); scanf(" %c", &choice); switch(choice){ case 'a': Enter_Params(); break; case 'b': Access_Cache(); break; case 'c': printf("Exiting Program");exit(0); break; default: printf("incorrect choice!"); } } return 0; } I AM MAINLY HAVING PROBLEMS BUILDING THE DYNAMIC ARRAY. FOR EXAMPLE, IF YOU INPUT THE FOLLOWING IT WILL CRASH EVERYTIME: a 65536 1024 16 Please help I cannot figure this out
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started