コード例 #1
0
ファイル: loader.c プロジェクト: 8l/YESS
/* Function Name: load
 * Purpose:       Driver function. Opens an input file, loads the machine code,
 *                performs a memory dump, then exits.
 *
 * Parameters:    argc - amount of arguments passed
 *                args[] - input file name
 * Returns:       TRUE if program loaded successfully
 *                FALSE if program failed to load
 */
bool load(int argc, char * args[])
{
    if((argc > 1) && (validFileName(args[1]))){ //check for valid file name and # of arguments

        FILE* f = fopen(args[1], "r"); //open the file
        
        unsigned char addrCounter = 0; //counter used to check for overlapping addresses
        char line[80];
        unsigned int addr;
        int i;              //Address parser count
        unsigned int lineCount = 0;
        int len;            //Length of instruction
        unsigned char dataByte;      //Temporary storage of info
        bool loadErr = FALSE;
        int check;
        while(fgets(line,80,f))     //While we haven't reached the end of the file...
        {           
            lineCount++;
            check = checkLine(line);                    //Check the line

            if(check == 1)                              //If the line is valid...
            {                                           
                addr = grabAddress(line);               //Grab the address
                if(addr < addrCounter){                 //Check for errors with the address
                    printError(lineCount, line);
                    loadErr = TRUE;
                    break;
                }
                len = lenInst(line);                    //Find the length of the instruction
                int i = 0;
                unsigned int byteNo = 0;
                while(byteNo < len)                          //And load the data byte by byte
                {
                    dataByte = grabDataByte(line, 9+i);
                    putByte(addr, dataByte, &loadErr);
                    addr++;
                    byteNo++;
                    addrCounter = addr;
                    i += 2;
                }
              
            }                 
            else if(check == 0)                         //If the line is invalid...
            {                                           //Halt loading and give an error message
                printError(lineCount, line);
                loadErr = TRUE;
                break;
            }    
            discardRest(line, f);                                 //If the line or instructions are just spaces, do nothing
        }

        return loadErr;                                 //return TRUE if program was successfully loaded
    }
    printf("file opening failed\n usage: yess <filename>.yo\n"); //error message

    return TRUE; //return if invalid filename
}
コード例 #2
0
ファイル: loader.c プロジェクト: srad1292/y86-simulator
/**
 * load
 * This function takes a command line argument containing
 * the name of a file and loads the contents of the file
 * into memory byte by byte until there is an error
 * @param: argc: the number of args
 *         args: the name of the file
 * @return: goodLine: returns false if an error was found
 */
bool load(int argc, char *args) {
    bool goodLine = TRUE; 
    int lineNumber = 1;
    if(validFileName(args)){
        FILE * fi;
        fi = fopen(args,"r");
        char record[80];      
        int prevAdr = -1,startAdr,lineSize;
        int firstLine = 0;
        int loop;
        int dataLoc,byteLoc;
        int dataLength;
        bool lastLineData;
        bool memError;
        while(fgets(record,80,fi)!=NULL && goodLine){
            dataLoc = 9;  //Where data starts
            byteLoc = 0;  //Which byte of data to get
            if (isAddress(record)){ 
                startAdr=setLastAddress(record); //Address in a record
            }
            int lineSize = lineLength(record); //Length of record
                
            if(record[lineSize]!='\n') {
                discardRest(record,fi);
            }
            
            //Calls the checkLine
            if(checkLine(record)) {
                dataLength = lengthOfData(record); //Length of data in a record
                
                //If the record isn't the first line of the file
                if(firstLine==1) {
                   if(!(lastLineData&&startAdr>prevAdr)) { //If there was data on the last line make sure the address is bigger
                      goodLine = FALSE;
                      //printf("Error on line %d\n",lineNumber);
                   }
                }
                //If the record is the first line
                else{
                    if(isAddress(record)) {
                        firstLine = 1;
                    }
                }
            }
            
            //checkLine returns false
            else{ 
                //printf("Error on line %d\n",lineNumber);
                goodLine = FALSE;
            }
            
            //If all checks have passed
            if(goodLine){
                //Goes through the data
                for(loop = 0; loop < dataLength;loop+=2){
                    //Adds the byte to memory
                    putByte(startAdr,grabDataByte(record,byteLoc),&memError);
                    //printf("%x\n",grabDataByte(record,byteLoc));
                    startAdr++;
                    byteLoc+=2;
                }
                //puts(record); //Output record
                prevAdr = startAdr - 1;
                lineNumber++;
                lastLineData = isData(record);
           } 
           else if(goodLine == FALSE){
               printf("Error on line %d\n", lineNumber);
               puts(record);
           }
      }   
        fclose(fi);
     }
     
     
     //If the file didn't open   
     else{
         goodLine = FALSE;
         printf("Error opening file");
     }
     
     
     return goodLine;

     
}