Exemplo n.º 1
0
int main(int argc, const char *argv[]) {
  if(!allocate_registers()) {
    printf("error allocating registers\n");
    return EXIT_FAILURE;
  }

  if(!allocate_ram()) {
    printf("error allocating ram\n");
    return EXIT_FAILURE;
  }

  if(!load_program(argv[1])) {
    printf("error loading program\n");
    return EXIT_FAILURE;
  }

  while(running) {
    cycle();
  }

  //debug_ram();

  return EXIT_SUCCESS;
}
Exemplo n.º 2
0
/********************** ************************/
int main(int argc,char *argv[]) {
    int n_pages  = 0;
    //int n_frames = 0;
	ram_info_t ram_info;//
	ram_info.n_frames = 0;//
	ram_info.algorithm = NULL;//
    int n_cache  = 0;
    int n_tlb    = 0;
    char *access_file = NULL;
    FILE *access_fd = NULL;
    addr_t virtual_addr = 0;
    addr_t physical_addr;
    pid_t pid = 0;
    char mode;

    /*
     * Parse arguments
     */
    if( 0 != parse_args(argc, argv, &access_file, &n_pages, &ram_info.n_frames, &n_cache, &n_tlb, &ram_info.algorithm) ) {//
        return -1;
    }

    /*
     * Setup data structures
     */
    srand(time(NULL));
    current_ref = (char *)malloc(sizeof(char) * MAX_LINE);
    clear_stats();

    stats.cache_size = n_cache;
    stats.tlb_size   = n_tlb;
    stats.num_pages  = n_pages;
    stats.num_frames = ram_info.n_frames;//

	printf("Allocating resources...\n");
    allocate_cache(n_cache);
    allocate_tlb(n_tlb);
    allocate_page_table(n_pages);
    allocate_page_directory(n_pages);
    //allocate_ram(n_frames);
    allocate_ram(ram_info);

    /*
     * Open the file that we are going to read
     */
    if( NULL == (access_fd = fopen(access_file, "r") ) ) {
        fprintf(stderr, "Error: Unable to open the access file <%s>\n", access_file);
        return -1;
    }

    /*
     * Read page requests from the file
     */
    gettimeofday(&stats.start, NULL);

    while(0 == feof(access_fd) ) {
        /* Read one line */
        current_ref[0] = '\0';
        if( NULL == fgets(current_ref, MAX_LINE, access_fd) ) {
            break;
        }

        /* Strip off the newline */
        if( '\n' == current_ref[strlen(current_ref)-1] ) {
            current_ref[strlen(current_ref)-1] = '\0';
        }

        extract_args(current_ref, &pid, &mode, &virtual_addr);

        /*
         * Memory management operations to access the page
         */
		if(VERBOSE){
			printf("-----------------------------------------------------------\n");
			printf("%s: Process %*d \t Access [Page %4d, Offset %#05x] (%#010x)\n",
				current_ref,
				MAX_PID_LEN, pid,
				GET_PAGE(virtual_addr), GET_OFFSET(virtual_addr), virtual_addr);
		}
		access_page(pid, mode, virtual_addr, &physical_addr);
		if(VERBOSE){
			printf("%s: Process %*d \t Access [Page %4d, Offset %#05x] (%#010x) --> (%#010x) [Frame %4d, Offset %#05x]\n",
				current_ref,
				MAX_PID_LEN, pid,
				GET_PAGE(virtual_addr), GET_OFFSET(virtual_addr), virtual_addr,
				physical_addr, GET_FRAME(physical_addr), GET_OFFSET(physical_addr));
		}
	}

    gettimeofday(&stats.end, NULL);
    display_stats();

    /*
     * Cleanup
     */
	 
    fclose(access_fd);
	
    if( NULL != current_ref ) {
        free(current_ref);
        current_ref = NULL;
    }
	
    free_ram();
	free_page_dir();
    free_tlb();
    free_cache();

    return 0;
}