Mirror of CollapseOS
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

136 строки
3.0KB

  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include <termios.h>
  5. #include "emul.h"
  6. #include "forth-bin.h"
  7. #include "blkfs-bin.h"
  8. // in sync with glue.asm
  9. #define RAMSTART 0x900
  10. #define STDIO_PORT 0x00
  11. // This binary is also used for automated tests and those tests, when
  12. // failing, send a non-zero value to RET_PORT to indicate failure
  13. #define RET_PORT 0x01
  14. // Port for block reads. Write 2 bytes, MSB first, on that port and then
  15. // read 1024 bytes from the DATA port.
  16. #define BLK_PORT 0x03
  17. #define BLKDATA_PORT 0x04
  18. static FILE *fp;
  19. static FILE *blkfp;
  20. static int retcode = 0;
  21. static uint16_t blkid = 0;
  22. static uint8_t iord_stdio()
  23. {
  24. int c = getc(fp);
  25. if (c == EOF) {
  26. c = 4; // ASCII EOT
  27. }
  28. return (uint8_t)c;
  29. }
  30. static void iowr_stdio(uint8_t val)
  31. {
  32. putchar(val);
  33. }
  34. static void iowr_ret(uint8_t val)
  35. {
  36. retcode = val;
  37. }
  38. static void iowr_blk(uint8_t val)
  39. {
  40. blkid <<= 8;
  41. blkid |= val;
  42. if (blkfp != NULL) {
  43. fseek(blkfp, blkid*1024, SEEK_SET);
  44. }
  45. }
  46. static uint8_t iord_blkdata()
  47. {
  48. uint8_t res = 0;
  49. if (blkfp != NULL) {
  50. int c = getc(blkfp);
  51. if (c != EOF) {
  52. res = c;
  53. }
  54. }
  55. return res;
  56. }
  57. static void iowr_blkdata(uint8_t val)
  58. {
  59. if (blkfp != NULL) {
  60. putc(val, blkfp);
  61. }
  62. }
  63. int main(int argc, char *argv[])
  64. {
  65. bool tty = false;
  66. struct termios termInfo;
  67. if (argc == 2) {
  68. fp = fopen(argv[1], "r");
  69. if (fp == NULL) {
  70. fprintf(stderr, "Can't open %s\n", argv[1]);
  71. return 1;
  72. }
  73. } else if (argc == 1) {
  74. fp = stdin;
  75. tty = isatty(fileno(stdin));
  76. if (tty) {
  77. // Turn echo off: the shell takes care of its own echoing.
  78. if (tcgetattr(0, &termInfo) == -1) {
  79. printf("Can't setup terminal.\n");
  80. return 1;
  81. }
  82. termInfo.c_lflag &= ~ECHO;
  83. termInfo.c_lflag &= ~ICANON;
  84. tcsetattr(0, TCSAFLUSH, &termInfo);
  85. }
  86. } else {
  87. fprintf(stderr, "Usage: ./forth [filename]\n");
  88. return 1;
  89. }
  90. blkfp = fopen("blkfs", "r+");
  91. if (blkfp) {
  92. fprintf(stderr, "Using blkfs file\n");
  93. } else {
  94. blkfp = fmemopen((char*)BLKFS, sizeof(BLKFS), "r");
  95. fprintf(stderr, "Using in-memory read-only blkfs\n");
  96. }
  97. Machine *m = emul_init();
  98. m->ramstart = RAMSTART;
  99. m->iord[STDIO_PORT] = iord_stdio;
  100. m->iowr[STDIO_PORT] = iowr_stdio;
  101. m->iowr[RET_PORT] = iowr_ret;
  102. m->iowr[BLK_PORT] = iowr_blk;
  103. m->iord[BLKDATA_PORT] = iord_blkdata;
  104. m->iowr[BLKDATA_PORT] = iowr_blkdata;
  105. // initialize memory
  106. for (int i=0; i<sizeof(KERNEL); i++) {
  107. m->mem[i] = KERNEL[i];
  108. }
  109. // Run!
  110. while (emul_step());
  111. if (tty) {
  112. printf("\nDone!\n");
  113. termInfo.c_lflag |= ECHO;
  114. termInfo.c_lflag |= ICANON;
  115. tcsetattr(0, TCSAFLUSH, &termInfo);
  116. emul_printdebug();
  117. }
  118. if (blkfp != NULL) {
  119. fclose(blkfp);
  120. }
  121. fclose(fp);
  122. return retcode;
  123. }