summaryrefslogtreecommitdiff
path: root/tests/tst-instruction.c
blob: 052fb9c2d87e67fc53c34a68df4ee2402768244f (plain)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <string.h>

#include <libmcpu.h>


static int failures = 0;

#define CHECK(cond,msg) \
  do { \
    if( !(cond) ) \
    { \
      fprintf( stderr, "FAIL: %s\n", (msg) ); \
      ++failures; \
    } \
  } while( 0 )


static cpu_operand
reg_operand( unsigned int bits, __mpu_uint32_t address )
{
  cpu_operand operand;

  memset( &operand, 0, sizeof(operand) );
  operand.type = CPU_OPERAND_REGISTER;
  operand.value.reg.bits = bits;
  operand.value.reg.address = address;

  return( operand );
}


static void
clear_instruction( cpu_instruction *instruction, cpu_opcode opcode )
{
  memset( instruction, 0, sizeof(*instruction) );
  instruction->opcode = opcode;
}


int
main( void )
{
  cpu_instruction instruction;
  __mpu_uint32_t psw;

  __cpu_init();

  /* NOP must not change the logical PSW. */
  __cpu_set_psw( PSW_CF_MASK | PSW_INXF_MASK | PSW_TF_MASK | PSW_IF_MASK );
  psw = __cpu_get_psw();

  clear_instruction( &instruction, CPU_OP_NOP );
  CHECK( __cpu_exec( &instruction ) == CPU_EXEC_SUCCESS, "NOP return code" );
  CHECK( __cpu_get_psw() == psw, "NOP preserves PSW" );

  /* MOV is a raw physical-register copy and accepts overlap. */
  {
    __mpu_UNIT_t *src = __cpu_reg_ptr( 128, 0x00100 );
    __mpu_UNIT_t *dst = __cpu_reg_ptr( 128, 0x00110 );
    unsigned int i;

    CHECK( src != 0 && dst != 0, "MOV register pointers" );

    for( i = 0; i < 16; ++i ) src[i] = (__mpu_UNIT_t)( 0x20 + i );
    memset( dst, 0, 16 );

    clear_instruction( &instruction, CPU_OP_MOV );
    instruction.operand[0] = reg_operand( 128, 0x00110 );
    instruction.operand[1] = reg_operand( 128, 0x00100 );

    psw = __cpu_get_psw();
    CHECK( __cpu_exec( &instruction ) == CPU_EXEC_SUCCESS, "MOV return code" );
    CHECK( memcmp( dst, src, 16 ) == 0, "MOV register contents" );
    CHECK( __cpu_get_psw() == psw, "MOV preserves PSW" );

    /* Overlap must have memmove semantics. */
    for( i = 0; i < 32; ++i ) __cpu_registers[0x00200 + i] = (__mpu_UNIT_t)i;

    clear_instruction( &instruction, CPU_OP_MOV );
    instruction.operand[0] = reg_operand( 128, 0x00210 );
    instruction.operand[1] = reg_operand( 128, 0x00200 );

    CHECK( __cpu_exec( &instruction ) == CPU_EXEC_SUCCESS,
           "overlapping MOV return code" );
    for( i = 0; i < 16; ++i )
      CHECK( __cpu_registers[0x00210 + i] == (__mpu_UNIT_t)i,
             "overlapping MOV contents" );
  }

  /* CLR clears physical storage and leaves PSW unchanged. */
  {
    __mpu_UNIT_t *dst = __cpu_reg_ptr( 256, 0x00300 );
    unsigned int i;

    CHECK( dst != 0, "CLR register pointer" );
    memset( dst, 0xa5, 32 );

    clear_instruction( &instruction, CPU_OP_CLR );
    instruction.operand[0] = reg_operand( 256, 0x00300 );

    psw = __cpu_get_psw();
    CHECK( __cpu_exec( &instruction ) == CPU_EXEC_SUCCESS, "CLR return code" );
    for( i = 0; i < 32; ++i ) CHECK( dst[i] == 0, "CLR register contents" );
    CHECK( __cpu_get_psw() == psw, "CLR preserves PSW" );
  }

  /* IADD delegates arithmetic and integer FLAGS to LibMPU. */
  {
    __mpu_UNIT_t *dst = __cpu_reg_ptr( 8, 0x00400 );
    __mpu_UNIT_t *a   = __cpu_reg_ptr( 8, 0x00401 );
    __mpu_UNIT_t *b   = __cpu_reg_ptr( 8, 0x00402 );

    *a = 0xff;
    *b = 0x01;
    *dst = 0x5a;

    __cpu_stt();
    __cpu_sti();
    CHECK( __cpu_stmode( 2 ) == 0, "set CPU status before IADD" );

    clear_instruction( &instruction, CPU_OP_IADD );
    instruction.operand[0] = reg_operand( 8, 0x00400 );
    instruction.operand[1] = reg_operand( 8, 0x00401 );
    instruction.operand[2] = reg_operand( 8, 0x00402 );

    CHECK( __cpu_exec( &instruction ) == CPU_EXEC_SUCCESS, "IADD return code" );
    CHECK( *dst == 0x00, "IADD result" );
    CHECK( __mpu_gtc() == 1, "IADD carry flag" );
    CHECK( __mpu_gtz() == 1, "IADD zero flag" );
    CHECK( __cpu_gtt() == 1, "IADD preserves TF" );
    CHECK( __cpu_gti() == 1, "IADD preserves IF" );
    CHECK( __cpu_gtmode() == 2, "IADD preserves MODE" );
  }

  /* IADC consumes LibMPU carry from the preceding operation. */
  {
    __mpu_UNIT_t *dst = __cpu_reg_ptr( 8, 0x00403 );
    __mpu_UNIT_t *a   = __cpu_reg_ptr( 8, 0x00404 );
    __mpu_UNIT_t *b   = __cpu_reg_ptr( 8, 0x00405 );

    *a = 1;
    *b = 2;
    *dst = 0;
    __mpu_stc();

    clear_instruction( &instruction, CPU_OP_IADC );
    instruction.operand[0] = reg_operand( 8, 0x00403 );
    instruction.operand[1] = reg_operand( 8, 0x00404 );
    instruction.operand[2] = reg_operand( 8, 0x00405 );

    CHECK( __cpu_exec( &instruction ) == CPU_EXEC_SUCCESS, "IADC return code" );
    CHECK( *dst == 4, "IADC result with carry" );
  }

  /* ISUB and ISBB use the same three-register representation. */
  {
    __mpu_UNIT_t *dst = __cpu_reg_ptr( 8, 0x00406 );
    __mpu_UNIT_t *a   = __cpu_reg_ptr( 8, 0x00407 );
    __mpu_UNIT_t *b   = __cpu_reg_ptr( 8, 0x00408 );

    *a = 9;
    *b = 3;

    clear_instruction( &instruction, CPU_OP_ISUB );
    instruction.operand[0] = reg_operand( 8, 0x00406 );
    instruction.operand[1] = reg_operand( 8, 0x00407 );
    instruction.operand[2] = reg_operand( 8, 0x00408 );

    CHECK( __cpu_exec( &instruction ) == CPU_EXEC_SUCCESS, "ISUB return code" );
    CHECK( *dst == 6, "ISUB result" );

    *a = 9;
    *b = 3;
    __mpu_stc();

    clear_instruction( &instruction, CPU_OP_ISBB );
    instruction.operand[0] = reg_operand( 8, 0x00406 );
    instruction.operand[1] = reg_operand( 8, 0x00407 );
    instruction.operand[2] = reg_operand( 8, 0x00408 );

    CHECK( __cpu_exec( &instruction ) == CPU_EXEC_SUCCESS, "ISBB return code" );
    CHECK( *dst == 5, "ISBB result with borrow" );
  }

  /* Operand validation belongs to the common executor. */
  clear_instruction( &instruction, CPU_OP_MOV );
  instruction.operand[0] = reg_operand( 16, 0x00500 );
  instruction.operand[1] = reg_operand( 32, 0x00504 );
  CHECK( __cpu_exec( &instruction ) == CPU_EXEC_WIDTH_FAULT,
         "MOV width mismatch" );

  clear_instruction( &instruction, CPU_OP_IADD );
  instruction.operand[0] = reg_operand( 32, 0x00501 ); /* unaligned */
  instruction.operand[1] = reg_operand( 32, 0x00504 );
  instruction.operand[2] = reg_operand( 32, 0x00508 );
  CHECK( __cpu_exec( &instruction ) == CPU_EXEC_REGISTER_FAULT,
         "invalid register operand" );

  clear_instruction( &instruction, CPU_OP_CLR );
  instruction.operand[0].type = CPU_OPERAND_IMMEDIATE;
  CHECK( __cpu_exec( &instruction ) == CPU_EXEC_OPERAND_FAULT,
         "unsupported operand type" );

  clear_instruction( &instruction, (cpu_opcode)0x7fff );
  CHECK( __cpu_exec( &instruction ) == CPU_EXEC_OPCODE_FAULT,
         "unknown opcode" );

  CHECK( __cpu_exec( (const cpu_instruction *)0 ) == CPU_EXEC_INSTRUCTION_FAULT,
         "NULL instruction" );

  /* HALT sets only the CPU-owned HF part of PSW. */
  __cpu_set_psw( PSW_CF_MASK | PSW_INXF_MASK | PSW_IF_MASK );
  clear_instruction( &instruction, CPU_OP_HALT );
  CHECK( __cpu_exec( &instruction ) == CPU_EXEC_SUCCESS, "HALT return code" );
  psw = __cpu_get_psw();
  CHECK( (psw & PSW_HF_MASK) != 0, "HALT sets HF" );
  CHECK( (psw & PSW_CF_MASK) != 0, "HALT preserves CF" );
  CHECK( (psw & PSW_INXF_MASK) != 0, "HALT preserves INXF" );
  CHECK( (psw & PSW_IF_MASK) != 0, "HALT preserves IF" );

  if( failures != 0 )
  {
    fprintf( stderr, "libcpu instruction regression: %d failure(s)\n", failures );
    return( 1 );
  }

  printf( "libcpu 0.0.3 decoded instruction/executor regression: PASS\n" );
  printf( "opcodes: NOP HALT MOV CLR IADD IADC ISUB ISBB\n" );
  printf( "binary encoding and addressing modes are not defined yet\n" );

  return( 0 );
}