hex_file.cc revision 5541
15222Sksewell@umich.edu/*
25222Sksewell@umich.edu * Copyright (c) 2007 MIPS Technologies, Inc.
35222Sksewell@umich.edu * All rights reserved.
45222Sksewell@umich.edu *
55222Sksewell@umich.edu * Redistribution and use in source and binary forms, with or without
65222Sksewell@umich.edu * modification, are permitted provided that the following conditions are
75222Sksewell@umich.edu * met: redistributions of source code must retain the above copyright
85222Sksewell@umich.edu * notice, this list of conditions and the following disclaimer;
95222Sksewell@umich.edu * redistributions in binary form must reproduce the above copyright
105222Sksewell@umich.edu * notice, this list of conditions and the following disclaimer in the
115222Sksewell@umich.edu * documentation and/or other materials provided with the distribution;
125222Sksewell@umich.edu * neither the name of the copyright holders nor the names of its
135222Sksewell@umich.edu * contributors may be used to endorse or promote products derived from
145222Sksewell@umich.edu * this software without specific prior written permission.
155222Sksewell@umich.edu *
165222Sksewell@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
175222Sksewell@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
185222Sksewell@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
195222Sksewell@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
205222Sksewell@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
215222Sksewell@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
225222Sksewell@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
235222Sksewell@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
245222Sksewell@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
255222Sksewell@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
265222Sksewell@umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
275222Sksewell@umich.edu *
285222Sksewell@umich.edu * Authors: Jaidev Patwardhan
295222Sksewell@umich.edu */
305222Sksewell@umich.edu
315541Snate@binkert.org#include <cctype>
325541Snate@binkert.org#include <cstdio>
335222Sksewell@umich.edu#include <list>
345222Sksewell@umich.edu#include <string>
355222Sksewell@umich.edu
365222Sksewell@umich.edu#include "base/cprintf.hh"
375222Sksewell@umich.edu#include "base/loader/hex_file.hh"
385222Sksewell@umich.edu#include "base/loader/symtab.hh"
395222Sksewell@umich.edu#include "mem/translating_port.hh"
405222Sksewell@umich.edu
415222Sksewell@umich.eduusing namespace std;
425541Snate@binkert.org/*
435541Snate@binkert.org * Load a Hex File into memory. Currently only used with MIPS
445541Snate@binkert.org * BARE_IRON mode. A hex file consists of [Address Data] tuples that
455541Snate@binkert.org * get directly loaded into physical memory. The address specified is
465541Snate@binkert.org * a word address (i.e., to get the byte address, shift left by 2) The
475541Snate@binkert.org * data is a full 32-bit hex value.
485222Sksewell@umich.edu*/
495222Sksewell@umich.eduHexFile::HexFile(const string _filename)
505222Sksewell@umich.edu    : filename(_filename)
515222Sksewell@umich.edu{
525541Snate@binkert.org    fp = fopen(filename.c_str(), "r");
535541Snate@binkert.org    if (fp == NULL)
545541Snate@binkert.org        panic("Unable to open %s\n", filename.c_str());
555222Sksewell@umich.edu}
565222Sksewell@umich.edu
575222Sksewell@umich.eduHexFile::~HexFile()
585222Sksewell@umich.edu{
595222Sksewell@umich.edu}
605222Sksewell@umich.edu
615222Sksewell@umich.edubool
625541Snate@binkert.orgHexFile::loadSections(Port *memPort)
635222Sksewell@umich.edu{
645541Snate@binkert.org    char Line[64];
655541Snate@binkert.org    Addr MemAddr;
665541Snate@binkert.org    uint32_t Data;
675541Snate@binkert.org    while (!feof(fp)) {
685541Snate@binkert.org        fgets(Line, 64, fp);
695541Snate@binkert.org        parseLine(Line, &MemAddr, &Data);
705541Snate@binkert.org        if (MemAddr != 0) {
715541Snate@binkert.org            // Now, write to memory
725541Snate@binkert.org            memPort->writeBlob(MemAddr << 2, (uint8_t *)&Data, sizeof(Data));
735222Sksewell@umich.edu        }
745222Sksewell@umich.edu    }
755222Sksewell@umich.edu    return true;
765222Sksewell@umich.edu}
775541Snate@binkert.org
785541Snate@binkert.orgvoid
795541Snate@binkert.orgHexFile::parseLine(char *Str, Addr *A, uint32_t *D)
805222Sksewell@umich.edu{
815541Snate@binkert.org    int i = 0;
825541Snate@binkert.org    bool Flag = false;
835541Snate@binkert.org    *A = 0;
845541Snate@binkert.org    *D = 0;
855541Snate@binkert.org    int Digit = 0;
865541Snate@binkert.org    unsigned Number = 0;
875222Sksewell@umich.edu
885541Snate@binkert.org    /* Skip white spaces */
895541Snate@binkert.org    while (Str[i] != '\0' && Str[i]==' ')
905541Snate@binkert.org        i++;
915541Snate@binkert.org
925541Snate@binkert.org    /* Ok, we're at some character...process things */
935541Snate@binkert.org    while (Str[i] != '\0') {
945541Snate@binkert.org        if (Str[i] >= '0' && Str[i] <= '9') {
955541Snate@binkert.org            Digit = Str[i] - '0';
965541Snate@binkert.org        } else if (Str[i] >= 'a' && Str[i] <= 'f') {
975541Snate@binkert.org            Digit = Str[i] - 'a' + 10;
985541Snate@binkert.org        } else if (Str[i] >= 'A' && Str[i] <= 'F') {
995541Snate@binkert.org          Digit=Str[i]-'A'+10;
1005541Snate@binkert.org        } else if (Str[i] == ' ' || Str[i] == '\n') {
1015541Snate@binkert.org            if (Number == 0)
1025541Snate@binkert.org                return;
1035541Snate@binkert.org            if (Flag == false) {
1045541Snate@binkert.org                *A = Number;
1055541Snate@binkert.org                Number = 0;
1065541Snate@binkert.org                Flag = true;
1075541Snate@binkert.org            } else {
1085541Snate@binkert.org                *D = Number;
1095541Snate@binkert.org                return;
1105541Snate@binkert.org            }
1115541Snate@binkert.org        } else {
1125541Snate@binkert.org            // Ok, we've encountered a non-hex character, cannot be a
1135541Snate@binkert.org            // valid line, skip and return 0's
1145541Snate@binkert.org            *A = 0;
1155541Snate@binkert.org            *D = 0;
1165541Snate@binkert.org            return;
1175222Sksewell@umich.edu        }
1185222Sksewell@umich.edu
1195541Snate@binkert.org        Number <<= 4;
1205541Snate@binkert.org        Number += Digit;
1215541Snate@binkert.org        i++;
1225222Sksewell@umich.edu    }
1235541Snate@binkert.org
1245541Snate@binkert.org    if (Flag != true) {
1255541Snate@binkert.org        *A = 0;
1265541Snate@binkert.org        *D = 0;
1275541Snate@binkert.org    } else {
1285541Snate@binkert.org        *D = Number;
1295222Sksewell@umich.edu    }
1305222Sksewell@umich.edu}
1315222Sksewell@umich.edu
1325222Sksewell@umich.eduvoid
1335222Sksewell@umich.eduHexFile::close()
1345222Sksewell@umich.edu{
1355541Snate@binkert.org    fclose(fp);
1365222Sksewell@umich.edu}
137