h8300-sim-io.patch 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743
  1. diff -Nur binutils-gdb.orig/gdb/h8300-tdep.c binutils-gdb/gdb/h8300-tdep.c
  2. --- binutils-gdb.orig/gdb/h8300-tdep.c 2015-11-16 11:55:54.522178529 +0100
  3. +++ binutils-gdb/gdb/h8300-tdep.c 2015-11-16 11:52:52.474025949 +0100
  4. @@ -1248,8 +1248,8 @@
  5. h8300_breakpoint_from_pc (struct gdbarch *gdbarch, CORE_ADDR *pcptr,
  6. int *lenptr)
  7. {
  8. - /*static unsigned char breakpoint[] = { 0x7A, 0xFF }; *//* ??? */
  9. - static unsigned char breakpoint[] = { 0x01, 0x80 }; /* Sleep */
  10. + static unsigned char breakpoint[] = { 0x7A, 0xFF }; /* ??? */
  11. + /*static unsigned char breakpoint[] = { 0x01, 0x80 };*/ /* Sleep */
  12. *lenptr = sizeof (breakpoint);
  13. return breakpoint;
  14. diff -Nur binutils-gdb.orig/sim/h8300/Makefile.in binutils-gdb/sim/h8300/Makefile.in
  15. --- binutils-gdb.orig/sim/h8300/Makefile.in 2015-11-16 11:55:55.634179462 +0100
  16. +++ binutils-gdb/sim/h8300/Makefile.in 2015-11-16 11:53:33.474060312 +0100
  17. @@ -17,7 +17,7 @@
  18. ## COMMON_PRE_CONFIG_FRAG
  19. -SIM_OBJS = compile.o \
  20. +SIM_OBJS = compile.o io.o \
  21. $(SIM_NEW_COMMON_OBJS)
  22. ## COMMON_POST_CONFIG_FRAG
  23. @@ -27,3 +27,5 @@
  24. $(srcdir)/../../include/opcode/h8300.h \
  25. $(srcdir)/../../include/gdb/remote-sim.h \
  26. $(srcdir)/../../include/gdb/callback.h
  27. +
  28. +io.o: io.c sim-main.h
  29. diff -Nur binutils-gdb.orig/sim/h8300/compile.c binutils-gdb/sim/h8300/compile.c
  30. --- binutils-gdb.orig/sim/h8300/compile.c 2015-11-16 11:55:55.634179462 +0100
  31. +++ binutils-gdb/sim/h8300/compile.c 2015-11-16 11:52:52.502025971 +0100
  32. @@ -41,11 +41,14 @@
  33. #endif
  34. int debug;
  35. +int h8300_interrupt_mode;
  36. host_callback *sim_callback;
  37. static SIM_OPEN_KIND sim_kind;
  38. static char *myname;
  39. +static int verbose_interrupt = 0;
  40. +static int logging = 0;
  41. /* FIXME: Needs to live in header file.
  42. This header should also include the things in remote-sim.h.
  43. @@ -578,10 +581,8 @@
  44. static int
  45. cmdline_location()
  46. {
  47. - if (h8300smode && !h8300_normal_mode)
  48. - return 0xffff00L;
  49. - else if (h8300hmode && !h8300_normal_mode)
  50. - return 0x2ff00L;
  51. + if ((h8300hmode || h8300smode) && !h8300_normal_mode)
  52. + return 0xff0000L;
  53. else
  54. return 0xff00L;
  55. }
  56. @@ -1037,12 +1038,15 @@
  57. /* 8-bit ABS is displacement from SBR.
  58. 16 and 32-bit ABS are displacement from ZERO.
  59. - (SBR will always be zero except for h8/sx)
  60. + (SBR will always be 0xffff00 except for h8/sx)
  61. */
  62. if ((x & SIZE) == L_8)
  63. p->reg = SBR_REGNUM;
  64. else
  65. - p->reg = ZERO_REGNUM;;
  66. + p->reg = ZERO_REGNUM;
  67. + /* address extend for @x:16 */
  68. + if ((x & SIZE) == L_16U)
  69. + p->literal += (p->literal >= 0x8000)?0xff0000:0x000000;
  70. }
  71. else if ((x & MODE) == MEMIND ||
  72. (x & MODE) == VECIND)
  73. @@ -1253,6 +1257,39 @@
  74. h8_set_cache_idx (sd, pc, idx);
  75. }
  76. +enum mem_access_type {MEM_RL,MEM_RW,MEM_RB,MEM_WL,MEM_WW,MEM_WB};
  77. +
  78. +struct memlog {
  79. + unsigned long pc;
  80. + unsigned long addr;
  81. + unsigned long data;
  82. + enum mem_access_type type;
  83. +};
  84. +
  85. +static struct memlog *memlog_buffer;
  86. +static int memlogtail;
  87. +static int memlogsize;
  88. +
  89. +static add_memlog(unsigned long pc, unsigned long addr,
  90. + enum mem_access_type type, unsigned long data)
  91. +{
  92. + if (!logging)
  93. + return;
  94. + if (memlogsize == memlogtail) {
  95. + memlog_buffer = (struct memlog *)realloc(memlog_buffer,
  96. + (memlogsize * sizeof(struct memlog) + 65536));
  97. + memlogsize += 65536 / sizeof(struct memlog);
  98. + }
  99. + if (memlog_buffer) {
  100. + memlog_buffer[memlogtail].pc = pc;
  101. + memlog_buffer[memlogtail].addr = addr;
  102. + memlog_buffer[memlogtail].type = type;
  103. + memlog_buffer[memlogtail].data = data;
  104. + memlogtail++;
  105. + }
  106. +}
  107. +
  108. +static int pc;
  109. static unsigned char *breg[32];
  110. static unsigned short *wreg[16];
  111. @@ -1265,33 +1302,46 @@
  112. #define GET_L_REG(X) h8_get_reg (sd, X)
  113. #define SET_L_REG(X, Y) h8_set_reg (sd, X, Y)
  114. -#define GET_MEMORY_L(X) \
  115. - ((X) < memory_size \
  116. - ? ((h8_get_memory (sd, (X)+0) << 24) | (h8_get_memory (sd, (X)+1) << 16) \
  117. - | (h8_get_memory (sd, (X)+2) << 8) | (h8_get_memory (sd, (X)+3) << 0)) \
  118. - : ((h8_get_eightbit (sd, ((X)+0) & 0xff) << 24) \
  119. - | (h8_get_eightbit (sd, ((X)+1) & 0xff) << 16) \
  120. - | (h8_get_eightbit (sd, ((X)+2) & 0xff) << 8) \
  121. - | (h8_get_eightbit (sd, ((X)+3) & 0xff) << 0)))
  122. -
  123. -#define GET_MEMORY_W(X) \
  124. - ((X) < memory_size \
  125. - ? ((h8_get_memory (sd, (X)+0) << 8) \
  126. - | (h8_get_memory (sd, (X)+1) << 0)) \
  127. - : ((h8_get_eightbit (sd, ((X)+0) & 0xff) << 8) \
  128. - | (h8_get_eightbit (sd, ((X)+1) & 0xff) << 0)))
  129. -
  130. -
  131. -#define GET_MEMORY_B(X) \
  132. - ((X) < memory_size ? (h8_get_memory (sd, (X))) \
  133. - : (h8_get_eightbit (sd, (X) & 0xff)))
  134. +#define GET_MEMORY_L(X) _get_memory_l(sd, X)
  135. +
  136. +static inline unsigned long _get_memory_l(SIM_DESC sd, unsigned long addr)
  137. +{
  138. + unsigned long result;
  139. + result =
  140. + ((h8_get_memory (sd, addr+0) << 24) | (h8_get_memory (sd, addr+1) << 16)
  141. + | (h8_get_memory (sd, addr+2) << 8) | (h8_get_memory (sd, addr+3) << 0));
  142. + add_memlog(pc, addr, MEM_RL, result);
  143. + return result;
  144. +}
  145. +
  146. +#define GET_MEMORY_W(X) _get_memory_w(sd, X)
  147. +
  148. +static inline unsigned short _get_memory_w(SIM_DESC sd, unsigned long addr)
  149. +{
  150. + unsigned short result;
  151. + result =
  152. + (h8_get_memory (sd, addr+0) << 8) | (h8_get_memory (sd, addr+1) << 0);
  153. + add_memlog(pc, addr, MEM_RW, result);
  154. + return result;
  155. +}
  156. +
  157. +#define GET_MEMORY_B(X) _get_memory_b(sd, X)
  158. +static inline unsigned short _get_memory_b(SIM_DESC sd, unsigned long addr)
  159. +{
  160. + unsigned short result;
  161. + result = h8_get_memory (sd, addr+0);
  162. + add_memlog(pc, addr, MEM_RB, result);
  163. + return result;
  164. +}
  165. +
  166. #define SET_MEMORY_L(X, Y) \
  167. { register unsigned char *_p; register int __y = (Y); \
  168. _p = ((X) < memory_size ? h8_get_memory_buf (sd) + (X) : \
  169. h8_get_eightbit_buf (sd) + ((X) & 0xff)); \
  170. _p[0] = __y >> 24; _p[1] = __y >> 16; \
  171. _p[2] = __y >> 8; _p[3] = __y >> 0; \
  172. + add_memlog(pc, X, MEM_WL, Y); \
  173. }
  174. #define SET_MEMORY_W(X, Y) \
  175. @@ -1299,11 +1349,13 @@
  176. _p = ((X) < memory_size ? h8_get_memory_buf (sd) + (X) : \
  177. h8_get_eightbit_buf (sd) + ((X) & 0xff)); \
  178. _p[0] = __y >> 8; _p[1] = __y; \
  179. + add_memlog(pc, X, MEM_WW, Y); \
  180. }
  181. #define SET_MEMORY_B(X, Y) \
  182. ((X) < memory_size ? (h8_set_memory (sd, (X), (Y))) \
  183. - : (h8_set_eightbit (sd, (X) & 0xff, (Y))))
  184. + : (h8_set_eightbit (sd, (X) & 0xff, (Y)))); \
  185. + add_memlog(pc, X, MEM_WB, Y)
  186. /* Simulate a memory fetch.
  187. Return 0 for success, -1 for failure.
  188. @@ -1779,15 +1831,13 @@
  189. free (h8_get_memory_buf (sd));
  190. if (h8_get_cache_idx_buf (sd))
  191. free (h8_get_cache_idx_buf (sd));
  192. - if (h8_get_eightbit_buf (sd))
  193. - free (h8_get_eightbit_buf (sd));
  194. h8_set_memory_buf (sd, (unsigned char *)
  195. calloc (sizeof (char), memory_size));
  196. h8_set_cache_idx_buf (sd, (unsigned short *)
  197. calloc (sizeof (short), memory_size));
  198. sd->memory_size = memory_size;
  199. - h8_set_eightbit_buf (sd, (unsigned char *) calloc (sizeof (char), 256));
  200. + h8_set_eightbit_buf (sd, (unsigned char *)h8_get_memory_buf(sd) + 0xffff00);
  201. h8_set_mask (sd, memory_size - 1);
  202. @@ -1865,6 +1915,105 @@
  203. goto next; \
  204. }
  205. +static unsigned long *trace_buffer;
  206. +static unsigned long tracesize;
  207. +static unsigned long tracetail;
  208. +
  209. +static void add_trace(unsigned long pc)
  210. +{
  211. + static unsigned long last_pc = 0xfffffff;
  212. +
  213. + if (pc == last_pc || logging == 0)
  214. + return;
  215. + last_pc = pc;
  216. + if (tracesize == tracetail) {
  217. + trace_buffer = (unsigned long *)realloc(trace_buffer,
  218. + (tracesize * sizeof(unsigned long) + 65536));
  219. + tracesize += 65536 / sizeof(unsigned long);
  220. + }
  221. + if (trace_buffer)
  222. + trace_buffer[tracetail++] = pc;
  223. +}
  224. +
  225. +static void init_history(void)
  226. +{
  227. + if(trace_buffer) {
  228. + free(trace_buffer);
  229. + trace_buffer = NULL;
  230. + }
  231. + tracesize = tracetail = 0;
  232. + if(memlog_buffer) {
  233. + free(memlog_buffer);
  234. + memlog_buffer = NULL;
  235. + }
  236. + memlogsize = memlogtail = 0;
  237. +}
  238. +
  239. +static int intlevel(SIM_DESC sd)
  240. +{
  241. + if(h8300smode && (h8300_interrupt_mode == 2)) {
  242. + return h8_get_ccr (sd) & 0x80?0x100:h8_get_exr (sd) << 8;
  243. + } else if (h8300_interrupt_mode == 1) {
  244. + switch((h8_get_ccr (sd) >> 6) & 3) {
  245. + case 0:
  246. + case 1:
  247. + return -1;
  248. + case 2:
  249. + return 1 << 8;
  250. + case 3:
  251. + return 0x100 << 8;
  252. + }
  253. + } else {
  254. + return h8_get_ccr (sd) & 0x80?0x100 << 8:-1;
  255. + }
  256. +}
  257. +
  258. +int iosimulation(SIM_DESC, int);
  259. +
  260. +#define exception(vector, pri) \
  261. +do { \
  262. + unsigned int ccr; \
  263. + unsigned long vbr = 0; \
  264. + int tmp; \
  265. + if (verbose_interrupt) \
  266. + (*sim_callback->printf_filtered) \
  267. + (sim_callback, "sim_resume: interrupt occured %d\n", (vector)); \
  268. + BUILDSR (sd); \
  269. + ccr = h8_get_ccr (sd); \
  270. + tmp = h8_get_reg (sd, SP_REGNUM); \
  271. + tmp -= 4; \
  272. + if (h8300sxmode) \
  273. + vbr = h8_get_vbr(sd); \
  274. + if (!h8300hmode || h8300_normal_mode) \
  275. + { \
  276. + SET_MEMORY_W (tmp, (ccr << 8) | ccr); \
  277. + SET_MEMORY_W (tmp, pc); \
  278. + pc = GET_MEMORY_W (vbr + (vector) * 2) & 0xffff; \
  279. + } \
  280. + else \
  281. + { \
  282. + SET_MEMORY_L (tmp, (ccr << 24) | pc); \
  283. + pc=GET_MEMORY_L(vbr + (vector) * 4) & 0xffffff; \
  284. + } \
  285. + if (h8300smode && (h8300_interrupt_mode == 2)) \
  286. + { \
  287. + int exr; \
  288. + exr = h8_get_exr (sd); \
  289. + tmp -= 2; \
  290. + SET_MEMORY_W (tmp, exr << 8); \
  291. + if ((pri) >= 0) \
  292. + { \
  293. + exr = pri; \
  294. + h8_set_exr (sd, exr); \
  295. + intMask = pri; \
  296. + } \
  297. + } \
  298. + h8_set_reg (sd, SP_REGNUM, tmp); \
  299. + ccr |= (h8300_interrupt_mode == 1)?0xc0:0x80; \
  300. + h8_set_ccr (sd, ccr); \
  301. + GETSR(sd); \
  302. + } while(0)
  303. +
  304. void
  305. sim_resume (SIM_DESC sd, int step, int siggnal)
  306. {
  307. @@ -1878,12 +2027,12 @@
  308. int rd;
  309. int ea;
  310. int bit;
  311. - int pc;
  312. int c, nz, v, n, u, h, ui, intMaskBit;
  313. int trace, intMask;
  314. int oldmask;
  315. enum sim_stop reason;
  316. int sigrc;
  317. + int vector;
  318. init_pointers (sd);
  319. @@ -1908,7 +2057,7 @@
  320. /* Get Status Register (flags). */
  321. GETSR (sd);
  322. - if (h8300smode) /* Get exr. */
  323. + if (h8300smode && h8300_interrupt_mode) /* Get exr. */
  324. {
  325. trace = (h8_get_exr (sd) >> 7) & 1;
  326. intMask = h8_get_exr (sd) & 7;
  327. @@ -1923,6 +2072,7 @@
  328. decoded_inst *code;
  329. top:
  330. + add_trace(pc);
  331. cidx = h8_get_cache_idx (sd, pc);
  332. if (cidx == (unsigned short) -1 ||
  333. cidx >= sd->sim_cache_size)
  334. @@ -1943,6 +2093,15 @@
  335. {
  336. cycles += code->cycles;
  337. insts++;
  338. + add_trace(pc);
  339. + if ((vector = iosimulation (sd, cycles)) &&
  340. + (intlevel(sd) < (vector & 0xff00)))
  341. + {
  342. + if (code->opcode == O (O_SLEEP, SN))
  343. + pc += 2;
  344. + exception(vector & 0xff, (vector & 0xff00) >> 8);
  345. + goto end;
  346. + }
  347. }
  348. switch (code->opcode)
  349. @@ -3253,7 +3412,7 @@
  350. goto end;
  351. if (code->src.type == X (OP_IMM, SB))
  352. - fetch (sd, &code->src, &ea);
  353. + fetch (sd, &code->src, &ea);
  354. else
  355. ea = 1;
  356. @@ -3548,16 +3707,20 @@
  357. /* Pops exr and ccr before pc -- otherwise identical to rts. */
  358. tmp = h8_get_reg (sd, SP_REGNUM);
  359. - if (h8300smode) /* pop exr */
  360. + if (h8300smode && (h8300_interrupt_mode == 2)) /* pop exr */
  361. {
  362. - h8_set_exr (sd, GET_MEMORY_L (tmp));
  363. - tmp += 4;
  364. + unsigned char exr;
  365. + exr = GET_MEMORY_W (tmp) >> 8;
  366. + h8_set_exr (sd, exr);
  367. + intMask = exr & 7;
  368. + trace = exr & 0x80;
  369. + tmp += 2;
  370. }
  371. if (h8300hmode && !h8300_normal_mode)
  372. {
  373. - h8_set_ccr (sd, GET_MEMORY_L (tmp));
  374. - tmp += 4;
  375. pc = GET_MEMORY_L (tmp);
  376. + h8_set_ccr (sd, pc >> 24);
  377. + pc &= 0x00ffffff;
  378. tmp += 4;
  379. }
  380. else
  381. @@ -3614,49 +3777,25 @@
  382. sim_engine_set_run_state (sd, sim_stopped,
  383. SIM_WSTOPSIG (h8_get_reg (sd, 0)));
  384. }
  385. -#endif
  386. else
  387. {
  388. /* Treat it as a sigtrap. */
  389. sim_engine_set_run_state (sd, sim_stopped, SIGTRAP);
  390. }
  391. +#else
  392. + else
  393. + sleep(0);
  394. +#endif
  395. goto end;
  396. - case O (O_TRAPA, SB): /* trapa */
  397. + case O (O_TRAPA, SB): { /* trapa */
  398. if (fetch (sd, &code->src, &res))
  399. goto end; /* res is vector number. */
  400. -
  401. - tmp = h8_get_reg (sd, SP_REGNUM);
  402. - if(h8300_normal_mode)
  403. - {
  404. - tmp -= 2;
  405. - SET_MEMORY_W (tmp, code->next_pc);
  406. - tmp -= 2;
  407. - SET_MEMORY_W (tmp, h8_get_ccr (sd));
  408. - }
  409. - else
  410. - {
  411. - tmp -= 4;
  412. - SET_MEMORY_L (tmp, code->next_pc);
  413. - tmp -= 4;
  414. - SET_MEMORY_L (tmp, h8_get_ccr (sd));
  415. - }
  416. - intMaskBit = 1;
  417. - BUILDSR (sd);
  418. -
  419. - if (h8300smode)
  420. - {
  421. - tmp -= 4;
  422. - SET_MEMORY_L (tmp, h8_get_exr (sd));
  423. - }
  424. -
  425. - h8_set_reg (sd, SP_REGNUM, tmp);
  426. -
  427. - if(h8300_normal_mode)
  428. - pc = GET_MEMORY_L (0x10 + res * 2); /* Vector addresses are 0x10,0x12,0x14 and 0x16 */
  429. - else
  430. - pc = GET_MEMORY_L (0x20 + res * 4);
  431. + res += 8;
  432. + pc += 2;
  433. + exception(res, -1);
  434. goto end;
  435. + }
  436. case O (O_BPT, SN):
  437. sim_engine_set_run_state (sd, sim_stopped, SIGTRAP);
  438. @@ -5005,15 +5144,13 @@
  439. free (h8_get_memory_buf (sd));
  440. if (h8_get_cache_idx_buf (sd))
  441. free (h8_get_cache_idx_buf (sd));
  442. - if (h8_get_eightbit_buf (sd))
  443. - free (h8_get_eightbit_buf (sd));
  444. h8_set_memory_buf (sd, (unsigned char *)
  445. calloc (sizeof (char), memory_size));
  446. h8_set_cache_idx_buf (sd, (unsigned short *)
  447. calloc (sizeof (short), memory_size));
  448. sd->memory_size = memory_size;
  449. - h8_set_eightbit_buf (sd, (unsigned char *) calloc (sizeof (char), 256));
  450. + h8_set_eightbit_buf (sd, (unsigned char *)h8_get_memory_buf(sd) + 0xffff00);
  451. /* `msize' must be a power of two. */
  452. if ((memory_size & (memory_size - 1)) != 0)
  453. @@ -5024,6 +5161,8 @@
  454. }
  455. h8_set_mask (sd, memory_size - 1);
  456. + init_history();
  457. + init_ioregs(sd);
  458. if (sim_load_file (sd, myname, sim_callback, prog, prog_bfd,
  459. sim_kind == SIM_OPEN_DEBUG,
  460. 0, sim_write)
  461. @@ -5074,3 +5213,187 @@
  462. return SIM_RC_OK;
  463. }
  464. +
  465. +static void show_trace(int lines)
  466. +{
  467. + unsigned long idx;
  468. + idx = tracetail - lines;
  469. + for (; lines > 0; --lines)
  470. + {
  471. + if (trace_buffer[idx] != -1)
  472. + (*sim_callback->printf_filtered) (sim_callback,
  473. + "0x%06x\n", trace_buffer[idx]);
  474. + idx++;
  475. + }
  476. + (*sim_callback->printf_filtered) (sim_callback, "\n");
  477. +}
  478. +
  479. +static void save_trace(const char *filename)
  480. +{
  481. + FILE *fp;
  482. + unsigned long idx;
  483. + fp = fopen(filename, "w");
  484. + if (!fp)
  485. + {
  486. + (*sim_callback->printf_filtered) (sim_callback,
  487. + "save-history: file open failed.\n");
  488. + return ;
  489. + }
  490. + for (idx = 0; idx < tracetail; idx++)
  491. + fprintf(fp, "0x%06x\n", trace_buffer[idx]);
  492. + fclose(fp);
  493. +}
  494. +
  495. +static const char *memtype_str[]={"RL","RW","RB","WL","WW","WB"};
  496. +
  497. +static void show_memlog(int lines)
  498. +{
  499. + unsigned long idx;
  500. + idx = memlogtail - lines;
  501. + if (memlog_buffer == NULL)
  502. + {
  503. + (*sim_callback->printf_filtered) (sim_callback, "no memlog\n");
  504. + return;
  505. + }
  506. + for (; lines > 0; --lines)
  507. + {
  508. + (*sim_callback->printf_filtered) (sim_callback,
  509. + "0x%06x 0x%06x %s %08x\n",
  510. + memlog_buffer[idx].pc,
  511. + memlog_buffer[idx].addr,
  512. + memtype_str[memlog_buffer[idx].type],
  513. + memlog_buffer[idx].data);
  514. + idx++;
  515. + }
  516. + (*sim_callback->printf_filtered) (sim_callback, "\n");
  517. +}
  518. +
  519. +static void save_memlog(const char *filename)
  520. +{
  521. + FILE *fp;
  522. + unsigned long idx;
  523. + if (memlog_buffer == NULL)
  524. + {
  525. + (*sim_callback->printf_filtered) (sim_callback, "no memlog\n");
  526. + return;
  527. + }
  528. + fp = fopen(filename, "w");
  529. + if (!fp)
  530. + {
  531. + (*sim_callback->printf_filtered) (sim_callback,
  532. + "save-history: file open failed.\n");
  533. + return ;
  534. + }
  535. + for (idx = 0; idx < memlogtail; idx++)
  536. + fprintf(fp, "0x%06x 0x%06x %s %08x\n",
  537. + memlog_buffer[idx].pc,
  538. + memlog_buffer[idx].addr,
  539. + memtype_str[memlog_buffer[idx].type],
  540. + memlog_buffer[idx].data);
  541. + fclose(fp);
  542. +}
  543. +
  544. +void
  545. +sim_do_command (SIM_DESC sd, const char *cmd)
  546. +{
  547. + if (cmd == NULL || *cmd == '\0')
  548. + cmd = "help";
  549. + if (strncmp(cmd, "show-trace", 10) == 0)
  550. + {
  551. + int lines = 16;
  552. + cmd += 10;
  553. + if (*cmd)
  554. + lines = atoi(cmd);
  555. + if (lines > 0)
  556. + show_trace(lines);
  557. + else
  558. + (*sim_callback->printf_filtered) (sim_callback,
  559. + "Invalid lines\n");
  560. + return;
  561. + }
  562. + else if (strncmp(cmd, "save-trace", 10) == 0)
  563. + {
  564. + cmd += 10;
  565. + while(isspace(*cmd))
  566. + cmd++;
  567. +
  568. + if (*cmd)
  569. + save_trace(cmd);
  570. + else
  571. + (*sim_callback->printf_filtered) (sim_callback,
  572. + "Invalid filename\n");
  573. + }
  574. + else if (strncmp(cmd, "show-mem", 8) == 0)
  575. + {
  576. + int lines = 16;
  577. + cmd += 8;
  578. + if (*cmd)
  579. + lines = atoi(cmd);
  580. + if (lines > 0)
  581. + show_memlog(lines);
  582. + else
  583. + (*sim_callback->printf_filtered) (sim_callback,
  584. + "Invalid lines\n");
  585. + return;
  586. + }
  587. + else if (strncmp(cmd, "save-mem", 8) == 0)
  588. + {
  589. + cmd += 8;
  590. + while(isspace(*cmd))
  591. + cmd++;
  592. + if (*cmd)
  593. + save_memlog(cmd);
  594. + else
  595. + (*sim_callback->printf_filtered) (sim_callback,
  596. + "Invalid filename\n");
  597. + }
  598. + else if (strncmp (cmd, "sci", 3) == 0)
  599. + {
  600. + cmd += 3;
  601. + while(isspace(*cmd)) cmd++;
  602. + if (strncmp (cmd, "pty", 3) == 0)
  603. + sci_open_pty(sim_callback);
  604. + else if (strncmp(cmd, "net", 3) == 0)
  605. + {
  606. + cmd += 3;
  607. + while(isspace(*cmd)) cmd++;
  608. + sci_open_net(sim_callback, atoi(cmd));
  609. + }
  610. + }
  611. + else if (strncmp (cmd, "verbose-int", 11) == 0)
  612. + {
  613. + cmd += 11;
  614. + while(isspace(*cmd)) cmd++;
  615. + verbose_interrupt = atoi(cmd);
  616. + }
  617. + else if (strncmp(cmd, "help", 4) == 0)
  618. + (*sim_callback->printf_filtered) (sim_callback,
  619. + "List of H8/300 Simulator commands\n\n"
  620. + "show-trace <n> -- show trace history\n"
  621. + "save-trace filename -- save trace history\n"
  622. + "show-mem <n> -- show memory access log\n"
  623. + "save-mem filename -- save memory access log\n"
  624. + "sci [pty|net port] -- open sci port\n"
  625. + "intmode mode -- set interrupt mode\n"
  626. + "verbose-int -- verbose interrupt\n"
  627. + "trace [on|off] -- trace switch\n"
  628. + );
  629. + else if (strncmp(cmd, "intmode", 7) == 0)
  630. + {
  631. + cmd += 7;
  632. + while(isspace(*cmd)) cmd++;
  633. + h8300_interrupt_mode = atoi(cmd);
  634. + }
  635. + else if (strncmp (cmd, "trace", 5) == 0)
  636. + {
  637. + cmd += 5;
  638. + while(isspace(*cmd)) cmd++;
  639. + if (strncmp (cmd, "on", 2) == 0)
  640. + logging = 1;
  641. + else if (strncmp(cmd, "off", 3) == 0)
  642. + logging = 0;
  643. + }
  644. + else
  645. + (*sim_callback->printf_filtered) (sim_callback,
  646. + "Error: Unknown \"%s\" command\n", cmd);
  647. +}
  648. diff -Nur binutils-gdb.orig/sim/h8300/io.c binutils-gdb/sim/h8300/io.c
  649. --- binutils-gdb.orig/sim/h8300/io.c 1970-01-01 01:00:00.000000000 +0100
  650. +++ binutils-gdb/sim/h8300/io.c 2015-11-16 11:52:52.502025971 +0100
  651. @@ -0,0 +1,1058 @@
  652. +/*
  653. + H8 simulator Internal Peripheral Support
  654. +*/
  655. +
  656. +#include <unistd.h>
  657. +#include <errno.h>
  658. +#include <fcntl.h>
  659. +#include <sys/time.h>
  660. +#include <string.h>
  661. +#define _XOPEN_SOURCE
  662. +#include <stdlib.h>
  663. +#include <sys/socket.h>
  664. +#include <netinet/in.h>
  665. +
  666. +#include "sim-main.h"
  667. +#undef CSIZE
  668. +#include <termios.h>
  669. +
  670. +#define MAX_SCI_CH 3
  671. +
  672. +#define SMR(ch) (STATE_CPU(sd, 0)->eightbit[sci_base[ch]+0])
  673. +#define BRR(ch) (STATE_CPU(sd, 0)->eightbit[sci_base[ch]+1])
  674. +#define SCR(ch) (STATE_CPU(sd, 0)->eightbit[sci_base[ch]+2])
  675. +#define TDR(ch) (STATE_CPU(sd, 0)->eightbit[sci_base[ch]+3])
  676. +#define SSR(ch) (STATE_CPU(sd, 0)->eightbit[sci_base[ch]+4])
  677. +#define RDR(ch) (STATE_CPU(sd, 0)->eightbit[sci_base[ch]+5])
  678. +
  679. +#define TCR8(ch) (STATE_CPU(sd, 0)->eightbit[timer8_base[ch] + 0])
  680. +#define TCSR8(ch) (STATE_CPU(sd, 0)->eightbit[timer8_base[ch] + 2])
  681. +#define TCORA8(ch) (STATE_CPU(sd, 0)->eightbit[timer8_base[ch] + 4])
  682. +#define TCORB8(ch) (STATE_CPU(sd, 0)->eightbit[timer8_base[ch] + 6])
  683. +#define TCNT8(ch) (STATE_CPU(sd, 0)->eightbit[timer8_base[ch] + 8])
  684. +
  685. +#define TSTR16 (STATE_CPU(sd, 0)->eightbit[0x60])
  686. +#define TISRA16 (STATE_CPU(sd, 0)->eightbit[0x64])
  687. +#define TISRB16 (STATE_CPU(sd, 0)->eightbit[0x65])
  688. +#define TISRC16 (STATE_CPU(sd, 0)->eightbit[0x66])
  689. +#define TCR16(ch) (STATE_CPU(sd, 0)->eightbit[0x68 + (ch) * 8])
  690. +#define TCNT16H(ch) (STATE_CPU(sd, 0)->eightbit[0x6a + (ch) * 8])
  691. +#define TCNT16L(ch) (STATE_CPU(sd, 0)->eightbit[0x6b + (ch) * 8])
  692. +#define GRA16H(ch) (STATE_CPU(sd, 0)->eightbit[0x6c + (ch) * 8])
  693. +#define GRA16L(ch) (STATE_CPU(sd, 0)->eightbit[0x6d + (ch) * 8])
  694. +#define GRB16H(ch) (STATE_CPU(sd, 0)->eightbit[0x6e + (ch) * 8])
  695. +#define GRB16L(ch) (STATE_CPU(sd, 0)->eightbit[0x6f + (ch) * 8])
  696. +
  697. +#define TPU_CH 6
  698. +#define TPU_TSTR (STATE_CPU(sd, 0)->eightbit[0xc0])
  699. +#define TPU_TCR(ch) (STATE_CPU(sd, 0)->memory[tpubase[ch] + 0])
  700. +#define TPU_TSR(ch) (STATE_CPU(sd, 0)->memory[tpubase[ch] + 5])
  701. +#define TPU_TCNTH(ch) (STATE_CPU(sd, 0)->memory[tpubase[ch] + 6])
  702. +#define TPU_TCNTL(ch) (STATE_CPU(sd, 0)->memory[tpubase[ch] + 7])
  703. +#define TPU_GRAH(ch) (STATE_CPU(sd, 0)->memory[tpubase[ch] + 8])
  704. +#define TPU_GRAL(ch) (STATE_CPU(sd, 0)->memory[tpubase[ch] + 9])
  705. +#define TPU_GRBH(ch) (STATE_CPU(sd, 0)->memory[tpubase[ch] + 10])
  706. +#define TPU_GRBL(ch) (STATE_CPU(sd, 0)->memory[tpubase[ch] + 11])
  707. +
  708. +#define IPRA_H8300H 0xfee018
  709. +#define IPRB_H8300H 0xfee019
  710. +
  711. +#define IPRA_H8300S 0xfffe00
  712. +
  713. +struct int_list_t {
  714. + int vector;
  715. + unsigned int isr_adr;
  716. + unsigned char isr_mask;
  717. + unsigned int ier_adr;
  718. + unsigned char ier_mask;
  719. +};
  720. +
  721. +static const unsigned char *sci_base;
  722. +static unsigned char ssr[MAX_SCI_CH];
  723. +static const unsigned char *timer8_base;
  724. +static const struct int_list_t *int_table;
  725. +
  726. +static const unsigned char h8300h_timer8_base[] = {0x80,0x81,0x90,0x91,0};
  727. +static const unsigned char h8300s_timer8_base[] = {0xb0,0xb1,0};
  728. +static const unsigned int tpubase[] = {0xffffd0,0xffffe0,0xfffff0,
  729. + 0xfffe80,0xfffe90,0xfffea0};
  730. +static const unsigned char h8300h_sci_base[] = {0xb0,0xb8,0xc0};
  731. +static const unsigned char h8300s_sci_base[] = {0x78,0x80,0x88};
  732. +static const unsigned char h8300sx_sci_base[] = {0x80,0x88,0x60};
  733. +
  734. +extern int h8300hmode;
  735. +extern int h8300smode;
  736. +extern int h8300sxmode;
  737. +
  738. +static const struct int_list_t h8300h_int_table[]= {
  739. + {24,0xffff64,0x01,0xffff64,0x10}, /* IMIA0 */
  740. + {25,0xffff65,0x01,0xffff65,0x10}, /* IMIB0 */
  741. + {26,0xffff66,0x01,0xffff66,0x10}, /* OVI0 */
  742. + {28,0xffff64,0x02,0xffff64,0x20}, /* IMIA1 */
  743. + {29,0xffff65,0x02,0xffff65,0x20}, /* IMIB1 */
  744. + {30,0xffff66,0x02,0xffff66,0x20}, /* OVI1 */
  745. + {32,0xffff64,0x04,0xffff64,0x40}, /* IMIA2 */
  746. + {33,0xffff65,0x04,0xffff65,0x40}, /* IMIB2 */
  747. + {34,0xffff66,0x04,0xffff66,0x40}, /* OVI2 */
  748. + {36,0xffff82,0x40,0xffff80,0x40}, /* CMIA0 */
  749. + {37,0xffff82,0x80,0xffff80,0x80}, /* CMIB0 */
  750. + {38,0xffff83,0x40,0xffff81,0x40}, /* CMIA1 */
  751. + {38,0xffff83,0x80,0xffff81,0x40}, /* CMIB1 */
  752. + {39,0xffff82,0x20,0xffff80,0x20}, /* TOVI0 */
  753. + {39,0xffff83,0x20,0xffff81,0x20}, /* TOVI1 */
  754. + {40,0xffff92,0x40,0xffff90,0x40}, /* CMIA2 */
  755. + {41,0xffff92,0x80,0xffff90,0x80}, /* CMIB2 */
  756. + {42,0xffff93,0x40,0xffff91,0x40}, /* CMIA3 */
  757. + {42,0xffff93,0x80,0xffff91,0x40}, /* CMIB3 */
  758. + {43,0xffff92,0x20,0xffff90,0x20}, /* TOVI2 */
  759. + {43,0xffff93,0x20,0xffff91,0x20}, /* TOVI3 */
  760. + {52,0xffffb4,0x38,0xffffb2,0x40}, /* ERI0 */
  761. + {53,0xffffb4,0x40,0xffffb2,0x40}, /* RXI0 */
  762. + {54,0xffffb4,0x80,0xffffb2,0x80}, /* TXI0 */
  763. + {55,0xffffb4,0x04,0xffffb2,0x04}, /* TEI0 */
  764. + {56,0xffffbc,0x38,0xffffba,0x40}, /* ERI1 */
  765. + {57,0xffffbc,0x40,0xffffba,0x40}, /* RXI1 */
  766. + {58,0xffffbc,0x80,0xffffba,0x80}, /* TXI1 */
  767. + {59,0xffffbc,0x04,0xffffba,0x04}, /* TEI1 */
  768. + {60,0xffffc4,0x38,0xffffc2,0x40}, /* ERI2 */
  769. + {61,0xffffc4,0x40,0xffffc2,0x40}, /* RXI2 */
  770. + {62,0xffffc4,0x80,0xffffc2,0x80}, /* TXI2 */
  771. + {63,0xffffc4,0x04,0xffffc2,0x04}, /* TEI2 */
  772. + {-1,0,0,0,0}
  773. +};
  774. +
  775. +static const struct int_list_t h8300s_int_table[]= {
  776. + {40,0xffffd5,0x01,0xffffd4,0x01}, /* TGI0A */
  777. + {41,0xffffd5,0x02,0xffffd4,0x02}, /* TGI0B */
  778. + {43,0xffffd5,0x10,0xffffd4,0x10}, /* TGI0V */
  779. + {48,0xffffe5,0x01,0xffffe4,0x01}, /* TGI1A */
  780. + {49,0xffffe5,0x01,0xffffe4,0x02}, /* TGI1B */
  781. + {50,0xffffe5,0x10,0xffffe4,0x10}, /* TGI1V */
  782. + {52,0xfffff5,0x01,0xfffff4,0x01}, /* TGI2A */
  783. + {53,0xfffff5,0x02,0xfffff4,0x02}, /* TGI2B */
  784. + {54,0xfffff5,0x10,0xfffff4,0x10}, /* TGI2V */
  785. + {56,0xfffe85,0x01,0xfffe84,0x01}, /* TGI3A */
  786. + {57,0xfffe85,0x02,0xfffe84,0x02}, /* TGI3B */
  787. + {60,0xfffe85,0x10,0xfffe84,0x10}, /* TGI3V */
  788. + {64,0xfffe95,0x01,0xfffe94,0x01}, /* TGI4A */
  789. + {65,0xfffe95,0x02,0xfffe94,0x02}, /* TGI4B */
  790. + {66,0xfffe95,0x10,0xfffe94,0x10}, /* TGI4V */
  791. + {68,0xfffea5,0x01,0xfffea4,0x01}, /* TGI5A */
  792. + {69,0xfffea5,0x02,0xfffea4,0x02}, /* TGI5B */
  793. + {70,0xfffea5,0x10,0xfffea4,0x10}, /* TGI5V */
  794. + {72,0xffffb2,0x40,0xffffb0,0x40}, /* CMIA0 */
  795. + {73,0xffffb2,0x80,0xffffb0,0x80}, /* CMIB0 */
  796. + {74,0xffffb2,0x20,0xffffb0,0x20}, /* CMIA1 */
  797. + {76,0xffffb3,0x40,0xffffb1,0x40}, /* CMIB1 */
  798. + {77,0xffffb3,0x80,0xffffb1,0x40}, /* TOVI0 */
  799. + {78,0xffffb3,0x20,0xffffb1,0x20}, /* TOVI1 */
  800. + {88,0xffff7c,0x38,0xffff7a,0x40}, /* ERI0 */
  801. + {89,0xffff7c,0x40,0xffff7a,0x40}, /* RXI0 */
  802. + {90,0xffff7c,0x80,0xffff7a,0x80}, /* TXI0 */
  803. + {91,0xffff7c,0x04,0xffff7a,0x04}, /* TEI0 */
  804. + {92,0xffff84,0x38,0xffff82,0x40}, /* ERI1 */
  805. + {93,0xffff84,0x40,0xffff82,0x40}, /* RXI1 */
  806. + {94,0xffff84,0x80,0xffff82,0x80}, /* TXI1 */
  807. + {95,0xffff84,0x04,0xffff82,0x04}, /* TEI1 */
  808. + {96,0xffff8c,0x38,0xffff8a,0x40}, /* ERI2 */
  809. + {97,0xffff8c,0x40,0xffff8a,0x40}, /* RXI2 */
  810. + {98,0xffff8c,0x80,0xffff8a,0x80}, /* TXI2 */
  811. + {99,0xffff8c,0x04,0xffff8a,0x04}, /* TEI2 */
  812. + {-1,0,0,0,0}
  813. +};
  814. +static const struct int_list_t h8300sx_int_table[]= {
  815. + {88,0xffffd5,0x01,0xffffd4,0x01}, /* TGI0A */
  816. + {89,0xffffd5,0x02,0xffffd4,0x02}, /* TGI0B */
  817. + {90,0xffffd5,0x01,0xffffd4,0x01}, /* TGI0C */
  818. + {91,0xffffd5,0x02,0xffffd4,0x02}, /* TGI0D */
  819. + {93,0xffffe5,0x01,0xffffe4,0x01}, /* TGI1A */
  820. + {94,0xffffe5,0x01,0xffffe4,0x02}, /* TGI1B */
  821. + {95,0xffffe5,0x10,0xffffe4,0x10}, /* TGI1V */
  822. + {96,0xffffe5,0x10,0xffffe4,0x10}, /* TGI1U */
  823. + {97,0xfffff5,0x01,0xfffff4,0x01}, /* TGI2A */
  824. + {98,0xfffff5,0x02,0xfffff4,0x02}, /* TGI2B */
  825. + {99,0xfffff5,0x10,0xfffff4,0x10}, /* TGI2V */
  826. + {100,0xfffff5,0x10,0xfffff4,0x10}, /* TGI2U */
  827. + {101,0xfffe85,0x01,0xfffe84,0x01}, /* TGI3A */
  828. + {102,0xfffe85,0x02,0xfffe84,0x02}, /* TGI3B */
  829. + {103,0xfffe85,0x01,0xfffe84,0x01}, /* TGI3A */
  830. + {104,0xfffe85,0x02,0xfffe84,0x02}, /* TGI3B */
  831. + {105,0xfffe85,0x10,0xfffe84,0x10}, /* TGI3V */
  832. + {106,0xfffe95,0x01,0xfffe94,0x01}, /* TGI4A */
  833. + {107,0xfffe95,0x02,0xfffe94,0x02}, /* TGI4B */
  834. + {108,0xfffe95,0x10,0xfffe94,0x10}, /* TGI4V */
  835. + {109,0xfffe95,0x10,0xfffe94,0x10}, /* TGI4U */
  836. + {110,0xfffea5,0x01,0xfffea4,0x01}, /* TGI5A */
  837. + {111,0xfffea5,0x02,0xfffea4,0x02}, /* TGI5B */
  838. + {112,0xfffea5,0x10,0xfffea4,0x10}, /* TGI5V */
  839. + {113,0xfffea5,0x10,0xfffea4,0x10}, /* TGI5V */
  840. + {116,0xffffb2,0x40,0xffffb0,0x40}, /* CMIA0 */
  841. + {117,0xffffb2,0x80,0xffffb0,0x80}, /* CMIB0 */
  842. + {118,0xffffb3,0x80,0xffffb1,0x40}, /* OVI0 */
  843. + {119,0xffffb2,0x20,0xffffb0,0x20}, /* CMIA1 */
  844. + {120,0xffffb3,0x40,0xffffb1,0x40}, /* CMIB1 */
  845. + {121,0xffffb3,0x20,0xffffb1,0x20}, /* OVI1 */
  846. + {144,0xffff7c,0x38,0xffff7a,0x40}, /* ERI0 */
  847. + {145,0xffff7c,0x40,0xffff7a,0x40}, /* RXI0 */
  848. + {146,0xffff7c,0x80,0xffff7a,0x80}, /* TXI0 */
  849. + {147,0xffff7c,0x04,0xffff7a,0x04}, /* TEI0 */
  850. + {148,0xffff84,0x38,0xffff82,0x40}, /* ERI1 */
  851. + {149,0xffff84,0x40,0xffff82,0x40}, /* RXI1 */
  852. + {150,0xffff84,0x80,0xffff82,0x80}, /* TXI1 */
  853. + {151,0xffff84,0x04,0xffff82,0x04}, /* TEI1 */
  854. + {152,0xffff8c,0x38,0xffff8a,0x40}, /* ERI2 */
  855. + {153,0xffff8c,0x40,0xffff8a,0x40}, /* RXI2 */
  856. + {154,0xffff8c,0x80,0xffff8a,0x80}, /* TXI2 */
  857. + {155,0xffff8c,0x04,0xffff8a,0x04}, /* TEI2 */
  858. + {-1,0,0,0,0}
  859. +};
  860. +
  861. +void
  862. +timer8(SIM_DESC sd, unsigned int cycles_diff)
  863. +{
  864. + static int prescale[3]={8,64,8192};
  865. + const int prescale_div[3]={8,64,8192};
  866. + static unsigned char tcsr[4]={0x00,0x00,0x00,0x00};
  867. + int tm, cnt, pcnt, cor;
  868. + for (pcnt = 0; pcnt < 3; pcnt++)
  869. + {
  870. + prescale[pcnt] -= cycles_diff;
  871. +
  872. + if (prescale[pcnt]<=0)
  873. + {
  874. + /* input time pulse */
  875. + for(tm=0; timer8_base[tm] != 0; tm++)
  876. + {
  877. + if ((TCR8(tm) & 0x07) == 0)
  878. + continue;
  879. + /* internal TCSR status clear */
  880. + tcsr[tm] &= (TCSR8(tm) & 0xf0);
  881. +
  882. + if ((TCR8(tm & 2) & 0x7) == 0x04)
  883. + {
  884. + /* 16bit mode */
  885. + if (tm & 1)
  886. + continue;
  887. + tcsr[tm+1] &= (TCSR8(tm+1) & 0xf0);
  888. + cnt = TCNT8(tm) << 8 | TCNT8(tm+1);
  889. + cnt++;
  890. + if (cnt >= 0x10000)
  891. + {
  892. + tcsr[tm] |= 0x20;
  893. + cnt = 0;
  894. + }
  895. + TCNT8(tm) = cnt >> 8;
  896. + TCNT8(tm-1) = cnt & 0xff;
  897. + /* TCORA compare match check */
  898. + cor = TCORA8(tm) << 8 | TCORA8(tm+1);
  899. + if (cnt >= cor)
  900. + {
  901. + tcsr[tm]|=0x40;
  902. + if ((TCR8(tm) & 0x18) == 0x08)
  903. + cnt = 0;
  904. + }
  905. + if ((cnt & 0xff) >= (cor & 0xff))
  906. + tcsr[tm+1]|=0x40;
  907. + /* TCORB compare match check */
  908. + cor = TCORB8(tm) << 8 | TCORB8(tm+1);
  909. + if (cnt >= cor)
  910. + {
  911. + tcsr[tm]|=0x80;
  912. + if ((TCR8(tm) & 0x18) == 0x10)
  913. + cnt = 0;
  914. + }
  915. + if ((cnt & 0xff) >= (cor & 0xff))
  916. + tcsr[tm+1]|=0x80;
  917. + TCNT8(tm) = cnt >> 8;
  918. + TCNT8(tm+1) = cnt & 0xff;
  919. + /* update TSCR */
  920. + TCSR8(tm) &= 0x1f;
  921. + TCSR8(tm) |= (tcsr[tm] & 0xe0);
  922. + TCSR8(tm+1) &= 0x1f;
  923. + TCSR8(tm+1) |= (tcsr[tm+1] & 0xe0);
  924. + }
  925. + else
  926. + {
  927. + /* 8bit mode */
  928. + /* update counter */
  929. + if ((TCR8(tm) & 0x07) == (pcnt+1))
  930. + {
  931. + cnt = ++TCNT8(tm);
  932. + if (cnt>=0x100)
  933. + {
  934. + tcsr[tm] |= 0x20;
  935. + cnt = 0;
  936. + }
  937. + }
  938. + /* TCORA compare match check*/
  939. + if (cnt >= TCORA8(tm))
  940. + {
  941. + tcsr[tm]|=0x40;
  942. + if ((TCR8(tm) & 0x18) == 0x08)
  943. + cnt = 0;
  944. + }
  945. + /* TCORB compare match check*/
  946. + if (cnt >= TCORB8(tm))
  947. + {
  948. + tcsr[tm]|=0x80;
  949. + if ((TCR8(tm) & 0x18) == 0x10)
  950. + cnt = 0;
  951. + }
  952. + TCNT8(tm) = cnt;
  953. + /* update TSCR */
  954. + TCSR8(tm) &= 0x1f;
  955. + TCSR8(tm) |= (tcsr[tm] & 0xe0);
  956. + }
  957. + }
  958. + prescale[pcnt]+=prescale_div[pcnt];
  959. + }
  960. + }
  961. +}
  962. +
  963. +static void
  964. +h8300sx_timer16(SIM_DESC sd, unsigned int cycles_diff)
  965. +{
  966. + static int prescale[4]={1,4,16,64};
  967. + const int prescale_div[4]={1,4,16,64};
  968. + static int tsr[TPU_CH];
  969. + int tm, cnt, pcnt, gr, pulse;
  970. + for (pcnt = 0; pcnt < 4; pcnt++) {
  971. + prescale[pcnt] -= cycles_diff;
  972. + pulse = -prescale[pcnt] / prescale_div[cnt] + 1;
  973. + if (prescale[pcnt]<=0)
  974. + {
  975. + /* input time pulse */
  976. + for(tm=0; tm < TPU_CH; tm++) {
  977. +
  978. + /* Timer enable check */
  979. + if (!(TPU_TSTR & (1 << tm)))
  980. + continue;
  981. +
  982. + /* internal TCSR status clear */
  983. + tsr[tm] &= TPU_TSR(tm);
  984. + /* update counter */
  985. + if ((TPU_TCR(tm) & 0x07) == pcnt)
  986. + {
  987. + cnt = ((TPU_TCNTH(tm) << 8) | TPU_TCNTL(tm));
  988. + cnt += pulse;
  989. +
  990. + /* CNT overflow check */
  991. + if (cnt>=0x10000)
  992. + {
  993. + tsr[tm] |= 0x10;
  994. + cnt = 0;
  995. + }
  996. +
  997. + /* GRA compare match check*/
  998. + gr = (TPU_GRAH(tm) << 8) | TPU_GRAL(tm);
  999. + if (cnt >= gr)
  1000. + {
  1001. + tsr[tm] |= 0x1;
  1002. + if ((TPU_TCR(tm) & 0x60) == 0x20)
  1003. + cnt = 0;
  1004. + }
  1005. +
  1006. + /* GRB compare match check*/
  1007. + gr = (TPU_GRBH(tm) << 8) | TPU_GRBL(tm);
  1008. + if (cnt >= gr)
  1009. + {
  1010. + tsr[tm] |= 0x2;
  1011. + if ((TPU_TCR(tm) & 0x60) == 0x20)
  1012. + cnt = 0;
  1013. + }
  1014. +
  1015. + /* update TCNT */
  1016. + TPU_TCNTH(tm) = (cnt >> 8);
  1017. + TPU_TCNTL(tm) = cnt & 0xff;
  1018. + }
  1019. +
  1020. + }
  1021. + prescale[pcnt]+=prescale_div[pcnt];
  1022. + /* update TSR */
  1023. + TPU_TSR(tm) |= tsr[tm];
  1024. + }
  1025. + }
  1026. +}
  1027. +
  1028. +static void
  1029. +h8300s_timer16(SIM_DESC sd, unsigned int cycles_diff)
  1030. +{
  1031. + static int prescale[4]={1,4,16,64};
  1032. + const int prescale_div[4]={1,4,16,64};
  1033. + static int tsr[TPU_CH];
  1034. + int tm, cnt, pcnt, gr, pulse;
  1035. + for (pcnt = 0; pcnt < 4; pcnt++) {
  1036. + prescale[pcnt] -= cycles_diff;
  1037. + pulse = -prescale[pcnt] / prescale_div[pcnt] + 1;
  1038. + if (prescale[pcnt]<=0)
  1039. + {
  1040. + /* input time pulse */
  1041. + for(tm=0; tm < TPU_CH; tm++) {
  1042. +
  1043. + /* Timer enable check */
  1044. + if (!(TPU_TSTR & (1 << tm)))
  1045. + continue;
  1046. +
  1047. + /* internal TCSR status clear */
  1048. + tsr[tm] &= TPU_TSR(tm);
  1049. + /* update counter */
  1050. + if ((TPU_TCR(tm) & 0x0f) == pcnt)
  1051. + {
  1052. + cnt = ((TPU_TCNTH(tm) << 8) | TPU_TCNTL(tm));
  1053. + cnt += pulse;
  1054. +
  1055. + /* CNT overflow check */
  1056. + if (cnt>=0x10000)
  1057. + {
  1058. + int cascade_low = tm % 3;
  1059. + tsr[tm] |= 0x10;
  1060. + cnt -= 0x10000;
  1061. + if (cascade_low == 2)
  1062. + {
  1063. + int cascade_high = tm - cascade_low + 1;
  1064. + if (TPU_TCR(cascade_high) & 0x0f == 0x0f)
  1065. + {
  1066. + int cascade_cnt;
  1067. + cascade_cnt = ((TPU_TCNTH(cascade_high) << 8) |
  1068. + TPU_TCNTL(cascade_high));
  1069. + cascade_cnt++;
  1070. + TPU_TCNTH(cascade_high) = (cascade_cnt >> 8);
  1071. + TPU_TCNTL(cascade_high) = cascade_cnt & 0xff;
  1072. + }
  1073. + }
  1074. + }
  1075. +
  1076. + /* GRA compare match check*/
  1077. + gr = (TPU_GRAH(tm) << 8) | TPU_GRAL(tm);
  1078. + if (cnt >= gr)
  1079. + {
  1080. + tsr[tm] |= 0x1;
  1081. + if ((TPU_TCR(tm) & 0xe0) == 0x20)
  1082. + cnt = 0;
  1083. + }
  1084. +
  1085. + /* GRB compare match check*/
  1086. + gr = (TPU_GRBH(tm) << 8) | TPU_GRBL(tm);
  1087. + if (cnt >= gr)
  1088. + {
  1089. + tsr[tm] |= 0x2;
  1090. + if ((TPU_TCR(tm) & 0xe0) == 0x40)
  1091. + cnt = 0;
  1092. + }
  1093. +
  1094. + /* update TCNT */
  1095. + TPU_TCNTH(tm) = (cnt >> 8);
  1096. + TPU_TCNTL(tm) = cnt & 0xff;
  1097. + }
  1098. +
  1099. + }
  1100. + prescale[pcnt]+=prescale_div[pcnt];
  1101. + /* update TSR */
  1102. + TPU_TSR(tm) |= tsr[tm];
  1103. + }
  1104. + }
  1105. +}
  1106. +
  1107. +static void
  1108. +h8300h_timer16(SIM_DESC sd, unsigned int cycles_diff)
  1109. +{
  1110. + static int prescale[4]={1,2,4,8};
  1111. + const int prescale_div[4]={1,2,4,8};
  1112. + static int tisra, tisrb, tisrc;
  1113. + int tm, cnt, pcnt, gr, pulse;
  1114. + for (pcnt = 0; pcnt < 4; pcnt++) {
  1115. + prescale[pcnt] -= cycles_diff;
  1116. + if (prescale[pcnt]<=0)
  1117. + {
  1118. + pulse = -prescale[pcnt] / prescale_div[pcnt] + 1;
  1119. + prescale[pcnt]+=prescale_div[pcnt];
  1120. + /* input time pulse */
  1121. + for(tm=0; tm < 3; tm++) {
  1122. +
  1123. + /* Timer enable check */
  1124. + if (!(TSTR16 & (1 << tm)))
  1125. + continue;
  1126. +
  1127. + /* internal TCSR status clear */
  1128. + tisra &= (0x07 & (TISRA16 & (1 << tm)));
  1129. + tisrb &= (0x07 & (TISRB16 & (1 << tm)));
  1130. + tisrc &= (0x07 & (TISRC16 & (1 << tm)));
  1131. + /* update counter */
  1132. + if ((TCR16(tm) & 0x07) == pcnt)
  1133. + {
  1134. + cnt = ((TCNT16H(tm) << 8) | TCNT16L(tm));
  1135. + cnt += pulse;
  1136. +
  1137. + /* CNT overflow check */
  1138. + if (cnt>=0x10000)
  1139. + {
  1140. + tisrc |= (1 << tm);
  1141. + cnt = 0;
  1142. + }
  1143. +
  1144. + /* GRA compare match check*/
  1145. + gr = (GRA16H(tm) << 8) | GRA16L(tm);
  1146. + if (cnt >= gr)
  1147. + {
  1148. + tisra |= (1 << tm);
  1149. + if ((TCR16(tm) & 0x60) == 0x20)
  1150. + cnt = 0;
  1151. + }
  1152. +
  1153. + /* GRB compare match check*/
  1154. + gr = (GRB16H(tm) << 8) | GRB16L(tm);
  1155. + if (cnt >= gr)
  1156. + {
  1157. + tisrb |= (1 << tm);
  1158. + if ((TCR16(tm) & 0x60) == 0x40)
  1159. + cnt = 0;
  1160. + }
  1161. + /* update TCNT */
  1162. + TCNT16H(tm) = (cnt >> 8);
  1163. + TCNT16L(tm) = cnt & 0xff;
  1164. + }
  1165. +
  1166. + }
  1167. + /* update TSCR */
  1168. + TISRA16 &= 0x70;
  1169. + TISRA16 |= tisra;
  1170. + TISRB16 &= 0x70;
  1171. + TISRB16 |= tisrb;
  1172. + TISRC16 &= 0x70;
  1173. + TISRC16 |= tisrc;
  1174. + }
  1175. + }
  1176. +}
  1177. +
  1178. +static struct {
  1179. + int fd;
  1180. + int socket;
  1181. + int iac;
  1182. + unsigned char cmd;
  1183. + struct sockaddr_in local;
  1184. + struct sockaddr_in remote;
  1185. + struct termios old_attr;
  1186. +} sci_port[MAX_SCI_CH];
  1187. +
  1188. +enum {PORT_NONE, PORT_PTY,PORT_NET};
  1189. +static int sci_port_type = PORT_NONE;
  1190. +
  1191. +static unsigned int
  1192. +sci_complete_time(SIM_DESC sd, int ch)
  1193. +{
  1194. + int length;
  1195. + int div[]={1,4,16,64};
  1196. + length = (SMR(ch) & 0x40)?7:8;
  1197. + length += (SMR(ch) & 0x20)?1:0;
  1198. + length += (SMR(ch) & 0x08)?1:0;
  1199. + length += 2;
  1200. + return length * 32 * div[SMR(ch) & 0x03] * BRR(ch);
  1201. +}
  1202. +
  1203. +static void
  1204. +sci_send_data(int ch, int txd)
  1205. +{
  1206. + char dt = txd;
  1207. + if (sci_port[ch].fd >= 0) {
  1208. + if (write(sci_port[ch].fd, &dt, 1) > 0)
  1209. + fsync(sci_port[ch].fd);
  1210. + else
  1211. + if (errno != EAGAIN)
  1212. + sci_port[ch].fd = -1;
  1213. + }
  1214. +}
  1215. +
  1216. +static void
  1217. +telnet_escape(int ch, char rd)
  1218. +{
  1219. + unsigned char cmd = sci_port[ch].cmd;
  1220. + unsigned char rep[3];
  1221. + switch(sci_port[ch].iac)
  1222. + {
  1223. + case 1:
  1224. + sci_port[ch].cmd = rd;
  1225. + sci_port[ch].iac++;
  1226. + break;
  1227. + case 2:
  1228. + if ((rd == 1 || rd == 3) && cmd == 0xfd)
  1229. + {
  1230. + sci_port[ch].iac = 0;
  1231. + return;
  1232. + }
  1233. + else if (rd == 1 || rd == 3)
  1234. + {
  1235. + if (cmd == 0xfb)
  1236. + cmd = 0xfd;
  1237. + else if (cmd == 0xfd)
  1238. + cmd = 0xfb;
  1239. + }
  1240. + else
  1241. + {
  1242. + if (cmd == 0xfb)
  1243. + cmd = 0xfe;
  1244. + else if (cmd == 0xfd)
  1245. + cmd = 0xfc;
  1246. + }
  1247. + rep[0] = 0xff;
  1248. + rep[1] = cmd;
  1249. + rep[2] = rd;
  1250. + write(sci_port[ch].fd, rep, sizeof(rep));
  1251. + sci_port[ch].iac = 0;
  1252. + break;
  1253. + }
  1254. +}
  1255. +
  1256. +static void
  1257. +telnet_request(int fd)
  1258. +{
  1259. + static unsigned char req[6] = {0xff, 0xfb, 0x03, 0xff, 0xfb, 0x01};
  1260. + write(fd, req, sizeof(req));
  1261. +}
  1262. +
  1263. +
  1264. +int
  1265. +sci_rcv_data(int ch, int *rxd)
  1266. +{
  1267. + unsigned char rd;
  1268. + if (sci_port[ch].fd >= 0)
  1269. + {
  1270. + if( read(sci_port[ch].fd, &rd , 1) > 0 )
  1271. + {
  1272. + if (sci_port_type == PORT_NET)
  1273. + {
  1274. + if (sci_port[ch].iac > 0)
  1275. + {
  1276. + telnet_escape(ch, rd);
  1277. + return 0;
  1278. + }
  1279. + else
  1280. + if (rd == 0xff)
  1281. + {
  1282. + sci_port[ch].iac = 1;
  1283. + return 0;
  1284. + }
  1285. + }
  1286. + *rxd = rd;
  1287. + return 1;
  1288. + }
  1289. + else
  1290. + {
  1291. + if (errno == EAGAIN)
  1292. + {
  1293. + return 0;
  1294. + }
  1295. + else
  1296. + {
  1297. + close(sci_port[ch].fd);
  1298. + sci_port[ch].fd = -1;
  1299. + }
  1300. + }
  1301. + }
  1302. + return 0;
  1303. +}
  1304. +
  1305. +static int net_accept(void)
  1306. +{
  1307. + int ch;
  1308. + for (ch = 0; ch < MAX_SCI_CH; ch++)
  1309. + {
  1310. + if(sci_port[ch].fd == -1)
  1311. + {
  1312. + int connectfd;
  1313. + socklen_t rem_size = sizeof(sci_port[ch].remote);
  1314. + connectfd = accept(sci_port[ch].socket,
  1315. + (struct sockaddr *)&sci_port[ch].remote,
  1316. + &rem_size);
  1317. + if (connectfd > 0)
  1318. + {
  1319. + unsigned char rd;
  1320. + int flag;
  1321. + sci_port[ch].fd = connectfd;
  1322. + telnet_request(connectfd);
  1323. + sci_port[ch].iac = 0;
  1324. + flag = fcntl(sci_port[ch].fd, F_GETFL, 0);
  1325. + fcntl(sci_port[ch].fd, F_SETFL, flag | O_NONBLOCK);
  1326. +
  1327. + while ( read(sci_port[ch].fd, &rd , 1) > 0 )
  1328. + {
  1329. + if (sci_port[ch].iac > 0)
  1330. + {
  1331. + telnet_escape(ch, rd);
  1332. + return 1;
  1333. + }
  1334. + else
  1335. + if (rd == 0xff)
  1336. + {
  1337. + sci_port[ch].iac = 1;
  1338. + return 1;
  1339. + }
  1340. + }
  1341. + }
  1342. + }
  1343. + }
  1344. + return 0;
  1345. +}
  1346. +
  1347. +static void
  1348. +sci(SIM_DESC sd, unsigned int cycles_diff)
  1349. +{
  1350. + static int tx_end_time[MAX_SCI_CH];
  1351. + static int rx_end_time[MAX_SCI_CH];
  1352. + static int txstate = 0;
  1353. + int data;
  1354. + int ch;
  1355. +
  1356. + if (sci_port_type == PORT_NET && net_accept())
  1357. + return;
  1358. +
  1359. + for (ch = 0; ch < MAX_SCI_CH; ch++)
  1360. + {
  1361. + /* clear internal ssr */
  1362. + ssr[ch] &= SSR(ch);
  1363. +
  1364. + /* Tx request */
  1365. + if((SCR(ch) & 0x20) && !(ssr[ch] & 0x80) && (txstate == 0))
  1366. + {
  1367. + sci_send_data(ch,TDR(ch));
  1368. + ssr[ch] &= ~0x04;
  1369. + /* TSR shift time */
  1370. + tx_end_time[ch] = 1;
  1371. + txstate = 1;
  1372. + }
  1373. + tx_end_time[ch] -= cycles_diff;
  1374. + /* Tx complete check */
  1375. + if(((ssr[ch] & 0x84) != 0x84) && (tx_end_time[ch] <= 0))
  1376. + if (!(ssr[ch] & 0x80))
  1377. + {
  1378. + ssr[ch] |= 0x80;
  1379. + tx_end_time[ch] = sci_complete_time(sd, ch);
  1380. + txstate = 0;
  1381. + }
  1382. + else
  1383. + ssr[ch] |= 0x04; /* All data transmit done */
  1384. + rx_end_time[ch] -= cycles_diff;
  1385. + /* Rx check */
  1386. + if (rx_end_time[ch] <= 0)
  1387. + /* RSR free & Rx Enabled */
  1388. + if ((SCR(ch) & 0x10) && sci_rcv_data(ch, &data))
  1389. + {
  1390. + /* Rx Overrun */
  1391. + if(ssr[ch] & 0x40)
  1392. + ssr[ch] |= 0x20;
  1393. + else
  1394. + /* Rx ok */
  1395. + {
  1396. + RDR(ch)=data;
  1397. + ssr[ch] |= 0x40;
  1398. + }
  1399. + /* RSR shift time */
  1400. + rx_end_time[ch] = sci_complete_time(sd, ch);
  1401. + }
  1402. +
  1403. + /* update SSR */
  1404. + SSR(ch) = ssr[ch];
  1405. + }
  1406. +}
  1407. +
  1408. +static int
  1409. +get_priority(SIM_DESC sd, int vec)
  1410. +{
  1411. + const static int ipr_bit[] = {
  1412. + -1, -1, -1, -1, -1, -1, -1, -1,
  1413. + -1, -1, -1, -1, 7, 6, 5, 5,
  1414. + 4, 4, 4, 4, 3, 3, 3, 3,
  1415. + 2, 2, 2, 2, 1, 1, 1, 1,
  1416. + 0, 0, 0, 0, 15, 15, 15, 15,
  1417. + 14, 14, 14, 14, 13, 13, 13, 13,
  1418. + -1, -1, -1, -1, 11, 11, 11, 11,
  1419. + 10, 10, 10, 10, 9, 9, 9, 9,
  1420. + };
  1421. + const static unsigned char ipr_table[] = {
  1422. + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 0 - 7 */
  1423. + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, /* 8 - 15 */
  1424. + 0x03, 0x02, 0x01, 0x00, 0x13, 0x12, 0x11, 0x10, /* 16 - 23 */
  1425. + 0x23, 0x22, 0x21, 0x20, 0x33, 0x32, 0x31, 0x30, /* 24 - 31 */
  1426. + 0x43, 0x42, 0x41, 0x40, 0x53, 0x53, 0x52, 0x52, /* 32 - 39 */
  1427. + 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, 0x51, /* 40 - 47 */
  1428. + 0x50, 0x50, 0x50, 0x50, 0x63, 0x63, 0x63, 0x63, /* 48 - 55 */
  1429. + 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, 0x62, /* 56 - 63 */
  1430. + 0x61, 0x61, 0x61, 0x61, 0x60, 0x60, 0x60, 0x60, /* 64 - 71 */
  1431. + 0x73, 0x73, 0x73, 0x73, 0x72, 0x72, 0x72, 0x72, /* 72 - 79 */
  1432. + 0x71, 0x71, 0x71, 0x71, 0x70, 0x83, 0x82, 0x81, /* 80 - 87 */
  1433. + 0x80, 0x80, 0x80, 0x80, 0x93, 0x93, 0x93, 0x93, /* 88 - 95 */
  1434. + 0x92, 0x92, 0x92, 0x92, 0x91, 0x91, 0x91, 0x91, /* 96 - 103 */
  1435. + 0x90, 0x90, 0x90, 0x90, 0xa3, 0xa3, 0xa3, 0xa3, /* 104 - 111 */
  1436. + 0xa2, 0xa2, 0xa2, 0xa2, 0xa1, 0xa1, 0xa1, 0xa1, /* 112 - 119 */
  1437. + 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, /* 120 - 127 */
  1438. + };
  1439. +
  1440. +
  1441. + if (h8300smode)
  1442. + {
  1443. + unsigned short ipr;
  1444. + int pos;
  1445. + if ((pos = ipr_table[vec]) == 0xff)
  1446. + return 0;
  1447. + ipr = (STATE_CPU(sd, 0)->memory[IPRA_H8300S + ((pos & 0xf0) >> 3)] << 8) |
  1448. + (STATE_CPU(sd, 0)->memory[IPRA_H8300S + ((pos & 0xf0) >> 3) + 1]);
  1449. + return vec + ((ipr >> ((pos & 0x0f) * 4)) & 7) * 0x100;
  1450. + }
  1451. + else if (h8300hmode)
  1452. + {
  1453. + int b;
  1454. + unsigned char ipr;
  1455. + if ((b = ipr_bit[vec]) < 0)
  1456. + return 0;
  1457. + ipr = (b < 8)?STATE_CPU(sd, 0)->memory[IPRA_H8300H]:
  1458. + STATE_CPU(sd, 0)->memory[IPRB_H8300H];
  1459. + b = 1 << (b & 7);
  1460. + if (ipr & b)
  1461. + return vec + 0x100;
  1462. + else
  1463. + return vec;
  1464. + }
  1465. +}
  1466. +
  1467. +static int
  1468. +intcont(SIM_DESC sd)
  1469. +{
  1470. + int irqno;
  1471. + for (irqno=0; int_table[irqno].vector > 0; irqno++)
  1472. + {
  1473. + if((STATE_CPU(sd, 0)->memory[int_table[irqno].ier_adr] &
  1474. + int_table[irqno].ier_mask) &&
  1475. + (STATE_CPU(sd, 0)->memory[int_table[irqno].isr_adr] &
  1476. + int_table[irqno].isr_mask))
  1477. + return get_priority(sd, int_table[irqno].vector);
  1478. + }
  1479. + return 0;
  1480. +}
  1481. +
  1482. +int
  1483. +iosimulation(SIM_DESC sd, int cycles)
  1484. +{
  1485. + static unsigned int prev_cycles = 0;
  1486. + unsigned int cycles_diff;
  1487. + cycles_diff = (cycles < prev_cycles)?cycles:(cycles - prev_cycles);
  1488. + prev_cycles = cycles;
  1489. + timer8(sd, cycles_diff);
  1490. + if (h8300smode)
  1491. + h8300s_timer16(sd, cycles_diff);
  1492. + else if (h8300hmode)
  1493. + h8300h_timer16(sd, cycles_diff);
  1494. + sci(sd, cycles_diff);
  1495. + return intcont(sd);
  1496. +}
  1497. +
  1498. +void init_ioregs(SIM_DESC sd)
  1499. +{
  1500. + struct INITTABLE {
  1501. + unsigned char addr;
  1502. + unsigned char data;
  1503. + };
  1504. + const struct INITTABLE h8300h_reg_ini[] = {
  1505. + 0x80,0x00,
  1506. + 0x81,0x00,
  1507. + 0x82,0x00,
  1508. + 0x83,0x00,
  1509. + 0x84,0xff,
  1510. + 0x85,0xff,
  1511. + 0x86,0xff,
  1512. + 0x87,0xff,
  1513. + 0x88,0x00,
  1514. + 0x89,0x00,
  1515. + 0x90,0x00,
  1516. + 0x91,0x00,
  1517. + 0x92,0x00,
  1518. + 0x93,0x00,
  1519. + 0x94,0xff,
  1520. + 0x95,0xff,
  1521. + 0x96,0xff,
  1522. + 0x97,0xff,
  1523. + 0x98,0x00,
  1524. + 0x99,0x00,
  1525. + 0xb0,0x00,
  1526. + 0xb1,0xff,
  1527. + 0xb2,0x00,
  1528. + 0xb3,0xff,
  1529. + 0xb4,0x84,
  1530. + 0xb8,0x00,
  1531. + 0xb9,0xff,
  1532. + 0xba,0x00,
  1533. + 0xbb,0xff,
  1534. + 0xbc,0x84,
  1535. + 0xc0,0x00,
  1536. + 0xc1,0xff,
  1537. + 0xc2,0x00,
  1538. + 0xc3,0xff,
  1539. + 0xc4,0x84,
  1540. + };
  1541. + const struct INITTABLE h8300s_reg_ini[] = {
  1542. + 0xb0,0x00,
  1543. + 0xb1,0x00,
  1544. + 0xb2,0x00,
  1545. + 0xb3,0x00,
  1546. + 0xb4,0xff,
  1547. + 0xb5,0xff,
  1548. + 0xb6,0xff,
  1549. + 0xb7,0xff,
  1550. + 0xb8,0x00,
  1551. + 0xb9,0x00,
  1552. + 0x78,0x00,
  1553. + 0x79,0xff,
  1554. + 0x7a,0x00,
  1555. + 0x7b,0xff,
  1556. + 0x7c,0x84,
  1557. + 0x80,0x00,
  1558. + 0x81,0xff,
  1559. + 0x82,0x00,
  1560. + 0x83,0xff,
  1561. + 0x84,0x84,
  1562. + 0x88,0x00,
  1563. + 0x89,0xff,
  1564. + 0x8a,0x00,
  1565. + 0x8b,0xff,
  1566. + 0x8c,0x84,
  1567. + };
  1568. + const struct INITTABLE h8300sx_reg_ini[] = {
  1569. + 0xb0,0x00,
  1570. + 0xb1,0x00,
  1571. + 0xb2,0x00,
  1572. + 0xb3,0x00,
  1573. + 0xb4,0xff,
  1574. + 0xb5,0xff,
  1575. + 0xb6,0xff,
  1576. + 0xb7,0xff,
  1577. + 0xb8,0x00,
  1578. + 0xb9,0x00,
  1579. + 0x80,0x00,
  1580. + 0x81,0xff,
  1581. + 0x82,0x00,
  1582. + 0x83,0xff,
  1583. + 0x84,0x84,
  1584. + 0x88,0x00,
  1585. + 0x89,0xff,
  1586. + 0x8a,0x00,
  1587. + 0x8b,0xff,
  1588. + 0x8c,0x84,
  1589. + 0x60,0x00,
  1590. + 0x61,0xff,
  1591. + 0x62,0x00,
  1592. + 0x63,0xff,
  1593. + 0x64,0x84,
  1594. + };
  1595. + int c;
  1596. + if (h8300sxmode) {
  1597. + sci_base = h8300sx_sci_base;
  1598. + timer8_base = h8300s_timer8_base;
  1599. + int_table = h8300sx_int_table;
  1600. + for(c=0;c<sizeof(h8300sx_reg_ini)/sizeof(struct INITTABLE);c++)
  1601. + STATE_CPU(sd, 0)->eightbit[h8300sx_reg_ini[c].addr]=h8300sx_reg_ini[c].data;
  1602. + }
  1603. + else if (h8300smode) {
  1604. + sci_base = h8300s_sci_base;
  1605. + timer8_base = h8300s_timer8_base;
  1606. + int_table = h8300s_int_table;
  1607. + for(c=0;c<sizeof(h8300s_reg_ini)/sizeof(struct INITTABLE);c++)
  1608. + STATE_CPU(sd, 0)->eightbit[h8300s_reg_ini[c].addr]=h8300s_reg_ini[c].data;
  1609. + }
  1610. + else if (h8300hmode) {
  1611. + sci_base = h8300h_sci_base;
  1612. + timer8_base = h8300h_timer8_base;
  1613. + int_table = h8300h_int_table;
  1614. + for(c=0;c<sizeof(h8300h_reg_ini)/sizeof(struct INITTABLE);c++)
  1615. + STATE_CPU(sd, 0)->eightbit[h8300h_reg_ini[c].addr]=h8300h_reg_ini[c].data;
  1616. + }
  1617. + for(c = 0; c< MAX_SCI_CH; c++)
  1618. + ssr[c] = 0x84;
  1619. +}
  1620. +
  1621. +static char *openpty(int ch)
  1622. +{
  1623. + const char nm[]="0123456789ABCDEF";
  1624. + static char ptyname[16];
  1625. + int c1,c2,fd;
  1626. + struct termios attr;
  1627. + fd = open("/dev/ptmx",O_RDWR|O_NONBLOCK);
  1628. + if(fd >= 0) {
  1629. + grantpt(fd);
  1630. + unlockpt(fd);
  1631. + ptsname_r(fd, ptyname, sizeof(ptyname));
  1632. + } else {
  1633. + for(c1='a';c1<='z';c1++)
  1634. + for(c2=0;c2<sizeof(nm)-1;c2++) {
  1635. + sprintf(ptyname,"/dev/pty%c%c",c1,nm[c2]);
  1636. + fd=open(ptyname,O_RDWR|O_NONBLOCK);
  1637. + if(fd != -1)
  1638. + break ;
  1639. + }
  1640. + ptyname[5]='t';
  1641. + }
  1642. + if (fd >= 0) {
  1643. + sci_port[ch].fd = fd;
  1644. + tcgetattr(fd, &attr);
  1645. + memcpy(&sci_port[ch].old_attr, &attr, sizeof(struct termios));
  1646. + attr.c_lflag &= ~ICANON;
  1647. + attr.c_cc[VMIN] = 0;
  1648. + attr.c_cc[VTIME] =0;
  1649. + tcsetattr(fd, TCSAFLUSH, &attr);
  1650. + return ptyname;
  1651. + } else {
  1652. + sci_port[ch].fd = -1;
  1653. + return NULL;
  1654. + }
  1655. +}
  1656. +
  1657. +void sci_open_pty(struct host_callback_struct *callback)
  1658. +{
  1659. + int ch;
  1660. + int max_ch;
  1661. + char *pty;
  1662. + for (ch = 0; ch < MAX_SCI_CH; ch++)
  1663. + {
  1664. + pty = openpty(ch);
  1665. + if (pty)
  1666. + (*callback->printf_filtered) (callback, "SCI%d = %s\n",ch ,pty);
  1667. + }
  1668. +}
  1669. +
  1670. +void sci_open_net(struct host_callback_struct *callback, int port)
  1671. +{
  1672. + int c;
  1673. + int flag;
  1674. + int socketfd;
  1675. + sci_port_type = PORT_NET;
  1676. + for (c = 0; c < MAX_SCI_CH; c++) {
  1677. + memset(&sci_port[c].local, 0, sizeof(sci_port[c].local));
  1678. + sci_port[c].local.sin_family = AF_INET;
  1679. + sci_port[c].local.sin_addr.s_addr = htonl(INADDR_ANY);
  1680. + sci_port[c].local.sin_port = htons(port + c);
  1681. + sci_port[c].fd = -1;
  1682. + socketfd = socket(AF_INET, SOCK_STREAM, 0);
  1683. + if (socketfd >= 0)
  1684. + {
  1685. + bind(socketfd, (struct sockaddr *)&sci_port[c].local, sizeof(sci_port[c].local));
  1686. + flag = fcntl(socketfd, F_GETFL, 0);
  1687. + fcntl(socketfd, F_SETFL, flag | O_NONBLOCK);
  1688. + listen(socketfd, 1);
  1689. + sci_port[c].socket = socketfd;
  1690. + (*callback->printf_filtered) (callback, "SCI%d = %d\n",c ,port+c);
  1691. + }
  1692. + }
  1693. +}
  1694. +
  1695. +void sci_close(void)
  1696. +{
  1697. + int ch;
  1698. + if (sci_port_type == PORT_NONE)
  1699. + return;
  1700. + for (ch = 0; ch < MAX_SCI_CH; ch++) {
  1701. + if(sci_port[ch].fd != -1) {
  1702. + if (sci_port_type == PORT_PTY)
  1703. + tcsetattr(sci_port[ch].fd, TCSAFLUSH, &sci_port[ch].old_attr);
  1704. + close(sci_port[ch].fd);
  1705. + if (sci_port_type == PORT_NET)
  1706. + close(sci_port[ch].socket);
  1707. + }
  1708. + }
  1709. +}