Actually call on_reset callback
[lsnes.git] / include / library / portctrl-parse-asmgen.hpp
blobf697c8f7bc20357ecac58ced4b5d89f81c098618
1 #ifndef _library__portctrl_parse_asmgen__hpp__included__
2 #define _library__portctrl_parse_asmgen__hpp__included__
4 #include "assembler.hpp"
6 namespace portctrl
8 namespace codegen
10 //Emit function prologue for serialization function. This also Initializes the read position to start of
11 //serialization buffer.
13 //The serialization function takes three parameters. In order:
14 // - Dummy pointer (to be ignored).
15 // - Readonly controller state (pointer).
16 // - serialization output buffer (pointer).
18 template<class T> void emit_serialize_prologue(T& a, assembler::label_list& labels);
20 //Emit code to serialize a button.
22 //The button is emitted to current write position as specified character if set, '.' if not set. Advances write
23 //position by one byte.
25 //Parameters:
26 // - offset: The byte in controller state the button bit is in (0-based).
27 // - mask: Bitmask for the button bit.
28 // - ch: The character to use for writing pressed button.
30 template<class T> void emit_serialize_button(T& a, assembler::label_list& labels, int32_t offset, uint8_t mask,
31 uint8_t ch);
33 //Emit code to serialize an axis.
35 //Call write_axis_value() in order to serialize the axis. Advance write position by indicated number of bytes.
37 //Parameters:
38 // - offset: The low byte of axis in controller state (the high byte is at <offset>+1). This field is signed.
40 template<class T> void emit_serialize_axis(T& a, assembler::label_list& labels, int32_t offset);
42 //Emit code to write a pipe sign ('|').
44 //Emit a '|' to current write position. Advance write position by one byte.
46 template<class T> void emit_serialize_pipe(T& a, assembler::label_list& labels);
48 //Emit function epilogue for serialization function.
50 //Serialization function returns the number of bytes in serialized output. The return type is size_t.
52 template<class T> void emit_serialize_epilogue(T& a, assembler::label_list& labels);
54 //Emit function prologue for deserialization function. This also initializes the read position to start of
55 //serialization buffer.
57 //The serialization function takes three parameters. In order:
58 // - Dummy pointer (to be ignored).
59 // - controller state to be modified (pointer).
60 // - readonly serialization input buffer (pointer).
62 template<class T> void emit_deserialize_prologue(T& a, assembler::label_list& labels);
64 //Emit code to clear the controller state.
66 //Parameters:
67 // - size: The number of bytes in controller state.
68 template<class T> void emit_deserialize_clear_storage(T& a, assembler::label_list& labels, int32_t size);
70 //Emit code to deserialize button.
72 //- If the current read position has '|', jump to <next_pipe> without advancing or modifying controller state.
73 //- If the current read position has '\r', \n' or '\0', jump to <end_deserialize> without advancing or modifying
74 // controller state.
75 //- If the current read position has anything except '.' or ' ', set the corresponding bit in controller state.
76 //- If the first two cases do not apply, advance read position by one byte.
78 // Parameters:
79 // - offset: The byte offset in controller state the button bit is in (0-based).
80 // - mask: Bitmask for the button bit.
81 // - next_pipe: Label to jump to when finding '|'.
82 // - end_deserialize: Label to jump to when finding end of input.
84 template<class T> void emit_deserialize_button(T& a, assembler::label_list& labels, int32_t offset,
85 uint8_t mask, assembler::label& next_pipe, assembler::label& end_deserialize);
87 //Emit code to deserialize axis.
89 //Call read_axis_value() in order to deserialize the value. Advance read position by indicated number of bytes.
91 // Parameters:
92 // - offset: The low byte of axis in controller state (the high byte is at <offset>+1). This field is signed.
94 template<class T> void emit_deserialize_axis(T& a, assembler::label_list& labels, int32_t offset);
96 //Emit code to skip until next pipe ('|')
98 //While current read position does not contain '|', '\r', '\n' nor '\0', advance current read position.
99 //If '|' is encountered, jump to <next_pipe>. If '\r', '\n' or '\0' is encountered, jump to <deserialize_end>.
101 // Parameters:
102 // - next_pipe: Address to jump to if '|' is found.
103 // - deserialize_end: Address to jump to if '\r', '\n' or '\0' is found.
105 template<class T> void emit_deserialize_skip_until_pipe(T& a, assembler::label_list& labels,
106 assembler::label& next_pipe, assembler::label& deserialize_end);
108 //Emit code to advance read position by one byte.
110 //Advance the read position by one byte. This is used to skip '|' bytes.
112 template<class T> void emit_deserialize_skip(T& a, assembler::label_list& labels);
114 //Emit code to arrange deserialization function to return DESERIALIZE_SPECIAL_BLANK instead of byte count.
116 //This changes the return value of deserialization function to DESERIALIZE_SPECIAL_BLANK.
118 template<class T> void emit_deserialize_special_blank(T& a, assembler::label_list& labels);
120 //Emit function epilogue for serialization function.
122 //Deserialization function returns the number of bytes in serialized input (or the special value
123 //DESERIALIZE_SPECIAL_BLANK). The return type is size_t.
125 template<class T> void emit_deserialize_epilogue(T& a, assembler::label_list& labels);
127 //Emit function prologue for Read function.
129 //The read function takes four parameters. In order:
130 // - Dummy pointer (to be ignored).
131 // - controller state to be queried.
132 // - controller number (unsigned)
133 // - control index number (unsigned)
135 //Initialze pending return value to 0.
137 template<class T> void emit_read_prologue(T& a, assembler::label_list& labels);
139 //Emit function epilogue for read function.
141 //The read function returns the pending return value (short):
142 // - 0 => Invalid control, or released button.
143 // - 1 => Pressed button.
144 // - (other) => Axis value.
146 template<class T> void emit_read_epilogue(T& a, assembler::label_list& labels);
148 //Emit dispatch code for read/write function.
150 //Read dispatch table immediately after the code generated by this and jump to obtained address. The table layout
151 //is controls from low to high number, controllers from low to high number. Each controller has 2^<ilog2controls>
152 //entries.
154 // Parameters:
155 // - controllers: Number of controllers.
156 // - ilog2controls: Two's logarithm of number of controls per controller (rounded up).
157 // - end: Label pointing to code causing read/write function to return.
159 template<class T> void emit_read_dispatch(T& a, assembler::label_list& labels,
160 unsigned controllers, unsigned ilog2controls, assembler::label& end);
162 //Emit a jump pointer.
164 //Emits a jump pointer read by emit_read_dispatch() and returns reference to the pointer target.
166 // Return value:
167 // - The newly created label to code this jumps to.
169 template<class T> assembler::label& emit_read_label(T& a, assembler::label_list& labels);
171 //Emit a specfied jump pointer.
173 //Emits a jump pointer read by emit_read_dispatch() pointing to specified label.
175 // Parameters:
176 // - b: The code to jump to.
178 template<class T> void emit_read_label_bad(T& a, assembler::label_list& labels, assembler::label& b);
180 //Emit code to read button value.
182 //If the specified bit is set, set pending return value to 1. Then jump to <end>.
184 // Parameters:
185 // - l: Label for the code fragment itself (this needs to be defined).
186 // - end: Code to return from the read function.
187 // - offset: Byte offset within controller state (0-based).
188 // - mask: Bitmask for the button bit.
190 template<class T> void emit_read_button(T& a, assembler::label_list& labels, assembler::label& l,
191 assembler::label& end, int32_t offset, uint8_t mask);
193 //Emit code to read axis value.
195 //Set pending return value to value in specified location. Then jump to <end>.
197 // Parameters:
198 // - l: Label for the code fragment itself (this needs to be defined).
199 // - end: Code to return from the read function.
200 // - offset: Low byte offset in controller state (high byte is at <offset>+1). Signed.
202 template<class T> void emit_read_axis(T& a, assembler::label_list& labels, assembler::label& l,
203 assembler::label& end, int32_t offset);
205 //Emit function prologue for Write function.
207 //The write function takes five parameters. In order:
208 // - Dummy pointer (to be ignored).
209 // - controller state to be queried.
210 // - controller number (unsigned)
211 // - control index number (unsigned)
212 // - value to write (short).
214 template<class T> void emit_write_prologue(T& a, assembler::label_list& labels);
216 //Emit epilogue for write function.
218 //Write function does not return anything.
220 template<class T> void emit_write_epilogue(T& a, assembler::label_list& labels);
222 //Emit code to write button value.
224 //If the value to write is nonzero, set specified bit to 1, otherwise set it to 0. Then jump to <end>.
226 // Parameters:
227 // - l: Label for the code fragment itself (this needs to be defined).
228 // - end: Code to return from the write function.
229 // - offset: Byte offset within controller state (0-based).
230 // - mask: Bitmask for the button bit.
232 template<class T> void emit_write_button(T& a, assembler::label_list& labels, assembler::label& l,
233 assembler::label& end, int32_t offset, uint8_t mask);
235 //Emit code to write axis value.
237 //Write the value to write to specified location. Then jump to <end>.
239 // Parameters:
240 // - l: Label for the code fragment itself (this needs to be defined).
241 // - end: Code to return from the write function.
242 // - offset: Low byte offset in controller state (high byte is at <offset>+1). Signed.
244 template<class T> void emit_write_axis(T& a, assembler::label_list& labels, assembler::label& l,
245 assembler::label& end, int32_t offset);
249 #endif