Compare commits
19 Commits
thumb
...
fae03a263b
| Author | SHA1 | Date | |
|---|---|---|---|
|
fae03a263b
|
|||
|
c4a9c5ee5e
|
|||
|
0029e302b2
|
|||
|
08cc582f23
|
|||
|
933b622493
|
|||
|
8b80f818c6
|
|||
|
441665abad
|
|||
|
1a2e101ebd
|
|||
|
f34efb183f
|
|||
|
9e6b121918
|
|||
|
15c4802838
|
|||
|
0062ad424b
|
|||
|
028c80f6cb
|
|||
|
174008f60c
|
|||
|
e0f7f32699
|
|||
|
36d71a4ee2
|
|||
|
03dbb7052f
|
|||
|
0f09874929
|
|||
|
03ebc6378a
|
2
.gitignore
vendored
2
.gitignore
vendored
@@ -3,5 +3,5 @@ result
|
||||
build/
|
||||
.cache/
|
||||
*~
|
||||
#*#
|
||||
\#*\#
|
||||
.#*
|
||||
|
||||
19
README.md
19
README.md
@@ -5,18 +5,21 @@ But if you are curious (probably not), read ahead
|
||||
# Dependencies
|
||||
## Tested toolchains
|
||||
|
||||
- LLVM 16.0.6
|
||||
- GCC 12.3.0
|
||||
- LLVM 18.1.7
|
||||
- GCC 14.1.0
|
||||
|
||||
In theory, any toolchain supporting at least the C++20 standard should work.
|
||||
In theory, any toolchain supporting at least the c++23 standard should work.
|
||||
I am using LLVM's clang and libcxx as the primary toolchain.
|
||||
|
||||
## Static libraries
|
||||
|
||||
| Name | Version | Required? |
|
||||
|:------:|:----------|:---------:|
|
||||
| fmt | >= 10.1.1 | yes |
|
||||
| catch2 | >= 3.4 | for tests |
|
||||
| Name | Version | Required? | Purpose |
|
||||
|:------:|:--------|:---------:|:---------:|
|
||||
| catch2 | >= 3.4 | no | for tests |
|
||||
|
||||
This goes without saying but using a different toolchain to compile these libraries before linking probably won't work.
|
||||
I will add meson wrap support once LLVM 17 is out, since I want to get rid of fmt.
|
||||
|
||||
-----
|
||||
|
||||
# LOG
|
||||
- June 11, 2024: After almost an year, I have come back to this silly abandoned project, will probably complete it soon.
|
||||
|
||||
@@ -7,8 +7,7 @@
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <unistd.h>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
// NOLINTBEGIN
|
||||
@@ -93,7 +92,7 @@ main(int argc, const char* argv[]) {
|
||||
matar::Cpu cpu(bus);
|
||||
while (true) {
|
||||
cpu.step();
|
||||
sleep(2);
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Exception: " << e.what() << std::endl;
|
||||
|
||||
30
flake.lock
generated
30
flake.lock
generated
@@ -5,11 +5,11 @@
|
||||
"nixpkgs-lib": "nixpkgs-lib"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1693611461,
|
||||
"narHash": "sha256-aPODl8vAgGQ0ZYFIRisxYG5MOGSkIczvu2Cd8Gb9+1Y=",
|
||||
"lastModified": 1717285511,
|
||||
"narHash": "sha256-iKzJcpdXih14qYVcZ9QC9XuZYnPc6T8YImb6dX166kw=",
|
||||
"owner": "hercules-ci",
|
||||
"repo": "flake-parts",
|
||||
"rev": "7f53fdb7bdc5bb237da7fefef12d099e4fd611ca",
|
||||
"rev": "2a55567fcf15b1b1c7ed712a2c6fadaec7412ea8",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
@@ -20,11 +20,11 @@
|
||||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1695318763,
|
||||
"narHash": "sha256-FHVPDRP2AfvsxAdc+AsgFJevMz5VBmnZglFUMlxBkcY=",
|
||||
"lastModified": 1717868076,
|
||||
"narHash": "sha256-c83Y9t815Wa34khrux81j8K8ET94ESmCuwORSKm2bQY=",
|
||||
"owner": "nixos",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "e12483116b3b51a185a33a272bf351e357ba9a99",
|
||||
"rev": "cd18e2ae9ab8e2a0a8d715b60c91b54c0ac35ff9",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
@@ -36,20 +36,14 @@
|
||||
},
|
||||
"nixpkgs-lib": {
|
||||
"locked": {
|
||||
"dir": "lib",
|
||||
"lastModified": 1693471703,
|
||||
"narHash": "sha256-0l03ZBL8P1P6z8MaSDS/MvuU8E75rVxe5eE1N6gxeTo=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "3e52e76b70d5508f3cec70b882a29199f4d1ee85",
|
||||
"type": "github"
|
||||
"lastModified": 1717284937,
|
||||
"narHash": "sha256-lIbdfCsf8LMFloheeE6N31+BMIeixqyQWbSr2vk79EQ=",
|
||||
"type": "tarball",
|
||||
"url": "https://github.com/NixOS/nixpkgs/archive/eb9ceca17df2ea50a250b6b27f7bf6ab0186f198.tar.gz"
|
||||
},
|
||||
"original": {
|
||||
"dir": "lib",
|
||||
"owner": "NixOS",
|
||||
"ref": "nixos-unstable",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
"type": "tarball",
|
||||
"url": "https://github.com/NixOS/nixpkgs/archive/eb9ceca17df2ea50a250b6b27f7bf6ab0186f198.tar.gz"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "memory.hh"
|
||||
#include "io/io.hh"
|
||||
#include <memory>
|
||||
|
||||
namespace matar {
|
||||
@@ -8,16 +9,17 @@ class Bus {
|
||||
public:
|
||||
Bus(const Memory& memory);
|
||||
|
||||
uint8_t read_byte(size_t address);
|
||||
void write_byte(size_t address, uint8_t byte);
|
||||
uint8_t read_byte(uint32_t address);
|
||||
void write_byte(uint32_t address, uint8_t byte);
|
||||
|
||||
uint16_t read_halfword(size_t address);
|
||||
void write_halfword(size_t address, uint16_t halfword);
|
||||
uint16_t read_halfword(uint32_t address);
|
||||
void write_halfword(uint32_t address, uint16_t halfword);
|
||||
|
||||
uint32_t read_word(size_t address);
|
||||
void write_word(size_t address, uint32_t word);
|
||||
uint32_t read_word(uint32_t address);
|
||||
void write_word(uint32_t address, uint32_t word);
|
||||
|
||||
private:
|
||||
IoDevices io;
|
||||
std::shared_ptr<Memory> memory;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <fmt/ostream.h>
|
||||
|
||||
namespace matar {
|
||||
enum class ShiftType {
|
||||
@@ -40,5 +39,14 @@ struct Shift {
|
||||
};
|
||||
|
||||
uint32_t
|
||||
eval_shift(ShiftType shift_type, uint32_t value, uint8_t amount, bool& carry);
|
||||
eval_shift(ShiftType shift_type, uint32_t value, uint32_t amount, bool& carry);
|
||||
|
||||
uint32_t
|
||||
sub(uint32_t a, uint32_t b, bool& carry, bool& overflow);
|
||||
|
||||
uint32_t
|
||||
add(uint32_t a, uint32_t b, bool& carry, bool& overflow, bool c = 0);
|
||||
|
||||
uint32_t
|
||||
sbc(uint32_t a, uint32_t b, bool& carry, bool& overflow, bool c);
|
||||
}
|
||||
@@ -2,10 +2,13 @@
|
||||
#include "cpu/alu.hh"
|
||||
#include "cpu/psr.hh"
|
||||
#include <cstdint>
|
||||
#include <fmt/ostream.h>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
namespace matar::arm {
|
||||
namespace matar {
|
||||
class Cpu;
|
||||
|
||||
namespace arm {
|
||||
|
||||
// https://en.cppreference.com/w/cpp/utility/variant/visit
|
||||
template<class... Ts>
|
||||
@@ -23,7 +26,7 @@ struct BranchAndExchange {
|
||||
|
||||
struct Branch {
|
||||
bool link;
|
||||
uint32_t offset;
|
||||
int32_t offset;
|
||||
};
|
||||
|
||||
struct Multiply {
|
||||
@@ -209,16 +212,19 @@ using InstructionData = std::variant<BranchAndExchange,
|
||||
SoftwareInterrupt>;
|
||||
|
||||
struct Instruction {
|
||||
Condition condition;
|
||||
InstructionData data;
|
||||
|
||||
Instruction(uint32_t insn);
|
||||
Instruction(Condition condition, InstructionData data) noexcept
|
||||
Instruction(Condition condition, InstructionData data)
|
||||
: condition(condition)
|
||||
, data(data){};
|
||||
, data(data) {};
|
||||
|
||||
void exec(Cpu& cpu);
|
||||
|
||||
#ifdef DISASSEMBLER
|
||||
std::string disassemble();
|
||||
#endif
|
||||
|
||||
Condition condition;
|
||||
InstructionData data;
|
||||
};
|
||||
}
|
||||
}
|
||||
3
include/cpu/arm/meson.build
Normal file
3
include/cpu/arm/meson.build
Normal file
@@ -0,0 +1,3 @@
|
||||
headers += files(
|
||||
'instruction.hh'
|
||||
)
|
||||
@@ -1,21 +1,70 @@
|
||||
#pragma once
|
||||
|
||||
#include "arm/instruction.hh"
|
||||
#include "bus.hh"
|
||||
#include "cpu/psr.hh"
|
||||
#include "thumb/instruction.hh"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace matar {
|
||||
class CpuImpl;
|
||||
|
||||
class Cpu {
|
||||
public:
|
||||
Cpu(const Bus& bus) noexcept;
|
||||
Cpu(const Cpu&) = delete;
|
||||
Cpu(Cpu&&) = delete;
|
||||
Cpu& operator=(const Cpu&) = delete;
|
||||
Cpu& operator=(Cpu&&) = delete;
|
||||
|
||||
~Cpu();
|
||||
|
||||
void step();
|
||||
void chg_mode(const Mode to);
|
||||
|
||||
private:
|
||||
std::unique_ptr<CpuImpl> impl;
|
||||
friend void arm::Instruction::exec(Cpu& cpu);
|
||||
friend void thumb::Instruction::exec(Cpu& cpu);
|
||||
|
||||
static constexpr uint8_t GPR_COUNT = 16;
|
||||
|
||||
static constexpr uint8_t GPR_FIQ_FIRST = 8;
|
||||
static constexpr uint8_t GPR_SVC_FIRST = 13;
|
||||
static constexpr uint8_t GPR_ABT_FIRST = 13;
|
||||
static constexpr uint8_t GPR_IRQ_FIRST = 13;
|
||||
static constexpr uint8_t GPR_UND_FIRST = 13;
|
||||
static constexpr uint8_t GPR_OLD_FIRST = 8;
|
||||
|
||||
std::shared_ptr<Bus> bus;
|
||||
std::array<uint32_t, GPR_COUNT> gpr; // general purpose registers
|
||||
|
||||
Psr cpsr; // current program status register
|
||||
Psr spsr; // status program status register
|
||||
|
||||
static constexpr uint8_t SP_INDEX = 13;
|
||||
static_assert(SP_INDEX < GPR_COUNT);
|
||||
uint32_t& sp = gpr[SP_INDEX];
|
||||
|
||||
static constexpr uint8_t LR_INDEX = 14;
|
||||
static_assert(LR_INDEX < GPR_COUNT);
|
||||
uint32_t& lr = gpr[LR_INDEX];
|
||||
|
||||
static constexpr uint8_t PC_INDEX = 15;
|
||||
static_assert(PC_INDEX < GPR_COUNT);
|
||||
uint32_t& pc = gpr[PC_INDEX];
|
||||
|
||||
struct {
|
||||
std::array<uint32_t, GPR_COUNT - GPR_FIQ_FIRST - 1> fiq;
|
||||
std::array<uint32_t, GPR_COUNT - GPR_SVC_FIRST - 1> svc;
|
||||
std::array<uint32_t, GPR_COUNT - GPR_ABT_FIRST - 1> abt;
|
||||
std::array<uint32_t, GPR_COUNT - GPR_IRQ_FIRST - 1> irq;
|
||||
std::array<uint32_t, GPR_COUNT - GPR_UND_FIRST - 1> und;
|
||||
|
||||
// visible registers before the mode switch
|
||||
std::array<uint32_t, GPR_COUNT - GPR_OLD_FIRST - 1> old;
|
||||
} gpr_banked; // banked general purpose registers
|
||||
|
||||
struct {
|
||||
Psr fiq;
|
||||
Psr svc;
|
||||
Psr abt;
|
||||
Psr irq;
|
||||
Psr und;
|
||||
} spsr_banked; // banked saved program status registers
|
||||
|
||||
bool is_flushed;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
headers += files(
|
||||
'alu.hh',
|
||||
'cpu.hh',
|
||||
'psr.hh'
|
||||
)
|
||||
|
||||
subdir('arm')
|
||||
subdir('thumb')
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <fmt/ostream.h>
|
||||
|
||||
namespace matar {
|
||||
enum class Mode {
|
||||
@@ -61,7 +60,7 @@ stringify(Condition cond) {
|
||||
CASE(GT)
|
||||
CASE(LE)
|
||||
case Condition::AL: {
|
||||
// empty
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,7 +115,6 @@ class Psr {
|
||||
|
||||
private:
|
||||
static constexpr uint32_t PSR_CLEAR_RESERVED = 0xF00000FF;
|
||||
static constexpr uint32_t PSR_CLEAR_MODE = 0xFFFFFFE0;
|
||||
|
||||
uint32_t psr;
|
||||
};
|
||||
@@ -3,10 +3,13 @@
|
||||
#include "cpu/alu.hh"
|
||||
#include "cpu/psr.hh"
|
||||
#include <cstdint>
|
||||
#include <fmt/ostream.h>
|
||||
#include <string>
|
||||
#include <variant>
|
||||
|
||||
namespace matar::thumb {
|
||||
namespace matar {
|
||||
class Cpu;
|
||||
|
||||
namespace thumb {
|
||||
|
||||
// https://en.cppreference.com/w/cpp/utility/variant/visit
|
||||
template<class... Ts>
|
||||
@@ -170,7 +173,7 @@ stringify(HiRegisterOperations::OpCode opcode) {
|
||||
}
|
||||
|
||||
struct PcRelativeLoad {
|
||||
uint8_t word;
|
||||
uint16_t word;
|
||||
uint8_t rd;
|
||||
};
|
||||
|
||||
@@ -206,20 +209,19 @@ struct LoadStoreHalfword {
|
||||
};
|
||||
|
||||
struct SpRelativeLoad {
|
||||
uint8_t word;
|
||||
uint16_t word;
|
||||
uint8_t rd;
|
||||
bool load;
|
||||
};
|
||||
|
||||
struct LoadAddress {
|
||||
uint8_t word;
|
||||
uint16_t word;
|
||||
uint8_t rd;
|
||||
bool sp;
|
||||
};
|
||||
|
||||
struct AddOffsetStackPointer {
|
||||
uint8_t word;
|
||||
bool sign;
|
||||
int16_t word;
|
||||
};
|
||||
|
||||
struct PushPopRegister {
|
||||
@@ -235,14 +237,16 @@ struct MultipleLoad {
|
||||
};
|
||||
|
||||
struct ConditionalBranch {
|
||||
uint16_t offset;
|
||||
int32_t offset;
|
||||
Condition condition;
|
||||
};
|
||||
|
||||
struct SoftwareInterrupt {};
|
||||
struct SoftwareInterrupt {
|
||||
uint8_t vector;
|
||||
};
|
||||
|
||||
struct UnconditionalBranch {
|
||||
uint16_t offset;
|
||||
int32_t offset;
|
||||
};
|
||||
|
||||
struct LongBranchWithLink {
|
||||
@@ -271,12 +275,17 @@ using InstructionData = std::variant<MoveShiftedRegister,
|
||||
LongBranchWithLink>;
|
||||
|
||||
struct Instruction {
|
||||
InstructionData data;
|
||||
|
||||
Instruction(uint16_t insn);
|
||||
Instruction(InstructionData data)
|
||||
: data(data) {}
|
||||
|
||||
void exec(Cpu& cpu);
|
||||
|
||||
#ifdef DISASSEMBLER
|
||||
std::string disassemble();
|
||||
#endif
|
||||
|
||||
InstructionData data;
|
||||
};
|
||||
}
|
||||
}
|
||||
3
include/cpu/thumb/meson.build
Normal file
3
include/cpu/thumb/meson.build
Normal file
@@ -0,0 +1,3 @@
|
||||
headers += files(
|
||||
'instruction.hh'
|
||||
)
|
||||
32
include/io/io.hh
Normal file
32
include/io/io.hh
Normal file
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
#include "lcd.hh"
|
||||
#include "sound.hh"
|
||||
#include <cstdint>
|
||||
|
||||
namespace matar {
|
||||
class IoDevices {
|
||||
public:
|
||||
uint8_t read_byte(uint32_t) const;
|
||||
void write_byte(uint32_t, uint8_t);
|
||||
|
||||
uint32_t read_word(uint32_t) const;
|
||||
void write_word(uint32_t, uint32_t);
|
||||
|
||||
uint16_t read_halfword(uint32_t) const;
|
||||
void write_halfword(uint32_t, uint16_t);
|
||||
|
||||
private:
|
||||
struct {
|
||||
using u16 = uint16_t;
|
||||
bool post_boot_flag;
|
||||
bool interrupt_master_enabler;
|
||||
u16 interrupt_enable;
|
||||
u16 interrupt_request_flags;
|
||||
u16 waitstate_control;
|
||||
bool low_power_mode;
|
||||
} system = {};
|
||||
|
||||
struct lcd lcd = {};
|
||||
struct sound sound = {};
|
||||
};
|
||||
}
|
||||
84
include/io/lcd.hh
Normal file
84
include/io/lcd.hh
Normal file
@@ -0,0 +1,84 @@
|
||||
#include <cstdint>
|
||||
|
||||
// NOLINTBEGIN(cppcoreguidelines-avoid-c-arrays)
|
||||
|
||||
/*
|
||||
4000000h 2 R/W DISPCNT LCD Control
|
||||
4000002h 2 R/W - Undocumented - Green Swap
|
||||
4000004h 2 R/W DISPSTAT General LCD Status (STAT,LYC)
|
||||
4000006h 2 R VCOUNT Vertical Counter (LY)
|
||||
4000008h 2 R/W BG0CNT BG0 Control
|
||||
400000Ah 2 R/W BG1CNT BG1 Control
|
||||
400000Ch 2 R/W BG2CNT BG2 Control
|
||||
400000Eh 2 R/W BG3CNT BG3 Control
|
||||
4000010h 2 W BG0HOFS BG0 X-Offset
|
||||
4000012h 2 W BG0VOFS BG0 Y-Offset
|
||||
4000014h 2 W BG1HOFS BG1 X-Offset
|
||||
4000016h 2 W BG1VOFS BG1 Y-Offset
|
||||
4000018h 2 W BG2HOFS BG2 X-Offset
|
||||
400001Ah 2 W BG2VOFS BG2 Y-Offset
|
||||
400001Ch 2 W BG3HOFS BG3 X-Offset
|
||||
400001Eh 2 W BG3VOFS BG3 Y-Offset
|
||||
4000020h 2 W BG2PA BG2 Rotation/Scaling Parameter A (dx)
|
||||
4000022h 2 W BG2PB BG2 Rotation/Scaling Parameter B (dmx)
|
||||
4000024h 2 W BG2PC BG2 Rotation/Scaling Parameter C (dy)
|
||||
4000026h 2 W BG2PD BG2 Rotation/Scaling Parameter D (dmy)
|
||||
4000028h 4 W BG2X BG2 Reference Point X-Coordinate
|
||||
400002Ch 4 W BG2Y BG2 Reference Point Y-Coordinate
|
||||
4000030h 2 W BG3PA BG3 Rotation/Scaling Parameter A (dx)
|
||||
4000032h 2 W BG3PB BG3 Rotation/Scaling Parameter B (dmx)
|
||||
4000034h 2 W BG3PC BG3 Rotation/Scaling Parameter C (dy)
|
||||
4000036h 2 W BG3PD BG3 Rotation/Scaling Parameter D (dmy)
|
||||
4000038h 4 W BG3X BG3 Reference Point X-Coordinate
|
||||
400003Ch 4 W BG3Y BG3 Reference Point Y-Coordinate
|
||||
4000040h 2 W WIN0H Window 0 Horizontal Dimensions
|
||||
4000042h 2 W WIN1H Window 1 Horizontal Dimensions
|
||||
4000044h 2 W WIN0V Window 0 Vertical Dimensions
|
||||
4000046h 2 W WIN1V Window 1 Vertical Dimensions
|
||||
4000048h 2 R/W WININ Inside of Window 0 and 1
|
||||
400004Ah 2 R/W WINOUT Inside of OBJ Window & Outside of Windows
|
||||
400004Ch 2 W MOSAIC Mosaic Size
|
||||
400004Eh - - Not used
|
||||
4000050h 2 R/W BLDCNT Color Special Effects Selection
|
||||
4000052h 2 R/W BLDALPHA Alpha Blending Coefficients
|
||||
4000054h 2 W BLDY Brightness (Fade-In/Out) Coefficient
|
||||
4000056h - - Not used
|
||||
*/
|
||||
|
||||
struct lcd {
|
||||
using u16 = uint16_t;
|
||||
|
||||
u16 lcd_control;
|
||||
u16 general_lcd_status;
|
||||
u16 vertical_counter;
|
||||
u16 bg0_control;
|
||||
u16 bg1_control;
|
||||
u16 bg2_control;
|
||||
u16 bg3_control;
|
||||
u16 bg0_x_offset;
|
||||
u16 bg0_y_offset;
|
||||
u16 bg1_x_offset;
|
||||
u16 bg1_y_offset;
|
||||
u16 bg2_x_offset;
|
||||
u16 bg2_y_offset;
|
||||
u16 bg3_x_offset;
|
||||
u16 bg3_y_offset;
|
||||
u16 bg2_rot_scaling_parameters[4];
|
||||
u16 bg2_reference_x[2];
|
||||
u16 bg2_reference_y[2];
|
||||
u16 bg3_rot_scaling_parameters[4];
|
||||
u16 bg3_reference_x[2];
|
||||
u16 bg3_reference_y[2];
|
||||
u16 win0_horizontal_dimensions;
|
||||
u16 win1_horizontal_dimensions;
|
||||
u16 win0_vertical_dimensions;
|
||||
u16 win1_vertical_dimensions;
|
||||
u16 inside_win_0_1;
|
||||
u16 outside_win;
|
||||
u16 mosaic_size;
|
||||
u16 color_special_effects_selection;
|
||||
u16 alpha_blending_coefficients;
|
||||
u16 brightness_coefficient;
|
||||
};
|
||||
|
||||
// NOLINTEND(cppcoreguidelines-avoid-c-arrays)
|
||||
3
include/io/meson.build
Normal file
3
include/io/meson.build
Normal file
@@ -0,0 +1,3 @@
|
||||
headers += files(
|
||||
'io.hh'
|
||||
)
|
||||
66
include/io/sound.hh
Normal file
66
include/io/sound.hh
Normal file
@@ -0,0 +1,66 @@
|
||||
#include <cstdint>
|
||||
|
||||
// NOLINTBEGIN(cppcoreguidelines-avoid-c-arrays)
|
||||
|
||||
/*
|
||||
4000060h 2 R/W SOUND1CNT_L Channel 1 Sweep register (NR10)
|
||||
4000062h 2 R/W SOUND1CNT_H Channel 1 Duty/Length/Envelope (NR11, NR12)
|
||||
4000064h 2 R/W SOUND1CNT_X Channel 1 Frequency/Control (NR13, NR14)
|
||||
4000066h - - Not used
|
||||
4000068h 2 R/W SOUND2CNT_L Channel 2 Duty/Length/Envelope (NR21, NR22)
|
||||
400006Ah - - Not used
|
||||
400006Ch 2 R/W SOUND2CNT_H Channel 2 Frequency/Control (NR23, NR24)
|
||||
400006Eh - - Not used
|
||||
4000070h 2 R/W SOUND3CNT_L Channel 3 Stop/Wave RAM select (NR30)
|
||||
4000072h 2 R/W SOUND3CNT_H Channel 3 Length/Volume (NR31, NR32)
|
||||
4000074h 2 R/W SOUND3CNT_X Channel 3 Frequency/Control (NR33, NR34)
|
||||
4000076h - - Not used
|
||||
4000078h 2 R/W SOUND4CNT_L Channel 4 Length/Envelope (NR41, NR42)
|
||||
400007Ah - - Not used
|
||||
400007Ch 2 R/W SOUND4CNT_H Channel 4 Frequency/Control (NR43, NR44)
|
||||
400007Eh - - Not used
|
||||
4000080h 2 R/W SOUNDCNT_L Control Stereo/Volume/Enable (NR50, NR51)
|
||||
4000082h 2 R/W SOUNDCNT_H Control Mixing/DMA Control
|
||||
4000084h 2 R/W SOUNDCNT_X Control Sound on/off (NR52)
|
||||
4000086h - - Not used
|
||||
4000088h 2 BIOS SOUNDBIAS Sound PWM Control
|
||||
400008Ah .. - - Not used
|
||||
4000090h 2x10h R/W WAVE_RAM Channel 3 Wave Pattern RAM (2 banks!!)
|
||||
40000A0h 4 W FIFO_A Channel A FIFO, Data 0-3
|
||||
40000A4h 4 W FIFO_B Channel B FIFO, Data 0-3
|
||||
*/
|
||||
|
||||
struct sound{
|
||||
using u16 = uint16_t;
|
||||
|
||||
// channel 1
|
||||
u16 ch1_sweep;
|
||||
u16 ch1_duty_length_env;
|
||||
u16 ch1_freq_control;
|
||||
|
||||
// channel 2
|
||||
u16 ch2_duty_length_env;
|
||||
u16 ch2_freq_control;
|
||||
|
||||
// channel 3
|
||||
u16 ch3_stop_wave_ram_select;
|
||||
u16 ch3_length_volume;
|
||||
u16 ch3_freq_control;
|
||||
u16 ch3_wave_pattern[8];
|
||||
|
||||
// channel 4
|
||||
u16 ch4_length_env;
|
||||
u16 ch4_freq_control;
|
||||
|
||||
// control
|
||||
u16 ctrl_stereo_volume;
|
||||
u16 ctrl_mixing;
|
||||
u16 ctrl_sound_on_off;
|
||||
u16 pwm_control;
|
||||
|
||||
// fifo
|
||||
u16 fifo_a[2];
|
||||
u16 fifo_b[2];
|
||||
};
|
||||
|
||||
// NOLINTEND(cppcoreguidelines-avoid-c-arrays)
|
||||
@@ -10,25 +10,23 @@
|
||||
namespace matar {
|
||||
class Memory {
|
||||
public:
|
||||
static constexpr size_t BIOS_SIZE = 1024 * 16;
|
||||
static constexpr uint32_t BIOS_SIZE = 1024 * 16;
|
||||
|
||||
Memory(std::array<uint8_t, BIOS_SIZE>&& bios, std::vector<uint8_t>&& rom);
|
||||
|
||||
uint8_t read(size_t address) const;
|
||||
void write(size_t address, uint8_t byte);
|
||||
uint8_t read(uint32_t address) const;
|
||||
void write(uint32_t address, uint8_t byte);
|
||||
|
||||
private:
|
||||
#define MEMORY_REGION(name, start, end) \
|
||||
static constexpr size_t name##_START = start; \
|
||||
static constexpr size_t name##_END = end;
|
||||
#define MEMORY_REGION(name, start) \
|
||||
static constexpr uint32_t name##_START = start;
|
||||
|
||||
#define DECL_MEMORY(name, ident, start, end) \
|
||||
MEMORY_REGION(name, start, end) \
|
||||
std::array<uint8_t, name##_END - name##_START + 1> ident;
|
||||
MEMORY_REGION(name, start) \
|
||||
std::array<uint8_t, end - start + 1> ident;
|
||||
|
||||
MEMORY_REGION(BIOS, 0x00000000, 0x00003FFF)
|
||||
MEMORY_REGION(BIOS, 0x00000000)
|
||||
std::array<uint8_t, BIOS_SIZE> bios;
|
||||
static_assert(BIOS_END - BIOS_START + 1 == BIOS_SIZE);
|
||||
|
||||
// board working RAM
|
||||
DECL_MEMORY(BOARD_WRAM, board_wram, 0x02000000, 0x0203FFFF)
|
||||
@@ -47,13 +45,12 @@ class Memory {
|
||||
|
||||
#undef DECL_MEMORY
|
||||
|
||||
MEMORY_REGION(ROM_0, 0x08000000, 0x09FFFFFF)
|
||||
MEMORY_REGION(ROM_1, 0x0A000000, 0x0BFFFFFF)
|
||||
MEMORY_REGION(ROM_2, 0x0C000000, 0x0DFFFFFF)
|
||||
MEMORY_REGION(ROM_0, 0x08000000)
|
||||
MEMORY_REGION(ROM_1, 0x0A000000)
|
||||
MEMORY_REGION(ROM_2, 0x0C000000)
|
||||
|
||||
#undef MEMORY_REGION
|
||||
|
||||
std::unordered_map<size_t, uint8_t> invalid_mem;
|
||||
std::unordered_map<uint32_t, uint8_t> invalid_mem;
|
||||
std::vector<uint8_t> rom;
|
||||
Header header;
|
||||
void parse_header();
|
||||
|
||||
@@ -8,5 +8,6 @@ inc = include_directories('.')
|
||||
|
||||
subdir('cpu')
|
||||
subdir('util')
|
||||
subdir('io')
|
||||
|
||||
install_headers(headers, subdir: meson.project_name(), preserve_path: true)
|
||||
24
meson.build
24
meson.build
@@ -4,33 +4,11 @@ project('matar', 'cpp',
|
||||
default_options : ['warning_level=3',
|
||||
'werror=true',
|
||||
'optimization=3',
|
||||
'cpp_std=c++20',
|
||||
'cpp_std=c++23',
|
||||
'default_library=static'])
|
||||
|
||||
compiler = meson.get_compiler('cpp')
|
||||
|
||||
'''
|
||||
TODO: use <print> and <format> instead of libfmt once LLVM 17 is out
|
||||
|
||||
if compiler.has_argument('-std=c++2c')
|
||||
add_global_arguments('-std=c++2c', language: 'cpp')
|
||||
elif compiler.has_argument('-std=c++23')
|
||||
add_global_arguments('-std=c++23', language: 'cpp')
|
||||
elif compiler.has_argument('-std=c++2b')
|
||||
add_global_arguments('-std=c++2b', language: 'cpp')
|
||||
elif compiler.has_argument('-std=c++20')
|
||||
add_global_arguments('-std=c++20', language: 'cpp')
|
||||
else
|
||||
error(compiler.get_id() + ' ' + compiler.version() + 'does not meet the compiler requirements')
|
||||
endif
|
||||
|
||||
if compiler.has_argument('-fexperimental-library')
|
||||
add_global_arguments('-fexperimental-library', language: 'cpp')
|
||||
else
|
||||
error(compiler.get_id() + ' ' + compiler.version() + 'does not support -fexperimental-library')
|
||||
endif
|
||||
'''
|
||||
|
||||
subdir('include')
|
||||
subdir('src')
|
||||
subdir('apps')
|
||||
|
||||
1248
nix/Cargo.lock
generated
Normal file
1248
nix/Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
@@ -1,17 +1,10 @@
|
||||
{ ... }: {
|
||||
perSystem = { pkgs, src, ... }:
|
||||
let
|
||||
llvm = pkgs.llvmPackages_16;
|
||||
llvm = pkgs.llvmPackages_18;
|
||||
stdenv = llvm.libcxxStdenv;
|
||||
|
||||
libraries = with pkgs; [
|
||||
((pkgs.fmt.override {
|
||||
inherit stdenv;
|
||||
enableShared = false;
|
||||
}).overrideAttrs (oa: {
|
||||
cmakeFlags = oa.cmakeFlags ++ [ "-DFMT_TEST=off" ];
|
||||
})).dev
|
||||
|
||||
(catch2_3.override { inherit stdenv; }).out
|
||||
];
|
||||
in
|
||||
@@ -19,7 +12,7 @@
|
||||
packages.matar-clang = pkgs.callPackage ./build.nix { inherit src libraries stdenv; };
|
||||
devShells.matar-clang = pkgs.callPackage ./shell.nix {
|
||||
inherit libraries stdenv;
|
||||
tools = with pkgs; [ clang-tools_16 ];
|
||||
tools = with pkgs; [ (clang-tools_18.override { enableLibcxx = true; }) ];
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
{ ... }: {
|
||||
perSystem = { pkgs, src, ... }:
|
||||
let
|
||||
stdenv = pkgs.gcc14Stdenv;
|
||||
|
||||
libraries = with pkgs; [
|
||||
(pkgs.fmt.override { enableShared = false; }).dev
|
||||
catch2_3.out
|
||||
(catch2_3.override { inherit stdenv; }).out
|
||||
];
|
||||
in
|
||||
{
|
||||
packages.matar = pkgs.callPackage ./build.nix { inherit src libraries; };
|
||||
devShells.matar = pkgs.callPackage ./shell.nix { inherit libraries; };
|
||||
packages.matar = pkgs.callPackage ./build.nix { inherit src libraries stdenv; };
|
||||
devShells.matar = pkgs.callPackage ./shell.nix { inherit libraries stdenv; };
|
||||
};
|
||||
}
|
||||
|
||||
58
src/bus.cc
58
src/bus.cc
@@ -3,53 +3,81 @@
|
||||
#include <memory>
|
||||
|
||||
namespace matar {
|
||||
|
||||
static constexpr uint32_t IO_START = 0x4000000;
|
||||
static constexpr uint32_t IO_END = 0x40003FE;
|
||||
|
||||
Bus::Bus(const Memory& memory)
|
||||
: memory(std::make_shared<Memory>(memory)) {}
|
||||
|
||||
uint8_t
|
||||
Bus::read_byte(size_t address) {
|
||||
Bus::read_byte(uint32_t address) {
|
||||
if (address >= IO_START && address <= IO_END)
|
||||
return io.read_byte(address);
|
||||
|
||||
return memory->read(address);
|
||||
}
|
||||
|
||||
void
|
||||
Bus::write_byte(size_t address, uint8_t byte) {
|
||||
Bus::write_byte(uint32_t address, uint8_t byte) {
|
||||
if (address >= IO_START && address <= IO_END) {
|
||||
io.write_byte(address, byte);
|
||||
return;
|
||||
}
|
||||
|
||||
memory->write(address, byte);
|
||||
}
|
||||
|
||||
uint16_t
|
||||
Bus::read_halfword(size_t address) {
|
||||
Bus::read_halfword(uint32_t address) {
|
||||
if (address & 0b01)
|
||||
glogger.warn("Reading a non aligned halfword address");
|
||||
|
||||
return memory->read(address) | memory->read(address + 1) << 8;
|
||||
if (address >= IO_START && address <= IO_END)
|
||||
return io.read_halfword(address);
|
||||
|
||||
return read_byte(address) | read_byte(address + 1) << 8;
|
||||
}
|
||||
|
||||
void
|
||||
Bus::write_halfword(size_t address, uint16_t halfword) {
|
||||
Bus::write_halfword(uint32_t address, uint16_t halfword) {
|
||||
if (address & 0b01)
|
||||
glogger.warn("Writing to a non aligned halfword address");
|
||||
|
||||
memory->write(address, halfword & 0xFF);
|
||||
memory->write(address + 1, halfword >> 8 & 0xFF);
|
||||
if (address >= IO_START && address <= IO_END) {
|
||||
io.write_halfword(address, halfword);
|
||||
return;
|
||||
}
|
||||
|
||||
write_byte(address, halfword & 0xFF);
|
||||
write_byte(address + 1, halfword >> 8 & 0xFF);
|
||||
}
|
||||
|
||||
uint32_t
|
||||
Bus::read_word(size_t address) {
|
||||
Bus::read_word(uint32_t address) {
|
||||
if (address & 0b11)
|
||||
glogger.warn("Reading a non aligned word address");
|
||||
|
||||
return memory->read(address) | memory->read(address + 1) << 8 |
|
||||
memory->read(address + 2) << 16 | memory->read(address + 3) << 24;
|
||||
if (address >= IO_START && address <= IO_END)
|
||||
return io.read_word(address);
|
||||
|
||||
return read_byte(address) | read_byte(address + 1) << 8 |
|
||||
read_byte(address + 2) << 16 | read_byte(address + 3) << 24;
|
||||
}
|
||||
|
||||
void
|
||||
Bus::write_word(size_t address, uint32_t word) {
|
||||
Bus::write_word(uint32_t address, uint32_t word) {
|
||||
if (address & 0b11)
|
||||
glogger.warn("Writing to a non aligned word address");
|
||||
|
||||
memory->write(address, word & 0xFF);
|
||||
memory->write(address + 1, word >> 8 & 0xFF);
|
||||
memory->write(address + 2, word >> 16 & 0xFF);
|
||||
memory->write(address + 3, word >> 24 & 0xFF);
|
||||
if (address >= IO_START && address <= IO_END) {
|
||||
io.write_word(address, word);
|
||||
return;
|
||||
}
|
||||
|
||||
write_byte(address, word & 0xFF);
|
||||
write_byte(address + 1, word >> 8 & 0xFF);
|
||||
write_byte(address + 2, word >> 16 & 0xFF);
|
||||
write_byte(address + 3, word >> 24 & 0xFF);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
#include "alu.hh"
|
||||
#include "cpu/alu.hh"
|
||||
#include "util/bits.hh"
|
||||
#include <bit>
|
||||
|
||||
namespace matar {
|
||||
uint32_t
|
||||
eval_shift(ShiftType shift_type, uint32_t value, uint8_t amount, bool& carry) {
|
||||
eval_shift(ShiftType shift_type, uint32_t value, uint32_t amount, bool& carry) {
|
||||
uint32_t eval = 0;
|
||||
|
||||
switch (shift_type) {
|
||||
@@ -48,4 +49,43 @@ eval_shift(ShiftType shift_type, uint32_t value, uint8_t amount, bool& carry) {
|
||||
|
||||
return eval;
|
||||
}
|
||||
|
||||
uint32_t
|
||||
sub(uint32_t a, uint32_t b, bool& carry, bool& overflow) {
|
||||
bool s1 = get_bit(a, 31);
|
||||
bool s2 = get_bit(b, 31);
|
||||
|
||||
uint32_t result = a - b;
|
||||
|
||||
carry = a >= b;
|
||||
overflow = s1 != s2 && s2 == get_bit(result, 31);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32_t
|
||||
add(uint32_t a, uint32_t b, bool& carry, bool& overflow, bool c) {
|
||||
bool s1 = get_bit(a, 31);
|
||||
bool s2 = get_bit(b, 31);
|
||||
|
||||
uint64_t result = a + b + c;
|
||||
|
||||
carry = get_bit(result, 32);
|
||||
overflow = s1 == s2 && s2 != get_bit(result, 31);
|
||||
|
||||
return result & 0xFFFFFFFF;
|
||||
}
|
||||
|
||||
uint32_t
|
||||
sbc(uint32_t a, uint32_t b, bool& carry, bool& overflow, bool c) {
|
||||
bool s1 = get_bit(a, 31);
|
||||
bool s2 = get_bit(b, 31);
|
||||
|
||||
uint64_t result = a - b - !c;
|
||||
|
||||
carry = get_bit(result, 32);
|
||||
overflow = s1 != s2 && s2 == get_bit(result, 31);
|
||||
|
||||
return result & 0xFFFFFFFF;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "instruction.hh"
|
||||
#include "cpu/arm/instruction.hh"
|
||||
#include "util/bits.hh"
|
||||
#include <format>
|
||||
|
||||
namespace matar::arm {
|
||||
std::string
|
||||
@@ -9,15 +10,18 @@ Instruction::disassemble() {
|
||||
return std::visit(
|
||||
overloaded{
|
||||
[condition](BranchAndExchange& data) {
|
||||
return fmt::format("BX{} R{:d}", condition, data.rn);
|
||||
return std::format("BX{} R{:d}", condition, data.rn);
|
||||
},
|
||||
[condition](Branch& data) {
|
||||
return fmt::format(
|
||||
"B{}{} 0x{:06X}", (data.link ? "L" : ""), condition, data.offset);
|
||||
return std::format(
|
||||
"B{}{} {:#06x}",
|
||||
(data.link ? "L" : ""),
|
||||
condition,
|
||||
static_cast<int32_t>(data.offset + 2 * INSTRUCTION_SIZE));
|
||||
},
|
||||
[condition](Multiply& data) {
|
||||
if (data.acc) {
|
||||
return fmt::format("MLA{}{} R{:d},R{:d},R{:d},R{:d}",
|
||||
return std::format("MLA{}{} R{:d},R{:d},R{:d},R{:d}",
|
||||
condition,
|
||||
(data.set ? "S" : ""),
|
||||
data.rd,
|
||||
@@ -25,7 +29,7 @@ Instruction::disassemble() {
|
||||
data.rs,
|
||||
data.rn);
|
||||
} else {
|
||||
return fmt::format("MUL{}{} R{:d},R{:d},R{:d}",
|
||||
return std::format("MUL{}{} R{:d},R{:d},R{:d}",
|
||||
condition,
|
||||
(data.set ? "S" : ""),
|
||||
data.rd,
|
||||
@@ -34,7 +38,7 @@ Instruction::disassemble() {
|
||||
}
|
||||
},
|
||||
[condition](MultiplyLong& data) {
|
||||
return fmt::format("{}{}{}{} R{:d},R{:d},R{:d},R{:d}",
|
||||
return std::format("{}{}{}{} R{:d},R{:d},R{:d},R{:d}",
|
||||
(data.uns ? 'U' : 'S'),
|
||||
(data.acc ? "MLAL" : "MULL"),
|
||||
condition,
|
||||
@@ -46,7 +50,7 @@ Instruction::disassemble() {
|
||||
},
|
||||
[](Undefined) { return std::string("UND"); },
|
||||
[condition](SingleDataSwap& data) {
|
||||
return fmt::format("SWP{}{} R{:d},R{:d},[R{:d}]",
|
||||
return std::format("SWP{}{} R{:d},R{:d},[R{:d}]",
|
||||
condition,
|
||||
(data.byte ? "B" : ""),
|
||||
data.rd,
|
||||
@@ -62,18 +66,18 @@ Instruction::disassemble() {
|
||||
expression = "";
|
||||
} else {
|
||||
expression =
|
||||
fmt::format(",{}#{:d}", (data.up ? '+' : '-'), *offset);
|
||||
std::format(",{}#{:d}", (data.up ? '+' : '-'), *offset);
|
||||
}
|
||||
} else if (const Shift* shift = std::get_if<Shift>(&data.offset)) {
|
||||
// Shifts are always immediate in single data transfer
|
||||
expression = fmt::format(",{}R{:d},{} #{:d}",
|
||||
expression = std::format(",{}R{:d},{} #{:d}",
|
||||
(data.up ? '+' : '-'),
|
||||
shift->rm,
|
||||
stringify(shift->data.type),
|
||||
shift->data.operand);
|
||||
}
|
||||
|
||||
return fmt::format(
|
||||
return std::format(
|
||||
"{}{}{}{} R{:d},[R{:d}{}]{}",
|
||||
(data.load ? "LDR" : "STR"),
|
||||
condition,
|
||||
@@ -91,15 +95,15 @@ Instruction::disassemble() {
|
||||
if (data.offset == 0) {
|
||||
expression = "";
|
||||
} else {
|
||||
expression = fmt::format(
|
||||
expression = std::format(
|
||||
",{}#{:d}", (data.up ? '+' : '-'), data.offset);
|
||||
}
|
||||
} else {
|
||||
expression =
|
||||
fmt::format(",{}R{:d}", (data.up ? '+' : '-'), data.offset);
|
||||
std::format(",{}R{:d}", (data.up ? '+' : '-'), data.offset);
|
||||
}
|
||||
|
||||
return fmt::format(
|
||||
return std::format(
|
||||
"{}{}{}{} R{:d},[R{:d}{}]{}",
|
||||
(data.load ? "LDR" : "STR"),
|
||||
condition,
|
||||
@@ -115,12 +119,12 @@ Instruction::disassemble() {
|
||||
|
||||
for (uint8_t i = 0; i < 16; i++) {
|
||||
if (get_bit(data.regs, i))
|
||||
fmt::format_to(std::back_inserter(regs), "R{:d},", i);
|
||||
std::format_to(std::back_inserter(regs), "R{:d},", i);
|
||||
};
|
||||
|
||||
regs.pop_back();
|
||||
|
||||
return fmt::format("{}{}{}{} R{:d}{},{{{}}}{}",
|
||||
return std::format("{}{}{}{} R{:d}{},{{{}}}{}",
|
||||
(data.load ? "LDM" : "STM"),
|
||||
condition,
|
||||
(data.up ? 'I' : 'D'),
|
||||
@@ -132,12 +136,12 @@ Instruction::disassemble() {
|
||||
},
|
||||
[condition](PsrTransfer& data) {
|
||||
if (data.type == PsrTransfer::Type::Mrs) {
|
||||
return fmt::format("MRS{} R{:d},{}",
|
||||
return std::format("MRS{} R{:d},{}",
|
||||
condition,
|
||||
data.operand,
|
||||
(data.spsr ? "SPSR_all" : "CPSR_all"));
|
||||
} else {
|
||||
return fmt::format(
|
||||
return std::format(
|
||||
"MSR{} {}_{},{}{}",
|
||||
condition,
|
||||
(data.spsr ? "SPSR" : "CPSR"),
|
||||
@@ -153,9 +157,9 @@ Instruction::disassemble() {
|
||||
|
||||
if (const uint32_t* operand =
|
||||
std::get_if<uint32_t>(&data.operand)) {
|
||||
op_2 = fmt::format("#{:d}", *operand);
|
||||
op_2 = std::format("#{:d}", *operand);
|
||||
} else if (const Shift* shift = std::get_if<Shift>(&data.operand)) {
|
||||
op_2 = fmt::format("R{:d},{} {}{:d}",
|
||||
op_2 = std::format("R{:d},{} {}{:d}",
|
||||
shift->rm,
|
||||
stringify(shift->data.type),
|
||||
(shift->data.immediate ? '#' : 'R'),
|
||||
@@ -165,7 +169,7 @@ Instruction::disassemble() {
|
||||
switch (data.opcode) {
|
||||
case OpCode::MOV:
|
||||
case OpCode::MVN:
|
||||
return fmt::format("{}{}{} R{:d},{}",
|
||||
return std::format("{}{}{} R{:d},{}",
|
||||
stringify(data.opcode),
|
||||
condition,
|
||||
(data.set ? "S" : ""),
|
||||
@@ -175,13 +179,13 @@ Instruction::disassemble() {
|
||||
case OpCode::TEQ:
|
||||
case OpCode::CMP:
|
||||
case OpCode::CMN:
|
||||
return fmt::format("{}{} R{:d},{}",
|
||||
return std::format("{}{} R{:d},{}",
|
||||
stringify(data.opcode),
|
||||
condition,
|
||||
data.rn,
|
||||
op_2);
|
||||
default:
|
||||
return fmt::format("{}{}{} R{:d},R{:d},{}",
|
||||
return std::format("{}{}{} R{:d},R{:d},{}",
|
||||
stringify(data.opcode),
|
||||
condition,
|
||||
(data.set ? "S" : ""),
|
||||
@@ -191,11 +195,11 @@ Instruction::disassemble() {
|
||||
}
|
||||
},
|
||||
[condition](SoftwareInterrupt) {
|
||||
return fmt::format("SWI{}", condition);
|
||||
return std::format("SWI{}", condition);
|
||||
},
|
||||
[condition](CoprocessorDataTransfer& data) {
|
||||
std::string expression = fmt::format(",#{:d}", data.offset);
|
||||
return fmt::format(
|
||||
std::string expression = std::format(",#{:d}", data.offset);
|
||||
return std::format(
|
||||
"{}{}{} p{:d},c{:d},[R{:d}{}]{}",
|
||||
(data.load ? "LDC" : "STC"),
|
||||
condition,
|
||||
@@ -207,7 +211,7 @@ Instruction::disassemble() {
|
||||
(data.pre ? (data.write ? "!" : "") : expression));
|
||||
},
|
||||
[condition](CoprocessorDataOperation& data) {
|
||||
return fmt::format("CDP{} p{},{},c{},c{},c{},{}",
|
||||
return std::format("CDP{} p{},{},c{},c{},c{},{}",
|
||||
condition,
|
||||
data.cpn,
|
||||
data.cp_opc,
|
||||
@@ -217,7 +221,7 @@ Instruction::disassemble() {
|
||||
data.cp);
|
||||
},
|
||||
[condition](CoprocessorRegisterTransfer& data) {
|
||||
return fmt::format("{}{} p{},{},R{},c{},c{},{}",
|
||||
return std::format("{}{} p{},{},R{},c{},c{},{}",
|
||||
(data.load ? "MRC" : "MCR"),
|
||||
condition,
|
||||
data.cpn,
|
||||
|
||||
@@ -1,24 +1,21 @@
|
||||
#include "cpu/cpu-impl.hh"
|
||||
#include "cpu/cpu.hh"
|
||||
#include "util/bits.hh"
|
||||
#include "util/log.hh"
|
||||
|
||||
namespace matar {
|
||||
namespace matar::arm {
|
||||
void
|
||||
CpuImpl::exec(const arm::Instruction instruction) {
|
||||
Condition cond = instruction.condition;
|
||||
arm::InstructionData data = instruction.data;
|
||||
|
||||
if (!cpsr.condition(cond)) {
|
||||
Instruction::exec(Cpu& cpu) {
|
||||
if (!cpu.cpsr.condition(condition)) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto pc_error = [](uint8_t r) {
|
||||
if (r == PC_INDEX)
|
||||
auto pc_error = [cpu](uint8_t r) {
|
||||
if (r == cpu.PC_INDEX)
|
||||
glogger.error("Using PC (R15) as operand register");
|
||||
};
|
||||
|
||||
auto pc_warn = [](uint8_t r) {
|
||||
if (r == PC_INDEX)
|
||||
auto pc_warn = [cpu](uint8_t r) {
|
||||
if (r == cpu.PC_INDEX)
|
||||
glogger.warn("Using PC (R15) as operand register");
|
||||
};
|
||||
|
||||
@@ -26,38 +23,40 @@ CpuImpl::exec(const arm::Instruction instruction) {
|
||||
|
||||
std::visit(
|
||||
overloaded{
|
||||
[this, pc_warn](BranchAndExchange& data) {
|
||||
State state = static_cast<State>(data.rn & 1);
|
||||
[&cpu, pc_warn](BranchAndExchange& data) {
|
||||
uint32_t addr = cpu.gpr[data.rn];
|
||||
State state = static_cast<State>(get_bit(addr, 0));
|
||||
|
||||
pc_warn(data.rn);
|
||||
|
||||
if (state != cpu.cpsr.state())
|
||||
glogger.info_bold("State changed");
|
||||
|
||||
// set state
|
||||
cpsr.set_state(state);
|
||||
cpu.cpsr.set_state(state);
|
||||
|
||||
// copy to PC
|
||||
pc = gpr[data.rn];
|
||||
cpu.pc = addr;
|
||||
|
||||
// ignore [1:0] bits for arm and 0 bit for thumb
|
||||
rst_bit(pc, 0);
|
||||
rst_bit(cpu.pc, 0);
|
||||
|
||||
if (state == State::Arm)
|
||||
rst_bit(pc, 1);
|
||||
rst_bit(cpu.pc, 1);
|
||||
|
||||
// pc is affected so flush the pipeline
|
||||
is_flushed = true;
|
||||
// PC is affected so flush the pipeline
|
||||
cpu.is_flushed = true;
|
||||
},
|
||||
[this](Branch& data) {
|
||||
[&cpu](Branch& data) {
|
||||
if (data.link)
|
||||
gpr[14] = pc - INSTRUCTION_SIZE;
|
||||
cpu.gpr[14] = cpu.pc - INSTRUCTION_SIZE;
|
||||
|
||||
// data.offset accounts for two instructions ahead when
|
||||
// disassembling, so need to adjust
|
||||
pc = static_cast<int32_t>(pc) - 2 * INSTRUCTION_SIZE + data.offset;
|
||||
cpu.pc += data.offset;
|
||||
|
||||
// pc is affected so flush the pipeline
|
||||
is_flushed = true;
|
||||
cpu.is_flushed = true;
|
||||
},
|
||||
[this, pc_error](Multiply& data) {
|
||||
[&cpu, pc_error](Multiply& data) {
|
||||
if (data.rd == data.rm)
|
||||
glogger.error("rd and rm are not distinct in {}",
|
||||
typeid(data).name());
|
||||
@@ -66,16 +65,16 @@ CpuImpl::exec(const arm::Instruction instruction) {
|
||||
pc_error(data.rd);
|
||||
pc_error(data.rd);
|
||||
|
||||
gpr[data.rd] =
|
||||
gpr[data.rm] * gpr[data.rs] + (data.acc ? gpr[data.rn] : 0);
|
||||
cpu.gpr[data.rd] = cpu.gpr[data.rm] * cpu.gpr[data.rs] +
|
||||
(data.acc ? cpu.gpr[data.rn] : 0);
|
||||
|
||||
if (data.set) {
|
||||
cpsr.set_z(gpr[data.rd] == 0);
|
||||
cpsr.set_n(get_bit(gpr[data.rd], 31));
|
||||
cpsr.set_c(0);
|
||||
cpu.cpsr.set_z(cpu.gpr[data.rd] == 0);
|
||||
cpu.cpsr.set_n(get_bit(cpu.gpr[data.rd], 31));
|
||||
cpu.cpsr.set_c(0);
|
||||
}
|
||||
},
|
||||
[this, pc_error](MultiplyLong& data) {
|
||||
[&cpu, pc_error](MultiplyLong& data) {
|
||||
if (data.rdhi == data.rdlo || data.rdhi == data.rm ||
|
||||
data.rdlo == data.rm)
|
||||
glogger.error("rdhi, rdlo and rm are not distinct in {}",
|
||||
@@ -91,58 +90,60 @@ CpuImpl::exec(const arm::Instruction instruction) {
|
||||
return static_cast<uint64_t>(x);
|
||||
};
|
||||
|
||||
uint64_t eval = cast(gpr[data.rm]) * cast(gpr[data.rs]) +
|
||||
(data.acc ? (cast(gpr[data.rdhi]) << 32) |
|
||||
cast(gpr[data.rdlo])
|
||||
: 0);
|
||||
uint64_t eval =
|
||||
cast(cpu.gpr[data.rm]) * cast(cpu.gpr[data.rs]) +
|
||||
(data.acc ? (cast(cpu.gpr[data.rdhi]) << 32) |
|
||||
cast(cpu.gpr[data.rdlo])
|
||||
: 0);
|
||||
|
||||
gpr[data.rdlo] = bit_range(eval, 0, 31);
|
||||
gpr[data.rdhi] = bit_range(eval, 32, 63);
|
||||
cpu.gpr[data.rdlo] = bit_range(eval, 0, 31);
|
||||
cpu.gpr[data.rdhi] = bit_range(eval, 32, 63);
|
||||
|
||||
} else {
|
||||
auto cast = [](uint32_t x) -> int64_t {
|
||||
return static_cast<int64_t>(static_cast<int32_t>(x));
|
||||
};
|
||||
|
||||
int64_t eval = cast(gpr[data.rm]) * cast(gpr[data.rs]) +
|
||||
(data.acc ? (cast(gpr[data.rdhi]) << 32) |
|
||||
cast(gpr[data.rdlo])
|
||||
int64_t eval = cast(cpu.gpr[data.rm]) * cast(cpu.gpr[data.rs]) +
|
||||
(data.acc ? (cast(cpu.gpr[data.rdhi]) << 32) |
|
||||
cast(cpu.gpr[data.rdlo])
|
||||
: 0);
|
||||
|
||||
gpr[data.rdlo] = bit_range(eval, 0, 31);
|
||||
gpr[data.rdhi] = bit_range(eval, 32, 63);
|
||||
cpu.gpr[data.rdlo] = bit_range(eval, 0, 31);
|
||||
cpu.gpr[data.rdhi] = bit_range(eval, 32, 63);
|
||||
}
|
||||
|
||||
if (data.set) {
|
||||
cpsr.set_z(gpr[data.rdhi] == 0 && gpr[data.rdlo] == 0);
|
||||
cpsr.set_n(get_bit(gpr[data.rdhi], 31));
|
||||
cpsr.set_c(0);
|
||||
cpsr.set_v(0);
|
||||
cpu.cpsr.set_z(cpu.gpr[data.rdhi] == 0 &&
|
||||
cpu.gpr[data.rdlo] == 0);
|
||||
cpu.cpsr.set_n(get_bit(cpu.gpr[data.rdhi], 31));
|
||||
cpu.cpsr.set_c(0);
|
||||
cpu.cpsr.set_v(0);
|
||||
}
|
||||
},
|
||||
[](Undefined) { glogger.warn("Undefined instruction"); },
|
||||
[this, pc_error](SingleDataSwap& data) {
|
||||
[&cpu, pc_error](SingleDataSwap& data) {
|
||||
pc_error(data.rm);
|
||||
pc_error(data.rn);
|
||||
pc_error(data.rd);
|
||||
|
||||
if (data.byte) {
|
||||
gpr[data.rd] = bus->read_byte(gpr[data.rn]);
|
||||
bus->write_byte(gpr[data.rn], gpr[data.rm] & 0xFF);
|
||||
cpu.gpr[data.rd] = cpu.bus->read_byte(cpu.gpr[data.rn]);
|
||||
cpu.bus->write_byte(cpu.gpr[data.rn], cpu.gpr[data.rm] & 0xFF);
|
||||
} else {
|
||||
gpr[data.rd] = bus->read_word(gpr[data.rn]);
|
||||
bus->write_word(gpr[data.rn], gpr[data.rm]);
|
||||
cpu.gpr[data.rd] = cpu.bus->read_word(cpu.gpr[data.rn]);
|
||||
cpu.bus->write_word(cpu.gpr[data.rn], cpu.gpr[data.rm]);
|
||||
}
|
||||
},
|
||||
[this, pc_warn, pc_error](SingleDataTransfer& data) {
|
||||
[&cpu, pc_warn, pc_error](SingleDataTransfer& data) {
|
||||
uint32_t offset = 0;
|
||||
uint32_t address = gpr[data.rn];
|
||||
uint32_t address = cpu.gpr[data.rn];
|
||||
|
||||
if (!data.pre && data.write)
|
||||
glogger.warn("Write-back enabled with post-indexing in {}",
|
||||
typeid(data).name());
|
||||
|
||||
if (data.rn == PC_INDEX && data.write)
|
||||
if (data.rn == cpu.PC_INDEX && data.write)
|
||||
glogger.warn("Write-back enabled with base register as PC {}",
|
||||
typeid(data).name());
|
||||
|
||||
@@ -156,24 +157,20 @@ CpuImpl::exec(const arm::Instruction instruction) {
|
||||
} else if (const Shift* shift = std::get_if<Shift>(&data.offset)) {
|
||||
uint8_t amount =
|
||||
(shift->data.immediate ? shift->data.operand
|
||||
: gpr[shift->data.operand] & 0xFF);
|
||||
: cpu.gpr[shift->data.operand] & 0xFF);
|
||||
|
||||
bool carry = cpsr.c();
|
||||
bool carry = cpu.cpsr.c();
|
||||
|
||||
if (!shift->data.immediate)
|
||||
pc_error(shift->data.operand);
|
||||
pc_error(shift->rm);
|
||||
|
||||
offset =
|
||||
eval_shift(shift->data.type, gpr[shift->rm], amount, carry);
|
||||
offset = eval_shift(
|
||||
shift->data.type, cpu.gpr[shift->rm], amount, carry);
|
||||
|
||||
cpsr.set_c(carry);
|
||||
cpu.cpsr.set_c(carry);
|
||||
}
|
||||
|
||||
// PC is always two instructions ahead
|
||||
if (data.rn == PC_INDEX)
|
||||
address -= 2 * INSTRUCTION_SIZE;
|
||||
|
||||
if (data.pre)
|
||||
address += (data.up ? offset : -offset);
|
||||
|
||||
@@ -181,35 +178,35 @@ CpuImpl::exec(const arm::Instruction instruction) {
|
||||
if (data.load) {
|
||||
// byte
|
||||
if (data.byte)
|
||||
gpr[data.rd] = bus->read_byte(address);
|
||||
cpu.gpr[data.rd] = cpu.bus->read_byte(address);
|
||||
// word
|
||||
else
|
||||
gpr[data.rd] = bus->read_word(address);
|
||||
cpu.gpr[data.rd] = cpu.bus->read_word(address);
|
||||
// store
|
||||
} else {
|
||||
// take PC into consideration
|
||||
if (data.rd == PC_INDEX)
|
||||
if (data.rd == cpu.PC_INDEX)
|
||||
address += INSTRUCTION_SIZE;
|
||||
|
||||
// byte
|
||||
if (data.byte)
|
||||
bus->write_byte(address, gpr[data.rd] & 0xFF);
|
||||
cpu.bus->write_byte(address, cpu.gpr[data.rd] & 0xFF);
|
||||
// word
|
||||
else
|
||||
bus->write_word(address, gpr[data.rd]);
|
||||
cpu.bus->write_word(address, cpu.gpr[data.rd]);
|
||||
}
|
||||
|
||||
if (!data.pre)
|
||||
address += (data.up ? offset : -offset);
|
||||
|
||||
if (!data.pre || data.write)
|
||||
gpr[data.rn] = address;
|
||||
cpu.gpr[data.rn] = address;
|
||||
|
||||
if (data.rd == PC_INDEX && data.load)
|
||||
is_flushed = true;
|
||||
if (data.rd == cpu.PC_INDEX && data.load)
|
||||
cpu.is_flushed = true;
|
||||
},
|
||||
[this, pc_warn, pc_error](HalfwordTransfer& data) {
|
||||
uint32_t address = gpr[data.rn];
|
||||
[&cpu, pc_warn, pc_error](HalfwordTransfer& data) {
|
||||
uint32_t address = cpu.gpr[data.rn];
|
||||
uint32_t offset = 0;
|
||||
|
||||
if (!data.pre && data.write)
|
||||
@@ -225,13 +222,13 @@ CpuImpl::exec(const arm::Instruction instruction) {
|
||||
// offset is register number (4 bits) when not an immediate
|
||||
if (!data.imm) {
|
||||
pc_error(data.offset);
|
||||
offset = gpr[data.offset];
|
||||
offset = cpu.gpr[data.offset];
|
||||
} else {
|
||||
offset = data.offset;
|
||||
}
|
||||
|
||||
// PC is always two instructions ahead
|
||||
if (data.rn == PC_INDEX)
|
||||
if (data.rn == cpu.PC_INDEX)
|
||||
address -= 2 * INSTRUCTION_SIZE;
|
||||
|
||||
if (data.pre)
|
||||
@@ -243,140 +240,154 @@ CpuImpl::exec(const arm::Instruction instruction) {
|
||||
if (data.sign) {
|
||||
// halfword
|
||||
if (data.half) {
|
||||
gpr[data.rd] = bus->read_halfword(address);
|
||||
cpu.gpr[data.rd] = cpu.bus->read_halfword(address);
|
||||
|
||||
// sign extend the halfword
|
||||
gpr[data.rd] =
|
||||
(static_cast<int32_t>(gpr[data.rd]) << 16) >> 16;
|
||||
cpu.gpr[data.rd] =
|
||||
(static_cast<int32_t>(cpu.gpr[data.rd]) << 16) >> 16;
|
||||
|
||||
// byte
|
||||
} else {
|
||||
gpr[data.rd] = bus->read_byte(address);
|
||||
cpu.gpr[data.rd] = cpu.bus->read_byte(address);
|
||||
|
||||
// sign extend the byte
|
||||
gpr[data.rd] =
|
||||
(static_cast<int32_t>(gpr[data.rd]) << 24) >> 24;
|
||||
cpu.gpr[data.rd] =
|
||||
(static_cast<int32_t>(cpu.gpr[data.rd]) << 24) >> 24;
|
||||
}
|
||||
// unsigned halfword
|
||||
} else if (data.half) {
|
||||
gpr[data.rd] = bus->read_halfword(address);
|
||||
cpu.gpr[data.rd] = cpu.bus->read_halfword(address);
|
||||
}
|
||||
// store
|
||||
} else {
|
||||
// take PC into consideration
|
||||
if (data.rd == PC_INDEX)
|
||||
if (data.rd == cpu.PC_INDEX)
|
||||
address += INSTRUCTION_SIZE;
|
||||
|
||||
// halfword
|
||||
if (data.half)
|
||||
bus->write_halfword(address, gpr[data.rd]);
|
||||
cpu.bus->write_halfword(address, cpu.gpr[data.rd]);
|
||||
}
|
||||
|
||||
if (!data.pre)
|
||||
address += (data.up ? offset : -offset);
|
||||
|
||||
if (!data.pre || data.write)
|
||||
gpr[data.rn] = address;
|
||||
cpu.gpr[data.rn] = address;
|
||||
|
||||
if (data.rd == PC_INDEX && data.load)
|
||||
is_flushed = true;
|
||||
if (data.rd == cpu.PC_INDEX && data.load)
|
||||
cpu.is_flushed = true;
|
||||
},
|
||||
[this, pc_error](BlockDataTransfer& data) {
|
||||
uint32_t address = gpr[data.rn];
|
||||
Mode mode = cpsr.mode();
|
||||
uint8_t alignment = 4; // word
|
||||
uint8_t i = 0;
|
||||
uint8_t n_regs = std::popcount(data.regs);
|
||||
[&cpu, pc_error](BlockDataTransfer& data) {
|
||||
static constexpr uint8_t alignment = 4; // word
|
||||
|
||||
uint32_t address = cpu.gpr[data.rn];
|
||||
Mode mode = cpu.cpsr.mode();
|
||||
int8_t i = 0;
|
||||
|
||||
pc_error(data.rn);
|
||||
|
||||
if (cpsr.mode() == Mode::User && data.s) {
|
||||
glogger.error("Bit S is set outside priviliged modes in {}",
|
||||
typeid(data).name());
|
||||
if (cpu.cpsr.mode() == Mode::User && data.s) {
|
||||
glogger.error("Bit S is set outside priviliged modes in block "
|
||||
"data transfer");
|
||||
}
|
||||
|
||||
// we just change modes to load user registers
|
||||
if ((!get_bit(data.regs, PC_INDEX) && data.s) ||
|
||||
if ((!get_bit(data.regs, cpu.PC_INDEX) && data.s) ||
|
||||
(!data.load && data.s)) {
|
||||
chg_mode(Mode::User);
|
||||
cpu.chg_mode(Mode::User);
|
||||
|
||||
if (data.write) {
|
||||
glogger.error(
|
||||
"Write-back enable for user bank registers in {}",
|
||||
typeid(data).name());
|
||||
glogger.error("Write-back enable for user bank registers "
|
||||
"in block data transfer");
|
||||
}
|
||||
}
|
||||
|
||||
// account for decrement
|
||||
if (!data.up)
|
||||
address -= (n_regs - 1) * alignment;
|
||||
|
||||
// increment beforehand
|
||||
if (data.pre)
|
||||
address += (data.up ? alignment : -alignment);
|
||||
|
||||
if (data.load) {
|
||||
if (get_bit(data.regs, PC_INDEX) && data.s && data.load) {
|
||||
// current mode's spsr is already loaded when it was
|
||||
if (get_bit(data.regs, cpu.PC_INDEX) && data.s && data.load) {
|
||||
// current mode's cpu.spsr is already loaded when it was
|
||||
// switched
|
||||
spsr = cpsr;
|
||||
cpu.spsr = cpu.cpsr;
|
||||
}
|
||||
|
||||
for (i = 0; i < GPR_COUNT; i++) {
|
||||
if (get_bit(data.regs, i)) {
|
||||
gpr[i] = bus->read_word(address);
|
||||
address += alignment;
|
||||
if (data.up) {
|
||||
for (i = 0; i < cpu.GPR_COUNT; i++) {
|
||||
if (get_bit(data.regs, i)) {
|
||||
cpu.gpr[i] = cpu.bus->read_word(address);
|
||||
address += alignment;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (i = cpu.GPR_COUNT - 1; i >= 0; i--) {
|
||||
if (get_bit(data.regs, i)) {
|
||||
cpu.gpr[i] = cpu.bus->read_word(address);
|
||||
address -= alignment;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (i = 0; i < GPR_COUNT; i++) {
|
||||
if (get_bit(data.regs, i)) {
|
||||
bus->write_word(address, gpr[i]);
|
||||
address += alignment;
|
||||
if (data.up) {
|
||||
for (i = 0; i < cpu.GPR_COUNT; i++) {
|
||||
if (get_bit(data.regs, i)) {
|
||||
cpu.bus->write_word(address, cpu.gpr[i]);
|
||||
address += alignment;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (i = cpu.GPR_COUNT - 1; i >= 0; i--) {
|
||||
if (get_bit(data.regs, i)) {
|
||||
cpu.bus->write_word(address, cpu.gpr[i]);
|
||||
address -= alignment;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!data.pre)
|
||||
address += (data.up ? alignment : -alignment);
|
||||
|
||||
// reset back to original address + offset if incremented earlier
|
||||
if (data.up)
|
||||
address -= n_regs * alignment;
|
||||
else
|
||||
address -= alignment;
|
||||
// fix increment
|
||||
if (data.pre)
|
||||
address += (data.up ? -alignment : alignment);
|
||||
|
||||
if (!data.pre || data.write)
|
||||
gpr[data.rn] = address;
|
||||
cpu.gpr[data.rn] = address;
|
||||
|
||||
if (data.load && get_bit(data.regs, PC_INDEX))
|
||||
is_flushed = true;
|
||||
if (data.load && get_bit(data.regs, cpu.PC_INDEX))
|
||||
cpu.is_flushed = true;
|
||||
|
||||
// load back the original mode registers
|
||||
chg_mode(mode);
|
||||
cpu.chg_mode(mode);
|
||||
},
|
||||
[this, pc_error](PsrTransfer& data) {
|
||||
if (data.spsr && cpsr.mode() == Mode::User) {
|
||||
glogger.error("Accessing SPSR in User mode in {}",
|
||||
[&cpu, pc_error](PsrTransfer& data) {
|
||||
if (data.spsr && cpu.cpsr.mode() == Mode::User) {
|
||||
glogger.error("Accessing CPU.SPSR in User mode in {}",
|
||||
typeid(data).name());
|
||||
}
|
||||
|
||||
Psr& psr = data.spsr ? spsr : cpsr;
|
||||
Psr& psr = data.spsr ? cpu.spsr : cpu.cpsr;
|
||||
|
||||
switch (data.type) {
|
||||
case PsrTransfer::Type::Mrs:
|
||||
pc_error(data.operand);
|
||||
gpr[data.operand] = psr.raw();
|
||||
cpu.gpr[data.operand] = psr.raw();
|
||||
break;
|
||||
case PsrTransfer::Type::Msr:
|
||||
pc_error(data.operand);
|
||||
|
||||
if (cpsr.mode() != Mode::User) {
|
||||
psr.set_all(gpr[data.operand]);
|
||||
if (cpu.cpsr.mode() != Mode::User) {
|
||||
if (!data.spsr) {
|
||||
Psr tmp = Psr(cpu.gpr[data.operand]);
|
||||
cpu.chg_mode(tmp.mode());
|
||||
}
|
||||
|
||||
psr.set_all(cpu.gpr[data.operand]);
|
||||
}
|
||||
break;
|
||||
case PsrTransfer::Type::Msr_flg:
|
||||
uint32_t operand =
|
||||
(data.imm ? data.operand : gpr[data.operand]);
|
||||
(data.imm ? data.operand : cpu.gpr[data.operand]);
|
||||
psr.set_n(get_bit(operand, 31));
|
||||
psr.set_z(get_bit(operand, 30));
|
||||
psr.set_c(get_bit(operand, 29));
|
||||
@@ -384,10 +395,10 @@ CpuImpl::exec(const arm::Instruction instruction) {
|
||||
break;
|
||||
}
|
||||
},
|
||||
[this, pc_error](DataProcessing& data) {
|
||||
[&cpu, pc_error](DataProcessing& data) {
|
||||
using OpCode = DataProcessing::OpCode;
|
||||
|
||||
uint32_t op_1 = gpr[data.rn];
|
||||
uint32_t op_1 = cpu.gpr[data.rn];
|
||||
uint32_t op_2 = 0;
|
||||
|
||||
uint32_t result = 0;
|
||||
@@ -398,64 +409,26 @@ CpuImpl::exec(const arm::Instruction instruction) {
|
||||
} else if (const Shift* shift = std::get_if<Shift>(&data.operand)) {
|
||||
uint8_t amount =
|
||||
(shift->data.immediate ? shift->data.operand
|
||||
: gpr[shift->data.operand] & 0xFF);
|
||||
: cpu.gpr[shift->data.operand] & 0xFF);
|
||||
|
||||
bool carry = cpsr.c();
|
||||
bool carry = cpu.cpsr.c();
|
||||
|
||||
if (!shift->data.immediate)
|
||||
pc_error(shift->data.operand);
|
||||
pc_error(shift->rm);
|
||||
|
||||
op_2 =
|
||||
eval_shift(shift->data.type, gpr[shift->rm], amount, carry);
|
||||
op_2 = eval_shift(
|
||||
shift->data.type, cpu.gpr[shift->rm], amount, carry);
|
||||
|
||||
cpsr.set_c(carry);
|
||||
cpu.cpsr.set_c(carry);
|
||||
|
||||
// PC is 12 bytes ahead when shifting
|
||||
if (data.rn == PC_INDEX)
|
||||
if (data.rn == cpu.PC_INDEX)
|
||||
op_1 += INSTRUCTION_SIZE;
|
||||
}
|
||||
|
||||
bool overflow = cpsr.v();
|
||||
bool carry = cpsr.c();
|
||||
|
||||
auto sub = [&carry, &overflow](uint32_t a, uint32_t b) -> uint32_t {
|
||||
bool s1 = get_bit(a, 31);
|
||||
bool s2 = get_bit(b, 31);
|
||||
|
||||
uint32_t result = a - b;
|
||||
|
||||
carry = b <= a;
|
||||
overflow = s1 != s2 && s2 == get_bit(result, 31);
|
||||
return result;
|
||||
};
|
||||
|
||||
auto add = [&carry, &overflow](
|
||||
uint32_t a, uint32_t b, bool c = 0) -> uint32_t {
|
||||
bool s1 = get_bit(a, 31);
|
||||
bool s2 = get_bit(b, 31);
|
||||
|
||||
// 33 bits
|
||||
uint64_t result_ = a + b + c;
|
||||
uint32_t result = result_ & 0xFFFFFFFF;
|
||||
|
||||
carry = get_bit(result_, 32);
|
||||
overflow = s1 == s2 && s2 != get_bit(result, 31);
|
||||
return result;
|
||||
};
|
||||
|
||||
auto sbc = [&carry,
|
||||
&overflow](uint32_t a, uint32_t b, bool c) -> uint32_t {
|
||||
bool s1 = get_bit(a, 31);
|
||||
bool s2 = get_bit(b, 31);
|
||||
|
||||
uint64_t result_ = a - b + c - 1;
|
||||
uint32_t result = result_ & 0xFFFFFFFF;
|
||||
|
||||
carry = get_bit(result_, 32);
|
||||
overflow = s1 != s2 && s2 == get_bit(result, 31);
|
||||
return result;
|
||||
};
|
||||
bool overflow = cpu.cpsr.v();
|
||||
bool carry = cpu.cpsr.c();
|
||||
|
||||
switch (data.opcode) {
|
||||
case OpCode::AND:
|
||||
@@ -469,23 +442,23 @@ CpuImpl::exec(const arm::Instruction instruction) {
|
||||
break;
|
||||
case OpCode::SUB:
|
||||
case OpCode::CMP:
|
||||
result = sub(op_1, op_2);
|
||||
result = sub(op_1, op_2, carry, overflow);
|
||||
break;
|
||||
case OpCode::RSB:
|
||||
result = sub(op_2, op_1);
|
||||
result = sub(op_2, op_1, carry, overflow);
|
||||
break;
|
||||
case OpCode::ADD:
|
||||
case OpCode::CMN:
|
||||
result = add(op_1, op_2);
|
||||
result = add(op_1, op_2, carry, overflow);
|
||||
break;
|
||||
case OpCode::ADC:
|
||||
result = add(op_1, op_2, carry);
|
||||
result = add(op_1, op_2, carry, overflow, carry);
|
||||
break;
|
||||
case OpCode::SBC:
|
||||
result = sbc(op_1, op_2, carry);
|
||||
result = sbc(op_1, op_2, carry, overflow, carry);
|
||||
break;
|
||||
case OpCode::RSC:
|
||||
result = sbc(op_2, op_1, carry);
|
||||
result = sbc(op_2, op_1, carry, overflow, carry);
|
||||
break;
|
||||
case OpCode::ORR:
|
||||
result = op_1 | op_2;
|
||||
@@ -501,19 +474,19 @@ CpuImpl::exec(const arm::Instruction instruction) {
|
||||
break;
|
||||
}
|
||||
|
||||
auto set_conditions = [this, carry, overflow, result]() {
|
||||
cpsr.set_c(carry);
|
||||
cpsr.set_v(overflow);
|
||||
cpsr.set_n(get_bit(result, 31));
|
||||
cpsr.set_z(result == 0);
|
||||
auto set_conditions = [&cpu, carry, overflow, result]() {
|
||||
cpu.cpsr.set_c(carry);
|
||||
cpu.cpsr.set_v(overflow);
|
||||
cpu.cpsr.set_n(get_bit(result, 31));
|
||||
cpu.cpsr.set_z(result == 0);
|
||||
};
|
||||
|
||||
if (data.set) {
|
||||
if (data.rd == PC_INDEX) {
|
||||
if (cpsr.mode() == Mode::User)
|
||||
if (data.rd == cpu.PC_INDEX) {
|
||||
if (cpu.cpsr.mode() == Mode::User)
|
||||
glogger.error("Running {} in User mode",
|
||||
typeid(data).name());
|
||||
spsr = cpsr;
|
||||
cpu.spsr = cpu.cpsr;
|
||||
} else {
|
||||
set_conditions();
|
||||
}
|
||||
@@ -523,15 +496,15 @@ CpuImpl::exec(const arm::Instruction instruction) {
|
||||
data.opcode == OpCode::CMP || data.opcode == OpCode::CMN) {
|
||||
set_conditions();
|
||||
} else {
|
||||
gpr[data.rd] = result;
|
||||
if (data.rd == PC_INDEX || data.opcode == OpCode::MVN)
|
||||
is_flushed = true;
|
||||
cpu.gpr[data.rd] = result;
|
||||
if (data.rd == cpu.PC_INDEX || data.opcode == OpCode::MVN)
|
||||
cpu.is_flushed = true;
|
||||
}
|
||||
},
|
||||
[this](SoftwareInterrupt) {
|
||||
chg_mode(Mode::Supervisor);
|
||||
pc = 0x08;
|
||||
spsr = cpsr;
|
||||
[&cpu](SoftwareInterrupt) {
|
||||
cpu.chg_mode(Mode::Supervisor);
|
||||
cpu.pc = 0x08;
|
||||
cpu.spsr = cpu.cpsr;
|
||||
},
|
||||
[](auto& data) {
|
||||
glogger.error("Unimplemented {} instruction", typeid(data).name());
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include "instruction.hh"
|
||||
#include "cpu/arm/instruction.hh"
|
||||
#include "util/bits.hh"
|
||||
#include <iterator>
|
||||
|
||||
namespace matar::arm {
|
||||
Instruction::Instruction(uint32_t insn)
|
||||
@@ -13,13 +12,11 @@ Instruction::Instruction(uint32_t insn)
|
||||
|
||||
// Branch
|
||||
} else if ((insn & 0x0E000000) == 0x0A000000) {
|
||||
bool link = get_bit(insn, 24);
|
||||
uint32_t offset = bit_range(insn, 0, 23);
|
||||
bool link = get_bit(insn, 24);
|
||||
int32_t offset = static_cast<int32_t>(bit_range(insn, 0, 23));
|
||||
|
||||
// lsh 2 and sign extend the 26 bit offset to 32 bits
|
||||
offset = (static_cast<int32_t>(offset) << 8) >> 6;
|
||||
|
||||
offset += 2 * INSTRUCTION_SIZE;
|
||||
offset = (offset << 8) >> 6;
|
||||
|
||||
data = Branch{ .link = link, .offset = offset };
|
||||
|
||||
|
||||
@@ -1,145 +0,0 @@
|
||||
#include "cpu-impl.hh"
|
||||
#include "util/bits.hh"
|
||||
#include "util/log.hh"
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <type_traits>
|
||||
|
||||
namespace matar {
|
||||
CpuImpl::CpuImpl(const Bus& bus) noexcept
|
||||
: bus(std::make_shared<Bus>(bus))
|
||||
, gpr({ 0 })
|
||||
, cpsr(0)
|
||||
, spsr(0)
|
||||
, is_flushed(false)
|
||||
, gpr_banked({ { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 } })
|
||||
, spsr_banked({ 0, 0, 0, 0, 0 }) {
|
||||
cpsr.set_mode(Mode::Supervisor);
|
||||
cpsr.set_irq_disabled(true);
|
||||
cpsr.set_fiq_disabled(true);
|
||||
cpsr.set_state(State::Arm);
|
||||
glogger.info("CPU successfully initialised");
|
||||
|
||||
// PC always points to two instructions ahead
|
||||
// PC - 2 is the instruction being executed
|
||||
pc += 2 * arm::INSTRUCTION_SIZE;
|
||||
}
|
||||
|
||||
/* change modes */
|
||||
void
|
||||
CpuImpl::chg_mode(const Mode to) {
|
||||
Mode from = cpsr.mode();
|
||||
|
||||
if (from == to)
|
||||
return;
|
||||
|
||||
/* TODO: replace visible registers with view once I understand how to
|
||||
* concatenate views */
|
||||
#define STORE_BANKED(mode, MODE) \
|
||||
std::copy(gpr.begin() + GPR_##MODE##_FIRST, \
|
||||
gpr.begin() + gpr.size() - 1, \
|
||||
gpr_banked.mode.begin())
|
||||
|
||||
switch (from) {
|
||||
case Mode::Fiq:
|
||||
STORE_BANKED(fiq, FIQ);
|
||||
spsr_banked.fiq = spsr;
|
||||
break;
|
||||
|
||||
case Mode::Supervisor:
|
||||
STORE_BANKED(svc, SVC);
|
||||
spsr_banked.svc = spsr;
|
||||
break;
|
||||
|
||||
case Mode::Abort:
|
||||
STORE_BANKED(abt, ABT);
|
||||
spsr_banked.abt = spsr;
|
||||
break;
|
||||
|
||||
case Mode::Irq:
|
||||
STORE_BANKED(irq, IRQ);
|
||||
spsr_banked.irq = spsr;
|
||||
break;
|
||||
|
||||
case Mode::Undefined:
|
||||
STORE_BANKED(und, UND);
|
||||
spsr_banked.und = spsr;
|
||||
break;
|
||||
|
||||
case Mode::User:
|
||||
case Mode::System:
|
||||
STORE_BANKED(old, SYS_USR);
|
||||
break;
|
||||
}
|
||||
|
||||
#define RESTORE_BANKED(mode, MODE) \
|
||||
std::copy(gpr_banked.mode.begin(), \
|
||||
gpr_banked.mode.end(), \
|
||||
gpr.begin() + GPR_##MODE##_FIRST)
|
||||
|
||||
switch (to) {
|
||||
case Mode::Fiq:
|
||||
RESTORE_BANKED(fiq, FIQ);
|
||||
spsr = spsr_banked.fiq;
|
||||
break;
|
||||
|
||||
case Mode::Supervisor:
|
||||
RESTORE_BANKED(svc, SVC);
|
||||
spsr = spsr_banked.svc;
|
||||
break;
|
||||
|
||||
case Mode::Abort:
|
||||
RESTORE_BANKED(abt, ABT);
|
||||
spsr = spsr_banked.abt;
|
||||
break;
|
||||
|
||||
case Mode::Irq:
|
||||
RESTORE_BANKED(irq, IRQ);
|
||||
spsr = spsr_banked.irq;
|
||||
break;
|
||||
|
||||
case Mode::Undefined:
|
||||
RESTORE_BANKED(und, UND);
|
||||
spsr = spsr_banked.und;
|
||||
break;
|
||||
|
||||
case Mode::User:
|
||||
case Mode::System:
|
||||
STORE_BANKED(old, SYS_USR);
|
||||
break;
|
||||
}
|
||||
|
||||
#undef RESTORE_BANKED
|
||||
|
||||
cpsr.set_mode(to);
|
||||
}
|
||||
|
||||
void
|
||||
CpuImpl::step() {
|
||||
// Current instruction is two instructions behind PC
|
||||
uint32_t cur_pc = pc - 2 * arm::INSTRUCTION_SIZE;
|
||||
|
||||
if (cpsr.state() == State::Arm) {
|
||||
uint32_t x = bus->read_word(cur_pc);
|
||||
arm::Instruction instruction(x);
|
||||
|
||||
exec(instruction);
|
||||
|
||||
#ifdef DISASSEMBLER
|
||||
glogger.info("{:#034b}", x);
|
||||
glogger.info("0x{:08X} : {}", cur_pc, instruction.disassemble());
|
||||
#endif
|
||||
|
||||
if (is_flushed) {
|
||||
// if flushed, do not increment the PC, instead set it to two
|
||||
// instructions ahead to account for flushed "fetch" and "decode"
|
||||
// instructions
|
||||
pc += 2 * arm::INSTRUCTION_SIZE;
|
||||
is_flushed = false;
|
||||
} else {
|
||||
// if not flushed continue like normal
|
||||
pc += arm::INSTRUCTION_SIZE;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,64 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "bus.hh"
|
||||
#include "cpu/arm/instruction.hh"
|
||||
#include "cpu/psr.hh"
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace matar {
|
||||
class CpuImpl {
|
||||
public:
|
||||
CpuImpl(const Bus& bus) noexcept;
|
||||
|
||||
void step();
|
||||
void chg_mode(const Mode to);
|
||||
void exec(const arm::Instruction instruction);
|
||||
|
||||
// TODO: get rid of this
|
||||
#ifndef MATAR_CPU_TESTS
|
||||
private:
|
||||
#endif
|
||||
|
||||
static constexpr uint8_t GPR_COUNT = 16;
|
||||
|
||||
static constexpr uint8_t GPR_FIQ_FIRST = 8;
|
||||
static constexpr uint8_t GPR_SVC_FIRST = 13;
|
||||
static constexpr uint8_t GPR_ABT_FIRST = 13;
|
||||
static constexpr uint8_t GPR_IRQ_FIRST = 13;
|
||||
static constexpr uint8_t GPR_UND_FIRST = 13;
|
||||
static constexpr uint8_t GPR_SYS_USR_FIRST = 8;
|
||||
|
||||
std::shared_ptr<Bus> bus;
|
||||
std::array<uint32_t, GPR_COUNT> gpr; // general purpose registers
|
||||
|
||||
Psr cpsr; // current program status register
|
||||
Psr spsr; // status program status register
|
||||
|
||||
static constexpr uint8_t PC_INDEX = 15;
|
||||
static_assert(PC_INDEX < GPR_COUNT);
|
||||
|
||||
uint32_t& pc = gpr[PC_INDEX];
|
||||
|
||||
bool is_flushed;
|
||||
|
||||
struct {
|
||||
std::array<uint32_t, GPR_COUNT - GPR_FIQ_FIRST - 1> fiq;
|
||||
std::array<uint32_t, GPR_COUNT - GPR_SVC_FIRST - 1> svc;
|
||||
std::array<uint32_t, GPR_COUNT - GPR_ABT_FIRST - 1> abt;
|
||||
std::array<uint32_t, GPR_COUNT - GPR_IRQ_FIRST - 1> irq;
|
||||
std::array<uint32_t, GPR_COUNT - GPR_UND_FIRST - 1> und;
|
||||
|
||||
// visible registers before the mode switch
|
||||
std::array<uint32_t, GPR_COUNT - GPR_SYS_USR_FIRST> old;
|
||||
} gpr_banked; // banked general purpose registers
|
||||
|
||||
struct {
|
||||
Psr fiq;
|
||||
Psr svc;
|
||||
Psr abt;
|
||||
Psr irq;
|
||||
Psr und;
|
||||
} spsr_banked; // banked saved program status registers
|
||||
};
|
||||
}
|
||||
173
src/cpu/cpu.cc
173
src/cpu/cpu.cc
@@ -1,14 +1,177 @@
|
||||
#include "cpu/cpu.hh"
|
||||
#include "cpu-impl.hh"
|
||||
#include "cpu/arm/instruction.hh"
|
||||
#include "cpu/thumb/instruction.hh"
|
||||
#include "util/bits.hh"
|
||||
#include "util/log.hh"
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
|
||||
namespace matar {
|
||||
Cpu::Cpu(const Bus& bus) noexcept
|
||||
: impl(std::make_unique<CpuImpl>(bus)){};
|
||||
: bus(std::make_shared<Bus>(bus))
|
||||
, gpr({ 0 })
|
||||
, cpsr(0)
|
||||
, spsr(0)
|
||||
, gpr_banked({ { 0 }, { 0 }, { 0 }, { 0 }, { 0 }, { 0 } })
|
||||
, spsr_banked({ 0, 0, 0, 0, 0 })
|
||||
, is_flushed(false) {
|
||||
cpsr.set_mode(Mode::Supervisor);
|
||||
cpsr.set_irq_disabled(true);
|
||||
cpsr.set_fiq_disabled(true);
|
||||
cpsr.set_state(State::Arm);
|
||||
glogger.info("CPU successfully initialised");
|
||||
|
||||
Cpu::~Cpu() = default;
|
||||
// PC always points to two instructions ahead
|
||||
// PC - 2 is the instruction being executed
|
||||
pc += 2 * arm::INSTRUCTION_SIZE;
|
||||
}
|
||||
|
||||
/* change modes */
|
||||
void
|
||||
Cpu::chg_mode(const Mode to) {
|
||||
Mode from = cpsr.mode();
|
||||
|
||||
if (from == to)
|
||||
return;
|
||||
|
||||
/* TODO: replace visible registers with view once I understand how to
|
||||
* concatenate views */
|
||||
#define STORE_BANKED(mode, MODE) \
|
||||
std::copy(gpr.begin() + GPR_##MODE##_FIRST, \
|
||||
gpr.end() - 1, \
|
||||
gpr_banked.mode.begin())
|
||||
|
||||
switch (from) {
|
||||
case Mode::Fiq:
|
||||
STORE_BANKED(fiq, FIQ);
|
||||
spsr_banked.fiq = spsr;
|
||||
std::copy(gpr_banked.old.begin(),
|
||||
gpr_banked.old.end() - 2, // dont copy R13 and R14
|
||||
gpr.begin() + GPR_OLD_FIRST);
|
||||
break;
|
||||
|
||||
case Mode::Supervisor:
|
||||
STORE_BANKED(svc, SVC);
|
||||
spsr_banked.svc = spsr;
|
||||
break;
|
||||
|
||||
case Mode::Abort:
|
||||
STORE_BANKED(abt, ABT);
|
||||
spsr_banked.abt = spsr;
|
||||
break;
|
||||
|
||||
case Mode::Irq:
|
||||
STORE_BANKED(irq, IRQ);
|
||||
spsr_banked.irq = spsr;
|
||||
break;
|
||||
|
||||
case Mode::Undefined:
|
||||
STORE_BANKED(und, UND);
|
||||
spsr_banked.und = spsr;
|
||||
break;
|
||||
|
||||
case Mode::User:
|
||||
case Mode::System:
|
||||
// we only take care of r13 and r14, because FIQ takes care of the
|
||||
// rest
|
||||
gpr_banked.old[5] = gpr[13];
|
||||
gpr_banked.old[6] = gpr[14];
|
||||
break;
|
||||
}
|
||||
|
||||
#undef STORE_BANKED
|
||||
|
||||
#define RESTORE_BANKED(mode, MODE) \
|
||||
std::copy(gpr_banked.mode.begin(), \
|
||||
gpr_banked.mode.end(), \
|
||||
gpr.begin() + GPR_##MODE##_FIRST)
|
||||
|
||||
switch (to) {
|
||||
case Mode::Fiq:
|
||||
RESTORE_BANKED(fiq, FIQ);
|
||||
spsr = spsr_banked.fiq;
|
||||
std::copy(gpr.begin() + GPR_FIQ_FIRST,
|
||||
gpr.end() - 2, // dont copy R13 and R14
|
||||
gpr_banked.old.begin());
|
||||
break;
|
||||
|
||||
case Mode::Supervisor:
|
||||
RESTORE_BANKED(svc, SVC);
|
||||
spsr = spsr_banked.svc;
|
||||
break;
|
||||
|
||||
case Mode::Abort:
|
||||
RESTORE_BANKED(abt, ABT);
|
||||
spsr = spsr_banked.abt;
|
||||
break;
|
||||
|
||||
case Mode::Irq:
|
||||
RESTORE_BANKED(irq, IRQ);
|
||||
spsr = spsr_banked.irq;
|
||||
break;
|
||||
|
||||
case Mode::Undefined:
|
||||
RESTORE_BANKED(und, UND);
|
||||
spsr = spsr_banked.und;
|
||||
break;
|
||||
|
||||
case Mode::User:
|
||||
case Mode::System:
|
||||
gpr[13] = gpr_banked.old[5];
|
||||
gpr[14] = gpr_banked.old[6];
|
||||
break;
|
||||
}
|
||||
|
||||
#undef RESTORE_BANKED
|
||||
|
||||
cpsr.set_mode(to);
|
||||
glogger.info_bold("Mode changed from {:b} to {:b}",
|
||||
static_cast<uint32_t>(from),
|
||||
static_cast<uint32_t>(to));
|
||||
}
|
||||
|
||||
void
|
||||
Cpu::step() {
|
||||
impl->step();
|
||||
};
|
||||
// halfword align
|
||||
rst_bit(pc, 0);
|
||||
if (cpsr.state() == State::Arm) {
|
||||
// word align
|
||||
rst_bit(pc, 1);
|
||||
// Current instruction is two instructions behind PC
|
||||
uint32_t cur_pc = pc - 2 * arm::INSTRUCTION_SIZE;
|
||||
arm::Instruction instruction(bus->read_word(cur_pc));
|
||||
|
||||
#ifdef DISASSEMBLER
|
||||
glogger.info("0x{:08X} : {}", cur_pc, instruction.disassemble());
|
||||
#endif
|
||||
|
||||
instruction.exec(*this);
|
||||
} else {
|
||||
uint32_t cur_pc = pc - 2 * thumb::INSTRUCTION_SIZE;
|
||||
thumb::Instruction instruction(bus->read_halfword(cur_pc));
|
||||
|
||||
#ifdef DISASSEMBLER
|
||||
glogger.info("0x{:08X} : {}", cur_pc, instruction.disassemble());
|
||||
#endif
|
||||
|
||||
instruction.exec(*this);
|
||||
}
|
||||
|
||||
// advance PC
|
||||
{
|
||||
size_t size = cpsr.state() == State::Arm ? arm::INSTRUCTION_SIZE
|
||||
: thumb::INSTRUCTION_SIZE;
|
||||
|
||||
if (is_flushed) {
|
||||
// if flushed, do not increment the PC, instead set it to two
|
||||
// instructions ahead to account for flushed "fetch" and "decode"
|
||||
// instructions
|
||||
pc += 2 * size;
|
||||
is_flushed = false;
|
||||
} else {
|
||||
// if not flushed continue like normal
|
||||
pc += size;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
lib_sources += files(
|
||||
'cpu-impl.cc',
|
||||
'cpu.cc',
|
||||
'psr.cc',
|
||||
'alu.cc'
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#include "psr.hh"
|
||||
#include "cpu/psr.hh"
|
||||
#include "util/bits.hh"
|
||||
#include "util/log.hh"
|
||||
|
||||
namespace matar {
|
||||
Psr::Psr(uint32_t raw)
|
||||
@@ -13,17 +12,17 @@ Psr::raw() const {
|
||||
|
||||
void
|
||||
Psr::set_all(uint32_t raw) {
|
||||
psr = raw & ~PSR_CLEAR_RESERVED;
|
||||
psr = raw;
|
||||
}
|
||||
|
||||
Mode
|
||||
Psr::mode() const {
|
||||
return static_cast<Mode>(psr & ~PSR_CLEAR_MODE);
|
||||
return static_cast<Mode>(psr & 0b11111);
|
||||
}
|
||||
|
||||
void
|
||||
Psr::set_mode(Mode mode) {
|
||||
psr &= PSR_CLEAR_MODE;
|
||||
psr &= 0b00000;
|
||||
psr |= static_cast<uint32_t>(mode);
|
||||
}
|
||||
|
||||
@@ -91,7 +90,7 @@ Psr::condition(Condition cond) const {
|
||||
case Condition::LE:
|
||||
return z() || (n() != v());
|
||||
case Condition::AL:
|
||||
return true && state() == State::Arm;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "instruction.hh"
|
||||
#include "cpu/thumb/instruction.hh"
|
||||
#include "util/bits.hh"
|
||||
#include <format>
|
||||
|
||||
namespace matar::thumb {
|
||||
std::string
|
||||
@@ -7,14 +8,14 @@ Instruction::disassemble() {
|
||||
return std::visit(
|
||||
overloaded{
|
||||
[](MoveShiftedRegister& data) {
|
||||
return fmt::format("{} R{:d},R{:d},#{:d}",
|
||||
return std::format("{} R{:d},R{:d},#{:d}",
|
||||
stringify(data.opcode),
|
||||
data.rd,
|
||||
data.rs,
|
||||
data.offset);
|
||||
},
|
||||
[](AddSubtract& data) {
|
||||
return fmt::format("{} R{:d},R{:d},{}{:d}",
|
||||
return std::format("{} R{:d},R{:d},{}{:d}",
|
||||
stringify(data.opcode),
|
||||
data.rd,
|
||||
data.rs,
|
||||
@@ -22,27 +23,27 @@ Instruction::disassemble() {
|
||||
data.offset);
|
||||
},
|
||||
[](MovCmpAddSubImmediate& data) {
|
||||
return fmt::format(
|
||||
return std::format(
|
||||
"{} R{:d},#{:d}", stringify(data.opcode), data.rd, data.offset);
|
||||
},
|
||||
[](AluOperations& data) {
|
||||
return fmt::format(
|
||||
return std::format(
|
||||
"{} R{:d},R{:d}", stringify(data.opcode), data.rd, data.rs);
|
||||
},
|
||||
[](HiRegisterOperations& data) {
|
||||
if (data.opcode == HiRegisterOperations::OpCode::BX) {
|
||||
return fmt::format("{} R{:d}", stringify(data.opcode), data.rs);
|
||||
return std::format("{} R{:d}", stringify(data.opcode), data.rs);
|
||||
}
|
||||
|
||||
return fmt::format(
|
||||
return std::format(
|
||||
"{} R{:d},R{:d}", stringify(data.opcode), data.rd, data.rs);
|
||||
},
|
||||
|
||||
[](PcRelativeLoad& data) {
|
||||
return fmt::format("LDR R{:d},[PC,#{:d}]", data.rd, data.word);
|
||||
return std::format("LDR R{:d},[PC,#{:d}]", data.rd, data.word);
|
||||
},
|
||||
[](LoadStoreRegisterOffset& data) {
|
||||
return fmt::format("{}{} R{:d},[R{:d},R{:d}]",
|
||||
return std::format("{}{} R{:d},[R{:d},R{:d}]",
|
||||
(data.load ? "LDR" : "STR"),
|
||||
(data.byte ? "B" : ""),
|
||||
data.rd,
|
||||
@@ -51,11 +52,11 @@ Instruction::disassemble() {
|
||||
},
|
||||
[](LoadStoreSignExtendedHalfword& data) {
|
||||
if (!data.s && !data.h) {
|
||||
return fmt::format(
|
||||
return std::format(
|
||||
"STRH R{:d},[R{:d},R{:d}]", data.rd, data.rb, data.ro);
|
||||
}
|
||||
|
||||
return fmt::format("{}{} R{:d},[R{:d},R{:d}]",
|
||||
return std::format("{}{} R{:d},[R{:d},R{:d}]",
|
||||
(data.s ? "LDS" : "LDR"),
|
||||
(data.h ? 'H' : 'B'),
|
||||
data.rd,
|
||||
@@ -63,7 +64,7 @@ Instruction::disassemble() {
|
||||
data.ro);
|
||||
},
|
||||
[](LoadStoreImmediateOffset& data) {
|
||||
return fmt::format("{}{} R{:d},[R{:d},#{:d}]",
|
||||
return std::format("{}{} R{:d},[R{:d},#{:d}]",
|
||||
(data.load ? "LDR" : "STR"),
|
||||
(data.byte ? "B" : ""),
|
||||
data.rd,
|
||||
@@ -71,34 +72,33 @@ Instruction::disassemble() {
|
||||
data.offset);
|
||||
},
|
||||
[](LoadStoreHalfword& data) {
|
||||
return fmt::format("{} R{:d},[R{:d},#{:d}]",
|
||||
return std::format("{} R{:d},[R{:d},#{:d}]",
|
||||
(data.load ? "LDRH" : "STRH"),
|
||||
data.rd,
|
||||
data.rb,
|
||||
data.offset);
|
||||
},
|
||||
[](SpRelativeLoad& data) {
|
||||
return fmt::format("{} R{:d},[SP,#{:d}]",
|
||||
return std::format("{} R{:d},[SP,#{:d}]",
|
||||
(data.load ? "LDR" : "STR"),
|
||||
data.rd,
|
||||
data.word);
|
||||
},
|
||||
[](LoadAddress& data) {
|
||||
return fmt::format("ADD R{:d},{},#{:d}",
|
||||
return std::format("ADD R{:d},{},#{:d}",
|
||||
data.rd,
|
||||
(data.sp ? "SP" : "PC"),
|
||||
data.word);
|
||||
},
|
||||
[](AddOffsetStackPointer& data) {
|
||||
return fmt::format(
|
||||
"ADD SP,#{}{:d}", (data.sign ? '-' : '+'), data.word);
|
||||
return std::format("ADD SP,#{:d}", data.word);
|
||||
},
|
||||
[](PushPopRegister& data) {
|
||||
std::string regs;
|
||||
|
||||
for (uint8_t i = 0; i < 16; i++) {
|
||||
if (get_bit(data.regs, i))
|
||||
fmt::format_to(std::back_inserter(regs), "R{:d},", i);
|
||||
std::format_to(std::back_inserter(regs), "R{:d},", i);
|
||||
};
|
||||
|
||||
if (data.load) {
|
||||
@@ -107,14 +107,14 @@ Instruction::disassemble() {
|
||||
else
|
||||
regs.pop_back();
|
||||
|
||||
return fmt::format("POP {{{}}}", regs);
|
||||
return std::format("POP {{{}}}", regs);
|
||||
} else {
|
||||
if (data.pclr)
|
||||
regs += "LR";
|
||||
else
|
||||
regs.pop_back();
|
||||
|
||||
return fmt::format("PUSH {{{}}}", regs);
|
||||
return std::format("PUSH {{{}}}", regs);
|
||||
}
|
||||
},
|
||||
[](MultipleLoad& data) {
|
||||
@@ -122,27 +122,32 @@ Instruction::disassemble() {
|
||||
|
||||
for (uint8_t i = 0; i < 16; i++) {
|
||||
if (get_bit(data.regs, i))
|
||||
fmt::format_to(std::back_inserter(regs), "R{:d},", i);
|
||||
std::format_to(std::back_inserter(regs), "R{:d},", i);
|
||||
};
|
||||
|
||||
regs.pop_back();
|
||||
|
||||
return fmt::format(
|
||||
return std::format(
|
||||
"{} R{}!,{{{}}}", (data.load ? "LDMIA" : "STMIA"), data.rb, regs);
|
||||
},
|
||||
[](SoftwareInterrupt) { return std::string("SWI"); },
|
||||
[](SoftwareInterrupt& data) {
|
||||
return std::format("SWI {:d}", data.vector);
|
||||
},
|
||||
[](ConditionalBranch& data) {
|
||||
return fmt::format("B{} {:d}",
|
||||
stringify(data.condition),
|
||||
data.offset);
|
||||
return std::format(
|
||||
"B{} #{:d}",
|
||||
stringify(data.condition),
|
||||
static_cast<int32_t>(data.offset + 2 * INSTRUCTION_SIZE));
|
||||
},
|
||||
[](UnconditionalBranch& data) {
|
||||
return fmt::format("B {:d}", data.offset);
|
||||
return std::format(
|
||||
"B #{:d}",
|
||||
static_cast<int32_t>(data.offset + 2 * INSTRUCTION_SIZE));
|
||||
},
|
||||
[](LongBranchWithLink& data) {
|
||||
// duh this manual be empty for H = 0
|
||||
return fmt::format(
|
||||
"BL{} {:d}", (data.high ? "H" : ""), data.offset);
|
||||
return std::format(
|
||||
"BL{} #{:d}", (data.high ? "H" : ""), data.offset);
|
||||
},
|
||||
[](auto) { return std::string("unknown instruction"); } },
|
||||
data);
|
||||
|
||||
391
src/cpu/thumb/exec.cc
Normal file
391
src/cpu/thumb/exec.cc
Normal file
@@ -0,0 +1,391 @@
|
||||
#include "cpu/cpu.hh"
|
||||
#include "util/bits.hh"
|
||||
#include "util/log.hh"
|
||||
|
||||
namespace matar::thumb {
|
||||
void
|
||||
Instruction::exec(Cpu& cpu) {
|
||||
auto set_cc = [&cpu](bool c, bool v, bool n, bool z) {
|
||||
cpu.cpsr.set_c(c);
|
||||
cpu.cpsr.set_v(v);
|
||||
cpu.cpsr.set_n(n);
|
||||
cpu.cpsr.set_z(z);
|
||||
};
|
||||
|
||||
std::visit(
|
||||
overloaded{
|
||||
[&cpu, set_cc](MoveShiftedRegister& data) {
|
||||
if (data.opcode == ShiftType::ROR)
|
||||
glogger.error("Invalid opcode in {}", typeid(data).name());
|
||||
|
||||
bool carry = cpu.cpsr.c();
|
||||
|
||||
uint32_t shifted =
|
||||
eval_shift(data.opcode, cpu.gpr[data.rs], data.offset, carry);
|
||||
|
||||
cpu.gpr[data.rd] = shifted;
|
||||
|
||||
set_cc(carry, cpu.cpsr.v(), get_bit(shifted, 31), shifted == 0);
|
||||
},
|
||||
[&cpu, set_cc](AddSubtract& data) {
|
||||
uint32_t offset =
|
||||
data.imm ? static_cast<uint32_t>(static_cast<int8_t>(data.offset))
|
||||
: cpu.gpr[data.offset];
|
||||
uint32_t result = 0;
|
||||
bool carry = cpu.cpsr.c();
|
||||
bool overflow = cpu.cpsr.v();
|
||||
|
||||
switch (data.opcode) {
|
||||
case AddSubtract::OpCode::ADD:
|
||||
result = add(cpu.gpr[data.rs], offset, carry, overflow);
|
||||
break;
|
||||
case AddSubtract::OpCode::SUB:
|
||||
result = sub(cpu.gpr[data.rs], offset, carry, overflow);
|
||||
break;
|
||||
}
|
||||
|
||||
cpu.gpr[data.rd] = result;
|
||||
set_cc(carry, overflow, get_bit(result, 31), result == 0);
|
||||
},
|
||||
[&cpu, set_cc](MovCmpAddSubImmediate& data) {
|
||||
uint32_t result = 0;
|
||||
bool carry = cpu.cpsr.c();
|
||||
bool overflow = cpu.cpsr.v();
|
||||
|
||||
switch (data.opcode) {
|
||||
case MovCmpAddSubImmediate::OpCode::MOV:
|
||||
result = data.offset;
|
||||
carry = 0;
|
||||
break;
|
||||
case MovCmpAddSubImmediate::OpCode::ADD:
|
||||
result =
|
||||
add(cpu.gpr[data.rd], data.offset, carry, overflow);
|
||||
break;
|
||||
case MovCmpAddSubImmediate::OpCode::SUB:
|
||||
case MovCmpAddSubImmediate::OpCode::CMP:
|
||||
result =
|
||||
sub(cpu.gpr[data.rd], data.offset, carry, overflow);
|
||||
break;
|
||||
}
|
||||
|
||||
set_cc(carry, overflow, get_bit(result, 31), result == 0);
|
||||
if (data.opcode != MovCmpAddSubImmediate::OpCode::CMP)
|
||||
cpu.gpr[data.rd] = result;
|
||||
},
|
||||
[&cpu, set_cc](AluOperations& data) {
|
||||
uint32_t op_1 = cpu.gpr[data.rd];
|
||||
uint32_t op_2 = cpu.gpr[data.rs];
|
||||
uint32_t result = 0;
|
||||
|
||||
bool carry = cpu.cpsr.c();
|
||||
bool overflow = cpu.cpsr.v();
|
||||
|
||||
switch (data.opcode) {
|
||||
case AluOperations::OpCode::AND:
|
||||
case AluOperations::OpCode::TST:
|
||||
result = op_1 & op_2;
|
||||
break;
|
||||
case AluOperations::OpCode::EOR:
|
||||
result = op_1 ^ op_2;
|
||||
break;
|
||||
case AluOperations::OpCode::LSL:
|
||||
result = eval_shift(ShiftType::LSL, op_1, op_2, carry);
|
||||
break;
|
||||
case AluOperations::OpCode::LSR:
|
||||
result = eval_shift(ShiftType::LSR, op_1, op_2, carry);
|
||||
break;
|
||||
case AluOperations::OpCode::ASR:
|
||||
result = eval_shift(ShiftType::ASR, op_1, op_2, carry);
|
||||
break;
|
||||
case AluOperations::OpCode::ADC:
|
||||
result = add(op_1, op_2, carry, overflow, carry);
|
||||
break;
|
||||
case AluOperations::OpCode::SBC:
|
||||
result = sbc(op_1, op_2, carry, overflow, carry);
|
||||
break;
|
||||
case AluOperations::OpCode::ROR:
|
||||
result = eval_shift(ShiftType::ROR, op_1, op_2, carry);
|
||||
break;
|
||||
case AluOperations::OpCode::NEG:
|
||||
result = -op_2;
|
||||
break;
|
||||
case AluOperations::OpCode::CMP:
|
||||
result = sub(op_1, op_2, carry, overflow);
|
||||
break;
|
||||
case AluOperations::OpCode::CMN:
|
||||
result = add(op_1, op_2, carry, overflow);
|
||||
break;
|
||||
case AluOperations::OpCode::ORR:
|
||||
result = op_1 | op_2;
|
||||
break;
|
||||
case AluOperations::OpCode::MUL:
|
||||
result = op_1 * op_2;
|
||||
break;
|
||||
case AluOperations::OpCode::BIC:
|
||||
result = op_1 & ~op_2;
|
||||
break;
|
||||
case AluOperations::OpCode::MVN:
|
||||
result = ~op_2;
|
||||
break;
|
||||
}
|
||||
|
||||
if (data.opcode != AluOperations::OpCode::TST &&
|
||||
data.opcode != AluOperations::OpCode::CMP &&
|
||||
data.opcode != AluOperations::OpCode::CMN)
|
||||
cpu.gpr[data.rd] = result;
|
||||
|
||||
set_cc(carry, overflow, get_bit(result, 31), result == 0);
|
||||
},
|
||||
[&cpu, set_cc](HiRegisterOperations& data) {
|
||||
uint32_t op_1 = cpu.gpr[data.rd];
|
||||
uint32_t op_2 = cpu.gpr[data.rs];
|
||||
|
||||
bool carry = cpu.cpsr.c();
|
||||
bool overflow = cpu.cpsr.v();
|
||||
|
||||
// PC is already current + 4, so dont need to do that
|
||||
if (data.rd == cpu.PC_INDEX)
|
||||
rst_bit(op_1, 0);
|
||||
|
||||
if (data.rs == cpu.PC_INDEX)
|
||||
rst_bit(op_2, 0);
|
||||
|
||||
switch (data.opcode) {
|
||||
case HiRegisterOperations::OpCode::ADD: {
|
||||
cpu.gpr[data.rd] = add(op_1, op_2, carry, overflow);
|
||||
|
||||
if (data.rd == cpu.PC_INDEX)
|
||||
cpu.is_flushed = true;
|
||||
} break;
|
||||
case HiRegisterOperations::OpCode::CMP: {
|
||||
uint32_t result = sub(op_1, op_2, carry, overflow);
|
||||
set_cc(carry, overflow, get_bit(result, 31), result == 0);
|
||||
} break;
|
||||
case HiRegisterOperations::OpCode::MOV: {
|
||||
cpu.gpr[data.rd] = op_2;
|
||||
|
||||
if (data.rd == cpu.PC_INDEX)
|
||||
cpu.is_flushed = true;
|
||||
} break;
|
||||
case HiRegisterOperations::OpCode::BX: {
|
||||
State state = static_cast<State>(get_bit(op_2, 0));
|
||||
|
||||
if (state != cpu.cpsr.state())
|
||||
glogger.info_bold("State changed");
|
||||
|
||||
// set state
|
||||
cpu.cpsr.set_state(state);
|
||||
|
||||
// copy to PC
|
||||
cpu.pc = op_2;
|
||||
|
||||
// ignore [1:0] bits for arm and 0 bit for thumb
|
||||
rst_bit(cpu.pc, 0);
|
||||
|
||||
if (state == State::Arm)
|
||||
rst_bit(cpu.pc, 1);
|
||||
|
||||
// pc is affected so flush the pipeline
|
||||
cpu.is_flushed = true;
|
||||
} break;
|
||||
}
|
||||
},
|
||||
[&cpu](PcRelativeLoad& data) {
|
||||
uint32_t pc = cpu.pc;
|
||||
rst_bit(pc, 0);
|
||||
rst_bit(pc, 1);
|
||||
|
||||
cpu.gpr[data.rd] = cpu.bus->read_word(pc + data.word);
|
||||
},
|
||||
[&cpu](LoadStoreRegisterOffset& data) {
|
||||
uint32_t address = cpu.gpr[data.rb] + cpu.gpr[data.ro];
|
||||
|
||||
if (data.load) {
|
||||
if (data.byte) {
|
||||
cpu.gpr[data.rd] = cpu.bus->read_byte(address);
|
||||
} else {
|
||||
cpu.gpr[data.rd] = cpu.bus->read_word(address);
|
||||
}
|
||||
} else {
|
||||
if (data.byte) {
|
||||
cpu.bus->write_byte(address, cpu.gpr[data.rd] & 0xFF);
|
||||
} else {
|
||||
cpu.bus->write_word(address, cpu.gpr[data.rd]);
|
||||
}
|
||||
}
|
||||
},
|
||||
[&cpu](LoadStoreSignExtendedHalfword& data) {
|
||||
uint32_t address = cpu.gpr[data.rb] + cpu.gpr[data.ro];
|
||||
|
||||
switch (data.s << 1 | data.h) {
|
||||
case 0b00:
|
||||
cpu.bus->write_halfword(address, cpu.gpr[data.rd] & 0xFFFF);
|
||||
break;
|
||||
case 0b01:
|
||||
cpu.gpr[data.rd] = cpu.bus->read_halfword(address);
|
||||
break;
|
||||
case 0b10:
|
||||
// sign extend and load the byte
|
||||
cpu.gpr[data.rd] =
|
||||
(static_cast<int32_t>(cpu.bus->read_byte(address))
|
||||
<< 24) >>
|
||||
24;
|
||||
break;
|
||||
case 0b11:
|
||||
// sign extend the halfword
|
||||
cpu.gpr[data.rd] =
|
||||
(static_cast<int32_t>(cpu.bus->read_halfword(address))
|
||||
<< 16) >>
|
||||
16;
|
||||
break;
|
||||
|
||||
// unreachable
|
||||
default: {
|
||||
}
|
||||
}
|
||||
},
|
||||
[&cpu](LoadStoreImmediateOffset& data) {
|
||||
uint32_t address = cpu.gpr[data.rb] + data.offset;
|
||||
|
||||
if (data.load) {
|
||||
if (data.byte) {
|
||||
cpu.gpr[data.rd] = cpu.bus->read_byte(address);
|
||||
} else {
|
||||
cpu.gpr[data.rd] = cpu.bus->read_word(address);
|
||||
}
|
||||
} else {
|
||||
if (data.byte) {
|
||||
cpu.bus->write_byte(address, cpu.gpr[data.rd] & 0xFF);
|
||||
} else {
|
||||
cpu.bus->write_word(address, cpu.gpr[data.rd]);
|
||||
}
|
||||
}
|
||||
},
|
||||
[&cpu](LoadStoreHalfword& data) {
|
||||
uint32_t address = cpu.gpr[data.rb] + data.offset;
|
||||
|
||||
if (data.load) {
|
||||
cpu.gpr[data.rd] = cpu.bus->read_halfword(address);
|
||||
} else {
|
||||
cpu.bus->write_halfword(address, cpu.gpr[data.rd] & 0xFFFF);
|
||||
}
|
||||
},
|
||||
[&cpu](SpRelativeLoad& data) {
|
||||
uint32_t address = cpu.sp + data.word;
|
||||
|
||||
if (data.load) {
|
||||
cpu.gpr[data.rd] = cpu.bus->read_word(address);
|
||||
} else {
|
||||
cpu.bus->write_word(address, cpu.gpr[data.rd]);
|
||||
}
|
||||
},
|
||||
[&cpu](LoadAddress& data) {
|
||||
if (data.sp) {
|
||||
cpu.gpr[data.rd] = cpu.sp + data.word;
|
||||
} else {
|
||||
// PC is already current + 4, so dont need to do that
|
||||
// force bit 1 to 0
|
||||
cpu.gpr[data.rd] = (cpu.pc & ~(1 << 1)) + data.word;
|
||||
}
|
||||
},
|
||||
[&cpu](AddOffsetStackPointer& data) { cpu.sp += data.word; },
|
||||
[&cpu](PushPopRegister& data) {
|
||||
static constexpr uint8_t alignment = 4;
|
||||
|
||||
if (data.load) {
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
if (get_bit(data.regs, i)) {
|
||||
cpu.gpr[i] = cpu.bus->read_word(cpu.sp);
|
||||
cpu.sp += alignment;
|
||||
}
|
||||
}
|
||||
|
||||
if (data.pclr) {
|
||||
cpu.pc = cpu.bus->read_word(cpu.sp);
|
||||
cpu.sp += alignment;
|
||||
cpu.is_flushed = true;
|
||||
}
|
||||
} else {
|
||||
if (data.pclr) {
|
||||
cpu.sp -= alignment;
|
||||
cpu.bus->write_word(cpu.sp, cpu.lr);
|
||||
}
|
||||
|
||||
for (int8_t i = 7; i >= 0; i--) {
|
||||
if (get_bit(data.regs, i)) {
|
||||
cpu.sp -= alignment;
|
||||
cpu.bus->write_word(cpu.sp, cpu.gpr[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
[&cpu](MultipleLoad& data) {
|
||||
static constexpr uint8_t alignment = 4;
|
||||
|
||||
uint32_t rb = cpu.gpr[data.rb];
|
||||
|
||||
if (data.load) {
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
if (get_bit(data.regs, i)) {
|
||||
cpu.gpr[i] = cpu.bus->read_word(rb);
|
||||
rb += alignment;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (int8_t i = 7; i >= 0; i--) {
|
||||
if (get_bit(data.regs, i)) {
|
||||
rb -= alignment;
|
||||
cpu.bus->write_word(rb, cpu.gpr[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cpu.gpr[data.rb] = rb;
|
||||
},
|
||||
[&cpu](ConditionalBranch& data) {
|
||||
if (data.condition == Condition::AL)
|
||||
glogger.warn("Condition 1110 (AL) is undefined");
|
||||
|
||||
if (!cpu.cpsr.condition(data.condition))
|
||||
return;
|
||||
|
||||
cpu.pc += data.offset;
|
||||
cpu.is_flushed = true;
|
||||
},
|
||||
[&cpu](SoftwareInterrupt& data) {
|
||||
// next instruction is one instruction behind PC
|
||||
cpu.lr = cpu.pc - INSTRUCTION_SIZE;
|
||||
cpu.spsr = cpu.cpsr;
|
||||
cpu.pc = data.vector;
|
||||
cpu.cpsr.set_state(State::Arm);
|
||||
cpu.chg_mode(Mode::Supervisor);
|
||||
cpu.is_flushed = true;
|
||||
},
|
||||
[&cpu](UnconditionalBranch& data) {
|
||||
cpu.pc += data.offset;
|
||||
cpu.is_flushed = true;
|
||||
},
|
||||
[&cpu](LongBranchWithLink& data) {
|
||||
// 12 bit integer
|
||||
int32_t offset = data.offset;
|
||||
|
||||
if (data.high) {
|
||||
uint32_t old_pc = cpu.pc;
|
||||
|
||||
cpu.pc = cpu.lr + offset;
|
||||
cpu.lr = (old_pc - INSTRUCTION_SIZE) | 1;
|
||||
cpu.is_flushed = true;
|
||||
} else {
|
||||
// 12 + 11 = 23 bit
|
||||
offset <<= 11;
|
||||
// sign extend
|
||||
offset = (offset << 9) >> 9;
|
||||
cpu.lr = cpu.pc + offset;
|
||||
}
|
||||
},
|
||||
[](auto& data) {
|
||||
glogger.error("Unknown thumb format : {}", typeid(data).name());
|
||||
} },
|
||||
data);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "instruction.hh"
|
||||
#include "cpu/thumb/instruction.hh"
|
||||
#include "util/bits.hh"
|
||||
#include "util/log.hh"
|
||||
|
||||
namespace matar::thumb {
|
||||
Instruction::Instruction(uint16_t insn) {
|
||||
@@ -55,16 +56,20 @@ Instruction::Instruction(uint16_t insn) {
|
||||
HiRegisterOperations::OpCode opcode =
|
||||
static_cast<HiRegisterOperations::OpCode>(bit_range(insn, 8, 9));
|
||||
|
||||
if (opcode == HiRegisterOperations::OpCode::BX && hi_1)
|
||||
glogger.warn("H1 set with BX");
|
||||
|
||||
rd += (hi_1 ? LO_GPR_COUNT : 0);
|
||||
rs += (hi_2 ? LO_GPR_COUNT : 0);
|
||||
|
||||
data = HiRegisterOperations{ .rd = rd, .rs = rs, .opcode = opcode };
|
||||
// Format 6: PC-relative load
|
||||
} else if ((insn & 0xF800) == 0x4800) {
|
||||
uint8_t word = bit_range(insn, 0, 7);
|
||||
uint8_t rd = bit_range(insn, 8, 10);
|
||||
uint16_t word = bit_range(insn, 0, 7);
|
||||
uint8_t rd = bit_range(insn, 8, 10);
|
||||
|
||||
data = PcRelativeLoad{ .word = word, .rd = rd };
|
||||
data =
|
||||
PcRelativeLoad{ .word = static_cast<uint16_t>(word << 2), .rd = rd };
|
||||
|
||||
// Format 7: Load/store with register offset
|
||||
} else if ((insn & 0xF200) == 0x5000) {
|
||||
@@ -91,13 +96,16 @@ Instruction::Instruction(uint16_t insn) {
|
||||
};
|
||||
|
||||
// Format 9: Load/store with immediate offset
|
||||
} else if ((insn & 0xF000) == 0x6000) {
|
||||
} else if ((insn & 0xE000) == 0x6000) {
|
||||
uint8_t rd = bit_range(insn, 0, 2);
|
||||
uint8_t rb = bit_range(insn, 3, 5);
|
||||
uint8_t offset = bit_range(insn, 6, 10);
|
||||
bool load = get_bit(insn, 11);
|
||||
bool byte = get_bit(insn, 12);
|
||||
|
||||
if (!byte)
|
||||
offset <<= 2;
|
||||
|
||||
data = LoadStoreImmediateOffset{
|
||||
.rd = rd, .rb = rb, .offset = offset, .load = load, .byte = byte
|
||||
};
|
||||
@@ -109,40 +117,43 @@ Instruction::Instruction(uint16_t insn) {
|
||||
uint8_t offset = bit_range(insn, 6, 10);
|
||||
bool load = get_bit(insn, 11);
|
||||
|
||||
offset <<= 1;
|
||||
|
||||
data = LoadStoreHalfword{
|
||||
.rd = rd, .rb = rb, .offset = offset, .load = load
|
||||
};
|
||||
|
||||
// Format 11: SP-relative load/store
|
||||
} else if ((insn & 0xF000) == 0x9000) {
|
||||
uint8_t word = bit_range(insn, 0, 7);
|
||||
uint8_t rd = bit_range(insn, 8, 10);
|
||||
bool load = get_bit(insn, 11);
|
||||
uint16_t word = bit_range(insn, 0, 7);
|
||||
uint8_t rd = bit_range(insn, 8, 10);
|
||||
bool load = get_bit(insn, 11);
|
||||
|
||||
word <<= 2;
|
||||
|
||||
data = SpRelativeLoad{ .word = word, .rd = rd, .load = load };
|
||||
|
||||
// Format 12: Load address
|
||||
} else if ((insn & 0xF000) == 0xA000) {
|
||||
uint8_t word = bit_range(insn, 0, 7);
|
||||
uint8_t rd = bit_range(insn, 8, 10);
|
||||
bool sp = get_bit(insn, 11);
|
||||
uint16_t word = bit_range(insn, 0, 7);
|
||||
uint8_t rd = bit_range(insn, 8, 10);
|
||||
bool sp = get_bit(insn, 11);
|
||||
|
||||
data = LoadAddress{ .word = word, .rd = rd, .sp = sp };
|
||||
|
||||
// Format 12: Load address
|
||||
} else if ((insn & 0xF000) == 0xA000) {
|
||||
uint8_t word = bit_range(insn, 0, 7);
|
||||
uint8_t rd = bit_range(insn, 8, 10);
|
||||
bool sp = get_bit(insn, 11);
|
||||
word <<= 2;
|
||||
|
||||
data = LoadAddress{ .word = word, .rd = rd, .sp = sp };
|
||||
|
||||
// Format 13: Add offset to stack pointer
|
||||
} else if ((insn & 0xFF00) == 0xB000) {
|
||||
uint8_t word = bit_range(insn, 0, 6);
|
||||
int16_t word = static_cast<int16_t>(bit_range(insn, 0, 6));
|
||||
bool sign = get_bit(insn, 7);
|
||||
|
||||
data = AddOffsetStackPointer{ .word = word, .sign = sign };
|
||||
word <<= 2;
|
||||
word = static_cast<int16_t>(word * (sign ? -1 : 1));
|
||||
|
||||
data = AddOffsetStackPointer{
|
||||
.word = word,
|
||||
};
|
||||
|
||||
// Format 14: Push/pop registers
|
||||
} else if ((insn & 0xF600) == 0xB400) {
|
||||
@@ -162,30 +173,41 @@ Instruction::Instruction(uint16_t insn) {
|
||||
|
||||
// Format 17: Software interrupt
|
||||
} else if ((insn & 0xFF00) == 0xDF00) {
|
||||
data = SoftwareInterrupt{};
|
||||
uint8_t vector = bit_range(insn, 0, 7);
|
||||
|
||||
data = SoftwareInterrupt{ .vector = vector };
|
||||
|
||||
// Format 16: Conditional branch
|
||||
} else if ((insn & 0xF000) == 0xD000) {
|
||||
uint16_t offset = bit_range(insn, 0, 7);
|
||||
int32_t offset = bit_range(insn, 0, 7);
|
||||
Condition condition = static_cast<Condition>(bit_range(insn, 8, 11));
|
||||
|
||||
data = ConditionalBranch{ .offset = static_cast<uint16_t>(offset << 1),
|
||||
.condition = condition };
|
||||
offset <<= 1;
|
||||
|
||||
// sign extend the 9 bit integer
|
||||
offset = (offset << 23) >> 23;
|
||||
|
||||
data = ConditionalBranch{ .offset = offset, .condition = condition };
|
||||
|
||||
// Format 18: Unconditional branch
|
||||
} else if ((insn & 0xF800) == 0xE000) {
|
||||
uint16_t offset = bit_range(insn, 0, 10);
|
||||
int32_t offset = bit_range(insn, 0, 10);
|
||||
|
||||
data =
|
||||
UnconditionalBranch{ .offset = static_cast<uint16_t>(offset << 1) };
|
||||
offset <<= 1;
|
||||
|
||||
// sign extend the 12 bit integer
|
||||
offset = (offset << 20) >> 20;
|
||||
|
||||
data = UnconditionalBranch{ .offset = offset };
|
||||
|
||||
// Format 19: Long branch with link
|
||||
} else if ((insn & 0xF000) == 0xF000) {
|
||||
uint16_t offset = bit_range(insn, 0, 10);
|
||||
bool high = get_bit(insn, 11);
|
||||
|
||||
data = LongBranchWithLink{ .offset = static_cast<uint16_t>(offset << 1),
|
||||
.high = high };
|
||||
offset <<= 1;
|
||||
|
||||
data = LongBranchWithLink{ .offset = offset, .high = high };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
lib_sources += files(
|
||||
'instruction.cc'
|
||||
'instruction.cc',
|
||||
'exec.cc'
|
||||
)
|
||||
|
||||
if get_option('disassembler')
|
||||
|
||||
279
src/io/io.cc
Normal file
279
src/io/io.cc
Normal file
@@ -0,0 +1,279 @@
|
||||
#include "io/io.hh"
|
||||
#include "util/bits.hh"
|
||||
#include "util/log.hh"
|
||||
|
||||
namespace matar {
|
||||
#define ADDR static constexpr uint32_t
|
||||
|
||||
// lcd
|
||||
ADDR DISPCNT = 0x4000000;
|
||||
ADDR DISPSTAT = 0x4000004;
|
||||
ADDR VCOUNT = 0x4000006;
|
||||
ADDR BG0CNT = 0x4000008;
|
||||
ADDR BG1CNT = 0x400000A;
|
||||
ADDR BG2CNT = 0x400000C;
|
||||
ADDR BG3CNT = 0x400000E;
|
||||
ADDR BG0HOFS = 0x4000010;
|
||||
ADDR BG0VOFS = 0x4000012;
|
||||
ADDR BG1HOFS = 0x4000014;
|
||||
ADDR BG1VOFS = 0x4000016;
|
||||
ADDR BG2HOFS = 0x4000018;
|
||||
ADDR BG2VOFS = 0x400001A;
|
||||
ADDR BG3HOFS = 0x400001C;
|
||||
ADDR BG3VOFS = 0x400001E;
|
||||
ADDR BG2PA = 0x4000020;
|
||||
ADDR BG2PB = 0x4000022;
|
||||
ADDR BG2PC = 0x4000024;
|
||||
ADDR BG2PD = 0x4000026;
|
||||
ADDR BG2X_L = 0x4000028;
|
||||
ADDR BG2X_H = 0x400002A;
|
||||
ADDR BG2Y_L = 0x400002C;
|
||||
ADDR BG2Y_H = 0x400002E;
|
||||
ADDR BG3PA = 0x4000030;
|
||||
ADDR BG3PB = 0x4000032;
|
||||
ADDR BG3PC = 0x4000034;
|
||||
ADDR BG3PD = 0x4000036;
|
||||
ADDR BG3X_L = 0x4000038;
|
||||
ADDR BG3X_H = 0x400003A;
|
||||
ADDR BG3Y_L = 0x400003C;
|
||||
ADDR BG3Y_H = 0x400003E;
|
||||
ADDR WIN0H = 0x4000040;
|
||||
ADDR WIN1H = 0x4000042;
|
||||
ADDR WIN0V = 0x4000044;
|
||||
ADDR WIN1V = 0x4000046;
|
||||
ADDR WININ = 0x4000048;
|
||||
ADDR WINOUT = 0x400004A;
|
||||
ADDR MOSAIC = 0x400004C;
|
||||
ADDR BLDCNT = 0x4000050;
|
||||
ADDR BLDALPHA = 0x4000052;
|
||||
ADDR BLDY = 0x4000054;
|
||||
|
||||
// sound
|
||||
ADDR SOUND1CNT_L = 0x4000060;
|
||||
ADDR SOUND1CNT_H = 0x4000062;
|
||||
ADDR SOUND1CNT_X = 0x4000064;
|
||||
ADDR SOUND2CNT_L = 0x4000068;
|
||||
ADDR SOUND2CNT_H = 0x400006C;
|
||||
ADDR SOUND3CNT_L = 0x4000070;
|
||||
ADDR SOUND3CNT_H = 0x4000072;
|
||||
ADDR SOUND3CNT_X = 0x4000074;
|
||||
ADDR SOUND4CNT_L = 0x4000078;
|
||||
ADDR SOUND4CNT_H = 0x400007C;
|
||||
ADDR SOUNDCNT_L = 0x4000080;
|
||||
ADDR SOUNDCNT_H = 0x4000082;
|
||||
ADDR SOUNDCNT_X = 0x4000084;
|
||||
ADDR SOUNDBIAS = 0x4000088;
|
||||
ADDR WAVE_RAM0_L = 0x4000090;
|
||||
ADDR WAVE_RAM0_H = 0x4000092;
|
||||
ADDR WAVE_RAM1_L = 0x4000094;
|
||||
ADDR WAVE_RAM1_H = 0x4000096;
|
||||
ADDR WAVE_RAM2_L = 0x4000098;
|
||||
ADDR WAVE_RAM2_H = 0x400009A;
|
||||
ADDR WAVE_RAM3_L = 0x400009C;
|
||||
ADDR WAVE_RAM3_H = 0x400009E;
|
||||
ADDR FIFO_A_L = 0x40000A0;
|
||||
ADDR FIFO_A_H = 0x40000A2;
|
||||
ADDR FIFO_B_L = 0x40000A4;
|
||||
ADDR FIFO_B_H = 0x40000A6;
|
||||
|
||||
// system
|
||||
ADDR POSTFLG = 0x4000300;
|
||||
ADDR IME = 0x4000208;
|
||||
ADDR IE = 0x4000200;
|
||||
ADDR IF = 0x4000202;
|
||||
ADDR WAITCNT = 0x4000204;
|
||||
ADDR HALTCNT = 0x4000301;
|
||||
|
||||
#undef ADDR
|
||||
|
||||
uint8_t
|
||||
IoDevices::read_byte(uint32_t address) const {
|
||||
uint16_t halfword = read_halfword(address & ~1);
|
||||
|
||||
if (address & 1)
|
||||
halfword >>= 8;
|
||||
|
||||
return halfword & 0xFF;
|
||||
}
|
||||
|
||||
void
|
||||
IoDevices::write_byte(uint32_t address, uint8_t byte) {
|
||||
uint16_t halfword = read_halfword(address & ~1);
|
||||
|
||||
if (address & 1)
|
||||
write_halfword(address & ~1,
|
||||
(static_cast<uint16_t>(byte) << 8) | (halfword & 0xFF));
|
||||
else
|
||||
write_halfword(address & ~1,
|
||||
(static_cast<uint16_t>(byte) | (halfword & 0xFF00)));
|
||||
}
|
||||
|
||||
uint32_t
|
||||
IoDevices::read_word(uint32_t address) const {
|
||||
return read_halfword(address) | read_halfword(address + 2) << 16;
|
||||
}
|
||||
|
||||
void
|
||||
IoDevices::write_word(uint32_t address, uint32_t word) {
|
||||
write_halfword(address, word & 0xFFFF);
|
||||
write_halfword(address + 2, (word >> 16) & 0xFFFF);
|
||||
}
|
||||
|
||||
uint16_t
|
||||
IoDevices::read_halfword(uint32_t address) const {
|
||||
switch (address) {
|
||||
|
||||
#define READ(name, var) \
|
||||
case name: \
|
||||
return var;
|
||||
|
||||
// lcd
|
||||
READ(DISPCNT, lcd.lcd_control)
|
||||
READ(DISPSTAT, lcd.general_lcd_status)
|
||||
READ(VCOUNT, lcd.vertical_counter)
|
||||
READ(WININ, lcd.inside_win_0_1)
|
||||
READ(WINOUT, lcd.outside_win)
|
||||
READ(BLDCNT, lcd.color_special_effects_selection)
|
||||
READ(BLDALPHA, lcd.alpha_blending_coefficients)
|
||||
|
||||
// sound
|
||||
READ(SOUND1CNT_L, sound.ch1_sweep)
|
||||
READ(SOUND1CNT_H, sound.ch1_duty_length_env)
|
||||
READ(SOUND1CNT_X, sound.ch1_freq_control)
|
||||
READ(SOUND2CNT_L, sound.ch2_duty_length_env)
|
||||
READ(SOUND2CNT_H, sound.ch2_freq_control)
|
||||
READ(SOUND3CNT_L, sound.ch3_stop_wave_ram_select)
|
||||
READ(SOUND3CNT_H, sound.ch3_length_volume)
|
||||
READ(SOUND3CNT_X, sound.ch3_freq_control)
|
||||
READ(WAVE_RAM0_L, sound.ch3_wave_pattern[0]);
|
||||
READ(WAVE_RAM0_H, sound.ch3_wave_pattern[1]);
|
||||
READ(WAVE_RAM1_L, sound.ch3_wave_pattern[2]);
|
||||
READ(WAVE_RAM1_H, sound.ch3_wave_pattern[3]);
|
||||
READ(WAVE_RAM2_L, sound.ch3_wave_pattern[4]);
|
||||
READ(WAVE_RAM2_H, sound.ch3_wave_pattern[5]);
|
||||
READ(WAVE_RAM3_L, sound.ch3_wave_pattern[6]);
|
||||
READ(WAVE_RAM3_H, sound.ch3_wave_pattern[7]);
|
||||
READ(SOUND4CNT_L, sound.ch4_length_env);
|
||||
READ(SOUND4CNT_H, sound.ch4_freq_control);
|
||||
READ(SOUNDCNT_L, sound.ctrl_stereo_volume);
|
||||
READ(SOUNDCNT_H, sound.ctrl_mixing);
|
||||
READ(SOUNDCNT_X, sound.ctrl_sound_on_off);
|
||||
READ(SOUNDBIAS, sound.pwm_control);
|
||||
|
||||
// system
|
||||
READ(POSTFLG, system.post_boot_flag)
|
||||
READ(IME, system.interrupt_master_enabler)
|
||||
READ(IE, system.interrupt_enable);
|
||||
READ(IF, system.interrupt_request_flags);
|
||||
READ(WAITCNT, system.waitstate_control);
|
||||
|
||||
#undef READ
|
||||
|
||||
default:
|
||||
glogger.warn("Unused IO address read at 0x{:08X}", address);
|
||||
}
|
||||
|
||||
return 0xFF;
|
||||
}
|
||||
|
||||
void
|
||||
IoDevices::write_halfword(uint32_t address, uint16_t halfword) {
|
||||
switch (address) {
|
||||
|
||||
#define WRITE(name, var) \
|
||||
case name: \
|
||||
var = halfword; \
|
||||
break;
|
||||
|
||||
#define WRITE_2(name, var, val) \
|
||||
case name: \
|
||||
var = val; \
|
||||
break;
|
||||
|
||||
// lcd
|
||||
WRITE(DISPCNT, lcd.lcd_control)
|
||||
WRITE(DISPSTAT, lcd.general_lcd_status)
|
||||
WRITE(BG0CNT, lcd.bg0_control)
|
||||
WRITE(BG1CNT, lcd.bg1_control)
|
||||
WRITE(BG2CNT, lcd.bg2_control)
|
||||
WRITE(BG3CNT, lcd.bg3_control)
|
||||
WRITE(BG0HOFS, lcd.bg0_x_offset)
|
||||
WRITE(BG0VOFS, lcd.bg0_y_offset)
|
||||
WRITE(BG1HOFS, lcd.bg1_x_offset)
|
||||
WRITE(BG1VOFS, lcd.bg1_y_offset)
|
||||
WRITE(BG2HOFS, lcd.bg2_x_offset)
|
||||
WRITE(BG2VOFS, lcd.bg2_y_offset)
|
||||
WRITE(BG3HOFS, lcd.bg3_x_offset)
|
||||
WRITE(BG3VOFS, lcd.bg3_y_offset)
|
||||
WRITE(BG2PA, lcd.bg2_rot_scaling_parameters[0])
|
||||
WRITE(BG2PB, lcd.bg2_rot_scaling_parameters[1])
|
||||
WRITE(BG2PC, lcd.bg2_rot_scaling_parameters[2])
|
||||
WRITE(BG2PD, lcd.bg2_rot_scaling_parameters[3])
|
||||
WRITE(BG2X_L, lcd.bg2_reference_x[0])
|
||||
WRITE(BG2X_H, lcd.bg2_reference_x[1])
|
||||
WRITE(BG2Y_L, lcd.bg2_reference_y[0])
|
||||
WRITE(BG2Y_H, lcd.bg2_reference_y[1])
|
||||
WRITE(BG3PA, lcd.bg3_rot_scaling_parameters[0])
|
||||
WRITE(BG3PB, lcd.bg3_rot_scaling_parameters[1])
|
||||
WRITE(BG3PC, lcd.bg3_rot_scaling_parameters[2])
|
||||
WRITE(BG3PD, lcd.bg3_rot_scaling_parameters[3])
|
||||
WRITE(BG3X_L, lcd.bg3_reference_x[0])
|
||||
WRITE(BG3X_H, lcd.bg3_reference_x[1])
|
||||
WRITE(BG3Y_L, lcd.bg3_reference_y[0])
|
||||
WRITE(BG3Y_H, lcd.bg3_reference_y[1])
|
||||
WRITE(WIN0H, lcd.win0_horizontal_dimensions)
|
||||
WRITE(WIN1H, lcd.win1_horizontal_dimensions)
|
||||
WRITE(WIN0V, lcd.win0_vertical_dimensions)
|
||||
WRITE(WIN1V, lcd.win1_vertical_dimensions)
|
||||
WRITE(WININ, lcd.inside_win_0_1)
|
||||
WRITE(WINOUT, lcd.outside_win)
|
||||
WRITE(MOSAIC, lcd.mosaic_size)
|
||||
WRITE(BLDCNT, lcd.color_special_effects_selection)
|
||||
WRITE(BLDALPHA, lcd.alpha_blending_coefficients)
|
||||
WRITE(BLDY, lcd.brightness_coefficient)
|
||||
|
||||
// sound
|
||||
WRITE(SOUND1CNT_L, sound.ch1_sweep)
|
||||
WRITE(SOUND1CNT_H, sound.ch1_duty_length_env)
|
||||
WRITE(SOUND1CNT_X, sound.ch1_freq_control)
|
||||
WRITE(SOUND2CNT_L, sound.ch2_duty_length_env)
|
||||
WRITE(SOUND2CNT_H, sound.ch2_freq_control)
|
||||
WRITE(SOUND3CNT_L, sound.ch3_stop_wave_ram_select)
|
||||
WRITE(SOUND3CNT_H, sound.ch3_length_volume)
|
||||
WRITE(SOUND3CNT_X, sound.ch3_freq_control)
|
||||
WRITE(WAVE_RAM0_L, sound.ch3_wave_pattern[0]);
|
||||
WRITE(WAVE_RAM0_H, sound.ch3_wave_pattern[1]);
|
||||
WRITE(WAVE_RAM1_L, sound.ch3_wave_pattern[2]);
|
||||
WRITE(WAVE_RAM1_H, sound.ch3_wave_pattern[3]);
|
||||
WRITE(WAVE_RAM2_L, sound.ch3_wave_pattern[4]);
|
||||
WRITE(WAVE_RAM2_H, sound.ch3_wave_pattern[5]);
|
||||
WRITE(WAVE_RAM3_L, sound.ch3_wave_pattern[6]);
|
||||
WRITE(WAVE_RAM3_H, sound.ch3_wave_pattern[7]);
|
||||
WRITE(SOUND4CNT_L, sound.ch4_length_env);
|
||||
WRITE(SOUND4CNT_H, sound.ch4_freq_control);
|
||||
WRITE(SOUNDCNT_L, sound.ctrl_stereo_volume);
|
||||
WRITE(SOUNDCNT_H, sound.ctrl_mixing);
|
||||
WRITE(SOUNDCNT_X, sound.ctrl_sound_on_off);
|
||||
WRITE(SOUNDBIAS, sound.pwm_control);
|
||||
WRITE(FIFO_A_L, sound.fifo_a[0]);
|
||||
WRITE(FIFO_A_H, sound.fifo_a[1]);
|
||||
WRITE(FIFO_B_L, sound.fifo_b[0]);
|
||||
WRITE(FIFO_B_H, sound.fifo_b[1]);
|
||||
|
||||
// system
|
||||
WRITE_2(POSTFLG, system.post_boot_flag, halfword & 1)
|
||||
WRITE_2(IME, system.interrupt_master_enabler, halfword & 1)
|
||||
WRITE(IE, system.interrupt_enable);
|
||||
WRITE(IF, system.interrupt_request_flags);
|
||||
WRITE(WAITCNT, system.waitstate_control);
|
||||
WRITE_2(HALTCNT, system.low_power_mode, get_bit(halfword, 7));
|
||||
|
||||
#undef WRITE
|
||||
#undef WRITE_2
|
||||
|
||||
default:
|
||||
glogger.warn("Unused IO address written at 0x{:08X}", address);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
3
src/io/meson.build
Normal file
3
src/io/meson.build
Normal file
@@ -0,0 +1,3 @@
|
||||
lib_sources += files(
|
||||
'io.cc',
|
||||
)
|
||||
@@ -1,9 +1,7 @@
|
||||
#include "memory.hh"
|
||||
#include "header.hh"
|
||||
#include "util/bits.hh"
|
||||
#include "util/crypto.hh"
|
||||
#include "util/log.hh"
|
||||
#include <bitset>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace matar {
|
||||
@@ -34,64 +32,49 @@ Memory::Memory(std::array<uint8_t, BIOS_SIZE>&& bios,
|
||||
glogger.info("Cartridge Title: {}", header.title);
|
||||
};
|
||||
|
||||
#define MATCHES(area) address >= area##_START&& address <= area##_END
|
||||
|
||||
uint8_t
|
||||
Memory::read(size_t address) const {
|
||||
if (MATCHES(BIOS)) {
|
||||
return bios[address];
|
||||
} else if (MATCHES(BOARD_WRAM)) {
|
||||
return board_wram[address - BOARD_WRAM_START];
|
||||
} else if (MATCHES(CHIP_WRAM)) {
|
||||
return chip_wram[address - CHIP_WRAM_START];
|
||||
} else if (MATCHES(PALETTE_RAM)) {
|
||||
return palette_ram[address - PALETTE_RAM_START];
|
||||
} else if (MATCHES(VRAM)) {
|
||||
return vram[address - VRAM_START];
|
||||
} else if (MATCHES(OAM_OBJ_ATTR)) {
|
||||
return oam_obj_attr[address - OAM_OBJ_ATTR_START];
|
||||
} else if (MATCHES(ROM_0)) {
|
||||
return rom[address - ROM_0_START];
|
||||
} else if (MATCHES(ROM_1)) {
|
||||
return rom[address - ROM_1_START];
|
||||
} else if (MATCHES(ROM_2)) {
|
||||
return rom[address - ROM_2_START];
|
||||
} else {
|
||||
glogger.error("Invalid memory region accessed");
|
||||
return 0xFF;
|
||||
}
|
||||
Memory::read(uint32_t address) const {
|
||||
#define MATCHES(AREA, area) \
|
||||
if (address >= AREA##_START && address < AREA##_START + area.size()) \
|
||||
return area[address - AREA##_START];
|
||||
|
||||
MATCHES(BIOS, bios)
|
||||
MATCHES(BOARD_WRAM, board_wram)
|
||||
MATCHES(CHIP_WRAM, chip_wram)
|
||||
MATCHES(PALETTE_RAM, palette_ram)
|
||||
MATCHES(VRAM, vram)
|
||||
MATCHES(OAM_OBJ_ATTR, oam_obj_attr)
|
||||
MATCHES(ROM_0, rom)
|
||||
MATCHES(ROM_1, rom)
|
||||
MATCHES(ROM_2, rom)
|
||||
|
||||
glogger.error("Invalid memory region accessed");
|
||||
return 0xFF;
|
||||
|
||||
#undef MATCHES
|
||||
}
|
||||
|
||||
void
|
||||
Memory::write(size_t address, uint8_t byte) {
|
||||
if (MATCHES(BIOS)) {
|
||||
bios[address] = byte;
|
||||
} else if (MATCHES(BOARD_WRAM)) {
|
||||
board_wram[address - BOARD_WRAM_START] = byte;
|
||||
} else if (MATCHES(CHIP_WRAM)) {
|
||||
chip_wram[address - CHIP_WRAM_START] = byte;
|
||||
} else if (MATCHES(PALETTE_RAM)) {
|
||||
palette_ram[address - PALETTE_RAM_START] = byte;
|
||||
} else if (MATCHES(VRAM)) {
|
||||
vram[address - VRAM_START] = byte;
|
||||
} else if (MATCHES(OAM_OBJ_ATTR)) {
|
||||
oam_obj_attr[address - OAM_OBJ_ATTR_START] = byte;
|
||||
} else if (MATCHES(ROM_0)) {
|
||||
rom[address - ROM_0_START] = byte;
|
||||
} else if (MATCHES(ROM_1)) {
|
||||
rom[address - ROM_1_START] = byte;
|
||||
} else if (MATCHES(ROM_2)) {
|
||||
rom[address - ROM_2_START] = byte;
|
||||
} else {
|
||||
glogger.error("Invalid memory region accessed");
|
||||
Memory::write(uint32_t address, uint8_t byte) {
|
||||
#define MATCHES(AREA, area) \
|
||||
if (address >= AREA##_START && address < AREA##_START + area.size()) { \
|
||||
area[address - AREA##_START] = byte; \
|
||||
return; \
|
||||
}
|
||||
}
|
||||
|
||||
MATCHES(BOARD_WRAM, board_wram)
|
||||
MATCHES(CHIP_WRAM, chip_wram)
|
||||
MATCHES(PALETTE_RAM, palette_ram)
|
||||
MATCHES(VRAM, vram)
|
||||
MATCHES(OAM_OBJ_ATTR, oam_obj_attr)
|
||||
|
||||
glogger.error("Invalid memory region accessed");
|
||||
|
||||
#undef MATCHES
|
||||
}
|
||||
|
||||
void
|
||||
Memory::parse_header() {
|
||||
|
||||
if (rom.size() < header.HEADER_SIZE) {
|
||||
throw std::out_of_range(
|
||||
"ROM is not large enough to even have a header");
|
||||
@@ -174,7 +157,7 @@ Memory::parse_header() {
|
||||
if (rom[0xB2] != 0x96)
|
||||
glogger.error("HEADER: invalid fixed byte at 0xB2");
|
||||
|
||||
for (size_t i = 0xB5; i < 0xBC; i++) {
|
||||
for (uint32_t i = 0xB5; i < 0xBC; i++) {
|
||||
if (rom[i] != 0x00)
|
||||
glogger.error("HEADER: invalid fixed bytes at 0xB5");
|
||||
}
|
||||
@@ -183,7 +166,7 @@ Memory::parse_header() {
|
||||
|
||||
// checksum
|
||||
{
|
||||
size_t i = 0xA0, chk = 0;
|
||||
uint32_t i = 0xA0, chk = 0;
|
||||
while (i <= 0xBC)
|
||||
chk -= rom[i++];
|
||||
chk -= 0x19;
|
||||
|
||||
@@ -1,19 +1,14 @@
|
||||
lib_sources = files(
|
||||
'memory.cc',
|
||||
'bus.cc'
|
||||
'bus.cc',
|
||||
)
|
||||
|
||||
subdir('util')
|
||||
subdir('cpu')
|
||||
subdir('io')
|
||||
|
||||
lib_cpp_args = []
|
||||
|
||||
fmt = dependency('fmt', version : '>=10.1.0', static: true)
|
||||
if not fmt.found()
|
||||
fmt = dependency('fmt', version : '>=10.1.0', static: false)
|
||||
lib_cpp_args += '-DFMT_HEADER_ONLY'
|
||||
endif
|
||||
|
||||
if get_option('disassembler')
|
||||
lib_cpp_args += '-DDISASSEMBLER'
|
||||
endif
|
||||
@@ -21,7 +16,6 @@ endif
|
||||
lib = library(
|
||||
meson.project_name(),
|
||||
lib_sources,
|
||||
dependencies: [fmt],
|
||||
include_directories: inc,
|
||||
install: true,
|
||||
cpp_args: lib_cpp_args
|
||||
|
||||
@@ -35,6 +35,6 @@ inline Int
|
||||
bit_range(Int num, size_t start, size_t end) {
|
||||
// NOTE: we do not require -1 if it is a signed integral
|
||||
Int left =
|
||||
std::numeric_limits<Int>::digits - (std::is_unsigned<Int>::value) - end;
|
||||
std::numeric_limits<Int>::digits - (!std::is_signed<Int>::value) - end;
|
||||
return static_cast<Int>(num << left) >> (left + start);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
#include <array>
|
||||
#include <bit>
|
||||
#include <fmt/core.h>
|
||||
#include <format>
|
||||
#include <string>
|
||||
|
||||
// Why I wrote this myself? I do not know
|
||||
@@ -110,7 +110,7 @@ sha256(std::array<uint8_t, N>& data) {
|
||||
|
||||
for (j = 0; j < 8; j++)
|
||||
for (i = 0; i < 4; i++)
|
||||
fmt::format_to(std::back_inserter(string),
|
||||
std::format_to(std::back_inserter(string),
|
||||
"{:02x}",
|
||||
((h[j] >> (24 - i * 8)) & 0xFF));
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "util/loglevel.hh"
|
||||
#include <fmt/ostream.h>
|
||||
#include <iostream>
|
||||
#include <print>
|
||||
|
||||
namespace logging {
|
||||
namespace ansi {
|
||||
@@ -14,25 +13,25 @@ static constexpr auto BOLD = "\033[1m";
|
||||
static constexpr auto RESET = "\033[0m";
|
||||
}
|
||||
|
||||
using fmt::print;
|
||||
using std::print;
|
||||
|
||||
class Logger {
|
||||
using LogLevel = matar::LogLevel;
|
||||
|
||||
public:
|
||||
Logger(LogLevel level = LogLevel::Debug, FILE* stream = stderr)
|
||||
Logger(LogLevel level = LogLevel::Debug, FILE* stream = stdout)
|
||||
: level(0)
|
||||
, stream(stream) {
|
||||
set_level(level);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void log(const fmt::format_string<Args...>& fmt, Args&&... args) {
|
||||
fmt::println(stream, fmt, std::forward<Args>(args)...);
|
||||
void log(const std::format_string<Args...>& fmt, Args&&... args) {
|
||||
std::println(stream, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void debug(const fmt::format_string<Args...>& fmt, Args&&... args) {
|
||||
void debug(const std::format_string<Args...>& fmt, Args&&... args) {
|
||||
if (level & static_cast<uint8_t>(LogLevel::Debug)) {
|
||||
print(stream, "{}{}[DEBUG] ", ansi::MAGENTA, ansi::BOLD);
|
||||
log(fmt, std::forward<Args>(args)...);
|
||||
@@ -41,7 +40,7 @@ class Logger {
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void info(const fmt::format_string<Args...>& fmt, Args&&... args) {
|
||||
void info(const std::format_string<Args...>& fmt, Args&&... args) {
|
||||
if (level & static_cast<uint8_t>(LogLevel::Info)) {
|
||||
print(stream, "{}[INFO] ", ansi::WHITE);
|
||||
log(fmt, std::forward<Args>(args)...);
|
||||
@@ -50,7 +49,16 @@ class Logger {
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void warn(const fmt::format_string<Args...>& fmt, Args&&... args) {
|
||||
void info_bold(const std::format_string<Args...>& fmt, Args&&... args) {
|
||||
if (level & static_cast<uint8_t>(LogLevel::Info)) {
|
||||
print(stream, "{}{}[INFO] ", ansi::WHITE, ansi::BOLD);
|
||||
log(fmt, std::forward<Args>(args)...);
|
||||
print(stream, ansi::RESET);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void warn(const std::format_string<Args...>& fmt, Args&&... args) {
|
||||
if (level & static_cast<uint8_t>(LogLevel::Warn)) {
|
||||
print(stream, "{}[WARN] ", ansi::YELLOW);
|
||||
log(fmt, std::forward<Args>(args)...);
|
||||
@@ -59,7 +67,7 @@ class Logger {
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void error(const fmt::format_string<Args...>& fmt, Args&&... args) {
|
||||
void error(const std::format_string<Args...>& fmt, Args&&... args) {
|
||||
if (level & static_cast<uint8_t>(LogLevel::Error)) {
|
||||
print(stream, "{}{}[ERROR] ", ansi::RED, ansi::BOLD);
|
||||
log(fmt, std::forward<Args>(args)...);
|
||||
|
||||
34
tests/bus.cc
34
tests/bus.cc
@@ -1,7 +1,7 @@
|
||||
#include "bus.hh"
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
static constexpr auto TAG = "[bus]";
|
||||
#define TAG "[bus]"
|
||||
|
||||
using namespace matar;
|
||||
|
||||
@@ -16,28 +16,30 @@ class BusFixture {
|
||||
};
|
||||
|
||||
TEST_CASE_METHOD(BusFixture, "Byte", TAG) {
|
||||
CHECK(bus.read_byte(3349) == 0);
|
||||
CHECK(bus.read_byte(0x30001A9) == 0);
|
||||
|
||||
bus.write_byte(3349, 0xEC);
|
||||
CHECK(bus.read_byte(3349) == 0xEC);
|
||||
CHECK(bus.read_word(3349) == 0xEC);
|
||||
CHECK(bus.read_halfword(3349) == 0xEC);
|
||||
bus.write_byte(0x30001A9, 0xEC);
|
||||
CHECK(bus.read_byte(0x30001A9) == 0xEC);
|
||||
CHECK(bus.read_word(0x30001A9) == 0xEC);
|
||||
CHECK(bus.read_halfword(0x30001A9) == 0xEC);
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(BusFixture, "Halfword", TAG) {
|
||||
CHECK(bus.read_halfword(33750745) == 0);
|
||||
CHECK(bus.read_halfword(0x202FED9) == 0);
|
||||
|
||||
bus.write_halfword(33750745, 0x1A4A);
|
||||
CHECK(bus.read_halfword(33750745) == 0x1A4A);
|
||||
CHECK(bus.read_word(33750745) == 0x1A4A);
|
||||
CHECK(bus.read_byte(33750745) == 0x4A);
|
||||
bus.write_halfword(0x202FED9, 0x1A4A);
|
||||
CHECK(bus.read_halfword(0x202FED9) == 0x1A4A);
|
||||
CHECK(bus.read_word(0x202FED9) == 0x1A4A);
|
||||
CHECK(bus.read_byte(0x202FED9) == 0x4A);
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(BusFixture, "Word", TAG) {
|
||||
CHECK(bus.read_word(100724276) == 0);
|
||||
CHECK(bus.read_word(0x600EE34) == 0);
|
||||
|
||||
bus.write_word(100724276, 0x3ACC491D);
|
||||
CHECK(bus.read_word(100724276) == 0x3ACC491D);
|
||||
CHECK(bus.read_halfword(100724276) == 0x491D);
|
||||
CHECK(bus.read_byte(100724276) == 0x1D);
|
||||
bus.write_word(0x600EE34, 0x3ACC491D);
|
||||
CHECK(bus.read_word(0x600EE34) == 0x3ACC491D);
|
||||
CHECK(bus.read_halfword(0x600EE34) == 0x491D);
|
||||
CHECK(bus.read_byte(0x600EE34) == 0x1D);
|
||||
}
|
||||
|
||||
#undef TAG
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,7 @@
|
||||
#include "cpu/arm/instruction.hh"
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
static constexpr auto TAG = "[arm][disassembly]";
|
||||
#define TAG "[arm][disassembly]"
|
||||
|
||||
using namespace matar;
|
||||
using namespace arm;
|
||||
@@ -31,15 +31,16 @@ TEST_CASE("Branch", TAG) {
|
||||
|
||||
// last 24 bits = 8748995
|
||||
// (8748995 << 8) >> 6 sign extended = 0xFE15FF0C
|
||||
// Also +8 since PC is two instructions ahead
|
||||
CHECK(b->offset == 0xFE15FF14);
|
||||
CHECK(b->offset == static_cast<int32_t>(0xfe15ff0c));
|
||||
CHECK(b->link == true);
|
||||
|
||||
#ifdef DISASSEMBLER
|
||||
CHECK(instruction.disassemble() == "BL 0xFE15FF14");
|
||||
// take prefetch into account
|
||||
// offset + 8 = 0xfe15ff0c + 8 = -0x1ea00e4 + 8
|
||||
CHECK(instruction.disassemble() == "BL -0x1ea00ec");
|
||||
|
||||
b->link = false;
|
||||
CHECK(instruction.disassemble() == "B 0xFE15FF14");
|
||||
CHECK(instruction.disassemble() == "B -0x1ea00ec");
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -505,3 +506,5 @@ TEST_CASE("Software Interrupt", TAG) {
|
||||
CHECK(instruction.disassemble() == "SWIEQ");
|
||||
#endif
|
||||
}
|
||||
|
||||
#undef TAG
|
||||
|
||||
96
tests/cpu/cpu-fixture.cc
Normal file
96
tests/cpu/cpu-fixture.cc
Normal file
@@ -0,0 +1,96 @@
|
||||
#include "cpu-fixture.hh"
|
||||
|
||||
Psr
|
||||
CpuFixture::psr(bool spsr) {
|
||||
Psr psr(0);
|
||||
Cpu tmp = cpu;
|
||||
arm::Instruction instruction(
|
||||
Condition::AL,
|
||||
arm::PsrTransfer{ .operand = 0,
|
||||
.spsr = spsr,
|
||||
.type = arm::PsrTransfer::Type::Mrs,
|
||||
.imm = false });
|
||||
|
||||
instruction.exec(tmp);
|
||||
|
||||
psr.set_all(getr_(0, tmp));
|
||||
return psr;
|
||||
}
|
||||
|
||||
void
|
||||
CpuFixture::set_psr(Psr psr, bool spsr) {
|
||||
// R0
|
||||
uint32_t old = getr(0);
|
||||
|
||||
setr(0, psr.raw());
|
||||
|
||||
arm::Instruction instruction(
|
||||
Condition::AL,
|
||||
arm::PsrTransfer{ .operand = 0,
|
||||
.spsr = spsr,
|
||||
.type = arm::PsrTransfer::Type::Msr,
|
||||
.imm = false });
|
||||
|
||||
instruction.exec(cpu);
|
||||
|
||||
setr(0, old);
|
||||
}
|
||||
|
||||
// We need these workarounds to just use the public API and not private
|
||||
// fields. Assuming that these work correctly is necessary. Besides, all that
|
||||
// matters is that the public API is correct.
|
||||
uint32_t
|
||||
CpuFixture::getr_(uint8_t r, Cpu& cpu) {
|
||||
uint32_t addr = 0x02000000;
|
||||
uint8_t offset = r == 15 ? 4 : 0;
|
||||
uint32_t word = bus.read_word(addr + offset);
|
||||
Cpu tmp = cpu;
|
||||
uint32_t ret = 0xFFFFFFFF;
|
||||
uint8_t base = r ? 0 : 1;
|
||||
|
||||
// set R0/R1 = addr
|
||||
arm::Instruction zero(
|
||||
Condition::AL,
|
||||
arm::DataProcessing{ .operand = addr,
|
||||
.rd = base,
|
||||
.rn = 0,
|
||||
.set = false,
|
||||
.opcode = arm::DataProcessing::OpCode::MOV });
|
||||
|
||||
// get register
|
||||
arm::Instruction get(
|
||||
Condition::AL,
|
||||
arm::SingleDataTransfer{ .offset = static_cast<uint16_t>(0),
|
||||
.rd = r,
|
||||
.rn = base,
|
||||
.load = false,
|
||||
.write = false,
|
||||
.byte = false,
|
||||
.up = true,
|
||||
.pre = true });
|
||||
|
||||
zero.exec(tmp);
|
||||
get.exec(tmp);
|
||||
|
||||
addr += offset;
|
||||
|
||||
ret = bus.read_word(addr);
|
||||
|
||||
bus.write_word(addr, word);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void
|
||||
CpuFixture::setr_(uint8_t r, uint32_t value, Cpu& cpu) {
|
||||
// set register
|
||||
arm::Instruction set(
|
||||
Condition::AL,
|
||||
arm::DataProcessing{ .operand = value,
|
||||
.rd = r,
|
||||
.rn = 0,
|
||||
.set = false,
|
||||
.opcode = arm::DataProcessing::OpCode::MOV });
|
||||
|
||||
set.exec(cpu);
|
||||
}
|
||||
42
tests/cpu/cpu-fixture.hh
Normal file
42
tests/cpu/cpu-fixture.hh
Normal file
@@ -0,0 +1,42 @@
|
||||
#include "cpu/cpu.hh"
|
||||
|
||||
using namespace matar;
|
||||
|
||||
class CpuFixture {
|
||||
public:
|
||||
CpuFixture()
|
||||
: bus(Memory(std::array<uint8_t, Memory::BIOS_SIZE>(),
|
||||
std::vector<uint8_t>(Header::HEADER_SIZE)))
|
||||
, cpu(bus) {}
|
||||
|
||||
protected:
|
||||
void exec(arm::InstructionData data, Condition condition = Condition::AL) {
|
||||
arm::Instruction instruction(condition, data);
|
||||
instruction.exec(cpu);
|
||||
}
|
||||
|
||||
void exec(thumb::InstructionData data) {
|
||||
thumb::Instruction instruction(data);
|
||||
instruction.exec(cpu);
|
||||
}
|
||||
|
||||
void reset(uint32_t value = 0) { setr(15, value + 8); }
|
||||
|
||||
uint32_t getr(uint8_t r) { return getr_(r, cpu); }
|
||||
|
||||
void setr(uint8_t r, uint32_t value) { setr_(r, value, cpu); }
|
||||
|
||||
Psr psr(bool spsr = false);
|
||||
|
||||
void set_psr(Psr psr, bool spsr = false);
|
||||
|
||||
Bus bus;
|
||||
Cpu cpu;
|
||||
|
||||
private:
|
||||
// hack to get a register
|
||||
uint32_t getr_(uint8_t r, Cpu& cpu);
|
||||
|
||||
// hack to set a register
|
||||
void setr_(uint8_t r, uint32_t value, Cpu& cpu);
|
||||
};
|
||||
@@ -1,2 +1,6 @@
|
||||
tests_sources += files(
|
||||
'cpu-fixture.cc'
|
||||
)
|
||||
|
||||
subdir('arm')
|
||||
subdir('thumb')
|
||||
995
tests/cpu/thumb/exec.cc
Normal file
995
tests/cpu/thumb/exec.cc
Normal file
@@ -0,0 +1,995 @@
|
||||
#include "cpu/cpu-fixture.hh"
|
||||
#include "cpu/thumb/instruction.hh"
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
using namespace matar;
|
||||
|
||||
#define TAG "[thumb][execution]"
|
||||
|
||||
using namespace thumb;
|
||||
|
||||
TEST_CASE_METHOD(CpuFixture, "Move Shifted Register", TAG) {
|
||||
InstructionData data = MoveShiftedRegister{
|
||||
.rd = 3, .rs = 5, .offset = 15, .opcode = ShiftType::LSL
|
||||
};
|
||||
MoveShiftedRegister* move = std::get_if<MoveShiftedRegister>(&data);
|
||||
|
||||
SECTION("LSL") {
|
||||
setr(3, 0);
|
||||
setr(5, 6687);
|
||||
// LSL
|
||||
exec(data);
|
||||
CHECK(getr(3) == 219119616);
|
||||
|
||||
setr(5, 0);
|
||||
// zero
|
||||
exec(data);
|
||||
CHECK(getr(3) == 0);
|
||||
CHECK(psr().z());
|
||||
}
|
||||
|
||||
SECTION("LSR") {
|
||||
move->opcode = ShiftType::LSR;
|
||||
setr(5, -1827489745);
|
||||
// LSR
|
||||
exec(data);
|
||||
CHECK(getr(3) == 75301);
|
||||
CHECK(!psr().n());
|
||||
|
||||
setr(5, 4444);
|
||||
// zero flag
|
||||
exec(data);
|
||||
CHECK(getr(3) == 0);
|
||||
CHECK(psr().z());
|
||||
}
|
||||
|
||||
SECTION("ASR") {
|
||||
setr(5, -1827489745);
|
||||
move->opcode = ShiftType::ASR;
|
||||
// ASR
|
||||
exec(data);
|
||||
CHECK(psr().n());
|
||||
CHECK(getr(3) == 4294911525);
|
||||
|
||||
setr(5, 500);
|
||||
// zero flag
|
||||
exec(data);
|
||||
CHECK(getr(3) == 0);
|
||||
CHECK(psr().z());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(CpuFixture, "Add/Subtract", TAG) {
|
||||
InstructionData data = AddSubtract{ .rd = 5,
|
||||
.rs = 2,
|
||||
.offset = 7,
|
||||
.opcode = AddSubtract::OpCode::ADD,
|
||||
.imm = false };
|
||||
AddSubtract* add = std::get_if<AddSubtract>(&data);
|
||||
setr(2, 378427891);
|
||||
setr(7, -666666);
|
||||
|
||||
SECTION("ADD") {
|
||||
// register
|
||||
exec(data);
|
||||
CHECK(getr(5) == 377761225);
|
||||
|
||||
add->imm = true;
|
||||
setr(2, (1u << 31) - 1);
|
||||
// immediate and overflow
|
||||
exec(data);
|
||||
CHECK(getr(5) == 2147483654);
|
||||
CHECK(psr().v());
|
||||
|
||||
setr(2, -7);
|
||||
// zero
|
||||
exec(data);
|
||||
CHECK(getr(5) == 0);
|
||||
CHECK(psr().z());
|
||||
}
|
||||
|
||||
add->imm = true;
|
||||
|
||||
SECTION("SUB") {
|
||||
add->opcode = AddSubtract::OpCode::SUB;
|
||||
setr(2, -((1u << 31) - 1));
|
||||
add->offset = 4;
|
||||
exec(data);
|
||||
CHECK(getr(5) == 2147483645);
|
||||
CHECK(psr().v());
|
||||
|
||||
setr(2, ~0u);
|
||||
add->offset = -4;
|
||||
// carry
|
||||
exec(data);
|
||||
CHECK(getr(5) == 3);
|
||||
CHECK(psr().c());
|
||||
|
||||
setr(2, 0);
|
||||
add->offset = 0;
|
||||
// zero
|
||||
exec(data);
|
||||
|
||||
CHECK(getr(5) == 0);
|
||||
CHECK(psr().z());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(CpuFixture, "Move/Compare/Add/Subtract Immediate", TAG) {
|
||||
InstructionData data = MovCmpAddSubImmediate{
|
||||
.offset = 251, .rd = 5, .opcode = MovCmpAddSubImmediate::OpCode::MOV
|
||||
};
|
||||
MovCmpAddSubImmediate* move = std::get_if<MovCmpAddSubImmediate>(&data);
|
||||
|
||||
SECTION("MOV") {
|
||||
exec(data);
|
||||
CHECK(getr(5) == 251);
|
||||
|
||||
move->offset = 0;
|
||||
// zero
|
||||
exec(data);
|
||||
CHECK(getr(5) == 0);
|
||||
CHECK(psr().z());
|
||||
}
|
||||
|
||||
SECTION("CMP") {
|
||||
setr(5, 251);
|
||||
move->opcode = MovCmpAddSubImmediate::OpCode::CMP;
|
||||
CHECK(!psr().z());
|
||||
exec(data);
|
||||
CHECK(getr(5) == 251);
|
||||
CHECK(psr().z());
|
||||
|
||||
// overflow
|
||||
setr(5, -((1u << 31) - 1));
|
||||
CHECK(!psr().v());
|
||||
exec(data);
|
||||
CHECK(getr(5) == 2147483649);
|
||||
CHECK(psr().v());
|
||||
}
|
||||
|
||||
SECTION("ADD") {
|
||||
move->opcode = MovCmpAddSubImmediate::OpCode::ADD;
|
||||
setr(5, (1u << 31) - 1);
|
||||
// immediate and overflow
|
||||
exec(data);
|
||||
CHECK(getr(5) == 2147483898);
|
||||
CHECK(psr().v());
|
||||
|
||||
setr(5, -251);
|
||||
// zero
|
||||
exec(data);
|
||||
CHECK(getr(5) == 0);
|
||||
CHECK(psr().z());
|
||||
}
|
||||
|
||||
SECTION("SUB") {
|
||||
// same as CMP but loaded
|
||||
setr(5, 251);
|
||||
move->opcode = MovCmpAddSubImmediate::OpCode::SUB;
|
||||
CHECK(!psr().z());
|
||||
exec(data);
|
||||
CHECK(getr(5) == 0);
|
||||
CHECK(psr().z());
|
||||
|
||||
// overflow
|
||||
setr(5, -((1u << 31) - 1));
|
||||
CHECK(!psr().v());
|
||||
exec(data);
|
||||
CHECK(getr(5) == 2147483398);
|
||||
CHECK(psr().v());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(CpuFixture, "ALU Operations", TAG) {
|
||||
InstructionData data =
|
||||
AluOperations{ .rd = 1, .rs = 3, .opcode = AluOperations::OpCode::AND };
|
||||
AluOperations* alu = std::get_if<AluOperations>(&data);
|
||||
|
||||
setr(1, 328940001);
|
||||
setr(3, -991);
|
||||
|
||||
SECTION("AND") {
|
||||
// 328940001 & -991
|
||||
exec(data);
|
||||
CHECK(getr(1) == 328939553);
|
||||
CHECK(!psr().n());
|
||||
|
||||
setr(3, 0);
|
||||
CHECK(!psr().z());
|
||||
// zero
|
||||
exec(data);
|
||||
CHECK(getr(1) == 0);
|
||||
CHECK(psr().z());
|
||||
}
|
||||
|
||||
SECTION("EOR") {
|
||||
alu->opcode = AluOperations::OpCode::EOR;
|
||||
// 328940001 ^ -991
|
||||
exec(data);
|
||||
CHECK(getr(1) == 3966027200);
|
||||
CHECK(psr().n());
|
||||
|
||||
setr(3, 3966027200);
|
||||
// zero
|
||||
exec(data);
|
||||
CHECK(getr(1) == 0);
|
||||
CHECK(psr().z());
|
||||
CHECK(!psr().n());
|
||||
}
|
||||
|
||||
SECTION("LSL") {
|
||||
setr(3, 3);
|
||||
alu->opcode = AluOperations::OpCode::LSL;
|
||||
// 328940001 << 3
|
||||
exec(data);
|
||||
CHECK(getr(1) == 2631520008);
|
||||
CHECK(psr().n());
|
||||
|
||||
setr(1, 0);
|
||||
// zero
|
||||
exec(data);
|
||||
CHECK(getr(1) == 0);
|
||||
CHECK(psr().z());
|
||||
}
|
||||
|
||||
SECTION("LSR") {
|
||||
alu->opcode = AluOperations::OpCode::LSR;
|
||||
setr(3, 991);
|
||||
// 328940001 >> 991
|
||||
exec(data);
|
||||
CHECK(getr(1) == 0);
|
||||
CHECK(psr().z());
|
||||
|
||||
setr(1, -83885328);
|
||||
setr(3, 5);
|
||||
// -83885328 >> 5
|
||||
exec(data);
|
||||
CHECK(getr(1) == 131596311);
|
||||
CHECK(!psr().z());
|
||||
CHECK(!psr().n());
|
||||
}
|
||||
|
||||
SECTION("ASR") {
|
||||
alu->opcode = AluOperations::OpCode::ASR;
|
||||
setr(3, 991);
|
||||
// 328940001 >> 991
|
||||
exec(data);
|
||||
CHECK(getr(1) == 0);
|
||||
CHECK(psr().z());
|
||||
|
||||
setr(1, -83885328);
|
||||
setr(3, 5);
|
||||
// -83885328 >> 5
|
||||
exec(data);
|
||||
CHECK(getr(1) == 4292345879);
|
||||
CHECK(!psr().z());
|
||||
CHECK(psr().n());
|
||||
}
|
||||
|
||||
SECTION("ADC") {
|
||||
alu->opcode = AluOperations::OpCode::ADC;
|
||||
setr(3, (1u << 31) - 1);
|
||||
Psr cpsr = psr();
|
||||
cpsr.set_c(true);
|
||||
set_psr(cpsr);
|
||||
// 2147483647 + 328940001 + 1
|
||||
exec(data);
|
||||
CHECK(getr(1) == 2476423649);
|
||||
CHECK(psr().v());
|
||||
CHECK(psr().n());
|
||||
CHECK(!psr().c());
|
||||
|
||||
setr(3, -328940001);
|
||||
setr(1, 328940001);
|
||||
// zero
|
||||
exec(data);
|
||||
CHECK(getr(1) == 0);
|
||||
CHECK(psr().z());
|
||||
}
|
||||
|
||||
SECTION("SBC") {
|
||||
alu->opcode = AluOperations::OpCode::SBC;
|
||||
setr(3, -((1u << 31) - 1));
|
||||
|
||||
Psr cpsr = psr();
|
||||
cpsr.set_c(false);
|
||||
set_psr(cpsr);
|
||||
|
||||
// 328940001 - -2147483647 - 1
|
||||
exec(data);
|
||||
CHECK(getr(1) == 2476423647);
|
||||
CHECK(psr().v());
|
||||
CHECK(psr().n());
|
||||
CHECK(!psr().c());
|
||||
|
||||
setr(1, -34892);
|
||||
setr(3, -34893);
|
||||
// zero
|
||||
exec(data);
|
||||
CHECK(getr(1) == 0);
|
||||
CHECK(psr().z());
|
||||
}
|
||||
|
||||
SECTION("ROR") {
|
||||
setr(3, 993);
|
||||
alu->opcode = AluOperations::OpCode::ROR;
|
||||
// 328940001 ROR 993
|
||||
exec(data);
|
||||
CHECK(getr(1) == 2311953648);
|
||||
CHECK(psr().n());
|
||||
CHECK(psr().c());
|
||||
|
||||
setr(1, 0);
|
||||
// zero
|
||||
exec(data);
|
||||
CHECK(getr(1) == 0);
|
||||
CHECK(psr().z());
|
||||
}
|
||||
|
||||
SECTION("TST") {
|
||||
alu->opcode = AluOperations::OpCode::TST;
|
||||
// 328940001 & -991
|
||||
exec(data);
|
||||
// no change
|
||||
CHECK(getr(1) == 328940001);
|
||||
|
||||
setr(3, 0);
|
||||
CHECK(!psr().z());
|
||||
// zero
|
||||
exec(data);
|
||||
CHECK(getr(1) == 328940001);
|
||||
CHECK(psr().z());
|
||||
}
|
||||
|
||||
SECTION("NEG") {
|
||||
alu->opcode = AluOperations::OpCode::NEG;
|
||||
// -(-991)
|
||||
exec(data);
|
||||
CHECK(getr(1) == 991);
|
||||
|
||||
setr(3, 0);
|
||||
// zero
|
||||
exec(data);
|
||||
CHECK(getr(1) == 0);
|
||||
CHECK(psr().z());
|
||||
}
|
||||
|
||||
SECTION("CMP") {
|
||||
alu->opcode = AluOperations::OpCode::CMP;
|
||||
setr(3, -((1u << 31) - 1));
|
||||
// 328940001 - -2147483647
|
||||
exec(data);
|
||||
// no change
|
||||
CHECK(getr(1) == 328940001);
|
||||
CHECK(psr().v());
|
||||
CHECK(psr().n());
|
||||
CHECK(!psr().c());
|
||||
|
||||
setr(1, -34892);
|
||||
setr(3, -34892);
|
||||
// zero
|
||||
exec(data);
|
||||
// no change (-34892)
|
||||
CHECK(getr(1) == 4294932404);
|
||||
CHECK(psr().z());
|
||||
}
|
||||
|
||||
SECTION("CMN") {
|
||||
alu->opcode = AluOperations::OpCode::CMN;
|
||||
setr(3, (1u << 31) - 1);
|
||||
// 2147483647 + 328940001
|
||||
exec(data);
|
||||
CHECK(getr(1) == 328940001);
|
||||
CHECK(psr().v());
|
||||
CHECK(psr().n());
|
||||
CHECK(!psr().c());
|
||||
|
||||
setr(3, -328940001);
|
||||
setr(1, 328940001);
|
||||
// zero
|
||||
exec(data);
|
||||
CHECK(getr(1) == 328940001);
|
||||
CHECK(psr().z());
|
||||
}
|
||||
|
||||
SECTION("ORR") {
|
||||
alu->opcode = AluOperations::OpCode::ORR;
|
||||
// 328940001 | -991
|
||||
exec(data);
|
||||
CHECK(getr(1) == 4294966753);
|
||||
CHECK(psr().n());
|
||||
|
||||
setr(1, 0);
|
||||
setr(3, 0);
|
||||
// zero
|
||||
exec(data);
|
||||
CHECK(getr(1) == 0);
|
||||
CHECK(psr().z());
|
||||
}
|
||||
|
||||
SECTION("MUL") {
|
||||
alu->opcode = AluOperations::OpCode::MUL;
|
||||
// 328940001 * -991 (lower 32 bits) (-325979540991 & 0xFFFFFFFF)
|
||||
exec(data);
|
||||
CHECK(getr(1) == 437973505);
|
||||
|
||||
setr(3, 0);
|
||||
// zero
|
||||
exec(data);
|
||||
CHECK(getr(1) == 0);
|
||||
CHECK(psr().z());
|
||||
}
|
||||
|
||||
SECTION("BIC") {
|
||||
alu->opcode = AluOperations::OpCode::BIC;
|
||||
// 328940001 & ~ -991
|
||||
exec(data);
|
||||
CHECK(getr(1) == 448);
|
||||
CHECK(!psr().n());
|
||||
|
||||
setr(3, ~0u);
|
||||
// zero
|
||||
exec(data);
|
||||
CHECK(getr(1) == 0);
|
||||
CHECK(psr().z());
|
||||
}
|
||||
|
||||
SECTION("MVN") {
|
||||
alu->opcode = AluOperations::OpCode::MVN;
|
||||
//~ -991
|
||||
exec(data);
|
||||
CHECK(getr(1) == 990);
|
||||
CHECK(!psr().n());
|
||||
|
||||
setr(3, 24358);
|
||||
// negative
|
||||
exec(data);
|
||||
CHECK(getr(1) == 4294942937);
|
||||
CHECK(psr().n());
|
||||
|
||||
setr(3, ~0u);
|
||||
// zero
|
||||
exec(data);
|
||||
CHECK(getr(1) == 0);
|
||||
CHECK(psr().z());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(CpuFixture, "Hi Register Operations/Branch Exchange", TAG) {
|
||||
InstructionData data = HiRegisterOperations{
|
||||
.rd = 5, .rs = 15, .opcode = HiRegisterOperations::OpCode::ADD
|
||||
};
|
||||
HiRegisterOperations* hi = std::get_if<HiRegisterOperations>(&data);
|
||||
|
||||
setr(15, 3452948950);
|
||||
setr(5, 958656720);
|
||||
|
||||
SECTION("ADD") {
|
||||
exec(data);
|
||||
CHECK(getr(5) == 116638374);
|
||||
|
||||
// hi + hi
|
||||
hi->rd = 14;
|
||||
hi->rs = 15;
|
||||
setr(14, 42589);
|
||||
exec(data);
|
||||
CHECK(getr(14) == 3452991539);
|
||||
}
|
||||
|
||||
SECTION("CMP") {
|
||||
hi->opcode = HiRegisterOperations::OpCode::CMP;
|
||||
exec(data);
|
||||
|
||||
// no change
|
||||
CHECK(getr(5) == 958656720);
|
||||
CHECK(!psr().n());
|
||||
CHECK(!psr().c());
|
||||
CHECK(!psr().v());
|
||||
CHECK(!psr().z());
|
||||
|
||||
setr(15, 958656720);
|
||||
// zero
|
||||
exec(data);
|
||||
// no change
|
||||
CHECK(getr(5) == 958656720);
|
||||
CHECK(psr().z());
|
||||
}
|
||||
|
||||
SECTION("MOV") {
|
||||
hi->opcode = HiRegisterOperations::OpCode::MOV;
|
||||
exec(data);
|
||||
|
||||
CHECK(getr(5) == 3452948950);
|
||||
}
|
||||
|
||||
SECTION("BX") {
|
||||
hi->opcode = HiRegisterOperations::OpCode::BX;
|
||||
hi->rs = 10;
|
||||
|
||||
SECTION("Arm") {
|
||||
setr(10, 2189988);
|
||||
exec(data);
|
||||
CHECK(getr(15) == 2189988);
|
||||
// switched to arm
|
||||
CHECK(psr().state() == State::Arm);
|
||||
}
|
||||
|
||||
SECTION("Thumb") {
|
||||
setr(10, 2189989);
|
||||
exec(data);
|
||||
CHECK(getr(15) == 2189988);
|
||||
|
||||
// switched to thumb
|
||||
CHECK(psr().state() == State::Thumb);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(CpuFixture, "PC Relative Load", TAG) {
|
||||
InstructionData data = PcRelativeLoad{ .word = 0x578, .rd = 0 };
|
||||
|
||||
setr(15, 0x3003FD5);
|
||||
// resetting bit 0 for 0x3003FD5, we get 0x3003FD4
|
||||
// 0x3003FD4 + 0x578
|
||||
bus.write_word(0x300454C, 489753492);
|
||||
|
||||
CHECK(getr(0) == 0);
|
||||
exec(data);
|
||||
CHECK(getr(0) == 489753492);
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(CpuFixture, "Load/Store with Register Offset", TAG) {
|
||||
InstructionData data = LoadStoreRegisterOffset{
|
||||
.rd = 3, .rb = 0, .ro = 7, .byte = false, .load = false
|
||||
};
|
||||
LoadStoreRegisterOffset* load = std::get_if<LoadStoreRegisterOffset>(&data);
|
||||
|
||||
setr(7, 0x3003000);
|
||||
setr(0, 0x332);
|
||||
setr(3, 389524259);
|
||||
|
||||
SECTION("store") {
|
||||
// 0x3003000 + 0x332
|
||||
CHECK(bus.read_word(0x3003332) == 0);
|
||||
exec(data);
|
||||
CHECK(bus.read_word(0x3003332) == 389524259);
|
||||
|
||||
// byte
|
||||
load->byte = true;
|
||||
bus.write_word(0x3003332, 0);
|
||||
exec(data);
|
||||
CHECK(bus.read_word(0x3003332) == 35);
|
||||
}
|
||||
|
||||
SECTION("load") {
|
||||
load->load = true;
|
||||
bus.write_word(0x3003332, 11123489);
|
||||
exec(data);
|
||||
CHECK(getr(3) == 11123489);
|
||||
|
||||
// byte
|
||||
load->byte = true;
|
||||
exec(data);
|
||||
CHECK(getr(3) == 33);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(CpuFixture, "Load/Store Sign Extended Byte/Halfword", TAG) {
|
||||
InstructionData data = LoadStoreSignExtendedHalfword{
|
||||
.rd = 3, .rb = 0, .ro = 7, .s = false, .h = false
|
||||
};
|
||||
LoadStoreSignExtendedHalfword* load =
|
||||
std::get_if<LoadStoreSignExtendedHalfword>(&data);
|
||||
|
||||
setr(7, 0x3003000);
|
||||
setr(0, 0x332);
|
||||
setr(3, 389524259);
|
||||
|
||||
SECTION("SH = 00") {
|
||||
// 0x3003000 + 0x332
|
||||
CHECK(bus.read_word(0x3003332) == 0);
|
||||
exec(data);
|
||||
CHECK(bus.read_word(0x3003332) == 43811);
|
||||
}
|
||||
|
||||
SECTION("SH = 01") {
|
||||
load->h = true;
|
||||
bus.write_word(0x3003332, 11123489);
|
||||
exec(data);
|
||||
CHECK(getr(3) == 47905);
|
||||
}
|
||||
|
||||
SECTION("SH = 10") {
|
||||
load->s = true;
|
||||
bus.write_word(0x3003332, 34521594);
|
||||
exec(data);
|
||||
// sign extended 250 byte (0xFA)
|
||||
CHECK(getr(3) == 4294967290);
|
||||
}
|
||||
|
||||
SECTION("SH = 11") {
|
||||
load->s = true;
|
||||
load->h = true;
|
||||
bus.write_word(0x3003332, 11123489);
|
||||
// sign extended 47905 halfword (0xBB21)
|
||||
exec(data);
|
||||
CHECK(getr(3) == 4294949665);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(CpuFixture, "Load/Store with Immediate Offset", TAG) {
|
||||
InstructionData data = LoadStoreImmediateOffset{
|
||||
.rd = 3, .rb = 0, .offset = 0x6E, .load = false, .byte = false
|
||||
};
|
||||
LoadStoreImmediateOffset* load =
|
||||
std::get_if<LoadStoreImmediateOffset>(&data);
|
||||
|
||||
setr(0, 0x300666A);
|
||||
setr(3, 389524259);
|
||||
|
||||
SECTION("store") {
|
||||
// 0x30066A + 0x6E
|
||||
CHECK(bus.read_word(0x30066D8) == 0);
|
||||
exec(data);
|
||||
CHECK(bus.read_word(0x30066D8) == 389524259);
|
||||
|
||||
// byte
|
||||
load->byte = true;
|
||||
bus.write_word(0x30066D8, 0);
|
||||
exec(data);
|
||||
CHECK(bus.read_word(0x30066D8) == 35);
|
||||
}
|
||||
|
||||
SECTION("load") {
|
||||
load->load = true;
|
||||
bus.write_word(0x30066D8, 11123489);
|
||||
exec(data);
|
||||
CHECK(getr(3) == 11123489);
|
||||
|
||||
// byte
|
||||
load->byte = true;
|
||||
exec(data);
|
||||
CHECK(getr(3) == 33);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(CpuFixture, "Load/Store Halfword", TAG) {
|
||||
InstructionData data =
|
||||
LoadStoreHalfword{ .rd = 3, .rb = 0, .offset = 0x6E, .load = false };
|
||||
LoadStoreHalfword* load = std::get_if<LoadStoreHalfword>(&data);
|
||||
|
||||
setr(0, 0x300666A);
|
||||
setr(3, 389524259);
|
||||
|
||||
SECTION("store") {
|
||||
// 0x300666A + 0x6E
|
||||
CHECK(bus.read_word(0x30066D8) == 0);
|
||||
exec(data);
|
||||
CHECK(bus.read_word(0x30066D8) == 43811);
|
||||
}
|
||||
|
||||
SECTION("load") {
|
||||
load->load = true;
|
||||
bus.write_word(0x30066D8, 11123489);
|
||||
exec(data);
|
||||
CHECK(getr(3) == 47905);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(CpuFixture, "SP Relative Load", TAG) {
|
||||
InstructionData data =
|
||||
SpRelativeLoad{ .word = 0x328, .rd = 1, .load = false };
|
||||
SpRelativeLoad* load = std::get_if<SpRelativeLoad>(&data);
|
||||
|
||||
setr(1, 2349505744);
|
||||
// sp
|
||||
setr(13, 0x3004A8A);
|
||||
|
||||
SECTION("store") {
|
||||
// 0x3004A8A + 0x328
|
||||
CHECK(bus.read_word(0x3004DB2) == 0);
|
||||
exec(data);
|
||||
CHECK(bus.read_word(0x3004DB2) == 2349505744);
|
||||
}
|
||||
|
||||
SECTION("load") {
|
||||
load->load = true;
|
||||
bus.write_word(0x3004DB2, 11123489);
|
||||
exec(data);
|
||||
CHECK(getr(1) == 11123489);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(CpuFixture, "Load Address", TAG) {
|
||||
InstructionData data = LoadAddress{ .word = 808, .rd = 1, .sp = false };
|
||||
LoadAddress* load = std::get_if<LoadAddress>(&data);
|
||||
|
||||
// pc
|
||||
setr(15, 336485);
|
||||
// sp
|
||||
setr(13, 69879977);
|
||||
|
||||
SECTION("PC") {
|
||||
exec(data);
|
||||
CHECK(getr(1) == 337293);
|
||||
}
|
||||
|
||||
SECTION("SP") {
|
||||
load->sp = true;
|
||||
exec(data);
|
||||
CHECK(getr(1) == 69880785);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(CpuFixture, "Add Offset to Stack Pointer", TAG) {
|
||||
InstructionData data = AddOffsetStackPointer{ .word = 473 };
|
||||
AddOffsetStackPointer* add = std::get_if<AddOffsetStackPointer>(&data);
|
||||
|
||||
// sp
|
||||
setr(13, 69879977);
|
||||
|
||||
SECTION("positive") {
|
||||
exec(data);
|
||||
CHECK(getr(13) == 69880450);
|
||||
}
|
||||
|
||||
SECTION("negative") {
|
||||
add->word = -473;
|
||||
exec(data);
|
||||
CHECK(getr(13) == 69879504);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(CpuFixture, "Push/Pop Registers", TAG) {
|
||||
InstructionData data =
|
||||
PushPopRegister{ .regs = 0b11010011, .pclr = false, .load = false };
|
||||
PushPopRegister* push = std::get_if<PushPopRegister>(&data);
|
||||
|
||||
static constexpr uint8_t alignment = 4;
|
||||
static constexpr uint32_t address = 0x30015AC;
|
||||
|
||||
// registers = 0, 1, 4, 6, 7
|
||||
SECTION("push (store)") {
|
||||
|
||||
// populate registers
|
||||
setr(0, 237164);
|
||||
setr(1, 679785111);
|
||||
setr(4, 905895898);
|
||||
setr(6, 131313333);
|
||||
setr(7, 131);
|
||||
|
||||
auto checker = [this]() {
|
||||
// address
|
||||
CHECK(bus.read_word(address) == 237164);
|
||||
CHECK(bus.read_word(address + alignment) == 679785111);
|
||||
CHECK(bus.read_word(address + alignment * 2) == 905895898);
|
||||
CHECK(bus.read_word(address + alignment * 3) == 131313333);
|
||||
CHECK(bus.read_word(address + alignment * 4) == 131);
|
||||
};
|
||||
|
||||
// set stack pointer to top of stack
|
||||
setr(13, address + alignment * 5);
|
||||
|
||||
SECTION("without LR") {
|
||||
exec(data);
|
||||
checker();
|
||||
CHECK(getr(13) == address);
|
||||
}
|
||||
|
||||
SECTION("with LR") {
|
||||
push->pclr = true;
|
||||
// populate lr
|
||||
setr(14, 999304);
|
||||
// add another word on stack (top + 4)
|
||||
setr(13, address + alignment * 6);
|
||||
exec(data);
|
||||
|
||||
CHECK(bus.read_word(address + alignment * 5) == 999304);
|
||||
checker();
|
||||
CHECK(getr(13) == address);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("pop (load)") {
|
||||
push->load = true;
|
||||
|
||||
// populate memory
|
||||
bus.write_word(address, 237164);
|
||||
bus.write_word(address + alignment, 679785111);
|
||||
bus.write_word(address + alignment * 2, 905895898);
|
||||
bus.write_word(address + alignment * 3, 131313333);
|
||||
bus.write_word(address + alignment * 4, 131);
|
||||
|
||||
auto checker = [this]() {
|
||||
CHECK(getr(0) == 237164);
|
||||
CHECK(getr(1) == 679785111);
|
||||
CHECK(getr(2) == 0);
|
||||
CHECK(getr(3) == 0);
|
||||
CHECK(getr(4) == 905895898);
|
||||
CHECK(getr(5) == 0);
|
||||
CHECK(getr(6) == 131313333);
|
||||
CHECK(getr(7) == 131);
|
||||
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
setr(i, 0);
|
||||
}
|
||||
};
|
||||
|
||||
// set stack pointer to bottom of stack
|
||||
setr(13, address);
|
||||
|
||||
SECTION("without SP") {
|
||||
exec(data);
|
||||
checker();
|
||||
CHECK(getr(13) == address + alignment * 5);
|
||||
}
|
||||
|
||||
SECTION("with SP") {
|
||||
push->pclr = true;
|
||||
// populate next address
|
||||
bus.write_word(address + alignment * 5, 93333912);
|
||||
exec(data);
|
||||
|
||||
CHECK(getr(15) == 93333912);
|
||||
checker();
|
||||
CHECK(getr(13) == address + alignment * 6);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(CpuFixture, "Multiple Load/Store", TAG) {
|
||||
InstructionData data =
|
||||
MultipleLoad{ .regs = 0b11010101, .rb = 2, .load = false };
|
||||
MultipleLoad* push = std::get_if<MultipleLoad>(&data);
|
||||
// registers = 0, 1, 4, 6, 7
|
||||
|
||||
static constexpr uint8_t alignment = 4;
|
||||
static constexpr uint32_t address = 0x30015AC;
|
||||
|
||||
SECTION("store") {
|
||||
|
||||
// populate registers
|
||||
setr(0, 237164);
|
||||
setr(4, 905895898);
|
||||
setr(6, 131313333);
|
||||
setr(7, 131);
|
||||
|
||||
// set R2 (base) to top of stack
|
||||
setr(2, address + alignment * 5);
|
||||
|
||||
exec(data);
|
||||
|
||||
CHECK(bus.read_word(address) == 237164);
|
||||
CHECK(bus.read_word(address + alignment) == address + alignment * 5);
|
||||
CHECK(bus.read_word(address + alignment * 2) == 905895898);
|
||||
CHECK(bus.read_word(address + alignment * 3) == 131313333);
|
||||
CHECK(bus.read_word(address + alignment * 4) == 131);
|
||||
// write back
|
||||
CHECK(getr(2) == address);
|
||||
}
|
||||
|
||||
SECTION("load") {
|
||||
push->load = true;
|
||||
|
||||
// populate memory
|
||||
bus.write_word(address, 237164);
|
||||
bus.write_word(address + alignment, 679785111);
|
||||
bus.write_word(address + alignment * 2, 905895898);
|
||||
bus.write_word(address + alignment * 3, 131313333);
|
||||
bus.write_word(address + alignment * 4, 131);
|
||||
|
||||
// base
|
||||
setr(2, address);
|
||||
|
||||
exec(data);
|
||||
CHECK(getr(0) == 237164);
|
||||
CHECK(getr(1) == 0);
|
||||
CHECK(getr(2) == address + alignment * 5); // write back
|
||||
CHECK(getr(3) == 0);
|
||||
CHECK(getr(4) == 905895898);
|
||||
CHECK(getr(5) == 0);
|
||||
CHECK(getr(6) == 131313333);
|
||||
CHECK(getr(7) == 131);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(CpuFixture, "Conditional Branch", TAG) {
|
||||
InstructionData data =
|
||||
ConditionalBranch{ .offset = -192, .condition = Condition::EQ };
|
||||
ConditionalBranch* branch = std::get_if<ConditionalBranch>(&data);
|
||||
|
||||
setr(15, 4589344);
|
||||
|
||||
SECTION("z") {
|
||||
Psr cpsr = psr();
|
||||
// condition is false
|
||||
exec(data);
|
||||
CHECK(getr(15) == 4589344);
|
||||
|
||||
cpsr.set_z(true);
|
||||
set_psr(cpsr);
|
||||
// condition is true
|
||||
exec(data);
|
||||
CHECK(getr(15) == 4589152);
|
||||
}
|
||||
|
||||
SECTION("c") {
|
||||
branch->condition = Condition::CS;
|
||||
Psr cpsr = psr();
|
||||
// condition is false
|
||||
exec(data);
|
||||
CHECK(getr(15) == 4589344);
|
||||
|
||||
cpsr.set_c(true);
|
||||
set_psr(cpsr);
|
||||
// condition is true
|
||||
exec(data);
|
||||
CHECK(getr(15) == 4589152);
|
||||
}
|
||||
|
||||
SECTION("n") {
|
||||
branch->condition = Condition::MI;
|
||||
Psr cpsr = psr();
|
||||
// condition is false
|
||||
exec(data);
|
||||
CHECK(getr(15) == 4589344);
|
||||
|
||||
cpsr.set_n(true);
|
||||
set_psr(cpsr);
|
||||
// condition is true
|
||||
exec(data);
|
||||
CHECK(getr(15) == 4589152);
|
||||
}
|
||||
|
||||
SECTION("v") {
|
||||
branch->condition = Condition::VS;
|
||||
Psr cpsr = psr();
|
||||
// condition is false
|
||||
exec(data);
|
||||
CHECK(getr(15) == 4589344);
|
||||
|
||||
cpsr.set_v(true);
|
||||
set_psr(cpsr);
|
||||
// condition is true
|
||||
exec(data);
|
||||
CHECK(getr(15) == 4589152);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(CpuFixture, "Software Interrupt", TAG) {
|
||||
InstructionData data = SoftwareInterrupt{ .vector = 33 };
|
||||
|
||||
setr(15, 4492);
|
||||
exec(data);
|
||||
CHECK(psr().raw() == psr(true).raw());
|
||||
CHECK(getr(14) == 4490);
|
||||
CHECK(getr(15) == 33);
|
||||
CHECK(psr().state() == State::Arm);
|
||||
CHECK(psr().mode() == Mode::Supervisor);
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(CpuFixture, "Unconditional Branch", TAG) {
|
||||
InstructionData data = UnconditionalBranch{ .offset = -920 };
|
||||
|
||||
setr(15, 4589344);
|
||||
exec(data);
|
||||
CHECK(getr(15) == 4588424);
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(CpuFixture, "Long Branch With Link", TAG) {
|
||||
InstructionData data = LongBranchWithLink{ .offset = 3262, .high = false };
|
||||
LongBranchWithLink* branch = std::get_if<LongBranchWithLink>(&data);
|
||||
|
||||
// high
|
||||
setr(15, 4589344);
|
||||
|
||||
exec(data);
|
||||
CHECK(getr(14) == 2881312);
|
||||
|
||||
// low
|
||||
branch->high = true;
|
||||
exec(data);
|
||||
CHECK(getr(14) == 4589343);
|
||||
CHECK(getr(15) == 2884574);
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "cpu/thumb/instruction.hh"
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
static constexpr auto TAG = "[thumb][disassembly]";
|
||||
#define TAG "[thumb][disassembly]"
|
||||
|
||||
using namespace matar;
|
||||
using namespace thumb;
|
||||
@@ -178,11 +178,12 @@ TEST_CASE("PC Relative Load", TAG) {
|
||||
PcRelativeLoad* ldr = nullptr;
|
||||
|
||||
REQUIRE((ldr = std::get_if<PcRelativeLoad>(&instruction.data)));
|
||||
CHECK(ldr->word == 230);
|
||||
// 230 << 2
|
||||
CHECK(ldr->word == 920);
|
||||
CHECK(ldr->rd == 2);
|
||||
|
||||
#ifdef DISASSEMBLER
|
||||
CHECK(instruction.disassemble() == "LDR R2,[PC,#230]");
|
||||
CHECK(instruction.disassemble() == "LDR R2,[PC,#920]");
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -247,21 +248,32 @@ TEST_CASE("Load/Store with Immediate Offset", TAG) {
|
||||
REQUIRE((ldr = std::get_if<LoadStoreImmediateOffset>(&instruction.data)));
|
||||
CHECK(ldr->rd == 5);
|
||||
CHECK(ldr->rb == 3);
|
||||
CHECK(ldr->offset == 22);
|
||||
// 22 << 4 when byte == false
|
||||
CHECK(ldr->offset == 88);
|
||||
CHECK(ldr->byte == false);
|
||||
CHECK(ldr->load == false);
|
||||
|
||||
#ifdef DISASSEMBLER
|
||||
CHECK(instruction.disassemble() == "STR R5,[R3,#22]");
|
||||
CHECK(instruction.disassemble() == "STR R5,[R3,#88]");
|
||||
|
||||
ldr->byte = true;
|
||||
ldr->load = true;
|
||||
CHECK(instruction.disassemble() == "LDR R5,[R3,#88]");
|
||||
#endif
|
||||
|
||||
// byte
|
||||
raw = 0b0111010110011101;
|
||||
instruction = Instruction(raw);
|
||||
|
||||
INFO(instruction.data.index());
|
||||
REQUIRE((ldr = std::get_if<LoadStoreImmediateOffset>(&instruction.data)));
|
||||
CHECK(ldr->byte == true);
|
||||
CHECK(ldr->offset == 22);
|
||||
|
||||
#ifdef DISASSEMBLER
|
||||
CHECK(instruction.disassemble() == "STRB R5,[R3,#22]");
|
||||
|
||||
ldr->load = true;
|
||||
CHECK(instruction.disassemble() == "LDRB R5,[R3,#22]");
|
||||
|
||||
ldr->byte = false;
|
||||
CHECK(instruction.disassemble() == "LDR R5,[R3,#22]");
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -273,14 +285,15 @@ TEST_CASE("Load/Store Halfword", TAG) {
|
||||
REQUIRE((ldr = std::get_if<LoadStoreHalfword>(&instruction.data)));
|
||||
CHECK(ldr->rd == 5);
|
||||
CHECK(ldr->rb == 3);
|
||||
CHECK(ldr->offset == 26);
|
||||
// 26 << 1
|
||||
CHECK(ldr->offset == 52);
|
||||
CHECK(ldr->load == false);
|
||||
|
||||
#ifdef DISASSEMBLER
|
||||
CHECK(instruction.disassemble() == "STRH R5,[R3,#26]");
|
||||
CHECK(instruction.disassemble() == "STRH R5,[R3,#52]");
|
||||
|
||||
ldr->load = true;
|
||||
CHECK(instruction.disassemble() == "LDRH R5,[R3,#26]");
|
||||
CHECK(instruction.disassemble() == "LDRH R5,[R3,#52]");
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -291,14 +304,15 @@ TEST_CASE("SP-Relative Load/Store", TAG) {
|
||||
|
||||
REQUIRE((ldr = std::get_if<SpRelativeLoad>(&instruction.data)));
|
||||
CHECK(ldr->rd == 4);
|
||||
CHECK(ldr->word == 157);
|
||||
// 157 << 2
|
||||
CHECK(ldr->word == 628);
|
||||
CHECK(ldr->load == false);
|
||||
|
||||
#ifdef DISASSEMBLER
|
||||
CHECK(instruction.disassemble() == "STR R4,[SP,#157]");
|
||||
CHECK(instruction.disassemble() == "STR R4,[SP,#628]");
|
||||
|
||||
ldr->load = true;
|
||||
CHECK(instruction.disassemble() == "LDR R4,[SP,#157]");
|
||||
CHECK(instruction.disassemble() == "LDR R4,[SP,#628]");
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -308,15 +322,16 @@ TEST_CASE("Load Adress", TAG) {
|
||||
LoadAddress* add = nullptr;
|
||||
|
||||
REQUIRE((add = std::get_if<LoadAddress>(&instruction.data)));
|
||||
CHECK(add->word == 143);
|
||||
// 143 << 2
|
||||
CHECK(add->word == 572);
|
||||
CHECK(add->rd == 1);
|
||||
CHECK(add->sp == false);
|
||||
|
||||
#ifdef DISASSEMBLER
|
||||
CHECK(instruction.disassemble() == "ADD R1,PC,#143");
|
||||
CHECK(instruction.disassemble() == "ADD R1,PC,#572");
|
||||
|
||||
add->sp = true;
|
||||
CHECK(instruction.disassemble() == "ADD R1,SP,#143");
|
||||
CHECK(instruction.disassemble() == "ADD R1,SP,#572");
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -326,14 +341,21 @@ TEST_CASE("Add Offset to Stack Pointer", TAG) {
|
||||
AddOffsetStackPointer* add = nullptr;
|
||||
|
||||
REQUIRE((add = std::get_if<AddOffsetStackPointer>(&instruction.data)));
|
||||
CHECK(add->word == 37);
|
||||
CHECK(add->sign == false);
|
||||
// 37 << 2
|
||||
CHECK(add->word == 148);
|
||||
|
||||
#ifdef DISASSEMBLER
|
||||
CHECK(instruction.disassemble() == "ADD SP,#+37");
|
||||
CHECK(instruction.disassemble() == "ADD SP,#148");
|
||||
#endif
|
||||
|
||||
add->sign = true;
|
||||
CHECK(instruction.disassemble() == "ADD SP,#-37");
|
||||
raw = 0b1011000010100101;
|
||||
instruction = Instruction(raw);
|
||||
|
||||
REQUIRE((add = std::get_if<AddOffsetStackPointer>(&instruction.data)));
|
||||
CHECK(add->word == -148);
|
||||
|
||||
#ifdef DISASSEMBLER
|
||||
CHECK(instruction.disassemble() == "ADD SP,#-148");
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -380,17 +402,19 @@ TEST_CASE("Multiple Load/Store", TAG) {
|
||||
}
|
||||
|
||||
TEST_CASE("Conditional Branch", TAG) {
|
||||
uint16_t raw = 0b1101100101110100;
|
||||
uint16_t raw = 0b1101100110110100;
|
||||
Instruction instruction(raw);
|
||||
ConditionalBranch* b = nullptr;
|
||||
|
||||
REQUIRE((b = std::get_if<ConditionalBranch>(&instruction.data)));
|
||||
// 116 << 2
|
||||
CHECK(b->offset == 232);
|
||||
// (-76 << 1)
|
||||
CHECK(b->offset == -152);
|
||||
CHECK(b->condition == Condition::LS);
|
||||
|
||||
#ifdef DISASSEMBLER
|
||||
CHECK(instruction.disassemble() == "BLS 232");
|
||||
// take prefetch into account
|
||||
// offset + 4 = -152 + 4
|
||||
CHECK(instruction.disassemble() == "BLS #-148");
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -402,7 +426,7 @@ TEST_CASE("SoftwareInterrupt") {
|
||||
REQUIRE((swi = std::get_if<SoftwareInterrupt>(&instruction.data)));
|
||||
|
||||
#ifdef DISASSEMBLER
|
||||
CHECK(instruction.disassemble() == "SWI");
|
||||
CHECK(instruction.disassemble() == "SWI 51");
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -412,11 +436,13 @@ TEST_CASE("Unconditional Branch") {
|
||||
UnconditionalBranch* b = nullptr;
|
||||
|
||||
REQUIRE((b = std::get_if<UnconditionalBranch>(&instruction.data)));
|
||||
// 1843 << 2
|
||||
REQUIRE(b->offset == 3686);
|
||||
// (2147483443 << 1)
|
||||
REQUIRE(b->offset == -410);
|
||||
|
||||
#ifdef DISASSEMBLER
|
||||
CHECK(instruction.disassemble() == "B 3686");
|
||||
// take prefetch into account
|
||||
// offset + 4 = -410 + 4
|
||||
CHECK(instruction.disassemble() == "B #-406");
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -431,9 +457,11 @@ TEST_CASE("Long Branch with link") {
|
||||
CHECK(bl->high == false);
|
||||
|
||||
#ifdef DISASSEMBLER
|
||||
CHECK(instruction.disassemble() == "BL 2520");
|
||||
CHECK(instruction.disassemble() == "BL #2520");
|
||||
|
||||
bl->high = true;
|
||||
CHECK(instruction.disassemble() == "BLH 2520");
|
||||
CHECK(instruction.disassemble() == "BLH #2520");
|
||||
#endif
|
||||
}
|
||||
|
||||
#undef TAG
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
tests_sources += files(
|
||||
'instruction.cc'
|
||||
'instruction.cc',
|
||||
'exec.cc'
|
||||
)
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "memory.hh"
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
static constexpr auto TAG = "[memory]";
|
||||
#define TAG "[memory]"
|
||||
|
||||
using namespace matar;
|
||||
|
||||
@@ -15,14 +15,18 @@ class MemFixture {
|
||||
Memory memory;
|
||||
};
|
||||
|
||||
TEST_CASE_METHOD(MemFixture, "bios", TAG) {
|
||||
memory.write(0, 0xAC);
|
||||
TEST_CASE("bios", TAG) {
|
||||
std::array<uint8_t, Memory::BIOS_SIZE> bios = { 0 };
|
||||
|
||||
// populate bios
|
||||
bios[0] = 0xAC;
|
||||
bios[0x3FFF] = 0x48;
|
||||
bios[0x2A56] = 0x10;
|
||||
|
||||
Memory memory(std::move(bios), std::vector<uint8_t>(Header::HEADER_SIZE));
|
||||
|
||||
CHECK(memory.read(0) == 0xAC);
|
||||
|
||||
memory.write(0x3FFF, 0x48);
|
||||
CHECK(memory.read(0x3FFF) == 0x48);
|
||||
|
||||
memory.write(0x2A56, 0x10);
|
||||
CHECK(memory.read(0x2A56) == 0x10);
|
||||
}
|
||||
|
||||
@@ -82,40 +86,33 @@ TEST_CASE_METHOD(MemFixture, "oam obj ram", TAG) {
|
||||
}
|
||||
|
||||
TEST_CASE("rom", TAG) {
|
||||
std::vector<uint8_t> rom(32 * 1024 * 1024, 0);
|
||||
|
||||
// populate rom
|
||||
rom[0] = 0xAC;
|
||||
rom[0x1FFFFFF] = 0x48;
|
||||
rom[0x0EF0256] = 0x10;
|
||||
|
||||
// 32 megabyte ROM
|
||||
Memory memory(std::array<uint8_t, Memory::BIOS_SIZE>(),
|
||||
std::vector<uint8_t>(32 * 1024 * 1024));
|
||||
Memory memory(std::array<uint8_t, Memory::BIOS_SIZE>(), std::move(rom));
|
||||
|
||||
SECTION("ROM1") {
|
||||
memory.write(0x8000000, 0xAC);
|
||||
CHECK(memory.read(0x8000000) == 0xAC);
|
||||
|
||||
memory.write(0x9FFFFFF, 0x48);
|
||||
CHECK(memory.read(0x9FFFFFF) == 0x48);
|
||||
|
||||
memory.write(0x8ef0256, 0x10);
|
||||
CHECK(memory.read(0x8ef0256) == 0x10);
|
||||
CHECK(memory.read(0x8EF0256) == 0x10);
|
||||
}
|
||||
|
||||
SECTION("ROM2") {
|
||||
memory.write(0xA000000, 0xAC);
|
||||
CHECK(memory.read(0xA000000) == 0xAC);
|
||||
|
||||
memory.write(0xBFFFFFF, 0x48);
|
||||
CHECK(memory.read(0xBFFFFFF) == 0x48);
|
||||
|
||||
memory.write(0xAEF0256, 0x10);
|
||||
CHECK(memory.read(0xAEF0256) == 0x10);
|
||||
}
|
||||
|
||||
SECTION("ROM3") {
|
||||
memory.write(0xC000000, 0xAC);
|
||||
CHECK(memory.read(0xC000000) == 0xAC);
|
||||
|
||||
memory.write(0xDFFFFFF, 0x48);
|
||||
CHECK(memory.read(0xDFFFFFF) == 0x48);
|
||||
|
||||
memory.write(0xCEF0256, 0x10);
|
||||
CHECK(memory.read(0xCEF0256) == 0x10);
|
||||
}
|
||||
}
|
||||
|
||||
#undef TAG
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "util/bits.hh"
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
static constexpr auto TAG = "[util][bits]";
|
||||
#define TAG "[util][bits]"
|
||||
|
||||
TEST_CASE("8 bits", TAG) {
|
||||
uint8_t num = 45;
|
||||
@@ -104,3 +104,5 @@ TEST_CASE("64 bits", TAG) {
|
||||
// 0b011010001
|
||||
CHECK(bit_range(num, 39, 47) == 209);
|
||||
}
|
||||
|
||||
#undef TAG
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "util/crypto.hh"
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
|
||||
static constexpr auto TAG = "[util][crypto]";
|
||||
#define TAG "[util][crypto]"
|
||||
|
||||
TEST_CASE("sha256 matar", TAG) {
|
||||
std::array<uint8_t, 5> data = { 'm', 'a', 't', 'a', 'r' };
|
||||
@@ -19,3 +19,5 @@ TEST_CASE("sha256 forgis", TAG) {
|
||||
CHECK(crypto::sha256(data) ==
|
||||
"cfddca2ce2673f355518cbe2df2a8522693c54723a469e8b36a4f68b90d2b759");
|
||||
}
|
||||
|
||||
#undef TAG
|
||||
|
||||
Reference in New Issue
Block a user