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.

82 lines
1.7KB

  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include "libz80/z80.h"
  4. #include "kernel.h"
  5. #include "zasm.h"
  6. /* zasm is a "pure memory" application. It starts up being told memory location
  7. * to read and memory location to write.
  8. *
  9. * This program works be writing stdin in a specific location in memory, run
  10. * zasm in a special wrapper, wait until we receive the stop signal, then
  11. * spit the contents of the dest memory to stdout.
  12. */
  13. // in sync with glue.asm
  14. #define READFROM 0xa000
  15. #define WRITETO 0xd000
  16. #define ZASM_CODE_OFFSET 0x8000
  17. static Z80Context cpu;
  18. static uint8_t mem[0xffff];
  19. static int running;
  20. // Number of bytes written to WRITETO
  21. // We receive that result from an OUT (C), A call. C contains LSB, A is MSB.
  22. static uint16_t written;
  23. static uint8_t io_read(int unused, uint16_t addr)
  24. {
  25. return 0;
  26. }
  27. static void io_write(int unused, uint16_t addr, uint8_t val)
  28. {
  29. written = (val << 8) + (addr & 0xff);
  30. running = 0;
  31. }
  32. static uint8_t mem_read(int unused, uint16_t addr)
  33. {
  34. return mem[addr];
  35. }
  36. static void mem_write(int unused, uint16_t addr, uint8_t val)
  37. {
  38. mem[addr] = val;
  39. }
  40. int main()
  41. {
  42. // initialize memory
  43. for (int i=0; i<sizeof(KERNEL); i++) {
  44. mem[i] = KERNEL[i];
  45. }
  46. for (int i=0; i<sizeof(ZASM); i++) {
  47. mem[i+ZASM_CODE_OFFSET] = ZASM[i];
  48. }
  49. int ptr = READFROM;
  50. int c = getchar();
  51. while (c != EOF) {
  52. mem[ptr] = c;
  53. ptr++;
  54. c = getchar();
  55. }
  56. // Run!
  57. running = 1;
  58. Z80RESET(&cpu);
  59. cpu.ioRead = io_read;
  60. cpu.ioWrite = io_write;
  61. cpu.memRead = mem_read;
  62. cpu.memWrite = mem_write;
  63. while (running) {
  64. Z80Execute(&cpu);
  65. }
  66. for (int i=0; i<written; i++) {
  67. putchar(mem[WRITETO+i]);
  68. }
  69. return 0;
  70. }