Example #1
0
File: loader.c Project: 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
}
Example #2
0
void testFunctions(){
printf("inside main\n \n ");
printf("\n validFileName tests: \n");
printf("bumba.y should return 0: ");
printf("%d\n", validFileName("bumba.y"));
printf("noDot should return 0: ");
printf("%d\n", validFileName("noDot"));
printf("victory.yo should return 1: ");
printf("%d\n", validFileName("victory.yo"));

printf("\n isAddress tests: \n");
printf("  0x01H: should return 0:");
printf("%d\n",isAddress("  0x01H:"));
printf("  1x014: should return 0:");
printf("%d\n",isAddress("  1x014:"));
printf("  0x014: should return 1:");
printf("%d\n",isAddress("  0x014:"));
printf("  0x6A4: should return 1:");
printf("%d\n",isAddress("  0x064:"));

//need to add testing for 0x014:00 ... spaces ... |
printf("\n isData tests: \n");
printf("  0x014: 0z  should return 0:");
printf("%d\n",isData("  0x014: 0z"));
printf("  0x014: 00ddxx01daq should return 0:");
printf("%d\n",isData("  0x014: 00ddxx01da0q"));
printf("  0x014: 00345dzqa000 should return 0:");
printf("%d\n",isData("  0x014: 000345dzqa000"));
printf("  0x014: 00ddd0000000 should return 1:");
printf("%d\n",isData("  0x014: 00ddd0000000 "));
printf("  0x014: 00000000001 should return 1: ");
printf("%d\n",isData("  0x014: 000000000001"));
printf("  0x014: 01 should return 1: ");
printf("%d\n",isData("  0x014: 01"));

printf("\n isSpaces tests: \n");
printf("should return 0: wxrHtl-jhplzsk");
printf("%d\n",isSpaces("wxrHtl-jhplzsk",0,14));
printf("%d\n",isSpaces("wxrHtl-jhplzsk",0,14));
printf("isSpaces should return 1: ");
printf("%d\n",isSpaces("               ",0,14));
printf("isSpaces should return 1: ");
printf("%d\n",isSpaces("   ",0,2));
printf("isSpaces should return 0: ");
printf("%d\n",isSpaces("d  ",0,2));

printf("checkHex tests: \n");
printf("abcdzf0123456789 should return 0: ");
printf("%d\n",checkHex("abcdzf0123456789",0,15));
printf("abcdef0123456789 should return 1: ");
printf("%d\n",checkHex("abcdef0123456789",0,15));

printf("grabAddress tests: \n");
printf("  0x01a should return 0x01a: ");
printf("%x\n",grabAddress("  0x01a"));
printf("  0x014 should return 0x014: ");
printf("%x\n",grabAddress("  0x014"));

printf("grabDataByte tests: \n");
printf("  01a402, 4 should return 02: ");
printf("%x\n", grabDataByte("01a402",4));
printf("  01a402, 2 should return a4: ");
printf("%x\n", grabDataByte("01a402",2));
printf("  01, 0 should return 01: ");
printf("%x\n", grabDataByte("01",0));


printf("countBytes tests: \n");
printf("  0x014: 00000000001 should return 5: ");
printf("%x\n", countBytes("  0x014: 0000000001 "));
printf("  0x014: 0201 should return 2: ");
printf("%x\n", countBytes("  0x014: 0201"));
printf("  0x014: 020100 should return 3: ");
printf("%x\n", countBytes("  0x014: 020100"));
printf("  0x014:  should return 0: ");
printf("%x\n", countBytes("  0x014: "));

printf("checkLine tests: \n");
printf("  0x014 000000000001 |comments should return 0: ");
printf("%d\n", checkLine("  0x014 000000000001 |comments\n"));
printf("0x014: 000000000001 |comments should return 0: ");
printf("%d\n", checkLine("0x014: 000000000001 |comments\n"));
printf("  0x014: 000000000001 |comments should return 1: ");
printf("%d\n", checkLine("  0x014: 000000000001 |comments\n"));
//printf("  01a402, 2 should return a4: ");
//printf("%x\n", grabDataByte("01a402",2));

//printf("  01, 0 should return 01: ");
/*
printf("discardRest should return 0: \n");
printf("%d\n",discardRest("",0,15));
printf("discardRest should return 1: \n");
printf("%d\n",discardRest("",0,15));
*/
}
Example #3
0
/**
 * 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;

     
}