Main Page   Compound List   File List   Compound Members   File Members  

main.c

Go to the documentation of this file.
00001 /*
00002  Copyright (c) 1992-1993 The Regents of the University of California.
00003  All rights reserved.  See copyright.h for copyright notice and limitation 
00004  of liability and disclaimer of warranty provisions.
00005  */
00006 
00007 #include "copyright.h"
00008 
00009 /* MIPS instruction interpreter */
00010 
00011 #include <stdio.h>
00012 #include <filehdr.h>
00013 #include <scnhdr.h>
00014 #include <syms.h>
00015 #include <ldfcn.h>
00016 #include "int.h"
00017 
00018 static FILE *fp;
00019 static LDFILE *ldptr;
00020 static SCNHDR texthead, rdatahead, datahead, sdatahead, sbsshead, bsshead;
00021 
00022 static char filename[1000] = "a.out";   /* default a.out file */
00023 static char self[256];                  /* name of invoking program */
00024 
00025 char mem[MEMSIZE];              /* main memory. use malloc later */
00026 int TRACE, Traptrace, Regtrace;
00027 int NROWS=64, ASSOC=1, LINESIZE=4, RAND=0, LRD=0;
00028 
00029 extern char *strcpy();
00030 
00031 main(argc, argv)
00032 int argc;
00033 char *argv[];
00034 {
00035         register char *s;
00036         char *fakeargv[3];
00037 
00038         strcpy(self, argv[0]);
00039         while  ( argc > 1  &&  argv[1][0] == '-' )
00040         {
00041                 --argc; ++argv;
00042                 for  ( s=argv[0]+1; *s != '\0'; ++s )
00043                         switch  ( *s )
00044                         {
00045                                 case 't':  TRACE = 1; break;
00046                                 case 'T':  Traptrace = 1; break;
00047                                 case 'r':  Regtrace = 1; break;
00048                                 case 'm':
00049                                         NROWS = atoi(*++argv);
00050                                         ASSOC = atoi(*++argv);
00051                                         LINESIZE = atoi(*++argv);
00052                                         RAND = ((*++argv)[0] == 'r');
00053                                         LRD = ((*argv)[0] == 'l')
00054                                            && ((*argv)[1] == 'r')
00055                                            && ((*argv)[2] == 'd');
00056                                         argc -= 4;
00057                                         break;
00058                         }
00059         }
00060 
00061         if (argc >= 2)
00062                 strcpy(filename, argv[1]);
00063         fp = fopen(filename, "r");
00064         if (fp == NULL)
00065         {
00066                 fprintf(stderr, "%s: Could not open '%s'\n", self, filename);
00067                 exit(0);
00068         }
00069         fclose(fp);
00070         load_program(filename);
00071         if  ( argv[1] == NULL )
00072         {
00073                 fakeargv[1] = "a.out";
00074                 fakeargv[2] = NULL;
00075                 argv = fakeargv;
00076                 ++argc;
00077         }
00078         runprogram(memoffset, argc-1, argv+1); /* where things normally start */
00079 }
00080 
00081 char *string(s)
00082 char *s;
00083 {
00084         char *p;
00085         extern char *malloc();
00086 
00087         p = malloc((unsigned) strlen(s)+1);
00088         strcpy(p, s);
00089         return p;
00090 }
00091 
00092 load_program(filename)
00093 char *filename;
00094 {
00095         register int pc, i, j, strindex, stl;
00096         char str[1111];
00097         int rc1, rc2;
00098 
00099         ldptr = ldopen(filename, NULL);
00100         if  ( ldptr == NULL )
00101         {
00102                 fprintf(stderr, "%s: Load read error on %s\n", self, filename);
00103                 exit(0);
00104         }
00105         if  ( TYPE(ldptr) != 0x162 )
00106         {
00107                 fprintf(stderr,
00108                         "big-endian object file (little-endian interp)\n");
00109                 exit(0);
00110         }
00111 
00112 #define LOADSECTION(head)       \
00113         if  ( head.s_scnptr != 0 ) \
00114         { \
00115                 /* printf("loading %s\n", head.s_name); /* */ \
00116                 pc = head.s_vaddr; \
00117                 FSEEK(ldptr, head.s_scnptr, 0); \
00118                 for  ( i=0; i<head.s_size; ++i ) \
00119                         *(char *) ((mem-memoffset)+pc++) = getc(fp); \
00120                 if (pc-memoffset >= MEMSIZE) \
00121                         { printf("MEMSIZE too small. Fix and recompile.\n"); \
00122                         exit(1); } \
00123         }
00124 
00125         if  ( ldnshread(ldptr, ".text", &texthead) != 1 )
00126                 printf("text section header missing\n");
00127         else
00128                 LOADSECTION(texthead)
00129 
00130         if  ( ldnshread(ldptr, ".rdata", &rdatahead) != 1 )
00131                 printf("rdata section header missing\n");
00132         else
00133                 LOADSECTION(rdatahead)
00134 
00135         if  ( ldnshread(ldptr, ".data", &datahead) != 1 )
00136                 printf("data section header missing\n");
00137         else
00138                 LOADSECTION(datahead)
00139 
00140         if  ( ldnshread(ldptr, ".sdata", &sdatahead) != 1 )
00141                 printf("sdata section header missing\n");
00142         else
00143                 LOADSECTION(sdatahead)
00144 
00145         if  ( ldnshread(ldptr, ".sbss", &sbsshead) != 1 )
00146                 printf("sbss section header missing\n");
00147         else
00148                 LOADSECTION(sbsshead)
00149 
00150         if  ( ldnshread(ldptr, ".bss", &bsshead) != 1 )
00151                 printf("bss section header missing\n");
00152         else
00153                 LOADSECTION(bsshead)
00154 
00155         /* BSS is already zeroed (statically-allocated mem) */
00156         /* this version ignores relocation info */
00157 }
00158 
00159 
00160 int *m_alloc(n)
00161 int n;
00162 {
00163         extern char *malloc();
00164 
00165         return (int *) (int) malloc((unsigned) n);
00166 }
00167 

Generated on Mon Feb 10 09:54:45 2003 for nachos by doxygen1.2.14 written by Dimitri van Heesch, © 1997-2002
The University of Southern California does not screen or control the content on this website and thus does not guarantee the accuracy, integrity, or quality of such content. All content on this website is provided by and is the sole responsibility of the person from which such content originated, and such content does not necessarily reflect the opinions of the University administration or the Board of Trustees