Add Bocfel interpreter
[projects/chimara/chimara.git] / interpreters / bocfel / memory.h
1 #ifndef ZTERP_MEMORY_H
2 #define ZTERP_MEMORY_H
3
4 #include <stdint.h>
5
6 #include "util.h"
7 #include "zterp.h"
8
9 /* Story files do not have access to memory beyond 64K.  If they do
10  * something that would cause such access, wrap appropriately.  This is
11  * the approach Frotz uses (at least for @loadw/@loadb), and is endorsed
12  * by Andrew Plotkin (see http://www.intfiction.org/forum/viewtopic.php?f=38&t=2052).
13  * The standard isn’t exactly clear on the issue, and this appears to be
14  * the most sensible way to deal with the problem.
15  */
16
17 extern uint8_t *memory, *dynamic_memory;
18 extern uint32_t memory_size;
19
20 #define BYTE(addr)              (memory[addr])
21 #define STORE_BYTE(addr, val)   ((void)(memory[addr] = (val)))
22
23 static inline uint16_t WORD(uint32_t addr)
24 {
25 #ifndef ZTERP_NO_CHEAT
26   uint16_t cheat_val;
27   if(cheat_find_freezew(addr, &cheat_val)) return cheat_val;
28 #endif
29   return (memory[addr] << 8) | memory[addr + 1];
30 }
31
32 static inline void STORE_WORD(uint32_t addr, uint16_t val)
33 {
34   memory[addr + 0] = val >> 8;
35   memory[addr + 1] = val & 0xff;
36 }
37
38 static inline uint8_t user_byte(uint16_t addr)
39 {
40   ZASSERT(addr < header.static_end, "attempt to read out-of-bounds address 0x%lx", (unsigned long)addr);
41
42   return BYTE(addr);
43 }
44
45 static inline uint16_t user_word(uint16_t addr)
46 {
47   ZASSERT(addr < header.static_end - 1, "attempt to read out-of-bounds address 0x%lx", (unsigned long)addr);
48
49   return WORD(addr);
50 }
51
52 void user_store_byte(uint16_t, uint8_t);
53 void user_store_word(uint16_t, uint16_t);
54
55 #endif