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.

122 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 "emul.h"
  7. #define WCOLS 80
  8. #define WLINES 32
  9. // in sync with glue.asm
  10. #define RAMSTART 0x900
  11. #define STDIO_PORT 0x00
  12. // This binary is also used for automated tests and those tests, when
  13. // failing, send a non-zero value to RET_PORT to indicate failure
  14. #define RET_PORT 0x01
  15. #define SETX_PORT 0x05
  16. #define SETY_PORT 0x06
  17. static FILE *fp;
  18. static int retcode = 0;
  19. WINDOW *bw, *dw, *w;
  20. void debug_panel()
  21. {
  22. char buf[30];
  23. emul_debugstr(buf);
  24. mvwaddnstr(dw, 0, 0, buf, 30);
  25. wrefresh(dw);
  26. }
  27. static uint8_t iord_stdio()
  28. {
  29. int c;
  30. if (fp != NULL) {
  31. c = getc(fp);
  32. } else {
  33. debug_panel();
  34. c = wgetch(w);
  35. }
  36. if (c == EOF) {
  37. c = 4; // ASCII EOT
  38. }
  39. return (uint8_t)c;
  40. }
  41. static void iowr_stdio(uint8_t val)
  42. {
  43. if (fp != NULL) {
  44. putchar(val);
  45. } else {
  46. if (val >= 0x20 || val == '\n') {
  47. wechochar(w, val);
  48. } else if (val == 0x08) {
  49. int y, x; getyx(w, y, x);
  50. wmove(w, y, x-1);
  51. }
  52. }
  53. }
  54. static void iowr_ret(uint8_t val)
  55. {
  56. retcode = val;
  57. }
  58. static void iowr_setx(uint8_t val)
  59. {
  60. int y, x; getyx(w, y, x);
  61. wmove(w, y, val);
  62. }
  63. static void iowr_sety(uint8_t val)
  64. {
  65. int y, x; getyx(w, y, x);
  66. wmove(w, val, x);
  67. }
  68. int main(int argc, char *argv[])
  69. {
  70. Machine *m = emul_init();
  71. if (m == NULL) {
  72. return 1;
  73. }
  74. m->ramstart = RAMSTART;
  75. m->iord[STDIO_PORT] = iord_stdio;
  76. m->iowr[STDIO_PORT] = iowr_stdio;
  77. m->iowr[RET_PORT] = iowr_ret;
  78. m->iowr[SETX_PORT] = iowr_setx;
  79. m->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 (emul_step());
  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 (emul_steps(1000)) {
  101. debug_panel();
  102. }
  103. nocbreak(); echo(); delwin(w); delwin(bw); delwin(dw); endwin();
  104. printf("\nDone!\n");
  105. emul_printdebug();
  106. } else {
  107. fprintf(stderr, "Usage: ./forth [filename]\n");
  108. retcode = 1;
  109. }
  110. emul_deinit();
  111. return retcode;
  112. }