libws libws
WSwan hardware library for the Wonderful toolchain
Loading...
Searching...
No Matches
memory.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Adrian "asie" Siekierka
3 *
4 * This software is provided 'as-is', without any express or implied
5 * warranty. In no event will the authors be held liable for any damages
6 * arising from the use of this software.
7 *
8 * Permission is granted to anyone to use this software for any purpose,
9 * including commercial applications, and to alter it and redistribute it
10 * freely, subject to the following restrictions:
11 *
12 * 1. The origin of this software must not be misrepresented; you must not
13 * claim that you wrote the original software. If you use this software
14 * in a product, an acknowledgment in the product documentation would be
15 * appreciated but is not required.
16 *
17 * 2. Altered source versions must be plainly marked as such, and must not be
18 * misrepresented as being the original software.
19 *
20 * 3. This notice may not be removed or altered from any source distribution.
21 */
22
26
27#ifndef LIBWS_MEMORY_H_
28#define LIBWS_MEMORY_H_
29
30#ifndef __ASSEMBLER__
31#include <stdbool.h>
32#include <stdint.h>
33#include <wonderful.h>
34#include "ports.h"
35#include "util.h"
36#endif
37
42
43#define WS_IRAM_SEGMENT 0x0000
44#define WS_SRAM_SEGMENT 0x1000
45#define WS_ROM0_SEGMENT 0x2000
46#define WS_ROM1_SEGMENT 0x3000
47
48#ifndef __ASSEMBLER__
49
55#define ws_iram __wf_iram
56
62#define ws_sram __wf_sram
63
71#define ws_rom __wf_rom
72
76#define ws_far __far
77
81#define ws_ptr_offset(x) ((uint16_t) (x))
82
86#define ws_ptr_segment(x) FP_SEG(x)
87
91#define ws_ptr_far(seg, ofs) MK_FP(seg, ofs)
92
96static inline uint32_t ws_ptr_to_linear(const void ws_far *src) {
97 return ((((uint32_t) src) >> 12) & 0xFFFF0) + ((uint16_t) ((uint32_t) src));
98}
99
103static inline const void ws_far *ws_ptr_from_linear(uint32_t src) {
104 return ws_ptr_far(src >> 4, src & 0xF);
105}
106
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)
123
124#ifdef LIBWS_USE_EXTBANK
125typedef uint16_t ws_bank_t;
126
128static inline ws_bank_t _ws_bank_save(uint8_t port, ws_bank_t new_bank) {
129 asm volatile("" ::: "memory");
130 volatile ws_bank_t old_bank = inportw(port);
131 outportw(port, new_bank);
132 asm volatile("" ::: "memory");
133 return old_bank;
134}
135
136static inline void _ws_bank_set(uint8_t port, ws_bank_t new_bank) {
137 asm volatile("" ::: "memory");
138 outportw(port, new_bank);
139 asm volatile("" ::: "memory");
140}
141
142#define _ws_bank_ram_port WS_CART_EXTBANK_RAM_PORT
143#define _ws_bank_rom0_port WS_CART_EXTBANK_ROM0_PORT
144#define _ws_bank_rom1_port WS_CART_EXTBANK_ROM1_PORT
145#define _ws_bank_roml_port WS_CART_EXTBANK_ROML_PORT
147#else
148typedef uint8_t ws_bank_t;
149
151static inline ws_bank_t _ws_bank_save(uint8_t port, ws_bank_t new_bank) {
152 asm volatile("" ::: "memory");
153 volatile ws_bank_t old_bank = inportb(port);
154 outportb(port, new_bank);
155 asm volatile("" ::: "memory");
156 return old_bank;
157}
158
159static inline void _ws_bank_set(uint8_t port, ws_bank_t new_bank) {
160 asm volatile("" ::: "memory");
161 outportb(port, new_bank);
162 asm volatile("" ::: "memory");
163}
164
165#define _ws_bank_ram_port WS_CART_BANK_RAM_PORT
166#define _ws_bank_rom0_port WS_CART_BANK_ROM0_PORT
167#define _ws_bank_rom1_port WS_CART_BANK_ROM1_PORT
168#define _ws_bank_roml_port WS_CART_BANK_ROML_PORT
170#endif
171
173#define ws_bank_within_(var, loc, prev_bank, ...) \
174 { \
175 extern const void __bank_ ## var; \
176 __attribute__((cleanup(ws_bank_ ## loc ## _cleanup_))) \
177 ws_bank_t prev_bank = ws_bank_ ## loc ## _save((unsigned int) (&__bank_ ## var)); \
178 __VA_ARGS__ \
179 }
180
181#define ws_bank_with_(var, loc, prev_bank, ...) \
182 { \
183 __attribute__((cleanup(ws_bank_ ## loc ## _cleanup_))) \
184 ws_bank_t prev_bank = ws_bank_ ## loc ## _save((unsigned int) var); \
185 __VA_ARGS__ \
186 }
188
195#define ws_bank_ram_save(new_bank) _ws_bank_save(_ws_bank_ram_port, (new_bank))
196
202#define ws_bank_ram_set(new_bank) _ws_bank_set(_ws_bank_ram_port, (new_bank))
203#define ws_bank_ram_restore ws_bank_ram_set
205static inline void ws_bank_ram_cleanup_(ws_bank_t *bank) { ws_bank_ram_restore(*bank); }
207
222#define ws_bank_within_ram(var, ...) ws_bank_within_(var, ram WF_MACRO_CONCAT(_wf_bank_, __COUNTER__), __VA_ARGS__)
223
237#define ws_bank_with_ram(bank, ...) ws_bank_with_(bank, ram, WF_MACRO_CONCAT(_wf_bank_, __COUNTER__), __VA_ARGS__)
238
245#define ws_bank_rom0_save(new_bank) _ws_bank_save(_ws_bank_rom0_port, (new_bank))
246
252#define ws_bank_rom0_set(new_bank) _ws_bank_set(_ws_bank_rom0_port, (new_bank))
253#define ws_bank_rom0_restore ws_bank_rom0_set
255static inline void ws_bank_rom0_cleanup_(ws_bank_t *bank) { ws_bank_rom0_restore(*bank); }
257
272#define ws_bank_within_rom0(var, ...) ws_bank_within_(var, rom0, WF_MACRO_CONCAT(_wf_bank_, __COUNTER__), __VA_ARGS__)
273
287#define ws_bank_with_rom0(bank, ...) ws_bank_with_(bank, rom0, WF_MACRO_CONCAT(_wf_bank_, __COUNTER__), __VA_ARGS__)
288
295#define ws_bank_rom1_save(new_bank) _ws_bank_save(_ws_bank_rom1_port, (new_bank))
296
302#define ws_bank_rom1_set(new_bank) _ws_bank_set(_ws_bank_rom1_port, (new_bank))
303#define ws_bank_rom1_restore ws_bank_rom1_set
305static inline void ws_bank_rom1_cleanup_(ws_bank_t *bank) { ws_bank_rom1_restore(*bank); }
307
322#define ws_bank_within_rom1(var, ...) ws_bank_within_(var, rom1, WF_MACRO_CONCAT(_wf_bank_, __COUNTER__), __VA_ARGS__)
323
337#define ws_bank_with_rom1(bank, ...) ws_bank_with_(bank, rom1, WF_MACRO_CONCAT(_wf_bank_, __COUNTER__), __VA_ARGS__)
338
345#define ws_bank_roml_save(new_bank) _ws_bank_save(_ws_bank_roml_port, (new_bank))
346
352#define ws_bank_roml_set(new_bank) _ws_bank_set(_ws_bank_roml_port, (new_bank))
353#define ws_bank_roml_restore ws_bank_roml_set
355static inline void ws_bank_roml_cleanup_(ws_bank_t *bank) { ws_bank_roml_restore(*bank); }
357
372#define ws_bank_within_roml(var, ...) ws_bank_within_(var, roml, WF_MACRO_CONCAT(_wf_bank_, __COUNTER__), __VA_ARGS__)
373
387#define ws_bank_with_roml(bank, ...) ws_bank_with_(bank, roml, WF_MACRO_CONCAT(_wf_bank_, __COUNTER__), __VA_ARGS__)
388
389#endif
390
392
393#endif /* LIBWS_MEMORY_H_ */
static const void ws_far * ws_ptr_from_linear(uint32_t src)
Definition memory.h:103
#define ws_far
Definition memory.h:76
#define ws_bank_ram_restore
Definition memory.h:203
uint8_t ws_bank_t
Type indicating a bank index.
Definition memory.h:148
#define ws_bank_rom0_restore
Definition memory.h:253
static uint32_t ws_ptr_to_linear(const void ws_far *src)
Definition memory.h:96
#define ws_ptr_far(seg, ofs)
Definition memory.h:91
#define ws_bank_rom1_restore
Definition memory.h:303
#define ws_bank_roml_restore
Definition memory.h:353