isa.cc revision 10037
14120Sgblack@eecs.umich.edu/*
24120Sgblack@eecs.umich.edu * Copyright (c) 2009 The Regents of The University of Michigan
34120Sgblack@eecs.umich.edu * All rights reserved.
44120Sgblack@eecs.umich.edu *
54120Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
64120Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
74120Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
84120Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
94120Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
104120Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
114120Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
124120Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
134120Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
144120Sgblack@eecs.umich.edu * this software without specific prior written permission.
154120Sgblack@eecs.umich.edu *
164120Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
174120Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
184120Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
194120Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
204120Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
214120Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
224120Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
234120Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
244120Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
254120Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
264120Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
274120Sgblack@eecs.umich.edu *
284120Sgblack@eecs.umich.edu * Authors: Gabe Black
294120Sgblack@eecs.umich.edu */
304120Sgblack@eecs.umich.edu
314120Sgblack@eecs.umich.edu#include "arch/mips/isa.hh"
324120Sgblack@eecs.umich.edu#include "arch/mips/mt.hh"
334120Sgblack@eecs.umich.edu#include "arch/mips/mt_constants.hh"
344120Sgblack@eecs.umich.edu#include "arch/mips/pra_constants.hh"
354120Sgblack@eecs.umich.edu#include "base/bitfield.hh"
364120Sgblack@eecs.umich.edu#include "cpu/base.hh"
374120Sgblack@eecs.umich.edu#include "cpu/thread_context.hh"
384120Sgblack@eecs.umich.edu#include "debug/MipsPRA.hh"
394120Sgblack@eecs.umich.edu#include "params/MipsISA.hh"
404120Sgblack@eecs.umich.edu
414120Sgblack@eecs.umich.edunamespace MipsISA
424120Sgblack@eecs.umich.edu{
434120Sgblack@eecs.umich.edu
444120Sgblack@eecs.umich.edustd::string
454120Sgblack@eecs.umich.eduISA::miscRegNames[NumMiscRegs] =
464120Sgblack@eecs.umich.edu{
474120Sgblack@eecs.umich.edu    "Index", "MVPControl", "MVPConf0", "MVPConf1", "", "", "", "",
484120Sgblack@eecs.umich.edu    "Random", "VPEControl", "VPEConf0", "VPEConf1",
494120Sgblack@eecs.umich.edu        "YQMask", "VPESchedule", "VPEScheFBack", "VPEOpt",
504120Sgblack@eecs.umich.edu    "EntryLo0", "TCStatus", "TCBind", "TCRestart",
514120Sgblack@eecs.umich.edu        "TCHalt", "TCContext", "TCSchedule", "TCScheFBack",
524120Sgblack@eecs.umich.edu    "EntryLo1", "", "", "", "", "", "", "",
534120Sgblack@eecs.umich.edu    "Context", "ContextConfig", "", "", "", "", "", "",
544120Sgblack@eecs.umich.edu    "PageMask", "PageGrain", "", "", "", "", "", "",
554120Sgblack@eecs.umich.edu    "Wired", "SRSConf0", "SRCConf1", "SRSConf2",
564120Sgblack@eecs.umich.edu        "SRSConf3", "SRSConf4", "", "",
574120Sgblack@eecs.umich.edu    "HWREna", "", "", "", "", "", "", "",
584120Sgblack@eecs.umich.edu    "BadVAddr", "", "", "", "", "", "", "",
594120Sgblack@eecs.umich.edu    "Count", "", "", "", "", "", "", "",
604120Sgblack@eecs.umich.edu    "EntryHi", "", "", "", "", "", "", "",
614166Sgblack@eecs.umich.edu    "Compare", "", "", "", "", "", "", "",
624141Sgblack@eecs.umich.edu    "Status", "IntCtl", "SRSCtl", "SRSMap", "", "", "", "",
634136Sgblack@eecs.umich.edu    "Cause", "", "", "", "", "", "", "",
644136Sgblack@eecs.umich.edu    "EPC", "", "", "", "", "", "", "",
654141Sgblack@eecs.umich.edu    "PRId", "EBase", "", "", "", "", "", "",
664141Sgblack@eecs.umich.edu    "Config", "Config1", "Config2", "Config3", "", "", "", "",
674121Sgblack@eecs.umich.edu    "LLAddr", "", "", "", "", "", "", "",
684120Sgblack@eecs.umich.edu    "WatchLo0", "WatchLo1", "WatchLo2", "WatchLo3",
694120Sgblack@eecs.umich.edu        "WatchLo4", "WatchLo5", "WatchLo6", "WatchLo7",
704120Sgblack@eecs.umich.edu    "WatchHi0", "WatchHi1", "WatchHi2", "WatchHi3",
714121Sgblack@eecs.umich.edu        "WatchHi4", "WatchHi5", "WatchHi6", "WatchHi7",
724121Sgblack@eecs.umich.edu    "XCContext64", "", "", "", "", "", "", "",
734121Sgblack@eecs.umich.edu    "", "", "", "", "", "", "", "",
744121Sgblack@eecs.umich.edu    "", "", "", "", "", "", "", "",
754121Sgblack@eecs.umich.edu    "Debug", "TraceControl1", "TraceControl2", "UserTraceData",
764121Sgblack@eecs.umich.edu        "TraceBPC", "", "", "",
774121Sgblack@eecs.umich.edu    "DEPC", "", "", "", "", "", "", "",
784121Sgblack@eecs.umich.edu    "PerfCnt0", "PerfCnt1", "PerfCnt2", "PerfCnt3",
794121Sgblack@eecs.umich.edu        "PerfCnt4", "PerfCnt5", "PerfCnt6", "PerfCnt7",
804121Sgblack@eecs.umich.edu    "ErrCtl", "", "", "", "", "", "", "",
814121Sgblack@eecs.umich.edu    "CacheErr0", "CacheErr1", "CacheErr2", "CacheErr3", "", "", "", "",
824121Sgblack@eecs.umich.edu    "TagLo0", "DataLo1", "TagLo2", "DataLo3",
834121Sgblack@eecs.umich.edu        "TagLo4", "DataLo5", "TagLo6", "DataLo7",
844581Sgblack@eecs.umich.edu    "TagHi0", "DataHi1", "TagHi2", "DataHi3",
854581Sgblack@eecs.umich.edu        "TagHi4", "DataHi5", "TagHi6", "DataHi7",
864121Sgblack@eecs.umich.edu    "ErrorEPC", "", "", "", "", "", "", "",
874121Sgblack@eecs.umich.edu    "DESAVE", "", "", "", "", "", "", "",
884121Sgblack@eecs.umich.edu    "LLFlag"
894121Sgblack@eecs.umich.edu};
904121Sgblack@eecs.umich.edu
914121Sgblack@eecs.umich.eduISA::ISA(Params *p)
924121Sgblack@eecs.umich.edu    : SimObject(p), numThreads(p->num_threads), numVpes(p->num_vpes)
934121Sgblack@eecs.umich.edu{
944121Sgblack@eecs.umich.edu    miscRegFile.resize(NumMiscRegs);
954121Sgblack@eecs.umich.edu    bankType.resize(NumMiscRegs);
964121Sgblack@eecs.umich.edu
974166Sgblack@eecs.umich.edu    for (int i=0; i < NumMiscRegs; i++) {
984121Sgblack@eecs.umich.edu        miscRegFile[i].resize(1);
994121Sgblack@eecs.umich.edu        bankType[i] = perProcessor;
1004166Sgblack@eecs.umich.edu    }
1014166Sgblack@eecs.umich.edu
1024166Sgblack@eecs.umich.edu    miscRegFile_WriteMask.resize(NumMiscRegs);
1034166Sgblack@eecs.umich.edu
1044166Sgblack@eecs.umich.edu    for (int i = 0; i < NumMiscRegs; i++) {
1054166Sgblack@eecs.umich.edu        miscRegFile_WriteMask[i].push_back(0);
1064166Sgblack@eecs.umich.edu    }
1074166Sgblack@eecs.umich.edu
1084121Sgblack@eecs.umich.edu    // Initialize all Per-VPE regs
1094121Sgblack@eecs.umich.edu    uint32_t per_vpe_regs[] = { MISCREG_VPE_CONTROL,
1104121Sgblack@eecs.umich.edu                                MISCREG_VPE_CONF0, MISCREG_VPE_CONF1,
1114166Sgblack@eecs.umich.edu                                MISCREG_YQMASK,
1124121Sgblack@eecs.umich.edu                                MISCREG_VPE_SCHEDULE, MISCREG_VPE_SCHEFBACK,
1134121Sgblack@eecs.umich.edu                                MISCREG_VPE_OPT, MISCREG_SRS_CONF0,
1144121Sgblack@eecs.umich.edu                                MISCREG_SRS_CONF1, MISCREG_SRS_CONF2,
1154121Sgblack@eecs.umich.edu                                MISCREG_SRS_CONF3, MISCREG_SRS_CONF4,
1164121Sgblack@eecs.umich.edu                                MISCREG_EBASE
1174121Sgblack@eecs.umich.edu                              };
1184141Sgblack@eecs.umich.edu    uint32_t num_vpe_regs = sizeof(per_vpe_regs) / 4;
1194141Sgblack@eecs.umich.edu    for (int i = 0; i < num_vpe_regs; i++) {
1204141Sgblack@eecs.umich.edu        if (numVpes > 1) {
1214141Sgblack@eecs.umich.edu            miscRegFile[per_vpe_regs[i]].resize(numVpes);
1224141Sgblack@eecs.umich.edu        }
1234121Sgblack@eecs.umich.edu        bankType[per_vpe_regs[i]] = perVirtProcessor;
1244121Sgblack@eecs.umich.edu    }
1254141Sgblack@eecs.umich.edu
1264141Sgblack@eecs.umich.edu    // Initialize all Per-TC regs
1274120Sgblack@eecs.umich.edu    uint32_t per_tc_regs[] = { MISCREG_STATUS,
1284120Sgblack@eecs.umich.edu                               MISCREG_TC_STATUS, MISCREG_TC_BIND,
1294120Sgblack@eecs.umich.edu                               MISCREG_TC_RESTART, MISCREG_TC_HALT,
130                               MISCREG_TC_CONTEXT, MISCREG_TC_SCHEDULE,
131                               MISCREG_TC_SCHEFBACK,
132                               MISCREG_DEBUG, MISCREG_LLADDR
133                             };
134    uint32_t num_tc_regs = sizeof(per_tc_regs) /  4;
135
136    for (int i = 0; i < num_tc_regs; i++) {
137        miscRegFile[per_tc_regs[i]].resize(numThreads);
138        bankType[per_tc_regs[i]] = perThreadContext;
139    }
140
141    clear();
142}
143
144const MipsISAParams *
145ISA::params() const
146{
147    return dynamic_cast<const Params *>(_params);
148}
149
150void
151ISA::clear()
152{
153    for(int i = 0; i < NumMiscRegs; i++) {
154        for (int j = 0; j < miscRegFile[i].size(); j++)
155            miscRegFile[i][j] = 0;
156
157        for (int k = 0; k < miscRegFile_WriteMask[i].size(); k++)
158            miscRegFile_WriteMask[i][k] = (long unsigned int)(-1);
159    }
160}
161
162
163void
164ISA::configCP()
165{
166    DPRINTF(MipsPRA, "Resetting CP0 State with %i TCs and %i VPEs\n",
167            numThreads, numVpes);
168
169    CoreSpecific cp;
170    panic("CP state must be set before the following code is used");
171
172    // Do Default CP0 initialization HERE
173
174    // Do Initialization for MT cores here (eventually use
175    // core_name parameter to toggle this initialization)
176    // ===================================================
177    DPRINTF(MipsPRA, "Initializing CP0 State.... ");
178
179    PRIdReg procId = readMiscRegNoEffect(MISCREG_PRID);
180    procId.coOp = cp.CP0_PRId_CompanyOptions;
181    procId.coId = cp.CP0_PRId_CompanyID;
182    procId.procId = cp.CP0_PRId_ProcessorID;
183    procId.rev = cp.CP0_PRId_Revision;
184    setMiscRegNoEffect(MISCREG_PRID, procId);
185
186    // Now, create Write Mask for ProcID register
187    MiscReg procIDMask = 0; // Read-Only register
188    replaceBits(procIDMask, 0, 32, 0);
189    setRegMask(MISCREG_PRID, procIDMask);
190
191    // Config
192    ConfigReg cfg = readMiscRegNoEffect(MISCREG_CONFIG);
193    cfg.be = cp.CP0_Config_BE;
194    cfg.at = cp.CP0_Config_AT;
195    cfg.ar = cp.CP0_Config_AR;
196    cfg.mt = cp.CP0_Config_MT;
197    cfg.vi = cp.CP0_Config_VI;
198    cfg.m = 1;
199    setMiscRegNoEffect(MISCREG_CONFIG, cfg);
200    // Now, create Write Mask for Config register
201    MiscReg cfg_Mask = 0x7FFF0007;
202    replaceBits(cfg_Mask, 0, 32, 0);
203    setRegMask(MISCREG_CONFIG, cfg_Mask);
204
205    // Config1
206    Config1Reg cfg1 = readMiscRegNoEffect(MISCREG_CONFIG1);
207    cfg1.mmuSize = cp.CP0_Config1_MMU;
208    cfg1.is = cp.CP0_Config1_IS;
209    cfg1.il = cp.CP0_Config1_IL;
210    cfg1.ia = cp.CP0_Config1_IA;
211    cfg1.ds = cp.CP0_Config1_DS;
212    cfg1.dl = cp.CP0_Config1_DL;
213    cfg1.da = cp.CP0_Config1_DA;
214    cfg1.fp = cp.CP0_Config1_FP;
215    cfg1.ep = cp.CP0_Config1_EP;
216    cfg1.wr = cp.CP0_Config1_WR;
217    cfg1.md = cp.CP0_Config1_MD;
218    cfg1.c2 = cp.CP0_Config1_C2;
219    cfg1.pc = cp.CP0_Config1_PC;
220    cfg1.m = cp.CP0_Config1_M;
221    setMiscRegNoEffect(MISCREG_CONFIG1, cfg1);
222    // Now, create Write Mask for Config register
223    MiscReg cfg1_Mask = 0; // Read Only Register
224    replaceBits(cfg1_Mask, 0, 32, 0);
225    setRegMask(MISCREG_CONFIG1, cfg1_Mask);
226
227    // Config2
228    Config2Reg cfg2 = readMiscRegNoEffect(MISCREG_CONFIG2);
229    cfg2.tu = cp.CP0_Config2_TU;
230    cfg2.ts = cp.CP0_Config2_TS;
231    cfg2.tl = cp.CP0_Config2_TL;
232    cfg2.ta = cp.CP0_Config2_TA;
233    cfg2.su = cp.CP0_Config2_SU;
234    cfg2.ss = cp.CP0_Config2_SS;
235    cfg2.sl = cp.CP0_Config2_SL;
236    cfg2.sa = cp.CP0_Config2_SA;
237    cfg2.m = cp.CP0_Config2_M;
238    setMiscRegNoEffect(MISCREG_CONFIG2, cfg2);
239    // Now, create Write Mask for Config register
240    MiscReg cfg2_Mask = 0x7000F000; // Read Only Register
241    replaceBits(cfg2_Mask, 0, 32, 0);
242    setRegMask(MISCREG_CONFIG2, cfg2_Mask);
243
244    // Config3
245    Config3Reg cfg3 = readMiscRegNoEffect(MISCREG_CONFIG3);
246    cfg3.dspp = cp.CP0_Config3_DSPP;
247    cfg3.lpa = cp.CP0_Config3_LPA;
248    cfg3.veic = cp.CP0_Config3_VEIC;
249    cfg3.vint = cp.CP0_Config3_VInt;
250    cfg3.sp = cp.CP0_Config3_SP;
251    cfg3.mt = cp.CP0_Config3_MT;
252    cfg3.sm = cp.CP0_Config3_SM;
253    cfg3.tl = cp.CP0_Config3_TL;
254    setMiscRegNoEffect(MISCREG_CONFIG3, cfg3);
255    // Now, create Write Mask for Config register
256    MiscReg cfg3_Mask = 0; // Read Only Register
257    replaceBits(cfg3_Mask, 0, 32, 0);
258    setRegMask(MISCREG_CONFIG3, cfg3_Mask);
259
260    // EBase - CPUNum
261    EBaseReg eBase = readMiscRegNoEffect(MISCREG_EBASE);
262    eBase.cpuNum = cp.CP0_EBase_CPUNum;
263    replaceBits(eBase, 31, 31, 1);
264    setMiscRegNoEffect(MISCREG_EBASE, eBase);
265    // Now, create Write Mask for Config register
266    MiscReg EB_Mask = 0x3FFFF000;// Except Exception Base, the
267                                 // entire register is read only
268    replaceBits(EB_Mask, 0, 32, 0);
269    setRegMask(MISCREG_EBASE, EB_Mask);
270
271    // SRS Control - HSS (Highest Shadow Set)
272    SRSCtlReg scsCtl = readMiscRegNoEffect(MISCREG_SRSCTL);
273    scsCtl.hss = cp.CP0_SrsCtl_HSS;
274    setMiscRegNoEffect(MISCREG_SRSCTL, scsCtl);
275    // Now, create Write Mask for the SRS Ctl register
276    MiscReg SC_Mask = 0x0000F3C0;
277    replaceBits(SC_Mask, 0, 32, 0);
278    setRegMask(MISCREG_SRSCTL, SC_Mask);
279
280    // IntCtl - IPTI, IPPCI
281    IntCtlReg intCtl = readMiscRegNoEffect(MISCREG_INTCTL);
282    intCtl.ipti = cp.CP0_IntCtl_IPTI;
283    intCtl.ippci = cp.CP0_IntCtl_IPPCI;
284    setMiscRegNoEffect(MISCREG_INTCTL, intCtl);
285    // Now, create Write Mask for the IntCtl register
286    MiscReg IC_Mask = 0x000003E0;
287    replaceBits(IC_Mask, 0, 32, 0);
288    setRegMask(MISCREG_INTCTL, IC_Mask);
289
290    // Watch Hi - M - FIXME (More than 1 Watch register)
291    WatchHiReg watchHi = readMiscRegNoEffect(MISCREG_WATCHHI0);
292    watchHi.m = cp.CP0_WatchHi_M;
293    setMiscRegNoEffect(MISCREG_WATCHHI0, watchHi);
294    // Now, create Write Mask for the IntCtl register
295    MiscReg wh_Mask = 0x7FFF0FFF;
296    replaceBits(wh_Mask, 0, 32, 0);
297    setRegMask(MISCREG_WATCHHI0, wh_Mask);
298
299    // Perf Ctr - M - FIXME (More than 1 PerfCnt Pair)
300    PerfCntCtlReg perfCntCtl = readMiscRegNoEffect(MISCREG_PERFCNT0);
301    perfCntCtl.m = cp.CP0_PerfCtr_M;
302    perfCntCtl.w = cp.CP0_PerfCtr_W;
303    setMiscRegNoEffect(MISCREG_PERFCNT0, perfCntCtl);
304    // Now, create Write Mask for the IntCtl register
305    MiscReg pc_Mask = 0x00007FF;
306    replaceBits(pc_Mask, 0, 32, 0);
307    setRegMask(MISCREG_PERFCNT0, pc_Mask);
308
309    // Random
310    setMiscRegNoEffect(MISCREG_CP0_RANDOM, 63);
311    // Now, create Write Mask for the IntCtl register
312    MiscReg random_Mask = 0;
313    replaceBits(random_Mask, 0, 32, 0);
314    setRegMask(MISCREG_CP0_RANDOM, random_Mask);
315
316    // PageGrain
317    PageGrainReg pageGrain = readMiscRegNoEffect(MISCREG_PAGEGRAIN);
318    pageGrain.esp = cp.CP0_Config3_SP;
319    setMiscRegNoEffect(MISCREG_PAGEGRAIN, pageGrain);
320    // Now, create Write Mask for the IntCtl register
321    MiscReg pg_Mask = 0x10000000;
322    replaceBits(pg_Mask, 0, 32, 0);
323    setRegMask(MISCREG_PAGEGRAIN, pg_Mask);
324
325    // Status
326    StatusReg status = readMiscRegNoEffect(MISCREG_STATUS);
327    // Only CU0 and IE are modified on a reset - everything else needs
328    // to be controlled on a per CPU model basis
329
330    // Enable CP0 on reset
331    // status.cu0 = 1;
332
333    // Enable ERL bit on a reset
334    status.erl = 1;
335    // Enable BEV bit on a reset
336    status.bev = 1;
337
338    setMiscRegNoEffect(MISCREG_STATUS, status);
339    // Now, create Write Mask for the Status register
340    MiscReg stat_Mask = 0xFF78FF17;
341    replaceBits(stat_Mask, 0, 32, 0);
342    setRegMask(MISCREG_STATUS, stat_Mask);
343
344
345    // MVPConf0
346    MVPConf0Reg mvpConf0 = readMiscRegNoEffect(MISCREG_MVP_CONF0);
347    mvpConf0.tca = 1;
348    mvpConf0.pvpe = numVpes - 1;
349    mvpConf0.ptc = numThreads - 1;
350    setMiscRegNoEffect(MISCREG_MVP_CONF0, mvpConf0);
351
352    // VPEConf0
353    VPEConf0Reg vpeConf0 = readMiscRegNoEffect(MISCREG_VPE_CONF0);
354    vpeConf0.mvp = 1;
355    setMiscRegNoEffect(MISCREG_VPE_CONF0, vpeConf0);
356
357    // TCBind
358    for (ThreadID tid = 0; tid < numThreads; tid++) {
359        TCBindReg tcBind = readMiscRegNoEffect(MISCREG_TC_BIND, tid);
360        tcBind.curTC = tid;
361        setMiscRegNoEffect(MISCREG_TC_BIND, tcBind, tid);
362    }
363    // TCHalt
364    TCHaltReg tcHalt = readMiscRegNoEffect(MISCREG_TC_HALT);
365    tcHalt.h = 0;
366    setMiscRegNoEffect(MISCREG_TC_HALT, tcHalt);
367
368    // TCStatus
369    // Set TCStatus Activated to 1 for the initial thread that is running
370    TCStatusReg tcStatus = readMiscRegNoEffect(MISCREG_TC_STATUS);
371    tcStatus.a = 1;
372    setMiscRegNoEffect(MISCREG_TC_STATUS, tcStatus);
373
374    // Set Dynamically Allocatable bit to 1 for all other threads
375    for (ThreadID tid = 1; tid < numThreads; tid++) {
376        tcStatus = readMiscRegNoEffect(MISCREG_TC_STATUS, tid);
377        tcStatus.da = 1;
378        setMiscRegNoEffect(MISCREG_TC_STATUS, tcStatus, tid);
379    }
380
381
382    MiscReg mask = 0x7FFFFFFF;
383
384    // Now, create Write Mask for the Index register
385    replaceBits(mask, 0, 32, 0);
386    setRegMask(MISCREG_INDEX, mask);
387
388    mask = 0x3FFFFFFF;
389    replaceBits(mask, 0, 32, 0);
390    setRegMask(MISCREG_ENTRYLO0, mask);
391    setRegMask(MISCREG_ENTRYLO1, mask);
392
393    mask = 0xFF800000;
394    replaceBits(mask, 0, 32, 0);
395    setRegMask(MISCREG_CONTEXT, mask);
396
397    mask = 0x1FFFF800;
398    replaceBits(mask, 0, 32, 0);
399    setRegMask(MISCREG_PAGEMASK, mask);
400
401    mask = 0x0;
402    replaceBits(mask, 0, 32, 0);
403    setRegMask(MISCREG_BADVADDR, mask);
404    setRegMask(MISCREG_LLADDR, mask);
405
406    mask = 0x08C00300;
407    replaceBits(mask, 0, 32, 0);
408    setRegMask(MISCREG_CAUSE, mask);
409
410}
411
412inline unsigned
413ISA::getVPENum(ThreadID tid)
414{
415    TCBindReg tcBind = miscRegFile[MISCREG_TC_BIND][tid];
416    return tcBind.curVPE;
417}
418
419MiscReg
420ISA::readMiscRegNoEffect(int misc_reg, ThreadID tid)
421{
422    unsigned reg_sel = (bankType[misc_reg] == perThreadContext)
423        ? tid : getVPENum(tid);
424    DPRINTF(MipsPRA, "Reading CP0 Register:%u Select:%u (%s) (%lx).\n",
425            misc_reg / 8, misc_reg % 8, miscRegNames[misc_reg],
426            miscRegFile[misc_reg][reg_sel]);
427    return miscRegFile[misc_reg][reg_sel];
428}
429
430//@TODO: MIPS MT's register view automatically connects
431//       Status to TCStatus depending on current thread
432//template <class TC>
433MiscReg
434ISA::readMiscReg(int misc_reg, ThreadContext *tc,  ThreadID tid)
435{
436    unsigned reg_sel = (bankType[misc_reg] == perThreadContext)
437        ? tid : getVPENum(tid);
438    DPRINTF(MipsPRA,
439            "Reading CP0 Register:%u Select:%u (%s) with effect (%lx).\n",
440            misc_reg / 8, misc_reg % 8, miscRegNames[misc_reg],
441            miscRegFile[misc_reg][reg_sel]);
442
443    return miscRegFile[misc_reg][reg_sel];
444}
445
446void
447ISA::setMiscRegNoEffect(int misc_reg, const MiscReg &val, ThreadID tid)
448{
449    unsigned reg_sel = (bankType[misc_reg] == perThreadContext)
450        ? tid : getVPENum(tid);
451    DPRINTF(MipsPRA,
452            "[tid:%i]: Setting (direct set) CP0 Register:%u "
453            "Select:%u (%s) to %#x.\n",
454            tid, misc_reg / 8, misc_reg % 8, miscRegNames[misc_reg], val);
455
456    miscRegFile[misc_reg][reg_sel] = val;
457}
458
459void
460ISA::setRegMask(int misc_reg, const MiscReg &val, ThreadID tid)
461{
462    unsigned reg_sel = (bankType[misc_reg] == perThreadContext)
463        ? tid : getVPENum(tid);
464    DPRINTF(MipsPRA,
465            "[tid:%i]: Setting CP0 Register: %u Select: %u (%s) to %#x\n",
466            tid, misc_reg / 8, misc_reg % 8, miscRegNames[misc_reg], val);
467    miscRegFile_WriteMask[misc_reg][reg_sel] = val;
468}
469
470// PROGRAMMER'S NOTES:
471// (1) Some CP0 Registers have fields that cannot
472// be overwritten. Make sure to handle those particular registers
473// with care!
474void
475ISA::setMiscReg(int misc_reg, const MiscReg &val,
476                    ThreadContext *tc, ThreadID tid)
477{
478    int reg_sel = (bankType[misc_reg] == perThreadContext)
479        ? tid : getVPENum(tid);
480
481    DPRINTF(MipsPRA,
482            "[tid:%i]: Setting CP0 Register:%u "
483            "Select:%u (%s) to %#x, with effect.\n",
484            tid, misc_reg / 8, misc_reg % 8, miscRegNames[misc_reg], val);
485
486    MiscReg cp0_val = filterCP0Write(misc_reg, reg_sel, val);
487
488    miscRegFile[misc_reg][reg_sel] = cp0_val;
489
490    scheduleCP0Update(tc->getCpuPtr(), Cycles(1));
491}
492
493/**
494 * This method doesn't need to adjust the Control Register Offset
495 * since it has already been done in the calling method
496 * (setRegWithEffect)
497*/
498MiscReg
499ISA::filterCP0Write(int misc_reg, int reg_sel, const MiscReg &val)
500{
501    MiscReg retVal = val;
502
503    // Mask off read-only regions
504    retVal &= miscRegFile_WriteMask[misc_reg][reg_sel];
505    MiscReg curVal = miscRegFile[misc_reg][reg_sel];
506    // Mask off current alue with inverse mask (clear writeable bits)
507    curVal &= (~miscRegFile_WriteMask[misc_reg][reg_sel]);
508    retVal |= curVal; // Combine the two
509    DPRINTF(MipsPRA,
510            "filterCP0Write: Mask: %lx, Inverse Mask: %lx, write Val: %x, "
511            "current val: %lx, written val: %x\n",
512            miscRegFile_WriteMask[misc_reg][reg_sel],
513            ~miscRegFile_WriteMask[misc_reg][reg_sel],
514            val, miscRegFile[misc_reg][reg_sel], retVal);
515    return retVal;
516}
517
518void
519ISA::scheduleCP0Update(BaseCPU *cpu, Cycles delay)
520{
521    if (!cp0Updated) {
522        cp0Updated = true;
523
524        //schedule UPDATE
525        CP0Event *cp0_event = new CP0Event(this, cpu, UpdateCP0);
526        cpu->schedule(cp0_event, cpu->clockEdge(delay));
527    }
528}
529
530void
531ISA::updateCPU(BaseCPU *cpu)
532{
533    ///////////////////////////////////////////////////////////////////
534    //
535    // EVALUATE CP0 STATE FOR MIPS MT
536    //
537    ///////////////////////////////////////////////////////////////////
538    MVPConf0Reg mvpConf0 = readMiscRegNoEffect(MISCREG_MVP_CONF0);
539    ThreadID num_threads = mvpConf0.ptc + 1;
540
541    for (ThreadID tid = 0; tid < num_threads; tid++) {
542        TCStatusReg tcStatus = readMiscRegNoEffect(MISCREG_TC_STATUS, tid);
543        TCHaltReg tcHalt = readMiscRegNoEffect(MISCREG_TC_HALT, tid);
544
545        //@todo: add vpe/mt check here thru mvpcontrol & vpecontrol regs
546        if (tcHalt.h == 1 || tcStatus.a == 0)  {
547            haltThread(cpu->getContext(tid));
548        } else if (tcHalt.h == 0 && tcStatus.a == 1) {
549            restoreThread(cpu->getContext(tid));
550        }
551    }
552
553    num_threads = mvpConf0.ptc + 1;
554
555    // Toggle update flag after we finished updating
556    cp0Updated = false;
557}
558
559ISA::CP0Event::CP0Event(CP0 *_cp0, BaseCPU *_cpu, CP0EventType e_type)
560    : Event(CPU_Tick_Pri), cp0(_cp0), cpu(_cpu), cp0EventType(e_type)
561{  }
562
563void
564ISA::CP0Event::process()
565{
566    switch (cp0EventType)
567    {
568      case UpdateCP0:
569        cp0->updateCPU(cpu);
570        break;
571    }
572}
573
574const char *
575ISA::CP0Event::description() const
576{
577    return "Coprocessor-0 event";
578}
579
580void
581ISA::CP0Event::scheduleEvent(Cycles delay)
582{
583    cpu->reschedule(this, cpu->clockEdge(delay), true);
584}
585
586void
587ISA::CP0Event::unscheduleEvent()
588{
589    if (scheduled())
590        squash();
591}
592
593}
594
595MipsISA::ISA *
596MipsISAParams::create()
597{
598    return new MipsISA::ISA(this);
599}
600