cpu.cc revision 2873
11689SN/A/*
22325SN/A * Copyright (c) 2004-2006 The Regents of The University of Michigan
31689SN/A * All rights reserved.
41689SN/A *
51689SN/A * Redistribution and use in source and binary forms, with or without
61689SN/A * modification, are permitted provided that the following conditions are
71689SN/A * met: redistributions of source code must retain the above copyright
81689SN/A * notice, this list of conditions and the following disclaimer;
91689SN/A * redistributions in binary form must reproduce the above copyright
101689SN/A * notice, this list of conditions and the following disclaimer in the
111689SN/A * documentation and/or other materials provided with the distribution;
121689SN/A * neither the name of the copyright holders nor the names of its
131689SN/A * contributors may be used to endorse or promote products derived from
141689SN/A * this software without specific prior written permission.
151689SN/A *
161689SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
171689SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
181689SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
191689SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
201689SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
211689SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
221689SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
231689SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
241689SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
251689SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
261689SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Kevin Lim
292756Sksewell@umich.edu *          Korey Sewell
301689SN/A */
311689SN/A
321858SN/A#include "config/full_system.hh"
332733Sktlim@umich.edu#include "config/use_checker.hh"
341858SN/A
351858SN/A#if FULL_SYSTEM
361060SN/A#include "sim/system.hh"
371060SN/A#else
381060SN/A#include "sim/process.hh"
391060SN/A#endif
401060SN/A
412325SN/A#include "cpu/activity.hh"
422683Sktlim@umich.edu#include "cpu/simple_thread.hh"
432680Sktlim@umich.edu#include "cpu/thread_context.hh"
442817Sksewell@umich.edu#include "cpu/o3/isa_specific.hh"
451717SN/A#include "cpu/o3/cpu.hh"
461060SN/A
472325SN/A#include "sim/root.hh"
482292SN/A#include "sim/stat_control.hh"
492292SN/A
502794Sktlim@umich.edu#if USE_CHECKER
512794Sktlim@umich.edu#include "cpu/checker/cpu.hh"
522794Sktlim@umich.edu#endif
532794Sktlim@umich.edu
541060SN/Ausing namespace std;
552669Sktlim@umich.eduusing namespace TheISA;
561060SN/A
572733Sktlim@umich.eduBaseO3CPU::BaseO3CPU(Params *params)
582292SN/A    : BaseCPU(params), cpu_id(0)
591060SN/A{
601060SN/A}
611060SN/A
622292SN/Avoid
632733Sktlim@umich.eduBaseO3CPU::regStats()
642292SN/A{
652292SN/A    BaseCPU::regStats();
662292SN/A}
672292SN/A
681060SN/Atemplate <class Impl>
691755SN/AFullO3CPU<Impl>::TickEvent::TickEvent(FullO3CPU<Impl> *c)
701060SN/A    : Event(&mainEventQueue, CPU_Tick_Pri), cpu(c)
711060SN/A{
721060SN/A}
731060SN/A
741060SN/Atemplate <class Impl>
751060SN/Avoid
761755SN/AFullO3CPU<Impl>::TickEvent::process()
771060SN/A{
781060SN/A    cpu->tick();
791060SN/A}
801060SN/A
811060SN/Atemplate <class Impl>
821060SN/Aconst char *
831755SN/AFullO3CPU<Impl>::TickEvent::description()
841060SN/A{
851755SN/A    return "FullO3CPU tick event";
861060SN/A}
871060SN/A
881060SN/Atemplate <class Impl>
892829Sksewell@umich.eduFullO3CPU<Impl>::ActivateThreadEvent::ActivateThreadEvent()
902829Sksewell@umich.edu    : Event(&mainEventQueue, CPU_Tick_Pri)
912829Sksewell@umich.edu{
922829Sksewell@umich.edu}
932829Sksewell@umich.edu
942829Sksewell@umich.edutemplate <class Impl>
952829Sksewell@umich.eduvoid
962829Sksewell@umich.eduFullO3CPU<Impl>::ActivateThreadEvent::init(int thread_num,
972829Sksewell@umich.edu                                           FullO3CPU<Impl> *thread_cpu)
982829Sksewell@umich.edu{
992829Sksewell@umich.edu    tid = thread_num;
1002829Sksewell@umich.edu    cpu = thread_cpu;
1012829Sksewell@umich.edu}
1022829Sksewell@umich.edu
1032829Sksewell@umich.edutemplate <class Impl>
1042829Sksewell@umich.eduvoid
1052829Sksewell@umich.eduFullO3CPU<Impl>::ActivateThreadEvent::process()
1062829Sksewell@umich.edu{
1072829Sksewell@umich.edu    cpu->activateThread(tid);
1082829Sksewell@umich.edu}
1092829Sksewell@umich.edu
1102829Sksewell@umich.edutemplate <class Impl>
1112829Sksewell@umich.educonst char *
1122829Sksewell@umich.eduFullO3CPU<Impl>::ActivateThreadEvent::description()
1132829Sksewell@umich.edu{
1142829Sksewell@umich.edu    return "FullO3CPU \"Activate Thread\" event";
1152829Sksewell@umich.edu}
1162829Sksewell@umich.edu
1172829Sksewell@umich.edutemplate <class Impl>
1182292SN/AFullO3CPU<Impl>::FullO3CPU(Params *params)
1192733Sktlim@umich.edu    : BaseO3CPU(params),
1201060SN/A      tickEvent(this),
1212292SN/A      removeInstsThisCycle(false),
1221060SN/A      fetch(params),
1231060SN/A      decode(params),
1241060SN/A      rename(params),
1251060SN/A      iew(params),
1261060SN/A      commit(params),
1271060SN/A
1282292SN/A      regFile(params->numPhysIntRegs, params->numPhysFloatRegs),
1291060SN/A
1302831Sksewell@umich.edu      freeList(params->numberOfThreads,
1312292SN/A               TheISA::NumIntRegs, params->numPhysIntRegs,
1322292SN/A               TheISA::NumFloatRegs, params->numPhysFloatRegs),
1331060SN/A
1342292SN/A      rob(params->numROBEntries, params->squashWidth,
1352292SN/A          params->smtROBPolicy, params->smtROBThreshold,
1362292SN/A          params->numberOfThreads),
1371060SN/A
1382831Sksewell@umich.edu      scoreboard(params->numberOfThreads,
1392292SN/A                 TheISA::NumIntRegs, params->numPhysIntRegs,
1402292SN/A                 TheISA::NumFloatRegs, params->numPhysFloatRegs,
1412292SN/A                 TheISA::NumMiscRegs * number_of_threads,
1422292SN/A                 TheISA::ZeroReg),
1431060SN/A
1442873Sktlim@umich.edu      timeBuffer(params->backComSize, params->forwardComSize),
1452873Sktlim@umich.edu      fetchQueue(params->backComSize, params->forwardComSize),
1462873Sktlim@umich.edu      decodeQueue(params->backComSize, params->forwardComSize),
1472873Sktlim@umich.edu      renameQueue(params->backComSize, params->forwardComSize),
1482873Sktlim@umich.edu      iewQueue(params->backComSize, params->forwardComSize),
1492873Sktlim@umich.edu      activityRec(NumStages,
1502873Sktlim@umich.edu                  params->backComSize + params->forwardComSize,
1512873Sktlim@umich.edu                  params->activity),
1521060SN/A
1531060SN/A      globalSeqNum(1),
1541060SN/A
1551858SN/A#if FULL_SYSTEM
1562292SN/A      system(params->system),
1571060SN/A      physmem(system->physmem),
1581060SN/A#endif // FULL_SYSTEM
1592292SN/A      mem(params->mem),
1602843Sktlim@umich.edu      drainCount(0),
1612316SN/A      deferRegistration(params->deferRegistration),
1622316SN/A      numThreads(number_of_threads)
1631060SN/A{
1641060SN/A    _status = Idle;
1651681SN/A
1662733Sktlim@umich.edu    checker = NULL;
1672733Sktlim@umich.edu
1682794Sktlim@umich.edu    if (params->checker) {
1692733Sktlim@umich.edu#if USE_CHECKER
1702316SN/A        BaseCPU *temp_checker = params->checker;
1712316SN/A        checker = dynamic_cast<Checker<DynInstPtr> *>(temp_checker);
1722316SN/A        checker->setMemory(mem);
1732316SN/A#if FULL_SYSTEM
1742316SN/A        checker->setSystem(params->system);
1752316SN/A#endif
1762794Sktlim@umich.edu#else
1772794Sktlim@umich.edu        panic("Checker enabled but not compiled in!");
1782794Sktlim@umich.edu#endif // USE_CHECKER
1792316SN/A    }
1802316SN/A
1811858SN/A#if !FULL_SYSTEM
1822292SN/A    thread.resize(number_of_threads);
1832292SN/A    tids.resize(number_of_threads);
1841681SN/A#endif
1851681SN/A
1862325SN/A    // The stages also need their CPU pointer setup.  However this
1872325SN/A    // must be done at the upper level CPU because they have pointers
1882325SN/A    // to the upper level CPU, and not this FullO3CPU.
1891060SN/A
1902292SN/A    // Set up Pointers to the activeThreads list for each stage
1912292SN/A    fetch.setActiveThreads(&activeThreads);
1922292SN/A    decode.setActiveThreads(&activeThreads);
1932292SN/A    rename.setActiveThreads(&activeThreads);
1942292SN/A    iew.setActiveThreads(&activeThreads);
1952292SN/A    commit.setActiveThreads(&activeThreads);
1961060SN/A
1971060SN/A    // Give each of the stages the time buffer they will use.
1981060SN/A    fetch.setTimeBuffer(&timeBuffer);
1991060SN/A    decode.setTimeBuffer(&timeBuffer);
2001060SN/A    rename.setTimeBuffer(&timeBuffer);
2011060SN/A    iew.setTimeBuffer(&timeBuffer);
2021060SN/A    commit.setTimeBuffer(&timeBuffer);
2031060SN/A
2041060SN/A    // Also setup each of the stages' queues.
2051060SN/A    fetch.setFetchQueue(&fetchQueue);
2061060SN/A    decode.setFetchQueue(&fetchQueue);
2072292SN/A    commit.setFetchQueue(&fetchQueue);
2081060SN/A    decode.setDecodeQueue(&decodeQueue);
2091060SN/A    rename.setDecodeQueue(&decodeQueue);
2101060SN/A    rename.setRenameQueue(&renameQueue);
2111060SN/A    iew.setRenameQueue(&renameQueue);
2121060SN/A    iew.setIEWQueue(&iewQueue);
2131060SN/A    commit.setIEWQueue(&iewQueue);
2141060SN/A    commit.setRenameQueue(&renameQueue);
2151060SN/A
2162292SN/A    commit.setIEWStage(&iew);
2172292SN/A    rename.setIEWStage(&iew);
2182292SN/A    rename.setCommitStage(&commit);
2192292SN/A
2202292SN/A#if !FULL_SYSTEM
2212307SN/A    int active_threads = params->workload.size();
2222831Sksewell@umich.edu
2232831Sksewell@umich.edu    if (active_threads > Impl::MaxThreads) {
2242831Sksewell@umich.edu        panic("Workload Size too large. Increase the 'MaxThreads'"
2252831Sksewell@umich.edu              "constant in your O3CPU impl. file (e.g. o3/alpha/impl.hh) or "
2262831Sksewell@umich.edu              "edit your workload size.");
2272831Sksewell@umich.edu    }
2282292SN/A#else
2292307SN/A    int active_threads = 1;
2302292SN/A#endif
2312292SN/A
2322316SN/A    //Make Sure That this a Valid Architeture
2332292SN/A    assert(params->numPhysIntRegs   >= numThreads * TheISA::NumIntRegs);
2342292SN/A    assert(params->numPhysFloatRegs >= numThreads * TheISA::NumFloatRegs);
2352292SN/A
2362292SN/A    rename.setScoreboard(&scoreboard);
2372292SN/A    iew.setScoreboard(&scoreboard);
2382292SN/A
2391060SN/A    // Setup the rename map for whichever stages need it.
2402292SN/A    PhysRegIndex lreg_idx = 0;
2412292SN/A    PhysRegIndex freg_idx = params->numPhysIntRegs; //Index to 1 after int regs
2421060SN/A
2432292SN/A    for (int tid=0; tid < numThreads; tid++) {
2442307SN/A        bool bindRegs = (tid <= active_threads - 1);
2452292SN/A
2462292SN/A        commitRenameMap[tid].init(TheISA::NumIntRegs,
2472292SN/A                                  params->numPhysIntRegs,
2482325SN/A                                  lreg_idx,            //Index for Logical. Regs
2492292SN/A
2502292SN/A                                  TheISA::NumFloatRegs,
2512292SN/A                                  params->numPhysFloatRegs,
2522325SN/A                                  freg_idx,            //Index for Float Regs
2532292SN/A
2542292SN/A                                  TheISA::NumMiscRegs,
2552292SN/A
2562292SN/A                                  TheISA::ZeroReg,
2572292SN/A                                  TheISA::ZeroReg,
2582292SN/A
2592292SN/A                                  tid,
2602292SN/A                                  false);
2612292SN/A
2622292SN/A        renameMap[tid].init(TheISA::NumIntRegs,
2632292SN/A                            params->numPhysIntRegs,
2642325SN/A                            lreg_idx,                  //Index for Logical. Regs
2652292SN/A
2662292SN/A                            TheISA::NumFloatRegs,
2672292SN/A                            params->numPhysFloatRegs,
2682325SN/A                            freg_idx,                  //Index for Float Regs
2692292SN/A
2702292SN/A                            TheISA::NumMiscRegs,
2712292SN/A
2722292SN/A                            TheISA::ZeroReg,
2732292SN/A                            TheISA::ZeroReg,
2742292SN/A
2752292SN/A                            tid,
2762292SN/A                            bindRegs);
2772292SN/A    }
2782292SN/A
2792292SN/A    rename.setRenameMap(renameMap);
2802292SN/A    commit.setRenameMap(commitRenameMap);
2812292SN/A
2822292SN/A    // Give renameMap & rename stage access to the freeList;
2832292SN/A    for (int i=0; i < numThreads; i++) {
2842292SN/A        renameMap[i].setFreeList(&freeList);
2852292SN/A    }
2861060SN/A    rename.setFreeList(&freeList);
2872292SN/A
2881060SN/A    // Setup the ROB for whichever stages need it.
2891060SN/A    commit.setROB(&rob);
2902292SN/A
2912292SN/A    lastRunningCycle = curTick;
2922292SN/A
2932829Sksewell@umich.edu    lastActivatedCycle = -1;
2942829Sksewell@umich.edu
2952292SN/A    contextSwitch = false;
2961060SN/A}
2971060SN/A
2981060SN/Atemplate <class Impl>
2991755SN/AFullO3CPU<Impl>::~FullO3CPU()
3001060SN/A{
3011060SN/A}
3021060SN/A
3031060SN/Atemplate <class Impl>
3041060SN/Avoid
3051755SN/AFullO3CPU<Impl>::fullCPURegStats()
3061062SN/A{
3072733Sktlim@umich.edu    BaseO3CPU::regStats();
3082292SN/A
3092733Sktlim@umich.edu    // Register any of the O3CPU's stats here.
3102292SN/A    timesIdled
3112292SN/A        .name(name() + ".timesIdled")
3122292SN/A        .desc("Number of times that the entire CPU went into an idle state and"
3132292SN/A              " unscheduled itself")
3142292SN/A        .prereq(timesIdled);
3152292SN/A
3162292SN/A    idleCycles
3172292SN/A        .name(name() + ".idleCycles")
3182292SN/A        .desc("Total number of cycles that the CPU has spent unscheduled due "
3192292SN/A              "to idling")
3202292SN/A        .prereq(idleCycles);
3212292SN/A
3222292SN/A    // Number of Instructions simulated
3232292SN/A    // --------------------------------
3242292SN/A    // Should probably be in Base CPU but need templated
3252292SN/A    // MaxThreads so put in here instead
3262292SN/A    committedInsts
3272292SN/A        .init(numThreads)
3282292SN/A        .name(name() + ".committedInsts")
3292292SN/A        .desc("Number of Instructions Simulated");
3302292SN/A
3312292SN/A    totalCommittedInsts
3322292SN/A        .name(name() + ".committedInsts_total")
3332292SN/A        .desc("Number of Instructions Simulated");
3342292SN/A
3352292SN/A    cpi
3362292SN/A        .name(name() + ".cpi")
3372292SN/A        .desc("CPI: Cycles Per Instruction")
3382292SN/A        .precision(6);
3392292SN/A    cpi = simTicks / committedInsts;
3402292SN/A
3412292SN/A    totalCpi
3422292SN/A        .name(name() + ".cpi_total")
3432292SN/A        .desc("CPI: Total CPI of All Threads")
3442292SN/A        .precision(6);
3452292SN/A    totalCpi = simTicks / totalCommittedInsts;
3462292SN/A
3472292SN/A    ipc
3482292SN/A        .name(name() + ".ipc")
3492292SN/A        .desc("IPC: Instructions Per Cycle")
3502292SN/A        .precision(6);
3512292SN/A    ipc =  committedInsts / simTicks;
3522292SN/A
3532292SN/A    totalIpc
3542292SN/A        .name(name() + ".ipc_total")
3552292SN/A        .desc("IPC: Total IPC of All Threads")
3562292SN/A        .precision(6);
3572292SN/A    totalIpc =  totalCommittedInsts / simTicks;
3582292SN/A
3591062SN/A}
3601062SN/A
3611062SN/Atemplate <class Impl>
3622871Sktlim@umich.eduPort *
3632871Sktlim@umich.eduFullO3CPU<Impl>::getPort(const std::string &if_name, int idx)
3642871Sktlim@umich.edu{
3652871Sktlim@umich.edu    if (if_name == "dcache_port")
3662871Sktlim@umich.edu        return iew.getDcachePort();
3672871Sktlim@umich.edu    else if (if_name == "icache_port")
3682871Sktlim@umich.edu        return fetch.getIcachePort();
3692871Sktlim@umich.edu    else
3702871Sktlim@umich.edu        panic("No Such Port\n");
3712871Sktlim@umich.edu}
3722871Sktlim@umich.edu
3732871Sktlim@umich.edutemplate <class Impl>
3741062SN/Avoid
3751755SN/AFullO3CPU<Impl>::tick()
3761060SN/A{
3772733Sktlim@umich.edu    DPRINTF(O3CPU, "\n\nFullO3CPU: Ticking main, FullO3CPU.\n");
3781060SN/A
3792292SN/A    ++numCycles;
3802292SN/A
3812325SN/A//    activity = false;
3822292SN/A
3832292SN/A    //Tick each of the stages
3841060SN/A    fetch.tick();
3851060SN/A
3861060SN/A    decode.tick();
3871060SN/A
3881060SN/A    rename.tick();
3891060SN/A
3901060SN/A    iew.tick();
3911060SN/A
3921060SN/A    commit.tick();
3931060SN/A
3942292SN/A#if !FULL_SYSTEM
3952292SN/A    doContextSwitch();
3962292SN/A#endif
3972292SN/A
3982292SN/A    // Now advance the time buffers
3991060SN/A    timeBuffer.advance();
4001060SN/A
4011060SN/A    fetchQueue.advance();
4021060SN/A    decodeQueue.advance();
4031060SN/A    renameQueue.advance();
4041060SN/A    iewQueue.advance();
4051060SN/A
4062325SN/A    activityRec.advance();
4072292SN/A
4082292SN/A    if (removeInstsThisCycle) {
4092292SN/A        cleanUpRemovedInsts();
4102292SN/A    }
4112292SN/A
4122325SN/A    if (!tickEvent.scheduled()) {
4132867Sktlim@umich.edu        if (_status == SwitchedOut ||
4142867Sktlim@umich.edu            getState() == SimObject::DrainedTiming) {
4152325SN/A            // increment stat
4162325SN/A            lastRunningCycle = curTick;
4172325SN/A        } else if (!activityRec.active()) {
4182325SN/A            lastRunningCycle = curTick;
4192325SN/A            timesIdled++;
4202325SN/A        } else {
4212325SN/A            tickEvent.schedule(curTick + cycles(1));
4222325SN/A        }
4232292SN/A    }
4242292SN/A
4252292SN/A#if !FULL_SYSTEM
4262292SN/A    updateThreadPriority();
4272292SN/A#endif
4282292SN/A
4291060SN/A}
4301060SN/A
4311060SN/Atemplate <class Impl>
4321060SN/Avoid
4331755SN/AFullO3CPU<Impl>::init()
4341060SN/A{
4352307SN/A    if (!deferRegistration) {
4362680Sktlim@umich.edu        registerThreadContexts();
4372292SN/A    }
4381060SN/A
4392292SN/A    // Set inSyscall so that the CPU doesn't squash when initially
4402292SN/A    // setting up registers.
4412292SN/A    for (int i = 0; i < number_of_threads; ++i)
4422292SN/A        thread[i]->inSyscall = true;
4432292SN/A
4442292SN/A    for (int tid=0; tid < number_of_threads; tid++) {
4451858SN/A#if FULL_SYSTEM
4462680Sktlim@umich.edu        ThreadContext *src_tc = threadContexts[tid];
4471681SN/A#else
4482680Sktlim@umich.edu        ThreadContext *src_tc = thread[tid]->getTC();
4491681SN/A#endif
4502292SN/A        // Threads start in the Suspended State
4512680Sktlim@umich.edu        if (src_tc->status() != ThreadContext::Suspended) {
4522292SN/A            continue;
4531060SN/A        }
4541060SN/A
4552292SN/A#if FULL_SYSTEM
4562680Sktlim@umich.edu        TheISA::initCPU(src_tc, src_tc->readCpuId());
4572292SN/A#endif
4582292SN/A    }
4592292SN/A
4602292SN/A    // Clear inSyscall.
4612292SN/A    for (int i = 0; i < number_of_threads; ++i)
4622292SN/A        thread[i]->inSyscall = false;
4632292SN/A
4642316SN/A    // Initialize stages.
4652292SN/A    fetch.initStage();
4662292SN/A    iew.initStage();
4672292SN/A    rename.initStage();
4682292SN/A    commit.initStage();
4692292SN/A
4702292SN/A    commit.setThreads(thread);
4712292SN/A}
4722292SN/A
4732292SN/Atemplate <class Impl>
4742292SN/Avoid
4752292SN/AFullO3CPU<Impl>::insertThread(unsigned tid)
4762292SN/A{
4772847Sksewell@umich.edu    DPRINTF(O3CPU,"[tid:%i] Initializing thread into CPU");
4782292SN/A    // Will change now that the PC and thread state is internal to the CPU
4792683Sktlim@umich.edu    // and not in the ThreadContext.
4802292SN/A#if FULL_SYSTEM
4812680Sktlim@umich.edu    ThreadContext *src_tc = system->threadContexts[tid];
4822292SN/A#else
4832847Sksewell@umich.edu    ThreadContext *src_tc = tcBase(tid);
4842292SN/A#endif
4852292SN/A
4862292SN/A    //Bind Int Regs to Rename Map
4872292SN/A    for (int ireg = 0; ireg < TheISA::NumIntRegs; ireg++) {
4882292SN/A        PhysRegIndex phys_reg = freeList.getIntReg();
4892292SN/A
4902292SN/A        renameMap[tid].setEntry(ireg,phys_reg);
4912292SN/A        scoreboard.setReg(phys_reg);
4922292SN/A    }
4932292SN/A
4942292SN/A    //Bind Float Regs to Rename Map
4952292SN/A    for (int freg = 0; freg < TheISA::NumFloatRegs; freg++) {
4962292SN/A        PhysRegIndex phys_reg = freeList.getFloatReg();
4972292SN/A
4982292SN/A        renameMap[tid].setEntry(freg,phys_reg);
4992292SN/A        scoreboard.setReg(phys_reg);
5002292SN/A    }
5012292SN/A
5022292SN/A    //Copy Thread Data Into RegFile
5032847Sksewell@umich.edu    //this->copyFromTC(tid);
5042292SN/A
5052847Sksewell@umich.edu    //Set PC/NPC/NNPC
5062847Sksewell@umich.edu    setPC(src_tc->readPC(), tid);
5072847Sksewell@umich.edu    setNextPC(src_tc->readNextPC(), tid);
5082847Sksewell@umich.edu#if THE_ISA != ALPHA_ISA
5092847Sksewell@umich.edu    setNextNPC(src_tc->readNextNPC(), tid);
5102847Sksewell@umich.edu#endif
5112292SN/A
5122680Sktlim@umich.edu    src_tc->setStatus(ThreadContext::Active);
5132292SN/A
5142292SN/A    activateContext(tid,1);
5152292SN/A
5162292SN/A    //Reset ROB/IQ/LSQ Entries
5172292SN/A    commit.rob->resetEntries();
5182292SN/A    iew.resetEntries();
5192292SN/A}
5202292SN/A
5212292SN/Atemplate <class Impl>
5222292SN/Avoid
5232292SN/AFullO3CPU<Impl>::removeThread(unsigned tid)
5242292SN/A{
5252847Sksewell@umich.edu    DPRINTF(O3CPU,"[tid:%i] Removing thread from CPU.");
5262847Sksewell@umich.edu
5272847Sksewell@umich.edu    // Copy Thread Data From RegFile
5282847Sksewell@umich.edu    // If thread is suspended, it might be re-allocated
5292847Sksewell@umich.edu    //this->copyToTC(tid);
5302847Sksewell@umich.edu
5312847Sksewell@umich.edu    // Unbind Int Regs from Rename Map
5322292SN/A    for (int ireg = 0; ireg < TheISA::NumIntRegs; ireg++) {
5332292SN/A        PhysRegIndex phys_reg = renameMap[tid].lookup(ireg);
5342292SN/A
5352292SN/A        scoreboard.unsetReg(phys_reg);
5362292SN/A        freeList.addReg(phys_reg);
5372292SN/A    }
5382292SN/A
5392847Sksewell@umich.edu    // Unbind Float Regs from Rename Map
5402292SN/A    for (int freg = 0; freg < TheISA::NumFloatRegs; freg++) {
5412292SN/A        PhysRegIndex phys_reg = renameMap[tid].lookup(freg);
5422292SN/A
5432292SN/A        scoreboard.unsetReg(phys_reg);
5442292SN/A        freeList.addReg(phys_reg);
5452292SN/A    }
5462292SN/A
5472847Sksewell@umich.edu    // Squash Throughout Pipeline
5482292SN/A    fetch.squash(0,tid);
5492292SN/A    decode.squash(tid);
5502292SN/A    rename.squash(tid);
5512292SN/A
5522292SN/A    assert(iew.ldstQueue.getCount(tid) == 0);
5532292SN/A
5542847Sksewell@umich.edu    // Reset ROB/IQ/LSQ Entries
5552292SN/A    if (activeThreads.size() >= 1) {
5562292SN/A        commit.rob->resetEntries();
5572292SN/A        iew.resetEntries();
5582292SN/A    }
5592292SN/A}
5602292SN/A
5612292SN/A
5622292SN/Atemplate <class Impl>
5632292SN/Avoid
5642292SN/AFullO3CPU<Impl>::activateWhenReady(int tid)
5652292SN/A{
5662733Sktlim@umich.edu    DPRINTF(O3CPU,"[tid:%i]: Checking if resources are available for incoming"
5672292SN/A            "(e.g. PhysRegs/ROB/IQ/LSQ) \n",
5682292SN/A            tid);
5692292SN/A
5702292SN/A    bool ready = true;
5712292SN/A
5722292SN/A    if (freeList.numFreeIntRegs() >= TheISA::NumIntRegs) {
5732733Sktlim@umich.edu        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
5742292SN/A                "Phys. Int. Regs.\n",
5752292SN/A                tid);
5762292SN/A        ready = false;
5772292SN/A    } else if (freeList.numFreeFloatRegs() >= TheISA::NumFloatRegs) {
5782733Sktlim@umich.edu        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
5792292SN/A                "Phys. Float. Regs.\n",
5802292SN/A                tid);
5812292SN/A        ready = false;
5822292SN/A    } else if (commit.rob->numFreeEntries() >=
5832292SN/A               commit.rob->entryAmount(activeThreads.size() + 1)) {
5842733Sktlim@umich.edu        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
5852292SN/A                "ROB entries.\n",
5862292SN/A                tid);
5872292SN/A        ready = false;
5882292SN/A    } else if (iew.instQueue.numFreeEntries() >=
5892292SN/A               iew.instQueue.entryAmount(activeThreads.size() + 1)) {
5902733Sktlim@umich.edu        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
5912292SN/A                "IQ entries.\n",
5922292SN/A                tid);
5932292SN/A        ready = false;
5942292SN/A    } else if (iew.ldstQueue.numFreeEntries() >=
5952292SN/A               iew.ldstQueue.entryAmount(activeThreads.size() + 1)) {
5962733Sktlim@umich.edu        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
5972292SN/A                "LSQ entries.\n",
5982292SN/A                tid);
5992292SN/A        ready = false;
6002292SN/A    }
6012292SN/A
6022292SN/A    if (ready) {
6032292SN/A        insertThread(tid);
6042292SN/A
6052292SN/A        contextSwitch = false;
6062292SN/A
6072292SN/A        cpuWaitList.remove(tid);
6082292SN/A    } else {
6092292SN/A        suspendContext(tid);
6102292SN/A
6112292SN/A        //blocks fetch
6122292SN/A        contextSwitch = true;
6132292SN/A
6142292SN/A        //do waitlist
6152292SN/A        cpuWaitList.push_back(tid);
6161060SN/A    }
6171060SN/A}
6181060SN/A
6191060SN/Atemplate <class Impl>
6201060SN/Avoid
6212829Sksewell@umich.eduFullO3CPU<Impl>::activateThread(unsigned int tid)
6221060SN/A{
6232292SN/A    list<unsigned>::iterator isActive = find(
6242292SN/A        activeThreads.begin(), activeThreads.end(), tid);
6252292SN/A
6262292SN/A    if (isActive == activeThreads.end()) {
6272829Sksewell@umich.edu        DPRINTF(O3CPU, "[tid:%i]: Adding to active threads list\n",
6282292SN/A                tid);
6292292SN/A
6302292SN/A        activeThreads.push_back(tid);
6312292SN/A    }
6322829Sksewell@umich.edu}
6332292SN/A
6341060SN/A
6352829Sksewell@umich.edutemplate <class Impl>
6362829Sksewell@umich.eduvoid
6372829Sksewell@umich.eduFullO3CPU<Impl>::activateContext(int tid, int delay)
6382829Sksewell@umich.edu{
6392829Sksewell@umich.edu    // Needs to set each stage to running as well.
6402829Sksewell@umich.edu    if (delay){
6412829Sksewell@umich.edu        DPRINTF(O3CPU, "[tid:%i]: Scheduling thread context to activate "
6422829Sksewell@umich.edu                "on cycle %d\n", tid, curTick + cycles(delay));
6432829Sksewell@umich.edu        scheduleActivateThreadEvent(tid, delay);
6442829Sksewell@umich.edu    } else {
6452829Sksewell@umich.edu        activateThread(tid);
6462829Sksewell@umich.edu    }
6471060SN/A
6482829Sksewell@umich.edu    if(lastActivatedCycle < curTick) {
6492829Sksewell@umich.edu        scheduleTickEvent(delay);
6502292SN/A
6512829Sksewell@umich.edu        // Be sure to signal that there's some activity so the CPU doesn't
6522829Sksewell@umich.edu        // deschedule itself.
6532829Sksewell@umich.edu        activityRec.activity();
6542829Sksewell@umich.edu        fetch.wakeFromQuiesce();
6552829Sksewell@umich.edu
6562829Sksewell@umich.edu        lastActivatedCycle = curTick;
6572829Sksewell@umich.edu
6582829Sksewell@umich.edu        _status = Running;
6592829Sksewell@umich.edu    }
6601060SN/A}
6611060SN/A
6621060SN/Atemplate <class Impl>
6631060SN/Avoid
6642292SN/AFullO3CPU<Impl>::suspendContext(int tid)
6651060SN/A{
6662847Sksewell@umich.edu    DPRINTF(O3CPU,"[tid: %i]: Suspending Thread Context.\n", tid);
6672292SN/A    unscheduleTickEvent();
6682292SN/A    _status = Idle;
6692292SN/A/*
6702292SN/A    //Remove From Active List, if Active
6712292SN/A    list<unsigned>::iterator isActive = find(
6722292SN/A        activeThreads.begin(), activeThreads.end(), tid);
6732292SN/A
6742292SN/A    if (isActive != activeThreads.end()) {
6752733Sktlim@umich.edu        DPRINTF(O3CPU,"[tid:%i]: Removing from active threads list\n",
6762292SN/A                tid);
6772292SN/A        activeThreads.erase(isActive);
6782292SN/A    }
6792292SN/A*/
6801060SN/A}
6811060SN/A
6821060SN/Atemplate <class Impl>
6831060SN/Avoid
6842292SN/AFullO3CPU<Impl>::deallocateContext(int tid)
6851060SN/A{
6862847Sksewell@umich.edu    DPRINTF(O3CPU,"[tid:%i]: Deallocating Thread Context", tid);
6872847Sksewell@umich.edu
6882292SN/A    //Remove From Active List, if Active
6892847Sksewell@umich.edu    list<unsigned>::iterator thread_it =
6902847Sksewell@umich.edu        find(activeThreads.begin(), activeThreads.end(), tid);
6912292SN/A
6922847Sksewell@umich.edu    if (thread_it != activeThreads.end()) {
6932733Sktlim@umich.edu        DPRINTF(O3CPU,"[tid:%i]: Removing from active threads list\n",
6942292SN/A                tid);
6952847Sksewell@umich.edu        activeThreads.erase(thread_it);
6962292SN/A
6972292SN/A        removeThread(tid);
6982292SN/A    }
6991060SN/A}
7001060SN/A
7011060SN/Atemplate <class Impl>
7021060SN/Avoid
7032292SN/AFullO3CPU<Impl>::haltContext(int tid)
7041060SN/A{
7052847Sksewell@umich.edu    DPRINTF(O3CPU,"[tid:%i]: Halting Thread Context", tid);
7062292SN/A/*
7072292SN/A    //Remove From Active List, if Active
7082292SN/A    list<unsigned>::iterator isActive = find(
7092292SN/A        activeThreads.begin(), activeThreads.end(), tid);
7102292SN/A
7112292SN/A    if (isActive != activeThreads.end()) {
7122733Sktlim@umich.edu        DPRINTF(O3CPU,"[tid:%i]: Removing from active threads list\n",
7132292SN/A                tid);
7142292SN/A        activeThreads.erase(isActive);
7152292SN/A
7162292SN/A        removeThread(tid);
7172292SN/A    }
7182292SN/A*/
7191060SN/A}
7201060SN/A
7211060SN/Atemplate <class Impl>
7222864Sktlim@umich.eduvoid
7232864Sktlim@umich.eduFullO3CPU<Impl>::serialize(std::ostream &os)
7242864Sktlim@umich.edu{
7252864Sktlim@umich.edu    SERIALIZE_ENUM(_status);
7262864Sktlim@umich.edu    BaseCPU::serialize(os);
7272864Sktlim@umich.edu    nameOut(os, csprintf("%s.tickEvent", name()));
7282864Sktlim@umich.edu    tickEvent.serialize(os);
7292864Sktlim@umich.edu
7302864Sktlim@umich.edu    // Use SimpleThread's ability to checkpoint to make it easier to
7312864Sktlim@umich.edu    // write out the registers.  Also make this static so it doesn't
7322864Sktlim@umich.edu    // get instantiated multiple times (causes a panic in statistics).
7332864Sktlim@umich.edu    static SimpleThread temp;
7342864Sktlim@umich.edu
7352864Sktlim@umich.edu    for (int i = 0; i < thread.size(); i++) {
7362864Sktlim@umich.edu        nameOut(os, csprintf("%s.xc.%i", name(), i));
7372864Sktlim@umich.edu        temp.copyTC(thread[i]->getTC());
7382864Sktlim@umich.edu        temp.serialize(os);
7392864Sktlim@umich.edu    }
7402864Sktlim@umich.edu}
7412864Sktlim@umich.edu
7422864Sktlim@umich.edutemplate <class Impl>
7432864Sktlim@umich.eduvoid
7442864Sktlim@umich.eduFullO3CPU<Impl>::unserialize(Checkpoint *cp, const std::string &section)
7452864Sktlim@umich.edu{
7462864Sktlim@umich.edu    UNSERIALIZE_ENUM(_status);
7472864Sktlim@umich.edu    BaseCPU::unserialize(cp, section);
7482864Sktlim@umich.edu    tickEvent.unserialize(cp, csprintf("%s.tickEvent", section));
7492864Sktlim@umich.edu
7502864Sktlim@umich.edu    // Use SimpleThread's ability to checkpoint to make it easier to
7512864Sktlim@umich.edu    // read in the registers.  Also make this static so it doesn't
7522864Sktlim@umich.edu    // get instantiated multiple times (causes a panic in statistics).
7532864Sktlim@umich.edu    static SimpleThread temp;
7542864Sktlim@umich.edu
7552864Sktlim@umich.edu    for (int i = 0; i < thread.size(); i++) {
7562864Sktlim@umich.edu        temp.copyTC(thread[i]->getTC());
7572864Sktlim@umich.edu        temp.unserialize(cp, csprintf("%s.xc.%i", section, i));
7582864Sktlim@umich.edu        thread[i]->getTC()->copyArchRegs(temp.getTC());
7592864Sktlim@umich.edu    }
7602864Sktlim@umich.edu}
7612864Sktlim@umich.edu
7622864Sktlim@umich.edutemplate <class Impl>
7632843Sktlim@umich.edubool
7642843Sktlim@umich.eduFullO3CPU<Impl>::drain(Event *drain_event)
7651060SN/A{
7662843Sktlim@umich.edu    drainCount = 0;
7672843Sktlim@umich.edu    fetch.drain();
7682843Sktlim@umich.edu    decode.drain();
7692843Sktlim@umich.edu    rename.drain();
7702843Sktlim@umich.edu    iew.drain();
7712843Sktlim@umich.edu    commit.drain();
7722325SN/A
7732325SN/A    // Wake the CPU and record activity so everything can drain out if
7742863Sktlim@umich.edu    // the CPU was not able to immediately drain.
7752864Sktlim@umich.edu    if (getState() != SimObject::DrainedTiming) {
7762864Sktlim@umich.edu        // A bit of a hack...set the drainEvent after all the drain()
7772864Sktlim@umich.edu        // calls have been made, that way if all of the stages drain
7782864Sktlim@umich.edu        // immediately, the signalDrained() function knows not to call
7792864Sktlim@umich.edu        // process on the drain event.
7802864Sktlim@umich.edu        drainEvent = drain_event;
7812864Sktlim@umich.edu
7822863Sktlim@umich.edu        wakeCPU();
7832863Sktlim@umich.edu        activityRec.activity();
7842843Sktlim@umich.edu
7852863Sktlim@umich.edu        return false;
7862863Sktlim@umich.edu    } else {
7872863Sktlim@umich.edu        return true;
7882863Sktlim@umich.edu    }
7892316SN/A}
7902310SN/A
7912316SN/Atemplate <class Impl>
7922316SN/Avoid
7932843Sktlim@umich.eduFullO3CPU<Impl>::resume()
7942316SN/A{
7952843Sktlim@umich.edu    fetch.resume();
7962843Sktlim@umich.edu    decode.resume();
7972843Sktlim@umich.edu    rename.resume();
7982843Sktlim@umich.edu    iew.resume();
7992843Sktlim@umich.edu    commit.resume();
8002316SN/A
8012864Sktlim@umich.edu    if (_status == SwitchedOut || _status == Idle)
8022864Sktlim@umich.edu        return;
8032864Sktlim@umich.edu
8042843Sktlim@umich.edu    if (!tickEvent.scheduled())
8052843Sktlim@umich.edu        tickEvent.schedule(curTick);
8062843Sktlim@umich.edu    _status = Running;
8072867Sktlim@umich.edu    changeState(SimObject::Timing);
8082843Sktlim@umich.edu}
8092316SN/A
8102843Sktlim@umich.edutemplate <class Impl>
8112843Sktlim@umich.eduvoid
8122843Sktlim@umich.eduFullO3CPU<Impl>::signalDrained()
8132843Sktlim@umich.edu{
8142843Sktlim@umich.edu    if (++drainCount == NumStages) {
8152316SN/A        if (tickEvent.scheduled())
8162316SN/A            tickEvent.squash();
8172863Sktlim@umich.edu
8182864Sktlim@umich.edu        changeState(SimObject::DrainedTiming);
8192863Sktlim@umich.edu
8202863Sktlim@umich.edu        if (drainEvent) {
8212863Sktlim@umich.edu            drainEvent->process();
8222863Sktlim@umich.edu            drainEvent = NULL;
8232863Sktlim@umich.edu        }
8242310SN/A    }
8252843Sktlim@umich.edu    assert(drainCount <= 5);
8262843Sktlim@umich.edu}
8272843Sktlim@umich.edu
8282843Sktlim@umich.edutemplate <class Impl>
8292843Sktlim@umich.eduvoid
8302843Sktlim@umich.eduFullO3CPU<Impl>::switchOut()
8312843Sktlim@umich.edu{
8322843Sktlim@umich.edu    fetch.switchOut();
8332843Sktlim@umich.edu    rename.switchOut();
8342843Sktlim@umich.edu    commit.switchOut();
8352843Sktlim@umich.edu    instList.clear();
8362843Sktlim@umich.edu    while (!removeList.empty()) {
8372843Sktlim@umich.edu        removeList.pop();
8382843Sktlim@umich.edu    }
8392843Sktlim@umich.edu
8402843Sktlim@umich.edu    _status = SwitchedOut;
8412843Sktlim@umich.edu#if USE_CHECKER
8422843Sktlim@umich.edu    if (checker)
8432843Sktlim@umich.edu        checker->switchOut();
8442843Sktlim@umich.edu#endif
8451060SN/A}
8461060SN/A
8471060SN/Atemplate <class Impl>
8481060SN/Avoid
8491755SN/AFullO3CPU<Impl>::takeOverFrom(BaseCPU *oldCPU)
8501060SN/A{
8512325SN/A    // Flush out any old data from the time buffers.
8522873Sktlim@umich.edu    for (int i = 0; i < timeBuffer.getSize(); ++i) {
8532307SN/A        timeBuffer.advance();
8542307SN/A        fetchQueue.advance();
8552307SN/A        decodeQueue.advance();
8562307SN/A        renameQueue.advance();
8572307SN/A        iewQueue.advance();
8582307SN/A    }
8592307SN/A
8602325SN/A    activityRec.reset();
8612307SN/A
8621060SN/A    BaseCPU::takeOverFrom(oldCPU);
8631060SN/A
8642307SN/A    fetch.takeOverFrom();
8652307SN/A    decode.takeOverFrom();
8662307SN/A    rename.takeOverFrom();
8672307SN/A    iew.takeOverFrom();
8682307SN/A    commit.takeOverFrom();
8692307SN/A
8701060SN/A    assert(!tickEvent.scheduled());
8711060SN/A
8722325SN/A    // @todo: Figure out how to properly select the tid to put onto
8732325SN/A    // the active threads list.
8742307SN/A    int tid = 0;
8752307SN/A
8762307SN/A    list<unsigned>::iterator isActive = find(
8772307SN/A        activeThreads.begin(), activeThreads.end(), tid);
8782307SN/A
8792307SN/A    if (isActive == activeThreads.end()) {
8802325SN/A        //May Need to Re-code this if the delay variable is the delay
8812325SN/A        //needed for thread to activate
8822733Sktlim@umich.edu        DPRINTF(O3CPU, "Adding Thread %i to active threads list\n",
8832307SN/A                tid);
8842307SN/A
8852307SN/A        activeThreads.push_back(tid);
8862307SN/A    }
8872307SN/A
8882325SN/A    // Set all statuses to active, schedule the CPU's tick event.
8892307SN/A    // @todo: Fix up statuses so this is handled properly
8902680Sktlim@umich.edu    for (int i = 0; i < threadContexts.size(); ++i) {
8912680Sktlim@umich.edu        ThreadContext *tc = threadContexts[i];
8922680Sktlim@umich.edu        if (tc->status() == ThreadContext::Active && _status != Running) {
8931681SN/A            _status = Running;
8941681SN/A            tickEvent.schedule(curTick);
8951681SN/A        }
8961060SN/A    }
8972307SN/A    if (!tickEvent.scheduled())
8982307SN/A        tickEvent.schedule(curTick);
8991060SN/A}
9001060SN/A
9011060SN/Atemplate <class Impl>
9021060SN/Auint64_t
9031755SN/AFullO3CPU<Impl>::readIntReg(int reg_idx)
9041060SN/A{
9051060SN/A    return regFile.readIntReg(reg_idx);
9061060SN/A}
9071060SN/A
9081060SN/Atemplate <class Impl>
9092455SN/AFloatReg
9102455SN/AFullO3CPU<Impl>::readFloatReg(int reg_idx, int width)
9111060SN/A{
9122455SN/A    return regFile.readFloatReg(reg_idx, width);
9131060SN/A}
9141060SN/A
9151060SN/Atemplate <class Impl>
9162455SN/AFloatReg
9172455SN/AFullO3CPU<Impl>::readFloatReg(int reg_idx)
9181060SN/A{
9192455SN/A    return regFile.readFloatReg(reg_idx);
9201060SN/A}
9211060SN/A
9221060SN/Atemplate <class Impl>
9232455SN/AFloatRegBits
9242455SN/AFullO3CPU<Impl>::readFloatRegBits(int reg_idx, int width)
9251060SN/A{
9262455SN/A    return regFile.readFloatRegBits(reg_idx, width);
9272455SN/A}
9282455SN/A
9292455SN/Atemplate <class Impl>
9302455SN/AFloatRegBits
9312455SN/AFullO3CPU<Impl>::readFloatRegBits(int reg_idx)
9322455SN/A{
9332455SN/A    return regFile.readFloatRegBits(reg_idx);
9341060SN/A}
9351060SN/A
9361060SN/Atemplate <class Impl>
9371060SN/Avoid
9381755SN/AFullO3CPU<Impl>::setIntReg(int reg_idx, uint64_t val)
9391060SN/A{
9401060SN/A    regFile.setIntReg(reg_idx, val);
9411060SN/A}
9421060SN/A
9431060SN/Atemplate <class Impl>
9441060SN/Avoid
9452455SN/AFullO3CPU<Impl>::setFloatReg(int reg_idx, FloatReg val, int width)
9461060SN/A{
9472455SN/A    regFile.setFloatReg(reg_idx, val, width);
9481060SN/A}
9491060SN/A
9501060SN/Atemplate <class Impl>
9511060SN/Avoid
9522455SN/AFullO3CPU<Impl>::setFloatReg(int reg_idx, FloatReg val)
9531060SN/A{
9542455SN/A    regFile.setFloatReg(reg_idx, val);
9551060SN/A}
9561060SN/A
9571060SN/Atemplate <class Impl>
9581060SN/Avoid
9592455SN/AFullO3CPU<Impl>::setFloatRegBits(int reg_idx, FloatRegBits val, int width)
9601060SN/A{
9612455SN/A    regFile.setFloatRegBits(reg_idx, val, width);
9622455SN/A}
9632455SN/A
9642455SN/Atemplate <class Impl>
9652455SN/Avoid
9662455SN/AFullO3CPU<Impl>::setFloatRegBits(int reg_idx, FloatRegBits val)
9672455SN/A{
9682455SN/A    regFile.setFloatRegBits(reg_idx, val);
9691060SN/A}
9701060SN/A
9711060SN/Atemplate <class Impl>
9721060SN/Auint64_t
9732292SN/AFullO3CPU<Impl>::readArchIntReg(int reg_idx, unsigned tid)
9741060SN/A{
9752292SN/A    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(reg_idx);
9762292SN/A
9772292SN/A    return regFile.readIntReg(phys_reg);
9782292SN/A}
9792292SN/A
9802292SN/Atemplate <class Impl>
9812292SN/Afloat
9822292SN/AFullO3CPU<Impl>::readArchFloatRegSingle(int reg_idx, unsigned tid)
9832292SN/A{
9842307SN/A    int idx = reg_idx + TheISA::FP_Base_DepTag;
9852307SN/A    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(idx);
9862292SN/A
9872669Sktlim@umich.edu    return regFile.readFloatReg(phys_reg);
9882292SN/A}
9892292SN/A
9902292SN/Atemplate <class Impl>
9912292SN/Adouble
9922292SN/AFullO3CPU<Impl>::readArchFloatRegDouble(int reg_idx, unsigned tid)
9932292SN/A{
9942307SN/A    int idx = reg_idx + TheISA::FP_Base_DepTag;
9952307SN/A    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(idx);
9962292SN/A
9972669Sktlim@umich.edu    return regFile.readFloatReg(phys_reg, 64);
9982292SN/A}
9992292SN/A
10002292SN/Atemplate <class Impl>
10012292SN/Auint64_t
10022292SN/AFullO3CPU<Impl>::readArchFloatRegInt(int reg_idx, unsigned tid)
10032292SN/A{
10042307SN/A    int idx = reg_idx + TheISA::FP_Base_DepTag;
10052307SN/A    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(idx);
10062292SN/A
10072669Sktlim@umich.edu    return regFile.readFloatRegBits(phys_reg);
10081060SN/A}
10091060SN/A
10101060SN/Atemplate <class Impl>
10111060SN/Avoid
10122292SN/AFullO3CPU<Impl>::setArchIntReg(int reg_idx, uint64_t val, unsigned tid)
10131060SN/A{
10142292SN/A    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(reg_idx);
10152292SN/A
10162292SN/A    regFile.setIntReg(phys_reg, val);
10171060SN/A}
10181060SN/A
10191060SN/Atemplate <class Impl>
10201060SN/Avoid
10212292SN/AFullO3CPU<Impl>::setArchFloatRegSingle(int reg_idx, float val, unsigned tid)
10221060SN/A{
10232292SN/A    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(reg_idx);
10242292SN/A
10252669Sktlim@umich.edu    regFile.setFloatReg(phys_reg, val);
10261060SN/A}
10271060SN/A
10281060SN/Atemplate <class Impl>
10291060SN/Avoid
10302292SN/AFullO3CPU<Impl>::setArchFloatRegDouble(int reg_idx, double val, unsigned tid)
10311060SN/A{
10322292SN/A    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(reg_idx);
10332292SN/A
10342669Sktlim@umich.edu    regFile.setFloatReg(phys_reg, val, 64);
10351060SN/A}
10361060SN/A
10371060SN/Atemplate <class Impl>
10381060SN/Avoid
10392292SN/AFullO3CPU<Impl>::setArchFloatRegInt(int reg_idx, uint64_t val, unsigned tid)
10401060SN/A{
10412292SN/A    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(reg_idx);
10421060SN/A
10432669Sktlim@umich.edu    regFile.setFloatRegBits(phys_reg, val);
10442292SN/A}
10452292SN/A
10462292SN/Atemplate <class Impl>
10472292SN/Auint64_t
10482292SN/AFullO3CPU<Impl>::readPC(unsigned tid)
10492292SN/A{
10502292SN/A    return commit.readPC(tid);
10511060SN/A}
10521060SN/A
10531060SN/Atemplate <class Impl>
10541060SN/Avoid
10552292SN/AFullO3CPU<Impl>::setPC(Addr new_PC,unsigned tid)
10561060SN/A{
10572292SN/A    commit.setPC(new_PC, tid);
10582292SN/A}
10591060SN/A
10602292SN/Atemplate <class Impl>
10612292SN/Auint64_t
10622292SN/AFullO3CPU<Impl>::readNextPC(unsigned tid)
10632292SN/A{
10642292SN/A    return commit.readNextPC(tid);
10652292SN/A}
10661060SN/A
10672292SN/Atemplate <class Impl>
10682292SN/Avoid
10692292SN/AFullO3CPU<Impl>::setNextPC(uint64_t val,unsigned tid)
10702292SN/A{
10712292SN/A    commit.setNextPC(val, tid);
10722292SN/A}
10731060SN/A
10742756Sksewell@umich.edu#if THE_ISA != ALPHA_ISA
10752756Sksewell@umich.edutemplate <class Impl>
10762756Sksewell@umich.eduuint64_t
10772756Sksewell@umich.eduFullO3CPU<Impl>::readNextNPC(unsigned tid)
10782756Sksewell@umich.edu{
10792756Sksewell@umich.edu    return commit.readNextNPC(tid);
10802756Sksewell@umich.edu}
10812756Sksewell@umich.edu
10822756Sksewell@umich.edutemplate <class Impl>
10832756Sksewell@umich.eduvoid
10842756Sksewell@umich.eduFullO3CPU<Impl>::setNextNNPC(uint64_t val,unsigned tid)
10852756Sksewell@umich.edu{
10862756Sksewell@umich.edu    commit.setNextNPC(val, tid);
10872756Sksewell@umich.edu}
10882756Sksewell@umich.edu#endif
10892756Sksewell@umich.edu
10902292SN/Atemplate <class Impl>
10912292SN/Atypename FullO3CPU<Impl>::ListIt
10922292SN/AFullO3CPU<Impl>::addInst(DynInstPtr &inst)
10932292SN/A{
10942292SN/A    instList.push_back(inst);
10951060SN/A
10962292SN/A    return --(instList.end());
10972292SN/A}
10981060SN/A
10992292SN/Atemplate <class Impl>
11002292SN/Avoid
11012292SN/AFullO3CPU<Impl>::instDone(unsigned tid)
11022292SN/A{
11032292SN/A    // Keep an instruction count.
11042292SN/A    thread[tid]->numInst++;
11052292SN/A    thread[tid]->numInsts++;
11062292SN/A    committedInsts[tid]++;
11072292SN/A    totalCommittedInsts++;
11082292SN/A
11092292SN/A    // Check for instruction-count-based events.
11102292SN/A    comInstEventQueue[tid]->serviceEvents(thread[tid]->numInst);
11112292SN/A}
11122292SN/A
11132292SN/Atemplate <class Impl>
11142292SN/Avoid
11152292SN/AFullO3CPU<Impl>::addToRemoveList(DynInstPtr &inst)
11162292SN/A{
11172292SN/A    removeInstsThisCycle = true;
11182292SN/A
11192292SN/A    removeList.push(inst->getInstListIt());
11201060SN/A}
11211060SN/A
11221060SN/Atemplate <class Impl>
11231060SN/Avoid
11241755SN/AFullO3CPU<Impl>::removeFrontInst(DynInstPtr &inst)
11251060SN/A{
11262733Sktlim@umich.edu    DPRINTF(O3CPU, "Removing committed instruction [tid:%i] PC %#x "
11272292SN/A            "[sn:%lli]\n",
11282303SN/A            inst->threadNumber, inst->readPC(), inst->seqNum);
11291060SN/A
11302292SN/A    removeInstsThisCycle = true;
11311060SN/A
11321060SN/A    // Remove the front instruction.
11332292SN/A    removeList.push(inst->getInstListIt());
11341060SN/A}
11351060SN/A
11361060SN/Atemplate <class Impl>
11371060SN/Avoid
11382292SN/AFullO3CPU<Impl>::removeInstsNotInROB(unsigned tid)
11391060SN/A{
11402733Sktlim@umich.edu    DPRINTF(O3CPU, "Thread %i: Deleting instructions from instruction"
11412292SN/A            " list.\n", tid);
11421060SN/A
11432292SN/A    ListIt end_it;
11441060SN/A
11452292SN/A    bool rob_empty = false;
11462292SN/A
11472292SN/A    if (instList.empty()) {
11482292SN/A        return;
11492292SN/A    } else if (rob.isEmpty(/*tid*/)) {
11502733Sktlim@umich.edu        DPRINTF(O3CPU, "ROB is empty, squashing all insts.\n");
11512292SN/A        end_it = instList.begin();
11522292SN/A        rob_empty = true;
11532292SN/A    } else {
11542292SN/A        end_it = (rob.readTailInst(tid))->getInstListIt();
11552733Sktlim@umich.edu        DPRINTF(O3CPU, "ROB is not empty, squashing insts not in ROB.\n");
11562292SN/A    }
11572292SN/A
11582292SN/A    removeInstsThisCycle = true;
11592292SN/A
11602292SN/A    ListIt inst_it = instList.end();
11612292SN/A
11622292SN/A    inst_it--;
11632292SN/A
11642292SN/A    // Walk through the instruction list, removing any instructions
11652292SN/A    // that were inserted after the given instruction iterator, end_it.
11662292SN/A    while (inst_it != end_it) {
11672292SN/A        assert(!instList.empty());
11682292SN/A
11692292SN/A        squashInstIt(inst_it, tid);
11702292SN/A
11712292SN/A        inst_it--;
11722292SN/A    }
11732292SN/A
11742292SN/A    // If the ROB was empty, then we actually need to remove the first
11752292SN/A    // instruction as well.
11762292SN/A    if (rob_empty) {
11772292SN/A        squashInstIt(inst_it, tid);
11782292SN/A    }
11791060SN/A}
11801060SN/A
11811060SN/Atemplate <class Impl>
11821060SN/Avoid
11832292SN/AFullO3CPU<Impl>::removeInstsUntil(const InstSeqNum &seq_num,
11842292SN/A                                  unsigned tid)
11851062SN/A{
11862292SN/A    assert(!instList.empty());
11872292SN/A
11882292SN/A    removeInstsThisCycle = true;
11892292SN/A
11902292SN/A    ListIt inst_iter = instList.end();
11912292SN/A
11922292SN/A    inst_iter--;
11932292SN/A
11942733Sktlim@umich.edu    DPRINTF(O3CPU, "Deleting instructions from instruction "
11952292SN/A            "list that are from [tid:%i] and above [sn:%lli] (end=%lli).\n",
11962292SN/A            tid, seq_num, (*inst_iter)->seqNum);
11971062SN/A
11982292SN/A    while ((*inst_iter)->seqNum > seq_num) {
11991062SN/A
12002292SN/A        bool break_loop = (inst_iter == instList.begin());
12011062SN/A
12022292SN/A        squashInstIt(inst_iter, tid);
12031062SN/A
12042292SN/A        inst_iter--;
12051062SN/A
12062292SN/A        if (break_loop)
12072292SN/A            break;
12082292SN/A    }
12092292SN/A}
12102292SN/A
12112292SN/Atemplate <class Impl>
12122292SN/Ainline void
12132292SN/AFullO3CPU<Impl>::squashInstIt(const ListIt &instIt, const unsigned &tid)
12142292SN/A{
12152292SN/A    if ((*instIt)->threadNumber == tid) {
12162733Sktlim@umich.edu        DPRINTF(O3CPU, "Squashing instruction, "
12172292SN/A                "[tid:%i] [sn:%lli] PC %#x\n",
12182292SN/A                (*instIt)->threadNumber,
12192292SN/A                (*instIt)->seqNum,
12202292SN/A                (*instIt)->readPC());
12211062SN/A
12221062SN/A        // Mark it as squashed.
12232292SN/A        (*instIt)->setSquashed();
12242292SN/A
12252325SN/A        // @todo: Formulate a consistent method for deleting
12262325SN/A        // instructions from the instruction list
12272292SN/A        // Remove the instruction from the list.
12282292SN/A        removeList.push(instIt);
12292292SN/A    }
12302292SN/A}
12312292SN/A
12322292SN/Atemplate <class Impl>
12332292SN/Avoid
12342292SN/AFullO3CPU<Impl>::cleanUpRemovedInsts()
12352292SN/A{
12362292SN/A    while (!removeList.empty()) {
12372733Sktlim@umich.edu        DPRINTF(O3CPU, "Removing instruction, "
12382292SN/A                "[tid:%i] [sn:%lli] PC %#x\n",
12392292SN/A                (*removeList.front())->threadNumber,
12402292SN/A                (*removeList.front())->seqNum,
12412292SN/A                (*removeList.front())->readPC());
12422292SN/A
12432292SN/A        instList.erase(removeList.front());
12442292SN/A
12452292SN/A        removeList.pop();
12461062SN/A    }
12471062SN/A
12482292SN/A    removeInstsThisCycle = false;
12491062SN/A}
12502325SN/A/*
12511062SN/Atemplate <class Impl>
12521062SN/Avoid
12531755SN/AFullO3CPU<Impl>::removeAllInsts()
12541060SN/A{
12551060SN/A    instList.clear();
12561060SN/A}
12572325SN/A*/
12581060SN/Atemplate <class Impl>
12591060SN/Avoid
12601755SN/AFullO3CPU<Impl>::dumpInsts()
12611060SN/A{
12621060SN/A    int num = 0;
12631060SN/A
12642292SN/A    ListIt inst_list_it = instList.begin();
12652292SN/A
12662292SN/A    cprintf("Dumping Instruction List\n");
12672292SN/A
12682292SN/A    while (inst_list_it != instList.end()) {
12692292SN/A        cprintf("Instruction:%i\nPC:%#x\n[tid:%i]\n[sn:%lli]\nIssued:%i\n"
12702292SN/A                "Squashed:%i\n\n",
12712292SN/A                num, (*inst_list_it)->readPC(), (*inst_list_it)->threadNumber,
12722292SN/A                (*inst_list_it)->seqNum, (*inst_list_it)->isIssued(),
12732292SN/A                (*inst_list_it)->isSquashed());
12741060SN/A        inst_list_it++;
12751060SN/A        ++num;
12761060SN/A    }
12771060SN/A}
12782325SN/A/*
12791060SN/Atemplate <class Impl>
12801060SN/Avoid
12811755SN/AFullO3CPU<Impl>::wakeDependents(DynInstPtr &inst)
12821060SN/A{
12831060SN/A    iew.wakeDependents(inst);
12841060SN/A}
12852325SN/A*/
12862292SN/Atemplate <class Impl>
12872292SN/Avoid
12882292SN/AFullO3CPU<Impl>::wakeCPU()
12892292SN/A{
12902325SN/A    if (activityRec.active() || tickEvent.scheduled()) {
12912325SN/A        DPRINTF(Activity, "CPU already running.\n");
12922292SN/A        return;
12932292SN/A    }
12942292SN/A
12952325SN/A    DPRINTF(Activity, "Waking up CPU\n");
12962325SN/A
12972325SN/A    idleCycles += (curTick - 1) - lastRunningCycle;
12982292SN/A
12992292SN/A    tickEvent.schedule(curTick);
13002292SN/A}
13012292SN/A
13022292SN/Atemplate <class Impl>
13032292SN/Aint
13042292SN/AFullO3CPU<Impl>::getFreeTid()
13052292SN/A{
13062292SN/A    for (int i=0; i < numThreads; i++) {
13072292SN/A        if (!tids[i]) {
13082292SN/A            tids[i] = true;
13092292SN/A            return i;
13102292SN/A        }
13112292SN/A    }
13122292SN/A
13132292SN/A    return -1;
13142292SN/A}
13152292SN/A
13162292SN/Atemplate <class Impl>
13172292SN/Avoid
13182292SN/AFullO3CPU<Impl>::doContextSwitch()
13192292SN/A{
13202292SN/A    if (contextSwitch) {
13212292SN/A
13222292SN/A        //ADD CODE TO DEACTIVE THREAD HERE (???)
13232292SN/A
13242292SN/A        for (int tid=0; tid < cpuWaitList.size(); tid++) {
13252292SN/A            activateWhenReady(tid);
13262292SN/A        }
13272292SN/A
13282292SN/A        if (cpuWaitList.size() == 0)
13292292SN/A            contextSwitch = true;
13302292SN/A    }
13312292SN/A}
13322292SN/A
13332292SN/Atemplate <class Impl>
13342292SN/Avoid
13352292SN/AFullO3CPU<Impl>::updateThreadPriority()
13362292SN/A{
13372292SN/A    if (activeThreads.size() > 1)
13382292SN/A    {
13392292SN/A        //DEFAULT TO ROUND ROBIN SCHEME
13402292SN/A        //e.g. Move highest priority to end of thread list
13412292SN/A        list<unsigned>::iterator list_begin = activeThreads.begin();
13422292SN/A        list<unsigned>::iterator list_end   = activeThreads.end();
13432292SN/A
13442292SN/A        unsigned high_thread = *list_begin;
13452292SN/A
13462292SN/A        activeThreads.erase(list_begin);
13472292SN/A
13482292SN/A        activeThreads.push_back(high_thread);
13492292SN/A    }
13502292SN/A}
13511060SN/A
13521755SN/A// Forward declaration of FullO3CPU.
13532818Sksewell@umich.edutemplate class FullO3CPU<O3CPUImpl>;
1354