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-2019 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 result A vector where the implementation will add:
104  * <ol>
105  * <li>machine code bytes in a standard notation
106  * <li>instruction mnemonic
107  * <li>instruction arguments
108  * <li>instruction comments
109  * </ol>
110  * All of the fields above are optional, but they have to be
111  * specified in the same order for a particular CPU implementation,
112  * so that the fields of the vector can be listed in a tabular
113  * format.
114  * @return The number of bytes that the instruction occupied.
115  */
116  virtual size_t DisassembleInstruction(uint64_t vaddr, vector<string>& result) = 0;
117 
118 
119  /********************************************************************/
120 
121  static void RunUnitTests(int& nSucceeded, int& nFailures);
122 
123 protected:
124  virtual void FlushCachedStateForComponent();
125  virtual bool PreRunCheckForComponent(GXemul* gxemul);
126  virtual void ShowRegisters(GXemul* gxemul, const vector<string>& arguments) const;
127 
128  uint64_t Unassemble(int nRows, bool indicatePC, uint64_t vaddr, ostream& output);
129 
130  /**
131  * \brief Virtual to physical address translation (MMU).
132  *
133  * This function should be overridden in each CPU implementation.
134  *
135  * @param vaddr The virtual address to translate.
136  * @param paddr The return value; physical address.
137  * @param writable This is set to true or false by the function,
138  * depending on if the memory at the virtual address was
139  * writable or not.
140  * @return True if the translation succeeded, false if there was a
141  * translation error.
142  */
143  virtual bool VirtualToPhysical(uint64_t vaddr, uint64_t& paddr,
144  bool& writable) = 0;
145 
146  /**
147  * \brief Format a virtual address as a displayable string.
148  *
149  * This function may be overridden in each CPU implementation.
150  * The default implementation just uses the stringstream << operator.
151  *
152  * @param vaddr The virtual address to translate.
153  * @return A string rendering of the virtual address, e.g. "0x00100f00"
154  */
155  virtual string VirtualAddressAsString(uint64_t vaddr)
156  {
157  stringstream ss;
158  ss.flags(std::ios::hex | std::ios::showbase);
159  ss << vaddr;
160  return ss.str();
161  }
162 
163  /**
164  * \brief Convert PC value to instuction address.
165  *
166  * Usually, this does not need to be overridden. However, some
167  * architectures use e.g. the lowest bit of the PC register to indicate
168  * a different encoding mode (MIPS16), but the instruction is still
169  * aligned as if the lowest bit was 0.
170  */
171  virtual uint64_t PCtoInstructionAddress(uint64_t pc)
172  {
173  return pc;
174  }
175 
176  // CPUComponent:
177  bool FunctionTraceCall();
178  bool FunctionTraceReturn();
179 
180  // Overridden by each CPU architecture:
181  virtual int FunctionTraceArgumentCount() { return 0; }
182  virtual int64_t FunctionTraceArgument(int n) { return 0; }
183  virtual bool FunctionTraceReturnImpl(int64_t& retval) { return false; }
184 
185 private:
186  bool LookupAddressDataBus(GXemul* gxemul = NULL);
187 
188 protected:
189  /*
190  * Variables common to all (or most) kinds of CPUs:
191  */
192 
193  // Framework frequency/runability:
194  double m_frequency;
195  bool m_paused;
196 
197  // Architecture fundamentals:
200 
201  // Program counter:
202  uint64_t m_pc;
203 
204  // Memory dump and disassembly:
205  uint64_t m_lastDumpAddr;
208 
209  // Endianness:
211 
212  // Function call trace:
217 
218  // Delay slot related:
221 
222 
223  /*
224  * Cached/volatile state:
225  */
227  uint64_t m_addressSelect;
229 
230 private:
231  SymbolRegistry m_symbolRegistry;
232 };
233 
234 
235 #endif // CPUCOMPONENT_H
data
u_short data
Definition: siireg.h:79
CPUComponent::m_inDelaySlot
bool m_inDelaySlot
Definition: CPUComponent.h:219
CPUComponent::m_paused
bool m_paused
Definition: CPUComponent.h:195
CPUComponent::m_showFunctionTraceReturn
bool m_showFunctionTraceReturn
Definition: CPUComponent.h:214
CPUComponent::GetSymbolRegistry
SymbolRegistry & GetSymbolRegistry()
Gets a reference to the CPU's symbol registry.
Definition: CPUComponent.h:63
GXemul
The main emulator class.
Definition: GXemul.h:55
CPUComponent::MethodMayBeReexecutedWithoutArgs
virtual bool MethodMayBeReexecutedWithoutArgs(const string &methodName) const
Returns whether a method name may be re-executed without args.
Definition: CPUComponent.cc:190
CPUComponent::Unassemble
uint64_t Unassemble(int nRows, bool indicatePC, uint64_t vaddr, ostream &output)
Definition: CPUComponent.cc:325
CPUComponent::m_functionCallTraceDepth
int32_t m_functionCallTraceDepth
Definition: CPUComponent.h:215
CPUComponent::AsAddressDataBus
virtual AddressDataBus * AsAddressDataBus()
Returns the component's AddressDataBus interface, if any.
Definition: CPUComponent.cc:414
CPUComponent::PreRunCheckForComponent
virtual bool PreRunCheckForComponent(GXemul *gxemul)
Checks the state of this component, before starting execution.
Definition: CPUComponent.cc:428
CPUComponent::FunctionTraceArgumentCount
virtual int FunctionTraceArgumentCount()
Definition: CPUComponent.h:181
CPUComponent::m_frequency
double m_frequency
Definition: CPUComponent.h:194
CPUComponent::m_cpuArchitecture
string m_cpuArchitecture
Definition: CPUComponent.h:198
CPUComponent::m_addressDataBus
AddressDataBus * m_addressDataBus
Definition: CPUComponent.h:226
CPUComponent::DisassembleInstruction
virtual size_t DisassembleInstruction(uint64_t vaddr, vector< string > &result)=0
Disassembles an instruction into readable strings.
SymbolRegistry.h
CPUComponent::FunctionTraceReturnImpl
virtual bool FunctionTraceReturnImpl(int64_t &retval)
Definition: CPUComponent.h:183
CPUComponent::FlushCachedStateForComponent
virtual void FlushCachedStateForComponent()
Resets the cached state of this component.
Definition: CPUComponent.cc:420
CPUComponent::VirtualToPhysical
virtual bool VirtualToPhysical(uint64_t vaddr, uint64_t &paddr, bool &writable)=0
Virtual to physical address translation (MMU).
CPUComponent::GetMethodNames
virtual void GetMethodNames(vector< string > &names) const
Retrieves a component's implemented method names.
Definition: CPUComponent.cc:178
CPUComponent::m_pc
uint64_t m_pc
Definition: CPUComponent.h:202
CPUComponent::m_hasUsedUnassemble
bool m_hasUsedUnassemble
Definition: CPUComponent.h:207
CPUComponent::m_lastDumpAddr
uint64_t m_lastDumpAddr
Definition: CPUComponent.h:205
SymbolRegistry
A registry for loaded symbols.
Definition: SymbolRegistry.h:41
Endianness
Endianness
Definition: misc.h:157
CPUComponent::FunctionTraceCall
bool FunctionTraceCall()
Definition: CPUComponent.cc:99
CPUComponent::GetCurrentFrequency
virtual double GetCurrentFrequency() const
Returns the current frequency (in Hz) that the component runs at.
Definition: CPUComponent.cc:70
UnitTest.h
CPUComponent::m_pageSize
int m_pageSize
Definition: CPUComponent.h:199
Component.h
CPUComponent::m_nrOfTracedFunctionCalls
int64_t m_nrOfTracedFunctionCalls
Definition: CPUComponent.h:216
CPUComponent::m_isBigEndian
bool m_isBigEndian
Definition: CPUComponent.h:210
CPUComponent::FunctionTraceReturn
bool FunctionTraceReturn()
Definition: CPUComponent.cc:143
CPUComponent::CPUComponent
CPUComponent(const string &className, const string &cpuKind)
Constructs a CPUComponent.
Definition: CPUComponent.cc:36
CPUComponent::RunUnitTests
static void RunUnitTests(int &nSucceeded, int &nFailures)
CPUComponent::WriteData
virtual bool WriteData(const uint8_t &data, Endianness endianness)
Writes 8-bit data to the currently selected address.
Definition: CPUComponent.cc:566
CPUComponent::m_addressSelect
uint64_t m_addressSelect
Definition: CPUComponent.h:227
CPUComponent::FunctionTraceArgument
virtual int64_t FunctionTraceArgument(int n)
Definition: CPUComponent.h:182
Component
A Component is a node in the configuration tree that makes up an emulation setup.
Definition: Component.h:64
UnitTestable
Base class for unit testable classes.
Definition: UnitTest.h:75
CPUComponent::VirtualAddressAsString
virtual string VirtualAddressAsString(uint64_t vaddr)
Format a virtual address as a displayable string.
Definition: CPUComponent.h:155
CPUComponent::ReadData
virtual bool ReadData(uint8_t &data, Endianness endianness)
Reads 8-bit data from the currently selected address.
Definition: CPUComponent.cc:504
CPUComponent::m_lastUnassembleVaddr
uint64_t m_lastUnassembleVaddr
Definition: CPUComponent.h:206
CPUComponent::GetSymbolRegistry
const SymbolRegistry & GetSymbolRegistry() const
Definition: CPUComponent.h:67
CPUComponent::ExecuteMethod
virtual void ExecuteMethod(GXemul *gxemul, const string &methodName, const vector< string > &arguments)
Executes a method on the component.
Definition: CPUComponent.cc:203
CPUComponent::AsCPUComponent
virtual CPUComponent * AsCPUComponent()
Returns the component's CPUComponent interface.
Definition: CPUComponent.cc:76
AddressDataBus
An interface for implementing components that read/write data via an address bus.
Definition: AddressDataBus.h:45
CPUComponent::PCtoInstructionAddress
virtual uint64_t PCtoInstructionAddress(uint64_t pc)
Convert PC value to instuction address.
Definition: CPUComponent.h:171
CPUComponent::m_showFunctionTraceCall
bool m_showFunctionTraceCall
Definition: CPUComponent.h:213
CPUComponent::ShowRegisters
virtual void ShowRegisters(GXemul *gxemul, const vector< string > &arguments) const
Definition: CPUComponent.cc:491
CPUComponent::ResetState
virtual void ResetState()
Resets the state variables of this component.
Definition: CPUComponent.cc:82
AddressDataBus.h
CPUComponent::m_delaySlotTarget
uint64_t m_delaySlotTarget
Definition: CPUComponent.h:220
CPUComponent::m_exceptionOrAbortInDelaySlot
bool m_exceptionOrAbortInDelaySlot
Definition: CPUComponent.h:228
CPUComponent::AddressSelect
virtual void AddressSelect(uint64_t address)
Place an address on the bus.
Definition: CPUComponent.cc:498
CPUComponent
A base-class for processors Component implementations.
Definition: CPUComponent.h:47

Generated on Tue Aug 25 2020 19:25:06 for GXemul by doxygen 1.8.18