Skip to content

Commit

Permalink
Add syscon peripheral
Browse files Browse the repository at this point in the history
  • Loading branch information
bane9 committed Jun 18, 2023
1 parent 96fed5a commit 56e1557
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 4 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ set(SRC_FILES_COMMON
source/peripherals/plic.cpp
source/peripherals/ram.cpp
source/peripherals/virtio.cpp
source/peripherals/syscon.cpp

source/instructions/loadinsn.cpp
source/instructions/otherinsn.cpp
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ Welcome to the RV64GC Emulator repository, a RISC-V emulator project developed u
- Raw framebuffer mode support: The firmware can write to memory-mapped locations to populate a framebuffer and display the contents on the screen.
- PLIC
- CLINT
- VIRTIO
- VIRTIO MMIO
- SYSCON
- bios (firmware), kernel and dtb loading
- Successfully completes all [RISCV imafdcsu ISA tests](https://github.com/riscv-software-src/riscv-tests), with some caveats (see [Testing](#testing))

Expand Down
4 changes: 3 additions & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "helper.hpp"
#include "plic.hpp"
#include "ram.hpp"
#include "syscon.hpp"
#include "virtio.hpp"
#include <algorithm>
#include <array>
Expand Down Expand Up @@ -171,8 +172,9 @@ int main(int argc, char* argv[])

RamDevice dram = RamDevice(DRAM_BASE, ram_size_total, helper::load_file(bios_path));
gpu::GpuDevice gpu = gpu::GpuDevice("RISC V emulator", font_path, 960, 540);
SysconDevice syscon = SysconDevice();

Cpu cpu = Cpu(&dram, &gpu, virtio_device);
Cpu cpu = Cpu(&dram, &gpu, virtio_device, &syscon);

if (dtb_path)
{
Expand Down
8 changes: 7 additions & 1 deletion source/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <utility>

Cpu::Cpu(RamDevice* dram_device, gpu::GpuDevice* gpu_device,
virtio::VirtioBlkDevice* virtio_blk_device)
virtio::VirtioBlkDevice* virtio_blk_device, SysconDevice* syscon_device)
: mmu(*this)
{
mode = cpu::Mode::Machine;
Expand Down Expand Up @@ -57,9 +57,15 @@ Cpu::Cpu(RamDevice* dram_device, gpu::GpuDevice* gpu_device,
bus.add_device(virtio_blk_device);
}

if (syscon_device != nullptr)
{
bus.add_device(syscon_device);
}

this->dram_device = dram_device;
this->gpu_device = gpu_device;
this->virtio_blk_device = virtio_blk_device;
this->syscon_device = syscon_device;

csr::init_handler_array();
}
Expand Down
5 changes: 4 additions & 1 deletion source/include/cpu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "mmu.hpp"
#include "plic.hpp"
#include "ram.hpp"
#include "syscon.hpp"
#include "virtio.hpp"
#include <array>
#include <iostream>
Expand All @@ -22,7 +23,8 @@ class Cpu
{
public:
Cpu(RamDevice* dram_device, gpu::GpuDevice* gpu_device = nullptr,
virtio::VirtioBlkDevice* virtio_blk_device = nullptr);
virtio::VirtioBlkDevice* virtio_blk_device = nullptr,
SysconDevice* syscon_device = nullptr);

void dump_registers(std::ostream& stream);

Expand Down Expand Up @@ -171,6 +173,7 @@ class Cpu
RamDevice* dram_device;
gpu::GpuDevice* gpu_device;
virtio::VirtioBlkDevice* virtio_blk_device;
SysconDevice* syscon_device;

public:
cpu::Mode mode;
Expand Down
33 changes: 33 additions & 0 deletions source/peripherals/include/syscon.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include "bus.hpp"
#include <ostream>
#include <string_view>

class SysconDevice : public BusDevice
{
public:
SysconDevice() = default;
virtual ~SysconDevice() = default;

uint64_t load(Bus& bus, uint64_t address, uint64_t length) override;
void store(Bus& bus, uint64_t address, uint64_t value, uint64_t length) override;

public:
uint64_t get_base_address() const override;
uint64_t get_end_address() const override;

void dump(std::ostream& stream) const override;
std::string_view get_peripheral_name() const override;

public:
static constexpr uint64_t poweroff_addr = 0x5555;

static constexpr uint64_t reboot_addr = 0x7777;

static constexpr uint64_t base_addr = poweroff_addr;

static constexpr uint64_t end_addr = reboot_addr;

static constexpr std::string_view peripheral_name = "SYSCON";
};
38 changes: 38 additions & 0 deletions source/peripherals/syscon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "syscon.hpp"
#include <cstdlib>

uint64_t SysconDevice::load(Bus& bus, uint64_t address, uint64_t length)
{
return 0;
}

void SysconDevice::store(Bus& bus, uint64_t address, uint64_t value, uint64_t length)
{
if (value == poweroff_addr)
{
std::exit(0);
}
else if (value == reboot_addr)
{
std::exit(0);
}
}

void SysconDevice::dump(std::ostream& stream) const
{
}

uint64_t SysconDevice::get_base_address() const
{
return base_addr;
}

uint64_t SysconDevice::get_end_address() const
{
return end_addr;
}

std::string_view SysconDevice::get_peripheral_name() const
{
return peripheral_name;
}

0 comments on commit 56e1557

Please sign in to comment.