-
Notifications
You must be signed in to change notification settings - Fork 6
/
reg.cpp
2715 lines (2582 loc) · 61.5 KB
/
reg.cpp
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
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Renesas RL78 processor module for IDA
// Based on R01US0015EJ0220, Rev.2.20, Nov 20, 2014
// In reality all registers are just references to memory locations.
// Pick the ones which are architecturally defined to be commonly used in
// instructions to list here, as well as those which IDA needs to track.
// The rest can be named via io naming.
enum RL78Register : decltype(op_t::reg) {
rX, rA, rC, rB, rE, rD, rL, rH, rAX, rBC, rDE, rHL,
rRB0, rRB1, rRB2, rRB3,
rPSW, rSP,
rCY,
// Fake segreg to make IDA happy
rVcs, rVds,
// RL78 segregs we'll use IDA to track
rCS, rES,
r_count
};
static const char *const reg_names[] = {
"x", "a", "c", "b", "e", "d", "l", "h", "ax", "bc", "de", "hl",
"rb0", "rb1", "rb2", "rb3",
"psw", "sp",
"cy",
"vcs", "vds",
"cs", "es",
};
static_assert(qnumber(reg_names) == r_count, "array doesn't match enum");
enum : decltype(insn_t::itype) {
RL78_unknown,
RL78_mov,
RL78_xch,
RL78_oneb,
RL78_clrb,
RL78_movs,
RL78_movw,
RL78_xchw,
RL78_onew,
RL78_clrw,
RL78_add,
RL78_addc,
RL78_sub,
RL78_subc,
RL78_and,
RL78_or,
RL78_xor,
RL78_cmp,
RL78_cmp0,
RL78_cmps,
RL78_addw,
RL78_subw,
RL78_cmpw,
RL78_mulu,
// S3 only
RL78_mulhu,
RL78_mulh,
RL78_divhu,
RL78_divwu,
RL78_machu,
RL78_mach,
// S3 only end
RL78_inc,
RL78_dec,
RL78_incw,
RL78_decw,
RL78_shr,
RL78_shrw,
RL78_shl,
RL78_shlw,
RL78_sar,
RL78_sarw,
RL78_ror,
RL78_rol,
RL78_rorc,
RL78_rolc,
RL78_rolwc,
RL78_mov1,
RL78_and1,
RL78_or1,
RL78_xor1,
RL78_set1,
RL78_clr1,
RL78_not1,
RL78_call,
RL78_callt,
RL78_brk,
RL78_ret,
RL78_reti,
RL78_retb,
RL78_push,
RL78_pop,
RL78_br,
RL78_bc,
RL78_bnc,
RL78_bz,
RL78_bnz,
RL78_bh,
RL78_bnh,
RL78_bt,
RL78_bf,
RL78_btclr,
RL78_skc,
RL78_sknc,
RL78_skz,
RL78_sknz,
RL78_skh,
RL78_sknh,
// sel is not on S1 (only one bank)
RL78_sel,
RL78_nop,
RL78_ei,
RL78_di,
RL78_halt,
RL78_stop,
// not real opcode?
RL78_brk1,
RL78_itype_count
};
static const instruc_t instructions[] = {
{ "" },
{ "mov", CF_CHG1 | CF_USE2 },
{ "xch", CF_CHG1 | CF_CHG2 },
{ "oneb", CF_CHG1 },
{ "clrb", CF_CHG1 },
{ "movs", CF_CHG1 | CF_USE2 },
{ "movw", CF_CHG1 | CF_USE2 },
{ "xchw", CF_CHG1 | CF_CHG2 },
{ "onew", CF_CHG1 },
{ "clrw", CF_CHG1 },
{ "add", CF_CHG1 | CF_USE2 },
{ "addc", CF_CHG1 | CF_USE2 },
{ "sub", CF_CHG1 | CF_USE2 },
{ "subc", CF_CHG1 | CF_USE2 },
{ "and", CF_CHG1 | CF_USE2 },
{ "or", CF_CHG1 | CF_USE2 },
{ "xor", CF_CHG1 | CF_USE2 },
{ "cmp", CF_USE1 | CF_USE2 },
{ "cmp0", CF_USE1 },
{ "cmps", CF_USE1 | CF_USE2 },
{ "addw", CF_CHG1 | CF_USE2 },
{ "subw", CF_CHG1 | CF_USE2 },
{ "cmpw", CF_USE1 | CF_USE2 },
{ "mulu", CF_USE1 },
{ "mulhu", },
{ "mulh", },
{ "divhu", },
{ "divwu", },
{ "machu", },
{ "mach", },
{ "inc", CF_CHG1 },
{ "dec", CF_CHG1 },
{ "incw", CF_CHG1 },
{ "decw", CF_CHG1 },
{ "shr", CF_CHG1 | CF_USE2 | CF_SHFT },
{ "shrw", CF_CHG1 | CF_USE2 | CF_SHFT },
{ "shl", CF_CHG1 | CF_USE2 | CF_SHFT },
{ "shlw", CF_CHG1 | CF_USE2 | CF_SHFT },
{ "sar", CF_CHG1 | CF_USE2 | CF_SHFT },
{ "sarw", CF_CHG1 | CF_USE2 | CF_SHFT },
{ "ror", CF_CHG1 | CF_USE2 },
{ "rol", CF_CHG1 },
{ "rorc", CF_CHG1 },
{ "rolc", CF_CHG1 },
{ "rolwc", CF_CHG1 },
{ "mov1", CF_CHG1 | CF_USE2 },
{ "and1", CF_CHG1 | CF_USE2 },
{ "or1", CF_CHG1 | CF_USE2 },
{ "xor1", CF_CHG1 | CF_USE2 },
{ "set1", CF_CHG1 },
{ "clr1", CF_CHG1 },
{ "not1", CF_CHG1 },
{ "call", CF_USE1 | CF_CALL },
{ "callt", CF_USE1 | CF_CALL },
{ "brk", CF_STOP },
{ "ret", CF_STOP },
{ "reti", CF_STOP },
{ "retb", CF_STOP },
{ "push", CF_USE1 },
{ "pop", CF_CHG1 },
{ "br", CF_USE1 | CF_STOP | CF_JUMP },
{ "bc", CF_USE1 },
{ "bnc", CF_USE1 },
{ "bz", CF_USE1 },
{ "bnz", CF_USE1 },
{ "bh", CF_USE1 },
{ "bnh", CF_USE1 },
{ "bt", CF_USE1 | CF_USE2 },
{ "bf", CF_USE1 | CF_USE2 },
{ "btclr", CF_USE1 | CF_USE2 },
{ "skc", }, // sk* possibly branch over *next* insn
{ "sknc", },
{ "skz", },
{ "sknz", },
{ "skh", },
{ "sknh", },
{ "sel", }, // CF_USE1 ?
{ "nop", },
{ "ei", }, // ei/di are macros
{ "di", },
{ "halt", },
{ "stop", CF_STOP },
{ "brk1", CF_STOP },
};
static_assert(qnumber(instructions) == RL78_itype_count, "array doesn't match enum");
static const asm_t assembler = {
AS_COLON | AS_N2CHR | ASH_HEXF0 | ASD_DECF0 | ASB_BINF0 | AS_ONEDUP | AS_NOXRF,
0,
"RL78 Assembler",
0,
nullptr, // array of automatically generated header lines
// they appear at the start of disassembled text
".org", // org directive
".end", // end directive
";", // comment string (see also cmnt2)
'"', // string literal delimiter
'\'', // char constant delimiter
"'\"", // special chars that can not appear
// as is in string and char literals
// Data representation (db,dw,...):
".db", // ascii
".db", // byte
".dw", // word
".dd", // dword
nullptr, // qword
nullptr, // oword
nullptr, // float
nullptr, // double
nullptr, // tbytes
nullptr, // packreal
nullptr, // dups
nullptr, // bss
".equ", // equ
nullptr, // seg
"$", // curip
nullptr, // out_func_header
nullptr, // out_func_footer
nullptr, // public
nullptr, // weak
nullptr, // extern
nullptr, // communal variable
nullptr, // get_type_name
nullptr, // align
'(', // lbrace
')', // rbrace
// Assembler-time operators
nullptr, // %
nullptr, // &
nullptr, // |
nullptr, // ^
nullptr, // ~
nullptr, // <<
nullptr, // >>
nullptr, // size of type (format string)
0, // flag2
nullptr, // cmnt2
nullptr, // low8
nullptr, // high8
nullptr, // low16
nullptr, // high16
nullptr, // include_fmt
nullptr, // vstruc_fmt
nullptr, // rva
nullptr, // yword
};
#define RL78_MAX_OPERANDS 2
enum : decltype(insn_t::auxpref) {
kHasPrefix = 1,
};
enum : optype_t {
o_bit = o_idpspec0,
};
enum : decltype(op_t::specflag2) {
kDirect,
kIndirect,
};
enum RL78Phrase : decltype(op_t::phrase) {
kReg,
kHlReg,
kReg8,
kReg16,
};
// for o_bit, actual type of base operand
#define bit_base_type specflag1
// for o_bit, indicate if operand is indirect
#define addr_mode specflag2
// for o_phrase and o_displ, RL78Phrase value
#define ind_reg specflag2
// Currently don't actually track widths, width prefixes seem to just make
// disasm distracting.
/* RL78 has 2 segment registers:
CS
only updated explicitly (or as memory)
only used via `call rp` or `br ax`
ES
only updated explicitly (or as memory)
`prefix` opcode indicates ES is used in memory ref done by insn
*/
/*
# Immediate data specification
$ 8-bit relative address specification
$! 16-bit relative address specification
! 16-bit absolute address specification
!! 20-bit absolute address specification
[ ] Indirect address specification
ES: Extension address specification
$addr20 1 rel just relative
$!addr20 2 rel "
!addr16 2 abs implies segment 0 (code), or f (data) unless segreg-prefixed
!!addr20 3 abs seg taken from addr
addr5 in op ind only used by callt. dst pc.s is always 0
*/
static ea_t sfr_abs(uint8 rel) {
return 0xfff00 + rel;
}
static ea_t saddr_abs(uint8 rel) {
// this logic doesn't seem documented for rl78, but it's what binutils
// does, and appears similar to 78k0. Renesas tools agree this is correct.
ea_t addr = 0xffe00 + rel;
if (rel < 0x20) {
addr |= 1 << 8;
}
return addr;
}
// #val (constant in opcode)
static void operand_imm_val(insn_t *out, int op_idx, uval_t val) {
auto &op = out->ops[op_idx];
op.type = o_imm;
// not really true...
op.dtype = dt_byte;
op.value = val;
}
// #byte
static void operand_imm8(insn_t *out, int op_idx) {
auto &op = out->ops[op_idx];
op.type = o_imm;
op.dtype = dt_byte;
op.value = out->get_next_byte();
}
// #word
static void operand_imm16(insn_t *out, int op_idx) {
auto &op = out->ops[op_idx];
op.type = o_imm;
op.dtype = dt_word;
op.value = out->get_next_word();
}
// saddr
static void operand_saddr(insn_t *out, int op_idx, op_dtype_t dtype = dt_byte) {
auto &op = out->ops[op_idx];
op.type = o_mem;
op.dtype = dtype;
op.addr = saddr_abs(out->get_next_byte());
}
// saddrp
static void operand_saddrp(insn_t *out, int op_idx) {
operand_saddr(out, op_idx, dt_word);
}
// saddr.bit
static void operand_saddr_bit(insn_t *out, int op_idx, uval_t bit) {
auto &op = out->ops[op_idx];
op.type = o_bit;
op.bit_base_type = o_mem;
op.dtype = dt_byte;
op.addr = saddr_abs(out->get_next_byte());
op.value = bit;
}
// TODO dtype?
// r
static void operand_r(insn_t *out, int op_idx, RL78Register reg) {
auto &op = out->ops[op_idx];
op.type = o_reg;
op.reg = reg;
}
// r.bit
static void operand_r_bit(insn_t *out, int op_idx, RL78Register reg, uval_t bit) {
auto &op = out->ops[op_idx];
op.type = o_bit;
op.bit_base_type = o_reg;
op.reg = reg;
op.value = bit;
}
// [r]
static void operand_r_ind(insn_t *out, int op_idx, RL78Register reg,
op_dtype_t dtype = dt_byte) {
auto &op = out->ops[op_idx];
op.specval = out->auxpref & kHasPrefix;
op.type = o_displ;
op.dtype = dtype;
op.phrase = kReg;
op.ind_reg = reg;
op.addr = 0;
}
// [r] (16bit)
static void operand_r_ind_d16(insn_t *out, int op_idx, RL78Register reg) {
operand_r_ind(out, op_idx, reg, dt_word);
}
// [r].bit
static void operand_r_ind_bit(insn_t *out, int op_idx, RL78Register reg, uval_t bit) {
auto &op = out->ops[op_idx];
op.specval = out->auxpref & kHasPrefix;
op.type = o_bit;
op.bit_base_type = o_reg;
op.addr_mode = kIndirect;
op.reg = reg;
op.value = bit;
}
static bool ea_to_reg(ea_t ea, RL78Register *reg) {
switch (ea) {
case 0xffff8:
*reg = rSP;
return true;
case 0xffffa:
*reg = rPSW;
return true;
case 0xffffc:
*reg = rCS;
return true;
case 0xffffd:
*reg = rES;
return true;
default:
return false;
}
}
static void operand_convert_to_r(insn_t *out, int op_idx) {
auto &op = out->ops[op_idx];
RL78Register reg;
if (!ea_to_reg(op.addr, ®)) {
return;
}
if (op.type == o_bit) {
operand_r_bit(out, op_idx, reg, op.value);
}
else {
operand_r(out, op_idx, reg);
}
}
// sfr
static void operand_sfr(insn_t *out, int op_idx, op_dtype_t dtype = dt_byte) {
auto &op = out->ops[op_idx];
op.type = o_mem;
op.dtype = dtype;
op.addr = sfr_abs(out->get_next_byte());
operand_convert_to_r(out, op_idx);
}
// sfrp
static void operand_sfrp(insn_t *out, int op_idx) {
operand_sfr(out, op_idx, dt_word);
}
// sfr.bit
static void operand_sfr_bit(insn_t *out, int op_idx, uval_t bit) {
auto &op = out->ops[op_idx];
op.type = o_bit;
op.bit_base_type = o_mem;
op.dtype = dt_byte;
op.addr = sfr_abs(out->get_next_byte());
op.value = bit;
operand_convert_to_r(out, op_idx);
}
/* Following ops may have 16bit dtype in forms besides sfrp/saddrp
movw !addr16, [hl+byte], [hl], [de+byte], [de], [sp+byte], word[b], word[c], word[bc]
addw, subw, cmpw !addr16, [hl+byte]
incw, decw !addr16, [hl+byte]
*/
/* prefixable
!addr16
!addr16.bit
[r]
[r].bit
[hl+r]
[r+byte]
word[r]
*/
// $addr20
// 1 byte, rel
// PC <- PC + 2 + jdisp8
// code only
static void operand_addr_rel(insn_t *out, int op_idx) {
auto &op = out->ops[op_idx];
op.type = o_near;
op.offb = (uint8)out->size;
op.addr = out->ip + (int8)out->get_next_byte();
op.addr += out->size;
op.value = op.addr;
}
// $!addr20
// 2 bytes, rel
// PC <- PC + 3 + jdisp16
// code only
static void operand_addr16_rel(insn_t *out, int op_idx) {
auto &op = out->ops[op_idx];
op.type = o_near;
op.offb = (uint8)out->size;
op.addr = out->ip + (int16)out->get_next_word();
op.addr += out->size;
op.value = op.addr;
}
// !addr16 (code)
// 2 bytes, abs
// PC <- 0000, addr16
static void operand_addr16_abs(insn_t *out, int op_idx) {
auto &op = out->ops[op_idx];
// for code, seg bits set to 0
op.type = o_far;
op.offb = (uint8)out->size;
op.addr = out->get_next_word();
op.value = op.addr;
}
// !addr16 (data)
static void operand_addr16_abs_d(insn_t *out, int op_idx,
op_dtype_t dtype = dt_byte) {
auto &op = out->ops[op_idx];
op.specval = out->auxpref & kHasPrefix;
op.type = o_mem;
op.dtype = dtype;
op.offb = (uint8)out->size;
op.addr = out->get_next_word();
if (!(op.specval & kHasPrefix)) {
op.addr += 0xf << 16;
}
op.value = op.addr;
operand_convert_to_r(out, op_idx);
}
// !addr16 (16bit data)
static void operand_addr16_abs_d16(insn_t *out, int op_idx) {
operand_addr16_abs_d(out, op_idx, dt_word);
}
// !addr16.bit
// data only
static void operand_addr16_abs_bit(insn_t *out, int op_idx, uval_t bit) {
auto &op = out->ops[op_idx];
op.specval = out->auxpref & kHasPrefix;
op.type = o_bit;
op.bit_base_type = o_mem;
op.offb = (uint8)out->size;
op.addr = out->get_next_word();
if (!(op.specval & kHasPrefix)) {
op.addr += 0xf << 16;
}
op.value = bit;
operand_convert_to_r(out, op_idx);
}
// !!addr20
// 3 bytes, abs
// PC <- addr20
// code only
static void operand_addr20_abs(insn_t *out, int op_idx) {
auto &op = out->ops[op_idx];
op.type = o_far;
op.offb = (uint8)out->size;
op.addr = out->get_next_word();
op.addr |= ((ea_t)out->get_next_byte()) << 16;
op.value = op.addr;
}
// [hl + r]
static void operand_hl_off_reg(insn_t *out, int op_idx, RL78Register reg) {
auto &op = out->ops[op_idx];
op.specval = out->auxpref & kHasPrefix;
op.type = o_phrase;
op.dtype = dt_byte;
op.phrase = kHlReg;
op.ind_reg = reg;
}
// [r + byte]
static void operand_r_off_imm8(insn_t *out, int op_idx, RL78Register reg,
op_dtype_t dtype = dt_byte) {
auto &op = out->ops[op_idx];
op.specval = out->auxpref & kHasPrefix;
op.type = o_displ;
op.dtype = dtype;
op.phrase = kReg8;
op.ind_reg = reg;
op.addr = out->get_next_byte();
}
// [r + byte] (16bit)
static void operand_r_off_imm8_d16(insn_t *out, int op_idx, RL78Register reg) {
operand_r_off_imm8(out, op_idx, reg, dt_word);
}
// word[r]
static void operand_r_off_imm16(insn_t *out, int op_idx, RL78Register reg,
op_dtype_t dtype = dt_byte) {
auto &op = out->ops[op_idx];
op.specval = out->auxpref & kHasPrefix;
op.type = o_displ;
op.dtype = dtype;
op.phrase = kReg16;
op.ind_reg = reg;
op.addr = out->get_next_word();
}
// word[r] (16bit)
static void operand_r_off_imm16_d16(insn_t *out, int op_idx, RL78Register reg) {
operand_r_off_imm16(out, op_idx, reg, dt_word);
}
static int opcode_31(insn_t *out) {
uint8 code = out->get_next_byte();
uint8 code_lo = code & 0xf;
uint8 code_hi = (code >> 4) & 0xf;
if (code_lo <= 5) {
const uint16 itype_lut[] = {
RL78_btclr, RL78_btclr,
RL78_bt, RL78_bt,
RL78_bf, RL78_bf,
};
out->itype = itype_lut[code_lo];
}
else if ((code_lo >= 7 && code_lo <= 0xb) && (code_hi >= 1 && code_hi <= 7)) {
const uint16 itype_lut[] = {
RL78_shl, RL78_shl, RL78_shl,
RL78_shr,
RL78_sar,
};
out->itype = itype_lut[code_lo - 7];
}
else if (code_lo >= 0xc && code_hi >= 1) {
const uint16 itype_lut[] = {
RL78_shlw, RL78_shlw,
RL78_shrw,
RL78_sarw,
};
out->itype = itype_lut[code_lo - 0xc];
}
else {
return 0;
}
switch (out->itype) {
case RL78_btclr:
case RL78_bt:
case RL78_bf: {
uval_t bit = code_hi & 7;
switch (code & 0x81) {
case 0x00: // bX saddr.bit, $addr20
operand_saddr_bit(out, 0, bit);
break;
case 0x01: // bX A.bit, $addr20
operand_r_bit(out, 0, rA, bit);
break;
case 0x80: // bX sfr.bit, $addr20
operand_sfr_bit(out, 0, bit);
break;
case 0x81: // bX [HL].bit, $addr20
operand_r_ind_bit(out, 0, rHL, bit);
break;
}
operand_addr_rel(out, 1);
} break;
case RL78_shl: { // shl r, cnt
const RL78Register reg_lut[] = { rC, rB, rA };
auto reg = reg_lut[code_lo - 7];
operand_r(out, 0, reg);
operand_imm_val(out, 1, code_hi);
} break;
case RL78_shr: // shr r, cnt
operand_r(out, 0, rA);
operand_imm_val(out, 1, code_hi);
break;
case RL78_sar: // sar A, cnt
operand_r(out, 0, rA);
operand_imm_val(out, 1, code_hi);
break;
case RL78_shlw: { // shlw rp, cnt
const RL78Register reg_lut[] = { rBC, rAX };
auto reg = reg_lut[code_lo - 0xc];
operand_r(out, 0, reg);
operand_imm_val(out, 1, code_hi);
} break;
case RL78_shrw: // shrw rp, cnt
operand_r(out, 0, rAX);
operand_imm_val(out, 1, code_hi);
break;
case RL78_sarw: // sarw rp, cnt
operand_r(out, 0, rAX);
operand_imm_val(out, 1, code_hi);
break;
}
return out->size;
}
static int opcode_61(insn_t *out) {
uint8 code = out->get_next_byte();
uint8 code_lo = code & 0xf;
uint8 code_hi = (code >> 4) & 0xf;
const uint16 itype_lut[] = {
RL78_add, RL78_addc, RL78_sub, RL78_subc, RL78_cmp, RL78_and,
RL78_or, RL78_xor, RL78_xch
};
if (code_hi <= 7 && code_lo <= 7) {
out->itype = itype_lut[code_hi];
operand_r(out, 0, (RL78Register)code_lo);
operand_r(out, 1, rA);
}
else if ((code_hi <= 7 && code_lo == 8) || (code_hi <= 8 && code_lo >= 0xa)) {
out->itype = itype_lut[code_hi];
operand_r(out, 0, rA);
operand_r(out, 1, (RL78Register)(rX + (code_lo & 7)));
}
else if (code_hi >= 8 && (code_lo >= 4 && code_lo <= 7)) {
// TODO callt hasn't been seen, so not bothered making it nice
out->itype = RL78_callt;
// addr5_abs
uint16 addr = 0x80 | ((code_lo & 3) << 4) | ((code_hi & 7) << 1);
auto &op = out->ops[0];
op.type = o_mem;
op.value = addr;
op.addr = addr;
op.dtype = dt_word;
}
else {
switch (code) {
case 0x09: // addw ax, [hl+byte]
out->itype = RL78_addw;
operand_r(out, 0, rAX);
operand_r_off_imm8_d16(out, 1, rHL);
break;
case 0x29: // subw ax, [hl+byte]
out->itype = RL78_subw;
operand_r(out, 0, rAX);
operand_r_off_imm8_d16(out, 1, rHL);
break;
case 0x49: // cmpw ax, [hl+byte]
out->itype = RL78_cmpw;
operand_r(out, 0, rAX);
operand_r_off_imm8_d16(out, 1, rHL);
break;
case 0x59: // inc [hl+byte]
out->itype = RL78_inc;
operand_r_off_imm8(out, 0, rHL);
break;
case 0x69: // dec [hl+byte]
out->itype = RL78_dec;
operand_r_off_imm8(out, 0, rHL);
break;
case 0x79: // incw [hl+byte]
out->itype = RL78_incw;
operand_r_off_imm8_d16(out, 0, rHL);
break;
case 0x89: // decw [hl+byte]
out->itype = RL78_decw;
operand_r_off_imm8_d16(out, 0, rHL);
break;
case 0x80:
case 0x90:
case 0xa0:
case 0xb0:
case 0xc0:
case 0xd0:
case 0xe0:
case 0xf0:
out->itype = itype_lut[code_hi & 7];
operand_r(out, 0, rA);
operand_hl_off_reg(out, 1, rB);
break;
case 0x82:
case 0x92:
case 0xa2:
case 0xb2:
case 0xc2:
case 0xd2:
case 0xe2:
case 0xf2:
out->itype = itype_lut[code_hi & 7];
operand_r(out, 0, rA);
operand_hl_off_reg(out, 1, rC);
break;
case 0xc3:
out->itype = RL78_bh;
operand_addr_rel(out, 0);
break;
case 0xd3:
out->itype = RL78_bnh;
operand_addr_rel(out, 0);
break;
case 0xe3:
out->itype = RL78_skh;
break;
case 0xf3:
out->itype = RL78_sknh;
break;
case 0xa8:
out->itype = RL78_xch;
operand_r(out, 0, rA);
operand_saddr(out, 1);
break;
case 0xb8:
out->itype = RL78_mov;
operand_r(out, 0, rES);
operand_saddr(out, 1);
break;
case 0xa9:
out->itype = RL78_xch;
operand_r(out, 0, rA);
operand_hl_off_reg(out, 1, rC);
break;
case 0xb9:
out->itype = RL78_xch;
operand_r(out, 0, rA);
operand_hl_off_reg(out, 1, rB);
break;
case 0xaa:
out->itype = RL78_xch;
operand_r(out, 0, rA);
operand_addr16_abs_d(out, 1);
break;
case 0xab:
out->itype = RL78_xch;
operand_r(out, 0, rA);
operand_sfr(out, 1);
break;
case 0xac: // xch a, [hl]
out->itype = RL78_xch;
operand_r(out, 0, rA);
operand_r_ind(out, 1, rHL);
break;
case 0xad:
out->itype = RL78_xch;
operand_r(out, 0, rA);
operand_r_off_imm8(out, 1, rHL);
break;
case 0xae: // xch a, [de]
out->itype = RL78_xch;
operand_r(out, 0, rA);
operand_r_ind(out, 1, rDE);
break;
case 0xaf:
out->itype = RL78_xch;
operand_r(out, 0, rA);
operand_r_off_imm8(out, 1, rDE);
break;
case 0xc8:
out->itype = RL78_skc;
break;
case 0xd8:
out->itype = RL78_sknc;
break;
case 0xe8:
out->itype = RL78_skz;
break;
case 0xf8:
out->itype = RL78_sknz;
break;
case 0xc9:
out->itype = RL78_mov;
operand_r(out, 0, rA);
operand_hl_off_reg(out, 1, rB);
break;
case 0xd9:
out->itype = RL78_mov;
operand_hl_off_reg(out, 0, rB);
operand_r(out, 1, rA);
break;
case 0xe9:
out->itype = RL78_mov;
operand_r(out, 0, rA);
operand_hl_off_reg(out, 1, rC);
break;
case 0xf9:
out->itype = RL78_mov;
operand_hl_off_reg(out, 0, rC);
operand_r(out, 1, rA);
break;
case 0xca:
case 0xda:
case 0xea:
case 0xfa:
out->itype = RL78_call;
operand_r(out, 0, (RL78Register)(rAX + (code_hi & 3)));
break;
case 0xcb:
out->itype = RL78_br;
operand_r(out, 0, rAX);
break;
case 0xdb:
out->itype = RL78_ror;
operand_r(out, 0, rA);
operand_imm_val(out, 1, 1);
break;
case 0xeb:
out->itype = RL78_rol;
operand_r(out, 0, rA);
operand_imm_val(out, 1, 1);
break;
case 0xfb:
out->itype = RL78_rorc;
operand_r(out, 0, rA);
operand_imm_val(out, 1, 1);
break;
case 0xcc:
out->itype = RL78_brk;
break;
case 0xdc:
out->itype = RL78_rolc;
operand_r(out, 0, rA);
operand_imm_val(out, 1, 1);
break;
case 0xec:
out->itype = RL78_retb;
break;
case 0xfc:
out->itype = RL78_reti;
break;
case 0xcd:
out->itype = RL78_pop;
operand_r(out, 0, rPSW);
break;
case 0xdd:
out->itype = RL78_push;
operand_r(out, 0, rPSW);
break;
case 0xed:
out->itype = RL78_halt;
break;
case 0xfd:
out->itype = RL78_stop;
break;
case 0xce:
out->itype = RL78_movs;
operand_r_off_imm8(out, 0, rHL);
operand_r(out, 1, rX);
break;
case 0xde:
out->itype = RL78_cmps;
operand_r(out, 0, rX);
operand_r_off_imm8(out, 1, rHL);
break;
case 0xee:
out->itype = RL78_rolwc;
operand_r(out, 0, rAX);
operand_imm_val(out, 1, 1);
break;
case 0xfe:
out->itype = RL78_rolwc;
operand_r(out, 0, rBC);
operand_imm_val(out, 1, 1);
break;
case 0xcf:
case 0xdf:
case 0xef:
case 0xff:
out->itype = RL78_sel;
operand_r(out, 0, (RL78Register)(rRB0 + (code_hi & 3)));
break;
default:
return 0;
}
}
return out->size;
}
static int opcode_71(insn_t *out) {
uint8 code = out->get_next_byte();
uint8 code_lo = code & 0xf;