1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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, (__mpu_char8_t *)"-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 );
}
|