diff options
| author | kx <kx@radix-linux.su> | 2026-10-09 18:00:15 +0300 |
|---|---|---|
| committer | kx <kx@radix-linux.su> | 2026-10-09 18:00:15 +0300 |
| commit | 065da3509019b15dd0e678072e6be629e5330bef (patch) | |
| tree | 69c934c9e786ac048b4fbe8f4c4c5571661521f4 /tests/tst-bytecode.c | |
| parent | d130fe27fa753be02ed76fd9b277aa3c3d928eb9 (diff) | |
| download | libmcpu-0.4.0.tar.xz | |
Version 0.4.00.4.0
Diffstat (limited to 'tests/tst-bytecode.c')
| -rw-r--r-- | tests/tst-bytecode.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/tests/tst-bytecode.c b/tests/tst-bytecode.c new file mode 100644 index 0000000..c4f885e --- /dev/null +++ b/tests/tst-bytecode.c @@ -0,0 +1,56 @@ +#include <stdio.h> +#include <string.h> +#include <libmcpu.h> +#define CHECK(c,m) do{if(!(c)){fprintf(stderr,"FAIL: %s\n",m);return 1;}}while(0) +int +main( void ) +{ + cpu_register r = + { + 128, 0 + }; + __mpu_UNIT_t reg[3], code[18]; + mpu_int128_t exp; + cpu_instruction insn; + int rc; + CHECK( __cpu_encode_reg24( reg, &r ) == 0, "encode REG24" ); + code[0] = 0x01; + code[1] = 0x10; + memcpy( code + 2, reg, 3 ); + /* LDI */ + code[5] = 0xff; + code[6] = 0xff; + code[7] = 0xff; + code[8] = 0xff; + code[9] = 0xff; + code[10] = 0xff; + code[11] = 0xff; + code[12] = 0xef; + /* -17 */ + code[13] = 0x00; + code[14] = 0x01; + /* HALT */ + CHECK( __cpu_decode_at( code, 15, 0, &insn ) == CPU_EXEC_SUCCESS, + "decode-at LDI" ); + CHECK( insn.opcode == CPU_OP_LDI && insn.length == 13, + "decode-at LDI fields" ); + CHECK( __cpu_decode_at( code, 15, 13, &insn ) == CPU_EXEC_SUCCESS, + "decode-at HALT" ); + CHECK( insn.opcode == CPU_OP_HALT && insn.length == 2, + "decode-at HALT fields" ); + CHECK( __cpu_decode_at( code, 14, 13, &insn ) == CPU_EXEC_DECODE_FAULT, + "decode-at bounds" ); + __cpu_init(); + CHECK( __cpu_program_set( code, 15 ) == 0, "program mmap/load" ); + CHECK( __cpu_program_ptr() != 0, "program pointer" ); + CHECK( __cpu_program_size() == 15, "program size" ); + rc = __cpu_run(); + CHECK( rc == 0, "run" ); + CHECK( __cpu_pc == 15, "PC advanced" ); + CHECK( __cpu_gth() != 0, "HF set" ); + iatoi( ( mpu_int * ) exp, "-17", NB_I128 ); + CHECK( !memcmp( __cpu_reg_ptr( 128, 0 ), exp, NB_I128 ), "LDI int64 -> int128" ); + __cpu_free_context(); + puts( "libcpu bytecode/program-memory regression: PASS" ); + return( 0 ); +} |
