| 1 | // Copyright 2010-2024 Chris Spiegel. |
| 2 | // |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | |
| 5 | #include <cstdlib> |
| 6 | #include <iomanip> |
| 7 | #include <ios> |
| 8 | #include <sstream> |
| 9 | #include <string> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include "memory.h" |
| 13 | #include "branch.h" |
| 14 | #include "meta.h" |
| 15 | #include "process.h" |
| 16 | #include "screen.h" |
| 17 | #include "types.h" |
| 18 | #include "util.h" |
| 19 | #include "zterp.h" |
| 20 | |
| 21 | // Story files do not have access to memory beyond 64K. If they do |
| 22 | // something that would cause such access, wrap appropriately. This is |
| 23 | // the approach Frotz uses (at least for @loadw/@loadb), and is endorsed |
| 24 | // by Andrew Plotkin (see http://www.intfiction.org/forum/viewtopic.php?f=38&t=2052). |
| 25 | // The standard isn’t exactly clear on the issue, and this appears to be |
| 26 | // the most sensible way to deal with the problem. |
| 27 | |
| 28 | std::vector<uint8_t> memory, dynamic_memory; |
| 29 | uint32_t memory_size; |
| 30 | |
| 31 | bool in_globals(uint16_t addr) |
| 32 | { |
| 33 | return addr >= header.globals && addr < header.globals + 480; |
| 34 | } |
| 35 | |
| 36 | bool is_global(uint16_t addr) |
| 37 | { |
| 38 | return in_globals(addr) && (addr - header.globals) % 2 == 0; |
| 39 | } |
| 40 | |
| 41 | static unsigned long addr_to_global(uint16_t addr) |
| 42 | { |
| 43 | return (addr - header.globals) / 2; |
| 44 | } |
| 45 | |
| 46 | std::string addrstring(uint16_t addr) |
| 47 | { |
| 48 | std::ostringstream ss; |
| 49 | |
| 50 | if (is_global(addr)) { |
| 51 | ss << "G" << std::setw(2) << std::setfill('0') << std::hex << addr_to_global(addr); |
| 52 | } else { |
| 53 | ss << "0x" << std::hex << addr; |
| 54 | } |
| 55 | |
| 56 | return ss.str(); |
| 57 | } |
| 58 | |
| 59 | uint8_t byte(uint32_t addr) |
| 60 | { |
| 61 | return memory[addr]; |
| 62 | } |
| 63 | |
| 64 | void store_byte(uint32_t addr, uint8_t val) |
| 65 | { |
| 66 | memory[addr] = val; |
| 67 | } |
| 68 | |
| 69 | uint8_t user_byte(uint16_t addr) |
| 70 | { |
| 71 | ZASSERT(addr < header.static_end, "attempt to read out-of-bounds address 0x%lx", static_cast<unsigned long>(addr)); |
| 72 | |
| 73 | return byte(addr); |
| 74 | } |
| 75 | |
| 76 | void user_store_byte(uint16_t addr, uint8_t v) |
| 77 | { |
| 78 | // If safety checks are off, there’s no point in checking these |
| 79 | // special cases. |
| 80 | #ifndef ZTERP_NO_SAFETY_CHECKS |
| 81 | if (addr == 0x01) { |
| 82 | ZASSERT(v == byte(addr) || (byte(addr) ^ v) == 8, "not allowed to modify any bits but 3 at 0x0001"); |
| 83 | } else if (addr == 0x10 && byte(addr) == v) { |
| 84 | // 0x10 can’t be modified, but let it slide if the story is |
| 85 | // storing the same value that’s already there. This is useful |
| 86 | // because the flags at 0x10 are stored in a word, so the story |
| 87 | // possibly could use @storew at 0x10 to modify the bits in 0x11. |
| 88 | return; |
| 89 | } else |
| 90 | #endif |
| 91 | if (addr == 0x11) { |
| 92 | ZASSERT((byte(addr) ^ v) < 8, "not allowed to modify bits 3-7 at 0x0011"); |
| 93 | |
| 94 | bool transcript_set = (v & FLAGS2_TRANSCRIPT) == FLAGS2_TRANSCRIPT; |
| 95 | if (!output_stream(transcript_set ? OSTREAM_TRANSCRIPT : -OSTREAM_TRANSCRIPT, 0)) { |
| 96 | v &= ~FLAGS2_TRANSCRIPT; |
| 97 | } |
| 98 | |
| 99 | bool fixed_set = (v & FLAGS2_FIXED) == FLAGS2_FIXED; |
| 100 | screen_set_header_bit(fixed_set); |
| 101 | } else { |
| 102 | ZASSERT(addr >= 0x40 && addr < header.static_start, "attempt to write to read-only address 0x%lx", static_cast<unsigned long>(addr)); |
| 103 | } |
| 104 | |
| 105 | store_byte(addr, v); |
| 106 | } |
| 107 | |
| 108 | uint16_t word(uint32_t addr) |
| 109 | { |
| 110 | #ifndef ZTERP_NO_CHEAT |
| 111 | uint16_t cheat_val; |
| 112 | if (cheat_find_freeze(addr, cheat_val)) { |
| 113 | return cheat_val; |
| 114 | } |
| 115 | #endif |
| 116 | return (memory[addr] << 8) | memory[addr + 1]; |
| 117 | } |
| 118 | |
| 119 | void store_word(uint32_t addr, uint16_t val) |
| 120 | { |
| 121 | #ifndef ZTERP_NO_WATCHPOINTS |
| 122 | if (addr < header.static_start - 1) { |
| 123 | watch_check(addr, word(addr), val); |
| 124 | } |
| 125 | #endif |
| 126 | |
| 127 | memory[addr + 0] = val >> 8; |
| 128 | memory[addr + 1] = val & 0xff; |
| 129 | } |
| 130 | |
| 131 | uint16_t user_word(uint16_t addr) |
| 132 | { |
| 133 | ZASSERT(addr < header.static_end - 1, "attempt to read out-of-bounds address 0x%lx", static_cast<unsigned long>(addr)); |
| 134 | |
| 135 | return word(addr); |
| 136 | } |
| 137 | |
| 138 | void user_store_word(uint16_t addr, uint16_t v) |
| 139 | { |
| 140 | #ifndef ZTERP_NO_WATCHPOINTS |
| 141 | watch_check(addr, user_word(addr), v); |
| 142 | #endif |
| 143 | |
| 144 | user_store_byte(addr + 0, v >> 8); |
| 145 | user_store_byte(addr + 1, v & 0xff); |
| 146 | } |
| 147 | |
| 148 | void zcopy_table() |
| 149 | { |
| 150 | uint16_t first = zargs[0], second = zargs[1], size = zargs[2]; |
| 151 | |
| 152 | if (second == 0) { |
| 153 | for (uint16_t i = 0; i < size; i++) { |
| 154 | user_store_byte(first + i, 0); |
| 155 | } |
| 156 | } else if (first > second || as_signed(size) < 0) { |
| 157 | long n = std::labs(as_signed(size)); |
| 158 | for (long i = 0; i < n; i++) { |
| 159 | user_store_byte(second + i, user_byte(first + i)); |
| 160 | } |
| 161 | } else { |
| 162 | for (uint16_t i = 0; i < size; i++) { |
| 163 | user_store_byte(second + size - i - 1, user_byte(first + size - i - 1)); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void zscan_table() |
| 169 | { |
| 170 | uint16_t addr = zargs[1]; |
| 171 | |
| 172 | if (znargs < 4) { |
| 173 | zargs[3] = 0x82; |
| 174 | } |
| 175 | |
| 176 | for (uint16_t i = 0; i < zargs[2]; i++) { |
| 177 | if ( |
| 178 | ((zargs[3] & 0x80) == 0x80 && (user_word(addr) == zargs[0])) || |
| 179 | ((zargs[3] & 0x80) != 0x80 && (user_byte(addr) == zargs[0])) |
| 180 | ) { |
| 181 | store(addr); |
| 182 | branch_if(true); |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | addr += zargs[3] & 0x7f; |
| 187 | } |
| 188 | |
| 189 | store(0); |
| 190 | branch_if(false); |
| 191 | } |
| 192 | |
| 193 | void zloadw() |
| 194 | { |
| 195 | store(user_word(zargs[0] + (2 * zargs[1]))); |
| 196 | } |
| 197 | |
| 198 | void zloadb() |
| 199 | { |
| 200 | store(user_byte(zargs[0] + zargs[1])); |
| 201 | } |
| 202 | |
| 203 | void zstoreb() |
| 204 | { |
| 205 | user_store_byte(zargs[0] + zargs[1], zargs[2]); |
| 206 | } |
| 207 | |
| 208 | void zstorew() |
| 209 | { |
| 210 | user_store_word(zargs[0] + (2 * zargs[1]), zargs[2]); |
| 211 | } |