CPUComponent.h Source File

Back to the index.

CPUComponent.h
Go to the documentation of this file.
1 #ifndef CPUCOMPONENT_H
2 #define CPUCOMPONENT_H
3 
4 /*
5  * Copyright (C) 2008-2010 Anders Gavare. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  * derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 // Note: Not included in the component registry.
32 
33 
34 #include "AddressDataBus.h"
35 #include "Component.h"
36 #include "SymbolRegistry.h"
37 #include "UnitTest.h"
38 
39 
40 /**
41  * \brief A base-class for processors Component implementations.
42  */
44  : public Component
45  , public AddressDataBus
46  , public UnitTestable
47 {
48 public:
49  /**
50  * \brief Constructs a CPUComponent.
51  *
52  * @param className The class name for the component.
53  * @param cpuKind The CPU kind, e.g. "MIPS R4400" for a
54  * MIPS R4400 processor.
55  */
56  CPUComponent(const string& className, const string& cpuKind);
57 
58  /**
59  * \brief Gets a reference to the CPU's symbol registry.
60  *
61  * @return A reference to the symbol registry.
62  */
64  {
65  return m_symbolRegistry;
66  }
68  {
69  return m_symbolRegistry;
70  }
71 
72  virtual void ResetState();
73 
74  virtual double GetCurrentFrequency() const;
75 
76  virtual CPUComponent* AsCPUComponent();
77 
78  virtual void GetMethodNames(vector<string>& names) const;
79 
80  virtual bool MethodMayBeReexecutedWithoutArgs(const string& methodName) const;
81 
82  virtual void ExecuteMethod(GXemul* gxemul,
83  const string& methodName,
84  const vector<string>& arguments);
85 
87 
88  /* Implementation of AddressDataBus: */
89  virtual void AddressSelect(uint64_t address);
90  virtual bool ReadData(uint8_t& data, Endianness endianness);
91  virtual bool ReadData(uint16_t& data, Endianness endianness);
92  virtual bool ReadData(uint32_t& data, Endianness endianness);
93  virtual bool ReadData(uint64_t& data, Endianness endianness);
94  virtual bool WriteData(const uint8_t& data, Endianness endianness);
95  virtual bool WriteData(const uint16_t& data, Endianness endianness);
96  virtual bool WriteData(const uint32_t& data, Endianness endianness);
97  virtual bool WriteData(const uint64_t& data, Endianness endianness);
98 
99  /**
100  * \brief Disassembles an instruction into readable strings.
101  *
102  * @param vaddr The virtual address of the program counter.
103  * @param maxLen The number of bytes in the instruction buffer.
104  * @param instruction A pointer to a buffer containing the instruction.
105  * @param result A vector where the implementation will add:
106  * <ol>
107  * <li>machine code bytes in a standard notation
108  * <li>instruction mnemonic
109  * <li>instruction arguments
110  * <li>instruction comments
111  * </ol>
112  * All of the fields above are optional, but they have to be
113  * specified in the same order for a particular CPU implementation,
114  * so that the fields of the vector can be listed in a tabular
115  * format.
116  * @return The number of bytes that the instruction occupied.
117  */
118  virtual size_t DisassembleInstruction(uint64_t vaddr, size_t maxLen,
119  unsigned char *instruction, vector<string>& result) = 0;
120 
121 
122  /********************************************************************/
123 
124  static void RunUnitTests(int& nSucceeded, int& nFailures);
125 
126 protected:
127  virtual void FlushCachedStateForComponent();
128  virtual bool PreRunCheckForComponent(GXemul* gxemul);
129  virtual void ShowRegisters(GXemul* gxemul, const vector<string>& arguments) const;
130 
131  uint64_t Unassemble(int nRows, bool indicatePC, uint64_t vaddr, ostream& output);
132 
133  /**
134  * \brief Virtual to physical address translation (MMU).
135  *
136  * This function should be overridden in each CPU implementation.
137  *
138  * @param vaddr The virtual address to translate.
139  * @param paddr The return value; physical address.
140  * @param writable This is set to true or false by the function,
141  * depending on if the memory at the virtual address was
142  * writable or not.
143  * @return True if the translation succeeded, false if there was a
144  * translation error.
145  */
146  virtual bool VirtualToPhysical(uint64_t vaddr, uint64_t& paddr,
147  bool& writable) = 0;
148 
149  /**
150  * \brief Format a virtual address as a displayable string.
151  *
152  * This function may be overridden in each CPU implementation.
153  * The default implementation just uses the stringstream << operator.
154  *
155  * @param vaddr The virtual address to translate.
156  * @return A string rendering of the virtual address, e.g. "0x00100f00"
157  */
158  virtual string VirtualAddressAsString(uint64_t vaddr)
159  {
160  stringstream ss;
161  ss.flags(std::ios::hex | std::ios::showbase);
162  ss << vaddr;
163  return ss.str();
164  }
165 
166  /**
167  * \brief Convert PC value to instuction address.
168  *
169  * Usually, this does not need to be overridden. However, some
170  * architectures use e.g. the lowest bit of the PC register to indicate
171  * a different encoding mode (MIPS16), but the instruction is still
172  * aligned as if the lowest bit was 0.
173  */
174  virtual uint64_t PCtoInstructionAddress(uint64_t pc)
175  {
176  return pc;
177  }
178 
179  // CPUComponent:
180  bool FunctionTraceCall();
181  bool FunctionTraceReturn();
182 
183  // Overridden by each CPU architecture:
184  virtual int FunctionTraceArgumentCount() { return 0; }
185  virtual int64_t FunctionTraceArgument(int n) { return 0; }
186  virtual bool FunctionTraceReturnImpl(int64_t& retval) { return false; }
187 
188 private:
189  bool LookupAddressDataBus(GXemul* gxemul = NULL);
190 
191 protected:
192  /*
193  * Variables common to all (or most) kinds of CPUs:
194  */
195 
196  // Framework frequency/runability:
197  double m_frequency;
198  bool m_paused;
199 
200  // Architecture fundamentals:
203 
204  // Program counter:
205  uint64_t m_pc;
206 
207  // Memory dump and disassembly:
208  uint64_t m_lastDumpAddr;
211 
212  // Endianness:
214 
215  // Function call trace:
220 
221  // Delay slot related:
224 
225 
226  /*
227  * Cached/volatile state:
228  */
230  uint64_t m_addressSelect;
232 
233 private:
234  SymbolRegistry m_symbolRegistry;
235 };
236 
237 
238 #endif // CPUCOMPONENT_H
A registry for loaded symbols.
double m_frequency
Definition: CPUComponent.h:197
uint64_t m_addressSelect
Definition: CPUComponent.h:230
uint64_t m_delaySlotTarget
Definition: CPUComponent.h:223
virtual bool WriteData(const uint8_t &data, Endianness endianness)
Writes 8-bit data to the currently selected address.
virtual int64_t FunctionTraceArgument(int n)
Definition: CPUComponent.h:185
Endianness
Definition: misc.h:156
virtual uint64_t PCtoInstructionAddress(uint64_t pc)
Convert PC value to instuction address.
Definition: CPUComponent.h:174
bool m_inDelaySlot
Definition: CPUComponent.h:222
const SymbolRegistry & GetSymbolRegistry() const
Definition: CPUComponent.h:67
uint64_t m_lastUnassembleVaddr
Definition: CPUComponent.h:209
bool FunctionTraceCall()
Definition: CPUComponent.cc:99
An interface for implementing components that read/write data via an address bus. ...
int64_t m_nrOfTracedFunctionCalls
Definition: CPUComponent.h:219
The main emulator class.
Definition: GXemul.h:54
virtual void ExecuteMethod(GXemul *gxemul, const string &methodName, const vector< string > &arguments)
Executes a method on the component.
int32_t m_functionCallTraceDepth
Definition: CPUComponent.h:218
bool FunctionTraceReturn()
bool m_showFunctionTraceCall
Definition: CPUComponent.h:216
bool m_exceptionOrAbortInDelaySlot
Definition: CPUComponent.h:231
virtual bool PreRunCheckForComponent(GXemul *gxemul)
Checks the state of this component, before starting execution.
virtual CPUComponent * AsCPUComponent()
Returns the component&#39;s CPUComponent interface.
Definition: CPUComponent.cc:76
u_short data
Definition: siireg.h:79
bool m_isBigEndian
Definition: CPUComponent.h:213
virtual void AddressSelect(uint64_t address)
Place an address on the bus.
bool m_hasUsedUnassemble
Definition: CPUComponent.h:210
virtual void GetMethodNames(vector< string > &names) const
Retrieves a component&#39;s implemented method names.
bool m_showFunctionTraceReturn
Definition: CPUComponent.h:217
A Component is a node in the configuration tree that makes up an emulation setup. ...
Definition: Component.h:62
string m_cpuArchitecture
Definition: CPUComponent.h:201
uint64_t m_pc
Definition: CPUComponent.h:205
SymbolRegistry & GetSymbolRegistry()
Gets a reference to the CPU&#39;s symbol registry.
Definition: CPUComponent.h:63
virtual bool MethodMayBeReexecutedWithoutArgs(const string &methodName) const
Returns whether a method name may be re-executed without args.
virtual void ShowRegisters(GXemul *gxemul, const vector< string > &arguments) const
virtual int FunctionTraceArgumentCount()
Definition: CPUComponent.h:184
virtual double GetCurrentFrequency() const
Returns the current frequency (in Hz) that the component runs at.
Definition: CPUComponent.cc:70
A base-class for processors Component implementations.
Definition: CPUComponent.h:43
virtual size_t DisassembleInstruction(uint64_t vaddr, size_t maxLen, unsigned char *instruction, vector< string > &result)=0
Disassembles an instruction into readable strings.
virtual void ResetState()
Resets the state variables of this component.
Definition: CPUComponent.cc:82
AddressDataBus * m_addressDataBus
Definition: CPUComponent.h:229
CPUComponent(const string &className, const string &cpuKind)
Constructs a CPUComponent.
Definition: CPUComponent.cc:36
virtual bool VirtualToPhysical(uint64_t vaddr, uint64_t &paddr, bool &writable)=0
Virtual to physical address translation (MMU).
virtual bool FunctionTraceReturnImpl(int64_t &retval)
Definition: CPUComponent.h:186
static void RunUnitTests(int &nSucceeded, int &nFailures)
uint64_t Unassemble(int nRows, bool indicatePC, uint64_t vaddr, ostream &output)
virtual void FlushCachedStateForComponent()
Resets the cached state of this component.
Base class for unit testable classes.
Definition: UnitTest.h:74
virtual AddressDataBus * AsAddressDataBus()
Returns the component&#39;s AddressDataBus interface, if any.
virtual bool ReadData(uint8_t &data, Endianness endianness)
Reads 8-bit data from the currently selected address.
uint64_t m_lastDumpAddr
Definition: CPUComponent.h:208
virtual string VirtualAddressAsString(uint64_t vaddr)
Format a virtual address as a displayable string.
Definition: CPUComponent.h:158

Generated on Fri Dec 7 2018 19:52:23 for GXemul by doxygen 1.8.13