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.

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