cpu.cc revision 2910
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>
1182875Sksewell@umich.eduFullO3CPU<Impl>::DeallocateContextEvent::DeallocateContextEvent()
1192875Sksewell@umich.edu    : Event(&mainEventQueue, CPU_Tick_Pri)
1202875Sksewell@umich.edu{
1212875Sksewell@umich.edu}
1222875Sksewell@umich.edu
1232875Sksewell@umich.edutemplate <class Impl>
1242875Sksewell@umich.eduvoid
1252875Sksewell@umich.eduFullO3CPU<Impl>::DeallocateContextEvent::init(int thread_num,
1262875Sksewell@umich.edu                                           FullO3CPU<Impl> *thread_cpu)
1272875Sksewell@umich.edu{
1282875Sksewell@umich.edu    tid = thread_num;
1292875Sksewell@umich.edu    cpu = thread_cpu;
1302875Sksewell@umich.edu}
1312875Sksewell@umich.edu
1322875Sksewell@umich.edutemplate <class Impl>
1332875Sksewell@umich.eduvoid
1342875Sksewell@umich.eduFullO3CPU<Impl>::DeallocateContextEvent::process()
1352875Sksewell@umich.edu{
1362875Sksewell@umich.edu    cpu->deactivateThread(tid);
1372875Sksewell@umich.edu    cpu->removeThread(tid);
1382875Sksewell@umich.edu}
1392875Sksewell@umich.edu
1402875Sksewell@umich.edutemplate <class Impl>
1412875Sksewell@umich.educonst char *
1422875Sksewell@umich.eduFullO3CPU<Impl>::DeallocateContextEvent::description()
1432875Sksewell@umich.edu{
1442875Sksewell@umich.edu    return "FullO3CPU \"Deallocate Context\" event";
1452875Sksewell@umich.edu}
1462875Sksewell@umich.edu
1472875Sksewell@umich.edutemplate <class Impl>
1482292SN/AFullO3CPU<Impl>::FullO3CPU(Params *params)
1492733Sktlim@umich.edu    : BaseO3CPU(params),
1501060SN/A      tickEvent(this),
1512292SN/A      removeInstsThisCycle(false),
1521060SN/A      fetch(params),
1531060SN/A      decode(params),
1541060SN/A      rename(params),
1551060SN/A      iew(params),
1561060SN/A      commit(params),
1571060SN/A
1582292SN/A      regFile(params->numPhysIntRegs, params->numPhysFloatRegs),
1591060SN/A
1602831Sksewell@umich.edu      freeList(params->numberOfThreads,
1612292SN/A               TheISA::NumIntRegs, params->numPhysIntRegs,
1622292SN/A               TheISA::NumFloatRegs, params->numPhysFloatRegs),
1631060SN/A
1642292SN/A      rob(params->numROBEntries, params->squashWidth,
1652292SN/A          params->smtROBPolicy, params->smtROBThreshold,
1662292SN/A          params->numberOfThreads),
1671060SN/A
1682831Sksewell@umich.edu      scoreboard(params->numberOfThreads,
1692292SN/A                 TheISA::NumIntRegs, params->numPhysIntRegs,
1702292SN/A                 TheISA::NumFloatRegs, params->numPhysFloatRegs,
1712292SN/A                 TheISA::NumMiscRegs * number_of_threads,
1722292SN/A                 TheISA::ZeroReg),
1731060SN/A
1742873Sktlim@umich.edu      timeBuffer(params->backComSize, params->forwardComSize),
1752873Sktlim@umich.edu      fetchQueue(params->backComSize, params->forwardComSize),
1762873Sktlim@umich.edu      decodeQueue(params->backComSize, params->forwardComSize),
1772873Sktlim@umich.edu      renameQueue(params->backComSize, params->forwardComSize),
1782873Sktlim@umich.edu      iewQueue(params->backComSize, params->forwardComSize),
1792873Sktlim@umich.edu      activityRec(NumStages,
1802873Sktlim@umich.edu                  params->backComSize + params->forwardComSize,
1812873Sktlim@umich.edu                  params->activity),
1821060SN/A
1831060SN/A      globalSeqNum(1),
1841060SN/A
1851858SN/A#if FULL_SYSTEM
1862292SN/A      system(params->system),
1871060SN/A      physmem(system->physmem),
1881060SN/A#endif // FULL_SYSTEM
1892292SN/A      mem(params->mem),
1902843Sktlim@umich.edu      drainCount(0),
1912316SN/A      deferRegistration(params->deferRegistration),
1922316SN/A      numThreads(number_of_threads)
1931060SN/A{
1941060SN/A    _status = Idle;
1951681SN/A
1962733Sktlim@umich.edu    checker = NULL;
1972733Sktlim@umich.edu
1982794Sktlim@umich.edu    if (params->checker) {
1992733Sktlim@umich.edu#if USE_CHECKER
2002316SN/A        BaseCPU *temp_checker = params->checker;
2012316SN/A        checker = dynamic_cast<Checker<DynInstPtr> *>(temp_checker);
2022316SN/A        checker->setMemory(mem);
2032316SN/A#if FULL_SYSTEM
2042316SN/A        checker->setSystem(params->system);
2052316SN/A#endif
2062794Sktlim@umich.edu#else
2072794Sktlim@umich.edu        panic("Checker enabled but not compiled in!");
2082794Sktlim@umich.edu#endif // USE_CHECKER
2092316SN/A    }
2102316SN/A
2111858SN/A#if !FULL_SYSTEM
2122292SN/A    thread.resize(number_of_threads);
2132292SN/A    tids.resize(number_of_threads);
2141681SN/A#endif
2151681SN/A
2162325SN/A    // The stages also need their CPU pointer setup.  However this
2172325SN/A    // must be done at the upper level CPU because they have pointers
2182325SN/A    // to the upper level CPU, and not this FullO3CPU.
2191060SN/A
2202292SN/A    // Set up Pointers to the activeThreads list for each stage
2212292SN/A    fetch.setActiveThreads(&activeThreads);
2222292SN/A    decode.setActiveThreads(&activeThreads);
2232292SN/A    rename.setActiveThreads(&activeThreads);
2242292SN/A    iew.setActiveThreads(&activeThreads);
2252292SN/A    commit.setActiveThreads(&activeThreads);
2261060SN/A
2271060SN/A    // Give each of the stages the time buffer they will use.
2281060SN/A    fetch.setTimeBuffer(&timeBuffer);
2291060SN/A    decode.setTimeBuffer(&timeBuffer);
2301060SN/A    rename.setTimeBuffer(&timeBuffer);
2311060SN/A    iew.setTimeBuffer(&timeBuffer);
2321060SN/A    commit.setTimeBuffer(&timeBuffer);
2331060SN/A
2341060SN/A    // Also setup each of the stages' queues.
2351060SN/A    fetch.setFetchQueue(&fetchQueue);
2361060SN/A    decode.setFetchQueue(&fetchQueue);
2372292SN/A    commit.setFetchQueue(&fetchQueue);
2381060SN/A    decode.setDecodeQueue(&decodeQueue);
2391060SN/A    rename.setDecodeQueue(&decodeQueue);
2401060SN/A    rename.setRenameQueue(&renameQueue);
2411060SN/A    iew.setRenameQueue(&renameQueue);
2421060SN/A    iew.setIEWQueue(&iewQueue);
2431060SN/A    commit.setIEWQueue(&iewQueue);
2441060SN/A    commit.setRenameQueue(&renameQueue);
2451060SN/A
2462292SN/A    commit.setIEWStage(&iew);
2472292SN/A    rename.setIEWStage(&iew);
2482292SN/A    rename.setCommitStage(&commit);
2492292SN/A
2502292SN/A#if !FULL_SYSTEM
2512307SN/A    int active_threads = params->workload.size();
2522831Sksewell@umich.edu
2532831Sksewell@umich.edu    if (active_threads > Impl::MaxThreads) {
2542831Sksewell@umich.edu        panic("Workload Size too large. Increase the 'MaxThreads'"
2552831Sksewell@umich.edu              "constant in your O3CPU impl. file (e.g. o3/alpha/impl.hh) or "
2562831Sksewell@umich.edu              "edit your workload size.");
2572831Sksewell@umich.edu    }
2582292SN/A#else
2592307SN/A    int active_threads = 1;
2602292SN/A#endif
2612292SN/A
2622316SN/A    //Make Sure That this a Valid Architeture
2632292SN/A    assert(params->numPhysIntRegs   >= numThreads * TheISA::NumIntRegs);
2642292SN/A    assert(params->numPhysFloatRegs >= numThreads * TheISA::NumFloatRegs);
2652292SN/A
2662292SN/A    rename.setScoreboard(&scoreboard);
2672292SN/A    iew.setScoreboard(&scoreboard);
2682292SN/A
2691060SN/A    // Setup the rename map for whichever stages need it.
2702292SN/A    PhysRegIndex lreg_idx = 0;
2712292SN/A    PhysRegIndex freg_idx = params->numPhysIntRegs; //Index to 1 after int regs
2721060SN/A
2732292SN/A    for (int tid=0; tid < numThreads; tid++) {
2742307SN/A        bool bindRegs = (tid <= active_threads - 1);
2752292SN/A
2762292SN/A        commitRenameMap[tid].init(TheISA::NumIntRegs,
2772292SN/A                                  params->numPhysIntRegs,
2782325SN/A                                  lreg_idx,            //Index for Logical. Regs
2792292SN/A
2802292SN/A                                  TheISA::NumFloatRegs,
2812292SN/A                                  params->numPhysFloatRegs,
2822325SN/A                                  freg_idx,            //Index for Float Regs
2832292SN/A
2842292SN/A                                  TheISA::NumMiscRegs,
2852292SN/A
2862292SN/A                                  TheISA::ZeroReg,
2872292SN/A                                  TheISA::ZeroReg,
2882292SN/A
2892292SN/A                                  tid,
2902292SN/A                                  false);
2912292SN/A
2922292SN/A        renameMap[tid].init(TheISA::NumIntRegs,
2932292SN/A                            params->numPhysIntRegs,
2942325SN/A                            lreg_idx,                  //Index for Logical. Regs
2952292SN/A
2962292SN/A                            TheISA::NumFloatRegs,
2972292SN/A                            params->numPhysFloatRegs,
2982325SN/A                            freg_idx,                  //Index for Float Regs
2992292SN/A
3002292SN/A                            TheISA::NumMiscRegs,
3012292SN/A
3022292SN/A                            TheISA::ZeroReg,
3032292SN/A                            TheISA::ZeroReg,
3042292SN/A
3052292SN/A                            tid,
3062292SN/A                            bindRegs);
3072292SN/A    }
3082292SN/A
3092292SN/A    rename.setRenameMap(renameMap);
3102292SN/A    commit.setRenameMap(commitRenameMap);
3112292SN/A
3122292SN/A    // Give renameMap & rename stage access to the freeList;
3132292SN/A    for (int i=0; i < numThreads; i++) {
3142292SN/A        renameMap[i].setFreeList(&freeList);
3152292SN/A    }
3161060SN/A    rename.setFreeList(&freeList);
3172292SN/A
3181060SN/A    // Setup the ROB for whichever stages need it.
3191060SN/A    commit.setROB(&rob);
3202292SN/A
3212292SN/A    lastRunningCycle = curTick;
3222292SN/A
3232829Sksewell@umich.edu    lastActivatedCycle = -1;
3242829Sksewell@umich.edu
3252292SN/A    contextSwitch = false;
3261060SN/A}
3271060SN/A
3281060SN/Atemplate <class Impl>
3291755SN/AFullO3CPU<Impl>::~FullO3CPU()
3301060SN/A{
3311060SN/A}
3321060SN/A
3331060SN/Atemplate <class Impl>
3341060SN/Avoid
3351755SN/AFullO3CPU<Impl>::fullCPURegStats()
3361062SN/A{
3372733Sktlim@umich.edu    BaseO3CPU::regStats();
3382292SN/A
3392733Sktlim@umich.edu    // Register any of the O3CPU's stats here.
3402292SN/A    timesIdled
3412292SN/A        .name(name() + ".timesIdled")
3422292SN/A        .desc("Number of times that the entire CPU went into an idle state and"
3432292SN/A              " unscheduled itself")
3442292SN/A        .prereq(timesIdled);
3452292SN/A
3462292SN/A    idleCycles
3472292SN/A        .name(name() + ".idleCycles")
3482292SN/A        .desc("Total number of cycles that the CPU has spent unscheduled due "
3492292SN/A              "to idling")
3502292SN/A        .prereq(idleCycles);
3512292SN/A
3522292SN/A    // Number of Instructions simulated
3532292SN/A    // --------------------------------
3542292SN/A    // Should probably be in Base CPU but need templated
3552292SN/A    // MaxThreads so put in here instead
3562292SN/A    committedInsts
3572292SN/A        .init(numThreads)
3582292SN/A        .name(name() + ".committedInsts")
3592292SN/A        .desc("Number of Instructions Simulated");
3602292SN/A
3612292SN/A    totalCommittedInsts
3622292SN/A        .name(name() + ".committedInsts_total")
3632292SN/A        .desc("Number of Instructions Simulated");
3642292SN/A
3652292SN/A    cpi
3662292SN/A        .name(name() + ".cpi")
3672292SN/A        .desc("CPI: Cycles Per Instruction")
3682292SN/A        .precision(6);
3692292SN/A    cpi = simTicks / committedInsts;
3702292SN/A
3712292SN/A    totalCpi
3722292SN/A        .name(name() + ".cpi_total")
3732292SN/A        .desc("CPI: Total CPI of All Threads")
3742292SN/A        .precision(6);
3752292SN/A    totalCpi = simTicks / totalCommittedInsts;
3762292SN/A
3772292SN/A    ipc
3782292SN/A        .name(name() + ".ipc")
3792292SN/A        .desc("IPC: Instructions Per Cycle")
3802292SN/A        .precision(6);
3812292SN/A    ipc =  committedInsts / simTicks;
3822292SN/A
3832292SN/A    totalIpc
3842292SN/A        .name(name() + ".ipc_total")
3852292SN/A        .desc("IPC: Total IPC of All Threads")
3862292SN/A        .precision(6);
3872292SN/A    totalIpc =  totalCommittedInsts / simTicks;
3882292SN/A
3891062SN/A}
3901062SN/A
3911062SN/Atemplate <class Impl>
3922871Sktlim@umich.eduPort *
3932871Sktlim@umich.eduFullO3CPU<Impl>::getPort(const std::string &if_name, int idx)
3942871Sktlim@umich.edu{
3952871Sktlim@umich.edu    if (if_name == "dcache_port")
3962871Sktlim@umich.edu        return iew.getDcachePort();
3972871Sktlim@umich.edu    else if (if_name == "icache_port")
3982871Sktlim@umich.edu        return fetch.getIcachePort();
3992871Sktlim@umich.edu    else
4002871Sktlim@umich.edu        panic("No Such Port\n");
4012871Sktlim@umich.edu}
4022871Sktlim@umich.edu
4032871Sktlim@umich.edutemplate <class Impl>
4041062SN/Avoid
4051755SN/AFullO3CPU<Impl>::tick()
4061060SN/A{
4072733Sktlim@umich.edu    DPRINTF(O3CPU, "\n\nFullO3CPU: Ticking main, FullO3CPU.\n");
4081060SN/A
4092292SN/A    ++numCycles;
4102292SN/A
4112325SN/A//    activity = false;
4122292SN/A
4132292SN/A    //Tick each of the stages
4141060SN/A    fetch.tick();
4151060SN/A
4161060SN/A    decode.tick();
4171060SN/A
4181060SN/A    rename.tick();
4191060SN/A
4201060SN/A    iew.tick();
4211060SN/A
4221060SN/A    commit.tick();
4231060SN/A
4242292SN/A#if !FULL_SYSTEM
4252292SN/A    doContextSwitch();
4262292SN/A#endif
4272292SN/A
4282292SN/A    // Now advance the time buffers
4291060SN/A    timeBuffer.advance();
4301060SN/A
4311060SN/A    fetchQueue.advance();
4321060SN/A    decodeQueue.advance();
4331060SN/A    renameQueue.advance();
4341060SN/A    iewQueue.advance();
4351060SN/A
4362325SN/A    activityRec.advance();
4372292SN/A
4382292SN/A    if (removeInstsThisCycle) {
4392292SN/A        cleanUpRemovedInsts();
4402292SN/A    }
4412292SN/A
4422325SN/A    if (!tickEvent.scheduled()) {
4432867Sktlim@umich.edu        if (_status == SwitchedOut ||
4442905Sktlim@umich.edu            getState() == SimObject::Drained) {
4452325SN/A            // increment stat
4462325SN/A            lastRunningCycle = curTick;
4472325SN/A        } else if (!activityRec.active()) {
4482325SN/A            lastRunningCycle = curTick;
4492325SN/A            timesIdled++;
4502325SN/A        } else {
4512325SN/A            tickEvent.schedule(curTick + cycles(1));
4522325SN/A        }
4532292SN/A    }
4542292SN/A
4552292SN/A#if !FULL_SYSTEM
4562292SN/A    updateThreadPriority();
4572292SN/A#endif
4582292SN/A
4591060SN/A}
4601060SN/A
4611060SN/Atemplate <class Impl>
4621060SN/Avoid
4631755SN/AFullO3CPU<Impl>::init()
4641060SN/A{
4652307SN/A    if (!deferRegistration) {
4662680Sktlim@umich.edu        registerThreadContexts();
4672292SN/A    }
4681060SN/A
4692292SN/A    // Set inSyscall so that the CPU doesn't squash when initially
4702292SN/A    // setting up registers.
4712292SN/A    for (int i = 0; i < number_of_threads; ++i)
4722292SN/A        thread[i]->inSyscall = true;
4732292SN/A
4742292SN/A    for (int tid=0; tid < number_of_threads; tid++) {
4751858SN/A#if FULL_SYSTEM
4762680Sktlim@umich.edu        ThreadContext *src_tc = threadContexts[tid];
4771681SN/A#else
4782680Sktlim@umich.edu        ThreadContext *src_tc = thread[tid]->getTC();
4791681SN/A#endif
4802292SN/A        // Threads start in the Suspended State
4812680Sktlim@umich.edu        if (src_tc->status() != ThreadContext::Suspended) {
4822292SN/A            continue;
4831060SN/A        }
4841060SN/A
4852292SN/A#if FULL_SYSTEM
4862680Sktlim@umich.edu        TheISA::initCPU(src_tc, src_tc->readCpuId());
4872292SN/A#endif
4882292SN/A    }
4892292SN/A
4902292SN/A    // Clear inSyscall.
4912292SN/A    for (int i = 0; i < number_of_threads; ++i)
4922292SN/A        thread[i]->inSyscall = false;
4932292SN/A
4942316SN/A    // Initialize stages.
4952292SN/A    fetch.initStage();
4962292SN/A    iew.initStage();
4972292SN/A    rename.initStage();
4982292SN/A    commit.initStage();
4992292SN/A
5002292SN/A    commit.setThreads(thread);
5012292SN/A}
5022292SN/A
5032292SN/Atemplate <class Impl>
5042292SN/Avoid
5052875Sksewell@umich.eduFullO3CPU<Impl>::activateThread(unsigned tid)
5062875Sksewell@umich.edu{
5072875Sksewell@umich.edu    list<unsigned>::iterator isActive = find(
5082875Sksewell@umich.edu        activeThreads.begin(), activeThreads.end(), tid);
5092875Sksewell@umich.edu
5102875Sksewell@umich.edu    if (isActive == activeThreads.end()) {
5112875Sksewell@umich.edu        DPRINTF(O3CPU, "[tid:%i]: Adding to active threads list\n",
5122875Sksewell@umich.edu                tid);
5132875Sksewell@umich.edu
5142875Sksewell@umich.edu        activeThreads.push_back(tid);
5152875Sksewell@umich.edu    }
5162875Sksewell@umich.edu}
5172875Sksewell@umich.edu
5182875Sksewell@umich.edutemplate <class Impl>
5192875Sksewell@umich.eduvoid
5202875Sksewell@umich.eduFullO3CPU<Impl>::deactivateThread(unsigned tid)
5212875Sksewell@umich.edu{
5222875Sksewell@umich.edu    //Remove From Active List, if Active
5232875Sksewell@umich.edu    list<unsigned>::iterator thread_it =
5242875Sksewell@umich.edu        find(activeThreads.begin(), activeThreads.end(), tid);
5252875Sksewell@umich.edu
5262875Sksewell@umich.edu    if (thread_it != activeThreads.end()) {
5272875Sksewell@umich.edu        DPRINTF(O3CPU,"[tid:%i]: Removing from active threads list\n",
5282875Sksewell@umich.edu                tid);
5292875Sksewell@umich.edu        activeThreads.erase(thread_it);
5302875Sksewell@umich.edu    }
5312875Sksewell@umich.edu}
5322875Sksewell@umich.edu
5332875Sksewell@umich.edutemplate <class Impl>
5342875Sksewell@umich.eduvoid
5352875Sksewell@umich.eduFullO3CPU<Impl>::activateContext(int tid, int delay)
5362875Sksewell@umich.edu{
5372875Sksewell@umich.edu    // Needs to set each stage to running as well.
5382875Sksewell@umich.edu    if (delay){
5392875Sksewell@umich.edu        DPRINTF(O3CPU, "[tid:%i]: Scheduling thread context to activate "
5402875Sksewell@umich.edu                "on cycle %d\n", tid, curTick + cycles(delay));
5412875Sksewell@umich.edu        scheduleActivateThreadEvent(tid, delay);
5422875Sksewell@umich.edu    } else {
5432875Sksewell@umich.edu        activateThread(tid);
5442875Sksewell@umich.edu    }
5452875Sksewell@umich.edu
5462875Sksewell@umich.edu    if(lastActivatedCycle < curTick) {
5472875Sksewell@umich.edu        scheduleTickEvent(delay);
5482875Sksewell@umich.edu
5492875Sksewell@umich.edu        // Be sure to signal that there's some activity so the CPU doesn't
5502875Sksewell@umich.edu        // deschedule itself.
5512875Sksewell@umich.edu        activityRec.activity();
5522875Sksewell@umich.edu        fetch.wakeFromQuiesce();
5532875Sksewell@umich.edu
5542875Sksewell@umich.edu        lastActivatedCycle = curTick;
5552875Sksewell@umich.edu
5562875Sksewell@umich.edu        _status = Running;
5572875Sksewell@umich.edu    }
5582875Sksewell@umich.edu}
5592875Sksewell@umich.edu
5602875Sksewell@umich.edutemplate <class Impl>
5612875Sksewell@umich.eduvoid
5622875Sksewell@umich.eduFullO3CPU<Impl>::deallocateContext(int tid, int delay)
5632875Sksewell@umich.edu{
5642875Sksewell@umich.edu    // Schedule removal of thread data from CPU
5652875Sksewell@umich.edu    if (delay){
5662875Sksewell@umich.edu        DPRINTF(O3CPU, "[tid:%i]: Scheduling thread context to deallocate "
5672875Sksewell@umich.edu                "on cycle %d\n", tid, curTick + cycles(delay));
5682875Sksewell@umich.edu        scheduleDeallocateContextEvent(tid, delay);
5692875Sksewell@umich.edu    } else {
5702875Sksewell@umich.edu        deactivateThread(tid);
5712875Sksewell@umich.edu        removeThread(tid);
5722875Sksewell@umich.edu    }
5732875Sksewell@umich.edu}
5742875Sksewell@umich.edu
5752875Sksewell@umich.edutemplate <class Impl>
5762875Sksewell@umich.eduvoid
5772875Sksewell@umich.eduFullO3CPU<Impl>::suspendContext(int tid)
5782875Sksewell@umich.edu{
5792875Sksewell@umich.edu    DPRINTF(O3CPU,"[tid: %i]: Suspending Thread Context.\n", tid);
5802910Sksewell@umich.edu    deactivateThread();
5812910Sksewell@umich.edu    if (activeThreads.size() == 0)
5822910Sksewell@umich.edu        unscheduleTickEvent();
5832875Sksewell@umich.edu    _status = Idle;
5842875Sksewell@umich.edu}
5852875Sksewell@umich.edu
5862875Sksewell@umich.edutemplate <class Impl>
5872875Sksewell@umich.eduvoid
5882875Sksewell@umich.eduFullO3CPU<Impl>::haltContext(int tid)
5892875Sksewell@umich.edu{
5902910Sksewell@umich.edu    //For now, this is the same as deallocate
5912910Sksewell@umich.edu    DPRINTF(O3CPU,"[tid:%i]: Halt Context called. Deallocating", tid);
5922910Sksewell@umich.edu    deallocateContext(tid, 1);
5932875Sksewell@umich.edu}
5942875Sksewell@umich.edu
5952875Sksewell@umich.edutemplate <class Impl>
5962875Sksewell@umich.eduvoid
5972292SN/AFullO3CPU<Impl>::insertThread(unsigned tid)
5982292SN/A{
5992847Sksewell@umich.edu    DPRINTF(O3CPU,"[tid:%i] Initializing thread into CPU");
6002292SN/A    // Will change now that the PC and thread state is internal to the CPU
6012683Sktlim@umich.edu    // and not in the ThreadContext.
6022292SN/A#if FULL_SYSTEM
6032680Sktlim@umich.edu    ThreadContext *src_tc = system->threadContexts[tid];
6042292SN/A#else
6052847Sksewell@umich.edu    ThreadContext *src_tc = tcBase(tid);
6062292SN/A#endif
6072292SN/A
6082292SN/A    //Bind Int Regs to Rename Map
6092292SN/A    for (int ireg = 0; ireg < TheISA::NumIntRegs; ireg++) {
6102292SN/A        PhysRegIndex phys_reg = freeList.getIntReg();
6112292SN/A
6122292SN/A        renameMap[tid].setEntry(ireg,phys_reg);
6132292SN/A        scoreboard.setReg(phys_reg);
6142292SN/A    }
6152292SN/A
6162292SN/A    //Bind Float Regs to Rename Map
6172292SN/A    for (int freg = 0; freg < TheISA::NumFloatRegs; freg++) {
6182292SN/A        PhysRegIndex phys_reg = freeList.getFloatReg();
6192292SN/A
6202292SN/A        renameMap[tid].setEntry(freg,phys_reg);
6212292SN/A        scoreboard.setReg(phys_reg);
6222292SN/A    }
6232292SN/A
6242292SN/A    //Copy Thread Data Into RegFile
6252847Sksewell@umich.edu    //this->copyFromTC(tid);
6262292SN/A
6272847Sksewell@umich.edu    //Set PC/NPC/NNPC
6282847Sksewell@umich.edu    setPC(src_tc->readPC(), tid);
6292847Sksewell@umich.edu    setNextPC(src_tc->readNextPC(), tid);
6302847Sksewell@umich.edu#if THE_ISA != ALPHA_ISA
6312847Sksewell@umich.edu    setNextNPC(src_tc->readNextNPC(), tid);
6322847Sksewell@umich.edu#endif
6332292SN/A
6342680Sktlim@umich.edu    src_tc->setStatus(ThreadContext::Active);
6352292SN/A
6362292SN/A    activateContext(tid,1);
6372292SN/A
6382292SN/A    //Reset ROB/IQ/LSQ Entries
6392292SN/A    commit.rob->resetEntries();
6402292SN/A    iew.resetEntries();
6412292SN/A}
6422292SN/A
6432292SN/Atemplate <class Impl>
6442292SN/Avoid
6452292SN/AFullO3CPU<Impl>::removeThread(unsigned tid)
6462292SN/A{
6472877Sksewell@umich.edu    DPRINTF(O3CPU,"[tid:%i] Removing thread context from CPU.\n", tid);
6482847Sksewell@umich.edu
6492847Sksewell@umich.edu    // Copy Thread Data From RegFile
6502847Sksewell@umich.edu    // If thread is suspended, it might be re-allocated
6512847Sksewell@umich.edu    //this->copyToTC(tid);
6522847Sksewell@umich.edu
6532847Sksewell@umich.edu    // Unbind Int Regs from Rename Map
6542292SN/A    for (int ireg = 0; ireg < TheISA::NumIntRegs; ireg++) {
6552292SN/A        PhysRegIndex phys_reg = renameMap[tid].lookup(ireg);
6562292SN/A
6572292SN/A        scoreboard.unsetReg(phys_reg);
6582292SN/A        freeList.addReg(phys_reg);
6592292SN/A    }
6602292SN/A
6612847Sksewell@umich.edu    // Unbind Float Regs from Rename Map
6622292SN/A    for (int freg = 0; freg < TheISA::NumFloatRegs; freg++) {
6632292SN/A        PhysRegIndex phys_reg = renameMap[tid].lookup(freg);
6642292SN/A
6652292SN/A        scoreboard.unsetReg(phys_reg);
6662292SN/A        freeList.addReg(phys_reg);
6672292SN/A    }
6682292SN/A
6692847Sksewell@umich.edu    // Squash Throughout Pipeline
6702292SN/A    fetch.squash(0,tid);
6712292SN/A    decode.squash(tid);
6722292SN/A    rename.squash(tid);
6732875Sksewell@umich.edu    iew.squash(tid);
6742875Sksewell@umich.edu    commit.rob->squash(commit.rob->readHeadInst(tid)->seqNum, tid);
6752292SN/A
6762292SN/A    assert(iew.ldstQueue.getCount(tid) == 0);
6772292SN/A
6782847Sksewell@umich.edu    // Reset ROB/IQ/LSQ Entries
6792292SN/A    if (activeThreads.size() >= 1) {
6802292SN/A        commit.rob->resetEntries();
6812292SN/A        iew.resetEntries();
6822292SN/A    }
6832292SN/A}
6842292SN/A
6852292SN/A
6862292SN/Atemplate <class Impl>
6872292SN/Avoid
6882292SN/AFullO3CPU<Impl>::activateWhenReady(int tid)
6892292SN/A{
6902733Sktlim@umich.edu    DPRINTF(O3CPU,"[tid:%i]: Checking if resources are available for incoming"
6912292SN/A            "(e.g. PhysRegs/ROB/IQ/LSQ) \n",
6922292SN/A            tid);
6932292SN/A
6942292SN/A    bool ready = true;
6952292SN/A
6962292SN/A    if (freeList.numFreeIntRegs() >= TheISA::NumIntRegs) {
6972733Sktlim@umich.edu        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
6982292SN/A                "Phys. Int. Regs.\n",
6992292SN/A                tid);
7002292SN/A        ready = false;
7012292SN/A    } else if (freeList.numFreeFloatRegs() >= TheISA::NumFloatRegs) {
7022733Sktlim@umich.edu        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
7032292SN/A                "Phys. Float. Regs.\n",
7042292SN/A                tid);
7052292SN/A        ready = false;
7062292SN/A    } else if (commit.rob->numFreeEntries() >=
7072292SN/A               commit.rob->entryAmount(activeThreads.size() + 1)) {
7082733Sktlim@umich.edu        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
7092292SN/A                "ROB entries.\n",
7102292SN/A                tid);
7112292SN/A        ready = false;
7122292SN/A    } else if (iew.instQueue.numFreeEntries() >=
7132292SN/A               iew.instQueue.entryAmount(activeThreads.size() + 1)) {
7142733Sktlim@umich.edu        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
7152292SN/A                "IQ entries.\n",
7162292SN/A                tid);
7172292SN/A        ready = false;
7182292SN/A    } else if (iew.ldstQueue.numFreeEntries() >=
7192292SN/A               iew.ldstQueue.entryAmount(activeThreads.size() + 1)) {
7202733Sktlim@umich.edu        DPRINTF(O3CPU,"[tid:%i] Suspending thread due to not enough "
7212292SN/A                "LSQ entries.\n",
7222292SN/A                tid);
7232292SN/A        ready = false;
7242292SN/A    }
7252292SN/A
7262292SN/A    if (ready) {
7272292SN/A        insertThread(tid);
7282292SN/A
7292292SN/A        contextSwitch = false;
7302292SN/A
7312292SN/A        cpuWaitList.remove(tid);
7322292SN/A    } else {
7332292SN/A        suspendContext(tid);
7342292SN/A
7352292SN/A        //blocks fetch
7362292SN/A        contextSwitch = true;
7372292SN/A
7382875Sksewell@umich.edu        //@todo: dont always add to waitlist
7392292SN/A        //do waitlist
7402292SN/A        cpuWaitList.push_back(tid);
7411060SN/A    }
7421060SN/A}
7431060SN/A
7441060SN/Atemplate <class Impl>
7452852Sktlim@umich.eduvoid
7462864Sktlim@umich.eduFullO3CPU<Impl>::serialize(std::ostream &os)
7472864Sktlim@umich.edu{
7482864Sktlim@umich.edu    SERIALIZE_ENUM(_status);
7492864Sktlim@umich.edu    BaseCPU::serialize(os);
7502864Sktlim@umich.edu    nameOut(os, csprintf("%s.tickEvent", name()));
7512864Sktlim@umich.edu    tickEvent.serialize(os);
7522864Sktlim@umich.edu
7532864Sktlim@umich.edu    // Use SimpleThread's ability to checkpoint to make it easier to
7542864Sktlim@umich.edu    // write out the registers.  Also make this static so it doesn't
7552864Sktlim@umich.edu    // get instantiated multiple times (causes a panic in statistics).
7562864Sktlim@umich.edu    static SimpleThread temp;
7572864Sktlim@umich.edu
7582864Sktlim@umich.edu    for (int i = 0; i < thread.size(); i++) {
7592864Sktlim@umich.edu        nameOut(os, csprintf("%s.xc.%i", name(), i));
7602864Sktlim@umich.edu        temp.copyTC(thread[i]->getTC());
7612864Sktlim@umich.edu        temp.serialize(os);
7622864Sktlim@umich.edu    }
7632864Sktlim@umich.edu}
7642864Sktlim@umich.edu
7652864Sktlim@umich.edutemplate <class Impl>
7662864Sktlim@umich.eduvoid
7672864Sktlim@umich.eduFullO3CPU<Impl>::unserialize(Checkpoint *cp, const std::string &section)
7682864Sktlim@umich.edu{
7692864Sktlim@umich.edu    UNSERIALIZE_ENUM(_status);
7702864Sktlim@umich.edu    BaseCPU::unserialize(cp, section);
7712864Sktlim@umich.edu    tickEvent.unserialize(cp, csprintf("%s.tickEvent", section));
7722864Sktlim@umich.edu
7732864Sktlim@umich.edu    // Use SimpleThread's ability to checkpoint to make it easier to
7742864Sktlim@umich.edu    // read in the registers.  Also make this static so it doesn't
7752864Sktlim@umich.edu    // get instantiated multiple times (causes a panic in statistics).
7762864Sktlim@umich.edu    static SimpleThread temp;
7772864Sktlim@umich.edu
7782864Sktlim@umich.edu    for (int i = 0; i < thread.size(); i++) {
7792864Sktlim@umich.edu        temp.copyTC(thread[i]->getTC());
7802864Sktlim@umich.edu        temp.unserialize(cp, csprintf("%s.xc.%i", section, i));
7812864Sktlim@umich.edu        thread[i]->getTC()->copyArchRegs(temp.getTC());
7822864Sktlim@umich.edu    }
7832864Sktlim@umich.edu}
7842864Sktlim@umich.edu
7852864Sktlim@umich.edutemplate <class Impl>
7862905Sktlim@umich.eduunsigned int
7872843Sktlim@umich.eduFullO3CPU<Impl>::drain(Event *drain_event)
7881060SN/A{
7892843Sktlim@umich.edu    drainCount = 0;
7902843Sktlim@umich.edu    fetch.drain();
7912843Sktlim@umich.edu    decode.drain();
7922843Sktlim@umich.edu    rename.drain();
7932843Sktlim@umich.edu    iew.drain();
7942843Sktlim@umich.edu    commit.drain();
7952325SN/A
7962325SN/A    // Wake the CPU and record activity so everything can drain out if
7972863Sktlim@umich.edu    // the CPU was not able to immediately drain.
7982905Sktlim@umich.edu    if (getState() != SimObject::Drained) {
7992864Sktlim@umich.edu        // A bit of a hack...set the drainEvent after all the drain()
8002864Sktlim@umich.edu        // calls have been made, that way if all of the stages drain
8012864Sktlim@umich.edu        // immediately, the signalDrained() function knows not to call
8022864Sktlim@umich.edu        // process on the drain event.
8032864Sktlim@umich.edu        drainEvent = drain_event;
8042843Sktlim@umich.edu
8052863Sktlim@umich.edu        wakeCPU();
8062863Sktlim@umich.edu        activityRec.activity();
8072852Sktlim@umich.edu
8082905Sktlim@umich.edu        return 1;
8092863Sktlim@umich.edu    } else {
8102905Sktlim@umich.edu        return 0;
8112863Sktlim@umich.edu    }
8122316SN/A}
8132310SN/A
8142316SN/Atemplate <class Impl>
8152316SN/Avoid
8162843Sktlim@umich.eduFullO3CPU<Impl>::resume()
8172316SN/A{
8182905Sktlim@umich.edu    assert(system->getMemoryMode() == System::Timing);
8192843Sktlim@umich.edu    fetch.resume();
8202843Sktlim@umich.edu    decode.resume();
8212843Sktlim@umich.edu    rename.resume();
8222843Sktlim@umich.edu    iew.resume();
8232843Sktlim@umich.edu    commit.resume();
8242316SN/A
8252905Sktlim@umich.edu    changeState(SimObject::Running);
8262905Sktlim@umich.edu
8272864Sktlim@umich.edu    if (_status == SwitchedOut || _status == Idle)
8282864Sktlim@umich.edu        return;
8292864Sktlim@umich.edu
8302843Sktlim@umich.edu    if (!tickEvent.scheduled())
8312843Sktlim@umich.edu        tickEvent.schedule(curTick);
8322843Sktlim@umich.edu    _status = Running;
8332843Sktlim@umich.edu}
8342316SN/A
8352843Sktlim@umich.edutemplate <class Impl>
8362843Sktlim@umich.eduvoid
8372843Sktlim@umich.eduFullO3CPU<Impl>::signalDrained()
8382843Sktlim@umich.edu{
8392843Sktlim@umich.edu    if (++drainCount == NumStages) {
8402316SN/A        if (tickEvent.scheduled())
8412316SN/A            tickEvent.squash();
8422863Sktlim@umich.edu
8432905Sktlim@umich.edu        changeState(SimObject::Drained);
8442863Sktlim@umich.edu
8452863Sktlim@umich.edu        if (drainEvent) {
8462863Sktlim@umich.edu            drainEvent->process();
8472863Sktlim@umich.edu            drainEvent = NULL;
8482863Sktlim@umich.edu        }
8492310SN/A    }
8502843Sktlim@umich.edu    assert(drainCount <= 5);
8512843Sktlim@umich.edu}
8522843Sktlim@umich.edu
8532843Sktlim@umich.edutemplate <class Impl>
8542843Sktlim@umich.eduvoid
8552843Sktlim@umich.eduFullO3CPU<Impl>::switchOut()
8562843Sktlim@umich.edu{
8572843Sktlim@umich.edu    fetch.switchOut();
8582843Sktlim@umich.edu    rename.switchOut();
8592843Sktlim@umich.edu    commit.switchOut();
8602843Sktlim@umich.edu    instList.clear();
8612843Sktlim@umich.edu    while (!removeList.empty()) {
8622843Sktlim@umich.edu        removeList.pop();
8632843Sktlim@umich.edu    }
8642843Sktlim@umich.edu
8652843Sktlim@umich.edu    _status = SwitchedOut;
8662843Sktlim@umich.edu#if USE_CHECKER
8672843Sktlim@umich.edu    if (checker)
8682843Sktlim@umich.edu        checker->switchOut();
8692843Sktlim@umich.edu#endif
8701060SN/A}
8711060SN/A
8721060SN/Atemplate <class Impl>
8731060SN/Avoid
8741755SN/AFullO3CPU<Impl>::takeOverFrom(BaseCPU *oldCPU)
8751060SN/A{
8762325SN/A    // Flush out any old data from the time buffers.
8772873Sktlim@umich.edu    for (int i = 0; i < timeBuffer.getSize(); ++i) {
8782307SN/A        timeBuffer.advance();
8792307SN/A        fetchQueue.advance();
8802307SN/A        decodeQueue.advance();
8812307SN/A        renameQueue.advance();
8822307SN/A        iewQueue.advance();
8832307SN/A    }
8842307SN/A
8852325SN/A    activityRec.reset();
8862307SN/A
8871060SN/A    BaseCPU::takeOverFrom(oldCPU);
8881060SN/A
8892307SN/A    fetch.takeOverFrom();
8902307SN/A    decode.takeOverFrom();
8912307SN/A    rename.takeOverFrom();
8922307SN/A    iew.takeOverFrom();
8932307SN/A    commit.takeOverFrom();
8942307SN/A
8951060SN/A    assert(!tickEvent.scheduled());
8961060SN/A
8972325SN/A    // @todo: Figure out how to properly select the tid to put onto
8982325SN/A    // the active threads list.
8992307SN/A    int tid = 0;
9002307SN/A
9012307SN/A    list<unsigned>::iterator isActive = find(
9022307SN/A        activeThreads.begin(), activeThreads.end(), tid);
9032307SN/A
9042307SN/A    if (isActive == activeThreads.end()) {
9052325SN/A        //May Need to Re-code this if the delay variable is the delay
9062325SN/A        //needed for thread to activate
9072733Sktlim@umich.edu        DPRINTF(O3CPU, "Adding Thread %i to active threads list\n",
9082307SN/A                tid);
9092307SN/A
9102307SN/A        activeThreads.push_back(tid);
9112307SN/A    }
9122307SN/A
9132325SN/A    // Set all statuses to active, schedule the CPU's tick event.
9142307SN/A    // @todo: Fix up statuses so this is handled properly
9152680Sktlim@umich.edu    for (int i = 0; i < threadContexts.size(); ++i) {
9162680Sktlim@umich.edu        ThreadContext *tc = threadContexts[i];
9172680Sktlim@umich.edu        if (tc->status() == ThreadContext::Active && _status != Running) {
9181681SN/A            _status = Running;
9191681SN/A            tickEvent.schedule(curTick);
9201681SN/A        }
9211060SN/A    }
9222307SN/A    if (!tickEvent.scheduled())
9232307SN/A        tickEvent.schedule(curTick);
9241060SN/A}
9251060SN/A
9261060SN/Atemplate <class Impl>
9271060SN/Auint64_t
9281755SN/AFullO3CPU<Impl>::readIntReg(int reg_idx)
9291060SN/A{
9301060SN/A    return regFile.readIntReg(reg_idx);
9311060SN/A}
9321060SN/A
9331060SN/Atemplate <class Impl>
9342455SN/AFloatReg
9352455SN/AFullO3CPU<Impl>::readFloatReg(int reg_idx, int width)
9361060SN/A{
9372455SN/A    return regFile.readFloatReg(reg_idx, width);
9381060SN/A}
9391060SN/A
9401060SN/Atemplate <class Impl>
9412455SN/AFloatReg
9422455SN/AFullO3CPU<Impl>::readFloatReg(int reg_idx)
9431060SN/A{
9442455SN/A    return regFile.readFloatReg(reg_idx);
9451060SN/A}
9461060SN/A
9471060SN/Atemplate <class Impl>
9482455SN/AFloatRegBits
9492455SN/AFullO3CPU<Impl>::readFloatRegBits(int reg_idx, int width)
9501060SN/A{
9512455SN/A    return regFile.readFloatRegBits(reg_idx, width);
9522455SN/A}
9532455SN/A
9542455SN/Atemplate <class Impl>
9552455SN/AFloatRegBits
9562455SN/AFullO3CPU<Impl>::readFloatRegBits(int reg_idx)
9572455SN/A{
9582455SN/A    return regFile.readFloatRegBits(reg_idx);
9591060SN/A}
9601060SN/A
9611060SN/Atemplate <class Impl>
9621060SN/Avoid
9631755SN/AFullO3CPU<Impl>::setIntReg(int reg_idx, uint64_t val)
9641060SN/A{
9651060SN/A    regFile.setIntReg(reg_idx, val);
9661060SN/A}
9671060SN/A
9681060SN/Atemplate <class Impl>
9691060SN/Avoid
9702455SN/AFullO3CPU<Impl>::setFloatReg(int reg_idx, FloatReg val, int width)
9711060SN/A{
9722455SN/A    regFile.setFloatReg(reg_idx, val, width);
9731060SN/A}
9741060SN/A
9751060SN/Atemplate <class Impl>
9761060SN/Avoid
9772455SN/AFullO3CPU<Impl>::setFloatReg(int reg_idx, FloatReg val)
9781060SN/A{
9792455SN/A    regFile.setFloatReg(reg_idx, val);
9801060SN/A}
9811060SN/A
9821060SN/Atemplate <class Impl>
9831060SN/Avoid
9842455SN/AFullO3CPU<Impl>::setFloatRegBits(int reg_idx, FloatRegBits val, int width)
9851060SN/A{
9862455SN/A    regFile.setFloatRegBits(reg_idx, val, width);
9872455SN/A}
9882455SN/A
9892455SN/Atemplate <class Impl>
9902455SN/Avoid
9912455SN/AFullO3CPU<Impl>::setFloatRegBits(int reg_idx, FloatRegBits val)
9922455SN/A{
9932455SN/A    regFile.setFloatRegBits(reg_idx, val);
9941060SN/A}
9951060SN/A
9961060SN/Atemplate <class Impl>
9971060SN/Auint64_t
9982292SN/AFullO3CPU<Impl>::readArchIntReg(int reg_idx, unsigned tid)
9991060SN/A{
10002292SN/A    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(reg_idx);
10012292SN/A
10022292SN/A    return regFile.readIntReg(phys_reg);
10032292SN/A}
10042292SN/A
10052292SN/Atemplate <class Impl>
10062292SN/Afloat
10072292SN/AFullO3CPU<Impl>::readArchFloatRegSingle(int reg_idx, unsigned tid)
10082292SN/A{
10092307SN/A    int idx = reg_idx + TheISA::FP_Base_DepTag;
10102307SN/A    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(idx);
10112292SN/A
10122669Sktlim@umich.edu    return regFile.readFloatReg(phys_reg);
10132292SN/A}
10142292SN/A
10152292SN/Atemplate <class Impl>
10162292SN/Adouble
10172292SN/AFullO3CPU<Impl>::readArchFloatRegDouble(int reg_idx, unsigned tid)
10182292SN/A{
10192307SN/A    int idx = reg_idx + TheISA::FP_Base_DepTag;
10202307SN/A    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(idx);
10212292SN/A
10222669Sktlim@umich.edu    return regFile.readFloatReg(phys_reg, 64);
10232292SN/A}
10242292SN/A
10252292SN/Atemplate <class Impl>
10262292SN/Auint64_t
10272292SN/AFullO3CPU<Impl>::readArchFloatRegInt(int reg_idx, unsigned tid)
10282292SN/A{
10292307SN/A    int idx = reg_idx + TheISA::FP_Base_DepTag;
10302307SN/A    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(idx);
10312292SN/A
10322669Sktlim@umich.edu    return regFile.readFloatRegBits(phys_reg);
10331060SN/A}
10341060SN/A
10351060SN/Atemplate <class Impl>
10361060SN/Avoid
10372292SN/AFullO3CPU<Impl>::setArchIntReg(int reg_idx, uint64_t val, unsigned tid)
10381060SN/A{
10392292SN/A    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(reg_idx);
10402292SN/A
10412292SN/A    regFile.setIntReg(phys_reg, val);
10421060SN/A}
10431060SN/A
10441060SN/Atemplate <class Impl>
10451060SN/Avoid
10462292SN/AFullO3CPU<Impl>::setArchFloatRegSingle(int reg_idx, float val, unsigned tid)
10471060SN/A{
10482292SN/A    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(reg_idx);
10492292SN/A
10502669Sktlim@umich.edu    regFile.setFloatReg(phys_reg, val);
10511060SN/A}
10521060SN/A
10531060SN/Atemplate <class Impl>
10541060SN/Avoid
10552292SN/AFullO3CPU<Impl>::setArchFloatRegDouble(int reg_idx, double val, unsigned tid)
10561060SN/A{
10572292SN/A    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(reg_idx);
10582292SN/A
10592669Sktlim@umich.edu    regFile.setFloatReg(phys_reg, val, 64);
10601060SN/A}
10611060SN/A
10621060SN/Atemplate <class Impl>
10631060SN/Avoid
10642292SN/AFullO3CPU<Impl>::setArchFloatRegInt(int reg_idx, uint64_t val, unsigned tid)
10651060SN/A{
10662292SN/A    PhysRegIndex phys_reg = commitRenameMap[tid].lookup(reg_idx);
10671060SN/A
10682669Sktlim@umich.edu    regFile.setFloatRegBits(phys_reg, val);
10692292SN/A}
10702292SN/A
10712292SN/Atemplate <class Impl>
10722292SN/Auint64_t
10732292SN/AFullO3CPU<Impl>::readPC(unsigned tid)
10742292SN/A{
10752292SN/A    return commit.readPC(tid);
10761060SN/A}
10771060SN/A
10781060SN/Atemplate <class Impl>
10791060SN/Avoid
10802292SN/AFullO3CPU<Impl>::setPC(Addr new_PC,unsigned tid)
10811060SN/A{
10822292SN/A    commit.setPC(new_PC, tid);
10832292SN/A}
10841060SN/A
10852292SN/Atemplate <class Impl>
10862292SN/Auint64_t
10872292SN/AFullO3CPU<Impl>::readNextPC(unsigned tid)
10882292SN/A{
10892292SN/A    return commit.readNextPC(tid);
10902292SN/A}
10911060SN/A
10922292SN/Atemplate <class Impl>
10932292SN/Avoid
10942292SN/AFullO3CPU<Impl>::setNextPC(uint64_t val,unsigned tid)
10952292SN/A{
10962292SN/A    commit.setNextPC(val, tid);
10972292SN/A}
10981060SN/A
10992756Sksewell@umich.edu#if THE_ISA != ALPHA_ISA
11002756Sksewell@umich.edutemplate <class Impl>
11012756Sksewell@umich.eduuint64_t
11022756Sksewell@umich.eduFullO3CPU<Impl>::readNextNPC(unsigned tid)
11032756Sksewell@umich.edu{
11042756Sksewell@umich.edu    return commit.readNextNPC(tid);
11052756Sksewell@umich.edu}
11062756Sksewell@umich.edu
11072756Sksewell@umich.edutemplate <class Impl>
11082756Sksewell@umich.eduvoid
11092756Sksewell@umich.eduFullO3CPU<Impl>::setNextNNPC(uint64_t val,unsigned tid)
11102756Sksewell@umich.edu{
11112756Sksewell@umich.edu    commit.setNextNPC(val, tid);
11122756Sksewell@umich.edu}
11132756Sksewell@umich.edu#endif
11142756Sksewell@umich.edu
11152292SN/Atemplate <class Impl>
11162292SN/Atypename FullO3CPU<Impl>::ListIt
11172292SN/AFullO3CPU<Impl>::addInst(DynInstPtr &inst)
11182292SN/A{
11192292SN/A    instList.push_back(inst);
11201060SN/A
11212292SN/A    return --(instList.end());
11222292SN/A}
11231060SN/A
11242292SN/Atemplate <class Impl>
11252292SN/Avoid
11262292SN/AFullO3CPU<Impl>::instDone(unsigned tid)
11272292SN/A{
11282292SN/A    // Keep an instruction count.
11292292SN/A    thread[tid]->numInst++;
11302292SN/A    thread[tid]->numInsts++;
11312292SN/A    committedInsts[tid]++;
11322292SN/A    totalCommittedInsts++;
11332292SN/A
11342292SN/A    // Check for instruction-count-based events.
11352292SN/A    comInstEventQueue[tid]->serviceEvents(thread[tid]->numInst);
11362292SN/A}
11372292SN/A
11382292SN/Atemplate <class Impl>
11392292SN/Avoid
11402292SN/AFullO3CPU<Impl>::addToRemoveList(DynInstPtr &inst)
11412292SN/A{
11422292SN/A    removeInstsThisCycle = true;
11432292SN/A
11442292SN/A    removeList.push(inst->getInstListIt());
11451060SN/A}
11461060SN/A
11471060SN/Atemplate <class Impl>
11481060SN/Avoid
11491755SN/AFullO3CPU<Impl>::removeFrontInst(DynInstPtr &inst)
11501060SN/A{
11512733Sktlim@umich.edu    DPRINTF(O3CPU, "Removing committed instruction [tid:%i] PC %#x "
11522292SN/A            "[sn:%lli]\n",
11532303SN/A            inst->threadNumber, inst->readPC(), inst->seqNum);
11541060SN/A
11552292SN/A    removeInstsThisCycle = true;
11561060SN/A
11571060SN/A    // Remove the front instruction.
11582292SN/A    removeList.push(inst->getInstListIt());
11591060SN/A}
11601060SN/A
11611060SN/Atemplate <class Impl>
11621060SN/Avoid
11632292SN/AFullO3CPU<Impl>::removeInstsNotInROB(unsigned tid)
11641060SN/A{
11652733Sktlim@umich.edu    DPRINTF(O3CPU, "Thread %i: Deleting instructions from instruction"
11662292SN/A            " list.\n", tid);
11671060SN/A
11682292SN/A    ListIt end_it;
11691060SN/A
11702292SN/A    bool rob_empty = false;
11712292SN/A
11722292SN/A    if (instList.empty()) {
11732292SN/A        return;
11742292SN/A    } else if (rob.isEmpty(/*tid*/)) {
11752733Sktlim@umich.edu        DPRINTF(O3CPU, "ROB is empty, squashing all insts.\n");
11762292SN/A        end_it = instList.begin();
11772292SN/A        rob_empty = true;
11782292SN/A    } else {
11792292SN/A        end_it = (rob.readTailInst(tid))->getInstListIt();
11802733Sktlim@umich.edu        DPRINTF(O3CPU, "ROB is not empty, squashing insts not in ROB.\n");
11812292SN/A    }
11822292SN/A
11832292SN/A    removeInstsThisCycle = true;
11842292SN/A
11852292SN/A    ListIt inst_it = instList.end();
11862292SN/A
11872292SN/A    inst_it--;
11882292SN/A
11892292SN/A    // Walk through the instruction list, removing any instructions
11902292SN/A    // that were inserted after the given instruction iterator, end_it.
11912292SN/A    while (inst_it != end_it) {
11922292SN/A        assert(!instList.empty());
11932292SN/A
11942292SN/A        squashInstIt(inst_it, tid);
11952292SN/A
11962292SN/A        inst_it--;
11972292SN/A    }
11982292SN/A
11992292SN/A    // If the ROB was empty, then we actually need to remove the first
12002292SN/A    // instruction as well.
12012292SN/A    if (rob_empty) {
12022292SN/A        squashInstIt(inst_it, tid);
12032292SN/A    }
12041060SN/A}
12051060SN/A
12061060SN/Atemplate <class Impl>
12071060SN/Avoid
12082292SN/AFullO3CPU<Impl>::removeInstsUntil(const InstSeqNum &seq_num,
12092292SN/A                                  unsigned tid)
12101062SN/A{
12112292SN/A    assert(!instList.empty());
12122292SN/A
12132292SN/A    removeInstsThisCycle = true;
12142292SN/A
12152292SN/A    ListIt inst_iter = instList.end();
12162292SN/A
12172292SN/A    inst_iter--;
12182292SN/A
12192733Sktlim@umich.edu    DPRINTF(O3CPU, "Deleting instructions from instruction "
12202292SN/A            "list that are from [tid:%i] and above [sn:%lli] (end=%lli).\n",
12212292SN/A            tid, seq_num, (*inst_iter)->seqNum);
12221062SN/A
12232292SN/A    while ((*inst_iter)->seqNum > seq_num) {
12241062SN/A
12252292SN/A        bool break_loop = (inst_iter == instList.begin());
12261062SN/A
12272292SN/A        squashInstIt(inst_iter, tid);
12281062SN/A
12292292SN/A        inst_iter--;
12301062SN/A
12312292SN/A        if (break_loop)
12322292SN/A            break;
12332292SN/A    }
12342292SN/A}
12352292SN/A
12362292SN/Atemplate <class Impl>
12372292SN/Ainline void
12382292SN/AFullO3CPU<Impl>::squashInstIt(const ListIt &instIt, const unsigned &tid)
12392292SN/A{
12402292SN/A    if ((*instIt)->threadNumber == tid) {
12412733Sktlim@umich.edu        DPRINTF(O3CPU, "Squashing instruction, "
12422292SN/A                "[tid:%i] [sn:%lli] PC %#x\n",
12432292SN/A                (*instIt)->threadNumber,
12442292SN/A                (*instIt)->seqNum,
12452292SN/A                (*instIt)->readPC());
12461062SN/A
12471062SN/A        // Mark it as squashed.
12482292SN/A        (*instIt)->setSquashed();
12492292SN/A
12502325SN/A        // @todo: Formulate a consistent method for deleting
12512325SN/A        // instructions from the instruction list
12522292SN/A        // Remove the instruction from the list.
12532292SN/A        removeList.push(instIt);
12542292SN/A    }
12552292SN/A}
12562292SN/A
12572292SN/Atemplate <class Impl>
12582292SN/Avoid
12592292SN/AFullO3CPU<Impl>::cleanUpRemovedInsts()
12602292SN/A{
12612292SN/A    while (!removeList.empty()) {
12622733Sktlim@umich.edu        DPRINTF(O3CPU, "Removing instruction, "
12632292SN/A                "[tid:%i] [sn:%lli] PC %#x\n",
12642292SN/A                (*removeList.front())->threadNumber,
12652292SN/A                (*removeList.front())->seqNum,
12662292SN/A                (*removeList.front())->readPC());
12672292SN/A
12682292SN/A        instList.erase(removeList.front());
12692292SN/A
12702292SN/A        removeList.pop();
12711062SN/A    }
12721062SN/A
12732292SN/A    removeInstsThisCycle = false;
12741062SN/A}
12752325SN/A/*
12761062SN/Atemplate <class Impl>
12771062SN/Avoid
12781755SN/AFullO3CPU<Impl>::removeAllInsts()
12791060SN/A{
12801060SN/A    instList.clear();
12811060SN/A}
12822325SN/A*/
12831060SN/Atemplate <class Impl>
12841060SN/Avoid
12851755SN/AFullO3CPU<Impl>::dumpInsts()
12861060SN/A{
12871060SN/A    int num = 0;
12881060SN/A
12892292SN/A    ListIt inst_list_it = instList.begin();
12902292SN/A
12912292SN/A    cprintf("Dumping Instruction List\n");
12922292SN/A
12932292SN/A    while (inst_list_it != instList.end()) {
12942292SN/A        cprintf("Instruction:%i\nPC:%#x\n[tid:%i]\n[sn:%lli]\nIssued:%i\n"
12952292SN/A                "Squashed:%i\n\n",
12962292SN/A                num, (*inst_list_it)->readPC(), (*inst_list_it)->threadNumber,
12972292SN/A                (*inst_list_it)->seqNum, (*inst_list_it)->isIssued(),
12982292SN/A                (*inst_list_it)->isSquashed());
12991060SN/A        inst_list_it++;
13001060SN/A        ++num;
13011060SN/A    }
13021060SN/A}
13032325SN/A/*
13041060SN/Atemplate <class Impl>
13051060SN/Avoid
13061755SN/AFullO3CPU<Impl>::wakeDependents(DynInstPtr &inst)
13071060SN/A{
13081060SN/A    iew.wakeDependents(inst);
13091060SN/A}
13102325SN/A*/
13112292SN/Atemplate <class Impl>
13122292SN/Avoid
13132292SN/AFullO3CPU<Impl>::wakeCPU()
13142292SN/A{
13152325SN/A    if (activityRec.active() || tickEvent.scheduled()) {
13162325SN/A        DPRINTF(Activity, "CPU already running.\n");
13172292SN/A        return;
13182292SN/A    }
13192292SN/A
13202325SN/A    DPRINTF(Activity, "Waking up CPU\n");
13212325SN/A
13222325SN/A    idleCycles += (curTick - 1) - lastRunningCycle;
13232292SN/A
13242292SN/A    tickEvent.schedule(curTick);
13252292SN/A}
13262292SN/A
13272292SN/Atemplate <class Impl>
13282292SN/Aint
13292292SN/AFullO3CPU<Impl>::getFreeTid()
13302292SN/A{
13312292SN/A    for (int i=0; i < numThreads; i++) {
13322292SN/A        if (!tids[i]) {
13332292SN/A            tids[i] = true;
13342292SN/A            return i;
13352292SN/A        }
13362292SN/A    }
13372292SN/A
13382292SN/A    return -1;
13392292SN/A}
13402292SN/A
13412292SN/Atemplate <class Impl>
13422292SN/Avoid
13432292SN/AFullO3CPU<Impl>::doContextSwitch()
13442292SN/A{
13452292SN/A    if (contextSwitch) {
13462292SN/A
13472292SN/A        //ADD CODE TO DEACTIVE THREAD HERE (???)
13482292SN/A
13492292SN/A        for (int tid=0; tid < cpuWaitList.size(); tid++) {
13502292SN/A            activateWhenReady(tid);
13512292SN/A        }
13522292SN/A
13532292SN/A        if (cpuWaitList.size() == 0)
13542292SN/A            contextSwitch = true;
13552292SN/A    }
13562292SN/A}
13572292SN/A
13582292SN/Atemplate <class Impl>
13592292SN/Avoid
13602292SN/AFullO3CPU<Impl>::updateThreadPriority()
13612292SN/A{
13622292SN/A    if (activeThreads.size() > 1)
13632292SN/A    {
13642292SN/A        //DEFAULT TO ROUND ROBIN SCHEME
13652292SN/A        //e.g. Move highest priority to end of thread list
13662292SN/A        list<unsigned>::iterator list_begin = activeThreads.begin();
13672292SN/A        list<unsigned>::iterator list_end   = activeThreads.end();
13682292SN/A
13692292SN/A        unsigned high_thread = *list_begin;
13702292SN/A
13712292SN/A        activeThreads.erase(list_begin);
13722292SN/A
13732292SN/A        activeThreads.push_back(high_thread);
13742292SN/A    }
13752292SN/A}
13761060SN/A
13771755SN/A// Forward declaration of FullO3CPU.
13782818Sksewell@umich.edutemplate class FullO3CPU<O3CPUImpl>;
1379