-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEasyCon.c
More file actions
857 lines (834 loc) · 26.5 KB
/
EasyCon.c
File metadata and controls
857 lines (834 loc) · 26.5 KB
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
#include "EasyCon.h"
#include "EasyCon_API.h"
#include "EasyAmiibo.h"
// global variables
static uint8_t mem[MEM_SIZE] = {0xFF, 0xFF, VERSION}; // preallocated memory for all purposes, as well as static instruction carrier
// static variables
static size_t serial_buffer_length = 0; // current length of serial buffer
static bool serial_command_ready = false; // CMD_READY acknowledged, ready to receive command byte
static uint8_t *flash_addr = 0; // start location for EEPROM flashing
static uint16_t flash_index = 0; // current buffer index
static uint16_t flash_count = 0; // number of bytes expected for this time
static uint16_t color_index = 0; // current buffer index
static uint8_t *script_addr = 0; // address of next instruction
static uint8_t *script_eof = 0; // address of EOF
static uint16_t tail_wait = 0; // insert an extra wait before next instruction (used by compressed instruction)
static uint32_t timer_elapsed = 0; // previous execution time
static bool auto_run = false;
static volatile uint8_t echo_ms = 0; // echo counter
// set led state
static volatile uint8_t _ledflag = 0;
// timers define
static volatile uint32_t timer_ms = 0; // script timer
static volatile uint32_t wait_ms = 0; // waiting counter
// some funcs only use in EasyCon
static void binaryop(uint8_t op, uint8_t reg, int16_t value);
// Initialize script. Load static script into EEPROM if exists.
void EasyCon_script_init(void)
{
uint16_t len = 0;
EasyCon_read_start();
if ((mem[0] != 0xFF) || (mem[1] != 0xFF))
{
len = mem[0] | ((mem[1] & 0x7F) << 8);
// flash instructions from firmware
EasyCon_write_start(0);
EasyCon_write_data(0, mem, len);
// for saving flash times
_seed = (uint16_t)EasyCon_read_byte(SEED_OFFSET + 1) << 8 | EasyCon_read_byte(SEED_OFFSET);
_seed += 1;
srand(_seed);
EasyCon_write_data(SEED_OFFSET, (uint8_t *)&_seed, 2);
EasyCon_write_end(0);
}
else
{
// randomize
_seed = (uint16_t)EasyCon_read_byte(SEED_OFFSET + 1) << 8 | EasyCon_read_byte(SEED_OFFSET);
_seed += 1;
srand(_seed);
EasyCon_write_start(1);
EasyCon_write_data(SEED_OFFSET, (uint8_t *)&_seed, 2);
EasyCon_write_end(1);
}
memset(mem, 0, sizeof(mem));
// calculate direction presets
for (int i = 0; i < 16; i++)
{
// part 1
int x = (Min(i, 8)) << 5;
int y = (Max(i, 8) - 8) << 5;
x = Min(x, 255);
y = Min(y, 255);
DX(i) = x;
DY(i) = y;
}
for (int i = 16; i < 32; i++)
{
// part 2
int x = (24 - Min(i, 24)) << 5;
int y = (32 - Max(i, 24)) << 5;
x = Min(x, 255);
y = Min(y, 255);
DX(i) = x;
DY(i) = y;
}
_report_echo = ECHO_TIMES;
// turn on/off led
_ledflag = EasyCon_read_byte(LED_SETTING);
// only if highest bit is 0
auto_run = (EasyCon_read_byte(1) >> 7) == 0;
}
void EasyCon_tick(void)
{
// increment timer
timer_ms++;
// decrement echo counter
if (echo_ms != 0)
echo_ms--;
// decrement waiting counter
if (wait_ms != 0 && (_report_echo == 0 || wait_ms > 1))
wait_ms--;
}
void EasyCon_report_send_callback(void)
{
// decrement echo counter
if (!_script_running || _report_echo > 0 || wait_ms < 2)
{
_report_echo = Max(0, _report_echo - 1);
}
echo_ms = ECHO_INTERVAL;
}
bool EasyCon_is_script_running(void)
{
if (_script_running == 1)
{
return true;
}
else
{
return false;
}
}
// Run script on startup.
void EasyCon_script_auto_start(void)
{
if (auto_run)
EasyCon_script_start();
}
// Run script.
void EasyCon_script_start(void)
{
script_addr = (uint8_t *)2;
uint16_t eof = EasyCon_read_byte(0) | (EasyCon_read_byte(1) << 8);
if (eof == 0xFFFF)
eof = 0;
script_eof = (uint8_t *)(eof & 0x7FFF);
// reset variables
wait_ms = 0;
///////////////////////////
echo_ms = 0;
///////////////////////////
timer_ms = 0;
tail_wait = 0;
memset(mem + VARSPACE_OFFSET, 0, sizeof(mem) - VARSPACE_OFFSET);
_script_running = 1;
_seed = (uint16_t)EasyCon_read_byte(SEED_OFFSET + 1) << 8 | EasyCon_read_byte(SEED_OFFSET);
if (_ledflag != 0)
return;
EasyCon_runningLED_on();
}
// Stop script.
void EasyCon_script_stop(void)
{
_script_running = 0;
timer_elapsed = timer_ms;
////////////////////////////
reset_hid_report();
///////////////////////////
_report_echo = ECHO_TIMES;
EasyCon_runningLED_off();
}
// Process script instructions.
void EasyCon_script_task(void)
{
while (true)
{
// status check
if (!_script_running)
return;
// timer check
if (wait_ms > 0)
return;
// release keys
EasyCon_blink_led();
for (int i = 0; i <= KEYCODE_MAX; i++)
{
if (KEY(i) != 0)
{
KEY(i)
--;
if (KEY(i) == 0)
{
if (i == 32)
{
// LS
set_left_stick(STICK_CENTER, STICK_CENTER);
_report_echo = ECHO_TIMES;
}
else if (i == 33)
{
// RS
set_right_stick(STICK_CENTER, STICK_CENTER);
_report_echo = ECHO_TIMES;
}
else if ((i & 0x10) == 0)
{
// Button
release_buttons(_BV(i));
_report_echo = ECHO_TIMES;
}
else
{
// HAT
set_HAT_switch(HAT_CENTER);
_report_echo = ECHO_TIMES;
}
}
}
}
if (tail_wait != 0)
{
// wait after compressed instruction
SETWAIT(tail_wait);
tail_wait = 0;
return;
}
if (script_addr >= script_eof)
{
// reaches EOF, end script
EasyCon_script_stop();
return;
}
_addr = (uint16_t)script_addr;
_ins0 = EasyCon_read_byte(script_addr++);
_ins1 = EasyCon_read_byte(script_addr++);
int32_t n;
int16_t reg;
// 0b10000000
if (_ins0 & 0x80)
{
// key/stick actions
// 0b01000000
if ((_ins0 & 0x40) == 0)
{
// Instruction : Key 0b11111
_keycode = (_ins0 >> 1) & 0x1F;
// modify report
if ((_keycode & 0x10) == 0)
{
// Button
press_buttons(_BV(_keycode));
_report_echo = ECHO_TIMES;
}
else
{
// HAT
set_HAT_switch(_keycode & 0xF);
_report_echo = ECHO_TIMES;
}
// post effect
if (E_SET)
{
// pre-loaded duration
SETWAIT(REG(_e_val));
RESETAFTER(_keycode, 1);
}
// 0b00000001
else if ((_ins0 & 0x01) == 0)
{
// standard
n = _ins1;
// unscale
n *= 10;
SETWAIT(n);
RESETAFTER(_keycode, 1);
}
// 0b10000000
else if ((_ins1 & 0x80) == 0)
{
// compressed 0b01111111
tail_wait = _ins1 & 0x7F;
// unscale
tail_wait *= 50;
SETWAIT(50);
RESETAFTER(_keycode, 1);
}
else
{
// hold 0b01111111
n = _ins1 & 0x7F;
RESETAFTER(_keycode, n);
}
}
else
{
// Instruction : Stick
_lr = (_ins0 >> 5) & 1;
_keycode = 32 | _lr;
// 0b11111
_direction = _ins0 & 0x1F;
// modify report
if (_lr)
{
// RS
set_right_stick(DX(_direction), DY(_direction));
_report_echo = ECHO_TIMES;
}
else
{
// LS
set_left_stick(DX(_direction), DY(_direction));
_report_echo = ECHO_TIMES;
}
// post effect
if (E_SET)
{
// pre-loaded duration
SETWAIT(REG(_e_val));
RESETAFTER(_keycode, 1);
}
// 0b10000000
else if ((_ins1 & 0x80) == 0)
{
// standard 0b01111111
n = _ins1 & 0x7F;
// unscale
n *= 50;
SETWAIT(n);
RESETAFTER(_keycode, 1);
}
else
{
// hold 0b01111111
n = _ins1 & 0x7F;
RESETAFTER(_keycode, n);
}
}
}
else
{
// flow control 0b1111
switch ((_ins0 >> 3) & 0x0F)
{
// 0b0000
case 0x00:
// 0b100
if ((_ins0 & 0x04) == 0)
{
// empty
}
else
{
// Instruction : SerialPrint
reg = _ins & ((1 << 9) - 1);
// 0b10
if ((_ins0 & 0x02) == 0)
{
EasyCon_serial_send(reg);
EasyCon_serial_send(reg >> 8);
}
else
{
EasyCon_serial_send(mem[reg]);
EasyCon_serial_send(mem[reg + 1]);
}
break;
}
break;
// 0b0001
case 0x01:
// Instruction : Wait
if (E_SET)
{
// pre-loaded duration
n = REG(_e_val);
}
// 0b100
else if ((_ins0 & 0x04) == 0)
{
// standard
n = _ins & ((1 << 10) - 1);
// unscale
n *= 10;
}
// 0b10
else if ((_ins0 & 0x02) == 0)
{
// extended
_ins2 = EasyCon_read_byte(script_addr++);
_ins3 = EasyCon_read_byte(script_addr++);
n = _insEx & ((1L << 25) - 1);
// unscale
n *= 10;
}
else
{
// high precision
n = _ins & ((1 << 9) - 1);
}
SETWAIT(n);
break;
// 0b0010
case 0x02:
// Instruction : For
if (_forstackindex == 0 || FOR_ADDR(_forstackindex - 1) != _addr)
{
// loop initialize
_forstackindex++;
FOR_I(_forstackindex - 1) = 0;
FOR_ADDR(_forstackindex - 1) = _addr;
FOR_NEXT(_forstackindex - 1) = _ins & ((1 << 11) - 1);
// pre-loaded arguments
if (E_SET)
{
// iterator
_ri0 = REG(_e_val) & 0xF;
if (_ri0 != 0)
{
// store iterator
FOR_I(_forstackindex - 1) = _ri0 | 0x80000000;
}
// count
_ri1 = (REG(_e_val) >> 4) & 0xF;
if (_ri1 != 0)
{
// write loop count
FOR_C(_forstackindex - 1) = REG(_ri1);
// Mode 2 : loop count overwritten
E(2);
// jump to next (for condition checking)
JUMP(FOR_NEXT(_forstackindex - 1));
break;
}
}
// Mode 0 : init
E(0);
// jump to next (for further initialization)
JUMP(FOR_NEXT(_forstackindex - 1));
break;
}
break;
// 0b0011
case 0x03:
// Instruction : Next 0b100
if (_ins0 & 0x04)
{
// extended
_ins2 = EasyCon_read_byte(script_addr++);
_ins3 = EasyCon_read_byte(script_addr++);
}
if (E_SET)
{
if (_e_val == 1)
{
// Mode 1 : break
_forstackindex--;
break;
}
else if (_e_val == 0)
{
// Mode 0 : init
if (_e_val == 0)
{
// initialize loop count 0b100
if ((_ins0 & 0x04) == 0)
{
// small number
FOR_C(_forstackindex - 1) = _ins & ((1 << 10) - 1);
if (FOR_C(_forstackindex - 1) == 0)
{
// infinite loop
FOR_C(_forstackindex - 1) = 0x80000000;
}
}
else
{
// large number
FOR_C(_forstackindex - 1) = _insEx & ((1L << 26) - 1);
}
}
}
else if (_e_val == 2)
{
// Mode 2 : loop count overwritten
// do nothing here
}
}
else
{
// normal loop step
if (FOR_I(_forstackindex - 1) & 0x80000000)
{
// iterator
REG(FOR_I(_forstackindex - 1) & 0xF) += 1;
}
else
{
// loop variable
FOR_I(_forstackindex - 1) += 1;
}
}
// check condition
if (FOR_I(_forstackindex - 1) & 0x80000000)
n = REG(FOR_I(_forstackindex - 1) & 0xF);
else
n = FOR_I(_forstackindex - 1);
if (FOR_C(_forstackindex - 1) != 0x80000000 && n >= FOR_C(_forstackindex - 1))
{
// end for
_forstackindex--;
}
else
{
// jump back
JUMP(FOR_ADDR(_forstackindex - 1));
}
break;
// 0b0100
case 0x04:
// 0b100
if (_ins0 & 0x04)
{
// comparisons 0b111
_ri0 = (_ins1 >> 3) & 0x07;
// 0b111
_ri1 = _ins1 & 0x07;
// 0b11
switch (_ins0 & 0x03)
{
// 0b00
case 0x00:
// Instruction : Equal
_v = REG(_ri0) == REG(_ri1);
break;
// 0b01
case 0x01:
// Instruction : NotEqual
_v = REG(_ri0) != REG(_ri1);
break;
// 0b10
case 0x02:
// Instruction : LessThan
_v = REG(_ri0) < REG(_ri1);
break;
// 0b11
case 0x03:
// Instruction : LessOrEqual
_v = REG(_ri0) <= REG(_ri1);
break;
}
_v = (bool)_v;
_flag = (bool)_flag;
switch (_ins1 >> 6)
{
// 0b00
case 0x00:
// assign
_flag = _v;
break;
// 0b01
case 0x01:
// and
_flag &= _v;
break;
// 0b10
case 0x02:
// or
_flag |= _v;
break;
// 0b11
case 0x03:
// xor
_flag ^= _v;
break;
}
}
else
{
switch (_ins1 >> 5)
{
// 0b000
case 0x00:
// Instruction : Break 0b10000
if ((_ins1 & 0x10) && !_flag)
break;
// 0b1111
_v = _ins1 & 0x0F;
_forstackindex -= _v;
E(1);
JUMP(FOR_NEXT(_forstackindex - 1));
break;
// 0b001
case 0x01:
// Instruction : Continue 0b10000
if ((_ins1 & 0x10) && !_flag)
break;
// 0b1111
_v = _ins1 & 0x0F;
_forstackindex -= _v;
JUMP(FOR_NEXT(_forstackindex - 1));
break;
// 0b111
case 0x07:
// Instruction : Return 0b10000
if ((_ins1 & 0x10) && !_flag)
break;
if (_callstackindex)
{
// sub function
// pop return address
JUMP(CALLSTACK(_callstackindex - 1));
_callstackindex--;
}
else
{
// main function
EasyCon_script_stop();
}
break;
}
}
break;
// 0b0101
case 0x05:
// 0b100
if ((_ins0 & 0x04) == 0)
{
// 0b111
_ri0 = (_ins >> 7) & 0x07;
if (_ri0 == 0)
{
if ((_ins1 & (1 << 6)) == 0)
{
// binary operations on instant
_ins2 = EasyCon_read_byte(script_addr++);
_ins3 = EasyCon_read_byte(script_addr++);
// 0b111
_v = (_ins >> 3) & 0x07;
_ri0 = _ins & 0x07;
reg = _insEx;
binaryop(_v, _ri0, reg);
}
else
{
// preserved
}
}
else
{
// Instruction : Mov 0b01111111
reg = _ins1 & 0x7F;
// fill sign bit
reg <<= 9;
reg >>= 9;
REG(_ri0) = reg;
}
}
// 0b110 0b100
else if ((_ins0 & 0x06) == 0x04)
{
// binary operations on register
_v = (_ins >> 6) & 0x07;
_ri0 = (_ins >> 3) & 0x07;
_ri1 = _ins & 0x07;
binaryop(_v, _ri0, REG(_ri1));
}
// 0b111 0b110
else if ((_ins0 & 0x07) == 0x06)
{
// bitwise shift
_ri0 = (_ins1 >> 4) & 0x07;
_v = _ins1 & 0x0F;
if (_ri0 == 0)
break;
if ((_ins1 & 0x80) == 0)
{
// Instruction : ShL
REG(_ri0) <<= _v;
}
else
{
// Instruction : ShR
REG(_ri0) >>= _v;
}
}
else
{
// unary operations
_ri0 = _ins1 & 0x07;
switch ((_ins1 >> 3) & 0x0F)
{
// 0b0010
case 0x02:
// Instruction : Negative
if (_ri0 == 0)
break;
REG(_ri0) = -REG(_ri0);
break;
// 0b0011
case 0x03:
// Instruction : Not
if (_ri0 == 0)
break;
REG(_ri0) = ~REG(_ri0);
break;
// 0b0100
case 0x04:
// Instruction : Push
_stackindex++;
STACK(_stackindex - 1) = REG(_ri0);
break;
// 0b0101
case 0x05:
// Instruction : Pop
if (_ri0 == 0)
break;
REG(_ri0) = STACK(_stackindex - 1);
_stackindex--;
break;
// 0b0111
case 0x07:
// Instruction : StoreOp
E(_ri0);
break;
// 0b1000
case 0x08:
// Instruction : Bool
if (_ri0 == 0)
break;
REG(_ri0) = (bool)REG(_ri0);
break;
// 0b1001
case 0x09:
// Instruction : Rand
if (_ri0 == 0)
break;
if (!_seed)
{
_seed = timer_ms;
EasyCon_write_start(1);
EasyCon_write_data(SEED_OFFSET, (uint8_t *)&_seed, 2);
EasyCon_write_end(1);
srand(_seed);
}
REG(_ri0) = rand() % REG(_ri0);
break;
}
}
break;
// 0b0110
case 0x06:
// branches
reg = _ins & ((1 << 9) - 1);
reg = reg << 7 >> 6;
switch ((_ins0 >> 1) & 0x3)
{
// 0b00
case 0x00:
// Instruction : Branch
JUMPNEAR(reg);
break;
// 0b01
case 0x01:
// Instruction : BranchTrue
if (_flag)
JUMPNEAR(reg);
break;
// 0b10
case 0x02:
// Instruction : BranchFalse
if (!_flag)
JUMPNEAR(reg);
break;
// 0b11
case 0x03:
// Instruction : Call
_callstackindex++;
STACK(_callstackindex - 1) = (int16_t)script_addr;
JUMPNEAR(reg);
break;
}
break;
// 0b0111
case 0x07:
// amiibo instructions?
break;
}
}
}
}
// Perform binary operations by operator code
static void binaryop(uint8_t op, uint8_t reg, int16_t value)
{
if (reg == 0)
return;
switch (op)
{
// 0b000
case 0x00:
// Mov
REG(reg) = value;
break;
// 0b001
case 0x01:
// Add
REG(reg) += value;
break;
// 0b010
case 0x02:
// Mul
REG(reg) *= value;
break;
// 0b011
case 0x03:
// Div
REG(reg) /= value;
break;
// 0b100
case 0x04:
// Mod
REG(reg) %= value;
break;
// 0b101
case 0x05:
// And
REG(reg) &= value;
break;
// 0b110
case 0x06:
// Or
REG(reg) |= value;
break;
// 0b111
case 0x07:
// Xor
REG(reg) ^= value;
break;
}
}
uint8_t EasyCon_is_LED_enable(void)
{
return _ledflag;
}
/* check if need send report.
* need implement
*/
bool EasyCon_need_send_report(void)
{
if (echo_ms == 0)
{
return true;
}
return false;
}