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.

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