From 1a6346bc98936573ba1ee98aab7559180e174d52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Fri, 8 Sep 2023 12:42:22 +0200 Subject: [PATCH] Add adapter for gb (#20) --- README.md | 6 +++-- bap-frames | 2 +- rz-tracetest/adapter.cpp | 54 +++++++++++++++++++++++++++++++++++++--- 3 files changed, 56 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 493f853..3828425 100644 --- a/README.md +++ b/README.md @@ -16,10 +16,12 @@ Trace sources The following sources are currently known to produce meaningful results with rz-tracetest: -* [VICE](https://github.com/rizinorg/vice) Patched VICE emulator for testing - 6502. * [QEMU](https://github.com/BinaryAnalysisPlatform/qemu) Patched for the BAP project. Specifically useful for ARM and potentially later x86 too. +* [VICE](https://github.com/rizinorg/vice) Patched VICE emulator for testing + 6502. +* [SameBoy](https://github.com/rizinorg/SameBoy) Patched Game Boy emulator for + testing gb (sm83) Other sources which have not been tested with rz-tracetest specifically yet: diff --git a/bap-frames b/bap-frames index 70e4584..3d3e96d 160000 --- a/bap-frames +++ b/bap-frames @@ -1 +1 @@ -Subproject commit 70e4584402ee974489631575b62ca8ef5879b5a7 +Subproject commit 3d3e96dcada01dc70d464b9504fd42c9fbed5957 diff --git a/rz-tracetest/adapter.cpp b/rz-tracetest/adapter.cpp index 2a3b596..95c1b6f 100644 --- a/rz-tracetest/adapter.cpp +++ b/rz-tracetest/adapter.cpp @@ -193,7 +193,7 @@ class PPCTraceAdapter : public TraceAdapter { return false; } - bool IgnoreUnknownReg(const std::string &rz_reg_name) const { + bool IgnoreUnknownReg(const std::string &rz_reg_name) const override { return rz_reg_name == "ca32" || rz_reg_name == "ov32"; } @@ -261,7 +261,7 @@ class I8051TraceAdapter : public TraceAdapter { return false; } - bool IgnoreUnknownReg(const std::string &rz_reg_name) const { + bool IgnoreUnknownReg(const std::string &rz_reg_name) const override { return true; } }; @@ -280,7 +280,53 @@ class MipsTraceAdapter : public TraceAdapter { return false; } - bool IgnoreUnknownReg(const std::string &rz_reg_name) const { + bool IgnoreUnknownReg(const std::string &rz_reg_name) const override { + return true; + } +}; + +class GBTraceAdapter : public TraceAdapter { + public: + std::string RizinArch() const override { + return "gb"; + } + + std::string TraceRegToRizin(const std::string &tracereg) const override { + if (tracereg == "pc") { + // Rizin extends pc to 32bit mpc + return "mpc"; + } + return tracereg; + } + + void AdjustRegContentsFromTrace(const std::string &tracename, RzBitVector *trace_val, RzAnalysisOp *op) const override { + if (tracename == "pc") { + // Rizin extends pc to 32bit mpc + ut16 v = rz_bv_to_ut16(trace_val); + rz_bv_fini(trace_val); + rz_bv_init(trace_val, 32); + rz_bv_set_from_ut64(trace_val, v); + } + } + + void PrintRegisterDetails(const std::string &tracename, const std::string &data, size_t bits_size) const override { + if (tracename == "f") { + if (bits_size != 8) { + return; + } + ut8 f = data[0]; + printf(" 0 %#04x = %d\n", 1 << 0, (f & (1 << 0)) != 0); + printf(" 1 %#04x = %d\n", 1 << 1, (f & (1 << 1)) != 0); + printf(" 2 %#04x = %d\n", 1 << 2, (f & (1 << 2)) != 0); + printf(" 3 %#04x = %d\n", 1 << 3, (f & (1 << 3)) != 0); + printf(" 4 %#04x C = %d\n", 1 << 4, (f & (1 << 4)) != 0); + printf(" 5 %#04x H = %d\n", 1 << 5, (f & (1 << 5)) != 0); + printf(" 6 %#04x N = %d\n", 1 << 6, (f & (1 << 6)) != 0); + printf(" 7 %#04x Z = %d\n", 1 << 7, (f & (1 << 7)) != 0); + } + } + + bool AllowNoOperandSameValueAssignment() const override { return true; } }; @@ -299,6 +345,8 @@ std::unique_ptr SelectTraceAdapter(frame_architecture arch) { return std::unique_ptr(new I8051TraceAdapter()); case frame_arch_mips: return std::unique_ptr(new MipsTraceAdapter()); + case frame_arch_sm83: + return std::unique_ptr(new GBTraceAdapter()); default: return nullptr; }