Skip to content

Commit 0924e7b

Browse files
committed
feat(bmem): add bmem_bload() and resource_bload()
1 parent 5301b92 commit 0924e7b

4 files changed

Lines changed: 143 additions & 3 deletions

File tree

include/bmem.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,66 @@ void bmem_copy_to_vmem(bmemptr_t src, vmemptr_t dst, uint32_t len);
196196
*/
197197
void bmem_bload_s(bmemptr_t src);
198198

199+
/**
200+
* Load a `BSAVE` formatted binary in banked memory into RAM.
201+
*
202+
* A `BSAVE` formatted binary must consist of a 7-byte header followed by the
203+
* data body, as follows:
204+
*
205+
* | address | contents |
206+
* |---------------|----------------------------|
207+
* | `src+0` | `0xFE` |
208+
* | `src+1` | lo-byte of `start` address |
209+
* | `src+2` | hi-byte of `start` address |
210+
* | `src+3` | lo-byte of `end` address |
211+
* | `src+4` | hi-byte of `end` address |
212+
* | `src+5` | lo-byte of `run` address |
213+
* | `src+6` | hi-byte of `run` address |
214+
* | `src+7` | `body[0]` |
215+
* | ... | ... |
216+
* | `src+7 + N-1` | `body[N-1]` |
217+
*
218+
* where `N == end - start + 1`, and `start <= end`.
219+
*
220+
* `start` and `end` are the range of addresses where the data body was, and
221+
* `run` is the address of the entry point (ignored by libmsx).
222+
*
223+
* This function copies the body of the data to the specified buffer starting
224+
* with `buf`.
225+
*
226+
* Do nothing in the following cases
227+
* - The data pointed by `src` is not a `BSAVE` formatted binary.
228+
* - Buffer size is too small.
229+
*
230+
* \param src Address of `BSAVE` foramtted binary in banked memory.
231+
* \param buf Pointer to the RAM buffer.
232+
* \param buf_size Buffer size (i.e., capacity) in bytes.
233+
*
234+
* \note
235+
* This function is supposed to copy from banked memory to page 3 of main RAM.
236+
* Thus the buffer specified by `buf` and `buf_size` must be in range of
237+
* `0xc000` to `0xfffe`. In `libmsx`, the `DATA` segment and stack areas are
238+
* placed on page 3 so this is reasonable.
239+
*
240+
* \note
241+
* In particular, page 2 is used to access banked memory so the buffer cannot be
242+
* in page 2. Page 0 is MAIN ROM, page 1 is `CODE` segment, and the library code
243+
* itself is included in `CODE` segment. So these areas cannot be specified as
244+
* the buffer.
245+
*
246+
* \note
247+
* Address `0xffff` is not memory, that is "extended slot selector" register.
248+
*
249+
* \attention
250+
* Stack areas and work areas are overridden if they intersect the buffer
251+
* specified by `buf` and `buf_size`. Unfortunately, however, the library cannot
252+
* determine the appropriate bounds. The application programmer must deal with
253+
* this.
254+
*
255+
* \sa bmem_read()
256+
*/
257+
void bmem_bload(bmemptr_t src, void * buf, size_t buf_size);
258+
199259
/** @} */
200260

201261
#endif // BMEM_H_

include/resources.h

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,12 @@ void resource_copy_to_vmem(const char * path, vmemptr_t dst);
9393
/**
9494
* Load a `BSAVE` formatted binary resource in banked memory into VRAM.
9595
*
96-
* Searches for an embedded resource in banked memory by name and loads it into
97-
* VRAM as a `BSAVE` formatted binary. If the resource is not found, or is not a
98-
* `BSAVE` formatted binary, do nothing.
96+
* Searches for embedded resources in banked memory by name and loads them into
97+
* VRAM as binaries in `BSAVE` format.
98+
*
99+
* Do nothing in the following cases
100+
* - The resource is not found,
101+
* - The resource is not a `BSAVE` formatted binary.
99102
*
100103
* This function is same as the following code:
101104
* ~~~ c
@@ -111,6 +114,33 @@ void resource_copy_to_vmem(const char * path, vmemptr_t dst);
111114
*/
112115
void resource_bload_s(const char * path);
113116

117+
/**
118+
* Load a `BSAVE` formatted binary resource in banked memory into RAM.
119+
*
120+
* Searches for embedded resources in banked memory by name and loads them into
121+
* RAM as binaries in `BSAVE` format.
122+
*
123+
* Do nothing in the following cases
124+
* - The resource is not found,
125+
* - The resource is not a `BSAVE` formatted binary.
126+
* - Buffer size is too small.
127+
*
128+
* This function is same as the following code:
129+
* ~~~ c
130+
* const ResourceIndex * res = resource_find(path);
131+
* if (res) {
132+
* bmem_bload(res->offset, buf, buf_size);
133+
* }
134+
* ~~~
135+
*
136+
* \param path path/file name of the resource.
137+
* \param buf Pointer to the RAM buffer.
138+
* \param buf_size Buffer size (i.e., capacity) in bytes.
139+
*
140+
* \sa bmem_bload_s()
141+
*/
142+
void resource_bload(const char * path, void * buf, size_t buf_size);
143+
114144
/** @} */
115145

116146
#endif // RESOURCES_H_

src/bmem_bload.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// -*- coding: utf-8-unix -*-
2+
/*
3+
* Copyright (c) 2021-2025 Daishi Mori (mori0091)
4+
*
5+
* This software is released under the MIT License.\n
6+
* See https://github.com/mori0091/libmsx/blob/main/LICENSE
7+
*
8+
* GitHub libmsx project\n
9+
* https://github.com/mori0091/libmsx
10+
*/
11+
/**
12+
* \file bmem_bload.c
13+
*/
14+
15+
#include <bmem.h>
16+
17+
void bmem_bload(bmemptr_t src, void * buf, size_t buf_size) {
18+
if (bmem_get(src) == 0xfe) {
19+
uint16_t beg = bmem_get_u16(src+1);
20+
uint16_t end = bmem_get_u16(src+3);
21+
if (beg < end) {
22+
const size_t len = end - beg + 1;
23+
if (len <= buf_size) {
24+
bmem_read(src+7, buf, len);
25+
}
26+
}
27+
}
28+
}

src/resource_bload.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// -*- coding: utf-8-unix -*-
2+
/*
3+
* Copyright (c) 2021-2025 Daishi Mori (mori0091)
4+
*
5+
* This software is released under the MIT License.\n
6+
* See https://github.com/mori0091/libmsx/blob/main/LICENSE
7+
*
8+
* GitHub libmsx project\n
9+
* https://github.com/mori0091/libmsx
10+
*/
11+
/**
12+
* \file resource_bload.c
13+
*/
14+
15+
#include <resources.h>
16+
17+
void resource_bload(const char * path, void * buf, size_t buf_size) {
18+
const ResourceIndex * res = resource_find(path);
19+
if (res) {
20+
bmem_bload(res->offset, buf, buf_size);
21+
}
22+
}

0 commit comments

Comments
 (0)