v4_cli 0.5.0

CLI tool for V4 VM bytecode deployment
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <cstring>

#include "doctest.h"
#include "mock_hal.h"
#include "v4/errors.hpp"
#include "v4/hal.h"
#include "v4/internal/vm.h"
#include "v4/opcodes.hpp"
#include "v4/sys_ids.h"
#include "v4/vm_api.h"

/* Mock HAL console control functions */
extern "C" void mock_hal_console_inject_input(const char* data, int len);
extern "C" const char* mock_hal_console_get_output(int* out_len);

/* Helper to emit bytecode */
static void emit8(v4_u8* code, int* k, v4_u8 byte)
{
  code[(*k)++] = byte;
}

static void emit32(v4_u8* code, int* k, v4_u32 val)
{
  code[(*k)++] = (val >> 0) & 0xFF;
  code[(*k)++] = (val >> 8) & 0xFF;
  code[(*k)++] = (val >> 16) & 0xFF;
  code[(*k)++] = (val >> 24) & 0xFF;
}

extern "C" v4_err vm_exec_raw(Vm* vm, const v4_u8* code, int code_len);

TEST_CASE("SYS GPIO_INIT")
{
  mock_hal_reset();

  Vm vm{};
  vm_reset(&vm);

  v4_u8 code[32];
  int k = 0;

  // Push pin 13, mode 1 (OUTPUT)
  emit8(code, &k, static_cast<v4_u8>(v4::Op::LIT));
  emit32(code, &k, 13);
  emit8(code, &k, static_cast<v4_u8>(v4::Op::LIT));
  emit32(code, &k, 1);

  // SYS GPIO_INIT
  emit8(code, &k, static_cast<v4_u8>(v4::Op::SYS));
  emit8(code, &k, V4_SYS_GPIO_INIT);

  // RET
  emit8(code, &k, static_cast<v4_u8>(v4::Op::RET));

  // Execute
  v4_err err = vm_exec_raw(&vm, code, k);
  REQUIRE(err == 0);

  // Check stack: should have error code (0)
  CHECK(vm_ds_depth_public(&vm) == 1);
  CHECK(vm_ds_peek_public(&vm, 0) == 0);

  // Check mock HAL state
  CHECK(mock_hal_gpio_get_mode(13) == V4_HAL_GPIO_MODE_OUTPUT);
}

TEST_CASE("SYS GPIO_WRITE and GPIO_READ")
{
  mock_hal_reset();

  Vm vm{};
  vm_reset(&vm);

  // First init pin 10 as output
  hal_gpio_mode(10, HAL_GPIO_OUTPUT);

  v4_u8 code[64];
  int k = 0;

  // Write high: pin=10, value=1
  emit8(code, &k, static_cast<v4_u8>(v4::Op::LIT));
  emit32(code, &k, 10);
  emit8(code, &k, static_cast<v4_u8>(v4::Op::LIT));
  emit32(code, &k, 1);
  emit8(code, &k, static_cast<v4_u8>(v4::Op::SYS));
  emit8(code, &k, V4_SYS_GPIO_WRITE);

  // Drop error code
  emit8(code, &k, static_cast<v4_u8>(v4::Op::DROP));

  // Read back: pin=10
  emit8(code, &k, static_cast<v4_u8>(v4::Op::LIT));
  emit32(code, &k, 10);
  emit8(code, &k, static_cast<v4_u8>(v4::Op::SYS));
  emit8(code, &k, V4_SYS_GPIO_READ);

  emit8(code, &k, static_cast<v4_u8>(v4::Op::RET));

  v4_err err = vm_exec_raw(&vm, code, k);
  REQUIRE(err == 0);

  // Stack: [value, err]
  CHECK(vm_ds_depth_public(&vm) == 2);
  CHECK(vm_ds_peek_public(&vm, 0) == 0);  // err = 0
  CHECK(vm_ds_peek_public(&vm, 1) == 1);  // value = 1
}

TEST_CASE("SYS UART_INIT and UART_PUTC")
{
  mock_hal_reset();

  Vm vm{};
  vm_reset(&vm);

  v4_u8 code[64];
  int k = 0;

  // Init UART0 at 115200
  emit8(code, &k, static_cast<v4_u8>(v4::Op::LIT));
  emit32(code, &k, 0);  // port
  emit8(code, &k, static_cast<v4_u8>(v4::Op::LIT));
  emit32(code, &k, 115200);  // baudrate
  emit8(code, &k, static_cast<v4_u8>(v4::Op::SYS));
  emit8(code, &k, V4_SYS_UART_INIT);
  emit8(code, &k, static_cast<v4_u8>(v4::Op::DROP));  // Drop error

  // Send 'A' (65)
  emit8(code, &k, static_cast<v4_u8>(v4::Op::LIT));
  emit32(code, &k, 0);  // port
  emit8(code, &k, static_cast<v4_u8>(v4::Op::LIT));
  emit32(code, &k, 65);  // char 'A'
  emit8(code, &k, static_cast<v4_u8>(v4::Op::SYS));
  emit8(code, &k, V4_SYS_UART_PUTC);

  emit8(code, &k, static_cast<v4_u8>(v4::Op::RET));

  v4_err err = vm_exec_raw(&vm, code, k);
  REQUIRE(err == 0);

  // Check transmitted data
  int tx_len;
  const char* tx_buf = mock_hal_uart_get_tx(0, &tx_len);
  REQUIRE(tx_buf != nullptr);
  CHECK(tx_len == 1);
  CHECK(tx_buf[0] == 'A');
}

TEST_CASE("SYS UART_GETC")
{
  mock_hal_reset();

  // Init UART and inject data
  hal_uart_config_t config = {115200, 8, 1, 0};
  hal_uart_open(0, &config);
  mock_hal_uart_inject_rx(0, "X", 1);

  Vm vm{};
  vm_reset(&vm);

  v4_u8 code[32];
  int k = 0;

  // Receive from UART0
  emit8(code, &k, static_cast<v4_u8>(v4::Op::LIT));
  emit32(code, &k, 0);  // port
  emit8(code, &k, static_cast<v4_u8>(v4::Op::SYS));
  emit8(code, &k, V4_SYS_UART_GETC);

  emit8(code, &k, static_cast<v4_u8>(v4::Op::RET));

  v4_err err = vm_exec_raw(&vm, code, k);
  REQUIRE(err == 0);

  // Stack: [char, err]
  CHECK(vm_ds_depth_public(&vm) == 2);
  CHECK(vm_ds_peek_public(&vm, 0) == 0);    // err = 0
  CHECK(vm_ds_peek_public(&vm, 1) == 'X');  // char = 'X'
}

TEST_CASE("SYS MILLIS")
{
  mock_hal_reset();
  mock_hal_set_millis(12345);

  Vm vm{};
  vm_reset(&vm);

  v4_u8 code[16];
  int k = 0;

  emit8(code, &k, static_cast<v4_u8>(v4::Op::SYS));
  emit8(code, &k, V4_SYS_MILLIS);
  emit8(code, &k, static_cast<v4_u8>(v4::Op::RET));

  v4_err err = vm_exec_raw(&vm, code, k);
  REQUIRE(err == 0);

  CHECK(vm_ds_depth_public(&vm) == 1);
  CHECK(vm_ds_peek_public(&vm, 0) == 12345);
}

TEST_CASE("SYS MICROS")
{
  mock_hal_reset();
  mock_hal_set_micros(0x123456789ABCULL);

  Vm vm{};
  vm_reset(&vm);

  v4_u8 code[16];
  int k = 0;

  emit8(code, &k, static_cast<v4_u8>(v4::Op::SYS));
  emit8(code, &k, V4_SYS_MICROS);
  emit8(code, &k, static_cast<v4_u8>(v4::Op::RET));

  v4_err err = vm_exec_raw(&vm, code, k);
  REQUIRE(err == 0);

  // Stack: [us_lo, us_hi]
  CHECK(vm_ds_depth_public(&vm) == 2);
  uint32_t us_hi = static_cast<uint32_t>(vm_ds_peek_public(&vm, 0));
  uint32_t us_lo = static_cast<uint32_t>(vm_ds_peek_public(&vm, 1));

  uint64_t reconstructed = (static_cast<uint64_t>(us_hi) << 32) | us_lo;
  CHECK(reconstructed == 0x123456789ABCULL);
}

TEST_CASE("SYS DELAY_MS")
{
  mock_hal_reset();
  mock_hal_set_millis(1000);

  Vm vm{};
  vm_reset(&vm);

  v4_u8 code[32];
  int k = 0;

  // Delay 500ms
  emit8(code, &k, static_cast<v4_u8>(v4::Op::LIT));
  emit32(code, &k, 500);
  emit8(code, &k, static_cast<v4_u8>(v4::Op::SYS));
  emit8(code, &k, V4_SYS_DELAY_MS);

  // Get millis after delay
  emit8(code, &k, static_cast<v4_u8>(v4::Op::SYS));
  emit8(code, &k, V4_SYS_MILLIS);

  emit8(code, &k, static_cast<v4_u8>(v4::Op::RET));

  v4_err err = vm_exec_raw(&vm, code, k);
  REQUIRE(err == 0);

  // Should be 1000 + 500 = 1500
  CHECK(vm_ds_depth_public(&vm) == 1);
  CHECK(vm_ds_peek_public(&vm, 0) == 1500);
}

TEST_CASE("SYS DELAY_US")
{
  mock_hal_reset();
  mock_hal_set_micros(10000);

  Vm vm{};
  vm_reset(&vm);

  v4_u8 code[32];
  int k = 0;

  // Delay 250us
  emit8(code, &k, static_cast<v4_u8>(v4::Op::LIT));
  emit32(code, &k, 250);
  emit8(code, &k, static_cast<v4_u8>(v4::Op::SYS));
  emit8(code, &k, V4_SYS_DELAY_US);

  // Get micros after delay (lower 32 bits)
  emit8(code, &k, static_cast<v4_u8>(v4::Op::SYS));
  emit8(code, &k, V4_SYS_MICROS);
  emit8(code, &k, static_cast<v4_u8>(v4::Op::DROP));  // Drop us_hi

  emit8(code, &k, static_cast<v4_u8>(v4::Op::RET));

  v4_err err = vm_exec_raw(&vm, code, k);
  REQUIRE(err == 0);

  // Should be 10000 + 250 = 10250
  CHECK(vm_ds_depth_public(&vm) == 1);
  CHECK(vm_ds_peek_public(&vm, 0) == 10250);
}

TEST_CASE("SYS SYSTEM_INFO")
{
  mock_hal_reset();

  Vm vm{};
  vm_reset(&vm);

  v4_u8 code[16];
  int k = 0;

  emit8(code, &k, static_cast<v4_u8>(v4::Op::SYS));
  emit8(code, &k, V4_SYS_SYSTEM_INFO);
  emit8(code, &k, static_cast<v4_u8>(v4::Op::RET));

  v4_err err = vm_exec_raw(&vm, code, k);
  REQUIRE(err == 0);

  // SYSTEM_INFO returns HAL_ERR_NOTSUP (-6) in new HAL API
  // Stack: [0, 0, err]
  CHECK(vm_ds_depth_public(&vm) == 3);
  v4_i32 hal_err = vm_ds_peek_public(&vm, 0);
  CHECK(hal_err == HAL_ERR_NOTSUP);  // -6
}

TEST_CASE("SYS error handling - invalid pin")
{
  mock_hal_reset();

  Vm vm{};
  vm_reset(&vm);

  v4_u8 code[32];
  int k = 0;

  // Try to init invalid pin (e.g., 999)
  emit8(code, &k, static_cast<v4_u8>(v4::Op::LIT));
  emit32(code, &k, 999);
  emit8(code, &k, static_cast<v4_u8>(v4::Op::LIT));
  emit32(code, &k, 1);
  emit8(code, &k, static_cast<v4_u8>(v4::Op::SYS));
  emit8(code, &k, V4_SYS_GPIO_INIT);

  emit8(code, &k, static_cast<v4_u8>(v4::Op::RET));

  v4_err err = vm_exec_raw(&vm, code, k);
  REQUIRE(err == 0);

  // Should have error code on stack
  CHECK(vm_ds_depth_public(&vm) == 1);
  v4_i32 hal_err = vm_ds_peek_public(&vm, 0);
  CHECK(hal_err == HAL_ERR_PARAM);  // -1 (invalid parameter)
}

TEST_CASE("SYS EMIT")
{
  mock_hal_reset();

  Vm vm{};
  vm_reset(&vm);

  v4_u8 code[32];
  int k = 0;

  // Emit 'A' (65)
  emit8(code, &k, static_cast<v4_u8>(v4::Op::LIT));
  emit32(code, &k, 65);
  emit8(code, &k, static_cast<v4_u8>(v4::Op::SYS));
  emit8(code, &k, V4_SYS_EMIT);

  // Drop error code
  emit8(code, &k, static_cast<v4_u8>(v4::Op::DROP));

  // Emit 'B' (66)
  emit8(code, &k, static_cast<v4_u8>(v4::Op::LIT));
  emit32(code, &k, 66);
  emit8(code, &k, static_cast<v4_u8>(v4::Op::SYS));
  emit8(code, &k, V4_SYS_EMIT);

  // Drop error code
  emit8(code, &k, static_cast<v4_u8>(v4::Op::DROP));

  emit8(code, &k, static_cast<v4_u8>(v4::Op::RET));

  v4_err err = vm_exec_raw(&vm, code, k);
  REQUIRE(err == 0);

  // Check console output
  int out_len = 0;
  const char* output = mock_hal_console_get_output(&out_len);
  REQUIRE(out_len == 2);
  CHECK(output[0] == 'A');
  CHECK(output[1] == 'B');
}

TEST_CASE("SYS KEY")
{
  mock_hal_reset();

  // Inject input characters
  mock_hal_console_inject_input("XY", 2);

  Vm vm{};
  vm_reset(&vm);

  v4_u8 code[32];
  int k = 0;

  // Read first character
  emit8(code, &k, static_cast<v4_u8>(v4::Op::SYS));
  emit8(code, &k, V4_SYS_KEY);

  // Drop error code
  emit8(code, &k, static_cast<v4_u8>(v4::Op::DROP));

  // Read second character
  emit8(code, &k, static_cast<v4_u8>(v4::Op::SYS));
  emit8(code, &k, V4_SYS_KEY);

  // Drop error code
  emit8(code, &k, static_cast<v4_u8>(v4::Op::DROP));

  emit8(code, &k, static_cast<v4_u8>(v4::Op::RET));

  v4_err err = vm_exec_raw(&vm, code, k);
  REQUIRE(err == 0);

  // Stack: [first_char, second_char]
  CHECK(vm_ds_depth_public(&vm) == 2);
  CHECK(vm_ds_peek_public(&vm, 1) == 'X');
  CHECK(vm_ds_peek_public(&vm, 0) == 'Y');
}

TEST_CASE("SYS KEY - no data available")
{
  mock_hal_reset();

  // No input injected

  Vm vm{};
  vm_reset(&vm);

  v4_u8 code[16];
  int k = 0;

  // Try to read character
  emit8(code, &k, static_cast<v4_u8>(v4::Op::SYS));
  emit8(code, &k, V4_SYS_KEY);

  emit8(code, &k, static_cast<v4_u8>(v4::Op::RET));

  v4_err err = vm_exec_raw(&vm, code, k);
  REQUIRE(err == 0);

  // Stack: [char, err]
  // In new HAL API, hal_console_read returns 0 when no data available (not an error)
  CHECK(vm_ds_depth_public(&vm) == 2);
  v4_i32 hal_err = vm_ds_peek_public(&vm, 0);
  CHECK(hal_err == HAL_OK);  // 0 (success, but 0 bytes read)
}