BBC Micro C library
os.h
Go to the documentation of this file.
1 /*
2 Copyright 2020 Peter Howkins
3 
4 Permission is hereby granted, free of charge, to any person obtaining a
5 copy of this software and associated documentation files (the
6 "Software"), to deal in the Software without restriction, including
7 without limitation the rights to use, copy, modify, merge, publish,
8 distribute, sublicense, and/or sell copies of the Software, and to
9 permit persons to whom the Software is furnished to do so, subject to
10 the following conditions:
11 
12 The above copyright notice and this permission notice shall be included
13 in all copies or substantial portions of the Software.
14 
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 */
23 #ifndef __OS_H__
24 #define __OS_H__
25 
33 #include <string.h>
34 #include "stdint.h"
35 
36 
42 static inline void
43 oswrch(uint8_t a_char)
44 {
45  __asm__ __volatile__("jsr $ffee" : : "Aq" (a_char));
46 }
47 
53 static inline uint8_t
54 osrdch(void)
55 {
56  uint8_t rega = 0;
57 
58  __asm__ __volatile__("jsr $ffe0" : "+Aq" (rega) );
59 
60  return rega;
61 }
62 
66 static inline void
67 osnewl(void)
68 {
69  __asm__ __volatile__("jsr $ffe7" : : : "a");
70 }
71 
81 static inline void
82 osasci(uint8_t a_char)
83 {
84  __asm__ __volatile__("jsr $ffe3" : : "Aq" (a_char));
85 }
86 
92 static inline void
94 {
95 // TODO
96 }
97 
98 
104 static inline void
106 {
107 // TODO
108 }
109 
119 extern uint8_t
120 osrdrm(uint8_t y_romnum, uintptr_t address);
121 
133 static inline uint8_t
135 {
136 // UNTESTED
137  return osrdrm(0, address);
138 }
139 
146 extern void
147 oswrsc(uint8_t a_byte, uintptr_t address);
148 
152 static inline void
153 oseven(uint8_t y_eventnum)
154 {
155 // TODO
156 }
157 
163 static inline void
164 oscli(char *xy_command)
165 {
166  uint8_t regx = ((uintptr_t) xy_command) & 0xff;
167  uint8_t regy = (((uintptr_t) xy_command) & 0xff00) >> 8;
168 
169  /* convert from C zero terminator to BBC OS terminator */
170  xy_command[strlen(xy_command)] = 0x0d;
171 
172  __asm__ __volatile__("jsr $fff7" : : "xq"(regx), "yq"(regy) );
173 }
174 
183 static inline void
184 osbyte(uint8_t a_func, uint8_t *x, uint8_t *y)
185 {
186  uint8_t regx = *x;
187  uint8_t regy = *y;
188 
189  __asm__ __volatile__("jsr $fff4" : "+xq"(regx), "+yq"(regy) : "Aq" (a_func));
190  *x = regx;
191  *y = regy;
192 }
193 
201 extern void
202 osword(uint8_t a_func, const void *xy_pointer);
203 
208 {
215 
219 typedef enum osfile_action_e
220 {
221  osfile_saveblock = 0,
222  osfile_writeinfo = 1,
223  osfile_writeload = 2,
224  osfile_writeexec = 3,
225  osfile_writeattr = 4,
226  osfile_readinfo = 5,
227  osfile_deletenamedfile = 6,
228  osfile_loadnamedfile = 0xff,
229 } osfile_action;
230 
237 extern void
238 osfile(uint8_t a_action, const osfile_parameterblock *xy_pblock);
239 
247 // TODO requires zero page access, how much does the C program mess with that?
248 // void osargs(uint8_t *a_action, uint8_t x_zp_poiner, uint8_t y_filehandle
249 
255 static inline uint8_t
256 osbget(uint8_t y_handle)
257 {
258  uint8_t rega = 0;
259 
260  __asm__ __volatile__("jsr $ffd7" : "+Aq" (rega) : "yq"(y_handle) );
261 
262  return rega;
263 }
264 
271 static inline void
272 osbput(uint8_t a_byte, uint8_t y_handle)
273 {
274  __asm__ __volatile__("jsr $ffd4" : : "Aq" (a_byte), "yq"(y_handle) );
275 }
276 
280 typedef struct osgbpb_controlblock_s
281 {
287 
291 typedef enum osgbpb_action_e
292 {
293  osgbpb_putbytes_seq = 1,
294  osgbpb_putbytes = 2,
295  osgbpb_getbytes_seq = 3,
296  osgbpb_getbytes = 4,
297  osgbpb_gettitleandbootopt = 5,
298  osgbpb_readdirectory = 6,
299  osgbpb_readlibrary = 7,
300  osgbpb_readfilenames = 8
301 } osgbpb_action;
302 
312 extern void
313 osgbpb(uint8_t a_action, const osgbpb_controlblock *xy_pblock);
314 
315 
324 extern uint8_t
325 osfind(uint8_t a_operation, uint8_t x, uint8_t y);
326 
333 static inline uint8_t
334 osfind_open_r(char *filename)
335 {
336  filename[strlen((filename))] = 0x0d;
337  return osfind(0x40, ((uintptr_t) filename & 0xff), ((uintptr_t) filename & 0xff00) >> 8);
338 }
339 
346 static inline uint8_t
347 osfind_open_w(char *filename)
348 {
349  filename[strlen((filename))] = 0x0d;
350  return osfind(0x80, ((uintptr_t) filename & 0xff), ((uintptr_t) filename & 0xff00) >> 8);
351 }
352 
359 static inline uint8_t
360 osfind_open_rw(char *filename)
361 {
362  filename[strlen((filename))] = 0x0d;
363  return osfind(0xc0, ((uintptr_t) filename & 0xff), ((uintptr_t) filename & 0xff00) >> 8);
364 }
365 
371 static inline void
373 {
374  osfind(0, 0, handle);
375 }
376 
377 
378 #endif // !__OS_H__
379 
380 
static void osasci(uint8_t a_char)
Definition: os.h:82
static uint8_t osrdsc(uintptr_t address)
Definition: os.h:134
enum osfile_action_e osfile_action
int32_t load_address
Definition: os.h:210
int32_t data_pointer
Definition: os.h:283
static void oswrch(uint8_t a_char)
Definition: os.h:43
void osgbpb(uint8_t a_action, const osgbpb_controlblock *xy_pblock)
static void gsread(uint8_t *a, uint8_t *x, uint8_t y)
Definition: os.h:105
static void osnewl(void)
Definition: os.h:67
enum osgbpb_action_e osgbpb_action
int32_t exec_address
Definition: os.h:211
static void oseven(uint8_t y_eventnum)
Definition: os.h:153
unsigned char uint8_t
Definition: stdint.h:34
void osfile(uint8_t a_action, const osfile_parameterblock *xy_pblock)
uint8_t osrdrm(uint8_t y_romnum, uintptr_t address)
static void oscli(char *xy_command)
Definition: os.h:164
static void osbyte(uint8_t a_func, uint8_t *x, uint8_t *y)
Definition: os.h:184
Definition: os.h:207
int32_t file_handle
Definition: os.h:282
static uint8_t osrdch(void)
Definition: os.h:54
struct osfile_parameterblock_s osfile_parameterblock
int32_t start_address
Definition: os.h:212
static void osbput(uint8_t a_byte, uint8_t y_handle)
Definition: os.h:272
osfile_action_e
Definition: os.h:219
uint16_t uintptr_t
Definition: stdint.h:59
int32_t seq_pointer
Definition: os.h:285
int32_t file_name
Definition: os.h:209
struct osgbpb_controlblock_s osgbpb_controlblock
int32_t end_address
Definition: os.h:213
Named types for various sizes.
uint8_t osfind(uint8_t a_operation, uint8_t x, uint8_t y)
long int32_t
Definition: stdint.h:51
static uint8_t osfind_open_w(char *filename)
Definition: os.h:347
static void osfind_close(uint8_t handle)
Definition: os.h:372
static uint8_t osbget(uint8_t y_handle)
Definition: os.h:256
static uint8_t osfind_open_r(char *filename)
Definition: os.h:334
void oswrsc(uint8_t a_byte, uintptr_t address)
osgbpb_action_e
Definition: os.h:291
int32_t num_bytes
Definition: os.h:284
static uint8_t osfind_open_rw(char *filename)
Definition: os.h:360
static void gsinit(uint8_t *a, uint8_t *y)
Definition: os.h:93
Definition: os.h:280
void osword(uint8_t a_func, const void *xy_pointer)