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.

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