base.hh revision 676
14134Sgblack@eecs.umich.edu/*
24134Sgblack@eecs.umich.edu * Copyright (c) 2003 The Regents of The University of Michigan
34134Sgblack@eecs.umich.edu * All rights reserved.
44134Sgblack@eecs.umich.edu *
54134Sgblack@eecs.umich.edu * Redistribution and use in source and binary forms, with or without
64134Sgblack@eecs.umich.edu * modification, are permitted provided that the following conditions are
74134Sgblack@eecs.umich.edu * met: redistributions of source code must retain the above copyright
84134Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer;
94134Sgblack@eecs.umich.edu * redistributions in binary form must reproduce the above copyright
104134Sgblack@eecs.umich.edu * notice, this list of conditions and the following disclaimer in the
114134Sgblack@eecs.umich.edu * documentation and/or other materials provided with the distribution;
124134Sgblack@eecs.umich.edu * neither the name of the copyright holders nor the names of its
134134Sgblack@eecs.umich.edu * contributors may be used to endorse or promote products derived from
144134Sgblack@eecs.umich.edu * this software without specific prior written permission.
154134Sgblack@eecs.umich.edu *
164134Sgblack@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
174134Sgblack@eecs.umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
184134Sgblack@eecs.umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
194134Sgblack@eecs.umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
204134Sgblack@eecs.umich.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
214134Sgblack@eecs.umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
224134Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
234134Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
244134Sgblack@eecs.umich.edu * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
254134Sgblack@eecs.umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
264134Sgblack@eecs.umich.edu * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
274134Sgblack@eecs.umich.edu */
284134Sgblack@eecs.umich.edu
294134Sgblack@eecs.umich.edu#ifndef __SIMPLE_CPU_HH__
304134Sgblack@eecs.umich.edu#define __SIMPLE_CPU_HH__
314134Sgblack@eecs.umich.edu
324134Sgblack@eecs.umich.edu#include "cpu/base_cpu.hh"
334134Sgblack@eecs.umich.edu#include "sim/eventq.hh"
344134Sgblack@eecs.umich.edu#include "base/loader/symtab.hh"
354134Sgblack@eecs.umich.edu#include "cpu/pc_event.hh"
364134Sgblack@eecs.umich.edu#include "base/statistics.hh"
374134Sgblack@eecs.umich.edu
384134Sgblack@eecs.umich.edu
394134Sgblack@eecs.umich.edu// forward declarations
404134Sgblack@eecs.umich.edu#ifdef FULL_SYSTEM
414134Sgblack@eecs.umich.educlass Processor;
424134Sgblack@eecs.umich.educlass Kernel;
434134Sgblack@eecs.umich.educlass AlphaITB;
444134Sgblack@eecs.umich.educlass AlphaDTB;
454134Sgblack@eecs.umich.educlass PhysicalMemory;
464134Sgblack@eecs.umich.edu
474134Sgblack@eecs.umich.educlass RemoteGDB;
484134Sgblack@eecs.umich.educlass GDBListener;
494134Sgblack@eecs.umich.edu#endif // FULL_SYSTEM
504134Sgblack@eecs.umich.edu
514134Sgblack@eecs.umich.educlass MemInterface;
524134Sgblack@eecs.umich.educlass Checkpoint;
534134Sgblack@eecs.umich.edu
544134Sgblack@eecs.umich.edunamespace Trace {
554134Sgblack@eecs.umich.edu    class InstRecord;
564134Sgblack@eecs.umich.edu}
574134Sgblack@eecs.umich.edu
585149Sgblack@eecs.umich.educlass SimpleCPU : public BaseCPU
595149Sgblack@eecs.umich.edu{
604134Sgblack@eecs.umich.edu  public:
614134Sgblack@eecs.umich.edu    // main simulation loop (one cycle)
624134Sgblack@eecs.umich.edu    void tick();
634134Sgblack@eecs.umich.edu
644134Sgblack@eecs.umich.edu  private:
654578Sgblack@eecs.umich.edu    class TickEvent : public Event
665063Sgblack@eecs.umich.edu    {
674682Sgblack@eecs.umich.edu      private:
685063Sgblack@eecs.umich.edu        SimpleCPU *cpu;
695075Sgblack@eecs.umich.edu
705063Sgblack@eecs.umich.edu      public:
715063Sgblack@eecs.umich.edu        TickEvent(SimpleCPU *c);
725063Sgblack@eecs.umich.edu        void process();
735063Sgblack@eecs.umich.edu        const char *description();
745075Sgblack@eecs.umich.edu    };
754134Sgblack@eecs.umich.edu
764134Sgblack@eecs.umich.edu    TickEvent tickEvent;
774134Sgblack@eecs.umich.edu
785045Sgblack@eecs.umich.edu    /// Schedule tick event, regardless of its current state.
795025Sgblack@eecs.umich.edu    void scheduleTickEvent(int delay)
805025Sgblack@eecs.umich.edu    {
815025Sgblack@eecs.umich.edu        if (tickEvent.squashed())
825025Sgblack@eecs.umich.edu            tickEvent.reschedule(curTick + delay);
835025Sgblack@eecs.umich.edu        else if (!tickEvent.scheduled())
845025Sgblack@eecs.umich.edu            tickEvent.schedule(curTick + delay);
855149Sgblack@eecs.umich.edu    }
865149Sgblack@eecs.umich.edu
875149Sgblack@eecs.umich.edu    /// Unschedule tick event, regardless of its current state.
885149Sgblack@eecs.umich.edu    void unscheduleTickEvent()
895161Sgblack@eecs.umich.edu    {
905323Sgblack@eecs.umich.edu        if (tickEvent.scheduled())
915357Sgblack@eecs.umich.edu            tickEvent.squash();
925357Sgblack@eecs.umich.edu    }
934134Sgblack@eecs.umich.edu
944134Sgblack@eecs.umich.edu  private:
954134Sgblack@eecs.umich.edu    Trace::InstRecord *traceData;
96    template<typename T>
97    void trace_data(T data) {
98      if (traceData) {
99        traceData->setData(data);
100      }
101    };
102
103  public:
104    //
105    enum Status {
106        Running,
107        Idle,
108        IcacheMissStall,
109        IcacheMissComplete,
110        DcacheMissStall,
111        SwitchedOut
112    };
113
114  private:
115    Status _status;
116
117  public:
118    void post_interrupt(int int_num, int index);
119
120    void zero_fill_64(Addr addr) {
121      static int warned = 0;
122      if (!warned) {
123        warn ("WH64 is not implemented");
124        warned = 1;
125      }
126    };
127
128#ifdef FULL_SYSTEM
129
130    SimpleCPU(const std::string &_name,
131              System *_system,
132              Counter max_insts_any_thread, Counter max_insts_all_threads,
133              Counter max_loads_any_thread, Counter max_loads_all_threads,
134              AlphaITB *itb, AlphaDTB *dtb, FunctionalMemory *mem,
135              MemInterface *icache_interface, MemInterface *dcache_interface,
136              bool _def_reg, Tick freq);
137
138#else
139
140    SimpleCPU(const std::string &_name, Process *_process,
141              Counter max_insts_any_thread,
142              Counter max_insts_all_threads,
143              Counter max_loads_any_thread,
144              Counter max_loads_all_threads,
145              MemInterface *icache_interface, MemInterface *dcache_interface,
146              bool _def_reg);
147
148#endif
149
150    virtual ~SimpleCPU();
151    virtual void init();
152
153    // execution context
154    ExecContext *xc;
155
156    void switchOut();
157    void takeOverFrom(BaseCPU *oldCPU);
158
159#ifdef FULL_SYSTEM
160    Addr dbg_vtophys(Addr addr);
161
162    bool interval_stats;
163#endif
164
165    // L1 instruction cache
166    MemInterface *icacheInterface;
167
168    // L1 data cache
169    MemInterface *dcacheInterface;
170
171    bool defer_registration;
172
173    // current instruction
174    MachInst inst;
175
176    // Refcounted pointer to the one memory request.
177    MemReqPtr memReq;
178
179    class CacheCompletionEvent : public Event
180    {
181      private:
182        SimpleCPU *cpu;
183
184      public:
185        CacheCompletionEvent(SimpleCPU *_cpu);
186
187        virtual void process();
188        virtual const char *description();
189    };
190
191    CacheCompletionEvent cacheCompletionEvent;
192
193    Status status() const { return _status; }
194
195    virtual void activateContext(int thread_num, int delay);
196    virtual void suspendContext(int thread_num);
197    virtual void deallocateContext(int thread_num);
198    virtual void haltContext(int thread_num);
199
200    // statistics
201    virtual void regStats();
202    virtual void resetStats();
203
204    // number of simulated instructions
205    Counter numInst;
206    Counter startNumInst;
207    Statistics::Formula numInsts;
208
209    // number of simulated memory references
210    Statistics::Scalar<> numMemRefs;
211
212    // number of simulated loads
213    Counter numLoad;
214    Counter startNumLoad;
215
216    // number of idle cycles
217    Statistics::Average<> notIdleFraction;
218    Statistics::Formula idleFraction;
219
220    // number of cycles stalled for I-cache misses
221    Statistics::Scalar<> icacheStallCycles;
222    Counter lastIcacheStall;
223
224    // number of cycles stalled for D-cache misses
225    Statistics::Scalar<> dcacheStallCycles;
226    Counter lastDcacheStall;
227
228    void processCacheCompletion();
229
230    virtual void serialize(std::ostream &os);
231    virtual void unserialize(Checkpoint *cp, const std::string &section);
232
233    template <class T>
234    Fault read(Addr addr, T &data, unsigned flags);
235
236    template <class T>
237    Fault write(T data, Addr addr, unsigned flags,
238                        uint64_t *res);
239
240    void prefetch(Addr addr, unsigned flags)
241    {
242        // need to do this...
243    }
244
245    void writeHint(Addr addr, int size)
246    {
247        // need to do this...
248    }
249
250    Fault copySrcTranslate(Addr src);
251
252    Fault copy(Addr dest);
253};
254
255#endif // __SIMPLE_CPU_HH__
256