110152Satgutier@umich.edu/*****************************************************************************
210152Satgutier@umich.edu *                                McPAT
310152Satgutier@umich.edu *                      SOFTWARE LICENSE AGREEMENT
410152Satgutier@umich.edu *            Copyright 2012 Hewlett-Packard Development Company, L.P.
510234Syasuko.eckert@amd.com *            Copyright (c) 2010-2013 Advanced Micro Devices, Inc.
610152Satgutier@umich.edu *                          All Rights Reserved
710152Satgutier@umich.edu *
810152Satgutier@umich.edu * Redistribution and use in source and binary forms, with or without
910152Satgutier@umich.edu * modification, are permitted provided that the following conditions are
1010152Satgutier@umich.edu * met: redistributions of source code must retain the above copyright
1110152Satgutier@umich.edu * notice, this list of conditions and the following disclaimer;
1210152Satgutier@umich.edu * redistributions in binary form must reproduce the above copyright
1310152Satgutier@umich.edu * notice, this list of conditions and the following disclaimer in the
1410152Satgutier@umich.edu * documentation and/or other materials provided with the distribution;
1510152Satgutier@umich.edu * neither the name of the copyright holders nor the names of its
1610152Satgutier@umich.edu * contributors may be used to endorse or promote products derived from
1710152Satgutier@umich.edu * this software without specific prior written permission.
1810152Satgutier@umich.edu
1910152Satgutier@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2010152Satgutier@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2110152Satgutier@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2210152Satgutier@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2310152Satgutier@umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2410152Satgutier@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2510152Satgutier@umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2610152Satgutier@umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2710152Satgutier@umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2810152Satgutier@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2910234Syasuko.eckert@amd.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3010152Satgutier@umich.edu *
3110152Satgutier@umich.edu ***************************************************************************/
3210234Syasuko.eckert@amd.com#include <sys/stat.h>
3310234Syasuko.eckert@amd.com
3410234Syasuko.eckert@amd.com#include <cassert>
3510152Satgutier@umich.edu#include <iostream>
3610152Satgutier@umich.edu
3710234Syasuko.eckert@amd.com#include "basic_components.h"
3810152Satgutier@umich.edu#include "io.h"
3910234Syasuko.eckert@amd.com#include "system.h"
4010152Satgutier@umich.edu#include "version.h"
4110152Satgutier@umich.edu#include "xmlParser.h"
4210152Satgutier@umich.edu
4310152Satgutier@umich.eduusing namespace std;
4410152Satgutier@umich.edu
4510152Satgutier@umich.eduvoid print_usage(char * argv0);
4610152Satgutier@umich.edu
4710234Syasuko.eckert@amd.comint main(int argc, char *argv[]) {
4810234Syasuko.eckert@amd.com    char* xml_file = NULL;
4910234Syasuko.eckert@amd.com    int plevel = 2;
5010234Syasuko.eckert@amd.com
5110234Syasuko.eckert@amd.com    for (int32_t i = 0; i < argc; i++) {
5210234Syasuko.eckert@amd.com        if (argv[i] == string("-infile")) {
5310234Syasuko.eckert@amd.com            xml_file = argv[++i];
5410234Syasuko.eckert@amd.com
5510234Syasuko.eckert@amd.com        } else if (argv[i] == string("-print_level")) {
5610234Syasuko.eckert@amd.com            plevel = atoi(argv[++i]);
5710234Syasuko.eckert@amd.com
5810234Syasuko.eckert@amd.com        } else if (argv[i] == string("-opt_for_clk")) {
5910234Syasuko.eckert@amd.com            McPATComponent::opt_for_clk = (bool)atoi(argv[++i]);
6010152Satgutier@umich.edu        }
6110234Syasuko.eckert@amd.com    }
6210152Satgutier@umich.edu
6310234Syasuko.eckert@amd.com    // Ensure that the XML file was specified
6410234Syasuko.eckert@amd.com    if (xml_file == NULL) {
6510234Syasuko.eckert@amd.com        cerr << "ERROR: Please specify infile\n\n";
6610234Syasuko.eckert@amd.com        print_usage(argv[0]);
6710234Syasuko.eckert@amd.com    }
6810152Satgutier@umich.edu
6910234Syasuko.eckert@amd.com    // Ensure that the XML file exists
7010234Syasuko.eckert@amd.com    struct stat file_info;
7110234Syasuko.eckert@amd.com    if (stat(xml_file, &file_info)) {
7210234Syasuko.eckert@amd.com        cerr << "ERROR: File not found: " << xml_file << endl << endl;
7310234Syasuko.eckert@amd.com        print_usage(argv[0]);
7410234Syasuko.eckert@amd.com    }
7510152Satgutier@umich.edu
7610234Syasuko.eckert@amd.com    cout << "McPAT (version " << VER_MAJOR << "." << VER_MINOR
7710234Syasuko.eckert@amd.com         << " of " << VER_UPDATE << ") is computing the target processor...\n "
7810234Syasuko.eckert@amd.com         << endl;
7910152Satgutier@umich.edu
8010234Syasuko.eckert@amd.com    // Parse the XML input file
8110234Syasuko.eckert@amd.com    XMLNode xml_data = XMLNode::openFileHelper(xml_file, "component");
8210234Syasuko.eckert@amd.com    unsigned int num_children = xml_data.nChildNode("component");
8310234Syasuko.eckert@amd.com    assert(num_children == 1);
8410234Syasuko.eckert@amd.com    XMLNode system_xml = xml_data.getChildNode("component");
8510234Syasuko.eckert@amd.com    assert(strcmp(system_xml.getAttribute("type"), "System") == 0);
8610152Satgutier@umich.edu
8710234Syasuko.eckert@amd.com    // Recursively instantiate the system hierarchy
8810234Syasuko.eckert@amd.com    System* system = new System(&system_xml);
8910152Satgutier@umich.edu
9010234Syasuko.eckert@amd.com    // Recursively compute chip area
9110234Syasuko.eckert@amd.com    system->computeArea();
9210234Syasuko.eckert@amd.com
9310234Syasuko.eckert@amd.com    // Recursively compute the power consumed
9410234Syasuko.eckert@amd.com    system->computeEnergy();
9510234Syasuko.eckert@amd.com
9610234Syasuko.eckert@amd.com    // Recursively output the computed values
9710234Syasuko.eckert@amd.com    system->displayData(2, plevel);
9810234Syasuko.eckert@amd.com
9910234Syasuko.eckert@amd.com    // Clean up
10010234Syasuko.eckert@amd.com    delete system;
10110234Syasuko.eckert@amd.com    return 0;
10210234Syasuko.eckert@amd.com
10310152Satgutier@umich.edu}
10410152Satgutier@umich.edu
10510234Syasuko.eckert@amd.comvoid print_usage(char * argv0) {
10610152Satgutier@umich.edu    cerr << "How to use McPAT:" << endl;
10710234Syasuko.eckert@amd.com    cerr << "  mcpat -infile <input file name>  -print_level < "
10810234Syasuko.eckert@amd.com         << "level of details 0~5 >  -opt_for_clk < 0 (optimize for ED^2P "
10910234Syasuko.eckert@amd.com         << "only)/1 (optimzed for target clock rate)>" << endl;
11010152Satgutier@umich.edu    exit(1);
11110152Satgutier@umich.edu}
112