diff --git a/asm/add.r8asm b/asm/add.r8asm new file mode 100644 index 0000000..26e06be --- /dev/null +++ b/asm/add.r8asm @@ -0,0 +1,137 @@ +%include "asm/lib.r8asm" + +; Macro bit_raw_add +; %1: CellBit A +; %2: CellBit B +; %3: CellBit Ci +; %4: CellBit S +; %5: CellBit Co +; %6: CellBit buf + +%macro bit_raw_add 6 +.anchor before_bit_raw_add +.tp %4 +biz + flb +rnz + +.tp %5 +biz + flb +rnz + +.tp %6 +biz + flb +rnz + +; sac=Ci: FLIP {A,B,buf} +.tp %3 +biz + .tp %1 + flb + .tp %2 + flb + .tp %6 + flb + .tp %3 + flb +rnz + +; sac=A: FLIP {Ci,S,buf} +.tp %1 +biz + .tp %3 + flb + .tp %4 + flb + .tp %6 + flb + .tp %1 + flb +rnz + +; sac=B: FLIP {A,Ci,Co,buf} +.tp %2 +biz + .tp %1 + flb + .tp %3 + flb + .tp %5 + flb + .tp %6 + flb + .tp %2 + flb +rnz + +; sac=S: SET1 {B,Co} +.tp %4 +biz + .tp %2 + set_1 + .tp %5 + set_1 + .tp %4 + flb +rnz + +; sac=buf: FLIP {A,B,Ci,S,Co} +.tp %6 +biz + .tp %1 + flb + .tp %2 + flb + .tp %3 + flb + .tp %4 + flb + .tp %5 + flb + .tp %6 + flb +rnz + +.tp before_bit_raw_add +%endmacro + + +; Macro raw_add +; %1: Cell A +; %2: Cell B +; %3: Cell Ci +; %4: Cell S +; %5: Cell Co +; %6: Cell tmp +%macro raw_add 6 +.anchor before_raw_add + +.tp %6 +erase +.tp %3 +bit_copy %6 %5 +.tp %6 +ror + +%rep _CELL_SIZE + bit_raw_add %1 %2 %3 %4 %5 %6 + .tp %5 + clb_bit_mov %3 + .tp %1 + ror + .tp %2 + ror + .tp %4 + ror +%endrep + +.tp %3 +clb_bit_mov %5 +.tp %6 +rol +clb_bit_mov %3 + +.tp before_add +%endmacro \ No newline at end of file diff --git a/asm/and.r8asm b/asm/and.r8asm new file mode 100644 index 0000000..c48c88d --- /dev/null +++ b/asm/and.r8asm @@ -0,0 +1,129 @@ +%include "asm/lib.r8asm" + +; Macro clb_bit_and +; %1: CellBit arg1 +; %2: CellBit arg2 +; %3: CellBit ans +%macro clb_bit_and 3 +.anchor before_clb_bit_and +.tp %3 +set_0 +.tp %1 +biz + .tp %2 + biz + .tp %3 + set_1 + .tp %2 + set_0 + rnz + .tp %1 + set_0 +rnz + +.tp before_clb_bit_and +%endmacro + + + +; Macro clb_and +; %1: Cell arg1 +; %2: Cell arg2 +; %3: Cell ans +%macro clb_and 3 +.anchor before_clb_and + +%rep _CELL_SIZE + clb_bit_and %1 %2 %3 + .tp %1 + ror + .tp %2 + ror + .tp %3 + ror +%endrep +.tp before_clb_and +%endmacro + + + +; Macro bit_and +; %1: UnitBit arg1 +; %2: UnitBit arg2 +; %3: CellBit ans +%macro bit_and 3 +.anchor before_and + +.tp %1 +stp +.anchor bit_and_buf_1 +.tp %2 +stp +.anchor bit_and_buf_2 + +.tp %3 +set_0 +.tp bit_and_buf_1 +set_0 +.tp bit_and_buf_2 +set_0 +.tp %1 +biz + .tp %3 + set_1 + .tp bit_and_buf_1 + set_1 + .tp %1 + flb +rnz +.tp %2 +biz + .tp %1 + set_1 + .tp %3 + set_1 + .tp %2 + flb +rnz +.tp %1 +biz + .tp %2 + flb + .tp %3 + flb + .tp %1 + flb +rnz +.tp bit_and_buf_1 +biz + .tp %1 + flb + .tp %3 + flb + .tp bit_and_buf_1 + flb +rnz + +.tp before_and +%endmacro + + + +; Macro and +; %1: Unit arg1 +; %2: Unit arg2 +; %3: Cell ans +%macro and 3 +.anchor before_and + +%rep _CELL_SIZE + bit_and %1 %2 %3 + .tp %1 + ror + .tp %2 + ror + .tp %3 + ror +%endrep +.tp before_and +%endmacro \ No newline at end of file diff --git a/asm/inc.r8asm b/asm/inc.r8asm new file mode 100644 index 0000000..3e2a619 --- /dev/null +++ b/asm/inc.r8asm @@ -0,0 +1,36 @@ +; TODO + + +%include asm/lib.r8asm +; Macro inc + +%macro inc 0 + +stp +flb +btp + +%rep _CELL_SIZE + + stp + + + biz + flb + btp + flb + stp + rol + flb + ror + rnz + + + out + ror + btp + out + ror +%endrep + +%endmacro \ No newline at end of file diff --git a/asm/lib.r8asm b/asm/lib.r8asm new file mode 100644 index 0000000..6de3803 --- /dev/null +++ b/asm/lib.r8asm @@ -0,0 +1,140 @@ +; Macro step +; %1: step_count +%macro step 1 +%rep %1 + stp + stp +%endrep +%endmacro + +; Macro back +; %1: back_count +%macro back 1 +%rep %1 + btp + btp +%endrep +%endmacro + +; Macro set_0 +; %this: CellBit operand +%macro set_0 0 +biz + flb +rnz +%endmacro + +; Macro set_1 +; %this: CellBit operand +%macro set_1 0 +set_0 +flb +%endmacro + +; Macro erase +; %this: Cell operand +%macro erase 0 +%rep _CELL_SIZE + set_0 + ror +%endrep +%endmacro + +; Macro rol +; %this: Cell operand +%macro rol 0 +%rep _ROL_STEP + ror +%endrep +%endmacro + + + +; Macro clb_bit_mov +; %this: CellBit data +; %1: CellBit destination + +%macro clb_bit_mov 1 +.anchor before_clb_bit_mov +.tp %1 +set_0 +.tp before_clb_bit_mov +biz + .tp %1 + flb + .tp before_clb_bit_mov + flb +rnz +.tp before_clb_bit_mov +%endmacro + + +; Macro bit_copy +; %this: CellBit data +; %1: CellBit destination +; %2: CellBit buffer + +%macro bit_copy 2 +.anchor before_bit_copy + +.tp %1 +set_0 +.tp %2 +set_0 +.tp before_bit_copy +biz + .tp %1 + flb + .tp %2 + flb + .tp before_bit_copy + flb +rnz +.tp %2 +clb_bit_mov before_bit_copy + +.tp before_bit_copy +%endmacro + + + +; Macro copy +; %this: CellBit data +; %1: CellBit destination +; %2: CellBit buffer + +%macro copy 2 +.anchor before_copy +%rep _CELL_SIZE + bit_copy %1 %2 + ror + .tp %1 + ror + .tp %2 + ror + .tp before_copy +%endrep +%endmacro + + +; Macro bit_copy_to_buffer +; %this: UnitBit operand +; %1: CellBit tmp +%macro bit_copy_to_buffer 1 +stp +.anchor bit_copy_to_buffer_buf_1 +btp +bit_copy bit_copy_to_buffer_buf_1 %1 +%endmacro + + +; Macro copy_to_buffer +; %this: Unit operand +; %1: Cell tmp + +%macro copy_to_buffer 0 +stp +.anchor copy_to_buffer_buf_1 +btp +copy copy_to_buffer_buf_1 %1 +%endmacro \ No newline at end of file diff --git a/asm/or.r8asm b/asm/or.r8asm new file mode 100644 index 0000000..740ebc1 --- /dev/null +++ b/asm/or.r8asm @@ -0,0 +1,131 @@ +%include "asm/lib.r8asm" +; Macro clb_bit_or +; %1: CellBit arg1 +; %2: CellBit arg2 +; %3: CellBit ans +%macro clb_bit_or 3 +.anchor before_clb_bit_or +.tp %3 +set_0 +.tp %1 +biz + .tp %3 + flb + .tp %1 + flb +rnz +.tp %2 +biz + .tp %3 + set_1 + .tp %2 + flb +rnz + +.tp before_clb_bit_or +%endmacro + + + +; Macro clb_or +; %1: Cell arg1 +; %2: Cell arg2 +; %3: Cell ans +%macro clb_or 3 +.anchor before_clb_or + +%rep _CELL_SIZE + clb_bit_or %1 %2 %3 + .tp %1 + ror + .tp %2 + ror + .tp %3 + ror +%endrep +.tp before_clb_or +%endmacro + + + + +; Macro bit_or +; %1: UnitBit arg1 +; %2: UnitBit arg2 +; %3: CellBit ans +%macro bit_or 3 +.anchor before_or + +.tp %1 +stp +.anchor bit_or_buf_1 +.tp %2 +stp +.anchor bit_or_buf_2 + +.tp %3 +set_0 +.tp bit_or_buf_1 +set_0 +.tp bit_or_buf_2 +set_0 +.tp %1 +biz + .tp %3 + set_1 + .tp %1 + flb +rnz +.tp %3 +biz + .tp %1 + flb + .tp %2 + flb + .tp bit_or_buf_1 + flb + .tp %3 + flb +rnz +.tp %2 +biz + .tp %3 + flb + .tp bit_or_buf_1 + flb + .tp %2 + flb +rnz +.tp bit_or_buf_1 +biz + .tp %2 + set_1 + .tp %3 + set_1 + .tp bit_or_buf_1 + flb +rnz + +.tp before_or +%endmacro + + + +; Macro or +; %1: Unit arg1 +; %2: Unit arg2 +; %3: Cell ans +%macro or 3 +.anchor before_or + +%rep _CELL_SIZE + bit_or %1 %2 %3 + .tp %1 + ror + .tp %2 + ror + .tp %3 + ror +%endrep +.tp before_or +%endmacro \ No newline at end of file diff --git a/asm/test.r8asm b/asm/test.r8asm new file mode 100644 index 0000000..110820d --- /dev/null +++ b/asm/test.r8asm @@ -0,0 +1,27 @@ +%include "asm/add.r8asm" + +.var A 0 +.var B 2 +.var Ci 4 +.var S 6 +.var Co 8 + +.tp A +.xor 127 +.tp B +.xor 11 +.tp Ci +.xor 1 + +raw_add A B Ci S Co 1 + +.tp A +out +.tp B +out +.tp Ci +out +.tp S +out +.tp Co +out \ No newline at end of file diff --git a/asm/xor.r8asm b/asm/xor.r8asm new file mode 100644 index 0000000..8412877 --- /dev/null +++ b/asm/xor.r8asm @@ -0,0 +1,124 @@ +%include "asm/lib.r8asm" + +; Macro clb_bit_xor +; %1: CellBit arg1 +; %2: CellBit arg2 +; %3: CellBit ans +%macro clb_bit_xor 3 +.anchor before_clb_bit_xor +.tp %3 +set_0 +.tp %1 +biz + .tp %3 + flb + .tp %1 + set_0 +rnz +.tp %2 +biz + .tp %3 + flb + .tp %2 + set_0 +rnz + +.tp before_clb_bit_xor +%endmacro + + + +; Macro clb_xor +; %1: Cell arg1 +; %2: Cell arg2 +; %3: Cell ans +%macro clb_xor 3 +.anchor before_clb_xor + +%rep _CELL_SIZE + clb_bit_xor %1 %2 %3 + .tp %1 + ror + .tp %2 + ror + .tp %3 + ror +%endrep +.tp before_clb_xor +%endmacro + + + +; Macro bit_xor +; %1: UnitBit arg1 +; %2: UnitBit arg2 +; %3: CellBit ans +%macro bit_xor 3 +.anchor before_xor + +.tp %1 +stp +.anchor bit_xor_buf_1 +.tp %2 +stp +.anchor bit_xor_buf_2 + +.tp %3 +set_0 +.tp bit_xor_buf_1 +set_0 +.tp bit_xor_buf_2 +set_0 +.tp %1 +biz + .tp %2 + flb + .tp bit_xor_buf_1 + flb + .tp %1 + flb +rnz +.tp %2 +biz + .tp %1 + flb + .tp %3 + flb + .tp bit_xor_buf_1 + flb + .tp %2 + flb +rnz +.tp bit_xor_buf_1 +biz + .tp %1 + flb + .tp %2 + flb + .tp bit_xor_buf_1 + flb +rnz + +.tp before_xor +%endmacro + + + +; Macro xor +; %1: Unit arg1 +; %2: Unit arg2 +; %3: Cell ans +%macro xor 3 +.anchor before_xor + +%rep _CELL_SIZE + bit_xor %1 %2 %3 + .tp %1 + ror + .tp %2 + ror + .tp %3 + ror +%endrep +.tp before_xor +%endmacro \ No newline at end of file diff --git a/include/r8asm/r8asm.hh b/include/r8asm/r8asm.hh index 85b5c01..4a36446 100644 --- a/include/r8asm/r8asm.hh +++ b/include/r8asm/r8asm.hh @@ -10,6 +10,8 @@ #include constexpr size_t CELL_SIZE = 8; +constexpr size_t CELL_MAX(((uint64_t)1 << CELL_SIZE) - 1); +constexpr size_t ROL_STEP(CELL_SIZE - 1); namespace r8asm_maps {} @@ -90,7 +92,10 @@ private: private: addr dataptr = 0; - std::vector> scopes = {{}}; + std::vector> scopes = { + {{"_CELL_SIZE", CELL_SIZE}, + {"_CELL_MAX", CELL_MAX}, + {"_ROL_STEP", ROL_STEP}}}; std::map macros; enum class builtin_t { diff --git a/tests/r8_test.cc b/tests/r8_test.cc index ff1253d..34d9612 100644 --- a/tests/r8_test.cc +++ b/tests/r8_test.cc @@ -1,2 +1,9 @@ -int main() { return (0); } \ No newline at end of file +#include "r8asm/r8asm.hh" +#include +int main() { + r8asm a; + a.import_file("asm/test.r8asm"); + std::cout << a.export_rot8(); + return (0); +} \ No newline at end of file