llvm-mos-sdk
neo6502.h
Go to the documentation of this file.
1 // Copyright 2024 LLVM-MOS Project
2 // Licensed under the Apache License, Version 2.0 with LLVM Exceptions.
3 // See https://github.com/llvm-mos/llvm-mos-sdk/blob/main/LICENSE for license
4 // information.
5 //
6 // Based on https://github.com/paulscottrobson/neo6502-firmware/blob/main/examples
7 
8 #include <stdint.h>
9 
10 #ifndef _NEO6502_H
11 #define _NEO6502_H
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 struct __ControlPort {
18  uint8_t command;
19  uint8_t function;
20  uint8_t error;
21  uint8_t status;
22  uint8_t params[8];
23  uint8_t reserved[4];
24 };
25 #define ControlPort (*(volatile struct __ControlPort *) 0xFF00)
26 
27 #define API_ERROR_NONE 0x00
28 #define API_STATUS_ESC 0x07
29 #define API_STATUS_ESC_MASK 0x80
30 
31 // System functions (Group 1)
32 #define API_GROUP_SYSTEM 0x01
33 #define API_FN_TIMER 0x01
34 #define API_FN_KEY_STATUS 0x02
35 #define API_FN_BASIC 0x03
36 #define API_FN_CREDITS 0x04
37 #define API_FN_SERIAL_STATUS 0x05
38 #define API_FN_LOCALE 0x06
39 #define API_FN_RESET 0x07
40 
41 // Console functions (Group 2)
42 #define API_GROUP_CONSOLE 0x02
43 #define API_FN_READ_CHAR 0x01
44 #define API_FN_CONSOLE_STATUS 0x02
45 #define API_FN_READ_LINE 0x03
46 #define API_FN_DEFINE_HOTKEY 0x04
47 #define API_FN_DEFINE_CHAR 0x05
48 #define API_FN_WRITE_CHAR 0x06
49 #define API_FN_SET_CURSOR_POS 0x07
50 #define API_FN_LIST_HOTKEYS 0x08
51 #define API_FN_SCREEN_SIZE 0x09
52 #define API_FN_INSERT_LINE 0x0A
53 #define API_FN_DELETE_LINE 0x0B
54 #define API_FN_CLEAR_SCREEN 0x0C
55 #define API_FN_CURSOR_POS 0x0D
56 #define API_FN_CLEAR_REGIION 0x0E
57 #define API_FN_SET_TEXT_COLOR 0x0F
58 #define API_FN_CURSOR_INVERSE 0x10
59 
60 // Console results (Group 2 Function 2)
61 #define API_QUEUE_EMPTY 0xFF
62 
63 // File I/O functions (Group 3)
64 #define API_GROUP_FILEIO 0x03
65 #define API_FN_LIST_DIRECTORY 0x01
66 #define API_FN_LOAD_FILENAME 0x02
67 #define API_FN_STORE_FILENAME 0x03
68 #define API_FN_FILE_OPEN 0x04
69 #define API_FN_FILE_CLOSE 0x05
70 #define API_FN_FILE_SEEK 0x06
71 #define API_FN_FILE_TELL 0x07
72 #define API_FN_FILE_READ 0x08
73 #define API_FN_FILE_WRITE 0x09
74 #define API_FN_FILE_SIZE 0x0A
75 #define API_FN_FILE_SET_SIZE 0x0B
76 #define API_FN_FILE_RENAME 0x0C
77 #define API_FN_FILE_DELETE 0x0D
78 #define API_FN_DIR_MKDIR 0x0E
79 #define API_FN_DIR_CHDIR 0x0F
80 #define API_FN_FILE_STAT 0x10
81 #define API_FN_DIR_OPEN 0x11
82 #define API_FN_DIR_READ 0x12
83 #define API_FN_DIR_CLOSE 0x13
84 #define API_FN_FILE_COPY 0x14
85 #define API_FN_FILE_SET_ATTR 0x15
86 #define API_FN_LIST_FILTERED 0x20
87 
88 // File I/O parameters (Group 3 Function 2)
89 #define API_FILE_TO_SCREEN 0xFFFF
90 
91 // Mathematics functions (Group 4)
92 #define API_GROUP_MATH 0x04
93 #define API_FN_ADD 0x00
94 #define API_FN_SUB 0x01
95 #define API_FN_MUL 0x02
96 #define API_FN_DIV_DEC 0x03
97 #define API_FN_DIV_INT 0x04
98 #define API_FN_MOD 0x05
99 #define API_FN_COMP 0x06
100 #define API_FN_NEG 0x10
101 #define API_FN_FLOOR 0x11
102 #define API_FN_SQRT 0x12
103 #define API_FN_SINE 0x13
104 #define API_FN_COS 0x14
105 #define API_FN_TAN 0x15
106 #define API_FN_ATAN 0x16
107 #define API_FN_EXP 0x17
108 #define API_FN_LOG 0x18
109 #define API_FN_ABS 0x19
110 #define API_FN_SIGN 0x1A
111 #define API_FN_RND_DEC 0x1B
112 #define API_FN_RND_INT 0x1C
113 #define API_FN_INT_TO_DEC 0x20
114 #define API_FN_STR_TO_NUM 0x21
115 #define API_FN_NUM_TO_STR 0x22
116 
117 // Graphics functions (Group 5)
118 #define API_GROUP_GRAPHICS 0x05
119 #define API_FN_SET_GFX 0x01
120 #define API_FN_DRAW_LINE 0x02
121 #define API_FN_DRAW_RECT 0x03
122 #define API_FN_DRAW_ELLIPSE 0x04
123 #define API_FN_DRAW_PIXEL 0x05
124 #define API_FN_DRAW_TEXT 0x06
125 #define API_FN_DRAW_IMG 0x07
126 #define API_FN_DRAW_TILEMAP 0x08
127 #define API_FN_SET_PALETTE 0x20
128 #define API_FN_READ_PIXEL 0x21
129 #define API_FN_RESET_PALETTE 0x22
130 #define API_FN_SET_TILEMAP 0x23
131 #define API_FN_READ_SPRITE_PXL 0x24
132 #define API_FN_FRAME_COUNT 0x25
133 #define API_FN_SET_COLOR 0x40
134 #define API_FN_SET_SOLID 0x41
135 #define API_FN_SET_DRAW_SIZE 0x42
136 #define API_FN_SET_FLIP 0x43
137 
138 // Graphics parameters (Group 5, Function 1 - Group 6, Function 2)
139 #define API_FLIP_HORZ 0x00
140 #define API_FLIP_VERT 0x01
141 
142 // Graphics results (Group 5, Functions 33,36)
143 #define API_PIXEL_TRANSPARENT 0x00
144 
145 // Sprites functions (Group 6)
146 #define API_GROUP_SPRITES 0x06
147 #define API_FN_SPRITE_RESET 0x01
148 #define API_FN_SPRITE_SET 0x02
149 #define API_FN_SPRITE_HIDE 0x03
150 #define API_FN_SPRITE_COLLISION 0x04
151 #define API_FN_SPRITE_POS 0x05
152 
153 // Sprites parameters (Group 6, Function 2)
154 #define API_SPRITE_TURTLE 0x00
155 #define API_SPRITE_32BIT 0x40
156 #define API_SPRITE_CLEAR 0x80
157 #define API_ANCHOR_BL 0x01
158 #define API_ANCHOR_B 0x02
159 #define API_ANCHOR_BR 0x03
160 #define API_ANCHOR_L 0x04
161 #define API_ANCHOR_C 0x05
162 #define API_ANCHOR_R 0x06
163 #define API_ANCHOR_TL 0x07
164 #define API_ANCHOR_T 0x08
165 #define API_ANCHOR_TR 0x09
166 
167 // Sprites results (Group 6, Function 4)
168 #define API_COLLISION_NONE 0x00
169 
170 // Controller functions (Group 7)
171 #define API_GROUP_CONTROLLER 0x07
172 #define API_FN_READ_CONTROLLER 0x01
173 
174 // Controller results (Group 7, Function 1)
175 #define API_CONTROLLER_LEFT 0x01
176 #define API_CONTROLLER_RIGHT 0x02
177 #define API_CONTROLLER_UP 0x04
178 #define API_CONTROLLER_DOWN 0x08
179 #define API_CONTROLLER_BTNA 0x10
180 #define API_CONTROLLER_BTNB 0x20
181 
182 // Sound functions (Group 8)
183 #define API_GROUP_SOUND 0x08
184 #define API_FN_RESET_SOUND 0x01
185 #define API_FN_RESET_CHANNEL 0x02
186 #define API_FN_BEEP 0x03
187 #define API_FN_QUEUE_SOUND 0x04
188 #define API_FN_PLAY_SOUND 0x05
189 #define API_FN_SOUND_STATUS 0x06
190 
191 // Sound parameters (Group 8, Functions 2,4,5)
192 #define API_SOUND_CH_00 0x00
193 
194 // Sound parameters (Group 8, Function 4)
195 #define API_NOTE_REST 0x0000
196 #define API_NOTE_C0 0x0010
197 #define API_NOTE_Cs0 0x0011
198 #define API_NOTE_Df0 0x0011
199 #define API_NOTE_D0 0x0012
200 #define API_NOTE_Ds0 0x0013
201 #define API_NOTE_Ef0 0x0013
202 #define API_NOTE_E0 0x0015
203 #define API_NOTE_F0 0x0016
204 #define API_NOTE_Fs0 0x0017
205 #define API_NOTE_Gf0 0x0017
206 #define API_NOTE_G0 0x0018
207 #define API_NOTE_Af0 0x001A
208 #define API_NOTE_Gs0 0x001A
209 #define API_NOTE_A0 0x001C
210 #define API_NOTE_As0 0x001D
211 #define API_NOTE_Bf0 0x001D
212 #define API_NOTE_B0 0x001F
213 #define API_NOTE_C1 0x0021
214 #define API_NOTE_Cs1 0x0023
215 #define API_NOTE_Df1 0x0023
216 #define API_NOTE_D1 0x0025
217 #define API_NOTE_Ds1 0x0027
218 #define API_NOTE_Ef1 0x0027
219 #define API_NOTE_E1 0x0029
220 #define API_NOTE_F1 0x002C
221 #define API_NOTE_Fs1 0x002E
222 #define API_NOTE_Gf1 0x002E
223 #define API_NOTE_G1 0x0031
224 #define API_NOTE_Af1 0x0034
225 #define API_NOTE_Gs1 0x0034
226 #define API_NOTE_A1 0x0037
227 #define API_NOTE_As1 0x003A
228 #define API_NOTE_Bf1 0x003A
229 #define API_NOTE_B1 0x003E
230 #define API_NOTE_C2 0x0041
231 #define API_NOTE_Cs2 0x0045
232 #define API_NOTE_Df2 0x0045
233 #define API_NOTE_D2 0x0049
234 #define API_NOTE_Ds2 0x004E
235 #define API_NOTE_Ef2 0x004E
236 #define API_NOTE_E2 0x0052
237 #define API_NOTE_F2 0x0057
238 #define API_NOTE_Fs2 0x005C
239 #define API_NOTE_Gf2 0x005C
240 #define API_NOTE_G2 0x0062
241 #define API_NOTE_Af2 0x0068
242 #define API_NOTE_Gs2 0x0068
243 #define API_NOTE_A2 0x006E
244 #define API_NOTE_As2 0x0075
245 #define API_NOTE_Bf2 0x0075
246 #define API_NOTE_B2 0x007B
247 #define API_NOTE_C3 0x0083
248 #define API_NOTE_Cs3 0x008B
249 #define API_NOTE_Df3 0x008B
250 #define API_NOTE_D3 0x0093
251 #define API_NOTE_Ds3 0x009C
252 #define API_NOTE_Ef3 0x009C
253 #define API_NOTE_E3 0x00A5
254 #define API_NOTE_F3 0x00AF
255 #define API_NOTE_Fs3 0x00B9
256 #define API_NOTE_Gf3 0x00B9
257 #define API_NOTE_G3 0x00C4
258 #define API_NOTE_Af3 0x00D0
259 #define API_NOTE_Gs3 0x00D0
260 #define API_NOTE_A3 0x00DC
261 #define API_NOTE_As3 0x00E9
262 #define API_NOTE_Bf3 0x00E9
263 #define API_NOTE_B3 0x00F7
264 #define API_NOTE_C4 0x0106
265 #define API_NOTE_Cs4 0x0115
266 #define API_NOTE_Df4 0x0115
267 #define API_NOTE_D4 0x0126
268 #define API_NOTE_Ds4 0x0137
269 #define API_NOTE_Ef4 0x0137
270 #define API_NOTE_E4 0x014A
271 #define API_NOTE_F4 0x015D
272 #define API_NOTE_Fs4 0x0172
273 #define API_NOTE_Gf4 0x0172
274 #define API_NOTE_G4 0x0188
275 #define API_NOTE_Af4 0x019F
276 #define API_NOTE_Gs4 0x019F
277 #define API_NOTE_A4 0x01B8
278 #define API_NOTE_As4 0x01D2
279 #define API_NOTE_Bf4 0x01D2
280 #define API_NOTE_B4 0x01EE
281 #define API_NOTE_C5 0x020B
282 #define API_NOTE_Cs5 0x022A
283 #define API_NOTE_Df5 0x022A
284 #define API_NOTE_D5 0x024B
285 #define API_NOTE_Ds5 0x026E
286 #define API_NOTE_Ef5 0x026E
287 #define API_NOTE_E5 0x0293
288 #define API_NOTE_F5 0x02BA
289 #define API_NOTE_Fs5 0x02E4
290 #define API_NOTE_Gf5 0x02E4
291 #define API_NOTE_G5 0x0310
292 #define API_NOTE_Af5 0x033F
293 #define API_NOTE_Gs5 0x033F
294 #define API_NOTE_A5 0x0370
295 #define API_NOTE_As5 0x03A4
296 #define API_NOTE_Bf5 0x03A4
297 #define API_NOTE_B5 0x03DC
298 #define API_NOTE_C6 0x0417
299 #define API_NOTE_Cs6 0x0455
300 #define API_NOTE_Df6 0x0455
301 #define API_NOTE_D6 0x0497
302 #define API_NOTE_Ds6 0x04DD
303 #define API_NOTE_Ef6 0x04DD
304 #define API_NOTE_E6 0x0527
305 #define API_NOTE_F6 0x0575
306 #define API_NOTE_Fs6 0x05C8
307 #define API_NOTE_Gf6 0x05C8
308 #define API_NOTE_G6 0x0620
309 #define API_NOTE_Af6 0x067D
310 #define API_NOTE_Gs6 0x067D
311 #define API_NOTE_A6 0x06E0
312 #define API_NOTE_As6 0x0749
313 #define API_NOTE_Bf6 0x0749
314 #define API_NOTE_B6 0x07B8
315 #define API_NOTE_C7 0x082D
316 #define API_NOTE_Cs7 0x08A9
317 #define API_NOTE_Df7 0x08A9
318 #define API_NOTE_D7 0x092D
319 #define API_NOTE_Ds7 0x09B9
320 #define API_NOTE_Ef7 0x09B9
321 #define API_NOTE_E7 0x0A4D
322 #define API_NOTE_F7 0x0AEA
323 #define API_NOTE_Fs7 0x0B90
324 #define API_NOTE_Gf7 0x0B90
325 #define API_NOTE_G7 0x0C40
326 #define API_NOTE_Af7 0x0CFA
327 #define API_NOTE_Gs7 0x0CFA
328 #define API_NOTE_A7 0x0DC0
329 #define API_NOTE_As7 0x0E91
330 #define API_NOTE_Bf7 0x0E91
331 #define API_NOTE_B7 0x0F6F
332 #define API_NOTE_C8 0x105A
333 #define API_NOTE_Cs8 0x1153
334 #define API_NOTE_Df8 0x1153
335 #define API_NOTE_D8 0x125B
336 #define API_NOTE_Ds8 0x1372
337 #define API_NOTE_Ef8 0x1372
338 #define API_NOTE_E8 0x149A
339 #define API_NOTE_F8 0x15D4
340 #define API_NOTE_Fs8 0x1720
341 #define API_NOTE_Gf8 0x1720
342 #define API_NOTE_G8 0x1880
343 #define API_NOTE_Af8 0x19F5
344 #define API_NOTE_Gs8 0x19F5
345 #define API_NOTE_A8 0x1B80
346 #define API_NOTE_As8 0x1D23
347 #define API_NOTE_Bf8 0x1D23
348 #define API_NOTE_B8 0x1EDE
349 #define API_NOTE_C9 0x20B4
350 #define API_NOTE_Cs9 0x22A6
351 #define API_NOTE_Df9 0x22A6
352 #define API_NOTE_D9 0x24B5
353 #define API_NOTE_Ds9 0x26E4
354 #define API_NOTE_Ef9 0x26E4
355 #define API_NOTE_E9 0x2934
356 #define API_NOTE_F9 0x2BA7
357 #define API_NOTE_Fs9 0x2E40
358 #define API_NOTE_Gf9 0x2E40
359 #define API_NOTE_G9 0x3100
360 #define API_NOTE_Af9 0x33EA
361 #define API_NOTE_Gs9 0x33EA
362 #define API_NOTE_A9 0x3700
363 #define API_NOTE_As9 0x3A45
364 #define API_NOTE_Bf9 0x3A45
365 #define API_NOTE_B9 0x3DBC
366 #define API_NOTE_C10 0x4168
367 #define API_NOTE_Cs10 0x454C
368 #define API_NOTE_Df10 0x454C
369 #define API_NOTE_D10 0x496B
370 #define API_NOTE_Ds10 0x4DC8
371 #define API_NOTE_Ef10 0x4DC8
372 #define API_TEMPO(bpm) (6000 / (bpm))
373 #define API_TEMPO_60 API_TEMPO(60)
374 #define API_TEMPO_80 API_TEMPO(80)
375 #define API_TEMPO_90 API_TEMPO(90)
376 #define API_TEMPO_120 API_TEMPO(120)
377 #define API_SLIDE_NONE 0x0000
378 #define API_SLIDE_SLOW 0x0004
379 #define API_SLIDE_MED 0x0008
380 #define API_SLIDE_FAST 0x0010
381 #define API_SOUND_SRC_BEEP 0x00
382 
383 // Sound parameters (Group 8, Function 5)
384 #define API_SFX_POSITIVE 0x00
385 #define API_SFX_NEGATIVE 0x01
386 #define API_SFX_ERROR 0x02
387 #define API_SFX_CONFIRM 0x03
388 #define API_SFX_REJECT 0x04
389 #define API_SFX_SWEEP 0x05
390 #define API_SFX_COIN 0x06
391 #define API_SFX_LASER_LONG 0x07
392 #define API_SFX_POWERUP 0x08
393 #define API_SFX_VICTORY 0x09
394 #define API_SFX_DEFEAT 0x0A
395 #define API_SFX_FANFARE 0x0B
396 #define API_SFX_ALARM1 0x0C
397 #define API_SFX_ALARM2 0x0D
398 #define API_SFX_ALARM3 0x0E
399 #define API_SFX_RING1 0x0F
400 #define API_SFX_RING2 0x10
401 #define API_SFX_RING3 0x11
402 #define API_SFX_DANGER 0x12
403 #define API_SFX_EXPL_LONG 0x13
404 #define API_SFX_EXPL_MEDIUM 0x14
405 #define API_SFX_EXPL_SHORT 0x15
406 #define API_SFX_LASER_MEDIUM 0x16
407 #define API_SFX_LASER_SHORT 0x17
408 
409 // Turtle Graphics functions (Group 9)
410 #define API_GROUP_TURTLE 0x09
411 #define API_FN_TURTLE_INIT 0x01
412 #define API_FN_TURTLE_TURN 0x02
413 #define API_FN_TURTLE_MOVE 0x03
414 #define API_FN_TURTLE_HIDE 0x04
415 #define API_FN_TURTLE_HOME 0x05
416 
417 // Turtle Graphics parameters (Group 9, Function 2)
418 #define API_TURTLE_LEFT 270
419 #define API_TURTLE_RIGHT 90
420 #define API_TURTLE_FLIP 180
421 
422 // Turtle Graphics parameters (Group 9, Function 3)
423 #define API_PEN_UP 0x00
424 #define API_PEN_DOWN 0x01
425 
426 // UExt functions (Group 10)
427 #define API_GROUP_UEXT 0x09
428 #define API_FN_UEXT_INIT 0x01
429 #define API_FN_GPIO_WRITE 0x02
430 #define API_FN_GPIO_READ 0x03
431 #define API_FN_SET_PORT_DIR 0x04
432 #define API_FN_I2C_WRITE 0x05
433 #define API_FN_I2C_READ 0x06
434 #define API_FN_ANALOG_READ 0x07
435 #define API_FN_I2C_STATUS 0x08
436 
437 // Colors
438 #define COLOR_BLACK 0x80
439 #define COLOR_RED 0x81
440 #define COLOR_GREEN 0x82
441 #define COLOR_YELLOW 0x83
442 #define COLOR_BLUE 0x84
443 #define COLOR_MAGENTA 0x85
444 #define COLOR_CYAN 0x86
445 #define COLOR_WHITE 0x87
446 #define COLOR_ALT_BLACK 0x88
447 #define COLOR_DARK_GREY 0x89
448 #define COLOR_DARK_GREEN 0x8A
449 #define COLOR_ORANGE 0x8B
450 #define COLOR_DARK_ORANGE 0x8C
451 #define COLOR_BROWN 0x8D
452 #define COLOR_PINK 0x8E
453 #define COLOR_LIGHT_GREY 0x8F
454 
455 #ifdef __cplusplus
456 }
457 #endif
458 
459 #endif
std::uint8_t
::uint8_t uint8_t
Definition: cstdint:21