llvm-mos-sdk
rp6502.h
Go to the documentation of this file.
1 #ifndef _RP6502_H
2 #define _RP6502_H
3 
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7 
8 /* RP6502 RIA $FFE0-$FFF9 */
9 struct __RP6502 {
10  const unsigned char ready;
11  unsigned char tx;
12  const unsigned char rx;
13  const unsigned char vsync;
14  unsigned char rw0;
15  unsigned char step0;
16  unsigned int addr0;
17  unsigned char rw1;
18  unsigned char step1;
19  unsigned int addr1;
20  unsigned char xstack;
21  unsigned int errno;
22  unsigned char op;
23  unsigned char irq;
24  const unsigned char spin;
25  const unsigned char busy;
26  const unsigned char lda;
27  unsigned char a;
28  const unsigned char ldx;
29  unsigned char x;
30  const unsigned char rts;
31  unsigned int sreg;
32 };
33 #define RIA (*(volatile struct __RP6502 *)0xFFE0)
34 
35 #define RIA_READY_TX_BIT 0x80
36 #define RIA_READY_RX_BIT 0x40
37 #define RIA_BUSY_BIT 0x80
38 
39 /* XSTACK helpers */
40 
41 void ria_push_long(unsigned long val);
42 void ria_push_int(unsigned int val);
43 #define ria_push_char(v) RIA.xstack = v
44 
45 long ria_pop_long(void);
46 int ria_pop_int(void);
47 #define ria_pop_char() RIA.xstack
48 
49 /* Set the RIA fastcall register */
50 
51 void ria_set_axsreg(unsigned long axsreg);
52 void ria_set_ax(unsigned int ax);
53 #define ria_set_a(v) RIA.a = v
54 
55 /* Run an OS operation */
56 
57 int ria_call_int(unsigned char op);
58 long ria_call_long(unsigned char op);
59 
60 /* These run _mappederrno() on error */
61 
62 int ria_call_int_errno(unsigned char op);
63 long ria_call_long_errno(unsigned char op);
64 
65 /* OS operation numbers */
66 
67 #define RIA_OP_EXIT 0xFF
68 #define RIA_OP_ZXSTACK 0x00
69 #define RIA_OP_XREG 0x01
70 #define RIA_OP_PHI2 0x02
71 #define RIA_OP_CODEPAGE 0x03
72 #define RIA_OP_LRAND 0x04
73 #define RIA_OP_STDIN_OPT 0x05
74 #define RIA_OP_CLOCK 0x0F
75 #define RIA_OP_CLOCK_GETRES 0x10
76 #define RIA_OP_CLOCK_GETTIME 0x11
77 #define RIA_OP_CLOCK_SETTIME 0x12
78 #define RIA_OP_CLOCK_GETTIMEZONE 0x13
79 #define RIA_OP_OPEN 0x14
80 #define RIA_OP_CLOSE 0x15
81 #define RIA_OP_READ_XSTACK 0x16
82 #define RIA_OP_READ_XRAM 0x17
83 #define RIA_OP_WRITE_XSTACK 0x18
84 #define RIA_OP_WRITE_XRAM 0x19
85 #define RIA_OP_LSEEK 0x1A
86 #define RIA_OP_UNLINK 0x1B
87 #define RIA_OP_RENAME 0x1C
88 
89 /* RIA constants for direct lseek calls. */
90 /* Note: Use constants from C library when calling C library. */
91 
92 #define RIA_SEEK_CUR 0
93 #define RIA_SEEK_END 1
94 #define RIA_SEEK_SET 2
95 
96 /* C API for the operating system. */
97 
98 int xregn(char device, char channel, unsigned char address, unsigned count,
99  ...);
100 int phi2(void);
101 int codepage(void);
102 long lrand(void);
103 int stdin_opt(unsigned long ctrl_bits, unsigned char str_length);
104 int read_xstack(void *buf, unsigned count, int fildes);
105 int read_xram(unsigned buf, unsigned count, int fildes);
106 int write_xstack(const void *buf, unsigned count, int fildes);
107 int write_xram(unsigned buf, unsigned count, int fildes);
108 
109 /* XRAM structure helpers */
110 
111 #define xram0_struct_set(addr, type, member, val) \
112  RIA.addr0 = (unsigned)(&((type *)0)->member) + (unsigned)addr; \
113  switch (sizeof(((type *)0)->member)) { \
114  case 1: \
115  RIA.rw0 = val; \
116  break; \
117  case 2: \
118  RIA.step0 = 1; \
119  RIA.rw0 = val & 0xff; \
120  RIA.rw0 = (val >> 8) & 0xff; \
121  break; \
122  case 4: \
123  RIA.step0 = 1; \
124  RIA.rw0 = (unsigned long)val & 0xff; \
125  RIA.rw0 = ((unsigned long)val >> 8) & 0xff; \
126  RIA.rw0 = ((unsigned long)val >> 16) & 0xff; \
127  RIA.rw0 = ((unsigned long)val >> 24) & 0xff; \
128  break; \
129  }
130 
131 #define xram1_struct_set(addr, type, member, val) \
132  RIA.addr1 = (unsigned)(&((type *)0)->member) + (unsigned)addr; \
133  switch (sizeof(((type *)0)->member)) { \
134  case 1: \
135  RIA.rw1 = val; \
136  break; \
137  case 2: \
138  RIA.step1 = 1; \
139  RIA.rw1 = val & 0xff; \
140  RIA.rw1 = (val >> 8) & 0xff; \
141  break; \
142  case 4: \
143  RIA.step1 = 1; \
144  RIA.rw1 = (unsigned long)val & 0xff; \
145  RIA.rw1 = ((unsigned long)val >> 8) & 0xff; \
146  RIA.rw1 = ((unsigned long)val >> 16) & 0xff; \
147  RIA.rw1 = ((unsigned long)val >> 24) & 0xff; \
148  break; \
149  }
150 
151 typedef struct {
152  unsigned char x_wrap; // bool
153  unsigned char y_wrap; // bool
154  int x_pos_px;
155  int y_pos_px;
158  unsigned xram_data_ptr;
160  unsigned xram_font_ptr;
162 
163 typedef struct {
164  unsigned char x_wrap; // bool
165  unsigned char y_wrap; // bool
166  int x_pos_px;
167  int y_pos_px;
170  unsigned xram_data_ptr;
172  unsigned xram_tile_ptr;
174 
175 typedef struct {
176  unsigned char x_wrap; // bool
177  unsigned char y_wrap; // bool
178  int x_pos_px;
179  int y_pos_px;
180  int width_px;
182  unsigned xram_data_ptr;
185 
186 typedef struct {
187  int x_pos_px;
188  int y_pos_px;
189  unsigned xram_sprite_ptr;
190  unsigned char log_size;
191  unsigned char has_opacity_metadata; // bool
193 
194 typedef struct {
195  int transform[6];
196  int x_pos_px;
197  int y_pos_px;
198  unsigned xram_sprite_ptr;
199  unsigned char log_size;
200  unsigned char has_opacity_metadata; // bool
202 
203 /* Values in __oserror are the union of these FatFs errors and errno.h */
204 
205 typedef enum {
206  FR_OK = 32, /* Succeeded */
207  FR_DISK_ERR, /* A hard error occurred in the low level disk I/O layer */
208  FR_INT_ERR, /* Assertion failed */
209  FR_NOT_READY, /* The physical drive cannot work */
210  FR_NO_FILE, /* Could not find the file */
211  FR_NO_PATH, /* Could not find the path */
212  FR_INVALID_NAME, /* The path name format is invalid */
213  FR_DENIED, /* Access denied due to prohibited access or directory full */
214  FR_EXIST, /* Access denied due to prohibited access */
215  FR_INVALID_OBJECT, /* The file/directory object is invalid */
216  FR_WRITE_PROTECTED, /* The physical drive is write protected */
217  FR_INVALID_DRIVE, /* The logical drive number is invalid */
218  FR_NOT_ENABLED, /* The volume has no work area */
219  FR_NO_FILESYSTEM, /* There is no valid FAT volume */
220  FR_MKFS_ABORTED, /* The f_mkfs() aborted due to any problem */
221  FR_TIMEOUT, /* Could not get a grant to access the volume within defined
222  period */
223  FR_LOCKED, /* The operation is rejected according to the file sharing policy
224  */
225  FR_NOT_ENOUGH_CORE, /* LFN working buffer could not be allocated */
226  FR_TOO_MANY_OPEN_FILES, /* Number of open files > FF_FS_LOCK */
227  FR_INVALID_PARAMETER /* Given parameter is invalid */
229 
230 #ifdef __cplusplus
231 }
232 #endif
233 
234 #endif /* _RP6502_H */
uint8_t uint16_t address
Definition: bios.h:180
byte x
Definition: api.h:26
#define errno
Definition: errno.h:9
char channel
Definition: famitone2.h:31
const void uint16_t count
Definition: memory.h:58
int write_xstack(const void *buf, unsigned count, int fildes)
int xregn(char device, char channel, unsigned char address, unsigned count,...)
FRESULT
Definition: rp6502.h:205
@ FR_NO_FILESYSTEM
Definition: rp6502.h:219
@ FR_EXIST
Definition: rp6502.h:214
@ FR_INVALID_PARAMETER
Definition: rp6502.h:227
@ FR_INVALID_OBJECT
Definition: rp6502.h:215
@ FR_TIMEOUT
Definition: rp6502.h:221
@ FR_INVALID_DRIVE
Definition: rp6502.h:217
@ FR_MKFS_ABORTED
Definition: rp6502.h:220
@ FR_TOO_MANY_OPEN_FILES
Definition: rp6502.h:226
@ FR_OK
Definition: rp6502.h:206
@ FR_LOCKED
Definition: rp6502.h:223
@ FR_INVALID_NAME
Definition: rp6502.h:212
@ FR_DENIED
Definition: rp6502.h:213
@ FR_NO_FILE
Definition: rp6502.h:210
@ FR_DISK_ERR
Definition: rp6502.h:207
@ FR_INT_ERR
Definition: rp6502.h:208
@ FR_WRITE_PROTECTED
Definition: rp6502.h:216
@ FR_NOT_READY
Definition: rp6502.h:209
@ FR_NO_PATH
Definition: rp6502.h:211
@ FR_NOT_ENOUGH_CORE
Definition: rp6502.h:225
@ FR_NOT_ENABLED
Definition: rp6502.h:218
int stdin_opt(unsigned long ctrl_bits, unsigned char str_length)
int ria_call_int_errno(unsigned char op)
int ria_call_int(unsigned char op)
int read_xram(unsigned buf, unsigned count, int fildes)
int ria_pop_int(void)
long lrand(void)
void ria_push_long(unsigned long val)
long ria_call_long(unsigned char op)
int codepage(void)
int write_xram(unsigned buf, unsigned count, int fildes)
int phi2(void)
void ria_push_int(unsigned int val)
int read_xstack(void *buf, unsigned count, int fildes)
void ria_set_axsreg(unsigned long axsreg)
long ria_pop_long(void)
void ria_set_ax(unsigned int ax)
long ria_call_long_errno(unsigned char op)
Definition: rp6502.h:151
unsigned xram_data_ptr
Definition: rp6502.h:158
int width_chars
Definition: rp6502.h:156
int x_pos_px
Definition: rp6502.h:154
unsigned char y_wrap
Definition: rp6502.h:153
unsigned char x_wrap
Definition: rp6502.h:152
unsigned xram_palette_ptr
Definition: rp6502.h:159
unsigned xram_font_ptr
Definition: rp6502.h:160
int y_pos_px
Definition: rp6502.h:155
int height_chars
Definition: rp6502.h:157
Definition: rp6502.h:163
int x_pos_px
Definition: rp6502.h:166
int y_pos_px
Definition: rp6502.h:167
unsigned xram_tile_ptr
Definition: rp6502.h:172
unsigned xram_palette_ptr
Definition: rp6502.h:171
unsigned xram_data_ptr
Definition: rp6502.h:170
unsigned char y_wrap
Definition: rp6502.h:165
int height_tiles
Definition: rp6502.h:169
unsigned char x_wrap
Definition: rp6502.h:164
int width_tiles
Definition: rp6502.h:168
Definition: rp6502.h:175
int height_px
Definition: rp6502.h:181
unsigned xram_data_ptr
Definition: rp6502.h:182
int y_pos_px
Definition: rp6502.h:179
unsigned char x_wrap
Definition: rp6502.h:176
int x_pos_px
Definition: rp6502.h:178
unsigned xram_palette_ptr
Definition: rp6502.h:183
int width_px
Definition: rp6502.h:180
unsigned char y_wrap
Definition: rp6502.h:177
Definition: rp6502.h:194
unsigned char has_opacity_metadata
Definition: rp6502.h:200
unsigned char log_size
Definition: rp6502.h:199
int y_pos_px
Definition: rp6502.h:197
int x_pos_px
Definition: rp6502.h:196
unsigned xram_sprite_ptr
Definition: rp6502.h:198
Definition: rp6502.h:186
unsigned char has_opacity_metadata
Definition: rp6502.h:191
int x_pos_px
Definition: rp6502.h:187
unsigned char log_size
Definition: rp6502.h:190
unsigned xram_sprite_ptr
Definition: rp6502.h:189
int y_pos_px
Definition: rp6502.h:188