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