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.

126 lines
2.6KB

  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <curses.h>
  5. #include <termios.h>
  6. #include "vm.h"
  7. #ifndef BLKFS_PATH
  8. #error BLKFS_PATH needed
  9. #endif
  10. #ifndef FBIN_PATH
  11. #error FBIN_PATH needed
  12. #endif
  13. #define WCOLS 80
  14. #define WLINES 32
  15. #define STDIO_PORT 0x00
  16. // This binary is also used for automated tests and those tests, when
  17. // failing, send a non-zero value to RET_PORT to indicate failure
  18. #define RET_PORT 0x01
  19. #define SETX_PORT 0x05
  20. #define SETY_PORT 0x06
  21. static FILE *fp;
  22. static int retcode = 0;
  23. WINDOW *bw, *dw, *w;
  24. void debug_panel()
  25. {
  26. char buf[30];
  27. VM_debugstr(buf);
  28. mvwaddnstr(dw, 0, 0, buf, 30);
  29. wrefresh(dw);
  30. }
  31. static uint8_t iord_stdio()
  32. {
  33. int c;
  34. if (fp != NULL) {
  35. c = getc(fp);
  36. } else {
  37. debug_panel();
  38. c = wgetch(w);
  39. }
  40. if (c == EOF) {
  41. c = 4; // ASCII EOT
  42. }
  43. return (uint8_t)c;
  44. }
  45. static void iowr_stdio(uint8_t val)
  46. {
  47. if (fp != NULL) {
  48. putchar(val);
  49. } else {
  50. if (val >= 0x20 || val == '\n') {
  51. wechochar(w, val);
  52. } else if (val == 0x08) {
  53. int y, x; getyx(w, y, x);
  54. wmove(w, y, x-1);
  55. }
  56. }
  57. }
  58. static void iowr_ret(uint8_t val)
  59. {
  60. retcode = val;
  61. }
  62. static void iowr_setx(uint8_t val)
  63. {
  64. int y, x; getyx(w, y, x);
  65. wmove(w, y, val);
  66. }
  67. static void iowr_sety(uint8_t val)
  68. {
  69. int y, x; getyx(w, y, x);
  70. wmove(w, val, x);
  71. }
  72. int main(int argc, char *argv[])
  73. {
  74. VM *vm = VM_init(FBIN_PATH, BLKFS_PATH);
  75. if (!vm) {
  76. return 1;
  77. }
  78. vm->iord[STDIO_PORT] = iord_stdio;
  79. vm->iowr[STDIO_PORT] = iowr_stdio;
  80. vm->iowr[RET_PORT] = iowr_ret;
  81. vm->iowr[SETX_PORT] = iowr_setx;
  82. vm->iowr[SETY_PORT] = iowr_sety;
  83. w = NULL;
  84. if (argc == 2) {
  85. fp = fopen(argv[1], "r");
  86. if (fp == NULL) {
  87. fprintf(stderr, "Can't open %s\n", argv[1]);
  88. return 1;
  89. }
  90. while (VM_steps(1000));
  91. fclose(fp);
  92. } else if (argc == 1) {
  93. fp = NULL;
  94. initscr(); cbreak(); noecho(); nl(); clear();
  95. // border window
  96. bw = newwin(WLINES+2, WCOLS+2, 0, 0);
  97. wborder(bw, 0, 0, 0, 0, 0, 0, 0, 0);
  98. wrefresh(bw);
  99. // debug panel
  100. dw = newwin(1, 30, LINES-1, COLS-30);
  101. w = newwin(WLINES, WCOLS, 1, 1);
  102. scrollok(w, 1);
  103. while (VM_steps(1000)) {
  104. debug_panel();
  105. }
  106. nocbreak(); echo(); delwin(w); delwin(bw); delwin(dw); endwin();
  107. printf("\nDone!\n");
  108. fprintf(stderr, "Done!\n");
  109. VM_printdbg();
  110. } else {
  111. fprintf(stderr, "Usage: ./forth [filename]\n");
  112. retcode = 1;
  113. }
  114. VM_deinit();
  115. return retcode;
  116. }