Remove all special treatment of the CPU class

This commit is contained in:
Lily Tsuru 2023-07-21 06:38:42 -04:00
parent 8eaf05a8ac
commit 6a3170dc5c
3 changed files with 8 additions and 16 deletions

View File

@ -92,11 +92,10 @@ namespace riscv {
virtual void Poke(AddressT address, u32 value) = 0; virtual void Poke(AddressT address, u32 value) = 0;
}; };
Bus(CPU* cpu); /// Bus destructor.
/// This frees the memory for all devices.
~Bus(); ~Bus();
CPU* GetCPU() { return attachedCpu; }
/// Attach a device to the bus. /// Attach a device to the bus.
/// ///
/// Note that once this function is called (and the device is successfully added), /// Note that once this function is called (and the device is successfully added),
@ -106,8 +105,8 @@ namespace riscv {
/// This function returns true if the device was able to be put on the bus. /// This function returns true if the device was able to be put on the bus.
/// This function returns false in the following error cases: /// This function returns false in the following error cases:
/// - [device] is a null pointer /// - [device] is a null pointer
/// - if [device] is a memory device (and thus reserves address space), adding it would /// - If [device] is a memory device (and thus reserves some address space), adding it
/// end up shadowing another previously-added device. /// to the address space would end up shadowing another previously-added device.
bool AttachDevice(Device* device); bool AttachDevice(Device* device);
/// Clock all clocked devices mapped onto the bus.. /// Clock all clocked devices mapped onto the bus..
@ -124,11 +123,6 @@ namespace riscv {
private: private:
Bus::Device* FindDeviceForAddress(AddressT address) const; Bus::Device* FindDeviceForAddress(AddressT address) const;
// TODO: The CPU needs not be a separate member or be treated specially, it too can be a Bus::Device now
// In fact that would probably be really clean and elegant for calling Step() properly
CPU* attachedCpu {};
/// All devices attached to the bus /// All devices attached to the bus
std::vector<Device*> devices; std::vector<Device*> devices;

View File

@ -12,7 +12,9 @@ namespace riscv {
/// Create /// Create
static System* WithMemory(AddressT ramSize); static System* WithMemory(AddressT ramSize);
void AddDevice(Bus::MmioDevice* device); ~System();
void AddDeviceToBus(Bus::Device* device);
/// returns false if the cpu broke execution /// returns false if the cpu broke execution
bool Step(); bool Step();
@ -29,6 +31,7 @@ namespace riscv {
/// How many instructions will the CPU execute each step /// How many instructions will the CPU execute each step
u32 ipsRate; u32 ipsRate;
// Most of our basic required devices.
CPU* cpu; CPU* cpu;
Bus* bus; Bus* bus;
devices::RamDevice* ram; devices::RamDevice* ram;

View File

@ -1,13 +1,8 @@
#include <algorithm> #include <algorithm>
#include <riscv/Bus.hpp> #include <riscv/Bus.hpp>
#include "riscv/Types.hpp"
namespace riscv { namespace riscv {
Bus::Bus(CPU* cpu) : attachedCpu(cpu) {
}
Bus::~Bus() { Bus::~Bus() {
// Free all devices // Free all devices
for(auto device: devices) for(auto device: devices)