xstormy16.cpu 47 KB

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