special.c 7.1 KB

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