h8300-sim-io.patch 48 KB

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