Mirror of CollapseOS
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

81 lines
2.0KB

  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include "emul.h"
  5. /* Staging binaries
  6. The role of a stage executable is to compile definitions in a dictionary and
  7. then spit the difference between the starting binary and the new binary.
  8. That binary can then be grafted to an exiting Forth binary to augment its
  9. dictionary.
  10. We could, if we wanted, run only with the bootstrap binary and compile core
  11. defs at runtime, but that would mean that those defs live in RAM. In may system,
  12. RAM is much more constrained than ROM, so it's worth it to give ourselves the
  13. trouble of compiling defs to binary.
  14. */
  15. #define RAMSTART 0
  16. #define STDIO_PORT 0x00
  17. // To know which part of RAM to dump, we listen to port 2, which at the end of
  18. // its compilation process, spits its HERE addr to port 2 (MSB first)
  19. #define HERE_PORT 0x02
  20. static int running;
  21. // We support double-pokes, that is, a first poke to tell where to start the
  22. // dump and a second one to tell where to stop. If there is only one poke, it's
  23. // then ending HERE and we start at sizeof(KERNEL).
  24. static uint16_t start_here = 0;
  25. static uint16_t end_here = 0;
  26. static uint8_t iord_stdio()
  27. {
  28. int c = getc(stdin);
  29. if (c == EOF) {
  30. running = 0;
  31. }
  32. return (uint8_t)c;
  33. }
  34. static void iowr_stdio(uint8_t val)
  35. {
  36. // uncomment when you need to debug staging
  37. // putc(val, stderr);
  38. }
  39. static void iowr_here(uint8_t val)
  40. {
  41. start_here <<=8;
  42. start_here |= (end_here >> 8);
  43. end_here <<= 8;
  44. end_here |= val;
  45. }
  46. int main(int argc, char *argv[])
  47. {
  48. Machine *m = emul_init();
  49. if (m == NULL) {
  50. return 1;
  51. }
  52. m->ramstart = RAMSTART;
  53. m->iord[STDIO_PORT] = iord_stdio;
  54. m->iowr[STDIO_PORT] = iowr_stdio;
  55. m->iowr[HERE_PORT] = iowr_here;
  56. // Run!
  57. running = 1;
  58. while (running && emul_step());
  59. // We're done, now let's spit dict data
  60. for (int i=start_here; i<end_here; i++) {
  61. putchar(m->mem[i]);
  62. }
  63. emul_deinit();
  64. emul_printdebug();
  65. return 0;
  66. }