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.

103 lines
2.5KB

  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. #define RAMSTART 0
  18. #define STDIO_PORT 0x00
  19. // To know which part of RAM to dump, we listen to port 2, which at the end of
  20. // its compilation process, spits its HERE addr to port 2 (MSB first)
  21. #define HERE_PORT 0x02
  22. // Port for block reads. Write 2 bytes, MSB first, on that port and then
  23. // read 1024 bytes from the DATA port.
  24. #define BLK_PORT 0x03
  25. #define BLKDATA_PORT 0x04
  26. static int running;
  27. // We support double-pokes, that is, a first poke to tell where to start the
  28. // dump and a second one to tell where to stop. If there is only one poke, it's
  29. // then ending HERE and we start at sizeof(KERNEL).
  30. static uint16_t start_here = 0;
  31. static uint16_t end_here = 0;
  32. static uint16_t blkid = 0;
  33. static unsigned int blkpos = 0;
  34. static uint8_t iord_stdio()
  35. {
  36. int c = getc(stdin);
  37. if (c == EOF) {
  38. running = 0;
  39. }
  40. return (uint8_t)c;
  41. }
  42. static void iowr_stdio(uint8_t val)
  43. {
  44. // we don't output stdout in stage0
  45. }
  46. static void iowr_here(uint8_t val)
  47. {
  48. start_here <<=8;
  49. start_here |= (end_here >> 8);
  50. end_here <<= 8;
  51. end_here |= val;
  52. }
  53. static void iowr_blk(uint8_t val)
  54. {
  55. blkid <<= 8;
  56. blkid |= val;
  57. blkpos = blkid * 1024;
  58. }
  59. static uint8_t iord_blkdata()
  60. {
  61. return BLKFS[blkpos++];
  62. }
  63. int main(int argc, char *argv[])
  64. {
  65. Machine *m = emul_init();
  66. m->ramstart = RAMSTART;
  67. m->iord[STDIO_PORT] = iord_stdio;
  68. m->iowr[STDIO_PORT] = iowr_stdio;
  69. m->iowr[HERE_PORT] = iowr_here;
  70. m->iowr[BLK_PORT] = iowr_blk;
  71. m->iord[BLKDATA_PORT] = iord_blkdata;
  72. // initialize memory
  73. for (int i=0; i<sizeof(KERNEL); i++) {
  74. m->mem[i] = KERNEL[i];
  75. }
  76. // Run!
  77. running = 1;
  78. while (running && emul_step());
  79. // We're done, now let's spit dict data
  80. for (int i=start_here; i<end_here; i++) {
  81. putchar(m->mem[i]);
  82. }
  83. emul_printdebug();
  84. return 0;
  85. }