Mirror of CollapseOS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

449 lines
11KB

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include "vm.h"
  6. // Port for block reads. Each read or write has to be done in 5 IO writes:
  7. // 1 - r/w. 1 for read, 2 for write.
  8. // 2 - blkid MSB
  9. // 3 - blkid LSB
  10. // 4 - dest addr MSB
  11. // 5 - dest addr LSB
  12. #define BLK_PORT 0x03
  13. static VM vm;
  14. static uint64_t blkop = 0; // 5 bytes
  15. static FILE *blkfp;
  16. // Read single byte from I/O handler, if set. addr is a word only because of
  17. // Forth's cell size, but can't actually address more than a byte-ful of ports.
  18. static byte io_read(word addr)
  19. {
  20. addr &= 0xff;
  21. IORD fn = vm.iord[addr];
  22. if (fn != NULL) {
  23. return fn();
  24. } else {
  25. fprintf(stderr, "Out of bounds I/O read: %d\n", addr);
  26. return 0;
  27. }
  28. }
  29. static void io_write(word addr, byte val)
  30. {
  31. addr &= 0xff;
  32. IOWR fn = vm.iowr[addr];
  33. if (fn != NULL) {
  34. fn(val);
  35. } else {
  36. fprintf(stderr, "Out of bounds I/O write: %d / %d (0x%x)\n", addr, val, val);
  37. }
  38. }
  39. // I/O hook to read/write a chunk of 1024 byte to blkfs at specified blkid.
  40. // This is used by EFS@ and EFS! in xcomp.fs.
  41. // See comment above BLK_PORT define for poking convention.
  42. static void iowr_blk(byte val)
  43. {
  44. blkop <<= 8;
  45. blkop |= val;
  46. byte rw = blkop >> 32;
  47. if (rw) {
  48. word blkid = (blkop >> 16);
  49. word dest = blkop & 0xffff;
  50. blkop = 0;
  51. fseek(blkfp, blkid*1024, SEEK_SET);
  52. if (rw==2) { // write
  53. fwrite(&vm.mem[dest], 1024, 1, blkfp);
  54. } else { // read
  55. fread(&vm.mem[dest], 1024, 1, blkfp);
  56. }
  57. }
  58. }
  59. // get/set word from/to memory
  60. static word gw(word addr) { return vm.mem[addr+(word)1] << 8 | vm.mem[addr]; }
  61. static void sw(word addr, word val) {
  62. vm.mem[addr] = val;
  63. vm.mem[addr+(word)1] = val >> 8;
  64. }
  65. // pop word from SP
  66. static word pop() {
  67. if (vm.uflw) return 0;
  68. if (vm.SP >= SP_ADDR) { vm.uflw = true; return 0; }
  69. return vm.mem[vm.SP++] | vm.mem[vm.SP++] << 8;
  70. }
  71. // push word to SP
  72. static void push(word x) {
  73. vm.SP -= 2;
  74. if (vm.SP <= vm.RS) {
  75. vm.oflw = true; vm.SP = SP_ADDR; vm.RS = RS_ADDR;
  76. return;
  77. }
  78. sw(vm.SP, x);
  79. if (vm.SP < vm.minSP) { vm.minSP = vm.SP; }
  80. }
  81. // pop word from RS
  82. static word popRS() {
  83. if (vm.uflw) return 0;
  84. if (vm.RS <= RS_ADDR) { vm.uflw = true; return 0; }
  85. word x = gw(vm.RS); vm.RS -= 2; return x;
  86. }
  87. // push word to RS
  88. static void pushRS(word val) {
  89. vm.RS += 2;
  90. if (vm.SP <= vm.RS) {
  91. vm.oflw = true; vm.SP = SP_ADDR; vm.RS = RS_ADDR;
  92. return;
  93. }
  94. sw(vm.RS, val);
  95. if (vm.RS > vm.maxRS) { vm.maxRS = vm.RS; }
  96. }
  97. // The functions below directly map to native forth words defined in the
  98. // dictionary (doc/dict.txt)
  99. static void execute(word wordref) {
  100. byte wtype = vm.mem[wordref];
  101. switch (wtype) {
  102. case 0: // native
  103. vm.nativew[vm.mem[wordref+(word)1]]();
  104. break;
  105. case 1: // compiled
  106. pushRS(vm.IP);
  107. vm.IP = wordref+1;
  108. break;
  109. case 2: // cell
  110. push(wordref+1);
  111. break;
  112. case 3: // does
  113. push(wordref+1);
  114. pushRS(vm.IP);
  115. vm.IP = gw(wordref+3);
  116. break;
  117. case 4: // alias
  118. execute(gw(wordref+1));
  119. break;
  120. case 5: // switch
  121. execute(gw(gw(wordref+1)));
  122. break;
  123. }
  124. }
  125. static word find(word daddr, word waddr) {
  126. byte len = vm.mem[waddr];
  127. waddr++;
  128. while (1) {
  129. if ((vm.mem[daddr-(word)1] & 0x7f) == len) {
  130. word d = daddr-3-len;
  131. // Sanity check
  132. if ((waddr+len >= MEMSIZE) || (d+len) >= MEMSIZE) return 0;
  133. if (strncmp(&vm.mem[waddr], &vm.mem[d], len) == 0) {
  134. return daddr;
  135. }
  136. }
  137. daddr -= 3;
  138. word offset = gw(daddr);
  139. if (offset) {
  140. daddr -= offset;
  141. } else {
  142. return 0;
  143. }
  144. }
  145. }
  146. static void EXIT() { vm.IP = popRS(); }
  147. static void _br_() {
  148. word off = vm.mem[vm.IP];
  149. if (off > 0x7f ) { off -= 0x100; }
  150. vm.IP += off;
  151. }
  152. static void _cbr_() { if (!pop()) { _br_(); } else { vm.IP++; } }
  153. static void _loop_() {
  154. word I = gw(vm.RS); I++; sw(vm.RS, I);
  155. if (I == gw(vm.RS-2)) { // don't branch
  156. popRS(); popRS();
  157. vm.IP++;
  158. } else { // branch
  159. _br_();
  160. }
  161. }
  162. static void SP_to_R_2() { word x = pop(); pushRS(pop()); pushRS(x); }
  163. static void nlit() { push(gw(vm.IP)); vm.IP += 2; }
  164. static void slit() { push(vm.IP); vm.IP += vm.mem[vm.IP] + 1; }
  165. static void SP_to_R() { pushRS(pop()); }
  166. static void R_to_SP() { push(popRS()); }
  167. static void R_to_SP_2() { word x = popRS(); push(popRS()); push(x); }
  168. static void EXECUTE() { execute(pop()); }
  169. static void ROT() { // a b c -- b c a
  170. word c = pop(); word b = pop(); word a = pop();
  171. push(b); push(c); push(a);
  172. }
  173. static void ROTR() { // a b c -- c a b
  174. word c = pop(); word b = pop(); word a = pop();
  175. push(c); push(a); push(b);
  176. }
  177. static void DUP() { // a -- a a
  178. word a = pop(); push(a); push(a);
  179. }
  180. static void CDUP() {
  181. word a = pop(); push(a); if (a) { push(a); }
  182. }
  183. static void DROP() { pop(); }
  184. static void SWAP() { // a b -- b a
  185. word b = pop(); word a = pop();
  186. push(b); push(a);
  187. }
  188. static void OVER() { // a b -- a b a
  189. word b = pop(); word a = pop();
  190. push(a); push(b); push(a);
  191. }
  192. static void PICK() {
  193. word x = pop();
  194. push(gw(vm.SP+x*2));
  195. }
  196. static void _roll_() { // "1 2 3 4 4 (roll)" --> "1 3 4 4"
  197. word x = pop();
  198. while (x) { vm.mem[vm.SP+x+(word)2] = vm.mem[vm.SP+x]; x--; }
  199. }
  200. static void DROP2() { pop(); pop(); }
  201. static void DUP2() { // a b -- a b a b
  202. word b = pop(); word a = pop();
  203. push(a); push(b); push(a); push(b);
  204. }
  205. static void S0() { push(SP_ADDR); }
  206. static void Saddr() { push(vm.SP); }
  207. static void AND() { push(pop() & pop()); }
  208. static void OR() { push(pop() | pop()); }
  209. static void XOR() { push(pop() ^ pop()); }
  210. static void NOT() { push(!pop()); }
  211. static void PLUS() { push(pop() + pop()); }
  212. static void MINUS() {
  213. word b = pop(); word a = pop();
  214. push(a - b);
  215. }
  216. static void MULT() { push(pop() * pop()); }
  217. static void DIVMOD() {
  218. word b = pop(); word a = pop();
  219. push(a % b); push(a / b);
  220. }
  221. static void STORE() {
  222. word a = pop(); word val = pop();
  223. sw(a, val);
  224. }
  225. static void FETCH() { push(gw(pop())); }
  226. static void CSTORE() {
  227. word a = pop(); word val = pop();
  228. vm.mem[a] = val;
  229. }
  230. static void CFETCH() { push(vm.mem[pop()]); }
  231. static void IO_OUT() {
  232. word a = pop(); word val = pop();
  233. io_write(a, val);
  234. }
  235. static void IO_IN() { push(io_read(pop())); }
  236. static void RI() { push(gw(vm.RS)); }
  237. static void RI_() { push(gw(vm.RS-2)); }
  238. static void RJ() { push(gw(vm.RS-4)); }
  239. static void BYE() { vm.running = false; }
  240. static void _resSP_() { vm.SP = SP_ADDR; }
  241. static void _resRS_() { vm.RS = RS_ADDR; }
  242. static void Seq() {
  243. word s1 = pop(); word s2 = pop();
  244. byte len = vm.mem[s1];
  245. if (len == vm.mem[s2]) {
  246. s1++; s2++;
  247. push(strncmp(&vm.mem[s1], &vm.mem[s2], len) == 0);
  248. } else {
  249. push(0);
  250. }
  251. }
  252. static void CMP() {
  253. word b = pop(); word a = pop();
  254. if (a == b) { push(0); } else if (a > b) { push(1); } else { push(-1); }
  255. }
  256. static void _find() {
  257. word waddr = pop(); word daddr = pop();
  258. daddr = find(daddr, waddr);
  259. if (daddr) {
  260. push(daddr); push(1);
  261. } else {
  262. push(waddr); push(0);
  263. }
  264. }
  265. static void ZERO() { push(0); }
  266. static void ONE() { push(1); }
  267. static void MONE() { push(-1); }
  268. static void PLUS1() { push(pop()+1); }
  269. static void MINUS1() { push(pop()-1); }
  270. static void MINUS2() { push(pop()-2); }
  271. static void PLUS2() { push(pop()+2); }
  272. static void RSHIFT() { word u = pop(); push(pop()>>u); }
  273. static void LSHIFT() { word u = pop(); push(pop()<<u); }
  274. static void TICKS() { usleep(pop()); }
  275. static void native(NativeWord func) {
  276. vm.nativew[vm.nativew_count++] = func;
  277. }
  278. VM* VM_init(char *bin_path, char *blkfs_path)
  279. {
  280. fprintf(stderr, "Using blkfs %s\n", blkfs_path);
  281. blkfp = fopen(blkfs_path, "r+");
  282. if (!blkfp) {
  283. fprintf(stderr, "Can't open\n");
  284. return NULL;
  285. }
  286. fseek(blkfp, 0, SEEK_END);
  287. if (ftell(blkfp) < 100 * 1024) {
  288. fclose(blkfp);
  289. fprintf(stderr, "emul/blkfs too small, something's wrong, aborting.\n");
  290. return NULL;
  291. }
  292. fseek(blkfp, 0, SEEK_SET);
  293. FILE *bfp = fopen(bin_path, "r");
  294. if (!bfp) {
  295. fprintf(stderr, "Can't open forth.bin\n");
  296. return NULL;
  297. }
  298. int i = 0;
  299. int c = getc(bfp);
  300. while (c != EOF) {
  301. vm.mem[i++] = c;
  302. c = getc(bfp);
  303. }
  304. fclose(bfp);
  305. // initialize rest of memory with random data. Many, many bugs we've seen in
  306. // Collapse OS were caused by bad initialization and weren't reproducable
  307. // in CVM because it has a neat zeroed-out memory. Let's make bugs easier
  308. // to spot.
  309. while (i<0x10000) {
  310. vm.mem[i++] = random();
  311. }
  312. vm.SP = SP_ADDR;
  313. vm.RS = RS_ADDR;
  314. vm.minSP = SP_ADDR;
  315. vm.maxRS = RS_ADDR;
  316. vm.nativew_count = 0;
  317. for (int i=0; i<0x100; i++) {
  318. vm.iord[i] = NULL;
  319. vm.iowr[i] = NULL;
  320. }
  321. vm.iowr[BLK_PORT] = iowr_blk;
  322. // Added in the same order as in xcomp.fs
  323. native(EXIT);
  324. native(_br_);
  325. native(_cbr_);
  326. native(_loop_);
  327. native(nlit);
  328. native(slit);
  329. native(SP_to_R);
  330. native(R_to_SP);
  331. native(SP_to_R_2);
  332. native(R_to_SP_2);
  333. native(EXECUTE);
  334. native(ROT);
  335. native(DUP);
  336. native(CDUP);
  337. native(DROP);
  338. native(SWAP);
  339. native(OVER);
  340. native(PICK);
  341. native(_roll_);
  342. native(DROP2);
  343. native(DUP2);
  344. native(S0);
  345. native(Saddr);
  346. native(AND);
  347. native(OR);
  348. native(XOR);
  349. native(NOT);
  350. native(PLUS);
  351. native(MINUS);
  352. native(MULT);
  353. native(DIVMOD);
  354. native(STORE);
  355. native(FETCH);
  356. native(CSTORE);
  357. native(CFETCH);
  358. native(IO_OUT);
  359. native(IO_IN);
  360. native(RI);
  361. native(RI_);
  362. native(RJ);
  363. native(BYE);
  364. native(_resSP_);
  365. native(_resRS_);
  366. native(Seq);
  367. native(CMP);
  368. native(_find);
  369. native(ZERO);
  370. native(ONE);
  371. native(MONE);
  372. native(PLUS1);
  373. native(MINUS1);
  374. native(PLUS2);
  375. native(MINUS2);
  376. native(RSHIFT);
  377. native(LSHIFT);
  378. native(TICKS);
  379. native(ROTR);
  380. vm.IP = gw(0x04) + 1; // BOOT
  381. sw(SYSVARS+0x02, gw(0x08)); // CURRENT
  382. sw(SYSVARS+0x04, gw(0x08)); // HERE
  383. vm.uflw = false;
  384. vm.oflw = false;
  385. vm.running = true;
  386. return &vm;
  387. }
  388. void VM_deinit()
  389. {
  390. fclose(blkfp);
  391. }
  392. bool VM_steps(int n) {
  393. if (!vm.running) {
  394. fprintf(stderr, "machine halted!\n");
  395. return false;
  396. }
  397. while (n && vm.running) {
  398. word wordref = gw(vm.IP);
  399. vm.IP += 2;
  400. execute(wordref);
  401. if (vm.uflw) {
  402. vm.uflw = false;
  403. execute(gw(0x06)); /* uflw */
  404. }
  405. if (vm.oflw) {
  406. vm.oflw = false;
  407. execute(gw(0x13)); /* oflw */
  408. }
  409. n--;
  410. }
  411. return vm.running;
  412. }
  413. void VM_memdump() {
  414. fprintf(stderr, "Dumping memory to memdump. IP %04x\n", vm.IP);
  415. FILE *fp = fopen("memdump", "w");
  416. fwrite(vm.mem, 0x10000, 1, fp);
  417. fclose(fp);
  418. }
  419. void VM_debugstr(char *s) {
  420. sprintf(s, "SP %04x (%04x) RS %04x (%04x)",
  421. vm.SP, vm.minSP, vm.RS, vm.maxRS);
  422. }
  423. void VM_printdbg() {
  424. char buf[0x100];
  425. VM_debugstr(buf);
  426. fprintf(stderr, "%s\n", buf);
  427. }