interp.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772
  1. /* Simulator for Atmel's AVR core.
  2. Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. Written by Tristan Gingold, AdaCore.
  4. This file is part of GDB, the GNU debugger.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /* This must come before any other includes. */
  16. #include "defs.h"
  17. #include <string.h>
  18. #include "bfd.h"
  19. #include "libiberty.h"
  20. #include "sim/sim.h"
  21. #include "sim-main.h"
  22. #include "sim-base.h"
  23. #include "sim-options.h"
  24. #include "sim-signal.h"
  25. /* As AVR is a 8/16 bits processor, define handy types. */
  26. typedef unsigned short int word;
  27. typedef signed short int sword;
  28. typedef unsigned char byte;
  29. typedef signed char sbyte;
  30. /* Max size of I space (which is always flash on avr). */
  31. #define MAX_AVR_FLASH (128 * 1024)
  32. #define PC_MASK (MAX_AVR_FLASH - 1)
  33. /* Mac size of D space. */
  34. #define MAX_AVR_SRAM (64 * 1024)
  35. #define SRAM_MASK (MAX_AVR_SRAM - 1)
  36. /* D space offset in ELF file. */
  37. #define SRAM_VADDR 0x800000
  38. /* Simulator specific ports. */
  39. #define STDIO_PORT 0x52
  40. #define EXIT_PORT 0x4F
  41. #define ABORT_PORT 0x49
  42. /* GDB defined register numbers. */
  43. #define AVR_SREG_REGNUM 32
  44. #define AVR_SP_REGNUM 33
  45. #define AVR_PC_REGNUM 34
  46. /* Memory mapped registers. */
  47. #define SREG 0x5F
  48. #define REG_SP 0x5D
  49. #define EIND 0x5C
  50. #define RAMPZ 0x5B
  51. #define REGX 0x1a
  52. #define REGY 0x1c
  53. #define REGZ 0x1e
  54. #define REGZ_LO 0x1e
  55. #define REGZ_HI 0x1f
  56. /* Sreg (status) bits. */
  57. #define SREG_I 0x80
  58. #define SREG_T 0x40
  59. #define SREG_H 0x20
  60. #define SREG_S 0x10
  61. #define SREG_V 0x08
  62. #define SREG_N 0x04
  63. #define SREG_Z 0x02
  64. #define SREG_C 0x01
  65. /* In order to speed up emulation we use a simple approach:
  66. a code is associated with each instruction. The pre-decoding occurs
  67. usually once when the instruction is first seen.
  68. This works well because I&D spaces are separated.
  69. Missing opcodes: sleep, spm, wdr (as they are mmcu dependent).
  70. */
  71. enum avr_opcode
  72. {
  73. /* Opcode not yet decoded. */
  74. OP_unknown,
  75. OP_bad,
  76. OP_nop,
  77. OP_rjmp,
  78. OP_rcall,
  79. OP_ret,
  80. OP_reti,
  81. OP_break,
  82. OP_brbs,
  83. OP_brbc,
  84. OP_bset,
  85. OP_bclr,
  86. OP_bld,
  87. OP_bst,
  88. OP_sbrc,
  89. OP_sbrs,
  90. OP_eor,
  91. OP_and,
  92. OP_andi,
  93. OP_or,
  94. OP_ori,
  95. OP_com,
  96. OP_swap,
  97. OP_neg,
  98. OP_out,
  99. OP_in,
  100. OP_cbi,
  101. OP_sbi,
  102. OP_sbic,
  103. OP_sbis,
  104. OP_ldi,
  105. OP_cpse,
  106. OP_cp,
  107. OP_cpi,
  108. OP_cpc,
  109. OP_sub,
  110. OP_sbc,
  111. OP_sbiw,
  112. OP_adiw,
  113. OP_add,
  114. OP_adc,
  115. OP_subi,
  116. OP_sbci,
  117. OP_inc,
  118. OP_dec,
  119. OP_lsr,
  120. OP_ror,
  121. OP_asr,
  122. OP_mul,
  123. OP_muls,
  124. OP_mulsu,
  125. OP_fmul,
  126. OP_fmuls,
  127. OP_fmulsu,
  128. OP_mov,
  129. OP_movw,
  130. OP_push,
  131. OP_pop,
  132. OP_st_X,
  133. OP_st_dec_X,
  134. OP_st_X_inc,
  135. OP_st_Y_inc,
  136. OP_st_dec_Y,
  137. OP_st_Z_inc,
  138. OP_st_dec_Z,
  139. OP_std_Y,
  140. OP_std_Z,
  141. OP_ldd_Y,
  142. OP_ldd_Z,
  143. OP_ld_Z_inc,
  144. OP_ld_dec_Z,
  145. OP_ld_Y_inc,
  146. OP_ld_dec_Y,
  147. OP_ld_X,
  148. OP_ld_X_inc,
  149. OP_ld_dec_X,
  150. OP_lpm,
  151. OP_lpm_Z,
  152. OP_lpm_inc_Z,
  153. OP_elpm,
  154. OP_elpm_Z,
  155. OP_elpm_inc_Z,
  156. OP_ijmp,
  157. OP_icall,
  158. OP_eijmp,
  159. OP_eicall,
  160. /* 2 words opcodes. */
  161. #define OP_2words OP_jmp
  162. OP_jmp,
  163. OP_call,
  164. OP_sts,
  165. OP_lds
  166. };
  167. struct avr_insn_cell
  168. {
  169. /* The insn (16 bits). */
  170. word op;
  171. /* Pre-decoding code. */
  172. enum avr_opcode code : 8;
  173. /* One byte of additional information. */
  174. byte r;
  175. };
  176. /* I&D memories. */
  177. /* TODO: Should be moved to SIM_CPU. */
  178. static struct avr_insn_cell flash[MAX_AVR_FLASH];
  179. static byte sram[MAX_AVR_SRAM];
  180. /* Sign extend a value. */
  181. static int sign_ext (word val, int nb_bits)
  182. {
  183. if (val & (1 << (nb_bits - 1)))
  184. return val | -(1 << nb_bits);
  185. return val;
  186. }
  187. /* Insn field extractors. */
  188. /* Extract xxxx_xxxRx_xxxx_RRRR. */
  189. static inline byte get_r (word op)
  190. {
  191. return (op & 0xf) | ((op >> 5) & 0x10);
  192. }
  193. /* Extract xxxx_xxxxx_xxxx_RRRR. */
  194. static inline byte get_r16 (word op)
  195. {
  196. return 16 + (op & 0xf);
  197. }
  198. /* Extract xxxx_xxxxx_xxxx_xRRR. */
  199. static inline byte get_r16_23 (word op)
  200. {
  201. return 16 + (op & 0x7);
  202. }
  203. /* Extract xxxx_xxxD_DDDD_xxxx. */
  204. static inline byte get_d (word op)
  205. {
  206. return (op >> 4) & 0x1f;
  207. }
  208. /* Extract xxxx_xxxx_DDDD_xxxx. */
  209. static inline byte get_d16 (word op)
  210. {
  211. return 16 + ((op >> 4) & 0x0f);
  212. }
  213. /* Extract xxxx_xxxx_xDDD_xxxx. */
  214. static inline byte get_d16_23 (word op)
  215. {
  216. return 16 + ((op >> 4) & 0x07);
  217. }
  218. /* Extract xxxx_xAAx_xxxx_AAAA. */
  219. static inline byte get_A (word op)
  220. {
  221. return (op & 0x0f) | ((op & 0x600) >> 5);
  222. }
  223. /* Extract xxxx_xxxx_AAAA_Axxx. */
  224. static inline byte get_biA (word op)
  225. {
  226. return (op >> 3) & 0x1f;
  227. }
  228. /* Extract xxxx_KKKK_xxxx_KKKK. */
  229. static inline byte get_K (word op)
  230. {
  231. return (op & 0xf) | ((op & 0xf00) >> 4);
  232. }
  233. /* Extract xxxx_xxKK_KKKK_Kxxx. */
  234. static inline int get_k (word op)
  235. {
  236. return sign_ext ((op & 0x3f8) >> 3, 7);
  237. }
  238. /* Extract xxxx_xxxx_xxDD_xxxx. */
  239. static inline byte get_d24 (word op)
  240. {
  241. return 24 + ((op >> 3) & 6);
  242. }
  243. /* Extract xxxx_xxxx_KKxx_KKKK. */
  244. static inline byte get_k6 (word op)
  245. {
  246. return (op & 0xf) | ((op >> 2) & 0x30);
  247. }
  248. /* Extract xxQx_QQxx_xxxx_xQQQ. */
  249. static inline byte get_q (word op)
  250. {
  251. return (op & 7) | ((op >> 7) & 0x18)| ((op >> 8) & 0x20);
  252. }
  253. /* Extract xxxx_xxxx_xxxx_xBBB. */
  254. static inline byte get_b (word op)
  255. {
  256. return (op & 7);
  257. }
  258. /* AVR is little endian. */
  259. static inline word
  260. read_word (unsigned int addr)
  261. {
  262. return sram[addr] | (sram[addr + 1] << 8);
  263. }
  264. static inline void
  265. write_word (unsigned int addr, word w)
  266. {
  267. sram[addr] = w;
  268. sram[addr + 1] = w >> 8;
  269. }
  270. static inline word
  271. read_word_post_inc (unsigned int addr)
  272. {
  273. word v = read_word (addr);
  274. write_word (addr, v + 1);
  275. return v;
  276. }
  277. static inline word
  278. read_word_pre_dec (unsigned int addr)
  279. {
  280. word v = read_word (addr) - 1;
  281. write_word (addr, v);
  282. return v;
  283. }
  284. static void
  285. update_flags_logic (byte res)
  286. {
  287. sram[SREG] &= ~(SREG_S | SREG_V | SREG_N | SREG_Z);
  288. if (res == 0)
  289. sram[SREG] |= SREG_Z;
  290. if (res & 0x80)
  291. sram[SREG] |= SREG_N | SREG_S;
  292. }
  293. static void
  294. update_flags_add (byte r, byte a, byte b)
  295. {
  296. byte carry;
  297. sram[SREG] &= ~(SREG_H | SREG_S | SREG_V | SREG_N | SREG_Z | SREG_C);
  298. if (r & 0x80)
  299. sram[SREG] |= SREG_N;
  300. carry = (a & b) | (a & ~r) | (b & ~r);
  301. if (carry & 0x08)
  302. sram[SREG] |= SREG_H;
  303. if (carry & 0x80)
  304. sram[SREG] |= SREG_C;
  305. if (((a & b & ~r) | (~a & ~b & r)) & 0x80)
  306. sram[SREG] |= SREG_V;
  307. if (!(sram[SREG] & SREG_N) ^ !(sram[SREG] & SREG_V))
  308. sram[SREG] |= SREG_S;
  309. if (r == 0)
  310. sram[SREG] |= SREG_Z;
  311. }
  312. static void update_flags_sub (byte r, byte a, byte b)
  313. {
  314. byte carry;
  315. sram[SREG] &= ~(SREG_H | SREG_S | SREG_V | SREG_N | SREG_Z | SREG_C);
  316. if (r & 0x80)
  317. sram[SREG] |= SREG_N;
  318. carry = (~a & b) | (b & r) | (r & ~a);
  319. if (carry & 0x08)
  320. sram[SREG] |= SREG_H;
  321. if (carry & 0x80)
  322. sram[SREG] |= SREG_C;
  323. if (((a & ~b & ~r) | (~a & b & r)) & 0x80)
  324. sram[SREG] |= SREG_V;
  325. if (!(sram[SREG] & SREG_N) ^ !(sram[SREG] & SREG_V))
  326. sram[SREG] |= SREG_S;
  327. /* Note: Z is not set. */
  328. }
  329. static enum avr_opcode
  330. decode (unsigned int pc)
  331. {
  332. word op1 = flash[pc].op;
  333. switch ((op1 >> 12) & 0x0f)
  334. {
  335. case 0x0:
  336. switch ((op1 >> 10) & 0x3)
  337. {
  338. case 0x0:
  339. switch ((op1 >> 8) & 0x3)
  340. {
  341. case 0x0:
  342. if (op1 == 0)
  343. return OP_nop;
  344. break;
  345. case 0x1:
  346. return OP_movw;
  347. case 0x2:
  348. return OP_muls;
  349. case 0x3:
  350. if (op1 & 0x80)
  351. {
  352. if (op1 & 0x08)
  353. return OP_fmulsu;
  354. else
  355. return OP_fmuls;
  356. }
  357. else
  358. {
  359. if (op1 & 0x08)
  360. return OP_fmul;
  361. else
  362. return OP_mulsu;
  363. }
  364. }
  365. break;
  366. case 0x1:
  367. return OP_cpc;
  368. case 0x2:
  369. flash[pc].r = SREG_C;
  370. return OP_sbc;
  371. case 0x3:
  372. flash[pc].r = 0;
  373. return OP_add;
  374. }
  375. break;
  376. case 0x1:
  377. switch ((op1 >> 10) & 0x3)
  378. {
  379. case 0x0:
  380. return OP_cpse;
  381. case 0x1:
  382. return OP_cp;
  383. case 0x2:
  384. flash[pc].r = 0;
  385. return OP_sub;
  386. case 0x3:
  387. flash[pc].r = SREG_C;
  388. return OP_adc;
  389. }
  390. break;
  391. case 0x2:
  392. switch ((op1 >> 10) & 0x3)
  393. {
  394. case 0x0:
  395. return OP_and;
  396. case 0x1:
  397. return OP_eor;
  398. case 0x2:
  399. return OP_or;
  400. case 0x3:
  401. return OP_mov;
  402. }
  403. break;
  404. case 0x3:
  405. return OP_cpi;
  406. case 0x4:
  407. return OP_sbci;
  408. case 0x5:
  409. return OP_subi;
  410. case 0x6:
  411. return OP_ori;
  412. case 0x7:
  413. return OP_andi;
  414. case 0x8:
  415. case 0xa:
  416. if (op1 & 0x0200)
  417. {
  418. if (op1 & 0x0008)
  419. {
  420. flash[pc].r = get_q (op1);
  421. return OP_std_Y;
  422. }
  423. else
  424. {
  425. flash[pc].r = get_q (op1);
  426. return OP_std_Z;
  427. }
  428. }
  429. else
  430. {
  431. if (op1 & 0x0008)
  432. {
  433. flash[pc].r = get_q (op1);
  434. return OP_ldd_Y;
  435. }
  436. else
  437. {
  438. flash[pc].r = get_q (op1);
  439. return OP_ldd_Z;
  440. }
  441. }
  442. break;
  443. case 0x9: /* 9xxx */
  444. switch ((op1 >> 8) & 0xf)
  445. {
  446. case 0x0:
  447. case 0x1:
  448. switch ((op1 >> 0) & 0xf)
  449. {
  450. case 0x0:
  451. return OP_lds;
  452. case 0x1:
  453. return OP_ld_Z_inc;
  454. case 0x2:
  455. return OP_ld_dec_Z;
  456. case 0x4:
  457. return OP_lpm_Z;
  458. case 0x5:
  459. return OP_lpm_inc_Z;
  460. case 0x6:
  461. return OP_elpm_Z;
  462. case 0x7:
  463. return OP_elpm_inc_Z;
  464. case 0x9:
  465. return OP_ld_Y_inc;
  466. case 0xa:
  467. return OP_ld_dec_Y;
  468. case 0xc:
  469. return OP_ld_X;
  470. case 0xd:
  471. return OP_ld_X_inc;
  472. case 0xe:
  473. return OP_ld_dec_X;
  474. case 0xf:
  475. return OP_pop;
  476. }
  477. break;
  478. case 0x2:
  479. case 0x3:
  480. switch ((op1 >> 0) & 0xf)
  481. {
  482. case 0x0:
  483. return OP_sts;
  484. case 0x1:
  485. return OP_st_Z_inc;
  486. case 0x2:
  487. return OP_st_dec_Z;
  488. case 0x9:
  489. return OP_st_Y_inc;
  490. case 0xa:
  491. return OP_st_dec_Y;
  492. case 0xc:
  493. return OP_st_X;
  494. case 0xd:
  495. return OP_st_X_inc;
  496. case 0xe:
  497. return OP_st_dec_X;
  498. case 0xf:
  499. return OP_push;
  500. }
  501. break;
  502. case 0x4:
  503. case 0x5:
  504. switch (op1 & 0xf)
  505. {
  506. case 0x0:
  507. return OP_com;
  508. case 0x1:
  509. return OP_neg;
  510. case 0x2:
  511. return OP_swap;
  512. case 0x3:
  513. return OP_inc;
  514. case 0x5:
  515. flash[pc].r = 0x80;
  516. return OP_asr;
  517. case 0x6:
  518. flash[pc].r = 0;
  519. return OP_lsr;
  520. case 0x7:
  521. return OP_ror;
  522. case 0x8: /* 9[45]x8 */
  523. switch ((op1 >> 4) & 0x1f)
  524. {
  525. case 0x00:
  526. case 0x01:
  527. case 0x02:
  528. case 0x03:
  529. case 0x04:
  530. case 0x05:
  531. case 0x06:
  532. case 0x07:
  533. return OP_bset;
  534. case 0x08:
  535. case 0x09:
  536. case 0x0a:
  537. case 0x0b:
  538. case 0x0c:
  539. case 0x0d:
  540. case 0x0e:
  541. case 0x0f:
  542. return OP_bclr;
  543. case 0x10:
  544. return OP_ret;
  545. case 0x11:
  546. return OP_reti;
  547. case 0x19:
  548. return OP_break;
  549. case 0x1c:
  550. return OP_lpm;
  551. case 0x1d:
  552. return OP_elpm;
  553. default:
  554. break;
  555. }
  556. break;
  557. case 0x9: /* 9[45]x9 */
  558. switch ((op1 >> 4) & 0x1f)
  559. {
  560. case 0x00:
  561. return OP_ijmp;
  562. case 0x01:
  563. return OP_eijmp;
  564. case 0x10:
  565. return OP_icall;
  566. case 0x11:
  567. return OP_eicall;
  568. default:
  569. break;
  570. }
  571. break;
  572. case 0xa:
  573. return OP_dec;
  574. case 0xc:
  575. case 0xd:
  576. flash[pc].r = ((op1 & 0x1f0) >> 3) | (op1 & 1);
  577. return OP_jmp;
  578. case 0xe:
  579. case 0xf:
  580. flash[pc].r = ((op1 & 0x1f0) >> 3) | (op1 & 1);
  581. return OP_call;
  582. }
  583. break;
  584. case 0x6:
  585. return OP_adiw;
  586. case 0x7:
  587. return OP_sbiw;
  588. case 0x8:
  589. return OP_cbi;
  590. case 0x9:
  591. return OP_sbic;
  592. case 0xa:
  593. return OP_sbi;
  594. case 0xb:
  595. return OP_sbis;
  596. case 0xc:
  597. case 0xd:
  598. case 0xe:
  599. case 0xf:
  600. return OP_mul;
  601. }
  602. break;
  603. case 0xb:
  604. flash[pc].r = get_A (op1);
  605. if (((op1 >> 11) & 1) == 0)
  606. return OP_in;
  607. else
  608. return OP_out;
  609. case 0xc:
  610. return OP_rjmp;
  611. case 0xd:
  612. return OP_rcall;
  613. case 0xe:
  614. return OP_ldi;
  615. case 0xf:
  616. switch ((op1 >> 9) & 7)
  617. {
  618. case 0:
  619. case 1:
  620. flash[pc].r = 1 << (op1 & 7);
  621. return OP_brbs;
  622. case 2:
  623. case 3:
  624. flash[pc].r = 1 << (op1 & 7);
  625. return OP_brbc;
  626. case 4:
  627. if ((op1 & 8) == 0)
  628. {
  629. flash[pc].r = 1 << (op1 & 7);
  630. return OP_bld;
  631. }
  632. break;
  633. case 5:
  634. if ((op1 & 8) == 0)
  635. {
  636. flash[pc].r = 1 << (op1 & 7);
  637. return OP_bst;
  638. }
  639. break;
  640. case 6:
  641. if ((op1 & 8) == 0)
  642. {
  643. flash[pc].r = 1 << (op1 & 7);
  644. return OP_sbrc;
  645. }
  646. break;
  647. case 7:
  648. if ((op1 & 8) == 0)
  649. {
  650. flash[pc].r = 1 << (op1 & 7);
  651. return OP_sbrs;
  652. }
  653. break;
  654. }
  655. }
  656. return OP_bad;
  657. }
  658. static void
  659. do_call (SIM_CPU *cpu, unsigned int npc)
  660. {
  661. const struct avr_sim_state *state = AVR_SIM_STATE (CPU_STATE (cpu));
  662. unsigned int sp = read_word (REG_SP);
  663. /* Big endian! */
  664. sram[sp--] = cpu->pc;
  665. sram[sp--] = cpu->pc >> 8;
  666. if (state->avr_pc22)
  667. {
  668. sram[sp--] = cpu->pc >> 16;
  669. cpu->cycles++;
  670. }
  671. write_word (REG_SP, sp);
  672. cpu->pc = npc & PC_MASK;
  673. cpu->cycles += 3;
  674. }
  675. static int
  676. get_insn_length (unsigned int p)
  677. {
  678. if (flash[p].code == OP_unknown)
  679. flash[p].code = decode(p);
  680. if (flash[p].code >= OP_2words)
  681. return 2;
  682. else
  683. return 1;
  684. }
  685. static unsigned int
  686. get_z (void)
  687. {
  688. return (sram[RAMPZ] << 16) | (sram[REGZ_HI] << 8) | sram[REGZ_LO];
  689. }
  690. static unsigned char
  691. get_lpm (unsigned int addr)
  692. {
  693. word w;
  694. w = flash[(addr >> 1) & PC_MASK].op;
  695. if (addr & 1)
  696. w >>= 8;
  697. return w;
  698. }
  699. static void
  700. gen_mul (SIM_CPU *cpu, unsigned int res)
  701. {
  702. write_word (0, res);
  703. sram[SREG] &= ~(SREG_Z | SREG_C);
  704. if (res == 0)
  705. sram[SREG] |= SREG_Z;
  706. if (res & 0x8000)
  707. sram[SREG] |= SREG_C;
  708. cpu->cycles++;
  709. }
  710. static void
  711. step_once (SIM_CPU *cpu)
  712. {
  713. unsigned int ipc;
  714. int code;
  715. word op;
  716. byte res;
  717. byte r, d, vd;
  718. again:
  719. code = flash[cpu->pc].code;
  720. op = flash[cpu->pc].op;
  721. #if 0
  722. if (tracing && code != OP_unknown)
  723. {
  724. if (verbose > 0) {
  725. int flags;
  726. int i;
  727. sim_cb_eprintf (callback, "R00-07:");
  728. for (i = 0; i < 8; i++)
  729. sim_cb_eprintf (callback, " %02x", sram[i]);
  730. sim_cb_eprintf (callback, " -");
  731. for (i = 8; i < 16; i++)
  732. sim_cb_eprintf (callback, " %02x", sram[i]);
  733. sim_cb_eprintf (callback, " SP: %02x %02x",
  734. sram[REG_SP + 1], sram[REG_SP]);
  735. sim_cb_eprintf (callback, "\n");
  736. sim_cb_eprintf (callback, "R16-31:");
  737. for (i = 16; i < 24; i++)
  738. sim_cb_eprintf (callback, " %02x", sram[i]);
  739. sim_cb_eprintf (callback, " -");
  740. for (i = 24; i < 32; i++)
  741. sim_cb_eprintf (callback, " %02x", sram[i]);
  742. sim_cb_eprintf (callback, " ");
  743. flags = sram[SREG];
  744. for (i = 0; i < 8; i++)
  745. sim_cb_eprintf (callback, "%c",
  746. flags & (0x80 >> i) ? "ITHSVNZC"[i] : '-');
  747. sim_cb_eprintf (callback, "\n");
  748. }
  749. if (!tracing)
  750. sim_cb_eprintf (callback, "%06x: %04x\n", 2 * cpu->pc, flash[cpu->pc].op);
  751. else
  752. {
  753. sim_cb_eprintf (callback, "pc=0x%06x insn=0x%04x code=%d r=%d\n",
  754. 2 * cpu->pc, flash[cpu->pc].op, code, flash[cpu->pc].r);
  755. disassemble_insn (CPU_STATE (cpu), cpu->pc);
  756. sim_cb_eprintf (callback, "\n");
  757. }
  758. }
  759. #endif
  760. ipc = cpu->pc;
  761. cpu->pc = (cpu->pc + 1) & PC_MASK;
  762. cpu->cycles++;
  763. switch (code)
  764. {
  765. case OP_unknown:
  766. flash[ipc].code = decode(ipc);
  767. cpu->pc = ipc;
  768. cpu->cycles--;
  769. goto again;
  770. case OP_nop:
  771. break;
  772. case OP_jmp:
  773. /* 2 words instruction, but we don't care about the pc. */
  774. cpu->pc = ((flash[ipc].r << 16) | flash[ipc + 1].op) & PC_MASK;
  775. cpu->cycles += 2;
  776. break;
  777. case OP_eijmp:
  778. cpu->pc = ((sram[EIND] << 16) | read_word (REGZ)) & PC_MASK;
  779. cpu->cycles += 2;
  780. break;
  781. case OP_ijmp:
  782. cpu->pc = read_word (REGZ) & PC_MASK;
  783. cpu->cycles += 1;
  784. break;
  785. case OP_call:
  786. /* 2 words instruction. */
  787. cpu->pc++;
  788. do_call (cpu, (flash[ipc].r << 16) | flash[ipc + 1].op);
  789. break;
  790. case OP_eicall:
  791. do_call (cpu, (sram[EIND] << 16) | read_word (REGZ));
  792. break;
  793. case OP_icall:
  794. do_call (cpu, read_word (REGZ));
  795. break;
  796. case OP_rcall:
  797. do_call (cpu, cpu->pc + sign_ext (op & 0xfff, 12));
  798. break;
  799. case OP_reti:
  800. sram[SREG] |= SREG_I;
  801. /* Fall through */
  802. case OP_ret:
  803. {
  804. const struct avr_sim_state *state = AVR_SIM_STATE (CPU_STATE (cpu));
  805. unsigned int sp = read_word (REG_SP);
  806. if (state->avr_pc22)
  807. {
  808. cpu->pc = sram[++sp] << 16;
  809. cpu->cycles++;
  810. }
  811. else
  812. cpu->pc = 0;
  813. cpu->pc |= sram[++sp] << 8;
  814. cpu->pc |= sram[++sp];
  815. write_word (REG_SP, sp);
  816. }
  817. cpu->cycles += 3;
  818. break;
  819. case OP_break:
  820. /* Stop on this address. */
  821. sim_engine_halt (CPU_STATE (cpu), cpu, NULL, ipc, sim_stopped, SIM_SIGTRAP);
  822. break;
  823. case OP_bld:
  824. d = get_d (op);
  825. r = flash[ipc].r;
  826. if (sram[SREG] & SREG_T)
  827. sram[d] |= r;
  828. else
  829. sram[d] &= ~r;
  830. break;
  831. case OP_bst:
  832. if (sram[get_d (op)] & flash[ipc].r)
  833. sram[SREG] |= SREG_T;
  834. else
  835. sram[SREG] &= ~SREG_T;
  836. break;
  837. case OP_sbrc:
  838. case OP_sbrs:
  839. if (((sram[get_d (op)] & flash[ipc].r) == 0) ^ ((op & 0x0200) != 0))
  840. {
  841. int l = get_insn_length (cpu->pc);
  842. cpu->pc += l;
  843. cpu->cycles += l;
  844. }
  845. break;
  846. case OP_push:
  847. {
  848. unsigned int sp = read_word (REG_SP);
  849. sram[sp--] = sram[get_d (op)];
  850. write_word (REG_SP, sp);
  851. }
  852. cpu->cycles++;
  853. break;
  854. case OP_pop:
  855. {
  856. unsigned int sp = read_word (REG_SP);
  857. sram[get_d (op)] = sram[++sp];
  858. write_word (REG_SP, sp);
  859. }
  860. cpu->cycles++;
  861. break;
  862. case OP_bclr:
  863. sram[SREG] &= ~(1 << ((op >> 4) & 0x7));
  864. break;
  865. case OP_bset:
  866. sram[SREG] |= 1 << ((op >> 4) & 0x7);
  867. break;
  868. case OP_rjmp:
  869. cpu->pc = (cpu->pc + sign_ext (op & 0xfff, 12)) & PC_MASK;
  870. cpu->cycles++;
  871. break;
  872. case OP_eor:
  873. d = get_d (op);
  874. res = sram[d] ^ sram[get_r (op)];
  875. sram[d] = res;
  876. update_flags_logic (res);
  877. break;
  878. case OP_and:
  879. d = get_d (op);
  880. res = sram[d] & sram[get_r (op)];
  881. sram[d] = res;
  882. update_flags_logic (res);
  883. break;
  884. case OP_andi:
  885. d = get_d16 (op);
  886. res = sram[d] & get_K (op);
  887. sram[d] = res;
  888. update_flags_logic (res);
  889. break;
  890. case OP_or:
  891. d = get_d (op);
  892. res = sram[d] | sram[get_r (op)];
  893. sram[d] = res;
  894. update_flags_logic (res);
  895. break;
  896. case OP_ori:
  897. d = get_d16 (op);
  898. res = sram[d] | get_K (op);
  899. sram[d] = res;
  900. update_flags_logic (res);
  901. break;
  902. case OP_com:
  903. d = get_d (op);
  904. res = ~sram[d];
  905. sram[d] = res;
  906. update_flags_logic (res);
  907. sram[SREG] |= SREG_C;
  908. break;
  909. case OP_swap:
  910. d = get_d (op);
  911. vd = sram[d];
  912. sram[d] = (vd >> 4) | (vd << 4);
  913. break;
  914. case OP_neg:
  915. d = get_d (op);
  916. vd = sram[d];
  917. res = -vd;
  918. sram[d] = res;
  919. sram[SREG] &= ~(SREG_H | SREG_S | SREG_V | SREG_N | SREG_Z | SREG_C);
  920. if (res == 0)
  921. sram[SREG] |= SREG_Z;
  922. else
  923. sram[SREG] |= SREG_C;
  924. if (res == 0x80)
  925. sram[SREG] |= SREG_V | SREG_N;
  926. else if (res & 0x80)
  927. sram[SREG] |= SREG_N | SREG_S;
  928. if ((res | vd) & 0x08)
  929. sram[SREG] |= SREG_H;
  930. break;
  931. case OP_inc:
  932. d = get_d (op);
  933. res = sram[d] + 1;
  934. sram[d] = res;
  935. sram[SREG] &= ~(SREG_S | SREG_V | SREG_N | SREG_Z);
  936. if (res == 0x80)
  937. sram[SREG] |= SREG_V | SREG_N;
  938. else if (res & 0x80)
  939. sram[SREG] |= SREG_N | SREG_S;
  940. else if (res == 0)
  941. sram[SREG] |= SREG_Z;
  942. break;
  943. case OP_dec:
  944. d = get_d (op);
  945. res = sram[d] - 1;
  946. sram[d] = res;
  947. sram[SREG] &= ~(SREG_S | SREG_V | SREG_N | SREG_Z);
  948. if (res == 0x7f)
  949. sram[SREG] |= SREG_V | SREG_S;
  950. else if (res & 0x80)
  951. sram[SREG] |= SREG_N | SREG_S;
  952. else if (res == 0)
  953. sram[SREG] |= SREG_Z;
  954. break;
  955. case OP_lsr:
  956. case OP_asr:
  957. d = get_d (op);
  958. vd = sram[d];
  959. res = (vd >> 1) | (vd & flash[ipc].r);
  960. sram[d] = res;
  961. sram[SREG] &= ~(SREG_S | SREG_V | SREG_N | SREG_Z | SREG_C);
  962. if (vd & 1)
  963. sram[SREG] |= SREG_C | SREG_S;
  964. if (res & 0x80)
  965. sram[SREG] |= SREG_N;
  966. if (!(sram[SREG] & SREG_N) ^ !(sram[SREG] & SREG_C))
  967. sram[SREG] |= SREG_V;
  968. if (res == 0)
  969. sram[SREG] |= SREG_Z;
  970. break;
  971. case OP_ror:
  972. d = get_d (op);
  973. vd = sram[d];
  974. res = vd >> 1 | (sram[SREG] << 7);
  975. sram[d] = res;
  976. sram[SREG] &= ~(SREG_S | SREG_V | SREG_N | SREG_Z | SREG_C);
  977. if (vd & 1)
  978. sram[SREG] |= SREG_C | SREG_S;
  979. if (res & 0x80)
  980. sram[SREG] |= SREG_N;
  981. if (!(sram[SREG] & SREG_N) ^ !(sram[SREG] & SREG_C))
  982. sram[SREG] |= SREG_V;
  983. if (res == 0)
  984. sram[SREG] |= SREG_Z;
  985. break;
  986. case OP_mul:
  987. gen_mul (cpu, (word)sram[get_r (op)] * (word)sram[get_d (op)]);
  988. break;
  989. case OP_muls:
  990. gen_mul (cpu, (sword)(sbyte)sram[get_r16 (op)]
  991. * (sword)(sbyte)sram[get_d16 (op)]);
  992. break;
  993. case OP_mulsu:
  994. gen_mul (cpu, (sword)(word)sram[get_r16_23 (op)]
  995. * (sword)(sbyte)sram[get_d16_23 (op)]);
  996. break;
  997. case OP_fmul:
  998. gen_mul (cpu, ((word)sram[get_r16_23 (op)]
  999. * (word)sram[get_d16_23 (op)]) << 1);
  1000. break;
  1001. case OP_fmuls:
  1002. gen_mul (cpu, ((sword)(sbyte)sram[get_r16_23 (op)]
  1003. * (sword)(sbyte)sram[get_d16_23 (op)]) << 1);
  1004. break;
  1005. case OP_fmulsu:
  1006. gen_mul (cpu, ((sword)(word)sram[get_r16_23 (op)]
  1007. * (sword)(sbyte)sram[get_d16_23 (op)]) << 1);
  1008. break;
  1009. case OP_adc:
  1010. case OP_add:
  1011. r = sram[get_r (op)];
  1012. d = get_d (op);
  1013. vd = sram[d];
  1014. res = r + vd + (sram[SREG] & flash[ipc].r);
  1015. sram[d] = res;
  1016. update_flags_add (res, vd, r);
  1017. break;
  1018. case OP_sub:
  1019. d = get_d (op);
  1020. vd = sram[d];
  1021. r = sram[get_r (op)];
  1022. res = vd - r;
  1023. sram[d] = res;
  1024. update_flags_sub (res, vd, r);
  1025. if (res == 0)
  1026. sram[SREG] |= SREG_Z;
  1027. break;
  1028. case OP_sbc:
  1029. {
  1030. byte old = sram[SREG];
  1031. d = get_d (op);
  1032. vd = sram[d];
  1033. r = sram[get_r (op)];
  1034. res = vd - r - (old & SREG_C);
  1035. sram[d] = res;
  1036. update_flags_sub (res, vd, r);
  1037. if (res == 0 && (old & SREG_Z))
  1038. sram[SREG] |= SREG_Z;
  1039. }
  1040. break;
  1041. case OP_subi:
  1042. d = get_d16 (op);
  1043. vd = sram[d];
  1044. r = get_K (op);
  1045. res = vd - r;
  1046. sram[d] = res;
  1047. update_flags_sub (res, vd, r);
  1048. if (res == 0)
  1049. sram[SREG] |= SREG_Z;
  1050. break;
  1051. case OP_sbci:
  1052. {
  1053. byte old = sram[SREG];
  1054. d = get_d16 (op);
  1055. vd = sram[d];
  1056. r = get_K (op);
  1057. res = vd - r - (old & SREG_C);
  1058. sram[d] = res;
  1059. update_flags_sub (res, vd, r);
  1060. if (res == 0 && (old & SREG_Z))
  1061. sram[SREG] |= SREG_Z;
  1062. }
  1063. break;
  1064. case OP_mov:
  1065. sram[get_d (op)] = sram[get_r (op)];
  1066. break;
  1067. case OP_movw:
  1068. d = (op & 0xf0) >> 3;
  1069. r = (op & 0x0f) << 1;
  1070. sram[d] = sram[r];
  1071. sram[d + 1] = sram[r + 1];
  1072. break;
  1073. case OP_out:
  1074. d = get_A (op) + 0x20;
  1075. res = sram[get_d (op)];
  1076. sram[d] = res;
  1077. if (d == STDIO_PORT)
  1078. putchar (res);
  1079. else if (d == EXIT_PORT)
  1080. sim_engine_halt (CPU_STATE (cpu), cpu, NULL, cpu->pc, sim_exited, 0);
  1081. else if (d == ABORT_PORT)
  1082. sim_engine_halt (CPU_STATE (cpu), cpu, NULL, cpu->pc, sim_exited, 1);
  1083. break;
  1084. case OP_in:
  1085. d = get_A (op) + 0x20;
  1086. sram[get_d (op)] = sram[d];
  1087. break;
  1088. case OP_cbi:
  1089. d = get_biA (op) + 0x20;
  1090. sram[d] &= ~(1 << get_b(op));
  1091. break;
  1092. case OP_sbi:
  1093. d = get_biA (op) + 0x20;
  1094. sram[d] |= 1 << get_b(op);
  1095. break;
  1096. case OP_sbic:
  1097. if (!(sram[get_biA (op) + 0x20] & 1 << get_b(op)))
  1098. {
  1099. int l = get_insn_length (cpu->pc);
  1100. cpu->pc += l;
  1101. cpu->cycles += l;
  1102. }
  1103. break;
  1104. case OP_sbis:
  1105. if (sram[get_biA (op) + 0x20] & 1 << get_b(op))
  1106. {
  1107. int l = get_insn_length (cpu->pc);
  1108. cpu->pc += l;
  1109. cpu->cycles += l;
  1110. }
  1111. break;
  1112. case OP_ldi:
  1113. res = get_K (op);
  1114. d = get_d16 (op);
  1115. sram[d] = res;
  1116. break;
  1117. case OP_lds:
  1118. sram[get_d (op)] = sram[flash[cpu->pc].op];
  1119. cpu->pc++;
  1120. cpu->cycles++;
  1121. break;
  1122. case OP_sts:
  1123. sram[flash[cpu->pc].op] = sram[get_d (op)];
  1124. cpu->pc++;
  1125. cpu->cycles++;
  1126. break;
  1127. case OP_cpse:
  1128. if (sram[get_r (op)] == sram[get_d (op)])
  1129. {
  1130. int l = get_insn_length (cpu->pc);
  1131. cpu->pc += l;
  1132. cpu->cycles += l;
  1133. }
  1134. break;
  1135. case OP_cp:
  1136. r = sram[get_r (op)];
  1137. d = sram[get_d (op)];
  1138. res = d - r;
  1139. update_flags_sub (res, d, r);
  1140. if (res == 0)
  1141. sram[SREG] |= SREG_Z;
  1142. break;
  1143. case OP_cpi:
  1144. r = get_K (op);
  1145. d = sram[get_d16 (op)];
  1146. res = d - r;
  1147. update_flags_sub (res, d, r);
  1148. if (res == 0)
  1149. sram[SREG] |= SREG_Z;
  1150. break;
  1151. case OP_cpc:
  1152. {
  1153. byte old = sram[SREG];
  1154. d = sram[get_d (op)];
  1155. r = sram[get_r (op)];
  1156. res = d - r - (old & SREG_C);
  1157. update_flags_sub (res, d, r);
  1158. if (res == 0 && (old & SREG_Z))
  1159. sram[SREG] |= SREG_Z;
  1160. }
  1161. break;
  1162. case OP_brbc:
  1163. if (!(sram[SREG] & flash[ipc].r))
  1164. {
  1165. cpu->pc = (cpu->pc + get_k (op)) & PC_MASK;
  1166. cpu->cycles++;
  1167. }
  1168. break;
  1169. case OP_brbs:
  1170. if (sram[SREG] & flash[ipc].r)
  1171. {
  1172. cpu->pc = (cpu->pc + get_k (op)) & PC_MASK;
  1173. cpu->cycles++;
  1174. }
  1175. break;
  1176. case OP_lpm:
  1177. sram[0] = get_lpm (read_word (REGZ));
  1178. cpu->cycles += 2;
  1179. break;
  1180. case OP_lpm_Z:
  1181. sram[get_d (op)] = get_lpm (read_word (REGZ));
  1182. cpu->cycles += 2;
  1183. break;
  1184. case OP_lpm_inc_Z:
  1185. sram[get_d (op)] = get_lpm (read_word_post_inc (REGZ));
  1186. cpu->cycles += 2;
  1187. break;
  1188. case OP_elpm:
  1189. sram[0] = get_lpm (get_z ());
  1190. cpu->cycles += 2;
  1191. break;
  1192. case OP_elpm_Z:
  1193. sram[get_d (op)] = get_lpm (get_z ());
  1194. cpu->cycles += 2;
  1195. break;
  1196. case OP_elpm_inc_Z:
  1197. {
  1198. unsigned int z = get_z ();
  1199. sram[get_d (op)] = get_lpm (z);
  1200. z++;
  1201. sram[REGZ_LO] = z;
  1202. sram[REGZ_HI] = z >> 8;
  1203. sram[RAMPZ] = z >> 16;
  1204. }
  1205. cpu->cycles += 2;
  1206. break;
  1207. case OP_ld_Z_inc:
  1208. sram[get_d (op)] = sram[read_word_post_inc (REGZ) & SRAM_MASK];
  1209. cpu->cycles++;
  1210. break;
  1211. case OP_ld_dec_Z:
  1212. sram[get_d (op)] = sram[read_word_pre_dec (REGZ) & SRAM_MASK];
  1213. cpu->cycles++;
  1214. break;
  1215. case OP_ld_X_inc:
  1216. sram[get_d (op)] = sram[read_word_post_inc (REGX) & SRAM_MASK];
  1217. cpu->cycles++;
  1218. break;
  1219. case OP_ld_dec_X:
  1220. sram[get_d (op)] = sram[read_word_pre_dec (REGX) & SRAM_MASK];
  1221. cpu->cycles++;
  1222. break;
  1223. case OP_ld_Y_inc:
  1224. sram[get_d (op)] = sram[read_word_post_inc (REGY) & SRAM_MASK];
  1225. cpu->cycles++;
  1226. break;
  1227. case OP_ld_dec_Y:
  1228. sram[get_d (op)] = sram[read_word_pre_dec (REGY) & SRAM_MASK];
  1229. cpu->cycles++;
  1230. break;
  1231. case OP_st_X:
  1232. sram[read_word (REGX) & SRAM_MASK] = sram[get_d (op)];
  1233. cpu->cycles++;
  1234. break;
  1235. case OP_st_X_inc:
  1236. sram[read_word_post_inc (REGX) & SRAM_MASK] = sram[get_d (op)];
  1237. cpu->cycles++;
  1238. break;
  1239. case OP_st_dec_X:
  1240. sram[read_word_pre_dec (REGX) & SRAM_MASK] = sram[get_d (op)];
  1241. cpu->cycles++;
  1242. break;
  1243. case OP_st_Z_inc:
  1244. sram[read_word_post_inc (REGZ) & SRAM_MASK] = sram[get_d (op)];
  1245. cpu->cycles++;
  1246. break;
  1247. case OP_st_dec_Z:
  1248. sram[read_word_pre_dec (REGZ) & SRAM_MASK] = sram[get_d (op)];
  1249. cpu->cycles++;
  1250. break;
  1251. case OP_st_Y_inc:
  1252. sram[read_word_post_inc (REGY) & SRAM_MASK] = sram[get_d (op)];
  1253. cpu->cycles++;
  1254. break;
  1255. case OP_st_dec_Y:
  1256. sram[read_word_pre_dec (REGY) & SRAM_MASK] = sram[get_d (op)];
  1257. cpu->cycles++;
  1258. break;
  1259. case OP_std_Y:
  1260. sram[read_word (REGY) + flash[ipc].r] = sram[get_d (op)];
  1261. cpu->cycles++;
  1262. break;
  1263. case OP_std_Z:
  1264. sram[read_word (REGZ) + flash[ipc].r] = sram[get_d (op)];
  1265. cpu->cycles++;
  1266. break;
  1267. case OP_ldd_Z:
  1268. sram[get_d (op)] = sram[read_word (REGZ) + flash[ipc].r];
  1269. cpu->cycles++;
  1270. break;
  1271. case OP_ldd_Y:
  1272. sram[get_d (op)] = sram[read_word (REGY) + flash[ipc].r];
  1273. cpu->cycles++;
  1274. break;
  1275. case OP_ld_X:
  1276. sram[get_d (op)] = sram[read_word (REGX) & SRAM_MASK];
  1277. cpu->cycles++;
  1278. break;
  1279. case OP_sbiw:
  1280. {
  1281. word wk = get_k6 (op);
  1282. word wres;
  1283. word wr;
  1284. d = get_d24 (op);
  1285. wr = read_word (d);
  1286. wres = wr - wk;
  1287. sram[SREG] &= ~(SREG_S | SREG_V | SREG_N | SREG_Z | SREG_C);
  1288. if (wres == 0)
  1289. sram[SREG] |= SREG_Z;
  1290. if (wres & 0x8000)
  1291. sram[SREG] |= SREG_N;
  1292. if (wres & ~wr & 0x8000)
  1293. sram[SREG] |= SREG_C;
  1294. if (~wres & wr & 0x8000)
  1295. sram[SREG] |= SREG_V;
  1296. if (((~wres & wr) ^ wres) & 0x8000)
  1297. sram[SREG] |= SREG_S;
  1298. write_word (d, wres);
  1299. }
  1300. cpu->cycles++;
  1301. break;
  1302. case OP_adiw:
  1303. {
  1304. word wk = get_k6 (op);
  1305. word wres;
  1306. word wr;
  1307. d = get_d24 (op);
  1308. wr = read_word (d);
  1309. wres = wr + wk;
  1310. sram[SREG] &= ~(SREG_S | SREG_V | SREG_N | SREG_Z | SREG_C);
  1311. if (wres == 0)
  1312. sram[SREG] |= SREG_Z;
  1313. if (wres & 0x8000)
  1314. sram[SREG] |= SREG_N;
  1315. if (~wres & wr & 0x8000)
  1316. sram[SREG] |= SREG_C;
  1317. if (wres & ~wr & 0x8000)
  1318. sram[SREG] |= SREG_V;
  1319. if (((wres & ~wr) ^ wres) & 0x8000)
  1320. sram[SREG] |= SREG_S;
  1321. write_word (d, wres);
  1322. }
  1323. cpu->cycles++;
  1324. break;
  1325. case OP_bad:
  1326. sim_engine_halt (CPU_STATE (cpu), cpu, NULL, cpu->pc, sim_signalled, SIM_SIGILL);
  1327. default:
  1328. sim_engine_halt (CPU_STATE (cpu), cpu, NULL, cpu->pc, sim_signalled, SIM_SIGILL);
  1329. }
  1330. }
  1331. void
  1332. sim_engine_run (SIM_DESC sd,
  1333. int next_cpu_nr, /* ignore */
  1334. int nr_cpus, /* ignore */
  1335. int siggnal) /* ignore */
  1336. {
  1337. SIM_CPU *cpu;
  1338. SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  1339. cpu = STATE_CPU (sd, 0);
  1340. while (1)
  1341. {
  1342. step_once (cpu);
  1343. if (sim_events_tick (sd))
  1344. sim_events_process (sd);
  1345. }
  1346. }
  1347. int
  1348. sim_write (SIM_DESC sd, SIM_ADDR addr, const unsigned char *buffer, int size)
  1349. {
  1350. int osize = size;
  1351. if (addr >= 0 && addr < SRAM_VADDR)
  1352. {
  1353. while (size > 0 && addr < (MAX_AVR_FLASH << 1))
  1354. {
  1355. word val = flash[addr >> 1].op;
  1356. if (addr & 1)
  1357. val = (val & 0xff) | (buffer[0] << 8);
  1358. else
  1359. val = (val & 0xff00) | buffer[0];
  1360. flash[addr >> 1].op = val;
  1361. flash[addr >> 1].code = OP_unknown;
  1362. addr++;
  1363. buffer++;
  1364. size--;
  1365. }
  1366. return osize - size;
  1367. }
  1368. else if (addr >= SRAM_VADDR && addr < SRAM_VADDR + MAX_AVR_SRAM)
  1369. {
  1370. addr -= SRAM_VADDR;
  1371. if (addr + size > MAX_AVR_SRAM)
  1372. size = MAX_AVR_SRAM - addr;
  1373. memcpy (sram + addr, buffer, size);
  1374. return size;
  1375. }
  1376. else
  1377. return 0;
  1378. }
  1379. int
  1380. sim_read (SIM_DESC sd, SIM_ADDR addr, unsigned char *buffer, int size)
  1381. {
  1382. int osize = size;
  1383. if (addr >= 0 && addr < SRAM_VADDR)
  1384. {
  1385. while (size > 0 && addr < (MAX_AVR_FLASH << 1))
  1386. {
  1387. word val = flash[addr >> 1].op;
  1388. if (addr & 1)
  1389. val >>= 8;
  1390. *buffer++ = val;
  1391. addr++;
  1392. size--;
  1393. }
  1394. return osize - size;
  1395. }
  1396. else if (addr >= SRAM_VADDR && addr < SRAM_VADDR + MAX_AVR_SRAM)
  1397. {
  1398. addr -= SRAM_VADDR;
  1399. if (addr + size > MAX_AVR_SRAM)
  1400. size = MAX_AVR_SRAM - addr;
  1401. memcpy (buffer, sram + addr, size);
  1402. return size;
  1403. }
  1404. else
  1405. {
  1406. /* Avoid errors. */
  1407. memset (buffer, 0, size);
  1408. return size;
  1409. }
  1410. }
  1411. static int
  1412. avr_reg_store (SIM_CPU *cpu, int rn, unsigned char *memory, int length)
  1413. {
  1414. if (rn < 32 && length == 1)
  1415. {
  1416. sram[rn] = *memory;
  1417. return 1;
  1418. }
  1419. if (rn == AVR_SREG_REGNUM && length == 1)
  1420. {
  1421. sram[SREG] = *memory;
  1422. return 1;
  1423. }
  1424. if (rn == AVR_SP_REGNUM && length == 2)
  1425. {
  1426. sram[REG_SP] = memory[0];
  1427. sram[REG_SP + 1] = memory[1];
  1428. return 2;
  1429. }
  1430. if (rn == AVR_PC_REGNUM && length == 4)
  1431. {
  1432. cpu->pc = (memory[0] >> 1) | (memory[1] << 7)
  1433. | (memory[2] << 15) | (memory[3] << 23);
  1434. cpu->pc &= PC_MASK;
  1435. return 4;
  1436. }
  1437. return 0;
  1438. }
  1439. static int
  1440. avr_reg_fetch (SIM_CPU *cpu, int rn, unsigned char *memory, int length)
  1441. {
  1442. if (rn < 32 && length == 1)
  1443. {
  1444. *memory = sram[rn];
  1445. return 1;
  1446. }
  1447. if (rn == AVR_SREG_REGNUM && length == 1)
  1448. {
  1449. *memory = sram[SREG];
  1450. return 1;
  1451. }
  1452. if (rn == AVR_SP_REGNUM && length == 2)
  1453. {
  1454. memory[0] = sram[REG_SP];
  1455. memory[1] = sram[REG_SP + 1];
  1456. return 2;
  1457. }
  1458. if (rn == AVR_PC_REGNUM && length == 4)
  1459. {
  1460. memory[0] = cpu->pc << 1;
  1461. memory[1] = cpu->pc >> 7;
  1462. memory[2] = cpu->pc >> 15;
  1463. memory[3] = cpu->pc >> 23;
  1464. return 4;
  1465. }
  1466. return 0;
  1467. }
  1468. static sim_cia
  1469. avr_pc_get (sim_cpu *cpu)
  1470. {
  1471. return cpu->pc;
  1472. }
  1473. static void
  1474. avr_pc_set (sim_cpu *cpu, sim_cia pc)
  1475. {
  1476. cpu->pc = pc;
  1477. }
  1478. static void
  1479. free_state (SIM_DESC sd)
  1480. {
  1481. if (STATE_MODULES (sd) != NULL)
  1482. sim_module_uninstall (sd);
  1483. sim_cpu_free_all (sd);
  1484. sim_state_free (sd);
  1485. }
  1486. SIM_DESC
  1487. sim_open (SIM_OPEN_KIND kind, host_callback *cb,
  1488. struct bfd *abfd, char * const *argv)
  1489. {
  1490. int i;
  1491. SIM_DESC sd = sim_state_alloc_extra (kind, cb, sizeof (struct avr_sim_state));
  1492. SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
  1493. /* Set default options before parsing user options. */
  1494. current_alignment = STRICT_ALIGNMENT;
  1495. current_target_byte_order = BFD_ENDIAN_LITTLE;
  1496. /* The cpu data is kept in a separately allocated chunk of memory. */
  1497. if (sim_cpu_alloc_all (sd, 1) != SIM_RC_OK)
  1498. {
  1499. free_state (sd);
  1500. return 0;
  1501. }
  1502. if (sim_pre_argv_init (sd, argv[0]) != SIM_RC_OK)
  1503. {
  1504. free_state (sd);
  1505. return 0;
  1506. }
  1507. /* The parser will print an error message for us, so we silently return. */
  1508. if (sim_parse_args (sd, argv) != SIM_RC_OK)
  1509. {
  1510. free_state (sd);
  1511. return 0;
  1512. }
  1513. /* Check for/establish the a reference program image. */
  1514. if (sim_analyze_program (sd, STATE_PROG_FILE (sd), abfd) != SIM_RC_OK)
  1515. {
  1516. free_state (sd);
  1517. return 0;
  1518. }
  1519. /* Configure/verify the target byte order and other runtime
  1520. configuration options. */
  1521. if (sim_config (sd) != SIM_RC_OK)
  1522. {
  1523. sim_module_uninstall (sd);
  1524. return 0;
  1525. }
  1526. if (sim_post_argv_init (sd) != SIM_RC_OK)
  1527. {
  1528. /* Uninstall the modules to avoid memory leaks,
  1529. file descriptor leaks, etc. */
  1530. sim_module_uninstall (sd);
  1531. return 0;
  1532. }
  1533. /* CPU specific initialization. */
  1534. for (i = 0; i < MAX_NR_PROCESSORS; ++i)
  1535. {
  1536. SIM_CPU *cpu = STATE_CPU (sd, i);
  1537. CPU_REG_FETCH (cpu) = avr_reg_fetch;
  1538. CPU_REG_STORE (cpu) = avr_reg_store;
  1539. CPU_PC_FETCH (cpu) = avr_pc_get;
  1540. CPU_PC_STORE (cpu) = avr_pc_set;
  1541. }
  1542. /* Clear all the memory. */
  1543. memset (sram, 0, sizeof (sram));
  1544. memset (flash, 0, sizeof (flash));
  1545. return sd;
  1546. }
  1547. SIM_RC
  1548. sim_create_inferior (SIM_DESC sd, struct bfd *abfd,
  1549. char * const *argv, char * const *env)
  1550. {
  1551. struct avr_sim_state *state = AVR_SIM_STATE (sd);
  1552. SIM_CPU *cpu = STATE_CPU (sd, 0);
  1553. SIM_ADDR addr;
  1554. /* Set the PC. */
  1555. if (abfd != NULL)
  1556. addr = bfd_get_start_address (abfd);
  1557. else
  1558. addr = 0;
  1559. sim_pc_set (cpu, addr);
  1560. if (abfd != NULL)
  1561. state->avr_pc22 = (bfd_get_mach (abfd) >= bfd_mach_avr6);
  1562. return SIM_RC_OK;
  1563. }