105caefcfSchristos; xstormy16 CPU core description. -*- Scheme -*-
205caefcfSchristos; Copyright 2011 Free Software Foundation, Inc.
305caefcfSchristos;
405caefcfSchristos; Contributed by Red Hat Inc;
505caefcfSchristos;
605caefcfSchristos; This file is part of the GNU Binutils.
705caefcfSchristos;
805caefcfSchristos; This program is free software; you can redistribute it and/or modify
905caefcfSchristos; it under the terms of the GNU General Public License as published by
1005caefcfSchristos; the Free Software Foundation; either version 3 of the License, or
1105caefcfSchristos; (at your option) any later version.
1205caefcfSchristos;
1305caefcfSchristos; This program is distributed in the hope that it will be useful,
1405caefcfSchristos; but WITHOUT ANY WARRANTY; without even the implied warranty of
1505caefcfSchristos; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1605caefcfSchristos; GNU General Public License for more details.
1705caefcfSchristos;
1805caefcfSchristos; You should have received a copy of the GNU General Public License
1905caefcfSchristos; along with this program; if not, write to the Free Software
2005caefcfSchristos; Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
2105caefcfSchristos; MA 02110-1301, USA.
2205caefcfSchristos
2305caefcfSchristos(define-rtl-version 0 8)
2405caefcfSchristos
2505caefcfSchristos(include "simplify.inc")
2605caefcfSchristos
2705caefcfSchristos(define-arch
2805caefcfSchristos  (name xstormy16)
2905caefcfSchristos  (comment "Xstormy16 architecture")
3005caefcfSchristos  (insn-lsb0? #f)
3105caefcfSchristos  (machs xstormy16)
3205caefcfSchristos  (isas xstormy16)
3305caefcfSchristos)
3405caefcfSchristos
3505caefcfSchristos(define-isa
3605caefcfSchristos  (name xstormy16)
3705caefcfSchristos  (comment "Xstormy16 instruction set")
3805caefcfSchristos  (default-insn-word-bitsize 32)
3905caefcfSchristos  (default-insn-bitsize 32)
4005caefcfSchristos  ; FIXME base-insn-bitsize should be 16 too, but at present CGEN has
4105caefcfSchristos  ; no support for instruction sets with opcode bits past
4205caefcfSchristos  ; base-insn-bitsize, so we must set it to at least 20.
4305caefcfSchristos  (base-insn-bitsize 32)
4405caefcfSchristos)
4505caefcfSchristos
4605caefcfSchristos(define-cpu
4705caefcfSchristos  (name xstormy16)
4805caefcfSchristos  (comment "Xstormy16 CPU core")
4905caefcfSchristos  (endian little)
5005caefcfSchristos  (insn-endian little)
5105caefcfSchristos  (insn-chunk-bitsize 16)
5205caefcfSchristos  (word-bitsize 32)
5305caefcfSchristos)
5405caefcfSchristos
5505caefcfSchristos(define-mach
5605caefcfSchristos  (name xstormy16)
5705caefcfSchristos  (comment "Xstormy16 CPU core")
5805caefcfSchristos  (cpu xstormy16)
5905caefcfSchristos  (isas xstormy16)
6005caefcfSchristos)
6105caefcfSchristos
6205caefcfSchristos(define-model
6305caefcfSchristos  (name xstormy16)
6405caefcfSchristos  (comment "Xstormy16 CPU core")
6505caefcfSchristos  (unit u-exec "Execution Unit" ()
6605caefcfSchristos	1 1 ; issue done
6705caefcfSchristos	() () () ())
6805caefcfSchristos)
6905caefcfSchristos
7005caefcfSchristos; IDOC attribute for instruction documentation.
7105caefcfSchristos
7205caefcfSchristos(define-attr
7305caefcfSchristos  (for insn)
7405caefcfSchristos  (type enum)
7505caefcfSchristos  (name IDOC)
7605caefcfSchristos  (comment "insn kind for documentation")
7705caefcfSchristos  (attrs META)
7805caefcfSchristos  (values
7905caefcfSchristos   (MEM - () "Memory")
8005caefcfSchristos   (ALU - () "ALU")
8105caefcfSchristos   (FPU - () "FPU")
8205caefcfSchristos   (BR - () "Branch")
8305caefcfSchristos   (PRIV - () "Priviledged")
8405caefcfSchristos   (MISC - () "Miscellaneous")
8505caefcfSchristos  )
8605caefcfSchristos)
8705caefcfSchristos
8805caefcfSchristos; Hardware elements.
8905caefcfSchristos
9005caefcfSchristos(define-hardware
9105caefcfSchristos  (name h-pc)
9205caefcfSchristos  (comment "program counter")
9305caefcfSchristos  (attrs PC)
9405caefcfSchristos  (type pc)
9505caefcfSchristos  (set (newval) (c-call "h_pc_set_handler" newval))
9605caefcfSchristos)
9705caefcfSchristos
9805caefcfSchristos(define-keyword
9905caefcfSchristos  (name gr-names)
10005caefcfSchristos  (enum-prefix H-GR-)
10105caefcfSchristos  (values (r0 0) (r1 1) (r2 2) (r3 3)
10205caefcfSchristos	  (r4 4) (r5 5) (r6 6) (r7 7)
10305caefcfSchristos	  (r8 8) (r9 9) (r10 10) (r11 11)
10405caefcfSchristos	  (r12 12) (r13 13) (r14 14) (r15 15)
10505caefcfSchristos	  (psw 14) (sp 15)))
10605caefcfSchristos
10705caefcfSchristos(define-keyword
10805caefcfSchristos  (name gr-Rb-names)
10905caefcfSchristos  (enum-prefix H-RBJ-)
11005caefcfSchristos  (values (r8 0) (r9 1) (r10 2) (r11 3)
11105caefcfSchristos	  (r12 4) (r13 5) (r14 6) (r15 7)
11205caefcfSchristos	  (psw 6) (sp 7)))
11305caefcfSchristos
11405caefcfSchristos(define-hardware
11505caefcfSchristos  (name h-gr)
11605caefcfSchristos  (comment "registers")
11705caefcfSchristos  (type register WI (16))
11805caefcfSchristos  (indices extern-keyword gr-names)
11905caefcfSchristos  (get (index) (and #xFFFF (raw-reg h-gr index)))
12005caefcfSchristos  (set (index newval) (c-call "h_gr_set_handler" index newval))
12105caefcfSchristos)
12205caefcfSchristos
12305caefcfSchristos(define-hardware
12405caefcfSchristos  (name h-Rb)
12505caefcfSchristos  (comment "Rb registers")
12605caefcfSchristos  (attrs VIRTUAL)
12705caefcfSchristos  (type register SI(8))
12805caefcfSchristos  (indices extern-keyword gr-Rb-names)
12905caefcfSchristos  (get (index) (reg h-gr (add index 8)))
13005caefcfSchristos  (set (index newval) (set (reg h-gr (add index 8)) newval))
13105caefcfSchristos)
13205caefcfSchristos
13305caefcfSchristos(define-hardware
13405caefcfSchristos  (name h-Rbj)
13505caefcfSchristos  (comment "Rbj registers")
13605caefcfSchristos  (attrs VIRTUAL)
13705caefcfSchristos  (type register SI(2))
13805caefcfSchristos  (indices extern-keyword gr-Rb-names)
13905caefcfSchristos  (get (index) (reg h-gr (add index 8)))
14005caefcfSchristos  (set (index newval) (set (reg h-gr (add index 8)) newval))
14105caefcfSchristos)
14205caefcfSchristos
14305caefcfSchristos(define-hardware
14405caefcfSchristos  (name h-Rpsw)
14505caefcfSchristos  (comment "Register number field of the PSW")
14605caefcfSchristos  (attrs VIRTUAL)
14705caefcfSchristos  (type register WI)
14805caefcfSchristos  (get () (and #xF (srl psw 12)))
14905caefcfSchristos  (set (newval) (set psw (or (and psw #xFFF)
15005caefcfSchristos			     (sll HI newval 12)))))
15105caefcfSchristos
15205caefcfSchristos(define-pmacro (define-psw-field fnam hnam index)
15305caefcfSchristos  (define-hardware
15405caefcfSchristos    (name hnam)
15505caefcfSchristos    (attrs VIRTUAL)
15605caefcfSchristos    (type register SI)
15705caefcfSchristos    (get () (and 1 (srl psw index)))
15805caefcfSchristos    (set (newval) (set psw (or (and psw (inv (sll HI 1 index)))
15905caefcfSchristos			       (sll HI newval index)))))
16005caefcfSchristos  ;(dnop fnam "" (SEM-ONLY) hnam f-nil)
16105caefcfSchristos)
16205caefcfSchristos(define-psw-field psw-z8  h-z8   0)
16305caefcfSchristos(dnop psw-z8 "" (SEM-ONLY) h-z8 f-nil)
16405caefcfSchristos(define-psw-field psw-z16 h-z16  1)
16505caefcfSchristos(dnop psw-z16 "" (SEM-ONLY) h-z16 f-nil)
16605caefcfSchristos(define-psw-field psw-cy  h-cy   2)
16705caefcfSchristos(dnop psw-cy "" (SEM-ONLY) h-cy f-nil)
16805caefcfSchristos(define-psw-field psw-hc  h-hc   3)
16905caefcfSchristos(dnop psw-hc "" (SEM-ONLY) h-hc f-nil)
17005caefcfSchristos(define-psw-field psw-ov  h-ov   4)
17105caefcfSchristos(dnop psw-ov "" (SEM-ONLY) h-ov f-nil)
17205caefcfSchristos(define-psw-field psw-pt  h-pt   5)
17305caefcfSchristos(dnop psw-pt "" (SEM-ONLY) h-pt f-nil)
17405caefcfSchristos(define-psw-field psw-s   h-s    6)
17505caefcfSchristos(dnop psw-s  "" (SEM-ONLY) h-s  f-nil)
17605caefcfSchristos
17705caefcfSchristos(define-hardware
17805caefcfSchristos  (name h-branchcond)
17905caefcfSchristos  (comment "Condition of a branch instruction")
18005caefcfSchristos  (type immediate (UINT 4))
18105caefcfSchristos  (values keyword ""
18205caefcfSchristos	  (("ge" 0) ("nc" 1) ("lt" 2) ("c" 3)
18305caefcfSchristos	   ("gt" 4) ("hi" 5) ("le" 6) ("ls" 7)
18405caefcfSchristos	   ("pl" 8) ("nv" 9) ("mi" 10) ("v" 11)
18505caefcfSchristos	   ("nz.b" 12) ("nz" 13) ("z.b" 14) ("z" 15)))
18605caefcfSchristos)
18705caefcfSchristos
18805caefcfSchristos(define-hardware
18905caefcfSchristos  (name h-wordsize)
19005caefcfSchristos  (comment "Data size")
19105caefcfSchristos  (type immediate (UINT 1))
19205caefcfSchristos  (values keyword "" ((".b" 0) (".w" 1) ("" 1)))
19305caefcfSchristos)
19405caefcfSchristos
19505caefcfSchristos
19605caefcfSchristos; Instruction fields, and the corresponding operands.
19705caefcfSchristos; Register fields
19805caefcfSchristos
19905caefcfSchristos(dnf f-Rd "general register destination" ()  12 4)
20005caefcfSchristos(dnop Rd "general register destination" ()  h-gr f-Rd)
20105caefcfSchristos
20205caefcfSchristos(dnf f-Rdm "general register destination" ()  13 3)
20305caefcfSchristos(dnop Rdm "general register destination" ()  h-gr f-Rdm)
20405caefcfSchristos
20505caefcfSchristos(dnf f-Rm "general register for memory" ()  4 3)
20605caefcfSchristos(dnop Rm "general register for memory" ()  h-gr f-Rm)
20705caefcfSchristos
20805caefcfSchristos(dnf f-Rs  "general register source" ()  8 4)
20905caefcfSchristos(dnop Rs "general register source" ()  h-gr f-Rs)
21005caefcfSchristos
21105caefcfSchristos(dnf f-Rb  "base register" ()  17 3)
21205caefcfSchristos(dnop Rb "base register" ()  h-Rb f-Rb)
21305caefcfSchristos
21405caefcfSchristos(dnf f-Rbj "base register for jump" () 11 1)
21505caefcfSchristos(dnop Rbj "base register for jump" () h-Rbj f-Rbj)
21605caefcfSchristos
21705caefcfSchristos; Main opcodes in 4 bit chunks
21805caefcfSchristos
21905caefcfSchristos(dnf f-op1 "opcode" ()  0 4)
22005caefcfSchristos(define-normal-insn-enum insn-op1 "insn op enums" () OP1_ f-op1
22105caefcfSchristos ( "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F" ))
22205caefcfSchristos
22305caefcfSchristos(dnf f-op2 "opcode" ()  4 4)
22405caefcfSchristos(define-normal-insn-enum insn-op2 "insn op enums" () OP2_ f-op2
22505caefcfSchristos ( "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F" ))
22605caefcfSchristos(dnop bcond2 "branch condition opcode" () h-branchcond f-op2)
22705caefcfSchristos
22805caefcfSchristos(dnf f-op2a "opcode" ()  4 3)
22905caefcfSchristos(define-normal-insn-enum insn-op2a "insn op enums" () OP2A_ f-op2a
23005caefcfSchristos ( "0" "2" "4" "6" "8" "A" "C" "E" ))
23105caefcfSchristos
23205caefcfSchristos(dnf f-op2m "opcode" ()  7 1)
23305caefcfSchristos(define-normal-insn-enum insn-op2m "insn op enums" () OP2M_ f-op2m
23405caefcfSchristos ( "0" "1" ))
23505caefcfSchristos(dnop ws2 "word size opcode" () h-wordsize f-op2m)
23605caefcfSchristos
23705caefcfSchristos(dnf f-op3 "opcode" ()  8 4)
23805caefcfSchristos(define-normal-insn-enum insn-op3 "insn op enums" () OP3_ f-op3
23905caefcfSchristos ( "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F" ))
24005caefcfSchristos
24105caefcfSchristos(dnf f-op3a "opcode" ()  8 2)
24205caefcfSchristos(define-normal-insn-enum insn-op3a "insn op enums" () OP3A_ f-op3a
24305caefcfSchristos ( "0" "1" "2" "3" ))
24405caefcfSchristos
24505caefcfSchristos(dnf f-op3b "opcode" ()  8 3)
24605caefcfSchristos(define-normal-insn-enum insn-op3b "insn op enums" () OP3B_ f-op3b
24705caefcfSchristos ( "0" "2" "4" "6" "8" "A" "C" "E" ))
24805caefcfSchristos
24905caefcfSchristos(dnf f-op4 "opcode" ()  12 4)
25005caefcfSchristos(define-normal-insn-enum insn-op4 "insn op enums" () OP4_ f-op4
25105caefcfSchristos ( "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F" ))
25205caefcfSchristos
25305caefcfSchristos(dnf f-op4m "opcode" () 12 1)
25405caefcfSchristos(define-normal-insn-enum insn-op4m "insn op enums" () OP4M_ f-op4m
25505caefcfSchristos ( "0" "1" ))
25605caefcfSchristos
25705caefcfSchristos(dnf f-op4b "opcode" () 15 1)
25805caefcfSchristos(define-normal-insn-enum insn-op4b "insn op enums" () OP4B_ f-op4b
25905caefcfSchristos ( "0" "1" ))
26005caefcfSchristos
26105caefcfSchristos(dnf f-op5 "opcode" ()  16 4)
26205caefcfSchristos(define-normal-insn-enum insn-op5 "insn op enums" () OP5_ f-op5
26305caefcfSchristos ( "0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "A" "B" "C" "D" "E" "F" ))
26405caefcfSchristos(dnop bcond5 "branch condition opcode" () h-branchcond f-op5)
26505caefcfSchristos
26605caefcfSchristos(dnf f-op5a "opcode" ()  16 1)
26705caefcfSchristos(define-normal-insn-enum insn-op5a "insn op enums" () OP5A_ f-op5a
26805caefcfSchristos ( "0" "1" ))
26905caefcfSchristos
27005caefcfSchristos; The whole first word
27105caefcfSchristos(dnf f-op "opcode" () 0 16)
27205caefcfSchristos
27305caefcfSchristos; Immediate fields
27405caefcfSchristos
27505caefcfSchristos(dnf f-imm2  "2 bit unsigned" () 10 2)
27605caefcfSchristos(dnop imm2 "2 bit unsigned immediate" () h-uint f-imm2)
27705caefcfSchristos
27805caefcfSchristos(dnf f-imm3  "3 bit unsigned" () 4 3)
27905caefcfSchristos(dnop imm3 "3 bit unsigned immediate" () h-uint f-imm3)
28005caefcfSchristos(dnf f-imm3b  "3 bit unsigned for bit tests" () 17 3)
28105caefcfSchristos(dnop imm3b "3 bit unsigned immediate for bit tests" () h-uint f-imm3b)
28205caefcfSchristos
28305caefcfSchristos(dnf f-imm4  "4 bit unsigned" () 8 4)
28405caefcfSchristos(define-operand
28505caefcfSchristos  (name imm4)
28605caefcfSchristos  (comment "4 bit unsigned immediate")
28705caefcfSchristos  (attrs)
28805caefcfSchristos  (type h-uint)
28905caefcfSchristos  (index f-imm4)
29005caefcfSchristos  (handlers (parse "small_immediate"))
29105caefcfSchristos)
29205caefcfSchristos
29305caefcfSchristos(dnf f-imm8  "8 bit unsigned" () 8 8)
29405caefcfSchristos(dnop imm8 "8 bit unsigned immediate" () h-uint f-imm8)
29505caefcfSchristos(define-operand
29605caefcfSchristos  (name imm8small)
29705caefcfSchristos  (comment "8 bit unsigned immediate")
29805caefcfSchristos  (attrs)
29905caefcfSchristos  (type h-uint)
30005caefcfSchristos  (index f-imm8)
30105caefcfSchristos  (handlers (parse "small_immediate"))
30205caefcfSchristos)
30305caefcfSchristos
30405caefcfSchristos(define-ifield
30505caefcfSchristos  (name f-imm12)
30605caefcfSchristos  (comment "12 bit signed")
30705caefcfSchristos  (attrs)
30805caefcfSchristos  (start 20)
30905caefcfSchristos  (length 12)
31005caefcfSchristos  (mode INT)
31105caefcfSchristos)
31205caefcfSchristos(dnop imm12 "12 bit signed immediate" () h-sint f-imm12)
31305caefcfSchristos
31405caefcfSchristos(dnf f-imm16 "16 bit" (SIGN-OPT) 16 16)
31505caefcfSchristos(define-operand
31605caefcfSchristos  (name imm16)
31705caefcfSchristos  (comment "16 bit immediate")
31805caefcfSchristos  (attrs)
31905caefcfSchristos  (type h-uint)
32005caefcfSchristos  (index f-imm16)
32105caefcfSchristos  (handlers (parse "immediate16"))
32205caefcfSchristos)
32305caefcfSchristos
32405caefcfSchristos(dnf f-lmem8  "8 bit unsigned low memory" (ABS-ADDR) 8 8)
32505caefcfSchristos(define-operand
32605caefcfSchristos  (name lmem8)
32705caefcfSchristos  (comment "8 bit unsigned immediate low memory")
32805caefcfSchristos  (attrs)
32905caefcfSchristos  (type h-uint)
33005caefcfSchristos  (index f-lmem8)
33105caefcfSchristos  (handlers (parse "mem8"))
33205caefcfSchristos)
33305caefcfSchristos(define-ifield
33405caefcfSchristos  (name f-hmem8)
33505caefcfSchristos  (comment "8 bit unsigned high memory")
33605caefcfSchristos  (attrs ABS-ADDR)
33705caefcfSchristos  (start 8)
33805caefcfSchristos  (length 8)
33905caefcfSchristos  (mode UINT)
34005caefcfSchristos  (encode (value pc) (sub HI value #x7F00))
34105caefcfSchristos  (decode (value pc) (add HI value #x7F00))
34205caefcfSchristos)
34305caefcfSchristos(define-operand
34405caefcfSchristos  (name hmem8)
34505caefcfSchristos  (comment "8 bit unsigned immediate high memory")
34605caefcfSchristos  (attrs)
34705caefcfSchristos  (type h-uint)
34805caefcfSchristos  (index f-hmem8)
34905caefcfSchristos  (handlers (parse "mem8"))
35005caefcfSchristos)
35105caefcfSchristos
35205caefcfSchristos(define-ifield
35305caefcfSchristos  (name f-rel8-2)
35405caefcfSchristos  (comment "8 bit relative address for 2-byte instruction")
35505caefcfSchristos  (attrs PCREL-ADDR)
35605caefcfSchristos  (start 8)
35705caefcfSchristos  (length 8)
35805caefcfSchristos  (mode INT)
35905caefcfSchristos  (encode (value pc) (sub SI value (add SI pc 2)))
36005caefcfSchristos  (decode (value pc) (add SI value (add SI pc 2)))
36105caefcfSchristos)
36205caefcfSchristos(dnop rel8-2 "8 bit relative address" () h-uint f-rel8-2)
36305caefcfSchristos
36405caefcfSchristos(define-ifield
36505caefcfSchristos  (name f-rel8-4)
36605caefcfSchristos  (comment "8 bit relative address for 4-byte instruction")
36705caefcfSchristos  (attrs PCREL-ADDR)
36805caefcfSchristos  (start 8)
36905caefcfSchristos  (length 8)
37005caefcfSchristos  (mode INT)
37105caefcfSchristos  (encode (value pc) (sub SI value (add SI pc 4)))
37205caefcfSchristos  (decode (value pc) (add SI value (add SI pc 4)))
37305caefcfSchristos)
37405caefcfSchristos(dnop rel8-4 "8 bit relative address" () h-uint f-rel8-4)
37505caefcfSchristos
37605caefcfSchristos(define-ifield
37705caefcfSchristos  (name f-rel12)
37805caefcfSchristos  (comment "12 bit relative address")
37905caefcfSchristos  (attrs PCREL-ADDR)
38005caefcfSchristos  (start 20)
38105caefcfSchristos  (length 12)
38205caefcfSchristos  (mode INT)
38305caefcfSchristos  (encode (value pc) (sub SI value (add SI pc 4)))
38405caefcfSchristos  (decode (value pc) (add SI value (add SI pc 4)))
38505caefcfSchristos)
38605caefcfSchristos(dnop rel12 "12 bit relative address" () h-uint f-rel12)
38705caefcfSchristos
38805caefcfSchristos(define-ifield
38905caefcfSchristos  (name f-rel12a)
39005caefcfSchristos  (comment "12 bit relative address")
39105caefcfSchristos  (attrs PCREL-ADDR)
39205caefcfSchristos  (start 4)
39305caefcfSchristos  (length 11)
39405caefcfSchristos  (mode INT)
39505caefcfSchristos  (encode (value pc) (sra SI (sub SI value (add SI pc 2)) 1))
396*f7172901Schristos  (decode (value pc) (add SI (mul value 2) (add SI pc 2)))
39705caefcfSchristos)
39805caefcfSchristos(dnop rel12a "12 bit relative address" () h-uint f-rel12a)
39905caefcfSchristos
40005caefcfSchristos(dnf f-abs24-1  "abs24 low part" () 8 8)
40105caefcfSchristos(dnf f-abs24-2  "abs24 high part" () 16 16)
40205caefcfSchristos(define-multi-ifield
40305caefcfSchristos  (name f-abs24)
40405caefcfSchristos  (comment "Absolute address for jmpf instruction")
40505caefcfSchristos  (attrs ABS-ADDR)
40605caefcfSchristos  (mode UINT)
40705caefcfSchristos  (subfields f-abs24-1 f-abs24-2)
40805caefcfSchristos  (insert (sequence ()
40905caefcfSchristos		    (set (ifield f-abs24-1) (and (ifield f-abs24) #xFF))
41005caefcfSchristos		    (set (ifield f-abs24-2) (srl (ifield f-abs24) 8))))
41105caefcfSchristos  (extract (set (ifield f-abs24) (or (sll (ifield f-abs24-2) 8) f-abs24-1)))
41205caefcfSchristos)
41305caefcfSchristos(dnop abs24 "24 bit absolute address" () h-uint f-abs24)
41405caefcfSchristos
41505caefcfSchristos; Names for registers
41605caefcfSchristos(dnop psw "program status word" (SEM-ONLY) h-gr 14)
41705caefcfSchristos(dnop Rpsw "N0-N3 of the program status word" (SEM-ONLY) h-Rpsw f-nil)
41805caefcfSchristos(dnop sp "stack pointer" (SEM-ONLY) h-gr 15)
41905caefcfSchristos(dnop R0 "R0" (SEM-ONLY) h-gr 0)
42005caefcfSchristos(dnop R1 "R1" (SEM-ONLY) h-gr 1)
42105caefcfSchristos(dnop R2 "R2" (SEM-ONLY) h-gr 2)
42205caefcfSchristos(dnop R8 "R8" (SEM-ONLY) h-gr 8)
42305caefcfSchristos
42405caefcfSchristos; Useful macros.
42505caefcfSchristos
42605caefcfSchristos; THe Z8, Z16, PT, and S flags of the PSW.
42705caefcfSchristos(define-pmacro (basic-psw value ws)
42805caefcfSchristos  (or (or (zflag (and value #xFF))
42905caefcfSchristos	  (sll HI (zflag HI value) 1))
43005caefcfSchristos      (or (sll HI (c-call BI "parity" value) 5)
43105caefcfSchristos	  (sll HI (nflag QI (srl value (mul ws 8))) 6))))
43205caefcfSchristos
43305caefcfSchristos
43405caefcfSchristos; Update the PSW for destination register Rd, set Rd to value.
43505caefcfSchristos(define-pmacro (set-psw Rd index value ws)
43605caefcfSchristos  (sequence ((HI nvalue))
43705caefcfSchristos    (set nvalue value)
43805caefcfSchristos    (set (reg HI h-gr index) nvalue)
43905caefcfSchristos    (set psw (or (and psw #x0F9C)
44005caefcfSchristos		 (or (sll index 12)
44105caefcfSchristos		     (basic-psw nvalue ws))))))
44205caefcfSchristos
44305caefcfSchristos; Update the PSW for destination register Rd.
44405caefcfSchristos(define-pmacro (set-psw-nowrite index value ws)
44505caefcfSchristos  (sequence ((HI nvalue))
44605caefcfSchristos    (set nvalue value)
44705caefcfSchristos    (set psw (or (and psw #x0F9C)
44805caefcfSchristos		 (or (sll index 12)
44905caefcfSchristos		     (basic-psw nvalue ws))))))
45005caefcfSchristos
45105caefcfSchristos; Update the PSW for destination non-register dest, set dest to value.
45205caefcfSchristos(define-pmacro (set-mem-psw dest value ws)
45305caefcfSchristos  (sequence ((HI nvalue))
45405caefcfSchristos    (set nvalue value)
45505caefcfSchristos    (set psw (or (and psw #xFF9C)
45605caefcfSchristos		 (basic-psw nvalue ws)))
45705caefcfSchristos    (set dest nvalue)))
45805caefcfSchristos
45905caefcfSchristos; Update the PSW as with set-psw, but also set the carry flag.
46005caefcfSchristos(define-pmacro (set-psw-carry Rd index value carry ws)
46105caefcfSchristos  (sequence ((HI nvalue) (HI newpsw))
46205caefcfSchristos    (set nvalue value)
46305caefcfSchristos    (set newpsw (or (or (and psw #x0F98)
46405caefcfSchristos		     (sll (and carry #x1) 2))
46505caefcfSchristos		 (or (sll index 12)
46605caefcfSchristos		     (basic-psw nvalue ws))))
46705caefcfSchristos    (set (reg HI h-gr index) nvalue)
46805caefcfSchristos    (set psw newpsw)
46905caefcfSchristos    ))
47005caefcfSchristos
47105caefcfSchristos; The all-purpose addition operation.
47205caefcfSchristos(define-pmacro (set-psw-add Rd index a b c)
47305caefcfSchristos  (sequence ((HI value) (HI newpsw))
47405caefcfSchristos    (set value (addc a b c))
47505caefcfSchristos    (set newpsw (or (or (and psw #x0F80)
47605caefcfSchristos			(basic-psw value 1))
47705caefcfSchristos		    (or (or (sll HI (add-oflag HI a b c) 4)
47805caefcfSchristos			    (sll HI (add-cflag HI a b c) 2))
47905caefcfSchristos			(or (and (srl HI (addc HI (and a #xF) (and b #xF) c)
48005caefcfSchristos				      1) #x8)
48105caefcfSchristos			    (sll index 12)))))
48205caefcfSchristos    (set (reg HI h-gr index) value)
48305caefcfSchristos    (set psw newpsw)
48405caefcfSchristos    ))
48505caefcfSchristos
48605caefcfSchristos; Set the PSW for a subtraction of a-b into Rd, but don't actually
48705caefcfSchristos; do the subtract.
48805caefcfSchristos(define-pmacro (set-psw-cmp Rd index a b)
48905caefcfSchristos  (sequence ((HI value))
49005caefcfSchristos    (set value (sub a b))
49105caefcfSchristos    (set psw (or (or (and psw #x0F80)
49205caefcfSchristos		     (basic-psw value 1))
49305caefcfSchristos		 (or (or (sll HI (sub-oflag HI a b 0) 4)
49405caefcfSchristos			 (sll HI (sub-cflag HI a b 0) 2))
49505caefcfSchristos		     (or (and (srl HI (sub HI (and a #xF) (and b #xF))
49605caefcfSchristos				   1) #x8)
49705caefcfSchristos			 (sll index 12)))))))
49805caefcfSchristos
49905caefcfSchristos; Likewise, for subtraction
50005caefcfSchristos; (this chip has a borrow for subtraction, rather than
50105caefcfSchristos; just using a carry for both).
50205caefcfSchristos(define-pmacro (set-psw-sub Rd index a b c)
50305caefcfSchristos  (sequence ((HI value) (HI newpsw))
50405caefcfSchristos    (set value (subc a b c))
50505caefcfSchristos    (set newpsw (or (or (and psw #x0F80)
50605caefcfSchristos		     (basic-psw value 1))
50705caefcfSchristos		 (or (or (sll HI (sub-oflag HI a b c) 4)
50805caefcfSchristos			 (sll HI (sub-cflag HI a b c) 2))
50905caefcfSchristos		     (or (and (srl HI (subc HI (and a #xF) (and b #xF) c)
51005caefcfSchristos				   1) #x8)
51105caefcfSchristos			 (sll index 12)))))
51205caefcfSchristos    (set (reg HI h-gr index) value)
51305caefcfSchristos    (set psw newpsw)
51405caefcfSchristos    ))
51505caefcfSchristos
51605caefcfSchristos; A 17-bit rotate-left operation
51705caefcfSchristos(define-pmacro (set-psw-rotate17 Rd index src c rot)
51805caefcfSchristos  (sequence ((SI tmpfoo))
51905caefcfSchristos    (set tmpfoo (or (or (and (sll SI src 15) #x7FFE0000)
52005caefcfSchristos		     src)
52105caefcfSchristos		 (or (sll SI c 31)
52205caefcfSchristos		     (sll SI c 16))))
52305caefcfSchristos    (set tmpfoo (rol tmpfoo (and rot #x1F)))
52405caefcfSchristos    (set-psw-carry (reg HI h-gr index) index (trunc HI tmpfoo) (and (srl tmpfoo 16) 1) 1)))
52505caefcfSchristos
52605caefcfSchristos; A 17-bit rotate-right operation
52705caefcfSchristos(define-pmacro (set-psw-rrotate17 Rd index src c rot)
52805caefcfSchristos  (sequence ((SI tmpfoo))
52905caefcfSchristos    (set tmpfoo (or (or (and (sll SI src 17) #xFFFE0000)
53005caefcfSchristos		     src)
53105caefcfSchristos		 (sll SI c 16)))
53205caefcfSchristos    (set tmpfoo (ror tmpfoo (and rot #x0F)))
53305caefcfSchristos    (set-psw-carry (reg HI h-gr index) index (trunc HI tmpfoo) (and (srl tmpfoo 16) 1) 1)))
53405caefcfSchristos
53505caefcfSchristos
53605caefcfSchristos; Move Operations
53705caefcfSchristos
53805caefcfSchristos(define-pmacro (alignfix-mem where)
53905caefcfSchristos  (mem HI (and where #xFFFE)))
54005caefcfSchristos
54105caefcfSchristos(define-pmacro (set-alignfix-mem where what)
54205caefcfSchristos  (set (mem HI (and where #xFFFE)) what))
54305caefcfSchristos
54405caefcfSchristos(define-pmacro (alignfix-mem-far where)
54505caefcfSchristos  (mem HI (and where #xFFFFFFFE)))
54605caefcfSchristos
54705caefcfSchristos(define-pmacro (set-alignfix-mem-far where what)
54805caefcfSchristos  (set (mem HI (and where #xFFFFFFFE)) what))
54905caefcfSchristos
55005caefcfSchristos(dni movlmemimm
55105caefcfSchristos     "Move immediate to low memory"
55205caefcfSchristos     ()
55305caefcfSchristos     ("mov$ws2 $lmem8,#$imm16")
55405caefcfSchristos     (+ OP1_7 OP2A_8 ws2 lmem8 imm16)
55505caefcfSchristos     (if ws2
55605caefcfSchristos	 (set-mem-psw (mem HI (and lmem8 #xFFFE)) imm16 ws2)
55705caefcfSchristos	 (set-mem-psw (mem QI lmem8) (and imm16 #xFF) ws2))
55805caefcfSchristos     ()
55905caefcfSchristos)
56005caefcfSchristos(dni movhmemimm
56105caefcfSchristos     "Move immediate to high memory"
56205caefcfSchristos     ()
56305caefcfSchristos     ("mov$ws2 $hmem8,#$imm16")
56405caefcfSchristos     (+ OP1_7 OP2A_A ws2 hmem8 imm16)
56505caefcfSchristos     (if ws2
56605caefcfSchristos	 (set-mem-psw (mem HI (and hmem8 #xFFFE)) imm16 ws2)
56705caefcfSchristos	 (set-mem-psw (mem QI hmem8) (and imm16 #xFF) ws2))
56805caefcfSchristos     ()
56905caefcfSchristos)
57005caefcfSchristos
57105caefcfSchristos(dni movlgrmem
57205caefcfSchristos     "Move low memory to register"
57305caefcfSchristos     ()
57405caefcfSchristos     ("mov$ws2 $Rm,$lmem8")
57505caefcfSchristos     (+ OP1_8 Rm ws2 lmem8)
57605caefcfSchristos     (if ws2
57705caefcfSchristos	 (set-psw Rm (index-of Rm) (alignfix-mem lmem8) ws2)
57805caefcfSchristos	 (set-psw Rm (index-of Rm) (mem QI lmem8) ws2))
57905caefcfSchristos     ()
58005caefcfSchristos)
58105caefcfSchristos(dni movhgrmem
58205caefcfSchristos     "Move high memory to register"
58305caefcfSchristos     ()
58405caefcfSchristos     ("mov$ws2 $Rm,$hmem8")
58505caefcfSchristos     (+ OP1_A Rm ws2 hmem8)
58605caefcfSchristos     (if ws2
58705caefcfSchristos	 (set-psw Rm (index-of Rm) (alignfix-mem hmem8) ws2)
58805caefcfSchristos	 (set-psw Rm (index-of Rm) (mem QI hmem8) ws2))
58905caefcfSchristos     ()
59005caefcfSchristos)
59105caefcfSchristos
59205caefcfSchristos(dni movlmemgr
59305caefcfSchristos     "Move low memory register to byte"
59405caefcfSchristos     ()
59505caefcfSchristos     ("mov$ws2 $lmem8,$Rm")
59605caefcfSchristos     (+ OP1_9 Rm ws2 lmem8)
59705caefcfSchristos     (if ws2
59805caefcfSchristos	 (set-mem-psw (mem HI (and lmem8 #xFFFE)) Rm ws2)
59905caefcfSchristos	 (set-mem-psw (mem QI lmem8) Rm ws2))
60005caefcfSchristos     ()
60105caefcfSchristos)
60205caefcfSchristos(dni movhmemgr
60305caefcfSchristos     "Move high memory register to byte"
60405caefcfSchristos     ()
60505caefcfSchristos     ("mov$ws2 $hmem8,$Rm")
60605caefcfSchristos     (+ OP1_B Rm ws2 hmem8)
60705caefcfSchristos     (if ws2
60805caefcfSchristos	 (set-mem-psw (mem HI (and hmem8 #xFFFE)) Rm ws2)
60905caefcfSchristos	 (set-mem-psw (mem QI hmem8) Rm ws2))
61005caefcfSchristos     ()
61105caefcfSchristos)
61205caefcfSchristos
61305caefcfSchristos(dni movgrgri
61405caefcfSchristos     "Move memory addressed by register to register"
61505caefcfSchristos     ()
61605caefcfSchristos     ("mov$ws2 $Rdm,($Rs)")
61705caefcfSchristos     (+ OP1_7 OP2A_0 ws2 Rs OP4M_0 Rdm)
61805caefcfSchristos     (if ws2
61905caefcfSchristos	 (set-psw Rdm (index-of Rdm) (alignfix-mem Rs) ws2)
62005caefcfSchristos	 (set-psw Rdm (index-of Rdm) (and #xFF (mem QI Rs)) ws2))
62105caefcfSchristos     ()
62205caefcfSchristos)
62305caefcfSchristos
62405caefcfSchristos(dni movgrgripostinc
62505caefcfSchristos     "Move memory addressed by postincrement register to register"
62605caefcfSchristos     ()
62705caefcfSchristos     ("mov$ws2 $Rdm,($Rs++)")
62805caefcfSchristos     (+ OP1_6 OP2A_0 ws2 Rs OP4M_0 Rdm)
62905caefcfSchristos     (sequence ()
63005caefcfSchristos	       (if ws2
63105caefcfSchristos		   (set-psw Rdm (index-of Rdm) (alignfix-mem Rs) ws2)
63205caefcfSchristos		   (set-psw Rdm (index-of Rdm) (and #xFF (mem QI Rs)) ws2))
63305caefcfSchristos	       (set Rs (add Rs (add 1 ws2))))
63405caefcfSchristos     ()
63505caefcfSchristos)
63605caefcfSchristos
63705caefcfSchristos(dni movgrgripredec
63805caefcfSchristos     "Move memory addressed by predecrement register to register"
63905caefcfSchristos     ()
64005caefcfSchristos     ("mov$ws2 $Rdm,(--$Rs)")
64105caefcfSchristos     (+ OP1_6 OP2A_8 ws2 Rs OP4M_0 Rdm)
64205caefcfSchristos     (sequence ()
64305caefcfSchristos	       (set Rs (sub Rs (add 1 ws2)))
64405caefcfSchristos	       (if ws2
64505caefcfSchristos		   (set-psw Rdm (index-of Rdm) (alignfix-mem Rs) ws2)
64605caefcfSchristos		   (set-psw Rdm (index-of Rdm) (and #xFF (mem QI Rs)) ws2)))
64705caefcfSchristos     ()
64805caefcfSchristos)
64905caefcfSchristos
65005caefcfSchristos(dni movgrigr
65105caefcfSchristos     "Move register to memory addressed by register"
65205caefcfSchristos     ()
65305caefcfSchristos     ("mov$ws2 ($Rs),$Rdm")
65405caefcfSchristos     (+ OP1_7 OP2A_2 ws2 Rs OP4M_0 Rdm)
65505caefcfSchristos     (sequence ()
65605caefcfSchristos	       (if ws2
65705caefcfSchristos		   (set-alignfix-mem Rs Rdm)
65805caefcfSchristos		   (set (mem QI Rs) Rdm))
65905caefcfSchristos	       (set-psw-nowrite (index-of Rdm) Rdm ws2))
66005caefcfSchristos     ()
66105caefcfSchristos)
66205caefcfSchristos
66305caefcfSchristos(dni movgripostincgr
66405caefcfSchristos     "Move register to memory addressed by postincrement register"
66505caefcfSchristos     ()
66605caefcfSchristos     ("mov$ws2 ($Rs++),$Rdm")
66705caefcfSchristos     (+ OP1_6 OP2A_2 ws2 Rs OP4M_0 Rdm)
66805caefcfSchristos     (sequence ()
66905caefcfSchristos	       (if ws2
67005caefcfSchristos		   (set-alignfix-mem Rs Rdm)
67105caefcfSchristos		   (set (mem QI Rs) Rdm))
67205caefcfSchristos	       (set-psw-nowrite (index-of Rdm) Rdm ws2)
67305caefcfSchristos	       (set Rs (add Rs (add ws2 1))))
67405caefcfSchristos     ()
67505caefcfSchristos)
67605caefcfSchristos
67705caefcfSchristos(dni movgripredecgr
67805caefcfSchristos     "Move register to memory addressed by predecrement register"
67905caefcfSchristos     ()
68005caefcfSchristos     ("mov$ws2 (--$Rs),$Rdm")
68105caefcfSchristos     (+ OP1_6 OP2A_A ws2 Rs OP4M_0 Rdm)
68205caefcfSchristos     (sequence ()
68305caefcfSchristos	       (set Rs (sub Rs (add ws2 1)))
68405caefcfSchristos	       (set-psw-nowrite (index-of Rdm) Rdm ws2)
68505caefcfSchristos	       (if ws2
68605caefcfSchristos		   (set-alignfix-mem Rs Rdm)
68705caefcfSchristos		   (set (mem QI Rs) Rdm)))
68805caefcfSchristos     ()
68905caefcfSchristos)
69005caefcfSchristos
69105caefcfSchristos(dni movgrgrii
69205caefcfSchristos     "Move memory addressed by indexed register to register"
69305caefcfSchristos     ()
69405caefcfSchristos     ("mov$ws2 $Rdm,($Rs,$imm12)")
69505caefcfSchristos     (+ OP1_7 OP2A_0 ws2 Rs OP4M_1 Rdm OP5_0 imm12)
69605caefcfSchristos     (if ws2
69705caefcfSchristos	 (set-psw Rdm (index-of Rdm) (alignfix-mem (add Rs imm12)) ws2)
69805caefcfSchristos	 (set-psw Rdm (index-of Rdm) (and #xFF (mem QI (add Rs imm12))) ws2))
69905caefcfSchristos     ()
70005caefcfSchristos)
70105caefcfSchristos
70205caefcfSchristos(dni movgrgriipostinc
70305caefcfSchristos     "Move memory addressed by indexed register postincrement to register"
70405caefcfSchristos     ()
70505caefcfSchristos     ("mov$ws2 $Rdm,($Rs++,$imm12)")
70605caefcfSchristos     (+ OP1_6 OP2A_0 ws2 Rs OP4M_1 Rdm OP5_0 imm12)
70705caefcfSchristos     (sequence ()
70805caefcfSchristos	       (if ws2
70905caefcfSchristos		   (set-psw Rdm (index-of Rdm) (alignfix-mem (add Rs imm12)) ws2)
71005caefcfSchristos		   (set-psw Rdm (index-of Rdm) (and #xFF (mem QI (add Rs imm12))) ws2))
71105caefcfSchristos	       (set Rs (add Rs (add ws2 1))))
71205caefcfSchristos     ()
71305caefcfSchristos)
71405caefcfSchristos
71505caefcfSchristos(dni movgrgriipredec
71605caefcfSchristos     "Move memory addressed by indexed register predecrement to register"
71705caefcfSchristos     ()
71805caefcfSchristos     ("mov$ws2 $Rdm,(--$Rs,$imm12)")
71905caefcfSchristos     (+ OP1_6 OP2A_8 ws2 Rs OP4M_1 Rdm OP5_0 imm12)
72005caefcfSchristos     (sequence ()
72105caefcfSchristos	       (set Rs (sub Rs (add ws2 1)))
72205caefcfSchristos	       (if ws2
72305caefcfSchristos		   (set-psw Rdm (index-of Rdm) (alignfix-mem (add Rs imm12)) ws2)
72405caefcfSchristos		   (set-psw Rdm (index-of Rdm) (and #xFF (mem QI (add Rs imm12))) ws2)))
72505caefcfSchristos     ()
72605caefcfSchristos)
72705caefcfSchristos
72805caefcfSchristos(dni movgriigr
72905caefcfSchristos     "Move register to memory addressed by indexed register"
73005caefcfSchristos     ()
73105caefcfSchristos     ("mov$ws2 ($Rs,$imm12),$Rdm")
73205caefcfSchristos     (+ OP1_7 OP2A_2 ws2 Rs OP4M_1 Rdm OP5_0 imm12)
73305caefcfSchristos     (sequence ()
73405caefcfSchristos	       (if ws2
73505caefcfSchristos		   (set-alignfix-mem (add Rs imm12) Rdm)
73605caefcfSchristos		   (set (mem QI (add Rs imm12)) Rdm))
73705caefcfSchristos	       (set-psw-nowrite (index-of Rdm) Rdm ws2))
73805caefcfSchristos     ()
73905caefcfSchristos)
74005caefcfSchristos
74105caefcfSchristos(dni movgriipostincgr
74205caefcfSchristos     "Move register to memory addressed by indexed register postincrement"
74305caefcfSchristos     ()
74405caefcfSchristos     ("mov$ws2 ($Rs++,$imm12),$Rdm")
74505caefcfSchristos     (+ OP1_6 OP2A_2 ws2 Rs OP4M_1 Rdm OP5_0 imm12)
74605caefcfSchristos     (sequence ()
74705caefcfSchristos	       (if ws2
74805caefcfSchristos		   (set-alignfix-mem (add Rs imm12) Rdm)
74905caefcfSchristos		   (set (mem QI (add Rs imm12)) Rdm))
75005caefcfSchristos	       (set-psw-nowrite (index-of Rdm) Rdm ws2)
75105caefcfSchristos	       (set Rs (add Rs (add ws2 1))))
75205caefcfSchristos     ()
75305caefcfSchristos)
75405caefcfSchristos
75505caefcfSchristos(dni movgriipredecgr
75605caefcfSchristos     "Move register to memory addressed by indexed register predecrement"
75705caefcfSchristos     ()
75805caefcfSchristos     ("mov$ws2 (--$Rs,$imm12),$Rdm")
75905caefcfSchristos     (+ OP1_6 OP2A_A ws2 Rs OP4M_1 Rdm OP5_0 imm12)
76005caefcfSchristos     (sequence ()
76105caefcfSchristos	       (set Rs (sub Rs (add ws2 1)))
76205caefcfSchristos	       (set-psw-nowrite (index-of Rdm) Rdm ws2)
76305caefcfSchristos	       (if ws2
76405caefcfSchristos		   (set-alignfix-mem (add Rs imm12) Rdm)
76505caefcfSchristos		   (set (mem QI (add Rs imm12)) Rdm)))
76605caefcfSchristos     ()
76705caefcfSchristos)
76805caefcfSchristos
76905caefcfSchristos(dni movgrgr
77005caefcfSchristos     "Move general register to general register"
77105caefcfSchristos     ()
77205caefcfSchristos     ("mov $Rd,$Rs")
77305caefcfSchristos     (+ OP1_4 OP2_6 Rs Rd)
77405caefcfSchristos     (set-psw Rd (index-of Rd) Rs 1)
77505caefcfSchristos     ()
77605caefcfSchristos)
77705caefcfSchristos
77805caefcfSchristos(dnmi movimm8
77905caefcfSchristos     "Move 8-bit immediate"
78005caefcfSchristos     ()
78105caefcfSchristos     ("mov Rx,#$imm8")
78205caefcfSchristos     (emit movwimm8 imm8)
78305caefcfSchristos)
78405caefcfSchristos
78505caefcfSchristos(dni movwimm8
78605caefcfSchristos     "Move 8-bit immediate"
78705caefcfSchristos     ()
78805caefcfSchristos     ("mov.w Rx,#$imm8")
78905caefcfSchristos     (+ OP1_4 OP2_7 imm8)
79005caefcfSchristos     (set-psw (reg HI h-gr Rpsw) Rpsw imm8 1)
79105caefcfSchristos     ()
79205caefcfSchristos)
79305caefcfSchristos
79405caefcfSchristos(dnmi movgrimm8
79505caefcfSchristos     "Move 8-bit immediate to general register"
79605caefcfSchristos     ()
79705caefcfSchristos     ("mov $Rm,#$imm8small")
79805caefcfSchristos     (emit movwgrimm8 Rm imm8small)
79905caefcfSchristos)
80005caefcfSchristos
80105caefcfSchristos(dni movwgrimm8
80205caefcfSchristos     "Move 8-bit immediate to general register"
80305caefcfSchristos     ()
80405caefcfSchristos     ("mov.w $Rm,#$imm8small")
80505caefcfSchristos     (+ OP1_2 Rm OP2M_1 imm8small)
80605caefcfSchristos     (set-psw Rm (index-of Rm) imm8small 1)
80705caefcfSchristos     ()
80805caefcfSchristos)
80905caefcfSchristos
81005caefcfSchristos(dnmi movgrimm16
81105caefcfSchristos     "Move 16-bit immediate to general register"
81205caefcfSchristos     ()
81305caefcfSchristos     ("mov $Rd,#$imm16")
81405caefcfSchristos     (emit movwgrimm16 Rd imm16)
81505caefcfSchristos)
81605caefcfSchristos
81705caefcfSchristos(dni movwgrimm16
81805caefcfSchristos     "Move 16-bit immediate to general register"
81905caefcfSchristos     ()
82005caefcfSchristos     ("mov.w $Rd,#$imm16")
82105caefcfSchristos     (+ OP1_3 OP2_1 OP3_3 Rd imm16)
82205caefcfSchristos     (set-psw Rd (index-of Rd) imm16 1)
82305caefcfSchristos     ()
82405caefcfSchristos)
82505caefcfSchristos
82605caefcfSchristos(dni movlowgr
82705caefcfSchristos     "Move 8 low bits to general register"
82805caefcfSchristos     ()
82905caefcfSchristos     ("mov.b $Rd,RxL")
83005caefcfSchristos     (+ OP1_3 OP2_0 OP3_C Rd)
83105caefcfSchristos     (set-psw Rd (index-of Rd) (or (and Rd #xFF00) (and (reg HI h-gr Rpsw) #xFF)) 0)
83205caefcfSchristos     ()
83305caefcfSchristos)
83405caefcfSchristos
83505caefcfSchristos(dni movhighgr
83605caefcfSchristos     "Move 8 high bits to general register"
83705caefcfSchristos     ()
83805caefcfSchristos     ("mov.b $Rd,RxH")
83905caefcfSchristos     (+ OP1_3 OP2_0 OP3_D Rd)
84005caefcfSchristos     (set-psw Rd (index-of Rd) (or (and Rd #x00FF) (and (reg HI h-gr Rpsw) #xFF00)) 1)
84105caefcfSchristos     ()
84205caefcfSchristos)
84305caefcfSchristos
84405caefcfSchristos(dni movfgrgri
84505caefcfSchristos     "Move far memory addressed by register to register"
84605caefcfSchristos     ()
84705caefcfSchristos     ("movf$ws2 $Rdm,($Rs)")
84805caefcfSchristos     (+ OP1_7 OP2A_4 ws2 Rs OP4M_0 Rdm)
84905caefcfSchristos     (if ws2
85005caefcfSchristos	 (set-psw Rdm (index-of Rdm) (alignfix-mem-far (or (sll SI R8 16) Rs)) ws2)
85105caefcfSchristos	 (set-psw Rdm (index-of Rdm) (and #xFF (mem QI (or (sll SI R8 16) Rs))) ws2))
85205caefcfSchristos     ()
85305caefcfSchristos)
85405caefcfSchristos
85505caefcfSchristos(dni movfgrgripostinc
85605caefcfSchristos     "Move far memory addressed by postincrement register to register"
85705caefcfSchristos     ()
85805caefcfSchristos     ("movf$ws2 $Rdm,($Rs++)")
85905caefcfSchristos     (+ OP1_6 OP2A_4 ws2 Rs OP4M_0 Rdm)
86005caefcfSchristos     (sequence ()
86105caefcfSchristos	       (if ws2
86205caefcfSchristos		   (set-psw Rdm (index-of Rdm) (alignfix-mem-far (join SI HI R8 Rs)) ws2)
86305caefcfSchristos		   (set-psw Rdm (index-of Rdm) (and #xFF (mem QI (join SI HI R8 Rs))) ws2))
86405caefcfSchristos	       (set Rs (add Rs (add ws2 1))))
86505caefcfSchristos     ()
86605caefcfSchristos)
86705caefcfSchristos
86805caefcfSchristos(dni movfgrgripredec
86905caefcfSchristos     "Move far memory addressed by predecrement register to register"
87005caefcfSchristos     ()
87105caefcfSchristos     ("movf$ws2 $Rdm,(--$Rs)")
87205caefcfSchristos     (+ OP1_6 OP2A_C ws2 Rs OP4M_0 Rdm)
87305caefcfSchristos     (sequence ()
87405caefcfSchristos	       (set Rs (sub Rs (add ws2 1)))
87505caefcfSchristos	       (if ws2
87605caefcfSchristos		   (set-psw Rdm (index-of Rdm) (alignfix-mem-far (join SI HI R8 Rs)) ws2)
87705caefcfSchristos		   (set-psw Rdm (index-of Rdm) (and #xFF (mem QI (join SI HI R8 Rs))) ws2)))
87805caefcfSchristos     ()
87905caefcfSchristos)
88005caefcfSchristos
88105caefcfSchristos(dni movfgrigr
88205caefcfSchristos     "Move far register to memory addressed by register"
88305caefcfSchristos     ()
88405caefcfSchristos     ("movf$ws2 ($Rs),$Rdm")
88505caefcfSchristos     (+ OP1_7 OP2A_6 ws2 Rs OP4M_0 Rdm)
88605caefcfSchristos     (sequence ()
88705caefcfSchristos	       (if ws2
88805caefcfSchristos		   (set-alignfix-mem-far (join SI HI R8 Rs) Rdm)
88905caefcfSchristos		   (set (mem QI (join SI HI R8 Rs)) Rdm))
89005caefcfSchristos	       (set-psw-nowrite (index-of Rdm) Rdm ws2))
89105caefcfSchristos     ()
89205caefcfSchristos)
89305caefcfSchristos
89405caefcfSchristos(dni movfgripostincgr
89505caefcfSchristos     "Move far register to memory addressed by postincrement register"
89605caefcfSchristos     ()
89705caefcfSchristos     ("movf$ws2 ($Rs++),$Rdm")
89805caefcfSchristos     (+ OP1_6 OP2A_6 ws2 Rs OP4M_0 Rdm)
89905caefcfSchristos     (sequence ()
90005caefcfSchristos	       (if ws2
90105caefcfSchristos		   (set-alignfix-mem-far (join SI HI R8 Rs) Rdm)
90205caefcfSchristos		   (set (mem QI (join SI HI R8 Rs)) Rdm))
90305caefcfSchristos	       (set-psw-nowrite (index-of Rdm) Rdm ws2)
90405caefcfSchristos	       (set Rs (add Rs (add ws2 1))))
90505caefcfSchristos     ()
90605caefcfSchristos)
90705caefcfSchristos
90805caefcfSchristos(dni movfgripredecgr
90905caefcfSchristos     "Move far register to memory addressed by predecrement register"
91005caefcfSchristos     ()
91105caefcfSchristos     ("movf$ws2 (--$Rs),$Rdm")
91205caefcfSchristos     (+ OP1_6 OP2A_E ws2 Rs OP4M_0 Rdm)
91305caefcfSchristos     (sequence ()
91405caefcfSchristos	       (set-psw-nowrite (index-of Rdm) Rdm ws2)
91505caefcfSchristos	       (set Rs (sub Rs (add ws2 1)))
91605caefcfSchristos	       (if ws2
91705caefcfSchristos		   (set-alignfix-mem-far (join SI HI R8 Rs) Rdm)
91805caefcfSchristos		   (set (mem QI (join SI HI R8 Rs)) Rdm)))
91905caefcfSchristos     ()
92005caefcfSchristos)
92105caefcfSchristos
92205caefcfSchristos(dni movfgrgrii
92305caefcfSchristos     "Move far memory addressed by indexed register to register"
92405caefcfSchristos     ()
92505caefcfSchristos     ("movf$ws2 $Rdm,($Rb,$Rs,$imm12)")
92605caefcfSchristos     (+ OP1_7 OP2A_4 ws2 Rs OP4M_1 Rdm OP5A_0 Rb imm12)
92705caefcfSchristos     (if ws2
92805caefcfSchristos	 (set-psw Rdm (index-of Rdm) (alignfix-mem-far (add (join SI HI Rb Rs) imm12)) ws2)
92905caefcfSchristos	 (set-psw Rdm (index-of Rdm) (and #xFF (mem QI (add (join SI HI Rb Rs) imm12))) ws2))
93005caefcfSchristos     ()
93105caefcfSchristos)
93205caefcfSchristos
93305caefcfSchristos(dni movfgrgriipostinc
93405caefcfSchristos "Move far memory addressed by indexed register postincrement to register"
93505caefcfSchristos     ()
93605caefcfSchristos     ("movf$ws2 $Rdm,($Rb,$Rs++,$imm12)")
93705caefcfSchristos     (+ OP1_6 OP2A_4 ws2 Rs OP4M_1 Rdm OP5A_0 Rb imm12)
93805caefcfSchristos     (sequence ()
93905caefcfSchristos	       (if ws2
94005caefcfSchristos		   (set-psw Rdm (index-of Rdm) (alignfix-mem-far (add (join SI HI Rb Rs) imm12)) ws2)
94105caefcfSchristos		   (set-psw Rdm (index-of Rdm) (and #xFF (mem QI (add (join SI HI Rb Rs) imm12))) ws2))
94205caefcfSchristos	       (set Rs (add Rs (add ws2 1)))
94305caefcfSchristos	       ; Note - despite the XStormy16 ISA documentation the
94405caefcfSchristos	       ; addition *is* propogated into the base register.
94505caefcfSchristos	       (if (eq Rs 0) (set Rb (add Rb 1)))
94605caefcfSchristos	       )
94705caefcfSchristos     ()
94805caefcfSchristos)
94905caefcfSchristos
95005caefcfSchristos(dni movfgrgriipredec
95105caefcfSchristos "Move far memory addressed by indexed register predecrement to register"
95205caefcfSchristos     ()
95305caefcfSchristos     ("movf$ws2 $Rdm,($Rb,--$Rs,$imm12)")
95405caefcfSchristos     (+ OP1_6 OP2A_C ws2 Rs OP4M_1 Rdm OP5A_0 Rb imm12)
95505caefcfSchristos     (sequence ()
95605caefcfSchristos	       ; Note - despite the XStormy16 ISA documentation the
95705caefcfSchristos	       ; subtraction *is* propogated into the base register.
95805caefcfSchristos	       (if (eq Rs 0) (set Rb (sub Rb 1)))
95905caefcfSchristos	       (set Rs (sub Rs (add ws2 1)))
96005caefcfSchristos	       (if ws2
96105caefcfSchristos		   (set-psw Rdm (index-of Rdm) (alignfix-mem-far (add (join SI HI Rb Rs) imm12)) ws2)
96205caefcfSchristos		   (set-psw Rdm (index-of Rdm) (and #xFF (mem QI (add (join SI HI Rb Rs) imm12))) ws2)))
96305caefcfSchristos     ()
96405caefcfSchristos)
96505caefcfSchristos
96605caefcfSchristos(dni movfgriigr
96705caefcfSchristos     "Move far register to memory addressed by indexed register"
96805caefcfSchristos     ()
96905caefcfSchristos     ("movf$ws2 ($Rb,$Rs,$imm12),$Rdm")
97005caefcfSchristos     (+ OP1_7 OP2A_6 ws2 Rs OP4M_1 Rdm OP5A_0 Rb imm12)
97105caefcfSchristos     (sequence ()
97205caefcfSchristos	       (if ws2
97305caefcfSchristos		   (set (mem HI (and (add (join SI HI Rb Rs) imm12) #xFFFFFFFE))
97405caefcfSchristos			Rdm)
97505caefcfSchristos		   (set (mem QI (add (join SI HI Rb Rs) imm12)) Rdm))
97605caefcfSchristos	       (set-psw-nowrite (index-of Rdm) Rdm ws2))
97705caefcfSchristos     ()
97805caefcfSchristos)
97905caefcfSchristos
98005caefcfSchristos
98105caefcfSchristos(dni movfgriipostincgr
98205caefcfSchristos     "Move far register to memory addressed by indexed register postincrement"
98305caefcfSchristos     ()
98405caefcfSchristos     ("movf$ws2 ($Rb,$Rs++,$imm12),$Rdm")
98505caefcfSchristos     (+ OP1_6 OP2A_6 ws2 Rs OP4M_1 Rdm OP5A_0 Rb imm12)
98605caefcfSchristos     (sequence ()
98705caefcfSchristos	       (if ws2
98805caefcfSchristos		   (set (mem HI (and (add (join SI HI Rb Rs) imm12) #xFFFFFFFE)) Rdm)
98905caefcfSchristos		   (set (mem QI (add (join SI HI Rb Rs) imm12)) Rdm))
99005caefcfSchristos	       (set-psw-nowrite (index-of Rdm) Rdm ws2)
99105caefcfSchristos	       (set Rs (add Rs (add ws2 1)))
99205caefcfSchristos	       ; Note - despite the XStormy16 ISA documentation the
99305caefcfSchristos	       ; addition *is* propogated into the base register.
99405caefcfSchristos	       (if (eq Rs 0) (set Rb (add Rb 1)))
99505caefcfSchristos	       )
99605caefcfSchristos     ()
99705caefcfSchristos)
99805caefcfSchristos
99905caefcfSchristos(dni movfgriipredecgr
100005caefcfSchristos  "Move far register to memory addressed by indexed register predecrement"
100105caefcfSchristos     ()
100205caefcfSchristos     ("movf$ws2 ($Rb,--$Rs,$imm12),$Rdm")
100305caefcfSchristos     (+ OP1_6 OP2A_E ws2 Rs OP4M_1 Rdm OP5A_0 Rb imm12)
100405caefcfSchristos     (sequence ()
100505caefcfSchristos	       ; Note - despite the XStormy16 ISA documentation the
100605caefcfSchristos	       ; subtraction *is* propogated into the base register.
100705caefcfSchristos	       (if (eq Rs 0) (set Rb (sub Rb 1)))
100805caefcfSchristos	       (set Rs (sub Rs (add ws2 1)))
100905caefcfSchristos	       (set-psw-nowrite (index-of Rdm) Rdm ws2)
101005caefcfSchristos	       (if ws2
101105caefcfSchristos		   (set (mem HI (and (add (join SI HI Rb Rs) imm12) #xFFFFFFFE)) Rdm)
101205caefcfSchristos		   (set (mem QI (add (join SI HI Rb Rs) imm12)) Rdm)))
101305caefcfSchristos     ()
101405caefcfSchristos)
101505caefcfSchristos
101605caefcfSchristos(dni maskgrgr
101705caefcfSchristos     "Mask insert controlled by general register"
101805caefcfSchristos     ()
101905caefcfSchristos     ("mask $Rd,$Rs")
102005caefcfSchristos     (+ OP1_3 OP2_3 Rs Rd)
102105caefcfSchristos     (set-psw Rd (index-of Rd) (or HI (and HI Rd (inv HI Rs)) (and (reg HI h-gr Rpsw) Rs)) 1)
102205caefcfSchristos     ()
102305caefcfSchristos)
102405caefcfSchristos
102505caefcfSchristos(dni maskgrimm16
102605caefcfSchristos     "Mask insert controlled by immediate value"
102705caefcfSchristos     ()
102805caefcfSchristos     ("mask $Rd,#$imm16")
102905caefcfSchristos     (+ OP1_3 OP2_0 OP3_E Rd imm16)
103005caefcfSchristos     (set-psw Rd (index-of Rd) (or (and Rd (inv imm16)) (and (reg HI h-gr Rpsw) imm16)) 1)
103105caefcfSchristos     ()
103205caefcfSchristos)
103305caefcfSchristos
103405caefcfSchristos; Push, Pop
103505caefcfSchristos(dni pushgr
103605caefcfSchristos     "Push register"
103705caefcfSchristos     ()
103805caefcfSchristos     ("push $Rd")
103905caefcfSchristos     (+ OP1_0 OP2_0 OP3_8 Rd)
104005caefcfSchristos     (sequence ()
104105caefcfSchristos	       (set (mem HI sp) Rd)
104205caefcfSchristos	       (set sp (add sp 2)))
104305caefcfSchristos     ()
104405caefcfSchristos)
104505caefcfSchristos
104605caefcfSchristos(dni popgr
104705caefcfSchristos     "Pop into a register"
104805caefcfSchristos     ()
104905caefcfSchristos     ("pop $Rd")
105005caefcfSchristos     (+ OP1_0 OP2_0 OP3_9 Rd)
105105caefcfSchristos     (sequence ()
105205caefcfSchristos	       (set sp (add sp -2))
105305caefcfSchristos	       (set Rd (mem HI sp)))
105405caefcfSchristos     ()
105505caefcfSchristos)
105605caefcfSchristos
105705caefcfSchristos; Swap
105805caefcfSchristos(dni swpn
105905caefcfSchristos     "Swap low nibbles"
106005caefcfSchristos     ()
106105caefcfSchristos     ("swpn $Rd")
106205caefcfSchristos     (+ OP1_3 OP2_0 OP3_9 Rd)
106305caefcfSchristos     (set-psw Rd (index-of Rd) (or (or (and (sll Rd 4) #xF0)
106405caefcfSchristos			 (and (srl Rd 4) #x0F))
106505caefcfSchristos		     (and Rd #xFF00)) 0)
106605caefcfSchristos     ()
106705caefcfSchristos)
106805caefcfSchristos
106905caefcfSchristos(dni swpb
107005caefcfSchristos     "Swap bytes"
107105caefcfSchristos     ()
107205caefcfSchristos     ("swpb $Rd")
107305caefcfSchristos     (+ OP1_3 OP2_0 OP3_8 Rd)
107405caefcfSchristos     (set-psw Rd (index-of Rd) (or (sll Rd 8) (srl Rd 8)) 1)
107505caefcfSchristos     ()
107605caefcfSchristos)
107705caefcfSchristos
107805caefcfSchristos(dni swpw
107905caefcfSchristos     "Swap words"
108005caefcfSchristos     ()
108105caefcfSchristos     ("swpw $Rd,$Rs")
108205caefcfSchristos     (+ OP1_3 OP2_2 Rs Rd)
108305caefcfSchristos     (sequence ((HI foo))
108405caefcfSchristos	       (set foo Rs)
108505caefcfSchristos	       (set Rs Rd)
108605caefcfSchristos	       (set-psw Rd (index-of Rd) foo 1))
108705caefcfSchristos     ()
108805caefcfSchristos)
108905caefcfSchristos
109005caefcfSchristos; Logical Operations
109105caefcfSchristos(dni andgrgr
109205caefcfSchristos     "AND general register with general register"
109305caefcfSchristos     ()
109405caefcfSchristos     ("and $Rd,$Rs")
109505caefcfSchristos     (+ OP1_4 OP2_0 Rs Rd)
109605caefcfSchristos     (set-psw Rd (index-of Rd) (and Rd Rs) 1)
109705caefcfSchristos     ()
109805caefcfSchristos)
109905caefcfSchristos
110005caefcfSchristos(dni andimm8
110105caefcfSchristos     "AND with 8-bit immediate"
110205caefcfSchristos     ()
110305caefcfSchristos     ("and Rx,#$imm8")
110405caefcfSchristos     (+ OP1_4 OP2_1 imm8)
110505caefcfSchristos     (set-psw (reg HI h-gr Rpsw) Rpsw (and (reg HI h-gr Rpsw) imm8) 1)
110605caefcfSchristos     ()
110705caefcfSchristos)
110805caefcfSchristos
110905caefcfSchristos(dni andgrimm16
111005caefcfSchristos     "AND general register with 16-bit immediate"
111105caefcfSchristos     ()
111205caefcfSchristos     ("and $Rd,#$imm16")
111305caefcfSchristos     (+ OP1_3 OP2_1 OP3_0 Rd imm16)
111405caefcfSchristos     (set-psw Rd (index-of Rd) (and Rd imm16) 1)
111505caefcfSchristos     ()
111605caefcfSchristos)
111705caefcfSchristos
111805caefcfSchristos(dni orgrgr
111905caefcfSchristos     "OR general register with general register"
112005caefcfSchristos     ()
112105caefcfSchristos     ("or $Rd,$Rs")
112205caefcfSchristos     (+ OP1_4 OP2_2 Rs Rd)
112305caefcfSchristos     (set-psw Rd (index-of Rd) (or Rd Rs) 1)
112405caefcfSchristos     ()
112505caefcfSchristos)
112605caefcfSchristos
112705caefcfSchristos(dni orimm8
112805caefcfSchristos     "OR with 8-bit immediate"
112905caefcfSchristos     ()
113005caefcfSchristos     ("or Rx,#$imm8")
113105caefcfSchristos     (+ OP1_4 OP2_3 imm8)
113205caefcfSchristos     (set-psw (reg HI h-gr Rpsw) Rpsw (or (reg HI h-gr Rpsw) imm8) 1)
113305caefcfSchristos     ()
113405caefcfSchristos)
113505caefcfSchristos
113605caefcfSchristos(dni orgrimm16
113705caefcfSchristos     "OR general register with 16-bit immediate"
113805caefcfSchristos     ()
113905caefcfSchristos     ("or $Rd,#$imm16")
114005caefcfSchristos     (+ OP1_3 OP2_1 OP3_1 Rd imm16)
114105caefcfSchristos     (set-psw Rd (index-of Rd) (or Rd imm16) 1)
114205caefcfSchristos     ()
114305caefcfSchristos)
114405caefcfSchristos
114505caefcfSchristos(dni xorgrgr
114605caefcfSchristos     "XOR general register with general register"
114705caefcfSchristos     ()
114805caefcfSchristos     ("xor $Rd,$Rs")
114905caefcfSchristos     (+ OP1_4 OP2_4 Rs Rd)
115005caefcfSchristos     (set-psw Rd (index-of Rd) (xor Rd Rs) 1)
115105caefcfSchristos     ()
115205caefcfSchristos)
115305caefcfSchristos
115405caefcfSchristos(dni xorimm8
115505caefcfSchristos     "XOR with 8-bit immediate"
115605caefcfSchristos     ()
115705caefcfSchristos     ("xor Rx,#$imm8")
115805caefcfSchristos     (+ OP1_4 OP2_5 imm8)
115905caefcfSchristos     (set-psw (reg HI h-gr Rpsw) Rpsw (xor (reg HI h-gr Rpsw) imm8) 1)
116005caefcfSchristos     ()
116105caefcfSchristos)
116205caefcfSchristos
116305caefcfSchristos(dni xorgrimm16
116405caefcfSchristos     "XOR general register with 16-bit immediate"
116505caefcfSchristos     ()
116605caefcfSchristos     ("xor $Rd,#$imm16")
116705caefcfSchristos     (+ OP1_3 OP2_1 OP3_2 Rd imm16)
116805caefcfSchristos     (set-psw Rd (index-of Rd) (xor Rd imm16) 1)
116905caefcfSchristos     ()
117005caefcfSchristos)
117105caefcfSchristos
117205caefcfSchristos(dni notgr
117305caefcfSchristos     "NOT general register"
117405caefcfSchristos     ()
117505caefcfSchristos     ("not $Rd")
117605caefcfSchristos     (+ OP1_3 OP2_0 OP3_B Rd)
117705caefcfSchristos     (set-psw Rd (index-of Rd) (inv Rd) 1)
117805caefcfSchristos     ()
117905caefcfSchristos)
118005caefcfSchristos
118105caefcfSchristos; Arithmetic operations
118205caefcfSchristos(dni addgrgr
118305caefcfSchristos     "ADD general register to general register"
118405caefcfSchristos     ()
118505caefcfSchristos     ("add $Rd,$Rs")
118605caefcfSchristos     (+ OP1_4 OP2_9 Rs Rd)
118705caefcfSchristos     (set-psw-add Rd (index-of Rd) Rd Rs 0)
118805caefcfSchristos     ()
118905caefcfSchristos)
119005caefcfSchristos
119105caefcfSchristos(dni addgrimm4
119205caefcfSchristos     "ADD 4-bit immediate to general register"
119305caefcfSchristos     ()
119405caefcfSchristos     ("add $Rd,#$imm4")
119505caefcfSchristos     (+ OP1_5 OP2_1 imm4 Rd)
119605caefcfSchristos     (set-psw-add Rd (index-of Rd) Rd imm4 0)
119705caefcfSchristos     ()
119805caefcfSchristos)
119905caefcfSchristos
120005caefcfSchristos(dni addimm8
120105caefcfSchristos     "ADD 8-bit immediate"
120205caefcfSchristos     ()
120305caefcfSchristos     ("add Rx,#$imm8")
120405caefcfSchristos     (+ OP1_5 OP2_9 imm8)
120505caefcfSchristos     (set-psw-add (reg HI h-gr Rpsw) Rpsw (reg HI h-gr Rpsw) imm8 0)
120605caefcfSchristos     ()
120705caefcfSchristos)
120805caefcfSchristos
120905caefcfSchristos(dni addgrimm16
121005caefcfSchristos     "ADD 16-bit immediate to general register"
121105caefcfSchristos     ()
121205caefcfSchristos     ("add $Rd,#$imm16")
121305caefcfSchristos     (+ OP1_3 OP2_1 OP3_4 Rd imm16)
121405caefcfSchristos     (set-psw-add Rd (index-of Rd) Rd imm16 0)
121505caefcfSchristos     ()
121605caefcfSchristos)
121705caefcfSchristos
121805caefcfSchristos(dni adcgrgr
121905caefcfSchristos     "ADD carry and general register to general register"
122005caefcfSchristos     ()
122105caefcfSchristos     ("adc $Rd,$Rs")
122205caefcfSchristos     (+ OP1_4 OP2_B Rs Rd)
122305caefcfSchristos     (set-psw-add Rd (index-of Rd) Rd Rs psw-cy)
122405caefcfSchristos     ()
122505caefcfSchristos)
122605caefcfSchristos
122705caefcfSchristos(dni adcgrimm4
122805caefcfSchristos     "ADD carry and 4-bit immediate to general register"
122905caefcfSchristos     ()
123005caefcfSchristos     ("adc $Rd,#$imm4")
123105caefcfSchristos     (+ OP1_5 OP2_3 imm4 Rd)
123205caefcfSchristos     (set-psw-add Rd (index-of Rd) Rd imm4 psw-cy)
123305caefcfSchristos     ()
123405caefcfSchristos)
123505caefcfSchristos
123605caefcfSchristos(dni adcimm8
123705caefcfSchristos     "ADD carry and 8-bit immediate"
123805caefcfSchristos     ()
123905caefcfSchristos     ("adc Rx,#$imm8")
124005caefcfSchristos     (+ OP1_5 OP2_B imm8)
124105caefcfSchristos     (set-psw-add (reg HI h-gr Rpsw) Rpsw (reg HI h-gr Rpsw) imm8 psw-cy)
124205caefcfSchristos     ()
124305caefcfSchristos)
124405caefcfSchristos
124505caefcfSchristos(dni adcgrimm16
124605caefcfSchristos     "ADD carry and 16-bit immediate to general register"
124705caefcfSchristos     ()
124805caefcfSchristos     ("adc $Rd,#$imm16")
124905caefcfSchristos     (+ OP1_3 OP2_1 OP3_5 Rd imm16)
125005caefcfSchristos     (set-psw-add Rd (index-of Rd) Rd imm16 psw-cy)
125105caefcfSchristos     ()
125205caefcfSchristos)
125305caefcfSchristos
125405caefcfSchristos(dni subgrgr
125505caefcfSchristos     "SUB general register from general register"
125605caefcfSchristos     ()
125705caefcfSchristos     ("sub $Rd,$Rs")
125805caefcfSchristos     (+ OP1_4 OP2_D Rs Rd)
125905caefcfSchristos     (set-psw-sub Rd (index-of Rd) Rd Rs 0)
126005caefcfSchristos     ()
126105caefcfSchristos)
126205caefcfSchristos
126305caefcfSchristos(dni subgrimm4
126405caefcfSchristos     "SUB 4-bit immediate from general register"
126505caefcfSchristos     ()
126605caefcfSchristos     ("sub $Rd,#$imm4")
126705caefcfSchristos     (+ OP1_5 OP2_5 imm4 Rd)
126805caefcfSchristos     (set-psw-sub Rd (index-of Rd) Rd imm4 0)
126905caefcfSchristos     ()
127005caefcfSchristos)
127105caefcfSchristos
127205caefcfSchristos(dni subimm8
127305caefcfSchristos     "SUB 8-bit immediate"
127405caefcfSchristos     ()
127505caefcfSchristos     ("sub Rx,#$imm8")
127605caefcfSchristos     (+ OP1_5 OP2_D imm8)
127705caefcfSchristos     (set-psw-sub (reg HI h-gr Rpsw) Rpsw (reg HI h-gr Rpsw) imm8 0)
127805caefcfSchristos     ()
127905caefcfSchristos)
128005caefcfSchristos
128105caefcfSchristos(dni subgrimm16
128205caefcfSchristos     "SUB 16-bit immediate from general register"
128305caefcfSchristos     ()
128405caefcfSchristos     ("sub $Rd,#$imm16")
128505caefcfSchristos     (+ OP1_3 OP2_1 OP3_6 Rd imm16)
128605caefcfSchristos     (set-psw-sub Rd (index-of Rd) Rd imm16 0)
128705caefcfSchristos     ()
128805caefcfSchristos)
128905caefcfSchristos
129005caefcfSchristos(dni sbcgrgr
129105caefcfSchristos     "SUB carry and general register from general register"
129205caefcfSchristos     ()
129305caefcfSchristos     ("sbc $Rd,$Rs")
129405caefcfSchristos     (+ OP1_4 OP2_F Rs Rd)
129505caefcfSchristos     (set-psw-sub Rd (index-of Rd) Rd Rs psw-cy)
129605caefcfSchristos     ()
129705caefcfSchristos)
129805caefcfSchristos
129905caefcfSchristos(dni sbcgrimm4
130005caefcfSchristos     "SUB carry and 4-bit immediate from general register"
130105caefcfSchristos     ()
130205caefcfSchristos     ("sbc $Rd,#$imm4")
130305caefcfSchristos     (+ OP1_5 OP2_7 imm4 Rd)
130405caefcfSchristos     (set-psw-sub Rd (index-of Rd) Rd imm4 psw-cy)
130505caefcfSchristos     ()
130605caefcfSchristos)
130705caefcfSchristos
130805caefcfSchristos(dni sbcgrimm8
130905caefcfSchristos     "SUB carry and 8-bit immediate"
131005caefcfSchristos     ()
131105caefcfSchristos     ("sbc Rx,#$imm8")
131205caefcfSchristos     (+ OP1_5 OP2_F imm8)
131305caefcfSchristos     (set-psw-sub (reg HI h-gr Rpsw) Rpsw (reg HI h-gr Rpsw) imm8 psw-cy)
131405caefcfSchristos     ()
131505caefcfSchristos)
131605caefcfSchristos
131705caefcfSchristos(dni sbcgrimm16
131805caefcfSchristos     "SUB carry and 16-bit immediate from general register"
131905caefcfSchristos     ()
132005caefcfSchristos     ("sbc $Rd,#$imm16")
132105caefcfSchristos     (+ OP1_3 OP2_1 OP3_7 Rd imm16)
132205caefcfSchristos     (set-psw-sub Rd (index-of Rd) Rd imm16 psw-cy)
132305caefcfSchristos     ()
132405caefcfSchristos)
132505caefcfSchristos
132605caefcfSchristos(dnmi incgr
132705caefcfSchristos     "Increment general register"
132805caefcfSchristos     ()
132905caefcfSchristos     ("inc $Rd")
133005caefcfSchristos     (emit incgrimm2 Rd (imm2 0))
133105caefcfSchristos)
133205caefcfSchristos
133305caefcfSchristos(dni incgrimm2
133405caefcfSchristos     "Increment general register by 2-bit immediate"
133505caefcfSchristos     ()
133605caefcfSchristos     ("inc $Rd,#$imm2")
133705caefcfSchristos     (+ OP1_3 OP2_0 OP3A_0 imm2 Rd)
133805caefcfSchristos     (set-psw Rd (index-of Rd) (add Rd (add imm2 1)) 1)
133905caefcfSchristos     ()
134005caefcfSchristos)
134105caefcfSchristos
134205caefcfSchristos(dnmi decgr
134305caefcfSchristos     "Decrement general register"
134405caefcfSchristos     ()
134505caefcfSchristos     ("dec $Rd")
134605caefcfSchristos     (emit decgrimm2 Rd (imm2 0))
134705caefcfSchristos)
134805caefcfSchristos
134905caefcfSchristos(dni decgrimm2
135005caefcfSchristos     "Decrement general register by 2-bit immediate"
135105caefcfSchristos     ()
135205caefcfSchristos     ("dec $Rd,#$imm2")
135305caefcfSchristos     (+ OP1_3 OP2_0 OP3A_1 imm2 Rd)
135405caefcfSchristos     (set-psw Rd (index-of Rd) (sub Rd (add imm2 1)) 1)
135505caefcfSchristos     ()
135605caefcfSchristos)
135705caefcfSchristos
135805caefcfSchristos; Logical Shift
135905caefcfSchristos(dni rrcgrgr
136005caefcfSchristos     "Rotate right general register by general register"
136105caefcfSchristos     ()
136205caefcfSchristos     ("rrc $Rd,$Rs")
136305caefcfSchristos     (+ OP1_3 OP2_8 Rs Rd)
136405caefcfSchristos     (set-psw-rrotate17 Rd (index-of Rd) Rd psw-cy Rs)
136505caefcfSchristos     ()
136605caefcfSchristos)
136705caefcfSchristos
136805caefcfSchristos(dni rrcgrimm4
136905caefcfSchristos     "Rotate right general register by immediate"
137005caefcfSchristos     ()
137105caefcfSchristos     ("rrc $Rd,#$imm4")
137205caefcfSchristos     (+ OP1_3 OP2_9 imm4 Rd)
137305caefcfSchristos     (set-psw-rrotate17 Rd (index-of Rd) Rd psw-cy imm4)
137405caefcfSchristos     ()
137505caefcfSchristos)
137605caefcfSchristos
137705caefcfSchristos(dni rlcgrgr
137805caefcfSchristos     "Rotate left general register by general register"
137905caefcfSchristos     ()
138005caefcfSchristos     ("rlc $Rd,$Rs")
138105caefcfSchristos     (+ OP1_3 OP2_A Rs Rd)
138205caefcfSchristos     (set-psw-rotate17 Rd (index-of Rd) Rd psw-cy (and Rs #xF))
138305caefcfSchristos     ()
138405caefcfSchristos)
138505caefcfSchristos
138605caefcfSchristos(dni rlcgrimm4
138705caefcfSchristos     "Rotate left general register by immediate"
138805caefcfSchristos     ()
138905caefcfSchristos     ("rlc $Rd,#$imm4")
139005caefcfSchristos     (+ OP1_3 OP2_B imm4 Rd)
139105caefcfSchristos     (set-psw-rotate17 Rd (index-of Rd) Rd psw-cy imm4)
139205caefcfSchristos     ()
139305caefcfSchristos)
139405caefcfSchristos
139505caefcfSchristos(dni shrgrgr
139605caefcfSchristos     "Shift right general register by general register"
139705caefcfSchristos     ()
139805caefcfSchristos     ("shr $Rd,$Rs")
139905caefcfSchristos     (+ OP1_3 OP2_C Rs Rd)
140005caefcfSchristos     (set-psw-carry Rd (index-of Rd)
140105caefcfSchristos		    (srl Rd (and Rs #xF))
140205caefcfSchristos		    (and SI (if SI (eq (and Rs #xF) 0)
140305caefcfSchristos			     psw-cy
140405caefcfSchristos			     (srl Rd (sub (and Rs #xF) 1)))
140505caefcfSchristos			 1) 1)
140605caefcfSchristos     ()
140705caefcfSchristos)
140805caefcfSchristos
140905caefcfSchristos(dni shrgrimm
141005caefcfSchristos     "Shift right general register by immediate"
141105caefcfSchristos     ()
141205caefcfSchristos     ("shr $Rd,#$imm4")
141305caefcfSchristos     (+ OP1_3 OP2_D imm4 Rd)
141405caefcfSchristos     (set-psw-carry Rd (index-of Rd)
141505caefcfSchristos		    (srl Rd imm4)
141605caefcfSchristos		    (and SI (if SI (eq imm4 0)
141705caefcfSchristos			     psw-cy
141805caefcfSchristos			     (srl Rd (sub imm4 1)))
141905caefcfSchristos			 1) 1)
142005caefcfSchristos     ()
142105caefcfSchristos)
142205caefcfSchristos
142305caefcfSchristos(dni shlgrgr
142405caefcfSchristos     "Shift left general register by general register"
142505caefcfSchristos     ()
142605caefcfSchristos     ("shl $Rd,$Rs")
142705caefcfSchristos     (+ OP1_3 OP2_E Rs Rd)
142805caefcfSchristos     (set-psw-carry Rd (index-of Rd)
142905caefcfSchristos		    (sll Rd (and Rs #xF))
143005caefcfSchristos		    (srl SI (if SI (eq (and Rs #xF) 0)
143105caefcfSchristos			     (sll psw-cy 15)
143205caefcfSchristos			     (sll Rd (sub (and Rs #xF) 1)))
143305caefcfSchristos			 15) 1)
143405caefcfSchristos     ()
143505caefcfSchristos)
143605caefcfSchristos
143705caefcfSchristos(dni shlgrimm
143805caefcfSchristos     "Shift left general register by immediate"
143905caefcfSchristos     ()
144005caefcfSchristos     ("shl $Rd,#$imm4")
144105caefcfSchristos     (+ OP1_3 OP2_F imm4 Rd)
144205caefcfSchristos     (set-psw-carry Rd (index-of Rd)
144305caefcfSchristos		    (sll Rd imm4)
144405caefcfSchristos		    (srl SI (if SI (eq imm4 0)
144505caefcfSchristos			     (sll psw-cy 15)
144605caefcfSchristos			     (sll Rd (sub imm4 1)))
144705caefcfSchristos			 15) 1)
144805caefcfSchristos     ()
144905caefcfSchristos)
145005caefcfSchristos
145105caefcfSchristos(dni asrgrgr
145205caefcfSchristos     "Arithmetic shift right general register by general register"
145305caefcfSchristos     ()
145405caefcfSchristos     ("asr $Rd,$Rs")
145505caefcfSchristos     (+ OP1_3 OP2_6 Rs Rd)
145605caefcfSchristos     (set-psw-carry Rd (index-of Rd)
145705caefcfSchristos		    (sra HI Rd (and Rs #xF))
145805caefcfSchristos		    (and SI (if SI (eq (and Rs #xF) 0)
145905caefcfSchristos			     psw-cy
146005caefcfSchristos			     (srl Rd (sub (and Rs #xF) 1)))
146105caefcfSchristos			 1) 1)
146205caefcfSchristos     ()
146305caefcfSchristos)
146405caefcfSchristos
146505caefcfSchristos(dni asrgrimm
146605caefcfSchristos     "Arithmetic shift right general register by immediate"
146705caefcfSchristos     ()
146805caefcfSchristos     ("asr $Rd,#$imm4")
146905caefcfSchristos     (+ OP1_3 OP2_7 imm4 Rd)
147005caefcfSchristos     (set-psw-carry Rd (index-of Rd)
147105caefcfSchristos		    (sra HI Rd imm4)
147205caefcfSchristos		    (and SI (if SI (eq imm4 0)
147305caefcfSchristos			     psw-cy
147405caefcfSchristos			     (srl Rd (sub imm4 1)))
147505caefcfSchristos			 1) 1)
147605caefcfSchristos     ()
147705caefcfSchristos)
147805caefcfSchristos
147905caefcfSchristos; Bitwise operations
148005caefcfSchristos(dni set1grimm
148105caefcfSchristos     "Set bit in general register by immediate"
148205caefcfSchristos     ()
148305caefcfSchristos     ("set1 $Rd,#$imm4")
148405caefcfSchristos     (+ OP1_0 OP2_9 imm4 Rd)
148505caefcfSchristos     (set-psw Rd (index-of Rd) (or Rd (sll 1 imm4)) 1)
148605caefcfSchristos     ()
148705caefcfSchristos)
148805caefcfSchristos
148905caefcfSchristos(dni set1grgr
149005caefcfSchristos     "Set bit in general register by general register"
149105caefcfSchristos     ()
149205caefcfSchristos     ("set1 $Rd,$Rs")
149305caefcfSchristos     (+ OP1_0 OP2_B Rs Rd)
149405caefcfSchristos     (set-psw Rd (index-of Rd) (or Rd (sll 1 (and Rs #xF))) 1)
149505caefcfSchristos     ()
149605caefcfSchristos)
149705caefcfSchristos
149805caefcfSchristos(dni set1lmemimm
149905caefcfSchristos     "Set bit in low memory by immediate"
150005caefcfSchristos     ()
150105caefcfSchristos     ("set1 $lmem8,#$imm3")
150205caefcfSchristos     (+ OP1_E imm3 OP2M_1 lmem8)
150305caefcfSchristos     (set-mem-psw (mem QI lmem8) (or (mem QI lmem8) (sll 1 imm3)) 0)
150405caefcfSchristos     ()
150505caefcfSchristos)
150605caefcfSchristos(dni set1hmemimm
150705caefcfSchristos     "Set bit in high memory by immediate"
150805caefcfSchristos     ()
150905caefcfSchristos     ("set1 $hmem8,#$imm3")
151005caefcfSchristos     (+ OP1_F imm3 OP2M_1 hmem8)
151105caefcfSchristos     (set-mem-psw (mem QI hmem8) (or (mem QI hmem8) (sll 1 imm3)) 0)
151205caefcfSchristos     ()
151305caefcfSchristos)
151405caefcfSchristos
151505caefcfSchristos(dni clr1grimm
151605caefcfSchristos     "Clear bit in general register by immediate"
151705caefcfSchristos     ()
151805caefcfSchristos     ("clr1 $Rd,#$imm4")
151905caefcfSchristos     (+ OP1_0 OP2_8 imm4 Rd)
152005caefcfSchristos     (set-psw Rd (index-of Rd) (and Rd (inv (sll 1 imm4))) 1)
152105caefcfSchristos     ()
152205caefcfSchristos)
152305caefcfSchristos
152405caefcfSchristos(dni clr1grgr
152505caefcfSchristos     "Clear bit in general register by general register"
152605caefcfSchristos     ()
152705caefcfSchristos     ("clr1 $Rd,$Rs")
152805caefcfSchristos     (+ OP1_0 OP2_A Rs Rd)
152905caefcfSchristos     (set-psw Rd (index-of Rd) (and Rd (inv (sll 1 (and Rs #xF)))) 1)
153005caefcfSchristos     ()
153105caefcfSchristos)
153205caefcfSchristos
153305caefcfSchristos(dni clr1lmemimm
153405caefcfSchristos     "Clear bit in low memory"
153505caefcfSchristos     ()
153605caefcfSchristos     ("clr1 $lmem8,#$imm3")
153705caefcfSchristos     (+ OP1_E imm3 OP2M_0 lmem8)
153805caefcfSchristos     (set-mem-psw (mem QI lmem8) (and (mem QI lmem8) (inv (sll 1 imm3))) 0)
153905caefcfSchristos     ()
154005caefcfSchristos)
154105caefcfSchristos(dni clr1hmemimm
154205caefcfSchristos     "Clear bit in high memory"
154305caefcfSchristos     ()
154405caefcfSchristos     ("clr1 $hmem8,#$imm3")
154505caefcfSchristos     (+ OP1_F imm3 OP2M_0 hmem8)
154605caefcfSchristos     (set-mem-psw (mem QI hmem8) (and (mem QI hmem8) (inv (sll 1 imm3))) 0)
154705caefcfSchristos     ()
154805caefcfSchristos)
154905caefcfSchristos
155005caefcfSchristos; Data conversion
155105caefcfSchristos
155205caefcfSchristos(dni cbwgr
155305caefcfSchristos     "Sign-extend byte in general register"
155405caefcfSchristos     ()
155505caefcfSchristos     ("cbw $Rd")
155605caefcfSchristos     (+ OP1_3 OP2_0 OP3_A Rd)
155705caefcfSchristos     (set-psw Rd (index-of Rd) (ext HI (trunc QI Rd)) 1)
155805caefcfSchristos     ()
155905caefcfSchristos)
156005caefcfSchristos
156105caefcfSchristos(dni revgr
156205caefcfSchristos     "Reverse bit pattern in general register"
156305caefcfSchristos     ()
156405caefcfSchristos     ("rev $Rd")
156505caefcfSchristos     (+ OP1_3 OP2_0 OP3_F Rd)
156605caefcfSchristos     (set-psw Rd (index-of Rd)
156705caefcfSchristos       (or (sll (and Rd #x0001) 15)
156805caefcfSchristos       (or (sll (and Rd #x0002) 13)
156905caefcfSchristos       (or (sll (and Rd #x0004) 11)
157005caefcfSchristos       (or (sll (and Rd #x0008) 9)
157105caefcfSchristos       (or (sll (and Rd #x0010) 7)
157205caefcfSchristos       (or (sll (and Rd #x0020) 5)
157305caefcfSchristos       (or (sll (and Rd #x0040) 3)
157405caefcfSchristos       (or (sll (and Rd #x0080) 1)
157505caefcfSchristos       (or (srl (and Rd #x0100) 1)
157605caefcfSchristos       (or (srl (and Rd #x0200) 3)
157705caefcfSchristos       (or (srl (and Rd #x0400) 5)
157805caefcfSchristos       (or (srl (and Rd #x0800) 7)
157905caefcfSchristos       (or (srl (and Rd #x1000) 9)
158005caefcfSchristos       (or (srl (and Rd #x2000) 11)
158105caefcfSchristos       (or (srl (and Rd #x4000) 13)
158205caefcfSchristos           (srl (and Rd #x8000) 15))))))))))))))))
158305caefcfSchristos       1)
158405caefcfSchristos     ()
158505caefcfSchristos)
158605caefcfSchristos
158705caefcfSchristos; Conditional Branches
158805caefcfSchristos
158905caefcfSchristos(define-pmacro (cbranch cond dest)
159005caefcfSchristos  (sequence ((BI tmp))
159105caefcfSchristos	    (case cond
159205caefcfSchristos	      ((0)  (set tmp (not (xor psw-s psw-ov))))			; ge
159305caefcfSchristos	      ((1)  (set tmp (not psw-cy)))				; nc
159405caefcfSchristos	      ((2)  (set tmp (xor psw-s psw-ov)))			; lt
159505caefcfSchristos	      ((3)  (set tmp psw-cy))					; c
159605caefcfSchristos	      ((4)  (set tmp (not (or (xor psw-s psw-ov) psw-z16))))	; gt
159705caefcfSchristos	      ((5)  (set tmp (not (or psw-cy psw-z16))))		; hi
159805caefcfSchristos	      ((6)  (set tmp (or (xor psw-s psw-ov) psw-z16)))		; le
159905caefcfSchristos	      ((7)  (set tmp (or psw-cy psw-z16)))			; ls
160005caefcfSchristos	      ((8)  (set tmp (not psw-s)))				; pl
160105caefcfSchristos	      ((9)  (set tmp (not psw-ov)))				; nv
160205caefcfSchristos	      ((10) (set tmp psw-s))					; mi
160305caefcfSchristos	      ((11) (set tmp psw-ov))					; v
160405caefcfSchristos	      ((12) (set tmp (not psw-z8)))				; nz.b
160505caefcfSchristos	      ((13) (set tmp (not psw-z16)))				; nz
160605caefcfSchristos	      ((14) (set tmp psw-z8))					; z.b
160705caefcfSchristos	      ((15) (set tmp psw-z16)))					; z
160805caefcfSchristos	    (if tmp (set pc dest)))
160905caefcfSchristos)
161005caefcfSchristos
161105caefcfSchristos(dni bccgrgr
161205caefcfSchristos     "Conditional branch comparing general register with general register"
161305caefcfSchristos     ()
161405caefcfSchristos     ("b$bcond5 $Rd,$Rs,$rel12")
161505caefcfSchristos     (+ OP1_0 OP2_D Rs Rd bcond5 rel12)
161605caefcfSchristos     (sequence ()
161705caefcfSchristos	       (set-psw-cmp Rd (index-of Rd) Rd Rs)
161805caefcfSchristos	       (cbranch bcond5 rel12))
161905caefcfSchristos     ()
162005caefcfSchristos)
162105caefcfSchristos
162205caefcfSchristos; 4 bytes
162305caefcfSchristos(dni bccgrimm8
162405caefcfSchristos     "Conditional branch comparing general register with 8-bit immediate"
162505caefcfSchristos     ()
162605caefcfSchristos     ("b$bcond5 $Rm,#$imm8,$rel12")
162705caefcfSchristos     (+ OP1_2 OP2M_0 Rm imm8 bcond5 rel12)
162805caefcfSchristos     (sequence ()
162905caefcfSchristos	       (set-psw-cmp Rm (index-of Rm) Rm imm8)
163005caefcfSchristos	       (cbranch bcond5 rel12))
163105caefcfSchristos     ()
163205caefcfSchristos)
163305caefcfSchristos
163405caefcfSchristos; 4 bytes
163505caefcfSchristos(dni bccimm16
163605caefcfSchristos     "Conditional branch comparing general register with 16-bit immediate"
163705caefcfSchristos     ()
163805caefcfSchristos     ("b$bcond2 Rx,#$imm16,${rel8-4}")
163905caefcfSchristos     (+ OP1_C bcond2 rel8-4 imm16)
164005caefcfSchristos     (sequence ()
164105caefcfSchristos	       (set-psw-cmp (reg HI h-gr Rpsw) Rpsw (reg HI h-gr Rpsw) imm16)
164205caefcfSchristos	       (cbranch bcond2 rel8-4))
164305caefcfSchristos     ()
164405caefcfSchristos)
164505caefcfSchristos
164605caefcfSchristos(dni bngrimm4
164705caefcfSchristos     "Test bit in general register by immediate and branch if 0"
164805caefcfSchristos     ()
164905caefcfSchristos     ("bn $Rd,#$imm4,$rel12")
165005caefcfSchristos     (+ OP1_0 OP2_4 imm4 Rd OP5_0 rel12)
165105caefcfSchristos     (sequence ()
165205caefcfSchristos	       (set Rpsw (index-of Rd))
165305caefcfSchristos	       (if (eq (and Rd (sll 1 imm4)) 0)
165405caefcfSchristos		   (set pc rel12)))
165505caefcfSchristos     ()
165605caefcfSchristos)
165705caefcfSchristos
165805caefcfSchristos(dni bngrgr
165905caefcfSchristos     "Test bit in general register by general register and branch if 0"
166005caefcfSchristos     ()
166105caefcfSchristos     ("bn $Rd,$Rs,$rel12")
166205caefcfSchristos     (+ OP1_0 OP2_6 Rs Rd OP5_0 rel12)
166305caefcfSchristos     (sequence ()
166405caefcfSchristos	       (set Rpsw (index-of Rd))
166505caefcfSchristos	       (if (eq (and Rd (sll 1 Rs)) 0)
166605caefcfSchristos		   (set pc rel12)))
166705caefcfSchristos     ()
166805caefcfSchristos)
166905caefcfSchristos
167005caefcfSchristos(dni bnlmemimm
167105caefcfSchristos     "Test bit in memory by immediate and branch if 0"
167205caefcfSchristos     ()
167305caefcfSchristos     ("bn $lmem8,#$imm3b,$rel12")
167405caefcfSchristos     (+ OP1_7 OP2_C lmem8 OP5A_0 imm3b rel12)
167505caefcfSchristos     (if (eq (and (mem QI lmem8) (sll 1 imm3b)) 0)
167605caefcfSchristos	 (set pc rel12))
167705caefcfSchristos     ()
167805caefcfSchristos)
167905caefcfSchristos
168005caefcfSchristos(dni bnhmemimm
168105caefcfSchristos     "Test bit in memory by immediate and branch if 0"
168205caefcfSchristos     ()
168305caefcfSchristos     ("bn $hmem8,#$imm3b,$rel12")
168405caefcfSchristos     (+ OP1_7 OP2_E hmem8 OP5A_0 imm3b rel12)
168505caefcfSchristos     (if (eq (and (mem QI hmem8) (sll 1 imm3b)) 0)
168605caefcfSchristos	 (set pc rel12))
168705caefcfSchristos     ()
168805caefcfSchristos)
168905caefcfSchristos
169005caefcfSchristos(dni bpgrimm4
169105caefcfSchristos     "Test bit in general register by immediate and branch if 1"
169205caefcfSchristos     ()
169305caefcfSchristos     ("bp $Rd,#$imm4,$rel12")
169405caefcfSchristos     (+ OP1_0 OP2_5 imm4 Rd OP5_0 rel12)
169505caefcfSchristos     (sequence ()
169605caefcfSchristos	       (set Rpsw (index-of Rd))
169705caefcfSchristos	       (if (ne (and Rd (sll 1 imm4)) 0)
169805caefcfSchristos		   (set pc rel12)))
169905caefcfSchristos     ()
170005caefcfSchristos)
170105caefcfSchristos
170205caefcfSchristos(dni bpgrgr
170305caefcfSchristos     "Test bit in general register by general register and branch if 1"
170405caefcfSchristos     ()
170505caefcfSchristos     ("bp $Rd,$Rs,$rel12")
170605caefcfSchristos     (+ OP1_0 OP2_7 Rs Rd OP5_0 rel12)
170705caefcfSchristos     (sequence ()
170805caefcfSchristos	       (set Rpsw (index-of Rd))
170905caefcfSchristos	       (if (ne (and Rd (sll 1 Rs)) 0)
171005caefcfSchristos		   (set pc rel12)))
171105caefcfSchristos     ()
171205caefcfSchristos)
171305caefcfSchristos
171405caefcfSchristos(dni bplmemimm
171505caefcfSchristos     "Test bit in memory by immediate and branch if 1"
171605caefcfSchristos     ()
171705caefcfSchristos     ("bp $lmem8,#$imm3b,$rel12")
171805caefcfSchristos     (+ OP1_7 OP2_D lmem8 OP5A_0 imm3b rel12)
171905caefcfSchristos     (if (ne (and (mem QI lmem8) (sll 1 imm3b)) 0)
172005caefcfSchristos	 (set pc rel12))
172105caefcfSchristos     ()
172205caefcfSchristos)
172305caefcfSchristos
172405caefcfSchristos(dni bphmemimm
172505caefcfSchristos     "Test bit in memory by immediate and branch if 1"
172605caefcfSchristos     ()
172705caefcfSchristos     ("bp $hmem8,#$imm3b,$rel12")
172805caefcfSchristos     (+ OP1_7 OP2_F hmem8 OP5A_0 imm3b rel12)
172905caefcfSchristos     (if (ne (and (mem QI hmem8) (sll 1 imm3b)) 0)
173005caefcfSchristos	 (set pc rel12))
173105caefcfSchristos     ()
173205caefcfSchristos)
173305caefcfSchristos
173405caefcfSchristos(dni bcc
173505caefcfSchristos     "Conditional branch on flag registers"
173605caefcfSchristos     ()
173705caefcfSchristos     ("b$bcond2 ${rel8-2}")
173805caefcfSchristos     (+ OP1_D bcond2 rel8-2)
173905caefcfSchristos     (cbranch bcond2 rel8-2)
174005caefcfSchristos     ()
174105caefcfSchristos)
174205caefcfSchristos
174305caefcfSchristos; Unconditional Branching
174405caefcfSchristos
174505caefcfSchristos(dni bgr
174605caefcfSchristos     "Branch to register"
174705caefcfSchristos     ()
174805caefcfSchristos     ("br $Rd")
174905caefcfSchristos     (+ OP1_0 OP2_0 OP3_2 Rd)
175005caefcfSchristos     (set pc (add (add pc 2) Rd))
175105caefcfSchristos     ()
175205caefcfSchristos)
175305caefcfSchristos
175405caefcfSchristos(dni br
175505caefcfSchristos     "Branch"
175605caefcfSchristos     ()
175705caefcfSchristos     ("br $rel12a")
175805caefcfSchristos     (+ OP1_1 rel12a OP4B_0)
175905caefcfSchristos     (set pc rel12a)
176005caefcfSchristos     ()
176105caefcfSchristos)
176205caefcfSchristos
176305caefcfSchristos(dni jmp
176405caefcfSchristos     "Jump"
176505caefcfSchristos     ()
176605caefcfSchristos     ("jmp $Rbj,$Rd")
176705caefcfSchristos     (+ OP1_0 OP2_0 OP3B_4 Rbj Rd)
176805caefcfSchristos     (set pc (join SI HI Rbj Rd))
176905caefcfSchristos     ()
177005caefcfSchristos)
177105caefcfSchristos
177205caefcfSchristos(dni jmpf
177305caefcfSchristos     "Jump far"
177405caefcfSchristos     ()
177505caefcfSchristos     ("jmpf $abs24")
177605caefcfSchristos     (+ OP1_0 OP2_2 abs24)
177705caefcfSchristos     (set pc abs24)
177805caefcfSchristos     ()
177905caefcfSchristos)
178005caefcfSchristos
178105caefcfSchristos; Call instructions
178205caefcfSchristos(define-pmacro (do-call dest ilen)
178305caefcfSchristos  (sequence ()
178405caefcfSchristos	    (set (mem SI sp) (add pc ilen))
178505caefcfSchristos	    (set sp (add sp 4))
178605caefcfSchristos	    (set pc dest)))
178705caefcfSchristos
178805caefcfSchristos(dni callrgr
178905caefcfSchristos     "Call relative to general register"
179005caefcfSchristos     ()
179105caefcfSchristos     ("callr $Rd")
179205caefcfSchristos     (+ OP1_0 OP2_0 OP3_1 Rd)
179305caefcfSchristos     (do-call (add Rd (add pc 2)) 2)
179405caefcfSchristos     ()
179505caefcfSchristos)
179605caefcfSchristos
179705caefcfSchristos(dni callrimm
179805caefcfSchristos     "Call relative to immediate address"
179905caefcfSchristos     ()
180005caefcfSchristos     ("callr $rel12a")
180105caefcfSchristos     (+ OP1_1 rel12a OP4B_1)
180205caefcfSchristos     (do-call rel12a 2)
180305caefcfSchristos     ()
180405caefcfSchristos)
180505caefcfSchristos
180605caefcfSchristos(dni callgr
180705caefcfSchristos     "Call to general registers"
180805caefcfSchristos     ()
180905caefcfSchristos     ("call $Rbj,$Rd")
181005caefcfSchristos     (+ OP1_0 OP2_0 OP3B_A Rbj Rd)
181105caefcfSchristos     (do-call (join SI HI Rbj Rd) 2)
181205caefcfSchristos     ()
181305caefcfSchristos)
181405caefcfSchristos
181505caefcfSchristos(dni callfimm
181605caefcfSchristos     "Call far to absolute address"
181705caefcfSchristos     ()
181805caefcfSchristos     ("callf $abs24")
181905caefcfSchristos     (+ OP1_0 OP2_1 abs24)
182005caefcfSchristos     (do-call abs24 4)
182105caefcfSchristos     ()
182205caefcfSchristos)
182305caefcfSchristos
182405caefcfSchristos(define-pmacro (do-calli dest ilen)
182505caefcfSchristos  (sequence ()
182605caefcfSchristos	    (set (mem SI sp) (add pc ilen))
182705caefcfSchristos	    (set (mem HI (add sp 4)) psw)
182805caefcfSchristos	    (set sp (add sp 6))
182905caefcfSchristos	    (set pc dest)))
183005caefcfSchristos
183105caefcfSchristos(dni icallrgr
183205caefcfSchristos     "Call interrupt to general registers pc-relative"
183305caefcfSchristos     ()
183405caefcfSchristos     ("icallr $Rd")
183505caefcfSchristos     (+ OP1_0 OP2_0 OP3_3 Rd)
183605caefcfSchristos     (do-calli (add Rd (add pc 2)) 2)
183705caefcfSchristos     ()
183805caefcfSchristos)
183905caefcfSchristos
184005caefcfSchristos(dni icallgr
184105caefcfSchristos     "Call interrupt to general registers"
184205caefcfSchristos     ()
184305caefcfSchristos     ("icall $Rbj,$Rd")
184405caefcfSchristos     (+ OP1_0 OP2_0 OP3B_6 Rbj Rd)
184505caefcfSchristos     (do-calli (join SI HI Rbj Rd) 2)
184605caefcfSchristos     ()
184705caefcfSchristos)
184805caefcfSchristos
184905caefcfSchristos(dni icallfimm
185005caefcfSchristos     "Call interrupt far to absolute address"
185105caefcfSchristos     ()
185205caefcfSchristos     ("icallf $abs24")
185305caefcfSchristos     (+ OP1_0 OP2_3 abs24)
185405caefcfSchristos     (do-calli abs24 4)
185505caefcfSchristos     ()
185605caefcfSchristos)
185705caefcfSchristos
185805caefcfSchristos; Return instructions
185905caefcfSchristos(dni iret
186005caefcfSchristos     "Return from interrupt"
186105caefcfSchristos     ()
186205caefcfSchristos     ("iret")
186305caefcfSchristos     (+ (f-op #x0002))
186405caefcfSchristos     (sequence ()
186505caefcfSchristos	       (set sp (sub sp 6))
186605caefcfSchristos	       (set pc (mem SI sp))
186705caefcfSchristos	       (set psw (mem HI (add sp 4))))
186805caefcfSchristos     ()
186905caefcfSchristos)
187005caefcfSchristos
187105caefcfSchristos(dni ret
187205caefcfSchristos     "Return"
187305caefcfSchristos     ()
187405caefcfSchristos     ("ret")
187505caefcfSchristos     (+ (f-op #x0003))
187605caefcfSchristos     (sequence ()
187705caefcfSchristos	       (set sp (sub sp 4))
187805caefcfSchristos	       (set pc (mem SI sp)))
187905caefcfSchristos     ()
188005caefcfSchristos)
188105caefcfSchristos
188205caefcfSchristos; Multiply and Divide instructions
188305caefcfSchristos
188405caefcfSchristos(dni mul
188505caefcfSchristos     "Multiply"
188605caefcfSchristos     ()
188705caefcfSchristos     ("mul")
188805caefcfSchristos     (+ (f-op #x00D0))
188905caefcfSchristos     (sequence ((SI value))
189005caefcfSchristos	       (set value (mul SI (and SI R0 #xFFFF) (and SI R2 #xFFFF)))
189105caefcfSchristos	       (set psw (or (and psw #xFF9C)
189205caefcfSchristos			    (basic-psw (trunc HI value) 1)))
189305caefcfSchristos	       (set R0 (trunc HI value))
189405caefcfSchristos	       (set R1 (trunc HI (srl value 16))))
189505caefcfSchristos     ()
189605caefcfSchristos)
189705caefcfSchristos(dni div
189805caefcfSchristos     "Divide"
189905caefcfSchristos     ()
190005caefcfSchristos     ("div")
190105caefcfSchristos     (+ (f-op #x00C0))
190205caefcfSchristos     (sequence ()
190305caefcfSchristos	       (set R1 (umod R0 R2))
190405caefcfSchristos	       (set-mem-psw R0 (udiv R0 R2) 1))
190505caefcfSchristos     ()
190605caefcfSchristos)
190705caefcfSchristos(dni sdiv
190805caefcfSchristos     "Signed Divide"
190905caefcfSchristos     ()
191005caefcfSchristos     ("sdiv")
191105caefcfSchristos     (+ (f-op #x00C8))
191205caefcfSchristos     (sequence ()
191305caefcfSchristos	       (set R1 (mod HI R0 R2))
191405caefcfSchristos	       (set-mem-psw R0 (div HI R0 R2) 1))
191505caefcfSchristos     ()
191605caefcfSchristos)
191705caefcfSchristos(dni sdivlh
191805caefcfSchristos     "Divide 32/16"
191905caefcfSchristos     ()
192005caefcfSchristos     ("sdivlh")
192105caefcfSchristos     (+ (f-op #x00E8))
192205caefcfSchristos     (sequence ((SI value))
192305caefcfSchristos	       (set value (add SI (sll SI (and SI R1 #xffff) #x10) (and SI R0 #xffff)))
192405caefcfSchristos	       (set R1 (mod SI value (ext SI (trunc HI R2))))
192505caefcfSchristos	       (set-mem-psw R0 (div SI value (ext SI (trunc HI R2))) 1))
192605caefcfSchristos     ()
192705caefcfSchristos)
192805caefcfSchristos(dni divlh
192905caefcfSchristos     "Divide 32/16"
193005caefcfSchristos     ()
193105caefcfSchristos     ("divlh")
193205caefcfSchristos     (+ (f-op #x00E0))
193305caefcfSchristos     (sequence ((SI value))
193405caefcfSchristos	       (set value (add SI (sll SI (and SI R1 #xffff) #x10) (and SI R0 #xffff)))
193505caefcfSchristos	       (set R1 (umod SI value R2))
193605caefcfSchristos	       (set-mem-psw R0 (udiv SI value R2) 1))
193705caefcfSchristos     ()
193805caefcfSchristos)
193905caefcfSchristos
194005caefcfSchristos; System Control
194105caefcfSchristos
194205caefcfSchristos; added per sanyo's req -- eq to nop for the moment, but can
194305caefcfSchristos; add function later
194405caefcfSchristos(dni reset "reset" () ("reset") (+ (f-op #x000f)) (nop) ())
194505caefcfSchristos
194605caefcfSchristos(dni nop "nop" () ("nop") (+ (f-op #x0000)) (nop) ())
194705caefcfSchristos
194805caefcfSchristos(dni halt "halt" () ("halt") (+ (f-op #x0008)) (c-call VOID "do_halt") ())
194905caefcfSchristos
195005caefcfSchristos(dni hold "hold" () ("hold") (+ (f-op #x000A)) (c-call VOID "do_hold") ())
195105caefcfSchristos
195205caefcfSchristos(dni holdx "holdx" () ("holdx") (+ (f-op #x000B)) (c-call VOID "do_holdx") ())
195305caefcfSchristos
195405caefcfSchristos(dni brk "brk" () ("brk") (+ (f-op #x0005)) (c-call VOID "do_brk") ())
195505caefcfSchristos
195605caefcfSchristos; An instruction for test instrumentation.
195705caefcfSchristos; Using a reserved opcode.
195805caefcfSchristos(dni syscall
195905caefcfSchristos  "simulator system call"
196005caefcfSchristos  ()
196105caefcfSchristos  ("--unused--")
196205caefcfSchristos  (+ (f-op #x0001))
196305caefcfSchristos  (c-call VOID "syscall")
196405caefcfSchristos  ()
196505caefcfSchristos)
1966