ysyx-workbench/npc/include/components.hpp

116 lines
3.2 KiB
C++
Raw Normal View History

#ifndef _NPC_COMPONENTS_H_
#define _NPC_COMPONENTS_H_
2024-04-12 01:35:41 +00:00
#include "types.h"
#include <array>
2024-04-12 01:35:41 +00:00
#include <cmath>
2024-04-04 16:16:58 +00:00
#include <cstdlib>
2024-04-09 09:03:21 +00:00
#include <exception>
#include <filesystem>
#include <fstream>
2024-04-04 16:16:58 +00:00
#include <iostream>
2024-04-09 09:03:21 +00:00
#include <map>
2024-04-12 01:35:41 +00:00
#include <sstream>
2024-04-09 09:03:21 +00:00
#include <stdexcept>
#include <string>
2024-04-12 01:35:41 +00:00
#include <vector>
2024-04-09 09:03:21 +00:00
template <typename T, std::size_t nr> class _RegistersBase {
std::array<T, nr> regs;
2024-04-04 16:16:58 +00:00
T pc;
virtual T fetch_pc();
virtual T fetch_reg(std::size_t id);
public:
2024-04-04 16:16:58 +00:00
T operator[](size_t id) { return fetch_reg(id); }
T get_pc() { return fetch_pc(); }
void update() {
for (int i = 0; i < regs.size(); i++) {
regs[i] = fetch_reg(i);
}
}
};
template <typename T, std::size_t n> class Memory {
std::size_t addr_to_index(std::size_t addr) {
2024-04-12 01:35:41 +00:00
extern bool g_skip_memcheck;
if (g_skip_memcheck) {
return 0;
}
2024-04-12 01:35:41 +00:00
if (addr < 0x80000000 || addr > 0x87ffffff) {
std::cerr << std::hex << "ACCESS " << addr << std::dec << std::endl;
throw std::runtime_error("Invalid memory access");
}
// Linear mapping
return (addr >> 2) - 0x20000000;
}
uint32_t expand_bits(uint8_t bits) {
uint32_t x = bits;
x = (x | (x << 7) | (x << 14) | (x << 21)) & 0x01010101;
x = x * 0xFF;
// printf("expand: %hhx->%x\n", bits, x);
return x;
}
public:
2024-04-04 16:16:58 +00:00
std::array<T, n> mem;
2024-04-12 01:35:41 +00:00
std::vector<std::array<uint64_t, 2>> trace_ranges;
Memory(std::filesystem::path filepath, bool is_binary,
std::vector<std::array<uint64_t, 2>> &&trace_ranges)
: trace_ranges(std::move(trace_ranges)) {
2024-04-09 09:03:21 +00:00
if (!std::filesystem::exists(filepath))
throw std::runtime_error("Memory file not found");
if (is_binary) {
std::ifstream file(filepath, std::ios::binary);
char *pmem = reinterpret_cast<char *>(mem.data());
2024-04-04 16:16:58 +00:00
file.read(pmem, mem.size() * sizeof(mem[0]));
} else {
std::string line;
std::ifstream file(filepath);
int i = 0;
while (std::getline(file, line)) {
mem[i++] = std::stoul(line, 0, 16);
}
}
}
const T &operator[](std::size_t addr) { return this->read(addr); }
/**
* Always reads and returns 4 bytes from the address raddr & ~0x3u.
*/
T read(int raddr) {
2024-04-04 16:16:58 +00:00
// printf("raddr: 0x%x\n", raddr);
return mem[addr_to_index((uint32_t)raddr)];
}
/**
* Always writes to the 4 bytes at the address `waddr` & ~0x3u.
* Each bit in `wmask` represents a mask for one byte in wdata.
* For example, wmask = 0x3 means only the lowest 2 bytes are written,
* and the other bytes in memory remain unchanged.
*/
void write(int waddr, T wdata, char wmask) {
2024-04-04 16:16:58 +00:00
// printf("waddr: 0x%x\n", waddr);
mem[addr_to_index((uint32_t)waddr)] = expand_bits(wmask) & wdata;
}
void *guest_to_host(std::size_t addr) {
return mem.data() + addr_to_index(addr);
}
2024-04-12 01:35:41 +00:00
void trace(paddr_t addr, bool is_read, word_t pc = 0, word_t value = 0) {
for (auto &r : trace_ranges) {
if (r[0] <= addr && r[1] >= addr) {
std::stringstream os;
os << std::hex;
if (pc != 0)
os << "0x" << pc << " ";
if (is_read)
os << "[R] ";
else
os << "[W] " << value << " -> ";
os << "0x" << addr << std::dec << std::endl;
std::cout << os.rdbuf();
break;
}
}
}
};
#endif