abcmem.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "common.h"
  2. #include "io.h"
  3. #include "abcio.h"
  4. #include "sys.h"
  5. #include "console.h"
  6. #include "config.h"
  7. #include "roms.h"
  8. /* Configure ABC memory map */
  9. struct rom {
  10. const char *data;
  11. size_t len;
  12. const char *name;
  13. };
  14. struct abc_mem_init {
  15. int addr;
  16. uint32_t flags;
  17. enum sysvar_enum enable;
  18. struct rom r;
  19. };
  20. /* Convenience aliases */
  21. #define MAPS ABCMEMMAP_MAPS
  22. #define PAGE_SHIFT ABCMEMMAP_PAGE_SHIFT
  23. #define PAGE_SIZE ABCMEMMAP_PAGE_SIZE
  24. #define PAGE_MASK ABCMEMMAP_PAGE_MASK
  25. #define PAGE_COUNT ABCMEMMAP_PAGE_COUNT
  26. #define PERMAP (PAGE_COUNT / MAPS)
  27. #define RD ABCMEMMAP_RD
  28. #define WR ABCMEMMAP_WR
  29. #define XM ABCMEMMAP_XM
  30. #define K 1024
  31. #define R(x) { (x), sizeof(x), #x }
  32. #define END { -1, 0, sysvar_null, { NULL, 0, NULL } }
  33. /* ---- ABC80 memory configurations ---- */
  34. /* Not really NV, but matches NVRAM in some expansions */
  35. static char __dram_bss __aligned(PAGE_SIZE) abc80_nvram[2][2*K];
  36. /* 16K external memory to expand to 512K */
  37. static char __dram_bss __aligned(PAGE_SIZE) abc80_extmem[16*K];
  38. static const struct abc_mem_init mem_init_abc80[] = {
  39. /* Put these here in case someone wants to enable both */
  40. { 20*K, RD|WR, config_abc_mem_abc80_nvram_20k, R(abc80_nvram[0]) },
  41. { 22*K, RD|WR, config_abc_mem_abc80_nvram_22k, R(abc80_nvram[1]) },
  42. { 16*K, RD, config_abc_mem_abc80_smartaid3, R(rom_abc80_smartaid3) },
  43. { 16*K, RD, config_abc_mem_abc80_superbasic, R(rom_abc80_superbasic16k) },
  44. { 20*K, RD, config_abc_mem_abc80_abc80l, R(rom_abc80_abc80l) },
  45. { 28*K, RD, config_abc_mem_abc80_superbasic, R(rom_abc80_superbasic28k) },
  46. { 32*K, RD|WR, config_abc_mem_abc80_ram, R(abc80_extmem) },
  47. END
  48. };
  49. static const struct abc_mem_init mem_init_abc80_nvram_20k[] = {
  50. { 24*K, RD, config_abc_mem_abc80_ufddos, R(rom_abc80_nvram_20k_ufddos80) },
  51. { 28*K, RD, config_abc_mem_abc80_pun80_28k, R(rom_abc80_nvram_20k_print80_28) },
  52. { 29*K, RD, config_abc_mem_abc80_pun80_29k, R(rom_abc80_nvram_20k_print80_29) },
  53. { 30*K, RD, config_abc_mem_abc80_pun80_30k, R(rom_abc80_nvram_20k_print80_30) },
  54. END
  55. };
  56. static const struct abc_mem_init mem_init_abc80_nvram_22k[] = {
  57. { 24*K, RD, config_abc_mem_abc80_ufddos, R(rom_abc80_nvram_22k_ufddos80) },
  58. { 28*K, RD, config_abc_mem_abc80_pun80_28k, R(rom_abc80_nvram_22k_print80_28) },
  59. { 29*K, RD, config_abc_mem_abc80_pun80_29k, R(rom_abc80_nvram_22k_print80_29) },
  60. { 30*K, RD, config_abc_mem_abc80_pun80_30k, R(rom_abc80_nvram_22k_print80_30) },
  61. END
  62. };
  63. static const struct abc_mem_init mem_init_abc80_no_nvram[] = {
  64. { 24*K, RD, config_abc_mem_abc80_ufddos, R(rom_abc80_no_nvram_ufddos80) },
  65. { 28*K, RD, config_abc_mem_abc80_pun80_28k, R(rom_abc80_no_nvram_print80_28) },
  66. { 29*K, RD, config_abc_mem_abc80_pun80_29k, R(rom_abc80_no_nvram_print80_29) },
  67. { 30*K, RD, config_abc_mem_abc80_pun80_30k, R(rom_abc80_no_nvram_print80_30) },
  68. END
  69. };
  70. /* ---- ABC800 memory configurations ---- */
  71. static const struct abc_mem_init mem_init_abc800[] = {
  72. { 24*K, RD, config_abc_mem_abc800_ufddos, R(rom_abc800_ufddos) },
  73. END
  74. };
  75. static void abc_map_list(uint32_t *memmap, const struct abc_mem_init *mem)
  76. {
  77. while (mem->addr >= 0) {
  78. bool enabled = !mem->enable || getvar_bool(mem->enable);
  79. const void *data = mem->r.data;
  80. size_t len = mem->r.len;
  81. if (!len)
  82. continue; /* Empty range, skip */
  83. uint32_t addr = mem->addr;
  84. bool bad = ((addr | len) & ~PAGE_MASK) ||
  85. (len > 0x10000 - addr) ||
  86. ((size_t)data & (PAGE_SIZE-1));
  87. con_printf("abcmem: %s %s @ 0x%04x len 0x%04x\n",
  88. mem->r.name ? mem->r.name : "(none)",
  89. bad ? "invalid" :
  90. !enabled ? "disabled" :
  91. data ? "mapping" : "unmapping",
  92. addr, len);
  93. if (enabled) {
  94. uint32_t *pg = &memmap[addr >> PAGE_SHIFT];
  95. uint32_t flags = mem->flags;
  96. if (data) {
  97. /* Mapped range */
  98. size_t pgv = (size_t)data | flags;
  99. for (uint32_t bytes = 0; bytes < len; bytes += PAGE_SIZE)
  100. *pg++ = pgv + bytes;
  101. } else {
  102. /* Unmapped range - set to 0 for clarity */
  103. memset(pg, 0, len >> (PAGE_SHIFT-2));
  104. }
  105. }
  106. mem++;
  107. }
  108. }
  109. void __cold abc_init_memmap(void)
  110. {
  111. static uint32_t memmap[MAPS][PERMAP]; /* Too big to fit on stack */
  112. const struct abc_mem_init *mem;
  113. memset(memmap, 0, sizeof memmap);
  114. /*
  115. * Currently the only map that gets populated is map 0; the
  116. * remaining map(s) get cleared by the memset() above.
  117. */
  118. if (is_abc800()) {
  119. abc_map_list(memmap[0], mem_init_abc800);
  120. } else {
  121. /* Put 22K first so UFD-DOS/PUN80 memory always is put at the end */
  122. if (getvar_bool(config_abc_mem_abc80_nvram_22k))
  123. abc_map_list(memmap[0], mem_init_abc80_nvram_22k);
  124. else if (getvar_bool(config_abc_mem_abc80_nvram_20k))
  125. abc_map_list(memmap[0], mem_init_abc80_nvram_20k);
  126. else
  127. abc_map_list(memmap[0], mem_init_abc80_no_nvram);
  128. abc_map_list(memmap[0], mem_init_abc80);
  129. }
  130. /* Install memory map into hardware registers */
  131. memcpy((void *)&ABCMEMMAP_PAGE(0), memmap, sizeof memmap);
  132. }