Mirror of CollapseOS
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

443 satır
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. switch (wtype) {
  105. case 0: // native
  106. vm.nativew[vm.mem[wordref+1]]();
  107. break;
  108. case 1: // compiled
  109. pushRS(vm.IP);
  110. vm.IP = wordref+1;
  111. break;
  112. case 2: // cell
  113. push(wordref+1);
  114. break;
  115. case 3: // does
  116. push(wordref+1);
  117. pushRS(vm.IP);
  118. vm.IP = gw(wordref+3);
  119. break;
  120. case 4: // alias
  121. execute(gw(wordref+1));
  122. break;
  123. case 5: // switch
  124. execute(gw(gw(wordref+1)));
  125. break;
  126. }
  127. }
  128. static word find(word daddr, word waddr) {
  129. byte len = vm.mem[waddr];
  130. while (1) {
  131. if ((vm.mem[daddr-1] & 0x7f) == len) {
  132. if (strncmp(&vm.mem[waddr+1], &vm.mem[daddr-3-len], len) == 0) {
  133. return daddr;
  134. }
  135. }
  136. daddr -= 3;
  137. word offset = gw(daddr);
  138. if (offset) {
  139. daddr -= offset;
  140. } else {
  141. return 0;
  142. }
  143. }
  144. }
  145. static void EXIT() { vm.IP = popRS(); }
  146. static void _br_() {
  147. word off = vm.mem[vm.IP];
  148. if (off > 0x7f ) { off -= 0x100; }
  149. vm.IP += off;
  150. }
  151. static void _cbr_() { if (!pop()) { _br_(); } else { vm.IP++; } }
  152. static void _loop_() {
  153. word I = gw(vm.RS); I++; sw(vm.RS, I);
  154. if (I == gw(vm.RS-2)) { // don't branch
  155. popRS(); popRS();
  156. vm.IP++;
  157. } else { // branch
  158. _br_();
  159. }
  160. }
  161. static void SP_to_R_2() { word x = pop(); pushRS(pop()); pushRS(x); }
  162. static void nlit() { push(gw(vm.IP)); vm.IP += 2; }
  163. static void slit() { push(vm.IP); vm.IP += vm.mem[vm.IP] + 1; }
  164. static void SP_to_R() { pushRS(pop()); }
  165. static void R_to_SP() { push(popRS()); }
  166. static void R_to_SP_2() { word x = popRS(); push(popRS()); push(x); }
  167. static void EXECUTE() { execute(pop()); }
  168. static void ROT() { // a b c -- b c a
  169. word c = pop(); word b = pop(); word a = pop();
  170. push(b); push(c); push(a);
  171. }
  172. static void DUP() { // a -- a a
  173. word a = pop(); push(a); push(a);
  174. }
  175. static void CDUP() {
  176. word a = pop(); push(a); if (a) { push(a); }
  177. }
  178. static void DROP() { pop(); }
  179. static void SWAP() { // a b -- b a
  180. word b = pop(); word a = pop();
  181. push(b); push(a);
  182. }
  183. static void OVER() { // a b -- a b a
  184. word b = pop(); word a = pop();
  185. push(a); push(b); push(a);
  186. }
  187. static void PICK() {
  188. word x = pop();
  189. push(gw(vm.SP+x*2));
  190. }
  191. static void _roll_() { // "1 2 3 4 4 (roll)" --> "1 3 4 4"
  192. word x = pop();
  193. while (x) { vm.mem[vm.SP+x+2] = vm.mem[vm.SP+x]; x--; }
  194. }
  195. static void DROP2() { pop(); pop(); }
  196. static void DUP2() { // a b -- a b a b
  197. word b = pop(); word a = pop();
  198. push(a); push(b); push(a); push(b);
  199. }
  200. static void S0() { push(SP_ADDR); }
  201. static void Saddr() { push(vm.SP); }
  202. static void AND() { push(pop() & pop()); }
  203. static void OR() { push(pop() | pop()); }
  204. static void XOR() { push(pop() ^ pop()); }
  205. static void NOT() { push(!pop()); }
  206. static void PLUS() { push(pop() + pop()); }
  207. static void MINUS() {
  208. word b = pop(); word a = pop();
  209. push(a - b);
  210. }
  211. static void MULT() { push(pop() * pop()); }
  212. static void DIVMOD() {
  213. word b = pop(); word a = pop();
  214. push(a % b); push(a / b);
  215. }
  216. static void STORE() {
  217. word a = pop(); word val = pop();
  218. sw(a, val);
  219. }
  220. static void FETCH() { push(gw(pop())); }
  221. static void CSTORE() {
  222. word a = pop(); word val = pop();
  223. vm.mem[a] = val;
  224. }
  225. static void CFETCH() { push(vm.mem[pop()]); }
  226. static void IO_OUT() {
  227. word a = pop(); word val = pop();
  228. io_write(a, val);
  229. }
  230. static void IO_IN() { push(io_read(pop())); }
  231. static void RI() { push(gw(vm.RS)); }
  232. static void RI_() { push(gw(vm.RS-2)); }
  233. static void RJ() { push(gw(vm.RS-4)); }
  234. static void BYE() { vm.running = false; }
  235. static void _resSP_() { vm.SP = SP_ADDR; }
  236. static void _resRS_() { vm.RS = RS_ADDR; }
  237. static void Seq() {
  238. word s1 = pop(); word s2 = pop();
  239. byte len = vm.mem[s1];
  240. if (len == vm.mem[s2]) {
  241. s1++; s2++;
  242. push(strncmp(&vm.mem[s1], &vm.mem[s2], len) == 0);
  243. } else {
  244. push(0);
  245. }
  246. }
  247. static void CMP() {
  248. word b = pop(); word a = pop();
  249. if (a == b) { push(0); } else if (a > b) { push(1); } else { push(-1); }
  250. }
  251. static void _find() {
  252. word waddr = pop(); word daddr = pop();
  253. daddr = find(daddr, waddr);
  254. if (daddr) {
  255. push(daddr); push(1);
  256. } else {
  257. push(waddr); push(0);
  258. }
  259. }
  260. static void ZERO() { push(0); }
  261. static void ONE() { push(1); }
  262. static void MONE() { push(-1); }
  263. static void PLUS1() { push(pop()+1); }
  264. static void MINUS1() { push(pop()-1); }
  265. static void MINUS2() { push(pop()-2); }
  266. static void PLUS2() { push(pop()+2); }
  267. static void RSHIFT() { word u = pop(); push(pop()>>u); }
  268. static void LSHIFT() { word u = pop(); push(pop()<<u); }
  269. static void TICKS() { usleep(pop()); }
  270. static void native(NativeWord func) {
  271. vm.nativew[vm.nativew_count++] = func;
  272. }
  273. VM* VM_init(char *blkfs_path) {
  274. fprintf(stderr, "Using blkfs %s\n", blkfs_path);
  275. blkfp = fopen(blkfs_path, "r+");
  276. if (!blkfp) {
  277. fprintf(stderr, "Can't open\n");
  278. return NULL;
  279. }
  280. fseek(blkfp, 0, SEEK_END);
  281. if (ftell(blkfp) < 100 * 1024) {
  282. fclose(blkfp);
  283. fprintf(stderr, "emul/blkfs too small, something's wrong, aborting.\n");
  284. return NULL;
  285. }
  286. fseek(blkfp, 0, SEEK_SET);
  287. FILE *bfp = fopen(FBIN_PATH, "r");
  288. if (!bfp) {
  289. fprintf(stderr, "Can't open forth.bin\n");
  290. return NULL;
  291. }
  292. int i = 0;
  293. int c = getc(bfp);
  294. while (c != EOF) {
  295. vm.mem[i++] = c;
  296. c = getc(bfp);
  297. }
  298. fclose(bfp);
  299. // initialize rest of memory with random data. Many, many bugs we've seen in
  300. // Collapse OS were caused by bad initialization and weren't reproducable
  301. // in CVM because it has a neat zeroed-out memory. Let's make bugs easier
  302. // to spot.
  303. while (i<0x10000) {
  304. vm.mem[i++] = random();
  305. }
  306. vm.SP = SP_ADDR;
  307. vm.RS = RS_ADDR;
  308. vm.minSP = SP_ADDR;
  309. vm.maxRS = RS_ADDR;
  310. vm.nativew_count = 0;
  311. for (int i=0; i<0x100; i++) {
  312. vm.iord[i] = NULL;
  313. vm.iowr[i] = NULL;
  314. }
  315. vm.iowr[BLK_PORT] = iowr_blk;
  316. // Added in the same order as in xcomp.fs
  317. native(EXIT);
  318. native(_br_);
  319. native(_cbr_);
  320. native(_loop_);
  321. native(nlit);
  322. native(slit);
  323. native(SP_to_R);
  324. native(R_to_SP);
  325. native(SP_to_R_2);
  326. native(R_to_SP_2);
  327. native(EXECUTE);
  328. native(ROT);
  329. native(DUP);
  330. native(CDUP);
  331. native(DROP);
  332. native(SWAP);
  333. native(OVER);
  334. native(PICK);
  335. native(_roll_);
  336. native(DROP2);
  337. native(DUP2);
  338. native(S0);
  339. native(Saddr);
  340. native(AND);
  341. native(OR);
  342. native(XOR);
  343. native(NOT);
  344. native(PLUS);
  345. native(MINUS);
  346. native(MULT);
  347. native(DIVMOD);
  348. native(STORE);
  349. native(FETCH);
  350. native(CSTORE);
  351. native(CFETCH);
  352. native(IO_OUT);
  353. native(IO_IN);
  354. native(RI);
  355. native(RI_);
  356. native(RJ);
  357. native(BYE);
  358. native(_resSP_);
  359. native(_resRS_);
  360. native(Seq);
  361. native(CMP);
  362. native(_find);
  363. native(ZERO);
  364. native(ONE);
  365. native(MONE);
  366. native(PLUS1);
  367. native(MINUS1);
  368. native(PLUS2);
  369. native(MINUS2);
  370. native(RSHIFT);
  371. native(LSHIFT);
  372. native(TICKS);
  373. vm.IP = gw(0x04) + 1; // BOOT
  374. sw(SYSVARS+0x02, gw(0x08)); // CURRENT
  375. sw(SYSVARS+0x04, gw(0x08)); // HERE
  376. vm.uflw = false;
  377. vm.oflw = false;
  378. vm.running = true;
  379. return &vm;
  380. }
  381. void VM_deinit()
  382. {
  383. fclose(blkfp);
  384. }
  385. bool VM_steps(int n) {
  386. if (!vm.running) {
  387. fprintf(stderr, "machine halted!\n");
  388. return false;
  389. }
  390. while (n && vm.running) {
  391. word wordref = gw(vm.IP);
  392. vm.IP += 2;
  393. execute(wordref);
  394. if (vm.uflw) {
  395. vm.uflw = false;
  396. execute(gw(0x06)); /* uflw */
  397. }
  398. if (vm.oflw) {
  399. vm.oflw = false;
  400. execute(gw(0x13)); /* oflw */
  401. }
  402. n--;
  403. }
  404. return vm.running;
  405. }
  406. void VM_memdump() {
  407. fprintf(stderr, "Dumping memory to memdump. IP %04x\n", vm.IP);
  408. FILE *fp = fopen("memdump", "w");
  409. fwrite(vm.mem, 0x10000, 1, fp);
  410. fclose(fp);
  411. }
  412. void VM_debugstr(char *s) {
  413. sprintf(s, "SP %04x (%04x) RS %04x (%04x)",
  414. vm.SP, vm.minSP, vm.RS, vm.maxRS);
  415. }
  416. void VM_printdbg() {
  417. char buf[0x100];
  418. VM_debugstr(buf);
  419. fprintf(stderr, "%s\n", buf);
  420. }