libElysianVMU 1.6.0
Full-featured, accurate, cross-platform library emulating the Dreamcast's Visual Memory Unit
Loading...
Searching...
No Matches
evmu_flash.h
Go to the documentation of this file.
1/*! \file
2 * \brief EvmuFlash peripheral, 8-bit FAT filesystem API
3 * \ingroup peripherals
4 *
5 * This file contains a very low-level, hardware
6 * abstraction around the VMU's flash chip. It
7 * models it at the raw byte-level for programming.
8 *
9 * \note
10 * Unless you're byte-banging your own CPU core or really
11 * want to do raw byte operations on flash, you most likely
12 * want to check out the two EvmuFlash derived types:
13 * * EvmuFat
14 * * EvmuFileManager
15 *
16 * \sa evmu_fat.h, evmu_file_manager.h
17 *
18 * \todo
19 * - Implement flash program wait cycles
20 * - Add capacity
21 *
22 * \test
23 * - Unit tests flexing truncated read/writes + verifying signals
24 *
25 * \author 2023 Falco Girgis
26 * \copyright MIT License
27*/
28
29#ifndef EVMU_FLASH_H
30#define EVMU_FLASH_H
31
32#include "../types/evmu_peripheral.h"
33#include "../types/evmu_imemory.h"
34
35/*! \name Type System
36 * \brief Type UUID and cast operators
37 * @{
38 */
39#define EVMU_FLASH_TYPE (GBL_TYPEID(EvmuFlash)) //!< Type UUID for EvmuFlash
40#define EVMU_FLASH(self) (GBL_CAST(EvmuFlash, self)) //!< Cast GblInstance to EvmuFlash
41#define EVMU_FLASH_CLASS(klass) (GBL_CLASS_CAST(EvmuFlash, klass)) //!< Cast GblClass to EvmuFlashClass
42#define EVMU_FLASH_GET_CLASS(self) (GBL_CLASSOF(EvmuFlash, self)) //!< Get EvmuFlashClass from GblInstance
43//! @}
44
45/*! \name Sizes
46 * \brief Sizes of flash and its banks
47 * @{
48 */
49#define EVMU_FLASH_BANK_SIZE 65536 //!< Size of a single flash bank
50#define EVMU_FLASH_BANKS 2 //!< Number of flash banks
51#define EVMU_FLASH_SIZE (EVMU_FLASH_BANK_SIZE * EVMU_FLASH_BANKS) //!< Total flash size in bytes
52//! @}
53
54/*! \name Programming Sequence
55 * \brief Sequence of target addresses and values for write programming
56 * @{
57 */
58#define EVMU_FLASH_PROGRAM_BYTE_COUNT 128 //!< Number of bytes software can write to flash once unlocked
59#define EVMU_FLASH_PROGRAM_STATE_0_ADDRESS 0x5555 //!< First target address for programming flash
60#define EVMU_FLASH_PROGRAM_STATE_0_VALUE 0xaa //!< First value to write when programming flash
61#define EVMU_FLASH_PROGRAM_STATE_1_ADDRESS 0x2aaa //!< Second target address for programming flash
62#define EVMU_FLASH_PROGRAM_STATE_1_VALUE 0x55 //!< Second value to write when programming flash
63#define EVMU_FLASH_PROGRAM_STATE_2_ADDRESS 0x5555 //!< Third target address for programming flash
64#define EVMU_FLASH_PROGRAM_STATE_2_VALUE 0xa0 //!< Third value to write when programming flash
65//! @}
66
67#define GBL_SELF_TYPE EvmuFlash
68
69GBL_DECLS_BEGIN
70
71GBL_DECLARE_STRUCT(EvmuFlash);
72
73//! Current state in the flash programming sequence to unlock writing
76 EVMU_FLASH_PROGRAM_STATE_1, //!< Second state
78 EVMU_FLASH_PROGRAM_STATE_COUNT //!< Number of states
79} EVMU_FLASH_PROGRAM_STATE;
80
81/*! \struct EvmuFlashClass
82 * \extends EvmuPeripheralClass
83 * \implements EvmuIMemoryClass
84 * \brief GblClass VTable structure for EvmuFlash
85 *
86 * Virtual method table for providing actual low-level
87 * flash access logic. The default implementation is
88 * constructing a byte array internally and is reading and
89 * writing to it.
90 *
91 * \note
92 * On a microcontroller, this could be subclassed and these
93 * methods could be doing actual flash chip accesses.
94 *
95 * \sa EvmuFlash
96 */
97GBL_CLASS_DERIVE_EMPTY(EvmuFlash, EvmuPeripheral, EvmuIMemory)
98
99/*! \struct EvmuFlash
100 * \extends EvmuPeripheral
101 * \implements EvmuIMemory
102 * \ingroup peripherals
103 *
104 * EvmuFlash offers the lowest, hardware-level access to
105 * the VMU's flash storage. Unless you know what you're
106 * doing, it's advised to work with a higher level API.
107 *
108 * \sa EvmuFat, EvmuFileManager
109 */
110GBL_INSTANCE_DERIVE(EvmuFlash, EvmuPeripheral)
111 //! User toggle: will be set after a flash value changes, you can reset and poll for changes
113GBL_INSTANCE_END
114
115//! \cond
116GBL_PROPERTIES(EvmuFlash,
117 (dataChanged, GBL_GENERIC, (READ, WRITE, OVERRIDE), GBL_BOOL_TYPE),
118 (programUnlocked, GBL_GENERIC, (READ, WRITE ), GBL_BOOL_TYPE),
119 (programState, GBL_GENERIC, (READ, WRITE ), GBL_ENUM_TYPE),
120 (programBytes, GBL_GENERIC, (READ, WRITE ), GBL_UINT8_TYPE),
121 (targetAddress, GBL_GENERIC, (READ, WRITE ), GBL_UINT32_TYPE)
122)
123
124GBL_SIGNALS(EvmuFlash,
125 (dataChanged, (GBL_UINT32_TYPE, baseAddress), (GBL_SIZE_TYPE, bytes), (GBL_POINTER_TYPE, value))
126)
127//! \endcond
128
129/*! \name Utilities
130 * \brief Static utility methods
131 * @{
132 */
133//! Returns the GblType UUID associated with EvmuFlash, for use with the libGimbal type system
135//! Returns the target address corresponding to the given state in the flash programming sequence
136EVMU_EXPORT EvmuAddress EvmuFlash_programAddress (EVMU_FLASH_PROGRAM_STATE state) GBL_NOEXCEPT;
137//! Returns the target value corresponding to the given state in the flash programming sequence
138EVMU_EXPORT EvmuWord EvmuFlash_programValue (EVMU_FLASH_PROGRAM_STATE state) GBL_NOEXCEPT;
139//! @}
140
141/*! \name Read Methods
142 * \brief Methods for reading or fetching state data
143 * \relatesalso EvmuFlash
144 * @{
145 */
146//! Returns the current state of the flash programming sequence for unlocking writes
147EVMU_EXPORT EVMU_FLASH_PROGRAM_STATE
148 EvmuFlash_programState (GBL_CSELF) GBL_NOEXCEPT;
149//! Returns the number of bytes remaining that can be written before reprogramming
150EVMU_EXPORT size_t EvmuFlash_programBytes (GBL_CSELF) GBL_NOEXCEPT;
151//! Returns the current target address of the next LDF or STF flash instruction
153//! Returns whether or not flash is currently unlocked for writing
154EVMU_EXPORT GblBool EvmuFlash_unlocked (GBL_CSELF) GBL_NOEXCEPT;
155//! @}
156
157/*! \name Memory Operations
158 * \brief Methods for read/write operations on flash memory
159 * \relatesalso EvmuFlash
160 * @{
161 */
162//! Reads a value from flash at the given address and returns its value
164 EvmuAddress address) GBL_NOEXCEPT;
165//! Reads the given number of bytes from flash into the buffer, returning the number successfully read
166EVMU_EXPORT EVMU_RESULT EvmuFlash_readBytes (GBL_CSELF,
167 EvmuAddress base,
168 void* pData,
169 size_t* pBytes) GBL_NOEXCEPT;
170//! Writes a value to flash at the given address (bypassing unlock sequence)
171EVMU_EXPORT EVMU_RESULT EvmuFlash_writeByte (GBL_SELF,
172 EvmuAddress address,
173 EvmuWord value) GBL_NOEXCEPT;
174//! Writes the given buffer to flash, returning nubmer of bytes written (bypassing unlock sequence)
175EVMU_EXPORT EVMU_RESULT EvmuFlash_writeBytes (GBL_SELF,
176 EvmuAddress base,
177 const void* pData,
178 size_t* pBytes) GBL_NOEXCEPT;
179//! @}
180
181GBL_DECLS_END
182
183#undef GBL_SELF_TYPE
184
185#endif // EVMU_FLASH_H
#define EVMU_EXPORT
Define used for adding attributes to export public symbols.
Definition evmu_api.h:18
GblType EvmuFlash_type(void)
Returns the GblType UUID associated with EvmuFlash, for use with the libGimbal type system.
EvmuWord EvmuFlash_programValue(EVMU_FLASH_PROGRAM_STATE state)
Returns the target value corresponding to the given state in the flash programming sequence.
EvmuAddress EvmuFlash_programAddress(EVMU_FLASH_PROGRAM_STATE state)
Returns the target address corresponding to the given state in the flash programming sequence.
EVMU_FLASH_PROGRAM_STATE
Current state in the flash programming sequence to unlock writing.
Definition evmu_flash.h:74
@ EVMU_FLASH_PROGRAM_STATE_1
Second state.
Definition evmu_flash.h:76
@ EVMU_FLASH_PROGRAM_STATE_0
First state.
Definition evmu_flash.h:75
@ EVMU_FLASH_PROGRAM_STATE_2
Third state.
Definition evmu_flash.h:77
@ EVMU_FLASH_PROGRAM_STATE_COUNT
Number of states.
Definition evmu_flash.h:78
#define EVMU_FLASH_BANKS
Number of flash banks.
Definition evmu_flash.h:50
#define EVMU_FLASH_BANK_SIZE
Size of a single flash bank.
Definition evmu_flash.h:49
uint8_t EvmuWord
Represents a single 8-bit CPU word.
uint32_t EvmuAddress
Represents a generic absolute address.
#define GBL_ENUM_TYPE
#define GBL_BOOL_TYPE
#define GBL_UINT8_TYPE
#define GBL_UINT32_TYPE
#define GBL_PROPERTIES(object,...)
uint8_t GblBool
uintptr_t GblType
EvmuFlash offers the lowest, hardware-level access to the VMU's flash storage.
Definition evmu_flash.h:110
EvmuWord EvmuFlash_readByte(const EvmuFlash *pSelf, EvmuAddress address)
Reads a value from flash at the given address and returns its value.
size_t EvmuFlash_programBytes(const EvmuFlash *pSelf)
Returns the number of bytes remaining that can be written before reprogramming.
EVMU_RESULT EvmuFlash_writeBytes(EvmuFlash *pSelf, EvmuAddress base, const void *pData, size_t *pBytes)
Writes the given buffer to flash, returning nubmer of bytes written (bypassing unlock sequence)
GblBool EvmuFlash_unlocked(const EvmuFlash *pSelf)
Returns whether or not flash is currently unlocked for writing.
EVMU_RESULT EvmuFlash_writeByte(EvmuFlash *pSelf, EvmuAddress address, EvmuWord value)
Writes a value to flash at the given address (bypassing unlock sequence)
EvmuAddress EvmuFlash_targetAddress(const EvmuFlash *pSelf)
Returns the current target address of the next LDF or STF flash instruction.
GblBool dataChanged
User toggle: will be set after a flash value changes, you can reset and poll for changes.
Definition evmu_flash.h:112
EVMU_FLASH_PROGRAM_STATE EvmuFlash_programState(const EvmuFlash *pSelf)
Returns the current state of the flash programming sequence for unlocking writes.
EVMU_RESULT EvmuFlash_readBytes(const EvmuFlash *pSelf, EvmuAddress base, void *pData, size_t *pBytes)
Reads the given number of bytes from flash into the buffer, returning the number successfully read.
#define GBL_CLASS_CAST(cType, klass)
#define GBL_CLASSOF(cType, self)
#define GBL_CAST(cType, self)