Home | History | Annotate | Download | only in cpu
      1 ; xstormy16 CPU core description. -*- Scheme -*-
      2 ; Copyright 2011 Free Software Foundation, Inc.
      3 ;
      4 ; Contributed by Red Hat Inc;
      5 ;
      6 ; This file is part of the GNU Binutils.
      7 ;
      8 ; This program is free software; you can redistribute it and/or modify
      9 ; it under the terms of the GNU General Public License as published by
     10 ; the Free Software Foundation; either version 3 of the License, or
     11 ; (at your option) any later version.
     12 ;
     13 ; This program is distributed in the hope that it will be useful,
     14 ; but WITHOUT ANY WARRANTY; without even the implied warranty of
     15 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16 ; GNU General Public License for more details.
     17 ;
     18 ; You should have received a copy of the GNU General Public License
     19 ; along with this program; if not, write to the Free Software
     20 ; Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     21 ; MA 02110-1301, USA.
     22 
     23 (define-rtl-version 0 8)
     24 
     25 (include "simplify.inc")
     26 
     27 (define-arch
     28   (name xstormy16)
     29   (comment "Xstormy16 architecture")
     30   (insn-lsb0? #f)
     31   (machs xstormy16)
     32   (isas xstormy16)
     33 )
     34 
     35 (define-isa
     36   (name xstormy16)
     37   (comment "Xstormy16 instruction set")
     38   (default-insn-word-bitsize 32)
     39   (default-insn-bitsize 32)
     40   ; FIXME base-insn-bitsize should be 16 too, but at present CGEN has
     41   ; no support for instruction sets with opcode bits past
     42   ; base-insn-bitsize, so we must set it to at least 20.
     43   (base-insn-bitsize 32)
     44 )
     45 
     46 (define-cpu
     47   (name xstormy16)
     48   (comment "Xstormy16 CPU core")
     49   (endian little)
     50   (insn-endian little)
     51   (insn-chunk-bitsize 16)
     52   (word-bitsize 32)
     53 )
     54 
     55 (define-mach
     56   (name xstormy16)
     57   (comment "Xstormy16 CPU core")
     58   (cpu xstormy16)
     59   (isas xstormy16)
     60 )
     61 
     62 (define-model
     63   (name xstormy16)
     64   (comment "Xstormy16 CPU core")
     65   (unit u-exec "Execution Unit" ()
     66 	1 1 ; issue done
     67 	() () () ())
     68 )
     69 
     70 ; IDOC attribute for instruction documentation.
     71 
     72 (define-attr
     73   (for insn)
     74   (type enum)
     75   (name IDOC)
     76   (comment "insn kind for documentation")
     77   (attrs META)
     78   (values
     79    (MEM - () "Memory")
     80    (ALU - () "ALU")
     81    (FPU - () "FPU")
     82    (BR - () "Branch")
     83    (PRIV - () "Priviledged")
     84    (MISC - () "Miscellaneous")
     85   )
     86 )
     87 
     89 ; Hardware elements.
     90 
     91 (define-hardware
     92   (name h-pc)
     93   (comment "program counter")
     94   (attrs PC)
     95   (type pc)
     96   (set (newval) (c-call "h_pc_set_handler" newval))
     97 )
     98 
     99 (define-keyword
    100   (name gr-names)
    101   (enum-prefix H-GR-)
    102   (values (r0 0) (r1 1) (r2 2) (r3 3)
    103 	  (r4 4) (r5 5) (r6 6) (r7 7)
    104 	  (r8 8) (r9 9) (r10 10) (r11 11)
    105 	  (r12 12) (r13 13) (r14 14) (r15 15)
    106 	  (psw 14) (sp 15)))
    107 
    108 (define-keyword
    109   (name gr-Rb-names)
    110   (enum-prefix H-RBJ-)
    111   (values (r8 0) (r9 1) (r10 2) (r11 3)
    112 	  (r12 4) (r13 5) (r14 6) (r15 7)
    113 	  (psw 6) (sp 7)))
    114 
    115 (define-hardware
    116   (name h-gr)
    117   (comment "registers")
    118   (type register WI (16))
    119   (indices extern-keyword gr-names)
    120   (get (index) (and #xFFFF (raw-reg h-gr index)))
    121   (set (index newval) (c-call "h_gr_set_handler" index newval))
    122 )
    123 
    124 (define-hardware
    125   (name h-Rb)
    126   (comment "Rb registers")
    127   (attrs VIRTUAL)
    128   (type register SI(8))
    129   (indices extern-keyword gr-Rb-names)
    130   (get (index) (reg h-gr (add index 8)))
    131   (set (index newval) (set (reg h-gr (add index 8)) newval))
    132 )
    133 
    134 (define-hardware
    135   (name h-Rbj)
    136   (comment "Rbj registers")
    137   (attrs VIRTUAL)
    138   (type register SI(2))
    139   (indices extern-keyword gr-Rb-names)
    140   (get (index) (reg h-gr (add index 8)))
    141   (set (index newval) (set (reg h-gr (add index 8)) newval))
    142 )
    143 
    144 (define-hardware
    145   (name h-Rpsw)
    146   (comment "Register number field of the PSW")
    147   (attrs VIRTUAL)
    148   (type register WI)
    149   (get () (and #xF (srl psw 12)))
    150   (set (newval) (set psw (or (and psw #xFFF)
    151 			     (sll HI newval 12)))))
    152 
    153 (define-pmacro (define-psw-field fnam hnam index)
    154   (define-hardware
    155     (name hnam)
    156     (attrs VIRTUAL)
    157     (type register SI)
    158     (get () (and 1 (srl psw index)))
    159     (set (newval) (set psw (or (and psw (inv (sll HI 1 index)))
    160 			       (sll HI newval index)))))
    161   ;(dnop fnam "" (SEM-ONLY) hnam f-nil)
    162 )
    163 (define-psw-field psw-z8  h-z8   0)
    164 (dnop psw-z8 "" (SEM-ONLY) h-z8 f-nil)
    165 (define-psw-field psw-z16 h-z16  1)
    166 (dnop psw-z16 "" (SEM-ONLY) h-z16 f-nil)
    167 (define-psw-field psw-cy  h-cy   2)
    168 (dnop psw-cy "" (SEM-ONLY) h-cy f-nil)
    169 (define-psw-field psw-hc  h-hc   3)
    170 (dnop psw-hc "" (SEM-ONLY) h-hc f-nil)
    171 (define-psw-field psw-ov  h-ov   4)
    172 (dnop psw-ov "" (SEM-ONLY) h-ov f-nil)
    173 (define-psw-field psw-pt  h-pt   5)
    174 (dnop psw-pt "" (SEM-ONLY) h-pt f-nil)
    175 (define-psw-field psw-s   h-s    6)
    176 (dnop psw-s  "" (SEM-ONLY) h-s  f-nil)
    177 
    178 (define-hardware
    179   (name h-branchcond)
    180   (comment "Condition of a branch instruction")
    181   (type immediate (UINT 4))
    182   (values keyword "" 
    183 	  (("ge" 0) ("nc" 1) ("lt" 2) ("c" 3)
    184 	   ("gt" 4) ("hi" 5) ("le" 6) ("ls" 7)
    185 	   ("pl" 8) ("nv" 9) ("mi" 10) ("v" 11)
    186 	   ("nz.b" 12) ("nz" 13) ("z.b" 14) ("z" 15)))
    187 )
    188 
    189 (define-hardware
    190   (name h-wordsize)
    191   (comment "Data size")
    192   (type immediate (UINT 1))
    193   (values keyword "" ((".b" 0) (".w" 1) ("" 1)))
    194 )
    195 	
    196 
    198 ; Instruction fields, and the corresponding operands.
    199 ; Register fields
    200 
    201 (dnf f-Rd "general register destination" ()  12 4)
    202 (dnop Rd "general register destination" ()  h-gr f-Rd)
    203 
    204 (dnf f-Rdm "general register destination" ()  13 3)
    205 (dnop Rdm "general register destination" ()  h-gr f-Rdm)
    206 
    207 (dnf f-Rm "general register for memory" ()  4 3)
    208 (dnop Rm "general register for memory" ()  h-gr f-Rm)
    209 
    210 (dnf f-Rs  "general register source" ()  8 4)
    211 (dnop Rs "general register source" ()  h-gr f-Rs)
    212 
    213 (dnf f-Rb  "base register" ()  17 3)
    214 (dnop Rb "base register" ()  h-Rb f-Rb)
    215 
    216 (dnf f-Rbj "base register for jump" () 11 1)
    217 (dnop Rbj "base register for jump" () h-Rbj f-Rbj)
    218 
    219 ; Main opcodes in 4 bit chunks
    220 
    221 (dnf f-op1 "opcode" ()  0 4)
    222 (define-normal-insn-enum insn-op1 "insn op enums" () OP1_ f-op1
    223  ( "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F" ))
    224 
    225 (dnf f-op2 "opcode" ()  4 4)
    226 (define-normal-insn-enum insn-op2 "insn op enums" () OP2_ f-op2
    227  ( "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F" ))
    228 (dnop bcond2 "branch condition opcode" () h-branchcond f-op2)
    229 
    230 (dnf f-op2a "opcode" ()  4 3)
    231 (define-normal-insn-enum insn-op2a "insn op enums" () OP2A_ f-op2a
    232  ( "0" "2" "4" "6" "8" "A" "C" "E" ))
    233 
    234 (dnf f-op2m "opcode" ()  7 1)
    235 (define-normal-insn-enum insn-op2m "insn op enums" () OP2M_ f-op2m
    236  ( "0" "1" ))
    237 (dnop ws2 "word size opcode" () h-wordsize f-op2m)
    238 
    239 (dnf f-op3 "opcode" ()  8 4)
    240 (define-normal-insn-enum insn-op3 "insn op enums" () OP3_ f-op3
    241  ( "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F" ))
    242 
    243 (dnf f-op3a "opcode" ()  8 2)
    244 (define-normal-insn-enum insn-op3a "insn op enums" () OP3A_ f-op3a
    245  ( "0" "1" "2" "3" ))
    246 
    247 (dnf f-op3b "opcode" ()  8 3)
    248 (define-normal-insn-enum insn-op3b "insn op enums" () OP3B_ f-op3b
    249  ( "0" "2" "4" "6" "8" "A" "C" "E" ))
    250 
    251 (dnf f-op4 "opcode" ()  12 4)
    252 (define-normal-insn-enum insn-op4 "insn op enums" () OP4_ f-op4
    253  ( "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F" ))
    254 
    255 (dnf f-op4m "opcode" () 12 1)
    256 (define-normal-insn-enum insn-op4m "insn op enums" () OP4M_ f-op4m
    257  ( "0" "1" ))
    258 
    259 (dnf f-op4b "opcode" () 15 1)
    260 (define-normal-insn-enum insn-op4b "insn op enums" () OP4B_ f-op4b
    261  ( "0" "1" ))
    262 
    263 (dnf f-op5 "opcode" ()  16 4)
    264 (define-normal-insn-enum insn-op5 "insn op enums" () OP5_ f-op5
    265  ( "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F" ))
    266 (dnop bcond5 "branch condition opcode" () h-branchcond f-op5)
    267 
    268 (dnf f-op5a "opcode" ()  16 1)
    269 (define-normal-insn-enum insn-op5a "insn op enums" () OP5A_ f-op5a
    270  ( "0" "1" ))
    271 
    272 ; The whole first word
    273 (dnf f-op "opcode" () 0 16)
    274 
    275 ; Immediate fields
    276 
    277 (dnf f-imm2  "2 bit unsigned" () 10 2)
    278 (dnop imm2 "2 bit unsigned immediate" () h-uint f-imm2)
    279 
    280 (dnf f-imm3  "3 bit unsigned" () 4 3)
    281 (dnop imm3 "3 bit unsigned immediate" () h-uint f-imm3)
    282 (dnf f-imm3b  "3 bit unsigned for bit tests" () 17 3)
    283 (dnop imm3b "3 bit unsigned immediate for bit tests" () h-uint f-imm3b)
    284 
    285 (dnf f-imm4  "4 bit unsigned" () 8 4)
    286 (define-operand 
    287   (name imm4)
    288   (comment "4 bit unsigned immediate")
    289   (attrs)
    290   (type h-uint)
    291   (index f-imm4)
    292   (handlers (parse "small_immediate"))
    293 )
    294 
    295 (dnf f-imm8  "8 bit unsigned" () 8 8)
    296 (dnop imm8 "8 bit unsigned immediate" () h-uint f-imm8)
    297 (define-operand 
    298   (name imm8small)
    299   (comment "8 bit unsigned immediate")
    300   (attrs)
    301   (type h-uint)
    302   (index f-imm8)
    303   (handlers (parse "small_immediate"))
    304 )
    305 
    306 (define-ifield
    307   (name f-imm12)
    308   (comment "12 bit signed")
    309   (attrs)
    310   (start 20)
    311   (length 12)
    312   (mode INT)
    313 )
    314 (dnop imm12 "12 bit signed immediate" () h-sint f-imm12)
    315 
    316 (dnf f-imm16 "16 bit" (SIGN-OPT) 16 16)
    317 (define-operand
    318   (name imm16)
    319   (comment "16 bit immediate")
    320   (attrs)
    321   (type h-uint)
    322   (index f-imm16)
    323   (handlers (parse "immediate16"))
    324 )
    325 
    326 (dnf f-lmem8  "8 bit unsigned low memory" (ABS-ADDR) 8 8)
    327 (define-operand 
    328   (name lmem8)
    329   (comment "8 bit unsigned immediate low memory")
    330   (attrs)
    331   (type h-uint)
    332   (index f-lmem8)
    333   (handlers (parse "mem8"))
    334 )
    335 (define-ifield 
    336   (name f-hmem8)
    337   (comment "8 bit unsigned high memory")
    338   (attrs ABS-ADDR) 
    339   (start 8)
    340   (length 8)
    341   (mode UINT)
    342   (encode (value pc) (sub HI value #x7F00))
    343   (decode (value pc) (add HI value #x7F00))
    344 )
    345 (define-operand 
    346   (name hmem8)
    347   (comment "8 bit unsigned immediate high memory")
    348   (attrs)
    349   (type h-uint)
    350   (index f-hmem8)
    351   (handlers (parse "mem8"))
    352 )
    353 
    354 (define-ifield
    355   (name f-rel8-2)
    356   (comment "8 bit relative address for 2-byte instruction")
    357   (attrs PCREL-ADDR)
    358   (start 8)
    359   (length 8)
    360   (mode INT)
    361   (encode (value pc) (sub SI value (add SI pc 2)))
    362   (decode (value pc) (add SI value (add SI pc 2)))
    363 )
    364 (dnop rel8-2 "8 bit relative address" () h-uint f-rel8-2)
    365 
    366 (define-ifield
    367   (name f-rel8-4)
    368   (comment "8 bit relative address for 4-byte instruction")
    369   (attrs PCREL-ADDR)
    370   (start 8)
    371   (length 8)
    372   (mode INT)
    373   (encode (value pc) (sub SI value (add SI pc 4)))
    374   (decode (value pc) (add SI value (add SI pc 4)))
    375 )
    376 (dnop rel8-4 "8 bit relative address" () h-uint f-rel8-4)
    377 
    378 (define-ifield
    379   (name f-rel12)
    380   (comment "12 bit relative address")
    381   (attrs PCREL-ADDR)
    382   (start 20)
    383   (length 12)
    384   (mode INT)
    385   (encode (value pc) (sub SI value (add SI pc 4)))
    386   (decode (value pc) (add SI value (add SI pc 4)))
    387 )
    388 (dnop rel12 "12 bit relative address" () h-uint f-rel12)
    389 
    390 (define-ifield
    391   (name f-rel12a)
    392   (comment "12 bit relative address")
    393   (attrs PCREL-ADDR)
    394   (start 4)
    395   (length 11)
    396   (mode INT)
    397   (encode (value pc) (sra SI (sub SI value (add SI pc 2)) 1))
    398   (decode (value pc) (add SI (sll value 1) (add SI pc 2)))
    399 )
    400 (dnop rel12a "12 bit relative address" () h-uint f-rel12a)
    401 
    402 (dnf f-abs24-1  "abs24 low part" () 8 8)
    403 (dnf f-abs24-2  "abs24 high part" () 16 16)
    404 (define-multi-ifield
    405   (name f-abs24)
    406   (comment "Absolute address for jmpf instruction")
    407   (attrs ABS-ADDR)
    408   (mode UINT)
    409   (subfields f-abs24-1 f-abs24-2)
    410   (insert (sequence ()
    411 		    (set (ifield f-abs24-1) (and (ifield f-abs24) #xFF))
    412 		    (set (ifield f-abs24-2) (srl (ifield f-abs24) 8))))
    413   (extract (set (ifield f-abs24) (or (sll (ifield f-abs24-2) 8) f-abs24-1)))
    414 )
    415 (dnop abs24 "24 bit absolute address" () h-uint f-abs24)
    416 
    417 ; Names for registers
    418 (dnop psw "program status word" (SEM-ONLY) h-gr 14)
    419 (dnop Rpsw "N0-N3 of the program status word" (SEM-ONLY) h-Rpsw f-nil)
    420 (dnop sp "stack pointer" (SEM-ONLY) h-gr 15)
    421 (dnop R0 "R0" (SEM-ONLY) h-gr 0)
    422 (dnop R1 "R1" (SEM-ONLY) h-gr 1)
    423 (dnop R2 "R2" (SEM-ONLY) h-gr 2)
    424 (dnop R8 "R8" (SEM-ONLY) h-gr 8)
    425 
    427 ; Useful macros.
    428 
    429 ; THe Z8, Z16, PT, and S flags of the PSW.
    430 (define-pmacro (basic-psw value ws)
    431   (or (or (zflag (and value #xFF))
    432 	  (sll HI (zflag HI value) 1))
    433       (or (sll HI (c-call BI "parity" value) 5)
    434 	  (sll HI (nflag QI (srl value (mul ws 8))) 6))))
    435 
    436 
    437 ; Update the PSW for destination register Rd, set Rd to value.
    438 (define-pmacro (set-psw Rd index value ws)
    439   (sequence ((HI nvalue))
    440     (set nvalue value)
    441     (set (reg HI h-gr index) nvalue)
    442     (set psw (or (and psw #x0F9C)
    443 		 (or (sll index 12)
    444 		     (basic-psw nvalue ws))))))
    445 
    446 ; Update the PSW for destination register Rd.
    447 (define-pmacro (set-psw-nowrite index value ws)
    448   (sequence ((HI nvalue))
    449     (set nvalue value)
    450     (set psw (or (and psw #x0F9C)
    451 		 (or (sll index 12)
    452 		     (basic-psw nvalue ws))))))
    453 
    454 ; Update the PSW for destination non-register dest, set dest to value.
    455 (define-pmacro (set-mem-psw dest value ws)
    456   (sequence ((HI nvalue))
    457     (set nvalue value)
    458     (set psw (or (and psw #xFF9C)
    459 		 (basic-psw nvalue ws)))
    460     (set dest nvalue)))
    461 
    462 ; Update the PSW as with set-psw, but also set the carry flag.
    463 (define-pmacro (set-psw-carry Rd index value carry ws)
    464   (sequence ((HI nvalue) (HI newpsw))
    465     (set nvalue value)
    466     (set newpsw (or (or (and psw #x0F98)
    467 		     (sll (and carry #x1) 2))
    468 		 (or (sll index 12)
    469 		     (basic-psw nvalue ws))))
    470     (set (reg HI h-gr index) nvalue)
    471     (set psw newpsw)
    472     ))
    473 
    474 ; The all-purpose addition operation.
    475 (define-pmacro (set-psw-add Rd index a b c)
    476   (sequence ((HI value) (HI newpsw))
    477     (set value (addc a b c))
    478     (set newpsw (or (or (and psw #x0F80)
    479 			(basic-psw value 1))
    480 		    (or (or (sll HI (add-oflag HI a b c) 4)
    481 			    (sll HI (add-cflag HI a b c) 2))
    482 			(or (and (srl HI (addc HI (and a #xF) (and b #xF) c) 
    483 				      1) #x8)
    484 			    (sll index 12)))))
    485     (set (reg HI h-gr index) value)
    486     (set psw newpsw)
    487     ))
    488 
    489 ; Set the PSW for a subtraction of a-b into Rd, but don't actually
    490 ; do the subtract.
    491 (define-pmacro (set-psw-cmp Rd index a b)
    492   (sequence ((HI value))
    493     (set value (sub a b))
    494     (set psw (or (or (and psw #x0F80)
    495 		     (basic-psw value 1))
    496 		 (or (or (sll HI (sub-oflag HI a b 0) 4)
    497 			 (sll HI (sub-cflag HI a b 0) 2))
    498 		     (or (and (srl HI (sub HI (and a #xF) (and b #xF))
    499 				   1) #x8)
    500 			 (sll index 12)))))))
    501 
    502 ; Likewise, for subtraction
    503 ; (this chip has a borrow for subtraction, rather than
    504 ; just using a carry for both).
    505 (define-pmacro (set-psw-sub Rd index a b c)
    506   (sequence ((HI value) (HI newpsw))
    507     (set value (subc a b c))
    508     (set newpsw (or (or (and psw #x0F80)
    509 		     (basic-psw value 1))
    510 		 (or (or (sll HI (sub-oflag HI a b c) 4)
    511 			 (sll HI (sub-cflag HI a b c) 2))
    512 		     (or (and (srl HI (subc HI (and a #xF) (and b #xF) c)
    513 				   1) #x8)
    514 			 (sll index 12)))))
    515     (set (reg HI h-gr index) value)
    516     (set psw newpsw)
    517     ))
    518 
    519 ; A 17-bit rotate-left operation
    520 (define-pmacro (set-psw-rotate17 Rd index src c rot)
    521   (sequence ((SI tmpfoo))
    522     (set tmpfoo (or (or (and (sll SI src 15) #x7FFE0000) 
    523 		     src)
    524 		 (or (sll SI c 31)
    525 		     (sll SI c 16))))
    526     (set tmpfoo (rol tmpfoo (and rot #x1F)))
    527     (set-psw-carry (reg HI h-gr index) index (trunc HI tmpfoo) (and (srl tmpfoo 16) 1) 1)))
    528 
    529 ; A 17-bit rotate-right operation
    530 (define-pmacro (set-psw-rrotate17 Rd index src c rot)
    531   (sequence ((SI tmpfoo))
    532     (set tmpfoo (or (or (and (sll SI src 17) #xFFFE0000) 
    533 		     src)
    534 		 (sll SI c 16)))
    535     (set tmpfoo (ror tmpfoo (and rot #x0F)))
    536     (set-psw-carry (reg HI h-gr index) index (trunc HI tmpfoo) (and (srl tmpfoo 16) 1) 1)))
    537 
    538 
    540 ; Move Operations
    541 
    542 (define-pmacro (alignfix-mem where)
    543   (mem HI (and where #xFFFE)))
    544 
    545 (define-pmacro (set-alignfix-mem where what)
    546   (set (mem HI (and where #xFFFE)) what))
    547 
    548 (define-pmacro (alignfix-mem-far where)
    549   (mem HI (and where #xFFFFFFFE)))
    550 
    551 (define-pmacro (set-alignfix-mem-far where what)
    552   (set (mem HI (and where #xFFFFFFFE)) what))
    553 
    554 (dni movlmemimm
    555      "Move immediate to low memory"
    556      ()
    557      ("mov$ws2 $lmem8,#$imm16")
    558      (+ OP1_7 OP2A_8 ws2 lmem8 imm16)
    559      (if ws2
    560 	 (set-mem-psw (mem HI (and lmem8 #xFFFE)) imm16 ws2)
    561 	 (set-mem-psw (mem QI lmem8) (and imm16 #xFF) ws2))
    562      ()
    563 )
    564 (dni movhmemimm
    565      "Move immediate to high memory"
    566      ()
    567      ("mov$ws2 $hmem8,#$imm16")
    568      (+ OP1_7 OP2A_A ws2 hmem8 imm16)
    569      (if ws2
    570 	 (set-mem-psw (mem HI (and hmem8 #xFFFE)) imm16 ws2)
    571 	 (set-mem-psw (mem QI hmem8) (and imm16 #xFF) ws2))
    572      ()
    573 )
    574 
    575 (dni movlgrmem
    576      "Move low memory to register"
    577      ()
    578      ("mov$ws2 $Rm,$lmem8")
    579      (+ OP1_8 Rm ws2 lmem8)
    580      (if ws2 
    581 	 (set-psw Rm (index-of Rm) (alignfix-mem lmem8) ws2)
    582 	 (set-psw Rm (index-of Rm) (mem QI lmem8) ws2))
    583      ()
    584 )
    585 (dni movhgrmem
    586      "Move high memory to register"
    587      ()
    588      ("mov$ws2 $Rm,$hmem8")
    589      (+ OP1_A Rm ws2 hmem8)
    590      (if ws2 
    591 	 (set-psw Rm (index-of Rm) (alignfix-mem hmem8) ws2)
    592 	 (set-psw Rm (index-of Rm) (mem QI hmem8) ws2))
    593      ()
    594 )
    595 
    596 (dni movlmemgr
    597      "Move low memory register to byte"
    598      ()
    599      ("mov$ws2 $lmem8,$Rm")
    600      (+ OP1_9 Rm ws2 lmem8)
    601      (if ws2 
    602 	 (set-mem-psw (mem HI (and lmem8 #xFFFE)) Rm ws2)
    603 	 (set-mem-psw (mem QI lmem8) Rm ws2))
    604      ()
    605 )
    606 (dni movhmemgr
    607      "Move high memory register to byte"
    608      ()
    609      ("mov$ws2 $hmem8,$Rm")
    610      (+ OP1_B Rm ws2 hmem8)
    611      (if ws2 
    612 	 (set-mem-psw (mem HI (and hmem8 #xFFFE)) Rm ws2)
    613 	 (set-mem-psw (mem QI hmem8) Rm ws2))
    614      ()
    615 )
    616 
    617 (dni movgrgri
    618      "Move memory addressed by register to register"
    619      ()
    620      ("mov$ws2 $Rdm,($Rs)")
    621      (+ OP1_7 OP2A_0 ws2 Rs OP4M_0 Rdm)
    622      (if ws2
    623 	 (set-psw Rdm (index-of Rdm) (alignfix-mem Rs) ws2)
    624 	 (set-psw Rdm (index-of Rdm) (and #xFF (mem QI Rs)) ws2))
    625      ()
    626 )
    627 
    628 (dni movgrgripostinc
    629      "Move memory addressed by postincrement register to register"
    630      ()
    631      ("mov$ws2 $Rdm,($Rs++)")
    632      (+ OP1_6 OP2A_0 ws2 Rs OP4M_0 Rdm)
    633      (sequence ()
    634 	       (if ws2
    635 		   (set-psw Rdm (index-of Rdm) (alignfix-mem Rs) ws2)
    636 		   (set-psw Rdm (index-of Rdm) (and #xFF (mem QI Rs)) ws2))
    637 	       (set Rs (add Rs (add 1 ws2))))
    638      ()
    639 )
    640 
    641 (dni movgrgripredec
    642      "Move memory addressed by predecrement register to register"
    643      ()
    644      ("mov$ws2 $Rdm,(--$Rs)")
    645      (+ OP1_6 OP2A_8 ws2 Rs OP4M_0 Rdm)
    646      (sequence ()
    647 	       (set Rs (sub Rs (add 1 ws2)))
    648 	       (if ws2
    649 		   (set-psw Rdm (index-of Rdm) (alignfix-mem Rs) ws2)
    650 		   (set-psw Rdm (index-of Rdm) (and #xFF (mem QI Rs)) ws2)))
    651      ()
    652 )
    653 
    654 (dni movgrigr
    655      "Move register to memory addressed by register"
    656      ()
    657      ("mov$ws2 ($Rs),$Rdm")
    658      (+ OP1_7 OP2A_2 ws2 Rs OP4M_0 Rdm)
    659      (sequence ()
    660 	       (if ws2
    661 		   (set-alignfix-mem Rs Rdm)
    662 		   (set (mem QI Rs) Rdm))
    663 	       (set-psw-nowrite (index-of Rdm) Rdm ws2))
    664      ()
    665 )
    666 
    667 (dni movgripostincgr
    668      "Move register to memory addressed by postincrement register"
    669      ()
    670      ("mov$ws2 ($Rs++),$Rdm")
    671      (+ OP1_6 OP2A_2 ws2 Rs OP4M_0 Rdm)
    672      (sequence ()
    673 	       (if ws2
    674 		   (set-alignfix-mem Rs Rdm)
    675 		   (set (mem QI Rs) Rdm))
    676 	       (set-psw-nowrite (index-of Rdm) Rdm ws2)
    677 	       (set Rs (add Rs (add ws2 1))))
    678      ()
    679 )
    680 
    681 (dni movgripredecgr
    682      "Move register to memory addressed by predecrement register"
    683      ()
    684      ("mov$ws2 (--$Rs),$Rdm")
    685      (+ OP1_6 OP2A_A ws2 Rs OP4M_0 Rdm)
    686      (sequence ()
    687 	       (set Rs (sub Rs (add ws2 1)))
    688 	       (set-psw-nowrite (index-of Rdm) Rdm ws2)
    689 	       (if ws2
    690 		   (set-alignfix-mem Rs Rdm)
    691 		   (set (mem QI Rs) Rdm)))
    692      ()
    693 )
    694 
    695 (dni movgrgrii
    696      "Move memory addressed by indexed register to register"
    697      ()
    698      ("mov$ws2 $Rdm,($Rs,$imm12)")
    699      (+ OP1_7 OP2A_0 ws2 Rs OP4M_1 Rdm OP5_0 imm12)
    700      (if ws2
    701 	 (set-psw Rdm (index-of Rdm) (alignfix-mem (add Rs imm12)) ws2)
    702 	 (set-psw Rdm (index-of Rdm) (and #xFF (mem QI (add Rs imm12))) ws2))
    703      ()
    704 )
    705 
    706 (dni movgrgriipostinc
    707      "Move memory addressed by indexed register postincrement to register"
    708      ()
    709      ("mov$ws2 $Rdm,($Rs++,$imm12)")
    710      (+ OP1_6 OP2A_0 ws2 Rs OP4M_1 Rdm OP5_0 imm12)
    711      (sequence ()
    712 	       (if ws2
    713 		   (set-psw Rdm (index-of Rdm) (alignfix-mem (add Rs imm12)) ws2)
    714 		   (set-psw Rdm (index-of Rdm) (and #xFF (mem QI (add Rs imm12))) ws2))
    715 	       (set Rs (add Rs (add ws2 1))))
    716      ()
    717 )
    718 
    719 (dni movgrgriipredec
    720      "Move memory addressed by indexed register predecrement to register"
    721      ()
    722      ("mov$ws2 $Rdm,(--$Rs,$imm12)")
    723      (+ OP1_6 OP2A_8 ws2 Rs OP4M_1 Rdm OP5_0 imm12)
    724      (sequence ()
    725 	       (set Rs (sub Rs (add ws2 1)))
    726 	       (if ws2
    727 		   (set-psw Rdm (index-of Rdm) (alignfix-mem (add Rs imm12)) ws2)
    728 		   (set-psw Rdm (index-of Rdm) (and #xFF (mem QI (add Rs imm12))) ws2)))
    729      ()
    730 )
    731 
    732 (dni movgriigr
    733      "Move register to memory addressed by indexed register"
    734      ()
    735      ("mov$ws2 ($Rs,$imm12),$Rdm")
    736      (+ OP1_7 OP2A_2 ws2 Rs OP4M_1 Rdm OP5_0 imm12)
    737      (sequence ()
    738 	       (if ws2
    739 		   (set-alignfix-mem (add Rs imm12) Rdm)
    740 		   (set (mem QI (add Rs imm12)) Rdm))
    741 	       (set-psw-nowrite (index-of Rdm) Rdm ws2))
    742      ()
    743 )
    744 
    745 (dni movgriipostincgr
    746      "Move register to memory addressed by indexed register postincrement"
    747      ()
    748      ("mov$ws2 ($Rs++,$imm12),$Rdm")
    749      (+ OP1_6 OP2A_2 ws2 Rs OP4M_1 Rdm OP5_0 imm12)
    750      (sequence ()
    751 	       (if ws2
    752 		   (set-alignfix-mem (add Rs imm12) Rdm)
    753 		   (set (mem QI (add Rs imm12)) Rdm))
    754 	       (set-psw-nowrite (index-of Rdm) Rdm ws2)
    755 	       (set Rs (add Rs (add ws2 1))))
    756      ()
    757 )
    758 
    759 (dni movgriipredecgr
    760      "Move register to memory addressed by indexed register predecrement"
    761      ()
    762      ("mov$ws2 (--$Rs,$imm12),$Rdm")
    763      (+ OP1_6 OP2A_A ws2 Rs OP4M_1 Rdm OP5_0 imm12)
    764      (sequence ()
    765 	       (set Rs (sub Rs (add ws2 1)))
    766 	       (set-psw-nowrite (index-of Rdm) Rdm ws2)
    767 	       (if ws2
    768 		   (set-alignfix-mem (add Rs imm12) Rdm)
    769 		   (set (mem QI (add Rs imm12)) Rdm)))
    770      ()
    771 )
    772 
    773 (dni movgrgr
    774      "Move general register to general register"
    775      ()
    776      ("mov $Rd,$Rs")
    777      (+ OP1_4 OP2_6 Rs Rd)
    778      (set-psw Rd (index-of Rd) Rs 1)
    779      ()
    780 )
    781 
    782 (dnmi movimm8
    783      "Move 8-bit immediate"
    784      ()
    785      ("mov Rx,#$imm8")
    786      (emit movwimm8 imm8)
    787 )
    788 
    789 (dni movwimm8
    790      "Move 8-bit immediate"
    791      ()
    792      ("mov.w Rx,#$imm8")
    793      (+ OP1_4 OP2_7 imm8)
    794      (set-psw (reg HI h-gr Rpsw) Rpsw imm8 1)
    795      ()
    796 )
    797 
    798 (dnmi movgrimm8
    799      "Move 8-bit immediate to general register"
    800      ()
    801      ("mov $Rm,#$imm8small")
    802      (emit movwgrimm8 Rm imm8small)
    803 )
    804 
    805 (dni movwgrimm8
    806      "Move 8-bit immediate to general register"
    807      ()
    808      ("mov.w $Rm,#$imm8small")
    809      (+ OP1_2 Rm OP2M_1 imm8small)
    810      (set-psw Rm (index-of Rm) imm8small 1)
    811      ()
    812 )
    813 
    814 (dnmi movgrimm16
    815      "Move 16-bit immediate to general register"
    816      ()
    817      ("mov $Rd,#$imm16")
    818      (emit movwgrimm16 Rd imm16)
    819 )
    820 
    821 (dni movwgrimm16
    822      "Move 16-bit immediate to general register"
    823      ()
    824      ("mov.w $Rd,#$imm16")
    825      (+ OP1_3 OP2_1 OP3_3 Rd imm16)
    826      (set-psw Rd (index-of Rd) imm16 1)
    827      ()
    828 )
    829 
    830 (dni movlowgr
    831      "Move 8 low bits to general register"
    832      ()
    833      ("mov.b $Rd,RxL")
    834      (+ OP1_3 OP2_0 OP3_C Rd)
    835      (set-psw Rd (index-of Rd) (or (and Rd #xFF00) (and (reg HI h-gr Rpsw) #xFF)) 0)
    836      ()
    837 )
    838 
    839 (dni movhighgr
    840      "Move 8 high bits to general register"
    841      ()
    842      ("mov.b $Rd,RxH")
    843      (+ OP1_3 OP2_0 OP3_D Rd)
    844      (set-psw Rd (index-of Rd) (or (and Rd #x00FF) (and (reg HI h-gr Rpsw) #xFF00)) 1)
    845      ()
    846 )
    847 
    848 (dni movfgrgri
    849      "Move far memory addressed by register to register"
    850      ()
    851      ("movf$ws2 $Rdm,($Rs)")
    852      (+ OP1_7 OP2A_4 ws2 Rs OP4M_0 Rdm)
    853      (if ws2
    854 	 (set-psw Rdm (index-of Rdm) (alignfix-mem-far (or (sll SI R8 16) Rs)) ws2)
    855 	 (set-psw Rdm (index-of Rdm) (and #xFF (mem QI (or (sll SI R8 16) Rs))) ws2))
    856      ()
    857 )
    858 
    859 (dni movfgrgripostinc
    860      "Move far memory addressed by postincrement register to register"
    861      ()
    862      ("movf$ws2 $Rdm,($Rs++)")
    863      (+ OP1_6 OP2A_4 ws2 Rs OP4M_0 Rdm)
    864      (sequence ()
    865 	       (if ws2
    866 		   (set-psw Rdm (index-of Rdm) (alignfix-mem-far (join SI HI R8 Rs)) ws2)
    867 		   (set-psw Rdm (index-of Rdm) (and #xFF (mem QI (join SI HI R8 Rs))) ws2))
    868 	       (set Rs (add Rs (add ws2 1))))
    869      ()
    870 )
    871 
    872 (dni movfgrgripredec
    873      "Move far memory addressed by predecrement register to register"
    874      ()
    875      ("movf$ws2 $Rdm,(--$Rs)")
    876      (+ OP1_6 OP2A_C ws2 Rs OP4M_0 Rdm)
    877      (sequence ()
    878 	       (set Rs (sub Rs (add ws2 1)))
    879 	       (if ws2
    880 		   (set-psw Rdm (index-of Rdm) (alignfix-mem-far (join SI HI R8 Rs)) ws2)
    881 		   (set-psw Rdm (index-of Rdm) (and #xFF (mem QI (join SI HI R8 Rs))) ws2)))
    882      ()
    883 )
    884 
    885 (dni movfgrigr
    886      "Move far register to memory addressed by register"
    887      ()
    888      ("movf$ws2 ($Rs),$Rdm")
    889      (+ OP1_7 OP2A_6 ws2 Rs OP4M_0 Rdm)
    890      (sequence ()
    891 	       (if ws2
    892 		   (set-alignfix-mem-far (join SI HI R8 Rs) Rdm)
    893 		   (set (mem QI (join SI HI R8 Rs)) Rdm))
    894 	       (set-psw-nowrite (index-of Rdm) Rdm ws2))
    895      ()
    896 )
    897 
    898 (dni movfgripostincgr
    899      "Move far register to memory addressed by postincrement register"
    900      ()
    901      ("movf$ws2 ($Rs++),$Rdm")
    902      (+ OP1_6 OP2A_6 ws2 Rs OP4M_0 Rdm)
    903      (sequence ()
    904 	       (if ws2
    905 		   (set-alignfix-mem-far (join SI HI R8 Rs) Rdm)
    906 		   (set (mem QI (join SI HI R8 Rs)) Rdm))
    907 	       (set-psw-nowrite (index-of Rdm) Rdm ws2)
    908 	       (set Rs (add Rs (add ws2 1))))
    909      ()
    910 )
    911 
    912 (dni movfgripredecgr
    913      "Move far register to memory addressed by predecrement register"
    914      ()
    915      ("movf$ws2 (--$Rs),$Rdm")
    916      (+ OP1_6 OP2A_E ws2 Rs OP4M_0 Rdm)
    917      (sequence ()
    918 	       (set-psw-nowrite (index-of Rdm) Rdm ws2)
    919 	       (set Rs (sub Rs (add ws2 1)))
    920 	       (if ws2
    921 		   (set-alignfix-mem-far (join SI HI R8 Rs) Rdm)
    922 		   (set (mem QI (join SI HI R8 Rs)) Rdm)))
    923      ()
    924 )
    925 
    926 (dni movfgrgrii
    927      "Move far memory addressed by indexed register to register"
    928      ()
    929      ("movf$ws2 $Rdm,($Rb,$Rs,$imm12)")
    930      (+ OP1_7 OP2A_4 ws2 Rs OP4M_1 Rdm OP5A_0 Rb imm12)
    931      (if ws2
    932 	 (set-psw Rdm (index-of Rdm) (alignfix-mem-far (add (join SI HI Rb Rs) imm12)) ws2)
    933 	 (set-psw Rdm (index-of Rdm) (and #xFF (mem QI (add (join SI HI Rb Rs) imm12))) ws2))
    934      ()
    935 )
    936 
    937 (dni movfgrgriipostinc
    938  "Move far memory addressed by indexed register postincrement to register"
    939      ()
    940      ("movf$ws2 $Rdm,($Rb,$Rs++,$imm12)")
    941      (+ OP1_6 OP2A_4 ws2 Rs OP4M_1 Rdm OP5A_0 Rb imm12)
    942      (sequence ()
    943 	       (if ws2
    944 		   (set-psw Rdm (index-of Rdm) (alignfix-mem-far (add (join SI HI Rb Rs) imm12)) ws2)
    945 		   (set-psw Rdm (index-of Rdm) (and #xFF (mem QI (add (join SI HI Rb Rs) imm12))) ws2))
    946 	       (set Rs (add Rs (add ws2 1)))
    947 	       ; Note - despite the XStormy16 ISA documentation the
    948 	       ; addition *is* propogated into the base register.
    949 	       (if (eq Rs 0) (set Rb (add Rb 1)))
    950 	       )
    951      ()
    952 )
    953 
    954 (dni movfgrgriipredec
    955  "Move far memory addressed by indexed register predecrement to register"
    956      ()
    957      ("movf$ws2 $Rdm,($Rb,--$Rs,$imm12)")
    958      (+ OP1_6 OP2A_C ws2 Rs OP4M_1 Rdm OP5A_0 Rb imm12)
    959      (sequence ()
    960 	       ; Note - despite the XStormy16 ISA documentation the
    961 	       ; subtraction *is* propogated into the base register.
    962 	       (if (eq Rs 0) (set Rb (sub Rb 1)))
    963 	       (set Rs (sub Rs (add ws2 1)))
    964 	       (if ws2
    965 		   (set-psw Rdm (index-of Rdm) (alignfix-mem-far (add (join SI HI Rb Rs) imm12)) ws2)
    966 		   (set-psw Rdm (index-of Rdm) (and #xFF (mem QI (add (join SI HI Rb Rs) imm12))) ws2)))
    967      ()
    968 )
    969 
    970 (dni movfgriigr
    971      "Move far register to memory addressed by indexed register"
    972      ()
    973      ("movf$ws2 ($Rb,$Rs,$imm12),$Rdm")
    974      (+ OP1_7 OP2A_6 ws2 Rs OP4M_1 Rdm OP5A_0 Rb imm12)
    975      (sequence ()
    976 	       (if ws2
    977 		   (set (mem HI (and (add (join SI HI Rb Rs) imm12) #xFFFFFFFE))
    978 			Rdm)
    979 		   (set (mem QI (add (join SI HI Rb Rs) imm12)) Rdm))
    980 	       (set-psw-nowrite (index-of Rdm) Rdm ws2))
    981      ()
    982 )
    983 
    984 
    985 (dni movfgriipostincgr
    986      "Move far register to memory addressed by indexed register postincrement"
    987      ()
    988      ("movf$ws2 ($Rb,$Rs++,$imm12),$Rdm")
    989      (+ OP1_6 OP2A_6 ws2 Rs OP4M_1 Rdm OP5A_0 Rb imm12)
    990      (sequence ()
    991 	       (if ws2
    992 		   (set (mem HI (and (add (join SI HI Rb Rs) imm12) #xFFFFFFFE)) Rdm)
    993 		   (set (mem QI (add (join SI HI Rb Rs) imm12)) Rdm))
    994 	       (set-psw-nowrite (index-of Rdm) Rdm ws2)
    995 	       (set Rs (add Rs (add ws2 1)))
    996 	       ; Note - despite the XStormy16 ISA documentation the
    997 	       ; addition *is* propogated into the base register.
    998 	       (if (eq Rs 0) (set Rb (add Rb 1)))
    999 	       )
   1000      ()
   1001 )
   1002 
   1003 (dni movfgriipredecgr
   1004   "Move far register to memory addressed by indexed register predecrement"
   1005      ()
   1006      ("movf$ws2 ($Rb,--$Rs,$imm12),$Rdm")
   1007      (+ OP1_6 OP2A_E ws2 Rs OP4M_1 Rdm OP5A_0 Rb imm12)
   1008      (sequence ()
   1009 	       ; Note - despite the XStormy16 ISA documentation the
   1010 	       ; subtraction *is* propogated into the base register.
   1011 	       (if (eq Rs 0) (set Rb (sub Rb 1)))
   1012 	       (set Rs (sub Rs (add ws2 1)))
   1013 	       (set-psw-nowrite (index-of Rdm) Rdm ws2)
   1014 	       (if ws2
   1015 		   (set (mem HI (and (add (join SI HI Rb Rs) imm12) #xFFFFFFFE)) Rdm)
   1016 		   (set (mem QI (add (join SI HI Rb Rs) imm12)) Rdm)))
   1017      ()
   1018 )
   1019 
   1020 (dni maskgrgr
   1021      "Mask insert controlled by general register"
   1022      ()
   1023      ("mask $Rd,$Rs")
   1024      (+ OP1_3 OP2_3 Rs Rd)
   1025      (set-psw Rd (index-of Rd) (or HI (and HI Rd (inv HI Rs)) (and (reg HI h-gr Rpsw) Rs)) 1)
   1026      ()
   1027 )
   1028 
   1029 (dni maskgrimm16
   1030      "Mask insert controlled by immediate value"
   1031      ()
   1032      ("mask $Rd,#$imm16")
   1033      (+ OP1_3 OP2_0 OP3_E Rd imm16)
   1034      (set-psw Rd (index-of Rd) (or (and Rd (inv imm16)) (and (reg HI h-gr Rpsw) imm16)) 1)
   1035      ()
   1036 )
   1037 
   1039 ; Push, Pop
   1040 (dni pushgr
   1041      "Push register"
   1042      ()
   1043      ("push $Rd")
   1044      (+ OP1_0 OP2_0 OP3_8 Rd)
   1045      (sequence ()
   1046 	       (set (mem HI sp) Rd)
   1047 	       (set sp (add sp 2)))
   1048      ()
   1049 )
   1050 
   1051 (dni popgr
   1052      "Pop into a register"
   1053      ()
   1054      ("pop $Rd")
   1055      (+ OP1_0 OP2_0 OP3_9 Rd)
   1056      (sequence ()
   1057 	       (set sp (add sp -2))
   1058 	       (set Rd (mem HI sp)))
   1059      ()
   1060 )
   1061 
   1062 ; Swap
   1063 (dni swpn
   1064      "Swap low nibbles"
   1065      ()
   1066      ("swpn $Rd")
   1067      (+ OP1_3 OP2_0 OP3_9 Rd)
   1068      (set-psw Rd (index-of Rd) (or (or (and (sll Rd 4) #xF0)
   1069 			 (and (srl Rd 4) #x0F))
   1070 		     (and Rd #xFF00)) 0)
   1071      ()
   1072 )
   1073 
   1074 (dni swpb
   1075      "Swap bytes"
   1076      ()
   1077      ("swpb $Rd")
   1078      (+ OP1_3 OP2_0 OP3_8 Rd)
   1079      (set-psw Rd (index-of Rd) (or (sll Rd 8) (srl Rd 8)) 1)
   1080      ()
   1081 )
   1082 
   1083 (dni swpw
   1084      "Swap words"
   1085      ()
   1086      ("swpw $Rd,$Rs")
   1087      (+ OP1_3 OP2_2 Rs Rd)
   1088      (sequence ((HI foo))
   1089 	       (set foo Rs)
   1090 	       (set Rs Rd)
   1091 	       (set-psw Rd (index-of Rd) foo 1))
   1092      ()
   1093 )
   1094 
   1096 ; Logical Operations
   1097 (dni andgrgr
   1098      "AND general register with general register"
   1099      ()
   1100      ("and $Rd,$Rs")
   1101      (+ OP1_4 OP2_0 Rs Rd)
   1102      (set-psw Rd (index-of Rd) (and Rd Rs) 1)
   1103      ()
   1104 )
   1105 
   1106 (dni andimm8
   1107      "AND with 8-bit immediate"
   1108      ()
   1109      ("and Rx,#$imm8")
   1110      (+ OP1_4 OP2_1 imm8)
   1111      (set-psw (reg HI h-gr Rpsw) Rpsw (and (reg HI h-gr Rpsw) imm8) 1)
   1112      ()
   1113 )
   1114 
   1115 (dni andgrimm16
   1116      "AND general register with 16-bit immediate"
   1117      ()
   1118      ("and $Rd,#$imm16")
   1119      (+ OP1_3 OP2_1 OP3_0 Rd imm16)
   1120      (set-psw Rd (index-of Rd) (and Rd imm16) 1)
   1121      ()
   1122 )
   1123 
   1124 (dni orgrgr
   1125      "OR general register with general register"
   1126      ()
   1127      ("or $Rd,$Rs")
   1128      (+ OP1_4 OP2_2 Rs Rd)
   1129      (set-psw Rd (index-of Rd) (or Rd Rs) 1)
   1130      ()
   1131 )
   1132 
   1133 (dni orimm8
   1134      "OR with 8-bit immediate"
   1135      ()
   1136      ("or Rx,#$imm8")
   1137      (+ OP1_4 OP2_3 imm8)
   1138      (set-psw (reg HI h-gr Rpsw) Rpsw (or (reg HI h-gr Rpsw) imm8) 1)
   1139      ()
   1140 )
   1141 
   1142 (dni orgrimm16
   1143      "OR general register with 16-bit immediate"
   1144      ()
   1145      ("or $Rd,#$imm16")
   1146      (+ OP1_3 OP2_1 OP3_1 Rd imm16)
   1147      (set-psw Rd (index-of Rd) (or Rd imm16) 1)
   1148      ()
   1149 )
   1150 
   1151 (dni xorgrgr
   1152      "XOR general register with general register"
   1153      ()
   1154      ("xor $Rd,$Rs")
   1155      (+ OP1_4 OP2_4 Rs Rd)
   1156      (set-psw Rd (index-of Rd) (xor Rd Rs) 1)
   1157      ()
   1158 )
   1159 
   1160 (dni xorimm8
   1161      "XOR with 8-bit immediate"
   1162      ()
   1163      ("xor Rx,#$imm8")
   1164      (+ OP1_4 OP2_5 imm8)
   1165      (set-psw (reg HI h-gr Rpsw) Rpsw (xor (reg HI h-gr Rpsw) imm8) 1)
   1166      ()
   1167 )
   1168 
   1169 (dni xorgrimm16
   1170      "XOR general register with 16-bit immediate"
   1171      ()
   1172      ("xor $Rd,#$imm16")
   1173      (+ OP1_3 OP2_1 OP3_2 Rd imm16)
   1174      (set-psw Rd (index-of Rd) (xor Rd imm16) 1)
   1175      ()
   1176 )
   1177 
   1178 (dni notgr
   1179      "NOT general register"
   1180      ()
   1181      ("not $Rd")
   1182      (+ OP1_3 OP2_0 OP3_B Rd)
   1183      (set-psw Rd (index-of Rd) (inv Rd) 1)
   1184      ()
   1185 )
   1186 
   1188 ; Arithmetic operations
   1189 (dni addgrgr
   1190      "ADD general register to general register"
   1191      ()
   1192      ("add $Rd,$Rs")
   1193      (+ OP1_4 OP2_9 Rs Rd)
   1194      (set-psw-add Rd (index-of Rd) Rd Rs 0)
   1195      ()
   1196 )
   1197 
   1198 (dni addgrimm4
   1199      "ADD 4-bit immediate to general register"
   1200      ()
   1201      ("add $Rd,#$imm4")
   1202      (+ OP1_5 OP2_1 imm4 Rd)
   1203      (set-psw-add Rd (index-of Rd) Rd imm4 0)
   1204      ()
   1205 )
   1206 
   1207 (dni addimm8
   1208      "ADD 8-bit immediate"
   1209      ()
   1210      ("add Rx,#$imm8")
   1211      (+ OP1_5 OP2_9 imm8)
   1212      (set-psw-add (reg HI h-gr Rpsw) Rpsw (reg HI h-gr Rpsw) imm8 0)
   1213      ()
   1214 )
   1215 
   1216 (dni addgrimm16
   1217      "ADD 16-bit immediate to general register"
   1218      ()
   1219      ("add $Rd,#$imm16")
   1220      (+ OP1_3 OP2_1 OP3_4 Rd imm16)
   1221      (set-psw-add Rd (index-of Rd) Rd imm16 0)
   1222      ()
   1223 )
   1224 
   1225 (dni adcgrgr
   1226      "ADD carry and general register to general register"
   1227      ()
   1228      ("adc $Rd,$Rs")
   1229      (+ OP1_4 OP2_B Rs Rd)
   1230      (set-psw-add Rd (index-of Rd) Rd Rs psw-cy)
   1231      ()
   1232 )
   1233 
   1234 (dni adcgrimm4
   1235      "ADD carry and 4-bit immediate to general register"
   1236      ()
   1237      ("adc $Rd,#$imm4")
   1238      (+ OP1_5 OP2_3 imm4 Rd)
   1239      (set-psw-add Rd (index-of Rd) Rd imm4 psw-cy)
   1240      ()
   1241 )
   1242 
   1243 (dni adcimm8
   1244      "ADD carry and 8-bit immediate"
   1245      ()
   1246      ("adc Rx,#$imm8")
   1247      (+ OP1_5 OP2_B imm8)
   1248      (set-psw-add (reg HI h-gr Rpsw) Rpsw (reg HI h-gr Rpsw) imm8 psw-cy)
   1249      ()
   1250 )
   1251 
   1252 (dni adcgrimm16
   1253      "ADD carry and 16-bit immediate to general register"
   1254      ()
   1255      ("adc $Rd,#$imm16")
   1256      (+ OP1_3 OP2_1 OP3_5 Rd imm16)
   1257      (set-psw-add Rd (index-of Rd) Rd imm16 psw-cy)
   1258      ()
   1259 )
   1260 
   1261 (dni subgrgr
   1262      "SUB general register from general register"
   1263      ()
   1264      ("sub $Rd,$Rs")
   1265      (+ OP1_4 OP2_D Rs Rd)
   1266      (set-psw-sub Rd (index-of Rd) Rd Rs 0)
   1267      ()
   1268 )
   1269 
   1270 (dni subgrimm4
   1271      "SUB 4-bit immediate from general register"
   1272      ()
   1273      ("sub $Rd,#$imm4")
   1274      (+ OP1_5 OP2_5 imm4 Rd)
   1275      (set-psw-sub Rd (index-of Rd) Rd imm4 0)
   1276      ()
   1277 )
   1278 
   1279 (dni subimm8
   1280      "SUB 8-bit immediate"
   1281      ()
   1282      ("sub Rx,#$imm8")
   1283      (+ OP1_5 OP2_D imm8)
   1284      (set-psw-sub (reg HI h-gr Rpsw) Rpsw (reg HI h-gr Rpsw) imm8 0)
   1285      ()
   1286 )
   1287 
   1288 (dni subgrimm16
   1289      "SUB 16-bit immediate from general register"
   1290      ()
   1291      ("sub $Rd,#$imm16")
   1292      (+ OP1_3 OP2_1 OP3_6 Rd imm16)
   1293      (set-psw-sub Rd (index-of Rd) Rd imm16 0)
   1294      ()
   1295 )
   1296 
   1297 (dni sbcgrgr
   1298      "SUB carry and general register from general register"
   1299      ()
   1300      ("sbc $Rd,$Rs")
   1301      (+ OP1_4 OP2_F Rs Rd)
   1302      (set-psw-sub Rd (index-of Rd) Rd Rs psw-cy)
   1303      ()
   1304 )
   1305 
   1306 (dni sbcgrimm4
   1307      "SUB carry and 4-bit immediate from general register"
   1308      ()
   1309      ("sbc $Rd,#$imm4")
   1310      (+ OP1_5 OP2_7 imm4 Rd)
   1311      (set-psw-sub Rd (index-of Rd) Rd imm4 psw-cy)
   1312      ()
   1313 )
   1314 
   1315 (dni sbcgrimm8
   1316      "SUB carry and 8-bit immediate"
   1317      ()
   1318      ("sbc Rx,#$imm8")
   1319      (+ OP1_5 OP2_F imm8)
   1320      (set-psw-sub (reg HI h-gr Rpsw) Rpsw (reg HI h-gr Rpsw) imm8 psw-cy)
   1321      ()
   1322 )
   1323 
   1324 (dni sbcgrimm16
   1325      "SUB carry and 16-bit immediate from general register"
   1326      ()
   1327      ("sbc $Rd,#$imm16")
   1328      (+ OP1_3 OP2_1 OP3_7 Rd imm16)
   1329      (set-psw-sub Rd (index-of Rd) Rd imm16 psw-cy)
   1330      ()
   1331 )
   1332 
   1333 (dnmi incgr
   1334      "Increment general register"
   1335      ()
   1336      ("inc $Rd")
   1337      (emit incgrimm2 Rd (imm2 0))
   1338 )
   1339 
   1340 (dni incgrimm2
   1341      "Increment general register by 2-bit immediate"
   1342      ()
   1343      ("inc $Rd,#$imm2")
   1344      (+ OP1_3 OP2_0 OP3A_0 imm2 Rd)
   1345      (set-psw Rd (index-of Rd) (add Rd (add imm2 1)) 1)
   1346      ()
   1347 )
   1348 
   1349 (dnmi decgr
   1350      "Decrement general register"
   1351      ()
   1352      ("dec $Rd")
   1353      (emit decgrimm2 Rd (imm2 0))
   1354 )
   1355 
   1356 (dni decgrimm2
   1357      "Decrement general register by 2-bit immediate"
   1358      ()
   1359      ("dec $Rd,#$imm2")
   1360      (+ OP1_3 OP2_0 OP3A_1 imm2 Rd)
   1361      (set-psw Rd (index-of Rd) (sub Rd (add imm2 1)) 1)
   1362      ()
   1363 )
   1364 
   1366 ; Logical Shift
   1367 (dni rrcgrgr
   1368      "Rotate right general register by general register"
   1369      ()
   1370      ("rrc $Rd,$Rs")
   1371      (+ OP1_3 OP2_8 Rs Rd)
   1372      (set-psw-rrotate17 Rd (index-of Rd) Rd psw-cy Rs)
   1373      ()
   1374 )
   1375 
   1376 (dni rrcgrimm4
   1377      "Rotate right general register by immediate"
   1378      ()
   1379      ("rrc $Rd,#$imm4")
   1380      (+ OP1_3 OP2_9 imm4 Rd)
   1381      (set-psw-rrotate17 Rd (index-of Rd) Rd psw-cy imm4)
   1382      ()
   1383 )
   1384 
   1385 (dni rlcgrgr
   1386      "Rotate left general register by general register"
   1387      ()
   1388      ("rlc $Rd,$Rs")
   1389      (+ OP1_3 OP2_A Rs Rd)
   1390      (set-psw-rotate17 Rd (index-of Rd) Rd psw-cy (and Rs #xF))
   1391      ()
   1392 )
   1393 
   1394 (dni rlcgrimm4
   1395      "Rotate left general register by immediate"
   1396      ()
   1397      ("rlc $Rd,#$imm4")
   1398      (+ OP1_3 OP2_B imm4 Rd)
   1399      (set-psw-rotate17 Rd (index-of Rd) Rd psw-cy imm4)
   1400      ()
   1401 )
   1402 
   1403 (dni shrgrgr
   1404      "Shift right general register by general register"
   1405      ()
   1406      ("shr $Rd,$Rs")
   1407      (+ OP1_3 OP2_C Rs Rd)
   1408      (set-psw-carry Rd (index-of Rd) 
   1409 		    (srl Rd (and Rs #xF)) 
   1410 		    (and SI (if SI (eq (and Rs #xF) 0)
   1411 			     psw-cy
   1412 			     (srl Rd (sub (and Rs #xF) 1)))
   1413 			 1) 1)
   1414      ()
   1415 )
   1416 
   1417 (dni shrgrimm
   1418      "Shift right general register by immediate"
   1419      ()
   1420      ("shr $Rd,#$imm4")
   1421      (+ OP1_3 OP2_D imm4 Rd)
   1422      (set-psw-carry Rd (index-of Rd) 
   1423 		    (srl Rd imm4) 
   1424 		    (and SI (if SI (eq imm4 0)
   1425 			     psw-cy
   1426 			     (srl Rd (sub imm4 1)))
   1427 			 1) 1)
   1428      ()
   1429 )
   1430 
   1431 (dni shlgrgr
   1432      "Shift left general register by general register"
   1433      ()
   1434      ("shl $Rd,$Rs")
   1435      (+ OP1_3 OP2_E Rs Rd)
   1436      (set-psw-carry Rd (index-of Rd) 
   1437 		    (sll Rd (and Rs #xF)) 
   1438 		    (srl SI (if SI (eq (and Rs #xF) 0)
   1439 			     (sll psw-cy 15)
   1440 			     (sll Rd (sub (and Rs #xF) 1)))
   1441 			 15) 1)
   1442      ()
   1443 )
   1444 
   1445 (dni shlgrimm
   1446      "Shift left general register by immediate"
   1447      ()
   1448      ("shl $Rd,#$imm4")
   1449      (+ OP1_3 OP2_F imm4 Rd)
   1450      (set-psw-carry Rd (index-of Rd) 
   1451 		    (sll Rd imm4) 
   1452 		    (srl SI (if SI (eq imm4 0)
   1453 			     (sll psw-cy 15)
   1454 			     (sll Rd (sub imm4 1)))
   1455 			 15) 1)
   1456      ()
   1457 )
   1458 
   1459 (dni asrgrgr
   1460      "Arithmetic shift right general register by general register"
   1461      ()
   1462      ("asr $Rd,$Rs")
   1463      (+ OP1_3 OP2_6 Rs Rd)
   1464      (set-psw-carry Rd (index-of Rd) 
   1465 		    (sra HI Rd (and Rs #xF)) 
   1466 		    (and SI (if SI (eq (and Rs #xF) 0)
   1467 			     psw-cy
   1468 			     (srl Rd (sub (and Rs #xF) 1)))
   1469 			 1) 1)
   1470      ()
   1471 )
   1472 
   1473 (dni asrgrimm
   1474      "Arithmetic shift right general register by immediate"
   1475      ()
   1476      ("asr $Rd,#$imm4")
   1477      (+ OP1_3 OP2_7 imm4 Rd)
   1478      (set-psw-carry Rd (index-of Rd) 
   1479 		    (sra HI Rd imm4) 
   1480 		    (and SI (if SI (eq imm4 0)
   1481 			     psw-cy
   1482 			     (srl Rd (sub imm4 1)))
   1483 			 1) 1)
   1484      ()
   1485 )
   1486 
   1488 ; Bitwise operations
   1489 (dni set1grimm
   1490      "Set bit in general register by immediate"
   1491      ()
   1492      ("set1 $Rd,#$imm4")
   1493      (+ OP1_0 OP2_9 imm4 Rd)
   1494      (set-psw Rd (index-of Rd) (or Rd (sll 1 imm4)) 1)
   1495      ()
   1496 )
   1497 
   1498 (dni set1grgr
   1499      "Set bit in general register by general register"
   1500      ()
   1501      ("set1 $Rd,$Rs")
   1502      (+ OP1_0 OP2_B Rs Rd)
   1503      (set-psw Rd (index-of Rd) (or Rd (sll 1 (and Rs #xF))) 1)
   1504      ()
   1505 )
   1506 
   1507 (dni set1lmemimm
   1508      "Set bit in low memory by immediate"
   1509      ()
   1510      ("set1 $lmem8,#$imm3")
   1511      (+ OP1_E imm3 OP2M_1 lmem8)
   1512      (set-mem-psw (mem QI lmem8) (or (mem QI lmem8) (sll 1 imm3)) 0)
   1513      ()
   1514 )
   1515 (dni set1hmemimm
   1516      "Set bit in high memory by immediate"
   1517      ()
   1518      ("set1 $hmem8,#$imm3")
   1519      (+ OP1_F imm3 OP2M_1 hmem8)
   1520      (set-mem-psw (mem QI hmem8) (or (mem QI hmem8) (sll 1 imm3)) 0)
   1521      ()
   1522 )
   1523 
   1524 (dni clr1grimm
   1525      "Clear bit in general register by immediate"
   1526      ()
   1527      ("clr1 $Rd,#$imm4")
   1528      (+ OP1_0 OP2_8 imm4 Rd)
   1529      (set-psw Rd (index-of Rd) (and Rd (inv (sll 1 imm4))) 1)
   1530      ()
   1531 )
   1532 
   1533 (dni clr1grgr
   1534      "Clear bit in general register by general register"
   1535      ()
   1536      ("clr1 $Rd,$Rs")
   1537      (+ OP1_0 OP2_A Rs Rd)
   1538      (set-psw Rd (index-of Rd) (and Rd (inv (sll 1 (and Rs #xF)))) 1)
   1539      ()
   1540 )
   1541 
   1542 (dni clr1lmemimm
   1543      "Clear bit in low memory"
   1544      ()
   1545      ("clr1 $lmem8,#$imm3")
   1546      (+ OP1_E imm3 OP2M_0 lmem8)
   1547      (set-mem-psw (mem QI lmem8) (and (mem QI lmem8) (inv (sll 1 imm3))) 0)
   1548      ()
   1549 )
   1550 (dni clr1hmemimm
   1551      "Clear bit in high memory"
   1552      ()
   1553      ("clr1 $hmem8,#$imm3")
   1554      (+ OP1_F imm3 OP2M_0 hmem8)
   1555      (set-mem-psw (mem QI hmem8) (and (mem QI hmem8) (inv (sll 1 imm3))) 0)
   1556      ()
   1557 )
   1558 
   1559 ; Data conversion
   1560 
   1561 (dni cbwgr
   1562      "Sign-extend byte in general register"
   1563      ()
   1564      ("cbw $Rd")
   1565      (+ OP1_3 OP2_0 OP3_A Rd)
   1566      (set-psw Rd (index-of Rd) (ext HI (trunc QI Rd)) 1)
   1567      ()
   1568 )
   1569 
   1570 (dni revgr
   1571      "Reverse bit pattern in general register"
   1572      ()
   1573      ("rev $Rd")
   1574      (+ OP1_3 OP2_0 OP3_F Rd)
   1575      (set-psw Rd (index-of Rd)
   1576        (or (sll (and Rd #x0001) 15)
   1577        (or (sll (and Rd #x0002) 13)
   1578        (or (sll (and Rd #x0004) 11)
   1579        (or (sll (and Rd #x0008) 9)
   1580        (or (sll (and Rd #x0010) 7)
   1581        (or (sll (and Rd #x0020) 5)
   1582        (or (sll (and Rd #x0040) 3)
   1583        (or (sll (and Rd #x0080) 1)
   1584        (or (srl (and Rd #x0100) 1)
   1585        (or (srl (and Rd #x0200) 3)
   1586        (or (srl (and Rd #x0400) 5)
   1587        (or (srl (and Rd #x0800) 7)
   1588        (or (srl (and Rd #x1000) 9)
   1589        (or (srl (and Rd #x2000) 11)
   1590        (or (srl (and Rd #x4000) 13)
   1591            (srl (and Rd #x8000) 15))))))))))))))))
   1592        1)
   1593      ()
   1594 )
   1595 
   1596 ; Conditional Branches
   1597 
   1598 (define-pmacro (cbranch cond dest)
   1599   (sequence ((BI tmp))
   1600 	    (case cond
   1601 	      ((0)  (set tmp (not (xor psw-s psw-ov))))			; ge
   1602 	      ((1)  (set tmp (not psw-cy)))				; nc
   1603 	      ((2)  (set tmp (xor psw-s psw-ov)))			; lt
   1604 	      ((3)  (set tmp psw-cy))					; c
   1605 	      ((4)  (set tmp (not (or (xor psw-s psw-ov) psw-z16))))	; gt
   1606 	      ((5)  (set tmp (not (or psw-cy psw-z16))))		; hi
   1607 	      ((6)  (set tmp (or (xor psw-s psw-ov) psw-z16)))		; le
   1608 	      ((7)  (set tmp (or psw-cy psw-z16)))			; ls
   1609 	      ((8)  (set tmp (not psw-s)))				; pl
   1610 	      ((9)  (set tmp (not psw-ov)))				; nv
   1611 	      ((10) (set tmp psw-s))					; mi
   1612 	      ((11) (set tmp psw-ov))					; v
   1613 	      ((12) (set tmp (not psw-z8)))				; nz.b
   1614 	      ((13) (set tmp (not psw-z16)))				; nz
   1615 	      ((14) (set tmp psw-z8))					; z.b
   1616 	      ((15) (set tmp psw-z16)))					; z
   1617 	    (if tmp (set pc dest)))
   1618 )
   1619 
   1620 (dni bccgrgr
   1621      "Conditional branch comparing general register with general register"
   1622      ()
   1623      ("b$bcond5 $Rd,$Rs,$rel12")
   1624      (+ OP1_0 OP2_D Rs Rd bcond5 rel12)
   1625      (sequence ()
   1626 	       (set-psw-cmp Rd (index-of Rd) Rd Rs)
   1627 	       (cbranch bcond5 rel12))
   1628      ()
   1629 )
   1630 
   1631 ; 4 bytes
   1632 (dni bccgrimm8
   1633      "Conditional branch comparing general register with 8-bit immediate"
   1634      ()
   1635      ("b$bcond5 $Rm,#$imm8,$rel12")
   1636      (+ OP1_2 OP2M_0 Rm imm8 bcond5 rel12)
   1637      (sequence ()
   1638 	       (set-psw-cmp Rm (index-of Rm) Rm imm8)
   1639 	       (cbranch bcond5 rel12))
   1640      ()
   1641 )
   1642 
   1643 ; 4 bytes
   1644 (dni bccimm16
   1645      "Conditional branch comparing general register with 16-bit immediate"
   1646      ()
   1647      ("b$bcond2 Rx,#$imm16,${rel8-4}")
   1648      (+ OP1_C bcond2 rel8-4 imm16)
   1649      (sequence ()
   1650 	       (set-psw-cmp (reg HI h-gr Rpsw) Rpsw (reg HI h-gr Rpsw) imm16)
   1651 	       (cbranch bcond2 rel8-4))
   1652      ()
   1653 )
   1654 
   1655 (dni bngrimm4
   1656      "Test bit in general register by immediate and branch if 0"
   1657      ()
   1658      ("bn $Rd,#$imm4,$rel12")
   1659      (+ OP1_0 OP2_4 imm4 Rd OP5_0 rel12)
   1660      (sequence ()
   1661 	       (set Rpsw (index-of Rd))
   1662 	       (if (eq (and Rd (sll 1 imm4)) 0)
   1663 		   (set pc rel12)))
   1664      ()
   1665 )
   1666 
   1667 (dni bngrgr
   1668      "Test bit in general register by general register and branch if 0"
   1669      ()
   1670      ("bn $Rd,$Rs,$rel12")
   1671      (+ OP1_0 OP2_6 Rs Rd OP5_0 rel12)
   1672      (sequence ()
   1673 	       (set Rpsw (index-of Rd))
   1674 	       (if (eq (and Rd (sll 1 Rs)) 0)
   1675 		   (set pc rel12)))
   1676      ()
   1677 )
   1678 
   1679 (dni bnlmemimm
   1680      "Test bit in memory by immediate and branch if 0"
   1681      ()
   1682      ("bn $lmem8,#$imm3b,$rel12")
   1683      (+ OP1_7 OP2_C lmem8 OP5A_0 imm3b rel12)
   1684      (if (eq (and (mem QI lmem8) (sll 1 imm3b)) 0)
   1685 	 (set pc rel12))
   1686      ()
   1687 )
   1688 
   1689 (dni bnhmemimm
   1690      "Test bit in memory by immediate and branch if 0"
   1691      ()
   1692      ("bn $hmem8,#$imm3b,$rel12")
   1693      (+ OP1_7 OP2_E hmem8 OP5A_0 imm3b rel12)
   1694      (if (eq (and (mem QI hmem8) (sll 1 imm3b)) 0)
   1695 	 (set pc rel12))
   1696      ()
   1697 )
   1698 
   1699 (dni bpgrimm4
   1700      "Test bit in general register by immediate and branch if 1"
   1701      ()
   1702      ("bp $Rd,#$imm4,$rel12")
   1703      (+ OP1_0 OP2_5 imm4 Rd OP5_0 rel12)
   1704      (sequence ()
   1705 	       (set Rpsw (index-of Rd))
   1706 	       (if (ne (and Rd (sll 1 imm4)) 0)
   1707 		   (set pc rel12)))
   1708      ()
   1709 )
   1710 
   1711 (dni bpgrgr
   1712      "Test bit in general register by general register and branch if 1"
   1713      ()
   1714      ("bp $Rd,$Rs,$rel12")
   1715      (+ OP1_0 OP2_7 Rs Rd OP5_0 rel12)
   1716      (sequence ()
   1717 	       (set Rpsw (index-of Rd))
   1718 	       (if (ne (and Rd (sll 1 Rs)) 0)
   1719 		   (set pc rel12)))
   1720      ()
   1721 )
   1722 
   1723 (dni bplmemimm
   1724      "Test bit in memory by immediate and branch if 1"
   1725      ()
   1726      ("bp $lmem8,#$imm3b,$rel12")
   1727      (+ OP1_7 OP2_D lmem8 OP5A_0 imm3b rel12)
   1728      (if (ne (and (mem QI lmem8) (sll 1 imm3b)) 0)
   1729 	 (set pc rel12))
   1730      ()
   1731 )
   1732 
   1733 (dni bphmemimm
   1734      "Test bit in memory by immediate and branch if 1"
   1735      ()
   1736      ("bp $hmem8,#$imm3b,$rel12")
   1737      (+ OP1_7 OP2_F hmem8 OP5A_0 imm3b rel12)
   1738      (if (ne (and (mem QI hmem8) (sll 1 imm3b)) 0)
   1739 	 (set pc rel12))
   1740      ()
   1741 )
   1742 
   1743 (dni bcc
   1744      "Conditional branch on flag registers"
   1745      ()
   1746      ("b$bcond2 ${rel8-2}")
   1747      (+ OP1_D bcond2 rel8-2)
   1748      (cbranch bcond2 rel8-2)
   1749      ()
   1750 )
   1751 
   1752 ; Unconditional Branching
   1753 
   1754 (dni bgr
   1755      "Branch to register"
   1756      ()
   1757      ("br $Rd")
   1758      (+ OP1_0 OP2_0 OP3_2 Rd)
   1759      (set pc (add (add pc 2) Rd))
   1760      ()
   1761 )
   1762 
   1763 (dni br
   1764      "Branch"
   1765      ()
   1766      ("br $rel12a")
   1767      (+ OP1_1 rel12a OP4B_0)
   1768      (set pc rel12a)
   1769      ()
   1770 )
   1771 
   1772 (dni jmp
   1773      "Jump"
   1774      ()
   1775      ("jmp $Rbj,$Rd")
   1776      (+ OP1_0 OP2_0 OP3B_4 Rbj Rd)
   1777      (set pc (join SI HI Rbj Rd))
   1778      ()
   1779 )
   1780 
   1781 (dni jmpf
   1782      "Jump far"
   1783      ()
   1784      ("jmpf $abs24")
   1785      (+ OP1_0 OP2_2 abs24)
   1786      (set pc abs24)
   1787      ()
   1788 )
   1789 
   1790 ; Call instructions
   1791 (define-pmacro (do-call dest ilen)
   1792   (sequence ()
   1793 	    (set (mem SI sp) (add pc ilen))
   1794 	    (set sp (add sp 4))
   1795 	    (set pc dest)))
   1796 
   1797 (dni callrgr
   1798      "Call relative to general register"
   1799      ()
   1800      ("callr $Rd")
   1801      (+ OP1_0 OP2_0 OP3_1 Rd)
   1802      (do-call (add Rd (add pc 2)) 2)
   1803      ()
   1804 )
   1805 
   1806 (dni callrimm
   1807      "Call relative to immediate address"
   1808      ()
   1809      ("callr $rel12a")
   1810      (+ OP1_1 rel12a OP4B_1)
   1811      (do-call rel12a 2)
   1812      ()
   1813 )
   1814 
   1815 (dni callgr
   1816      "Call to general registers"
   1817      ()
   1818      ("call $Rbj,$Rd")
   1819      (+ OP1_0 OP2_0 OP3B_A Rbj Rd)
   1820      (do-call (join SI HI Rbj Rd) 2)
   1821      ()
   1822 )
   1823 
   1824 (dni callfimm
   1825      "Call far to absolute address"
   1826      ()
   1827      ("callf $abs24")
   1828      (+ OP1_0 OP2_1 abs24)
   1829      (do-call abs24 4)
   1830      ()
   1831 )
   1832 
   1833 (define-pmacro (do-calli dest ilen)
   1834   (sequence ()
   1835 	    (set (mem SI sp) (add pc ilen))
   1836 	    (set (mem HI (add sp 4)) psw)
   1837 	    (set sp (add sp 6))
   1838 	    (set pc dest)))
   1839 
   1840 (dni icallrgr
   1841      "Call interrupt to general registers pc-relative"
   1842      ()
   1843      ("icallr $Rd")
   1844      (+ OP1_0 OP2_0 OP3_3 Rd)
   1845      (do-calli (add Rd (add pc 2)) 2)
   1846      ()
   1847 )
   1848 
   1849 (dni icallgr
   1850      "Call interrupt to general registers"
   1851      ()
   1852      ("icall $Rbj,$Rd")
   1853      (+ OP1_0 OP2_0 OP3B_6 Rbj Rd)
   1854      (do-calli (join SI HI Rbj Rd) 2)
   1855      ()
   1856 )
   1857 
   1858 (dni icallfimm
   1859      "Call interrupt far to absolute address"
   1860      ()
   1861      ("icallf $abs24")
   1862      (+ OP1_0 OP2_3 abs24)
   1863      (do-calli abs24 4)
   1864      ()
   1865 )
   1866 
   1867 ; Return instructions
   1868 (dni iret
   1869      "Return from interrupt"
   1870      ()
   1871      ("iret")
   1872      (+ (f-op #x0002))
   1873      (sequence ()
   1874 	       (set sp (sub sp 6))
   1875 	       (set pc (mem SI sp))
   1876 	       (set psw (mem HI (add sp 4))))
   1877      ()
   1878 )
   1879 
   1880 (dni ret
   1881      "Return"
   1882      ()
   1883      ("ret")
   1884      (+ (f-op #x0003))
   1885      (sequence ()
   1886 	       (set sp (sub sp 4))
   1887 	       (set pc (mem SI sp)))
   1888      ()
   1889 )
   1890 
   1891 ; Multiply and Divide instructions
   1892 
   1893 (dni mul
   1894      "Multiply"
   1895      ()
   1896      ("mul")
   1897      (+ (f-op #x00D0))
   1898      (sequence ((SI value))
   1899 	       (set value (mul SI (and SI R0 #xFFFF) (and SI R2 #xFFFF)))
   1900 	       (set psw (or (and psw #xFF9C)
   1901 			    (basic-psw (trunc HI value) 1)))
   1902 	       (set R0 (trunc HI value))
   1903 	       (set R1 (trunc HI (srl value 16))))
   1904      ()
   1905 )
   1906 (dni div
   1907      "Divide"
   1908      ()
   1909      ("div")
   1910      (+ (f-op #x00C0))
   1911      (sequence ()
   1912 	       (set R1 (umod R0 R2))
   1913 	       (set-mem-psw R0 (udiv R0 R2) 1))
   1914      ()
   1915 )
   1916 (dni sdiv
   1917      "Signed Divide"
   1918      ()
   1919      ("sdiv")
   1920      (+ (f-op #x00C8))
   1921      (sequence ()
   1922 	       (set R1 (mod HI R0 R2))
   1923 	       (set-mem-psw R0 (div HI R0 R2) 1))
   1924      ()
   1925 )
   1926 (dni sdivlh
   1927      "Divide 32/16"
   1928      ()
   1929      ("sdivlh")
   1930      (+ (f-op #x00E8))
   1931      (sequence ((SI value))
   1932 	       (set value (add SI (sll SI (and SI R1 #xffff) #x10) (and SI R0 #xffff)))
   1933 	       (set R1 (mod SI value (ext SI (trunc HI R2))))
   1934 	       (set-mem-psw R0 (div SI value (ext SI (trunc HI R2))) 1))
   1935      ()
   1936 )
   1937 (dni divlh
   1938      "Divide 32/16"
   1939      ()
   1940      ("divlh")
   1941      (+ (f-op #x00E0))
   1942      (sequence ((SI value))
   1943 	       (set value (add SI (sll SI (and SI R1 #xffff) #x10) (and SI R0 #xffff)))
   1944 	       (set R1 (umod SI value R2))
   1945 	       (set-mem-psw R0 (udiv SI value R2) 1))
   1946      ()
   1947 )
   1948 
   1949 ; System Control
   1950 
   1951 ; added per sanyo's req -- eq to nop for the moment, but can 
   1952 ; add function later
   1953 (dni reset "reset" () ("reset") (+ (f-op #x000f)) (nop) ())
   1954 
   1955 (dni nop "nop" () ("nop") (+ (f-op #x0000)) (nop) ())
   1956 
   1957 (dni halt "halt" () ("halt") (+ (f-op #x0008)) (c-call VOID "do_halt") ())
   1958 
   1959 (dni hold "hold" () ("hold") (+ (f-op #x000A)) (c-call VOID "do_hold") ())
   1960 
   1961 (dni holdx "holdx" () ("holdx") (+ (f-op #x000B)) (c-call VOID "do_holdx") ())
   1962 
   1963 (dni brk "brk" () ("brk") (+ (f-op #x0005)) (c-call VOID "do_brk") ())
   1964 
   1965 ; An instruction for test instrumentation.
   1966 ; Using a reserved opcode.
   1967 (dni syscall
   1968   "simulator system call"
   1969   ()
   1970   ("--unused--")
   1971   (+ (f-op #x0001))
   1972   (c-call VOID "syscall")
   1973   ()
   1974 )
   1975