/***************************************************************************/
/*                            DATAPATH SIMULATOR                           */
/*                                                                         */
/*                              By Emily Ezust                             */
/*                                   1993				   */
/*                                                                         */
/*                 Based on Andrew Tanenbaum's hypothetical                */
/*                   Complex Instruction Set Computer                      */
/*                                                                         */
/***************************************************************************/

/***************************************************************/
/*                                                             */
/* Copyright (c) 1995 by Emily Ezust                           */
/*                                                             */
/* This software is hereby licensed for non-commercial use     */
/* only.  It may be freely distributed but it may not be used  */
/* for monetary profit.  Commercial use of this software is    */
/* not permitted without an explicit written agreement with    */
/* the author.                                                 */
/*                                                             */
/***************************************************************/


/* file administration module */
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "structs.h"
#include <signal.h>
#include <curses.h>
#include "constants.h"
#define FILENAME_MAX 30
extern int mcp[CONTROL_STORE_SIZE][INST_LENGTH];
extern int num_inst ;
extern int program_size ;
extern int ram[RAM_SIZE] ;
extern char microcode[MAX_MICROCODE_SIZE][MAX_PSEUDOINST_LENGTH] ;

extern int read_code(FILE *, int) ;
extern void print_code(int, FILE *, int, int) ;

void load_mic()
{
  FILE *fp = NULL;
  char mic_file[20] ;
  int i = 0, m = 0;
  int j ;
  char c;
  
  move(22,0) ;
  printw("Filename of the 13-component-format microprogram: ") ;
  refresh() ;
  scanw("%s", mic_file) ;
  move(22,0) ;
  clrtoeol() ;
  if ((fp = fopen(mic_file, "r")) != NULL)
    {
      /* put instructions from file into array mcp of microinstructions */

      for(i =0, m = 0; !feof(fp); i++) 
	{
	  fscanf(fp, "%d", &mcp[m][i]) ;
	  if (i == INST_LENGTH - 1) 
	    {
	      i = -1 ;
	      m++ ;
	    }
	}
      num_inst = m ;
      if (m == 1)
	message("Loaded one instruction.") ;
      else
	message2("Loaded ", m, " instructions.") ;
      fclose(fp) ;
    }
  else 
    message("File does not exist.") ; 
  refresh() ;
  move(22,0) ;
  printw("Enter the filename of the microprogram pseudocode (Mic-1): ") ;
  refresh() ;
  scanw("%s", mic_file) ;
  move(22,0) ;
  clrtoeol() ;
  if ((fp = fopen(mic_file, "r")) != NULL)
    {
      /* put instructions from file into array microcode of pseudocode */
      for(i =0; !feof(fp) && i <= num_inst; i++)  
	{
	  fscanf(fp, "%c", &c) ;
	  for (j = 0; (c != '\n') && (!feof(fp)) ; j++) 
	    {
	      microcode[i][j] = c ;
	      fscanf(fp, "%c", &c) ;
	    }
	  microcode[i][j] = '\0' ;
	}
      fclose(fp) ;
    }
  else 
    message("File does not exist.") ; 
}

void load_program(void)
{
  FILE *fp = NULL;
  char prog_file[20] ;
  int i, temp ;
 
  move(22,0) ;
  printw("Enter the filename of the Mac-1 file: ") ;
  refresh() ;
  scanw("%s", prog_file) ;
  move(22,0) ;
  clrtoeol() ;
  refresh() ;
  if ((fp = fopen(prog_file, "r")) != NULL)
    {
      /* put code from file into array ram of Mac-1 code.
	 File contains assembly language */
      for(i = 0; !feof(fp); i++) 
	{
	  temp = read_code(fp, FALSE) ;
	  if (temp < pow2(16)) 
	    ram[i] = temp ;
	  else if (temp == pow2(17))
	    i-- ;
	  else
	    {
	      message2("Error! Bad code on line ", i, "") ; 
	      i = 0;
	      break;
	    }
	}
      program_size = i ;
      if (i == 1)
	message("Loaded one line of code.") ;
      else
	message2("Loaded ", i, " lines of code.") ;
      fclose(fp) ;
    }
  else
    message("File does not exist.") ; 
}

void save_mic() 
{
  FILE *fp ;
  char mic_file[FILENAME_MAX] ;
  int i, m ;
  char overwrite = 'y';

  if (num_inst) 
    {
      move(22,0) ;      
      printw("Name of file in which to save the Mic-1 microprogram: ") ;
      refresh() ;
      scanw("%s", mic_file) ;
      move(22,0) ;
      clrtoeol() ;
      getch() ;
      if ((fp = fopen(mic_file, "r")) != NULL) 
	{
	  printw("File already exists. Overwrite [y/n]? ") ;
	  refresh() ;
	  overwrite = getch() ;
	  move(22,0) ;
	  clrtoeol() ; 
	  refresh() ; 
	}
      if (overwrite == 'y' || overwrite == 'Y')
	{
	  fp = fopen(mic_file, "w") ;
	  for (i = 0; i < num_inst; i++) 
	    {
	      for (m = 0; m < INST_LENGTH; m++)
		fprintf(fp, "%d ", mcp[i][m]) ;
	      fprintf(fp, "\n") ;
	    }
 	  message("Microprogram saved.") ;
	}
      else
	message("Microprogram not saved.") ; 
      fclose(fp) ;
    }
  else
    message("No microprogram to save!") ; 
}

void save_program() 
{
  FILE *fp ;
  char prog_file[FILENAME_MAX] ;
  char overwrite = 'y' ;
  int i ;
  
  if (program_size)
    {
      move(22,0) ;      
      printw("Name of file in which to save the Mac-1 program: ") ;
      refresh() ;
      scanw("%s", prog_file) ;
      move(22,0) ;
      clrtoeol() ;
      refresh() ;
      if ((fp = fopen(prog_file, "r")) != NULL) 
	{
	  getch() ;
	  printw("File already exists. Overwrite [y/n]? ") ;
	  refresh() ;
	  overwrite = getch() ;
	  move(22,0) ;
	  clrtoeol() ;
	  refresh() ;
	}
      if (overwrite == 'y' || overwrite == 'Y') 
	{
	  fp = fopen(prog_file, "w") ;
	  for (i = 0; i < program_size; i++)  
	    {
	      print_code(i, fp, RAM, FALSE) ;
	      if (i < program_size - 1)
		fprintf(fp, "\n") ;
	    }
	  message("Program saved.") ; 
	}
      else
	message("Program not saved.") ;
      fclose(fp) ;
    }
  else
    message("No program to save!") ;
}

