From 23d2c9c22ff4e6b3cfb4e0f6d02dc2ef41ad33ca Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 17 Jan 2023 14:47:40 +0800 Subject: [PATCH] feat: demo of RawCode checker --- src/main.cc | 4 +- src/raw_code/convert.cc | 9 +++-- src/raw_code/raw_code.cc | 84 ++++++++++++++++++++++++++++++++-------- src/raw_code/raw_code.h | 2 +- 4 files changed, 76 insertions(+), 23 deletions(-) diff --git a/src/main.cc b/src/main.cc index 3e1e48f..eb72a9d 100644 --- a/src/main.cc +++ b/src/main.cc @@ -378,7 +378,9 @@ int main() { // std::cout << CommonCode(RawCode(RawCode::extract(0x4FEA13400))) << std::endl; - std::cout << CommonCode(0x4FEA13400).to_raw_code().to_common_code() << std::endl; +// std::cout << CommonCode(0x4FEA13400).to_raw_code().to_common_code() << std::endl; + + std::cout << RawCode::check(CommonCode(0x4FEA13400).to_raw_code().unwrap()) << std::endl; // std::cerr << (clock() - start_time) * 1000 / CLOCKS_PER_SEC << "ms" << std::endl; std::cerr << (clock() - start_time) * 1000000 / CLOCKS_PER_SEC << "us" << std::endl; diff --git a/src/raw_code/convert.cc b/src/raw_code/convert.cc index f826361..eebb6eb 100644 --- a/src/raw_code/convert.cc +++ b/src/raw_code/convert.cc @@ -6,14 +6,15 @@ RawCode::RawCode(const CommonCode &common_code) { code = RawCode::extract(common_code.unwrap()); // load from common code } -CommonCode RawCode::to_common_code() const { - - // TODO: check before release common code - // TODO: raw code pass RawCode::check -> using CommonCode::unsafe_create directly +RawCode RawCode::from_common_code(const CommonCode &common_code) { + return RawCode(common_code); // load from common code +} +CommonCode RawCode::to_common_code() const { if (!RawCode::check(code)) { throw std::runtime_error("invalid raw code"); } + /// pass check -> common code must valid return CommonCode::unsafe_create(RawCode::compact(code)); // release common code } diff --git a/src/raw_code/raw_code.cc b/src/raw_code/raw_code.cc index 5c13d74..4a62031 100644 --- a/src/raw_code/raw_code.cc +++ b/src/raw_code/raw_code.cc @@ -6,6 +6,73 @@ uint64_t RawCode::unwrap() const { // get raw uint64_t code return code; } +#include + +/// we can check any raw_code in this function +bool RawCode::check(uint64_t raw_code) { + + if (raw_code >> 60) { + return false; // high 4-bits must be zero + } + + int head_num = 0; + int space_num = 0; + + for (int addr = 0; addr < 20; ++addr, raw_code >>= 3) { + +// std::cout << "addr = " << addr << " | block = " << (raw_code & 0b111) << std::endl; + + switch (raw_code & 0b111) { + case B_space: + ++space_num; + break; + case B_1x1: + /// do nothing + break; + case B_1x2: + + if ((addr & 0b11) == 0b11 || (raw_code & F_1x2) != C_1x2) { + return false; + } + + break; + case B_2x1: + + if (addr > 15 || (raw_code & F_2x1) != C_2x1) { + return false; + } + + break; + case B_2x2: + ++head_num; + + if (addr > 14 || (addr & 0b11) == 0b11 || (raw_code & F_2x2) != C_2x2) { + return false; + } + + break; + + case B_fill: + /// do nothing + break; + + default: + return false; // unknown flag + } + + } + + // TODO: one 2x2 / >=2 space + + // TODO: check head_num and space_num + + if (head_num != 1 || space_num < 2) { + return false; + } + + return true; +} + std::string RawCode::dump_case() const { std::string result; result.reserve(40); // 5 lines * ("x x x x\n") @@ -25,20 +92,3 @@ std::string RawCode::dump_case() const { } return result; } - -bool RawCode::check(uint64_t raw_code) { - - // TODO: check function for RawCode - - /// we can check any raw_code in this function - - // TODO: for check function -> one 2x2 / >=2 space - - // TODO: check the high 4-bits equal to zero - - return true; -} - - - - diff --git a/src/raw_code/raw_code.h b/src/raw_code/raw_code.h index 40ee27c..e7a2084 100644 --- a/src/raw_code/raw_code.h +++ b/src/raw_code/raw_code.h @@ -25,7 +25,7 @@ public: // static RawCode create(uint64_t raw_code); -// static RawCode from_common_code(const CommonCode &common_code); + static RawCode from_common_code(const CommonCode &common_code); // TODO: unsafe_create static function