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.

88 lines
1.6KB

  1. /* Common code between shell, zasm and runbin.
  2. They all run on the same kind of virtual machine: A z80 CPU, 64K of RAM/ROM.
  3. */
  4. #include <string.h>
  5. #include "emul.h"
  6. static Machine m;
  7. static uint8_t io_read(int unused, uint16_t addr)
  8. {
  9. addr &= 0xff;
  10. IORD fn = m.iord[addr];
  11. if (fn != NULL) {
  12. return fn();
  13. } else {
  14. fprintf(stderr, "Out of bounds I/O read: %d\n", addr);
  15. return 0;
  16. }
  17. }
  18. static void io_write(int unused, uint16_t addr, uint8_t val)
  19. {
  20. addr &= 0xff;
  21. IOWR fn = m.iowr[addr];
  22. if (fn != NULL) {
  23. fn(val);
  24. } else {
  25. fprintf(stderr, "Out of bounds I/O write: %d / %d (0x%x)\n", addr, val, val);
  26. }
  27. }
  28. static uint8_t mem_read(int unused, uint16_t addr)
  29. {
  30. return m.mem[addr];
  31. }
  32. static void mem_write(int unused, uint16_t addr, uint8_t val)
  33. {
  34. if (addr < m.ramstart) {
  35. fprintf(stderr, "Writing to ROM (%d)!\n", addr);
  36. }
  37. m.mem[addr] = val;
  38. }
  39. Machine* emul_init()
  40. {
  41. memset(m.mem, 0, 0x10000);
  42. m.ramstart = 0;
  43. m.minsp = 0xffff;
  44. for (int i=0; i<0x100; i++) {
  45. m.iord[i] = NULL;
  46. m.iowr[i] = NULL;
  47. }
  48. Z80RESET(&m.cpu);
  49. m.cpu.memRead = mem_read;
  50. m.cpu.memWrite = mem_write;
  51. m.cpu.ioRead = io_read;
  52. m.cpu.ioWrite = io_write;
  53. return &m;
  54. }
  55. bool emul_step()
  56. {
  57. if (!m.cpu.halted) {
  58. Z80Execute(&m.cpu);
  59. ushort newsp = m.cpu.R1.wr.SP;
  60. if (newsp != 0 && newsp < m.minsp) {
  61. m.minsp = newsp;
  62. }
  63. return true;
  64. } else {
  65. return false;
  66. }
  67. }
  68. void emul_loop()
  69. {
  70. while (emul_step());
  71. }
  72. void emul_printdebug()
  73. {
  74. fprintf(stderr, "Min SP: %04x\n", m.minsp);
  75. }