27#ifndef LIBWS_MEMORY_H_
28#define LIBWS_MEMORY_H_
43#define WS_IRAM_SEGMENT 0x0000
44#define WS_SRAM_SEGMENT 0x1000
45#define WS_ROM0_SEGMENT 0x2000
46#define WS_ROM1_SEGMENT 0x3000
55#define ws_iram __wf_iram
62#define ws_sram __wf_sram
71#define ws_rom __wf_rom
81#define ws_ptr_offset(x) ((uint16_t) (x))
86#define ws_ptr_segment(x) FP_SEG(x)
91#define ws_ptr_far(seg, ofs) MK_FP(seg, ofs)
97 return ((((uint32_t) src) >> 12) & 0xFFFF0) + ((uint16_t) ((uint32_t) src));
110#define WS_IRAM_MEM ((uint8_t ws_iram*) 0x00000000)
114#define WS_SRAM_MEM ((uint8_t ws_sram*) 0x10000000)
118#define WS_ROM0_MEM ((uint8_t __far*) 0x20000000)
122#define WS_ROM1_MEM ((uint8_t __far*) 0x30000000)
124#ifdef LIBWS_USE_EXTBANK
129 asm volatile(
"" :::
"memory");
132 asm volatile(
"" :::
"memory");
136static inline ws_bank_t _ws_bank_get(uint8_t port) {
137 asm volatile(
"" :::
"memory");
139 asm volatile(
"" :::
"memory");
143static inline void _ws_bank_set(uint8_t port,
ws_bank_t new_bank) {
144 asm volatile(
"" :::
"memory");
146 asm volatile(
"" :::
"memory");
149#define _ws_bank_ram_port WS_CART_EXTBANK_RAM_PORT
150#define _ws_bank_rom0_port WS_CART_EXTBANK_ROM0_PORT
151#define _ws_bank_rom1_port WS_CART_EXTBANK_ROM1_PORT
152#define _ws_bank_roml_port WS_CART_EXTBANK_ROML_PORT
159 asm volatile(
"" :::
"memory");
162 asm volatile(
"" :::
"memory");
166static inline ws_bank_t _ws_bank_get(uint8_t port) {
167 asm volatile(
"" :::
"memory");
169 asm volatile(
"" :::
"memory");
173static inline void _ws_bank_set(uint8_t port,
ws_bank_t new_bank) {
174 asm volatile(
"" :::
"memory");
176 asm volatile(
"" :::
"memory");
179#define _ws_bank_ram_port WS_CART_BANK_RAM_PORT
180#define _ws_bank_rom0_port WS_CART_BANK_ROM0_PORT
181#define _ws_bank_rom1_port WS_CART_BANK_ROM1_PORT
182#define _ws_bank_roml_port WS_CART_BANK_ROML_PORT
187#define ws_bank_within_(var, loc, prev_bank, ...) \
189 extern const void __bank_ ## var; \
190 __attribute__((cleanup(ws_bank_ ## loc ## _cleanup_))) \
191 ws_bank_t prev_bank = ws_bank_ ## loc ## _save((unsigned int) (&__bank_ ## var)); \
195#define ws_bank_with_(var, loc, prev_bank, ...) \
197 __attribute__((cleanup(ws_bank_ ## loc ## _cleanup_))) \
198 ws_bank_t prev_bank = ws_bank_ ## loc ## _save((unsigned int) var); \
209#define ws_bank_ram_save(new_bank) _ws_bank_save(_ws_bank_ram_port, (new_bank))
216#define ws_bank_ram_get() _ws_bank_get(_ws_bank_ram_port)
223#define ws_bank_ram_set(new_bank) _ws_bank_set(_ws_bank_ram_port, (new_bank))
224#define ws_bank_ram_restore ws_bank_ram_set
243#define ws_bank_within_ram(var, ...) ws_bank_within_(var, ram WF_MACRO_CONCAT(_wf_bank_, __COUNTER__), __VA_ARGS__)
258#define ws_bank_with_ram(bank, ...) ws_bank_with_(bank, ram, WF_MACRO_CONCAT(_wf_bank_, __COUNTER__), __VA_ARGS__)
265#define ws_bank_rom0_get() _ws_bank_get(_ws_bank_rom0_port)
273#define ws_bank_rom0_save(new_bank) _ws_bank_save(_ws_bank_rom0_port, (new_bank))
280#define ws_bank_rom0_set(new_bank) _ws_bank_set(_ws_bank_rom0_port, (new_bank))
281#define ws_bank_rom0_restore ws_bank_rom0_set
300#define ws_bank_within_rom0(var, ...) ws_bank_within_(var, rom0, WF_MACRO_CONCAT(_wf_bank_, __COUNTER__), __VA_ARGS__)
315#define ws_bank_with_rom0(bank, ...) ws_bank_with_(bank, rom0, WF_MACRO_CONCAT(_wf_bank_, __COUNTER__), __VA_ARGS__)
323#define ws_bank_rom1_save(new_bank) _ws_bank_save(_ws_bank_rom1_port, (new_bank))
330#define ws_bank_rom1_get() _ws_bank_get(_ws_bank_rom1_port)
337#define ws_bank_rom1_set(new_bank) _ws_bank_set(_ws_bank_rom1_port, (new_bank))
338#define ws_bank_rom1_restore ws_bank_rom1_set
357#define ws_bank_within_rom1(var, ...) ws_bank_within_(var, rom1, WF_MACRO_CONCAT(_wf_bank_, __COUNTER__), __VA_ARGS__)
372#define ws_bank_with_rom1(bank, ...) ws_bank_with_(bank, rom1, WF_MACRO_CONCAT(_wf_bank_, __COUNTER__), __VA_ARGS__)
380#define ws_bank_roml_save(new_bank) _ws_bank_save(_ws_bank_roml_port, (new_bank))
387#define ws_bank_roml_get() _ws_bank_get(_ws_bank_roml_port)
394#define ws_bank_roml_set(new_bank) _ws_bank_set(_ws_bank_roml_port, (new_bank))
395#define ws_bank_roml_restore ws_bank_roml_set
414#define ws_bank_within_roml(var, ...) ws_bank_within_(var, roml, WF_MACRO_CONCAT(_wf_bank_, __COUNTER__), __VA_ARGS__)
429#define ws_bank_with_roml(bank, ...) ws_bank_with_(bank, roml, WF_MACRO_CONCAT(_wf_bank_, __COUNTER__), __VA_ARGS__)
434#define ws_bank_with_flash_(var, prev_bank, ...) \
436 __attribute__((cleanup(ws_bank_flash_cleanup_))) \
437 volatile uint8_t prev_bank = inportb(WS_CART_BANK_FLASH_PORT); \
438 outportb(WS_CART_BANK_FLASH_PORT, var); \
449#define ws_bank_with_flash(val, ...) ws_bank_with_flash_(val, WF_MACRO_CONCAT(_wf_bank_, __COUNTER__), __VA_ARGS__)
static void outportw(uint8_t port, uint16_t value)
Write a word to the given port.
static void outportb(uint8_t port, uint8_t value)
Write a byte to the given port.
static uint8_t inportb(uint8_t port)
Read a byte from the given port.
static uint16_t inportw(uint8_t port)
Read a word from the given port.
#define WS_CART_BANK_FLASH_PORT
static const void ws_far * ws_ptr_from_linear(uint32_t src)
#define ws_bank_ram_restore
uint8_t ws_bank_t
Type indicating a bank index.
#define ws_bank_rom0_restore
static uint32_t ws_ptr_to_linear(const void ws_far *src)
#define ws_ptr_far(seg, ofs)
#define ws_bank_rom1_restore
#define ws_bank_roml_restore