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.5KB

  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) {
  51. wechochar(w, val);
  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(FBIN_PATH, 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, 0);
  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. }