special.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <mach.h>
  5. #define Extern extern
  6. #include "mips.h"
  7. void Snor(ulong);
  8. void Ssll(ulong);
  9. void Ssra(ulong);
  10. void Sslt(ulong);
  11. void Ssltu(ulong);
  12. void Sand(ulong);
  13. void Saddu(ulong);
  14. void Sadd(ulong);
  15. void Sjr(ulong);
  16. void Sor(ulong);
  17. void Ssubu(ulong);
  18. void Sjalr(ulong);
  19. void Sdivu(ulong);
  20. void Smfhi(ulong);
  21. void Smflo(ulong);
  22. void Sxor(ulong);
  23. void Smult(ulong);
  24. void Smultu(ulong);
  25. void Sdiv(ulong);
  26. void Ssrl(ulong);
  27. void Ssllv(ulong);
  28. void Ssrlv(ulong);
  29. void Ssrav(ulong);
  30. Inst ispec[] =
  31. {
  32. { Ssll, "sll", Iarith },
  33. { undef, "" },
  34. { Ssrl, "srl", Iarith },
  35. { Ssra, "sra", Iarith },
  36. { Ssllv, "sllv", Iarith },
  37. { undef, "" },
  38. { Ssrlv, "srlv", Iarith },
  39. { Ssrav, "srav", Iarith },
  40. { Sjr, "jr", Ibranch },
  41. { Sjalr, "jalr", Ibranch },
  42. { undef, "" },
  43. { undef, "" },
  44. { Ssyscall, "sysc", Isyscall },
  45. { undef, "" },
  46. { undef, "" },
  47. { undef, "" },
  48. { Smfhi, "mfhi", Ireg },
  49. { undef, "" },
  50. { Smflo, "mflo", Ireg },
  51. { undef, "" },
  52. { undef, "" },
  53. { undef, "" },
  54. { undef, "" },
  55. { undef, "" },
  56. { Smult, "mult", Iarith },
  57. { Smultu, "multu" },
  58. { Sdiv, "div", Iarith },
  59. { Sdivu, "divu", Iarith },
  60. { undef, "" },
  61. { undef, "" },
  62. { undef, "" },
  63. { undef, "" },
  64. { Sadd, "add", Iarith },
  65. { Saddu, "addu", Iarith },
  66. { undef, "" },
  67. { Ssubu, "subu", Iarith },
  68. { Sand, "and", Iarith },
  69. { Sor, "or", Iarith },
  70. { Sxor, "xor", Iarith },
  71. { Snor, "nor", Iarith },
  72. { undef, "" },
  73. { undef, "" },
  74. { Sslt, "slt", Iarith },
  75. { Ssltu, "sltu", Iarith },
  76. { undef, "" },
  77. { undef, "" },
  78. { undef, "" },
  79. { undef, "" },
  80. { undef, "" },
  81. { undef, "" },
  82. { undef, "" },
  83. { undef, "" },
  84. { undef, "" },
  85. { undef, "" },
  86. { undef, "" },
  87. { undef, "" },
  88. { undef, "" },
  89. { undef, "" },
  90. { undef, "" },
  91. { undef, "" },
  92. { undef, "" },
  93. { undef, "" },
  94. { undef, "" },
  95. { undef, "" },
  96. { 0 }
  97. };
  98. void
  99. Ispecial(ulong inst)
  100. {
  101. Inst *i;
  102. i = &ispec[inst&0x3f];
  103. reg.ip = i;
  104. i->count++;
  105. (*i->func)(inst);
  106. }
  107. void
  108. Snor(ulong inst)
  109. {
  110. int rs, rt, rd;
  111. Get3(rs, rt, rd, inst);
  112. if(trace)
  113. itrace("nor\tr%d,r%d,r%d", rd, rs, rt);
  114. if(inst == INOP)
  115. nopcount++;
  116. else
  117. reg.r[rd] = ~(reg.r[rt]|reg.r[rs]);
  118. }
  119. void
  120. Ssll(ulong inst)
  121. {
  122. int rd, rt, shamt;
  123. SpecialGetrtrd(rt, rd, inst);
  124. shamt = (inst>>6)&0x1f;
  125. if(trace)
  126. itrace("sll\tr%d,r%d,%d", rd, rt, shamt);
  127. reg.r[rd] = reg.r[rt]<<shamt;
  128. }
  129. void
  130. Ssllv(ulong inst)
  131. {
  132. int rd, rt, rs;
  133. Get3(rs, rt, rd, inst);
  134. if(trace)
  135. itrace("sllv\tr%d,r%d,r%d", rd, rt, rs);
  136. reg.r[rd] = reg.r[rt]<<(reg.r[rs]&0x1f);
  137. }
  138. void
  139. Ssrlv(ulong inst)
  140. {
  141. int rd, rt, rs;
  142. Get3(rs, rt, rd, inst);
  143. if(trace)
  144. itrace("srlv\tr%d,r%d,r%d", rd, rt, rs);
  145. reg.r[rd] = (ulong)reg.r[rt] >> (reg.r[rs]&0x1f);
  146. }
  147. void
  148. Ssrav(ulong inst)
  149. {
  150. int rd, rt, rs, shamt;
  151. Get3(rs, rt, rd, inst);
  152. if(trace)
  153. itrace("srav\tr%d,r%d,r%d", rd, rt, rs);
  154. shamt = reg.r[rs]&0x1f;
  155. if(shamt != 0 && (reg.r[rt] & SIGNBIT))
  156. reg.r[rd] = reg.r[rt]>>shamt | ~((1<<(32-shamt))-1);
  157. else
  158. reg.r[rd] = reg.r[rt]>>shamt;
  159. }
  160. void
  161. Ssrl(ulong inst)
  162. {
  163. int rd, rt, shamt;
  164. SpecialGetrtrd(rt, rd, inst);
  165. shamt = (inst>>6)&0x1f;
  166. if(trace)
  167. itrace("srl\tr%d,r%d,%d", rd, rt, shamt);
  168. reg.r[rd] = (ulong)reg.r[rt] >> shamt;
  169. }
  170. void
  171. Ssra(ulong inst)
  172. {
  173. int rd, rt, shamt;
  174. SpecialGetrtrd(rt, rd, inst);
  175. shamt = (inst>>6)&0x1f;
  176. if(trace)
  177. itrace("sra\tr%d,r%d,%d", rd, rt, shamt);
  178. if(shamt != 0 && (reg.r[rt] & SIGNBIT))
  179. reg.r[rd] = reg.r[rt]>>shamt | ~((1<<(32-shamt))-1);
  180. else
  181. reg.r[rd] = reg.r[rt]>>shamt;
  182. }
  183. void
  184. Sslt(ulong inst)
  185. {
  186. int rs, rt, rd;
  187. Get3(rs, rt, rd, inst);
  188. if(trace)
  189. itrace("slt\tr%d,r%d,r%d", rd, rs, rt);
  190. reg.r[rd] = reg.r[rs] < reg.r[rt] ? 1 : 0;
  191. }
  192. void
  193. Ssltu(ulong inst)
  194. {
  195. int rs, rt, rd;
  196. Get3(rs, rt, rd, inst);
  197. if(trace)
  198. itrace("sltu\tr%d,r%d,r%d", rd, rs, rt);
  199. reg.r[rd] = (unsigned)reg.r[rs] < (unsigned)reg.r[rt] ? 1 : 0;
  200. }
  201. void
  202. Sand(ulong inst)
  203. {
  204. int rs, rt, rd;
  205. Get3(rs, rt, rd, inst);
  206. if(trace)
  207. itrace("and\tr%d,r%d,r%d", rd, rs, rt);
  208. reg.r[rd] = reg.r[rs] & reg.r[rt];
  209. }
  210. void
  211. Saddu(ulong inst)
  212. {
  213. int rs, rt, rd;
  214. Get3(rs, rt, rd, inst);
  215. if(trace)
  216. itrace("addu\tr%d,r%d,r%d", rd, rs, rt);
  217. reg.r[rd] = reg.r[rs] + reg.r[rt];
  218. }
  219. void
  220. Sadd(ulong inst)
  221. {
  222. int rs, rt, rd;
  223. Get3(rs, rt, rd, inst);
  224. if(trace)
  225. itrace("add\tr%d,r%d,r%d", rd, rs, rt);
  226. reg.r[rd] = reg.r[rs] + reg.r[rt];
  227. }
  228. void
  229. Ssubu(ulong inst)
  230. {
  231. int rs, rt, rd;
  232. Get3(rs, rt, rd, inst);
  233. if(trace)
  234. itrace("subu\tr%d,r%d,r%d", rd, rs, rt);
  235. reg.r[rd] = reg.r[rs] - reg.r[rt];
  236. }
  237. void
  238. Sor(ulong inst)
  239. {
  240. int rs, rt, rd;
  241. Get3(rs, rt, rd, inst);
  242. if(trace)
  243. itrace("or\tr%d,r%d,r%d", rd, rs, rt);
  244. reg.r[rd] = reg.r[rs] | reg.r[rt];
  245. }
  246. void
  247. Sxor(ulong inst)
  248. {
  249. int rs, rt, rd;
  250. Get3(rs, rt, rd, inst);
  251. if(trace)
  252. itrace("or\tr%d,r%d,r%d", rd, rs, rt);
  253. reg.r[rd] = reg.r[rs] ^ reg.r[rt];
  254. }
  255. void
  256. Sjr(ulong inst)
  257. {
  258. ulong npc;
  259. int rs;
  260. Symbol s;
  261. rs = (inst>>21)&0x1f;
  262. npc = reg.r[rs];
  263. if(trace)
  264. itrace("jr\t0x%lux", npc);
  265. /* Do the delay slot */
  266. reg.ir = ifetch(reg.pc+4);
  267. Statbra();
  268. Iexec(reg.ir);
  269. if(calltree) {
  270. if(rs == 31 || rs == 2) {
  271. findsym(npc, CTEXT, &s);
  272. Bprint(bioout, "%8lux return to %lux %s r1=%lux\n",
  273. reg.pc, npc, s.name, reg.r[1]);
  274. }
  275. }
  276. reg.pc = npc-4;
  277. }
  278. void
  279. Sjalr(ulong inst)
  280. {
  281. ulong npc;
  282. int rs, rd;
  283. Symbol s;
  284. rs = (inst>>21)&0x1f;
  285. rd = (inst>>11)&0x1f;
  286. npc = reg.r[rs];
  287. if(trace)
  288. itrace("jalr\tr%d,r%d", rd, rs);
  289. reg.r[rd] = reg.pc+8;
  290. /* Do the delay slot */
  291. reg.ir = ifetch(reg.pc+4);
  292. Statbra();
  293. Iexec(reg.ir);
  294. if(calltree) {
  295. findsym(npc, CTEXT, &s);
  296. if(rs == 31)
  297. Bprint(bioout, "%8lux return to %8lux %s\n",
  298. reg.pc, npc, s.name);
  299. else {
  300. printparams(&s, reg.r[29]);
  301. Bputc(bioout, '\n');
  302. }
  303. }
  304. reg.pc = npc-4;
  305. }
  306. void
  307. Sdivu(ulong inst)
  308. {
  309. int rs, rt;
  310. Getrsrt(rs,rt,inst);
  311. if(trace)
  312. itrace("divu\tr%d,r%d", rs, rt);
  313. reg.mlo = (ulong)reg.r[rs]/(ulong)reg.r[rt];
  314. reg.mhi = (ulong)reg.r[rs]%(ulong)reg.r[rt];
  315. }
  316. void
  317. Sdiv(ulong inst)
  318. {
  319. int rs, rt;
  320. Getrsrt(rs,rt,inst);
  321. if(trace)
  322. itrace("div\tr%d,r%d", rs, rt);
  323. reg.mlo = reg.r[rs]/reg.r[rt];
  324. reg.mhi = reg.r[rs]%reg.r[rt];
  325. }
  326. void
  327. Smfhi(ulong inst)
  328. {
  329. int rd;
  330. rd = (inst>>11)&0x1ff;
  331. if(trace)
  332. itrace("mfhi\tr%d", rd);
  333. reg.r[rd] = reg.mhi;
  334. }
  335. void
  336. Smflo(ulong inst)
  337. {
  338. int rd;
  339. rd = (inst>>11)&0x1ff;
  340. if(trace)
  341. itrace("mflo\tr%d", rd);
  342. reg.r[rd] = reg.mlo;
  343. }
  344. void
  345. Smult(ulong inst)
  346. {
  347. int rs, rt;
  348. Mul m;
  349. Getrsrt(rs,rt,inst);
  350. if(trace)
  351. itrace("mult\tr%d,r%d", rs,rt);
  352. m = mul(reg.r[rs], reg.r[rt]);
  353. reg.mlo = m.lo;
  354. reg.mhi = m.hi;
  355. }
  356. void
  357. Smultu(ulong inst)
  358. {
  359. int rs, rt;
  360. Mulu m;
  361. Getrsrt(rs,rt,inst);
  362. if(trace)
  363. itrace("multu\tr%d,r%d", rs,rt);
  364. m = mulu(reg.r[rs], reg.r[rt]);
  365. reg.mlo = m.lo;
  366. reg.mhi = m.hi;
  367. }