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.

152 lines
3.3KB

  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <curses.h>
  4. #include "cpu.h"
  5. #define WCOLS 80
  6. #define WLINES 25
  7. #ifndef FBIN_PATH
  8. #error FBIN_PATH needed
  9. #endif
  10. #ifndef BLKFS_PATH
  11. #error BLKFS_PATH needed
  12. #endif
  13. extern uint8_t byteregtable[8];
  14. extern union _bytewordregs_ regs;
  15. extern INTHOOK INTHOOKS[0x100];
  16. static FILE *fp;
  17. static FILE *blkfp;
  18. static int retcode = 0;
  19. WINDOW *bw, *dw, *w;
  20. /* we have a fake INT API:
  21. INT 1: EMIT. AL = char to spit
  22. INT 2: KEY. AL = char read
  23. INT 3: AT-XY. AL = x, BL = y
  24. INT 4: BLKREAD. AX = blkid, BX = dest addr
  25. INT 5: BLKWRITE. AX = blkid, BX = src addr
  26. INT 6: RETCODE. AX = retcode.
  27. */
  28. void int1() {
  29. uint8_t val = regs.byteregs[regal];
  30. if (fp != NULL) {
  31. putchar(val);
  32. } else {
  33. if (val >= 0x20 || val == '\n') {
  34. wechochar(w, val);
  35. } else if (val == 0x08) {
  36. int y, x; getyx(w, y, x);
  37. wmove(w, y, x-1);
  38. }
  39. }
  40. }
  41. void int2() {
  42. int c;
  43. if (fp != NULL) {
  44. c = getc(fp);
  45. } else {
  46. // debug_panel();
  47. c = wgetch(w);
  48. }
  49. if (c == EOF) {
  50. c = 4; // ASCII EOT
  51. }
  52. regs.byteregs[regal] = c;
  53. }
  54. void int3() {
  55. wmove(w, regs.byteregs[regbl], regs.byteregs[regal]);
  56. }
  57. void int4() {
  58. uint16_t blkid = getreg16(regax);
  59. uint16_t dest = getreg16(regbx);
  60. fseek(blkfp, blkid*1024, SEEK_SET);
  61. for (int i=0; i<1024; i++) {
  62. write86(dest+i, getc(blkfp));
  63. }
  64. }
  65. void int5() {
  66. uint16_t blkid = getreg16(regax);
  67. uint16_t dest = getreg16(regbx);
  68. fseek(blkfp, blkid*1024, SEEK_SET);
  69. for (int i=0; i<1024; i++) {
  70. putc(read86(dest+i), blkfp);
  71. }
  72. }
  73. void int6() {
  74. retcode = getreg16(regax);
  75. }
  76. int main(int argc, char *argv[])
  77. {
  78. INTHOOKS[1] = int1;
  79. INTHOOKS[2] = int2;
  80. INTHOOKS[3] = int3;
  81. INTHOOKS[4] = int4;
  82. INTHOOKS[5] = int5;
  83. INTHOOKS[6] = int6;
  84. reset86(0);
  85. fprintf(stderr, "Using blkfs %s\n", BLKFS_PATH);
  86. blkfp = fopen(BLKFS_PATH, "r+");
  87. if (!blkfp) {
  88. fprintf(stderr, "Can't open\n");
  89. return 1;
  90. }
  91. fseek(blkfp, 0, SEEK_END);
  92. if (ftell(blkfp) < 100 * 1024) {
  93. fclose(blkfp);
  94. fprintf(stderr, "emul/blkfs too small, something's wrong, aborting.\n");
  95. return 1;
  96. }
  97. fseek(blkfp, 0, SEEK_SET);
  98. // initialize memory
  99. FILE *bfp = fopen(FBIN_PATH, "r");
  100. if (!bfp) {
  101. fprintf(stderr, "Can't open forth.bin\n");
  102. return 1;
  103. }
  104. int i = 0;
  105. int c = getc(bfp);
  106. while (c != EOF) {
  107. write86(i++, c);
  108. c = getc(bfp);
  109. }
  110. fclose(bfp);
  111. w = NULL;
  112. if (argc == 2) {
  113. fp = fopen(argv[1], "r");
  114. if (fp == NULL) {
  115. fprintf(stderr, "Can't open %s\n", argv[1]);
  116. return 1;
  117. }
  118. while (exec86(100));
  119. fclose(fp);
  120. } else if (argc == 1) {
  121. initscr(); cbreak(); noecho(); nl(); clear();
  122. // border window
  123. bw = newwin(WLINES+2, WCOLS+2, 0, 0);
  124. wborder(bw, 0, 0, 0, 0, 0, 0, 0, 0);
  125. wrefresh(bw);
  126. // debug panel
  127. dw = newwin(1, 30, LINES-1, COLS-30);
  128. w = newwin(WLINES, WCOLS, 1, 1);
  129. scrollok(w, 1);
  130. while (exec86(100)) {
  131. //debug_panel();
  132. }
  133. nocbreak(); echo(); delwin(w); delwin(bw); delwin(dw); endwin();
  134. printf("\nDone!\n");
  135. //emul_printdebug();
  136. }
  137. return retcode;
  138. }