Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
; Ubicom IP2K CPU description.  -*- Scheme -*-
; Copyright (C) 2002, 2009, 2011 Free Software Foundation, Inc.
;
; Contributed by Red Hat Inc;
;
; This file is part of the GNU Binutils.
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 3 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
; MA 02110-1301, USA.

(define-rtl-version 0 8)

(include "simplify.inc")

; define-arch must appear first

(define-arch
  (name ip2k) ; name of cpu family
  (comment "Ubicom IP2000 family")
  (default-alignment aligned)
  (insn-lsb0? #t)
  (machs ip2022 ip2022ext)
  (isas ip2k)
)

; Attributes.

(define-attr
  (for insn)
  (type boolean)
  (name EXT-SKIP-INSN)
  (comment "instruction is a PAGE, LOADL, LOADH or BREAKX instruction")
)

(define-attr
  (for insn)
  (type boolean)
  (name SKIPA)
  (comment "instruction is a SKIP instruction")
)

; Instruction set parameters.

(define-isa
  (name ip2k)
  (comment "Ubicom IP2000 ISA")

  (default-insn-word-bitsize 16)
  (default-insn-bitsize 16)
  (base-insn-bitsize 16)
)

; Cpu family definitions.


(define-cpu
  ; cpu names must be distinct from the architecture name and machine names.
  (name ip2kbf)
  (comment "Ubicom IP2000 Family")
  (endian big)
  (word-bitsize 16)
)

(define-mach
  (name ip2022)
  (comment "Ubicom IP2022")
  (cpu ip2kbf)
)

(define-mach
  (name ip2022ext)
  (comment "Ubicom IP2022 extended")
  (cpu ip2kbf)
)


; Model descriptions.

(define-model
  (name ip2k) (comment "VPE 2xxx") (attrs)
  (mach ip2022ext)

  (unit u-exec "Execution Unit" ()
	1 1 ; issue done
	() ; state
	() ; inputs
	() ; outputs
	() ; profile action (default)
	)
)


; FIXME: It might simplify things to separate the execute process from the
; one that updates the PC.

; Instruction fields.
;
; Attributes:
; XXX: what VPE attrs
; PCREL-ADDR: pc relative value (for reloc and disassembly purposes)
; ABS-ADDR: absolute address (for reloc and disassembly purposes?)
; RESERVED: bits are not used to decode insn, must be all 0
; RELOC: there is a relocation associated with this field (experiment)


(dnf f-imm8      "imm8"                () 7 8)
(dnf f-reg       "reg"         (ABS-ADDR) 8 9)
(dnf f-addr16cjp "addr16cjp"   (ABS-ADDR) 12 13)
(dnf f-dir       "dir"                 () 9 1)
(dnf f-bitno     "bit number"          () 11 3)
(dnf f-op3       "op3"                 () 15 3)
(dnf f-op4       "op4"                 () 15 4)
(dnf f-op4mid    "op4mid"              () 11 4)
(dnf f-op6       "op6"                 () 15 6)
(dnf f-op8       "op8"                 () 15 8)
(dnf f-op6-10low "op6-10low"           () 9 10)
(dnf f-op6-7low  "op6-7low"            () 9 7)
(dnf f-reti3     "reti3"               () 2 3)
(dnf f-skipb     "sb/snb"      (ABS-ADDR) 12 1)
(dnf f-page3     "page3"               ()  2 3)
;(define-ifield (name f-page3) (comment "page3") (attrs) (start 2) (length 3)
;  (encode (value pc) (srl WI value 13))
;  (decode (value pc) (sll WI value 13))
;)
; To fix the page/call asymmetry
;(define-ifield (name f-page3) (comment "page3") (attrs) (start 2) (length 3)
;  (encode (value pc) (srl WI value 13))
;  (decode (value pc) (sll WI value 13))
;)



; Enums.

; insn-op6: bits 15-10
(define-normal-insn-enum insn-op6 "op6 enums" () OP6_ f-op6
  (OTHER1 OTHER2 SUB DEC OR AND XOR ADD 
   TEST NOT INC DECSZ RR RL SWAP INCSZ
   CSE POP SUBC DECSNZ MULU MULS INCSNZ  ADDC
   - - - - - - - -   
   - - - - - - - -
   - - - - - - - -
   - - - - - - - -
   - - - - - - - -   
   )
)

; insn-dir: bit 9
(define-normal-insn-enum insn-dir "dir enums" () DIR_ f-dir
  ; This bit specifies the polarity of many two-operand instructions:
  ; TO_W writes result to W regiser  (eg. ADDC W,$fr)
  ; NOTTO_W writes result in general register  (eg. ADDC $fr,W)
  (TO_W NOTTO_W)
)


; insn-op4: bits 15-12
(define-normal-insn-enum insn-op4 "op4 enums" () OP4_ f-op4
  (- - - - - - - LITERAL
   CLRB SETB SNB SB - - - -
   )
)

; insn-op4mid: bits 11-8
; used for f-op4=LITERAL
(define-normal-insn-enum insn-op4mid "op4mid enums" () OP4MID_ f-op4mid
  (LOADH_L LOADL_L MULU_L MULS_L PUSH_L  -  CSNE_L CSE_L
   RETW_L CMP_L SUB_L ADD_L MOV_L OR_L AND_L XOR_L)
)

; insn-op3: bits 15-13
(define-normal-insn-enum insn-op3 "op3 enums" () OP3_ f-op3
  (- - - - - - CALL JMP)
)


  
; Hardware pieces.

; Bank-relative general purpose registers

; (define-pmacro (build-reg-name n) (.splice (.str "$" n) n))

(define-keyword
  (name register-names)
  (enum-prefix H-REGISTERS-)
  (values
   ; These are the "Special Purpose Registers" that are not reserved
   ("ADDRSEL" #x2) ("ADDRX" #x3)
   ("IPH" #x4) ("IPL" #x5) ("SPH" #x6) ("SPL" #x7)
   ("PCH" #x8) ("PCL" #x9) ("WREG" #xA) ("STATUS" #xB)
   ("DPH" #xC) ("DPL" #xD) ("SPDREG" #xE) ("MULH" #xF)
   ("ADDRH" #x10) ("ADDRL" #x11) ("DATAH" #x12) ("DATAL" #x13)
   ("INTVECH" #x14) ("INTVECL" #x15) ("INTSPD" #x16) ("INTF" #x17)
   ("INTE" #x18) ("INTED" #x19) ("FCFG" #x1A) ("TCTRL" #x1B)
   ("XCFG" #x1C) ("EMCFG" #x1D) ("IPCH" #x1E) ("IPCL" #x1F)
   ("RAIN" #x20) ("RAOUT" #x21) ("RADIR" #x22) ("LFSRH" #x23)
   ("RBIN" #x24) ("RBOUT" #x25) ("RBDIR" #x26) ("LFSRL" #x27)
   ("RCIN" #x28) ("RCOUT" #x29) ("RCDIR" #x2A) ("LFSRA" #x2B)
   ("RDIN" #x2C) ("RDOUT" #x2D) ("RDDIR" #x2E)   
   ("REIN" #x30) ("REOUT" #x31) ("REDIR" #x32)   
   ("RFIN" #x34) ("RFOUT" #x35) ("RFDIR" #x36)
                 ("RGOUT" #x39) ("RGDIR" #x3A)
   ("RTTMR" #x40) ("RTCFG" #x41) ("T0TMR" #x42) ("T0CFG" #x43)
   ("T1CNTH" #x44) ("T1CNTL" #x45) ("T1CAP1H" #x46) ("T1CAP1L" #x47)
   ("T1CAP2H" #x48) ("T1CMP2H" #x48) ("T1CAP2L" #x49) ("T1CMP2L" #x49) ; note aliases
                                     ("T1CMP1H" #x4A) ("T1CMP1L" #x4B)
   ("T1CFG1H" #x4C) ("T1CFG1L" #x4D) ("T1CFG2H" #x4E) ("T1CFG2L" #x4F)
   ("ADCH" #x50) ("ADCL" #x51) ("ADCCFG" #x52) ("ADCTMR" #x53)
   ("T2CNTH" #x54) ("T2CNTL" #x55) ("T2CAP1H" #x56) ("T2CAP1L" #x57)
   ("T2CAP2H" #x58) ("T2CMP2H" #x58) ("T2CAP2L" #x59) ("T2CMP2L" #x59) ; note aliases
                                     ("T2CMP1H" #x5A) ("T2CMP1L" #x5B)
   ("T2CFG1H" #x5C) ("T2CFG1L" #x5D) ("T2CFG2H" #x5E) ("T2CFG2L" #x5F)
   ("S1TMRH" #x60) ("S1TMRL" #x61) ("S1TBUFH" #x62) ("S1TBUFL" #x63)
   ("S1TCFG" #x64) ("S1RCNT" #x65) ("S1RBUFH" #x66) ("S1RBUFL" #x67)
   ("S1RCFG" #x68) ("S1RSYNC" #x69) ("S1INTF" #x6A) ("S1INTE" #x6B)
   ("S1MODE" #x6C) ("S1SMASK" #x6D) ("PSPCFG" #x6E) ("CMPCFG" #x6F)
   ("S2TMRH" #x70) ("S2TMRL" #x71) ("S2TBUFH" #x72) ("S2TBUFL" #x73)
   ("S2TCFG" #x74) ("S2RCNT" #x75) ("S2RBUFH" #x76) ("S2RBUFL" #x77)
   ("S2RCFG" #x78) ("S2RSYNC" #x79) ("S2INTF" #x7A) ("S2INTE" #x7B)
   ("S2MODE" #x7C) ("S2SMASK" #x7D) ("CALLH" #x7E) ("CALLL" #x7F))
  )

(define-hardware
  (name h-spr)
  (comment "special-purpose registers")
  (type register QI (128))
  (get (index) (c-call QI "get_spr" index ))
  (set (index newval) (c-call VOID "set_spr" index newval ))
)


;;(define-hardware
;;  (name h-gpr-global)
;;  (comment "gpr registers - global")
;;  (type register QI (128))
;;)

; The general register

(define-hardware
  (name h-registers)
  (comment "all addressable registers")
  (attrs VIRTUAL)
  (type register QI (512))
  (get (index) (c-call QI "get_h_registers" index ))
  (set (index newval) (c-call VOID "set_h_registers" index newval ))
)

; The hardware stack.
; Use {push,pop}_pc_stack c-calls to operate on this hardware element.

(define-hardware
  (name h-stack)
  (comment "hardware stack")
  (type register UHI (16))
)

(dsh h-pabits "page bits" () (register QI))
(dsh h-zbit "zero bit" () (register BI))
(dsh h-cbit "carry bit" () (register BI))
(dsh h-dcbit "digit-carry bit" () (register BI))
(dnh h-pc "program counter" (PC PROFILE) (pc) () () ())


; Operands

(define-operand (name addr16cjp) (comment "13-bit address") (attrs) 
  (type h-uint) (index f-addr16cjp) (handlers (parse "addr16_cjp") (print "dollarhex_cj"))) ; overload lit8 printer
(define-operand (name fr) (comment "register") (attrs) 
  (type h-registers) (index f-reg) (handlers (parse "fr") (print "fr")))
(define-operand (name lit8) (comment "8-bit signed literal") (attrs)
  (type h-sint) (index f-imm8) (handlers (parse "lit8") (print "dollarhex8")))
(define-operand (name bitno) (comment "bit number") (attrs)
  (type h-uint) (index f-bitno) (handlers (parse "bit3")(print "decimal")))
(define-operand (name addr16p) (comment "page number") (attrs)
  (type h-uint) (index f-page3) (handlers (parse "addr16_cjp") (print "dollarhex_p")))
(define-operand (name addr16h) (comment "high 8 bits of address") (attrs)
  (type h-uint) (index f-imm8) (handlers (parse "addr16") (print "dollarhex_addr16h")))
(define-operand (name addr16l) (comment "low 8 bits of address") (attrs)
  (type h-uint) (index f-imm8) (handlers (parse "addr16") (print "dollarhex_addr16l")))
(define-operand (name reti3) (comment "reti flags") (attrs)
  (type h-uint) (index f-reti3) (handlers (print "dollarhex")))
(dnop pabits   "page bits"                 () h-pabits f-nil)
(dnop zbit     "zero bit"                  () h-zbit f-nil)
(dnop cbit     "carry bit"                 () h-cbit f-nil)
(dnop dcbit    "digit carry bit"           () h-dcbit f-nil)
;;(dnop bank     "bank register"             () h-bank-no f-nil)

(define-pmacro w     (reg h-spr #x0A))
(define-pmacro mulh  (reg h-spr #x0F))
(define-pmacro dph   (reg h-spr #x0C))
(define-pmacro dpl   (reg h-spr #x0D))
(define-pmacro sph   (reg h-spr #x06))
(define-pmacro spl   (reg h-spr #x07))
(define-pmacro iph   (reg h-spr #x04))
(define-pmacro ipl   (reg h-spr #x05))
(define-pmacro addrh (reg h-spr #x10))
(define-pmacro addrl (reg h-spr #x11))



; Pseudo-RTL for DC flag calculations
; "DC" = "digit carry", ie carry between nibbles
(define-pmacro (add-dcflag a b c)
  (add-cflag (sll QI a 4) (sll QI b 4) c)
)

(define-pmacro (sub-dcflag a b c)
  (sub-cflag (sll QI a 4) (sll QI b 4) c)
)

; Check to see if an fr is one of IPL, SPL, DPL, ADDRL, PCL.
(define-pmacro (LregCheck isLreg fr9bit)
   (sequence()
      (set isLreg #x0) ;; Assume it's not an Lreg
      (if (or (or (eq fr9bit #x5) (eq fr9bit #x7))
	      (or (eq fr9bit #x9)
		  (or (eq fr9bit #xd) (eq fr9bit #x11))))
          (set isLreg #x1)
      )
   )
) 


; Instructions, in order of the "Instruction Set Map" table on
; pp 19-20 of IP2022 spec V1.09

(dni jmp "Jump"
     ()
     "jmp $addr16cjp"
     (+ OP3_JMP addr16cjp)
     (set pc (or (sll pabits 13) addr16cjp))
     ()
)

; note that in call, we push pc instead of pc + 1 because the ip2k increments
; the pc prior to execution of the instruction
(dni call "Call"
     ()
     "call $addr16cjp"
     (+ OP3_CALL addr16cjp)
     (sequence ()
	       (c-call "push_pc_stack" pc)
	       (set pc (or (sll pabits 13) addr16cjp)))
     ()
)

(dni sb "Skip if bit set"
     ()
     "sb $fr,$bitno"
     (+ OP4_SB bitno fr)
     (if (and fr (sll 1 bitno))
	 (skip 1))
     ()
)

(dni snb "Skip if bit clear"
     ()
     "snb $fr,$bitno"
     (+ OP4_SNB bitno fr)
     (if (not (and fr (sll 1 bitno)))
	 (skip 1))
     ()
)

(dni setb "Set bit"
     ()
     "setb $fr,$bitno"
     (+ OP4_SETB bitno fr)
     (set fr (or fr (sll 1 bitno)))
     ()
)

(dni clrb "Clear bit"
     ()
     "clrb $fr,$bitno"
     (+ OP4_CLRB bitno fr)
     (set fr (and fr (inv (sll 1 bitno))))
     ()
)

(dni xorw_l "XOR W,literal"
     ()
     "xor W,#$lit8"
     (+ OP4_LITERAL OP4MID_XOR_L lit8)
     (sequence ()
	       (set w (xor w lit8))
	       (set zbit (zflag w)))
     ()
)

(dni andw_l "AND W,literal"
     ()
     "and W,#$lit8"
     (+ OP4_LITERAL OP4MID_AND_L lit8)
     (sequence ()
	       (set w (and w lit8))
	       (set zbit (zflag w)))
     ()
)

(dni orw_l "OR W,literal"
     ()
     "or W,#$lit8"
     (+ OP4_LITERAL OP4MID_OR_L lit8)
     (sequence ()
	       (set w (or w lit8))
	       (set zbit (zflag w)))
     ()
)

(dni addw_l "ADD W,literal"
     ()
     "add W,#$lit8"
     (+ OP4_LITERAL OP4MID_ADD_L lit8)
     (sequence ()
	       (set cbit (add-cflag w lit8 0))
	       (set dcbit (add-dcflag w lit8 0))
	       (set w (add w lit8))
	       (set zbit (zflag w)))
     ()
)

(dni subw_l "SUB W,literal"
     ()
     "sub W,#$lit8"
     (+ OP4_LITERAL OP4MID_SUB_L lit8)
     (sequence ()
	       (set cbit (not (sub-cflag lit8 w 0)))
	       (set dcbit (not (sub-dcflag lit8 w 0)))
	       (set zbit (zflag (sub w lit8)))
	       (set w (sub lit8 w)))
     ()
)

(dni cmpw_l "CMP W,literal"
     ()
     "cmp W,#$lit8"
     (+ OP4_LITERAL OP4MID_CMP_L lit8)
     (sequence ()
	       (set cbit (not (sub-cflag lit8 w 0)))
	       (set dcbit (not (sub-dcflag lit8 w 0)))
	       (set zbit (zflag (sub w lit8))))
     ()
)

(dni retw_l "RETW literal"
     ()
     "retw #$lit8"
     (+ OP4_LITERAL OP4MID_RETW_L lit8)
     (sequence ((USI new_pc))
	       (set w lit8)
	       (set new_pc (c-call UHI "pop_pc_stack"))
	       (set pabits (srl new_pc 13))
	       (set pc new_pc))
     ()
)

(dni csew_l "CSE W,literal"
     ()
     "cse W,#$lit8"
     (+ OP4_LITERAL OP4MID_CSE_L lit8)
     (if (eq w lit8)
	 (skip 1))
     ()
)

(dni csnew_l "CSNE W,literal"
     ()
     "csne W,#$lit8"
     (+ OP4_LITERAL OP4MID_CSNE_L lit8)
     (if (not (eq w lit8))
	 (skip 1))
     ()
)

(dni push_l "Push #lit8"
     ()
     "push #$lit8"
     (+ OP4_LITERAL OP4MID_PUSH_L lit8)
     (sequence ()
        (c-call "push" lit8)
        (c-call VOID "adjuststackptr" (const -1))

     )
     ()
)

(dni mulsw_l "Multiply W,literal (signed)"
     ()
     "muls W,#$lit8"
     (+ OP4_LITERAL OP4MID_MULS_L lit8)
     (sequence ((SI tmp))
	       (set tmp (mul (ext SI w) (ext SI (and UQI #xff lit8))))
	       (set w (and tmp #xFF))
	       (set mulh (srl tmp 8)))
     ()
)

(dni muluw_l "Multiply W,literal (unsigned)"
     ()
     "mulu W,#$lit8"
     (+ OP4_LITERAL OP4MID_MULU_L lit8)
     (sequence ((USI tmp))
	       (set tmp (and #xFFFF (mul (zext USI w) (zext USI lit8))))
	       (set w (and tmp #xFF))
	       (set mulh (srl tmp 8)))
     ()
)

(dni loadl_l "LoadL literal"
    (EXT-SKIP-INSN)
    "loadl #$lit8"
    (+ OP4_LITERAL OP4MID_LOADL_L lit8)
    (set dpl (and lit8 #x00FF))
    ()
)

(dni loadh_l "LoadH literal"
    (EXT-SKIP-INSN)
    "loadh #$lit8"
    (+ OP4_LITERAL OP4MID_LOADH_L lit8)
    (set dph (and lit8 #x00FF))
    ()
)

(dni loadl_a "LoadL addr16l"
    (EXT-SKIP-INSN)
    "loadl $addr16l"
    (+ OP4_LITERAL OP4MID_LOADL_L addr16l)
    (set dpl (and addr16l #x00FF))
    ()
)

(dni loadh_a "LoadH addr16h"
    (EXT-SKIP-INSN)
    "loadh $addr16h"
    (+ OP4_LITERAL OP4MID_LOADH_L addr16h)
    (set dph (and addr16l #x0FF00))
    ()
)

;; THIS NO LONGER EXISTS -> Now LOADL
;;(dni bank_l "Bank literal"
;;     ()
;;     "bank #$lit8"
;;     (+ OP4_LITERAL OP4MID_BANK_L lit8)
;;     (set bank lit8)
;;     ()
;;)

(dni addcfr_w "Add w/carry fr,W"
     ()
     "addc $fr,W"
     (+ OP6_ADDC DIR_NOTTO_W fr)
     (sequence ((QI result) (BI newcbit) (QI isLreg) (HI 16bval))
	       (set newcbit (add-cflag w fr cbit))
	       (set dcbit (add-dcflag w fr cbit))
               ;; If fr is an Lreg, then we have to do 16-bit arithmetic.
               ;; We can take advantage of the fact that by a lucky
               ;; coincidence, the address of register xxxH is always      
               ;; one lower than the address of register xxxL.
               (LregCheck isLreg (ifield f-reg))
	       (if (eq isLreg #x1)
                  (sequence() 
                     (set 16bval (reg h-spr (sub (ifield f-reg) 1)))
		     (set 16bval (sll 16bval 8))
		     (set 16bval (or 16bval (and (reg h-spr (ifield f-reg)) #xFF))) 
                     (set 16bval (addc HI 16bval w cbit))
		     (set (reg h-spr (ifield f-reg)) (and 16bval #xFF))
		     (set (reg h-spr (sub (ifield f-reg) 1)) 
                          (and (srl 16bval 8) #xFF))
                     (set result (reg h-spr (ifield f-reg)))
                  )      
	       (set result (addc w fr cbit)) ;; else part
               )

	       (set zbit (zflag result))
	       (set cbit newcbit)
	       (set fr result))
     ()
)

(dni addcw_fr "Add w/carry W,fr"
     ()
     "addc W,$fr"
     (+ OP6_ADDC DIR_TO_W fr)
     (sequence ((QI result) (BI newcbit))
	       (set newcbit (add-cflag w fr cbit))
	       (set dcbit (add-dcflag w fr cbit))
	       (set result (addc w fr cbit))
	       (set zbit (zflag result))
	       (set cbit newcbit)
	       (set w result))
     ()
)


(dni incsnz_fr "Skip if fr++ not zero"
     ()
     "incsnz $fr"
     (+ OP6_INCSNZ DIR_NOTTO_W fr)
     (sequence ((QI isLreg) (HI 16bval))
        (LregCheck isLreg (ifield f-reg))
        ;; If fr is an Lreg, then we have to do 16-bit arithmetic.
        ;; We can take advantage of the fact that by a lucky
        ;; coincidence, the address of register xxxH is always
        ;; one lower than the address of register xxxL.
        (if (eq isLreg #x1)
           (sequence()
              ; Create the 16 bit value
              (set 16bval (reg h-spr (sub (ifield f-reg) 1)))
              (set 16bval (sll 16bval 8))
              (set 16bval (or 16bval (and (reg h-spr (ifield f-reg)) #xFF)))
              ; Do 16 bit arithmetic.
	      (set 16bval (add HI 16bval 1))
              ; Separate the 16 bit values into the H and L regs
              (set (reg h-spr (ifield f-reg)) (and 16bval #xFF))
              (set (reg h-spr (sub (ifield f-reg) 1))
                   (and (srl 16bval 8) #xFF))
              (set fr (reg h-spr (ifield f-reg)))
           )
	   (set fr (add fr 1)) ; Do 8 bit arithmetic.
        )
	(if (not (zflag fr))
	   (skip 1)))
     ()
)

(dni incsnzw_fr "Skip if W=fr+1  not zero"
     ()
     "incsnz W,$fr"
     (+ OP6_INCSNZ DIR_TO_W fr)
     (sequence ()
	       (set w (add fr 1))
	       (if (not (zflag w))
		   (skip 1)))
     ()
)

(dni mulsw_fr "Multiply W,fr (signed)"
     ()
     "muls W,$fr"
     (+ OP6_MULS DIR_TO_W fr)
     (sequence ((SI tmp))
	       (set tmp (mul (ext SI w) (ext SI fr)))
	       (set w (and tmp #xFF))
	       (set mulh (srl tmp 8)))
     ()
)

(dni muluw_fr "Multiply W,fr (unsigned)"
     ()
     "mulu W,$fr"
     (+ OP6_MULU DIR_TO_W fr)
     (sequence ((USI tmp))
	       (set tmp (and #xFFFF (mul (zext USI w) (zext USI fr))))
	       (set w (and tmp #xFF))
	       (set mulh (srl tmp 8)))
     ()
)

(dni decsnz_fr "Skip if fr-- not zero"
     ()
     "decsnz $fr"
     (+ OP6_DECSNZ DIR_NOTTO_W fr)
     (sequence ((QI isLreg) (HI 16bval))
         (LregCheck isLreg (ifield f-reg))
         ;; If fr is an Lreg, then we have to do 16-bit arithmetic.
         ;; We can take advantage of the fact that by a lucky
         ;; coincidence, the address of register xxxH is always
         ;; one lower than the address of register xxxL.
         (if (eq isLreg #x1)
            (sequence()
               ; Create the 16 bit value
               (set 16bval (reg h-spr (sub (ifield f-reg) 1)))
               (set 16bval (sll 16bval 8))
               (set 16bval (or 16bval (and (reg h-spr (ifield f-reg)) #xFF)))
               ; New 16 bit instruction
               (set 16bval (sub HI 16bval 1))
               ; Separate the 16 bit values into the H and L regs
               (set (reg h-spr (ifield f-reg)) (and 16bval #xFF))
               (set (reg h-spr (sub (ifield f-reg) 1))
                    (and (srl 16bval 8) #xFF))
               (set fr (reg h-spr (ifield f-reg)))
            )
            ; Original instruction
	    (set fr (sub fr 1))
         )
	    (if (not (zflag fr))
	       (skip 1)))
     ()
)

(dni decsnzw_fr "Skip if W=fr-1 not zero"
     ()
     "decsnz W,$fr"
     (+ OP6_DECSNZ DIR_TO_W fr)
     (sequence ()
	       (set w (sub fr 1))
	       (if (not (zflag w))
		   (skip 1)))
     ()
)

(dni subcw_fr "Subract w/carry W,fr"
     ()
     "subc W,$fr"
     (+ OP6_SUBC DIR_TO_W fr)
     (sequence ((QI result) (BI newcbit))
	       (set newcbit (not (sub-cflag fr w (not cbit))))
	       (set dcbit (not (sub-dcflag fr w (not cbit))))
	       (set result (subc fr w (not cbit)))
	       (set zbit (zflag result))
	       (set cbit newcbit)
	       (set w result))
     ()
)

(dni subcfr_w "Subtract w/carry fr,W"
     ()
     "subc $fr,W"
     (+ OP6_SUBC DIR_NOTTO_W fr)
     (sequence ((QI result) (BI newcbit) (QI isLreg) (HI 16bval))
	       (set newcbit (not (sub-cflag fr w (not cbit))))
	       (set dcbit (not (sub-dcflag fr w (not cbit))))
               (LregCheck isLreg (ifield f-reg))
               ;; If fr is an Lreg, then we have to do 16-bit arithmetic.
               ;; We can take advantage of the fact that by a lucky
               ;; coincidence, the address of register xxxH is always
               ;; one lower than the address of register xxxL.
               (if (eq isLreg #x1)
                  (sequence()
                     ; Create the 16 bit value
                     (set 16bval (reg h-spr (sub (ifield f-reg) 1)))
                     (set 16bval (sll 16bval 8))
                     (set 16bval (or 16bval (and (reg h-spr (ifield f-reg)) #xFF)))
                     ; New 16 bit instruction
	             (set 16bval (subc HI 16bval w (not cbit)))
                     ; Separate the 16 bit values into the H and L regs
                     (set (reg h-spr (ifield f-reg)) (and 16bval #xFF))
                     (set (reg h-spr (sub (ifield f-reg) 1))
                          (and (srl 16bval 8) #xFF))
                     (set result (reg h-spr (ifield f-reg)))
                  )
               ; Original instruction
	       (set result (subc fr w (not cbit)))
               )


	       (set zbit (zflag result))
	       (set cbit newcbit)
	       (set fr result))
     ()
)


(dni pop_fr "Pop fr"
     ()
     "pop $fr"
     (+ OP6_POP (f-dir 1) fr)
     (sequence()
        (set fr (c-call QI "pop")) 
        (c-call VOID "adjuststackptr" (const 1))
     )
     ()
)

(dni push_fr "Push fr"
     ()
     "push $fr"
     (+ OP6_POP (f-dir 0) fr)
     (sequence()
        (c-call "push" fr)
        (c-call VOID "adjuststackptr" (const -1))
     )
     ()
)

(dni csew_fr "Skip if equal W,fr"
     ()
     "cse W,$fr"
     (+ OP6_CSE (f-dir 1) fr)
     (if (eq w fr)
	 (skip 1))
     ()
)

(dni csnew_fr "Skip if not-equal W,fr"
     ()
     "csne W,$fr"
     (+ OP6_CSE (f-dir 0) fr)
     (if (not (eq w fr))
	 (skip 1))
     ()
)

;;(dni csaw_fr "Skip if W above fr"
;;     ((MACH ip2022ext))
;;     "csa W,$fr"
;;     (+ OP6_CSAB (f-dir 1) fr)
;;     (if (gt w fr)
;;	 (skip 1))
;;    ()
;;)

;;(dni csbw_fr "Skip if W below fr"
;;     ((MACH ip2022ext))
;;     "csb W,$fr"
;;     (+ OP6_CSAB (f-dir 0) fr)
;;     (if (lt w fr)
;;	 (skip 1))
;;    ()
;;)

(dni incsz_fr "Skip if fr++ zero"
     ()
     "incsz $fr"
     (+ OP6_INCSZ DIR_NOTTO_W fr)
     (sequence ((QI isLreg) (HI 16bval))
          (LregCheck isLreg (ifield f-reg))
          ;; If fr is an Lreg, then we have to do 16-bit arithmetic.
          ;; We can take advantage of the fact that by a lucky
          ;; coincidence, the address of register xxxH is always
          ;; one lower than the address of register xxxL.
          (if (eq isLreg #x1)
             (sequence()
                ; Create the 16 bit value
                (set 16bval (reg h-spr (sub (ifield f-reg) 1)))
                (set 16bval (sll 16bval 8))
                (set 16bval (or 16bval (and (reg h-spr (ifield f-reg)) #xFF)))
                ; New 16 bit instruction
                (set 16bval (add HI 16bval 1))
                ; Separate the 16 bit values into the H and L regs
                (set (reg h-spr (ifield f-reg)) (and 16bval #xFF))
                (set (reg h-spr (sub (ifield f-reg) 1))
                     (and (srl 16bval 8) #xFF))
                (set fr (reg h-spr (ifield f-reg)))
             )
             ; Original instruction
	     (set fr (add fr 1))
          )
	       (if (zflag fr)
		   (skip 1)))
     ()
)

(dni incszw_fr "Skip if W=fr+1 zero"
     ()
     "incsz W,$fr"
     (+ OP6_INCSZ DIR_TO_W fr)
     (sequence ()
	       (set w (add fr 1))
	       (if (zflag w)
		   (skip 1)))
     ()
)

(dni swap_fr "Swap fr nibbles"
     ()
     "swap $fr"
     (+ OP6_SWAP DIR_NOTTO_W fr)
     (set fr (or (and (sll fr 4) #xf0)
		 (and (srl fr 4) #x0f)))
     ()
)

(dni swapw_fr "Swap fr nibbles into W"
     ()
     "swap W,$fr"
     (+ OP6_SWAP DIR_TO_W fr)
     (set w (or (and (sll fr 4) #xf0)
		(and (srl fr 4) #x0f)))
     ()
)

(dni rl_fr "Rotate fr left with carry"
     ()
     "rl $fr"
     (+ OP6_RL DIR_NOTTO_W fr)
     (sequence ((QI newfr) (BI newc))
	       (set newc (and fr #x80))
	       (set newfr (or (sll fr 1) (if QI cbit 1 0)))
	       (set cbit (if QI newc 1 0))
	       (set fr newfr))
     ()
)

(dni rlw_fr "Rotate fr left with carry into W"
     ()
     "rl W,$fr"
     (+ OP6_RL DIR_TO_W fr)
     (sequence ((QI newfr) (BI newc))
	       (set newc (and fr #x80))
	       (set newfr (or (sll fr 1) (if QI cbit 1 0)))
	       (set cbit (if QI newc 1 0))
	       (set w newfr))
     ()
)

(dni rr_fr "Rotate fr right with carry"
     ()
     "rr $fr"
     (+ OP6_RR DIR_NOTTO_W fr)
     (sequence ((QI newfr) (BI newc))
	       (set newc (and fr #x01))
	       (set newfr (or (srl fr 1) (if QI cbit #x80 #x00)))
	       (set cbit (if QI newc 1 0))
	       (set fr newfr))
     ()
)

(dni rrw_fr "Rotate fr right with carry into W"
     ()
     "rr W,$fr"
     (+ OP6_RR DIR_TO_W fr)
     (sequence ((QI newfr) (BI newc))
	       (set newc (and fr #x01))
	       (set newfr (or (srl fr 1) (if QI cbit #x80 #x00)))
	       (set cbit (if QI newc 1 0))
	       (set w newfr))
     ()
)

(dni decsz_fr "Skip if fr-- zero"
     ()
     "decsz $fr"
     (+ OP6_DECSZ DIR_NOTTO_W fr)
     (sequence ((QI isLreg) (HI 16bval))
          (LregCheck isLreg (ifield f-reg))
          ;; If fr is an Lreg, then we have to do 16-bit arithmetic.
          ;; We can take advantage of the fact that by a lucky
          ;; coincidence, the address of register xxxH is always
          ;; one lower than the address of register xxxL.
          (if (eq isLreg #x1)
             (sequence()
                ; Create the 16 bit value
                (set 16bval (reg h-spr (sub (ifield f-reg) 1)))
                (set 16bval (sll 16bval 8))
                (set 16bval (or 16bval (and (reg h-spr (ifield f-reg)) #xFF)))
                ; New 16 bit instruction
                (set 16bval (sub HI 16bval 1))
                ; Separate the 16 bit values into the H and L regs
                (set (reg h-spr (ifield f-reg)) (and 16bval #xFF))
                (set (reg h-spr (sub (ifield f-reg) 1))
                     (and (srl 16bval 8) #xFF))
                (set fr (reg h-spr (ifield f-reg)))
             )
             ; Original instruction
	     (set fr (sub fr 1))
          )
	       (if (zflag fr)
		   (skip 1)))
     ()
)

(dni decszw_fr "Skip if W=fr-1 zero"
     ()
     "decsz W,$fr"
     (+ OP6_DECSZ DIR_TO_W fr)
     (sequence ()
	       (set w (sub fr 1))
	       (if (zflag w)
		   (skip 1)))
     ()
)

(dni inc_fr "Increment fr"
     ()
     "inc $fr"
     (+ OP6_INC DIR_NOTTO_W fr)
     (sequence ((QI isLreg) (HI 16bval))
          (LregCheck isLreg (ifield f-reg))
          ;; If fr is an Lreg, then we have to do 16-bit arithmetic.
          ;; We can take advantage of the fact that by a lucky
          ;; coincidence, the address of register xxxH is always
          ;; one lower than the address of register xxxL.
          (if (eq isLreg #x1)
             (sequence()
                ; Create the 16 bit value
                (set 16bval (reg h-spr (sub (ifield f-reg) 1)))
                (set 16bval (sll 16bval 8))
                (set 16bval (or 16bval (and (reg h-spr (ifield f-reg)) #xFF)))
                ; New 16 bit instruction
		(set 16bval (add HI 16bval 1))
                ; Separate the 16 bit values into the H and L regs
                (set (reg h-spr (ifield f-reg)) (and 16bval #xFF))
                (set (reg h-spr (sub (ifield f-reg) 1))
                     (and (srl 16bval 8) #xFF))
                (set fr (reg h-spr (ifield f-reg)))
             )
             ; Original instruction
	     (set fr (add fr 1))
           )
	       (set zbit (zflag fr)))
     ()
)

(dni incw_fr "Increment fr into w"
     ()
     "inc W,$fr"
     (+ OP6_INC DIR_TO_W fr)
     (sequence ()
	       (set w (add fr 1))
	       (set zbit (zflag w)))
     ()
)

(dni not_fr "Invert fr"
     ()
     "not $fr"
     (+ OP6_NOT DIR_NOTTO_W fr)
     (sequence ()
	       (set fr (inv fr))
	       (set zbit (zflag fr)))
     ()
)

(dni notw_fr "Invert fr into w"
     ()
     "not W,$fr"
     (+ OP6_NOT DIR_TO_W fr)
     (sequence ()
	       (set w (inv fr))
	       (set zbit (zflag w)))
     ()
)

(dni test_fr "Test fr"
     ()
     "test $fr"
     (+ OP6_TEST DIR_NOTTO_W fr)
     (sequence ()
	       (set zbit (zflag fr)))
     ()
)

(dni movw_l "MOV W,literal"
     ()
     "mov W,#$lit8"
     (+ OP4_LITERAL OP4MID_MOV_L lit8)
     (set w lit8)
     ()
)

(dni movfr_w "Move/test w into fr"
     ()
     "mov $fr,W"
     (+ OP6_OTHER1 DIR_NOTTO_W fr)
     (set fr w)
     ()
)

(dni movw_fr "Move/test fr into w"
     ()
     "mov W,$fr"
     (+ OP6_TEST DIR_TO_W fr)
     (sequence ()
	       (set w fr)
	       (set zbit (zflag w)))
     ()
)


(dni addfr_w "Add fr,W"
     ()
     "add $fr,W"
     (+ OP6_ADD DIR_NOTTO_W fr)
     (sequence ((QI result) (QI isLreg) (HI 16bval))
	       (set cbit (add-cflag w fr 0))
	       (set dcbit (add-dcflag w fr 0))
               (LregCheck isLreg (ifield f-reg))

               ;; If fr is an Lreg, then we have to do 16-bit arithmetic.
               ;; We can take advantage of the fact that by a lucky
               ;; coincidence, the address of register xxxH is always 
               ;; one lower than the address of register xxxL.
               (if (eq isLreg #x1)
                  (sequence()
                     (set 16bval (reg h-spr (sub (ifield f-reg) 1)))
                     (set 16bval (sll 16bval 8))
                     (set 16bval (or 16bval (and (reg h-spr (ifield f-reg)) #xFF)))
                     (set 16bval (add HI (and w #xFF) 16bval))
                     (set (reg h-spr (ifield f-reg)) (and 16bval #xFF))
                     (set (reg h-spr (sub (ifield f-reg) 1))
                          (and (srl 16bval 8) #xFF))
                     (set result (reg h-spr (ifield f-reg)))
                  )
	       (set result (addc w fr 0)) ;; else part
               )
	       (set zbit (zflag result))
	       (set fr result))
     ()
)

(dni addw_fr "Add W,fr"
     ()
     "add W,$fr"
     (+ OP6_ADD DIR_TO_W fr)
     (sequence ((QI result))
	       (set cbit (add-cflag w fr 0))
	       (set dcbit (add-dcflag w fr 0))
	       (set result (addc w fr 0))
	       (set zbit (zflag result))
	       (set w result))
     ()
)

(dni xorfr_w "XOR fr,W"
     ()
     "xor $fr,W"
     (+ OP6_XOR DIR_NOTTO_W fr)
     (sequence ()
	       (set fr (xor w fr))
	       (set zbit (zflag fr)))
     ()
)

(dni xorw_fr "XOR W,fr"
     ()
     "xor W,$fr"
     (+ OP6_XOR DIR_TO_W fr)
     (sequence ()
	       (set w (xor fr w))
	       (set zbit (zflag w)))
     ()
)

(dni andfr_w "AND fr,W"
     ()
     "and $fr,W"
     (+ OP6_AND DIR_NOTTO_W fr)
     (sequence ()
	       (set fr (and w fr))
	       (set zbit (zflag fr)))
     ()
)

(dni andw_fr "AND W,fr"
     ()
     "and W,$fr"
     (+ OP6_AND DIR_TO_W fr)
     (sequence ()
	       (set w (and fr w))
	       (set zbit (zflag w)))
     ()
)

(dni orfr_w "OR fr,W"
     ()
     "or $fr,W"
     (+ OP6_OR DIR_NOTTO_W fr)
     (sequence ()
	       (set fr (or w fr))
	       (set zbit (zflag fr)))
     ()
)

(dni orw_fr "OR W,fr"
     ()
     "or W,$fr"
     (+ OP6_OR DIR_TO_W fr)
     (sequence ()
	       (set w (or fr w))
	       (set zbit (zflag w)))
     ()
)

(dni dec_fr "Decrement fr"
     ()
     "dec $fr"
     (+ OP6_DEC DIR_NOTTO_W fr)
     (sequence ((QI isLreg) (HI 16bval))
          (LregCheck isLreg (ifield f-reg))
          ;; If fr is an Lreg, then we have to do 16-bit arithmetic.
          ;; We can take advantage of the fact that by a lucky
          ;; coincidence, the address of register xxxH is always
          ;; one lower than the address of register xxxL.
          (if (eq isLreg #x1)
             (sequence()
                ; Create the 16 bit value
                (set 16bval (reg h-spr (sub (ifield f-reg) 1)))
                (set 16bval (sll 16bval 8))
                (set 16bval (or 16bval (and (reg h-spr (ifield f-reg)) #xFF)))
                ; New 16 bit instruction
		(set 16bval (sub HI 16bval 1))
                ; Separate the 16 bit values into the H and L regs
                (set (reg h-spr (ifield f-reg)) (and 16bval #xFF))
                (set (reg h-spr (sub (ifield f-reg) 1))
                     (and (srl 16bval 8) #xFF))
                (set fr (reg h-spr (ifield f-reg)))
             )
             ; Original instruction
	     (set fr (sub fr 1))
	  )
	     (set zbit (zflag fr)))
     ()
)

(dni decw_fr "Decrement fr into w"
     ()
     "dec W,$fr"
     (+ OP6_DEC DIR_TO_W fr)
     (sequence ()
	       (set w (sub fr 1))
	       (set zbit (zflag w)))
     ()
)

(dni subfr_w "Sub fr,W"
     ()
     "sub $fr,W"
     (+ OP6_SUB DIR_NOTTO_W fr)
     (sequence ((QI result) (QI isLreg) (HI 16bval))
	       (set cbit (not (sub-cflag fr w 0)))
	       (set dcbit (not (sub-dcflag fr w 0)))
               (LregCheck isLreg (ifield f-reg))
               ;; If fr is an Lreg, then we have to do 16-bit arithmetic.
               ;; We can take advantage of the fact that by a lucky
               ;; coincidence, the address of register xxxH is always
               ;; one lower than the address of register xxxL.
               (if (eq isLreg #x1)
                  (sequence()
                     ; Create the 16 bit value
                     (set 16bval (reg h-spr (sub (ifield f-reg) 1)))
                     (set 16bval (sll 16bval 8))
                     (set 16bval (or 16bval (and (reg h-spr (ifield f-reg)) #xFF)))
                     ; New 16 bit instruction
                     (set 16bval (sub HI 16bval (and w #xFF)))
                     ; Separate the 16 bit values into the H and L regs
                     (set (reg h-spr (ifield f-reg)) (and 16bval #xFF))
                     (set (reg h-spr (sub (ifield f-reg) 1))
                          (and (srl 16bval 8) #xFF))
                     (set result (reg h-spr (ifield f-reg)))
                  )
               ; Original instruction
	       (set result (subc fr w 0))
               )
	       (set zbit (zflag result))
	       (set fr result))
     ()
)

(dni subw_fr "Sub W,fr"
     ()
     "sub W,$fr"
     (+ OP6_SUB DIR_TO_W fr)
     (sequence ((QI result))
	       (set cbit (not (sub-cflag fr w 0)))
	       (set dcbit (not (sub-dcflag fr w 0)))
	       (set result (subc fr w 0))
	       (set zbit (zflag result))
	       (set w result))
     ()
)

(dni clr_fr "Clear fr"
     ()
     "clr $fr"
     (+ OP6_OTHER2 (f-dir 1) fr)
     (sequence ()
	       (set fr 0)
	       (set zbit (zflag fr)))
     ()
)

(dni cmpw_fr "CMP W,fr"
     ()
     "cmp W,$fr"
     (+ OP6_OTHER2 (f-dir 0) fr)
     (sequence ()
	       (set cbit (not (sub-cflag fr w 0)))
	       (set dcbit (not (sub-dcflag fr w 0)))
	       (set zbit (zflag (sub w fr))))
     ()
)

(dni speed "Set speed"
     ()
     "speed #$lit8"
     (+ (f-op8 1) lit8)
     (set (reg h-registers #x0E) lit8)
     ()
)

(dni ireadi "Insn memory read with increment"
     ()
     "ireadi"
     (+ OP6_OTHER1 (f-op6-10low #x1D))
     (c-call "do_insn_read")
     ()
)

(dni iwritei "Insn memory write with increment"
     ()
     "iwritei"
     (+ OP6_OTHER1 (f-op6-10low #x1C))
     (c-call "do_insn_write")
     ()
)

(dni fread "Flash read"
     ()
     "fread"
     (+ OP6_OTHER1 (f-op6-10low #x1B))
     (c-call "do_flash_read")
     ()
)

(dni fwrite "Flash write"
     ()
     "fwrite"
     (+ OP6_OTHER1 (f-op6-10low #x1A))
     (c-call "do_flash_write")
     ()
)

(dni iread "Insn memory read"
     ()
     "iread"
     (+ OP6_OTHER1 (f-op6-10low #x19))
     (c-call "do_insn_read")
     ()
)

(dni iwrite "Insn memory write"
     ()
     "iwrite"
     (+ OP6_OTHER1 (f-op6-10low #x18))
     (c-call "do_insn_write")
     ()
)

(dni page "Set insn page"
     (EXT-SKIP-INSN)
     ;"page $page3"
     "page $addr16p"
     ;(+ OP6_OTHER1 (f-op6-7low #x2) page3)
     ;(set pabits (srl page3 13))
     (+ OP6_OTHER1 (f-op6-7low #x2) addr16p)
     (set pabits addr16p)
     ()
)

(dni system "System call"
     ()
     "system"
     (+ OP6_OTHER1 (f-op6-10low #xff))
     (c-call "do_system")
     ()
)

(dni reti "Return from interrupt"
     ()
     "reti #$reti3"
     (+ OP6_OTHER1 (f-op6-7low #x1) reti3)
     (c-call "do_reti" reti3)
     ()
)

(dni ret "Return"
     ()
     "ret"
     (+ OP6_OTHER1 (f-op6-10low #x07))
     (sequence ((USI new_pc))
	       (set new_pc (c-call UHI "pop_pc_stack"))
	       (set pabits (srl new_pc 13))
	       (set pc new_pc))
     ()
)

(dni int "Software interrupt"
     ()
     "int"
     (+ OP6_OTHER1 (f-op6-10low #x6))
     (nop)
     ()
)

(dni breakx "Breakpoint with extended skip"
     (EXT-SKIP-INSN)
     "breakx"
     (+ OP6_OTHER1 (f-op6-10low #x5))
     (c-call "do_break" pc)
     ()
)

(dni cwdt "Clear watchdog timer"
     ()
     "cwdt"
     (+ OP6_OTHER1 (f-op6-10low #x4))
     (c-call "do_clear_wdt")
     ()
)

(dni ferase "Flash erase"
     ()
     "ferase"
     (+ OP6_OTHER1 (f-op6-10low #x3))
     (c-call "do_flash_erase")
     ()
)

(dni retnp "Return, no page"
     ()
     "retnp"
     (+ OP6_OTHER1 (f-op6-10low #x2))
     (sequence ((USI new_pc))
	       (set new_pc (c-call UHI "pop_pc_stack"))
	       (set pc new_pc))
     ()
)

(dni break "Breakpoint"
     ()
     "break"
     (+ OP6_OTHER1 (f-op6-10low #x1))
     (c-call "do_break" pc)
     ()
)

(dni nop "No operation"
     ()
     "nop"
     (+ OP6_OTHER1 (f-op6-10low #x0))
     (nop)
     ()
)
			   

; Macro instructions
(dnmi sc "Skip on carry"
      ()
      "sc"
      (emit sb (bitno 0) (fr #xB)) ; sb status.0
)

(dnmi snc "Skip on no carry"
      ()
      "snc"
      (emit snb (bitno 0) (fr #xB)) ; snb status.0
)
      
(dnmi sz "Skip on zero"
      ()
      "sz"
      (emit sb (bitno 2) (fr #xB)) ; sb status.2
)

(dnmi snz "Skip on no zero"
      ()
      "snz"
      (emit snb (bitno 2) (fr #xB)) ; snb status.2
)

(dnmi skip "Skip always"
      (SKIPA)
      "skip"
      (emit snb (bitno 0) (fr 9)) ; snb pcl.0 | (pcl&1)<<12
)

(dnmi skipb "Skip always"
      (SKIPA)
      "skip"
      (emit sb (bitno 0) (fr 9)) ; sb pcl.0 | (pcl&1)<<12
)