h8300-sim-io.patch 47 KB

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