Skip to content

Commit

Permalink
Refactorting of class names (Peripheral)
Browse files Browse the repository at this point in the history
  • Loading branch information
moizumi99 committed Jul 3, 2020
1 parent c7ac5f6 commit 08502b6
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 40 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ add_executable(cpu_test
system_call_emulator.cpp system_call_emulator.h
pte.cpp pte.h
Mmu.cpp Mmu.h
riscv_cpu_common.h Disassembler.cpp Disassembler.h Peripheral.cpp Peripheral.h)
riscv_cpu_common.h Disassembler.cpp Disassembler.h PeripheralEmulator.cpp PeripheralEmulator.h)

add_executable(RISCV_Emulator
assembler.cc
Expand All @@ -57,7 +57,7 @@ add_executable(RISCV_Emulator
system_call_emulator.h
pte.cpp pte.h
Mmu.cpp Mmu.h
riscv_cpu_common.h Disassembler.cpp Disassembler.h Peripheral.cpp Peripheral.h)
riscv_cpu_common.h Disassembler.cpp Disassembler.h PeripheralEmulator.cpp PeripheralEmulator.h)

add_executable(memory_wrapper_test
memory_wrapper.cpp
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CPPFLAGS = -Wall -g
TARGET = RISCV_Emulator
CPU_OBJS = RISCV_cpu.o load_assembler.o assembler.o bit_tools.o \
instruction_encdec.o memory_wrapper.o system_call_emulator.o pte.o Mmu.o \
Disassembler.o Peripheral.o
Disassembler.o PeripheralEmulator.o
OBJS = RISCV_Emulator.o $(CPU_OBJS)
TEST_TARGETS = cpu_test pte_test
WRAPPER_TESTS = memory_wrapper_test load_assembler_test
Expand Down
30 changes: 15 additions & 15 deletions Peripheral.cpp → PeripheralEmulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,39 @@
//

#include <iostream>
#include "Peripheral.h"
#include "PeripheralEmulator.h"
namespace RISCV_EMULATOR {

Peripheral::Peripheral(int mxl) : mxl_(mxl) {}
PeripheralEmulator::PeripheralEmulator(int mxl) : mxl_(mxl) {}

void Peripheral::SetMemory(std::shared_ptr<MemoryWrapper> memory) {
void PeripheralEmulator::SetMemory(std::shared_ptr<MemoryWrapper> memory) {
memory_ = memory;
}

void Peripheral::SetHostEmulationEnable(bool enable) {
host_emulation_ = enable;
void PeripheralEmulator::SetHostEmulationEnable(bool enable) {
host_emulation_enable_ = enable;
}

uint64_t Peripheral::GetHostValue() {
uint64_t PeripheralEmulator::GetHostValue() {
return host_value_;
}

bool Peripheral::GetHostErrorFlag() {
bool PeripheralEmulator::GetHostErrorFlag() {
return error_flag_;
}

bool Peripheral::GetHostEndFlag() {
bool PeripheralEmulator::GetHostEndFlag() {
return end_flag_;
}

void Peripheral::UartEmulation() {
void PeripheralEmulator::DeviceEmulation() {
if (uart_write_) {
std::cout << static_cast<char>(uart_write_value_);
uart_write_ = false;
}
}
// reference: https://github.com/riscv/riscv-isa-sim/issues/364
void Peripheral::CheckPeripheralWrite(uint64_t address, int width, uint64_t data) {
void PeripheralEmulator::CheckDeviceWrite(uint64_t address, int width, uint64_t data) {
// Check if the write is to host communication.
if (mxl_ == 1) {
host_write_ |= (address & 0xFFFFFFFF) == kToHost0 ? 1 : 0;
Expand All @@ -52,17 +52,17 @@ void Peripheral::CheckPeripheralWrite(uint64_t address, int width, uint64_t data
}
}

bool Peripheral::PeripheralEmulation() {
if (host_emulation_) {
void PeripheralEmulator::Emulation() {
if (host_emulation_enable_) {
HostEmulation();
}
if (uart_enable_) {
UartEmulation();
if (device_emulation_enable) {
DeviceEmulation();
}
}

// reference: https://github.com/riscv/riscv-isa-sim/issues/364
void Peripheral::HostEmulation() {
void PeripheralEmulator::HostEmulation() {
if (host_write_ == 0) {
return;
}
Expand Down
26 changes: 16 additions & 10 deletions Peripheral.h → PeripheralEmulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// Created by moiz on 7/2/20.

//
#ifndef ASSEMBLER_TEST_PERIPHERAL_H
#define ASSEMBLER_TEST_PERIPHERAL_H
#ifndef ASSEMBLER_TEST_PERIPHERALEMULATOR_H
#define ASSEMBLER_TEST_PERIPHERALEMULATOR_H

#include <cstdint>
#include <memory>
Expand All @@ -21,40 +21,46 @@ constexpr uint64_t kFromHost = 0x80001040;
constexpr uint64_t kUartBase = 0x10000000;
constexpr uint64_t kUartSize = 6;

class Peripheral {
class PeripheralEmulator {
public:
Peripheral(int mxl);
PeripheralEmulator(int mxl);

void SetMemory(std::shared_ptr<MemoryWrapper> memory);
void Emulation();

void CheckPeripheralWrite(uint64_t address, int width, uint64_t data);
void CheckDeviceWrite(uint64_t address, int width, uint64_t data);

// Host Emulation.
void SetHostEmulationEnable(bool enable);
void HostEmulation();
bool PeripheralEmulation();

bool GetHostEndFlag();

uint64_t GetHostValue();

bool GetHostErrorFlag();

void UartEmulation();
// Device Emulation.
void SetDeviceEmulationEnable(bool enable) {
device_emulation_enable = enable;
}
void DeviceEmulation();

private:
std::shared_ptr<MemoryWrapper> memory_;
int mxl_;
bool host_emulation_ = false;
bool host_emulation_enable_ = false;
int host_write_ = false;
uint64_t host_value_ = 0;
bool end_flag_ = false;
bool error_flag_ = false;

bool uart_enable_ = true;
// UART emulation enable.
bool device_emulation_enable = false;
bool uart_write_ = false;
uint8_t uart_write_value_ = 0;
};

} // namespace RISCV_EMULATOR

#endif //ASSEMBLER_TEST_PERIPHERAL_H
#endif //ASSEMBLER_TEST_PERIPHERALEMULATOR_H
16 changes: 10 additions & 6 deletions RISCV_Emulator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -415,14 +415,15 @@ uint64_t GetEntryPoint(std::vector<uint8_t> &program) {
}
}

std::tuple<bool, std::string, bool, bool, bool, bool, bool>
std::tuple<bool, std::string, bool, bool, bool, bool, bool, bool>
ParseCmd(int argc, char (***argv)) {
bool error = false;
bool verbose = false;
bool address64bit = false;
bool paging = false;
bool ecall_emulation = false;
bool host_emulation = false;
bool device_enable = false;
std::string filename = "";
if (argc < 2) {
error = true;
Expand All @@ -439,6 +440,8 @@ ParseCmd(int argc, char (***argv)) {
ecall_emulation = true;
} else if ((*argv)[i][1] == 'h') {
host_emulation = true;
} else if ((*argv)[i][1] == 'd') {
device_enable = true;
}
} else {
if (filename == "") {
Expand All @@ -450,7 +453,7 @@ ParseCmd(int argc, char (***argv)) {
}
}
return std::make_tuple(error, filename, verbose, address64bit, paging,
ecall_emulation, host_emulation);
ecall_emulation, host_emulation, device_enable);
}

constexpr int k32BitMmuLevelOneSize = 1024; // 1024 x 4 B = 4 KiB.
Expand Down Expand Up @@ -549,8 +552,7 @@ void SetDefaultMmuTable64(std::shared_ptr<MemoryWrapper> memory) {
}


void
SetDefaultMmuTable(bool address64bit, std::shared_ptr<MemoryWrapper> memory) {
void SetDefaultMmuTable(bool address64bit, std::shared_ptr<MemoryWrapper> memory) {
if (address64bit) {
SetDefaultMmuTable64(memory);
} else {
Expand All @@ -559,7 +561,7 @@ SetDefaultMmuTable(bool address64bit, std::shared_ptr<MemoryWrapper> memory) {
}

int run(int argc, char *argv[]) {
bool cmdline_error, verbose, address64bit, paging, ecall_emulation, host_emulation;
bool cmdline_error, verbose, address64bit, paging, ecall_emulation, host_emulation, device_emulation;
std::string filename;

auto options = ParseCmd(argc, &argv);
Expand All @@ -570,7 +572,7 @@ int run(int argc, char *argv[]) {
paging = std::get<4>(options);
ecall_emulation = std::get<5>(options);
host_emulation = std::get<6>(options);

device_emulation = std::get<7>(options);

if (cmdline_error) {
std::cerr << "Uasge: " << argv[0] << " elf_file" << "[-v][-64][-p][-e][-h]"
Expand All @@ -580,6 +582,7 @@ int run(int argc, char *argv[]) {
std::cerr << "-p: Paging Enabled from Start" << std::endl;
std::cerr << "-64: 64 bit (RV64I) (default is 32 bit mode, RV32I)"
<< std::endl;
std::cerr << "-d: Device emulation of UART" << std::endl;
std::cerr << "-h: Use tohost and fromhost function" << std::endl;
return -1;
}
Expand Down Expand Up @@ -612,6 +615,7 @@ int run(int argc, char *argv[]) {
RiscvCpu cpu(address64bit);
cpu.SetEcallEmulationEnable(ecall_emulation);
cpu.SetHostEmulationEnable(host_emulation);
cpu.SetDeviceEmulationEnable(device_emulation);
cpu.SetRegister(SP, sp_value);
cpu.SetRegister(GP, global_pointer);
SetDefaultMmuTable(address64bit, memory);
Expand Down
8 changes: 4 additions & 4 deletions RISCV_cpu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace RISCV_EMULATOR {
reg_[i] = 0;
}
InitializeCsrs();
peripheral = std::make_unique<Peripheral>(mxl_);
peripheral = std::make_unique<PeripheralEmulator>(mxl_);
}

RiscvCpu::RiscvCpu() : RiscvCpu(false) {}
Expand Down Expand Up @@ -544,7 +544,7 @@ namespace RISCV_EMULATOR {
uint64_t next_data = reg_[rs2] >> (access_width * 8);
StoreWd(next_address, next_data, next_width);
}
peripheral->CheckPeripheralWrite(address, width, reg_[rs2]);
peripheral->CheckDeviceWrite(address, width, reg_[rs2]);
}

void
Expand Down Expand Up @@ -1057,7 +1057,7 @@ namespace RISCV_EMULATOR {
DumpRegisters();
}

if (host_emulation_) {
if (host_emulation_ || peripheral_emulation_) {
PeripheralEmulations();
}

Expand All @@ -1071,7 +1071,7 @@ namespace RISCV_EMULATOR {

// reference: https://github.com/riscv/riscv-isa-sim/issues/364
void RiscvCpu::PeripheralEmulations() {
peripheral->PeripheralEmulation();
peripheral->Emulation();
if (peripheral->GetHostEndFlag()) {
reg_[A0] = peripheral->GetHostValue();
error_flag_ |= peripheral->GetHostErrorFlag();
Expand Down
10 changes: 8 additions & 2 deletions RISCV_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "riscv_cpu_common.h"
#include "bit_tools.h"
#include "memory_wrapper.h"
#include "Peripheral.h"
#include "PeripheralEmulator.h"

namespace RISCV_EMULATOR {

Expand Down Expand Up @@ -135,11 +135,17 @@ class RiscvCpu {
host_emulation_ = host_emulation;
peripheral->SetHostEmulationEnable(host_emulation);
};

void SetDeviceEmulationEnable(bool enable) {
peripheral_emulation_=enable;
peripheral->SetDeviceEmulationEnable(enable);
}
private:
void PeripheralEmulations();
std::unique_ptr<Peripheral> peripheral;
std::unique_ptr<PeripheralEmulator> peripheral;
bool ecall_emulation_ = false;
bool host_emulation_ = false;
bool peripheral_emulation_ = false;
uint64_t top_ = 0x80000000;
uint64_t bottom_ = 0x40000000;
uint64_t brk_ = bottom_;
Expand Down

0 comments on commit 08502b6

Please sign in to comment.