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.

113 lines
2.7KB

  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include "emul.h"
  5. #include "forth-bin.h"
  6. #include "blkfs-bin.h"
  7. /* Staging binaries
  8. The role of a stage executable is to compile definitions in a dictionary and
  9. then spit the difference between the starting binary and the new binary.
  10. That binary can then be grafted to an exiting Forth binary to augment its
  11. dictionary.
  12. We could, if we wanted, run only with the bootstrap binary and compile core
  13. defs at runtime, but that would mean that those defs live in RAM. In may system,
  14. RAM is much more constrained than ROM, so it's worth it to give ourselves the
  15. trouble of compiling defs to binary.
  16. */
  17. // When DEBUG is set, stage1 is a core-less forth that works interactively.
  18. // Useful for... debugging!
  19. // By the way: there's a double-echo in stagedbg. It's normal. Don't panic.
  20. //#define DEBUG
  21. #define RAMSTART 0
  22. #define STDIO_PORT 0x00
  23. // To know which part of RAM to dump, we listen to port 2, which at the end of
  24. // its compilation process, spits its HERE addr to port 2 (MSB first)
  25. #define HERE_PORT 0x02
  26. // Port for block reads. Write 2 bytes, MSB first, on that port and then
  27. // read 1024 bytes from the DATA port.
  28. #define BLK_PORT 0x03
  29. #define BLKDATA_PORT 0x04
  30. static int running;
  31. // We support double-pokes, that is, a first poke to tell where to start the
  32. // dump and a second one to tell where to stop. If there is only one poke, it's
  33. // then ending HERE and we start at sizeof(KERNEL).
  34. static uint16_t start_here = 0;
  35. static uint16_t end_here = 0;
  36. static uint16_t blkid = 0;
  37. static unsigned int blkpos = 0;
  38. static uint8_t iord_stdio()
  39. {
  40. int c = getc(stdin);
  41. if (c == EOF) {
  42. running = 0;
  43. }
  44. return (uint8_t)c;
  45. }
  46. static void iowr_stdio(uint8_t val)
  47. {
  48. // we don't output stdout in stage0
  49. #ifdef DEBUG
  50. // ... unless we're in DEBUG mode!
  51. putchar(val);
  52. #endif
  53. }
  54. static void iowr_here(uint8_t val)
  55. {
  56. start_here <<=8;
  57. start_here |= (end_here >> 8);
  58. end_here <<= 8;
  59. end_here |= val;
  60. }
  61. static void iowr_blk(uint8_t val)
  62. {
  63. blkid <<= 8;
  64. blkid |= val;
  65. blkpos = blkid * 1024;
  66. }
  67. static uint8_t iord_blkdata()
  68. {
  69. return BLKFS[blkpos++];
  70. }
  71. int main(int argc, char *argv[])
  72. {
  73. Machine *m = emul_init();
  74. m->ramstart = RAMSTART;
  75. m->iord[STDIO_PORT] = iord_stdio;
  76. m->iowr[STDIO_PORT] = iowr_stdio;
  77. m->iowr[HERE_PORT] = iowr_here;
  78. m->iowr[BLK_PORT] = iowr_blk;
  79. m->iord[BLKDATA_PORT] = iord_blkdata;
  80. // initialize memory
  81. for (int i=0; i<sizeof(KERNEL); i++) {
  82. m->mem[i] = KERNEL[i];
  83. }
  84. // Run!
  85. running = 1;
  86. while (running && emul_step());
  87. #ifndef DEBUG
  88. // We're done, now let's spit dict data
  89. for (int i=start_here; i<end_here; i++) {
  90. putchar(m->mem[i]);
  91. }
  92. #endif
  93. return 0;
  94. }