llvm-mos-sdk
cbm.h
Go to the documentation of this file.
1 // Copyright 2022 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 // Originally from cc65. Modified from original version.
7 
8 // clang-format off
9 
10 /*****************************************************************************/
11 /* */
12 /* cbm.h */
13 /* */
14 /* CBM system-specific definitions */
15 /* */
16 /* */
17 /* */
18 /* (C) 1998-2015, Ullrich von Bassewitz */
19 /* Roemerstrasse 52 */
20 /* D-70794 Filderstadt */
21 /* EMail: uz@cc65.org */
22 /* */
23 /* */
24 /* This software is provided 'as-is', without any expressed or implied */
25 /* warranty. In no event will the authors be held liable for any damages */
26 /* arising from the use of this software. */
27 /* */
28 /* Permission is granted to anyone to use this software for any purpose, */
29 /* including commercial applications, and to alter it and redistribute it */
30 /* freely, subject to the following restrictions: */
31 /* */
32 /* 1. The origin of this software must not be misrepresented; you must not */
33 /* claim that you wrote the original software. If you use this software */
34 /* in a product, an acknowledgment in the product documentation would be */
35 /* appreciated but is not required. */
36 /* 2. Altered source versions must be plainly marked as such, and must not */
37 /* be misrepresented as being the original software. */
38 /* 3. This notice may not be removed or altered from any source */
39 /* distribution. */
40 /* */
41 /*****************************************************************************/
42 
43 
44 
45 #ifndef _CBM_H
46 #define _CBM_H
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 
53 /* Check for errors */
54 #if !defined(__CBM__)
55 # error This module may be used only when compiling for CBM machines!
56 #endif
57 
58 
59 
60 /* We need NULL. */
61 #include <stddef.h>
62 
63 /* Load the system-specific files here, if needed. */
64 #if defined(__C64__) && !defined(_C64_H)
65 # include <c64.h>
66 #elif defined(__VIC20__) && !defined(_VIC20_H)
67 # include <vic20.h>
68 #elif defined(__C128__) && !defined(_C128_H)
69 # include <c128.h>
70 #elif defined(__PLUS4__) && !defined(_PLUS4_H)
71 # include <plus4.h>
72 #elif defined(__C16__) && !defined(_C16_H)
73 # include <c16.h>
74 #elif defined(__CBM510__) && !defined(_CBM510_H)
75 # include <cbm510.h>
76 #elif defined(__CBM610__) && !defined(_CBM610_H)
77 # include <cbm610.h>
78 #elif defined(__PET__) && !defined(_PET_H)
79 # include <pet.h>
80 #elif defined(__CX16__) && !defined(_CX16_H)
81 # include <cx16.h>
82 #endif
83 
84 // TODO
85 #if 0
86 /* Include definitions for CBM file types */
87 #include <cbm_filetype.h>
88 #endif
89 
90 
91 
92 #define JOY_FIRE_MASK JOY_BTN_1_MASK
93 #define JOY_FIRE(v) ((v) & JOY_FIRE_MASK)
94 
95 
96 
97 /*****************************************************************************/
98 /* Variables */
99 /*****************************************************************************/
100 
101 
102 
103 /* The file stream implementation and the POSIX I/O functions will
104 ** use the following variable to determine the file type to use.
105 */
106 // TODO
107 //extern char _filetype; /* Defaults to 's' */
108 
109 
110 
111 /*****************************************************************************/
112 /* Character-codes (CBM charset) */
113 /*****************************************************************************/
114 
115 
116 
117 #define CH_HLINE 192
118 #define CH_VLINE 221
119 #define CH_ULCORNER 176
120 #define CH_URCORNER 174
121 #define CH_LLCORNER 173
122 #define CH_LRCORNER 189
123 #define CH_TTEE 178
124 #define CH_BTEE 177
125 #define CH_LTEE 171
126 #define CH_RTEE 179
127 #define CH_CROSS 219
128 #define CH_CURS_UP 145
129 #define CH_CURS_DOWN 17
130 #define CH_CURS_LEFT 157
131 #define CH_CURS_RIGHT 29
132 #define CH_PI 222
133 #define CH_HOME 19
134 #define CH_DEL 20
135 #define CH_INS 148
136 #define CH_ENTER 13
137 #define CH_STOP 3
138 #define CH_LIRA 92
139 #define CH_ESC 27
140 #define CH_FONT_LOWER 14
141 #define CH_FONT_UPPER 142
142 
143 
144 
145 /*****************************************************************************/
146 /* Definitions for directory reading functions */
147 /*****************************************************************************/
148 
149 
150 
151 /* CBM FILE ACCESS */
152 #define CBM_A_RO 1 /* Read only */
153 #define CBM_A_WO 2 /* Write only */
154 #define CBM_A_RW 3 /* Read, Write */
155 
156 struct cbm_dirent {
157  char name[17]; /* File name in PetSCII, limited to 16 chars */
158  unsigned int size; /* Size, in 254-/256-byte blocks */
159  unsigned char type;
160  unsigned char access;
161 };
162 
163 
164 
165 /*****************************************************************************/
166 /* Machine info */
167 /*****************************************************************************/
168 
169 
170 // TODO
171 #if 0
172 
173 #define TV_NTSC 0
174 #define TV_PAL 1
175 #define TV_OTHER 2
176 
177 unsigned char get_tv (void);
178 /* Return the video mode the machine is using. */
179 
180 #define KBREPEAT_CURSOR 0x00
181 #define KBREPEAT_NONE 0x40
182 #define KBREPEAT_ALL 0x80
183 
184 unsigned char __fastcall__ kbrepeat (unsigned char mode);
185 /* Changes which keys have automatic repeat. */
186 
187 #if !defined(__CBM610__)
188 void waitvsync (void);
189 /* Wait for the start of the next video field. */
190 #endif
191 
192 #endif
193 
194 /*****************************************************************************/
195 /* CBM kernal functions */
196 /*****************************************************************************/
197 
198 /* Constants to use with cbm_open() for openning a file for reading or
199 ** writing without the need to append ",r" or ",w" to the filename.
200 **
201 ** e.g., cbm_open(2, 8, CBM_READ, "0:data,s");
202 */
203 #define CBM_READ 0 /* default is ",p" */
204 #define CBM_WRITE 1 /* ditto */
205 #define CBM_SEQ 2 /* default is ",r" -- or ",s" when writing */
206 
207 /* Kernal-level functions */
208 unsigned char cbm_k_acptr (void);
209 unsigned char cbm_k_basin (void);
210 void cbm_k_bsout (unsigned char C);
211 unsigned char cbm_k_chkin (unsigned char FN) __attribute__((leaf));
212 unsigned char cbm_k_chkout (unsigned char FN) __attribute__((leaf));
213 unsigned char cbm_k_chrin (void);
214 void cbm_k_chrout (unsigned char C);
215 void cbm_k_cint (void);
216 void cbm_k_ciout (unsigned char C);
217 unsigned char cbm_k_ckout (unsigned char FN) __attribute__((leaf));
218 void cbm_k_clall (void);
219 void cbm_k_close (unsigned char FN);
220 void cbm_k_clrch (void);
221 unsigned char cbm_k_getin (void);
222 unsigned cbm_k_iobase (void) __attribute__((leaf));
223 void cbm_k_listen (unsigned char dev);
224 void *cbm_k_load(unsigned char flag, void *startaddr) __attribute__((leaf));
225 unsigned char cbm_k_open (void) __attribute__((leaf));
226 unsigned char cbm_k_readst (void);
227 unsigned char cbm_k_save(void *startaddr, void *endaddr_plusone) __attribute__((leaf));
228 void cbm_k_scnkey (void);
229 void cbm_k_second (unsigned char addr);
230 void cbm_k_setlfs (unsigned char LFN, unsigned char DEV,
231  unsigned char SA) __attribute__((leaf));
232 void cbm_k_setnam (const char* Name) __attribute__((leaf));
233 void cbm_k_settim (unsigned long timer);
234 void cbm_k_talk (unsigned char dev);
235 void cbm_k_tksa (unsigned char addr);
236 void cbm_k_udtim (void);
237 void cbm_k_unlsn (void);
238 void cbm_k_untlk (void);
239 
240 
241 /*****************************************************************************/
242 /* BASIC-like file I/O functions */
243 /*****************************************************************************/
244 
245 /* The cbm_* I/O functions below set _oserror (see errno.h),
246 ** in case of an error.
247 **
248 ** error-code BASIC error
249 ** ---------- -----------
250 ** 1 = too many files
251 ** 2 = file open
252 ** 3 = file not open
253 ** 4 = file not found
254 ** 5 = device not present
255 ** 6 = not input-file
256 ** 7 = not output-file
257 ** 8 = missing file-name
258 ** 9 = illegal device-number
259 **
260 ** 10 = STOP-key pushed
261 ** 11 = general I/O-error
262 */
263 
264 
265 // TODO
266 #if 0
267 
268 unsigned int __fastcall__ cbm_load (const char* name, unsigned char device, void* data);
269 /* Loads file "name", from given device, to given address -- or, to the load
270 ** address of the file if "data" is the null pointer (like load"name",8,1
271 ** in BASIC).
272 ** Returns number of bytes that were loaded if loading was successful;
273 ** otherwise 0, "_oserror" contains an error-code, then (see table above).
274 */
275 
276 unsigned char __fastcall__ cbm_save (const char* name, unsigned char device,
277  const void* addr, unsigned int size);
278 /* Saves "size" bytes, starting at "addr", to a file.
279 ** Returns 0 if saving was successful, otherwise an error-code (see table
280 ** above).
281 */
282 
283 unsigned char __fastcall__ cbm_open (unsigned char lfn, unsigned char device,
284  unsigned char sec_addr, const char* name);
285 /* Opens a file. Works just like the BASIC command.
286 ** Returns 0 if openning was successful, otherwise an error-code (see table
287 ** above).
288 */
289 
290 void __fastcall__ cbm_close (unsigned char lfn);
291 /* Closes a file */
292 
293 int __fastcall__ cbm_read (unsigned char lfn, void* buffer, unsigned int size);
294 /* Reads up to "size" bytes from a file into "buffer".
295 ** Returns the number of actually-read bytes, 0 if there are no bytes left.
296 ** -1 in case of an error; then, _oserror contains an error-code (see table
297 ** above). (Remember: 0 means end-of-file; -1 means error.)
298 */
299 
300 int __fastcall__ cbm_write (unsigned char lfn, const void* buffer,
301  unsigned int size);
302 /* Writes up to "size" bytes from "buffer" to a file.
303 ** Returns the number of actually-written bytes, or -1 in case of an error;
304 ** _oserror contains an error-code, then (see above table).
305 */
306 
307 unsigned char cbm_opendir (unsigned char lfn, unsigned char device, ...);
308 /* Opens directory listing. Returns 0 if opening directory was successful;
309 ** otherwise, an error-code corresponding to cbm_open(). As an optional
310 ** argument, the name of the directory may be passed to the function. If
311 ** no explicit name is specified, "$" is used.
312 */
313 
314 unsigned char __fastcall__ cbm_readdir (unsigned char lfn,
315  struct cbm_dirent* l_dirent);
316 /* Reads one directory line into cbm_dirent structure.
317 ** Returns 0 if reading directory-line was successful.
318 ** Returns non-zero if reading directory failed, or no more file-names to read.
319 ** Returns 2 on last line. Then, l_dirent->size = the number of "blocks free",
320 ** "blocks used", or "mb free". Return codes:
321 ** 0 = read file-name
322 ** 1 = couldn't read directory
323 ** 2 = read "blocks free", "blocks used", or "mb free"
324 ** 3 = couldn't find start of file-name
325 ** 4 = couldn't find end of file-name
326 ** 5 = couldn't read file-type
327 ** 6 = premature end of file
328 */
329 
330 void __fastcall__ cbm_closedir (unsigned char lfn);
331 /* Closes directory by cbm_close(lfn) */
332 
333 #endif
334 
335 #ifdef __cplusplus
336 }
337 #endif
338 
339 /* End of cbm.h */
340 #endif
cbm_dirent::type
unsigned char type
Definition: cbm.h:159
cbm_k_bsout
void cbm_k_bsout(unsigned char C)
cbm_k_chrout
void cbm_k_chrout(unsigned char C)
cbm_dirent::size
unsigned int size
Definition: cbm.h:158
data
char const void * data
Definition: neslib.h:92
c128.h
cbm_k_tksa
void cbm_k_tksa(unsigned char addr)
c64.h
cbm_k_save
unsigned char cbm_k_save(void *startaddr, void *endaddr_plusone) __attribute__((leaf))
cbm_k_udtim
void cbm_k_udtim(void)
cbm_dirent
Definition: cbm.h:156
cbm_k_basin
unsigned char cbm_k_basin(void)
cbm_k_setlfs
void cbm_k_setlfs(unsigned char LFN, unsigned char DEV, unsigned char SA) __attribute__((leaf))
cbm_k_chrin
unsigned char cbm_k_chrin(void)
cbm_k_ckout
unsigned char cbm_k_ckout(unsigned char FN) __attribute__((leaf))
get_tv
unsigned char get_tv(void) __attribute__((leaf))
cbm_k_listen
void cbm_k_listen(unsigned char dev)
cbm_k_second
void cbm_k_second(unsigned char addr)
cbm_k_scnkey
void cbm_k_scnkey(void)
cbm_k_chkout
unsigned char cbm_k_chkout(unsigned char FN) __attribute__((leaf))
cbm_k_untlk
void cbm_k_untlk(void)
size
unsigned size
Definition: neslib.h:185
cx16.h
vic20.h
cbm_k_load
void * cbm_k_load(unsigned char flag, void *startaddr) __attribute__((leaf))
cbm_k_acptr
unsigned char cbm_k_acptr(void)
cbm_dirent::access
unsigned char access
Definition: cbm.h:160
cbm_k_settim
void cbm_k_settim(unsigned long timer)
cbm_k_clrch
void cbm_k_clrch(void)
cbm_k_clall
void cbm_k_clall(void)
cbm_k_unlsn
void cbm_k_unlsn(void)
cbm_k_close
void cbm_k_close(unsigned char FN)
cbm_k_ciout
void cbm_k_ciout(unsigned char C)
waitvsync
void waitvsync(void)
cbm_k_iobase
unsigned cbm_k_iobase(void) __attribute__((leaf))
cbm_k_getin
unsigned char cbm_k_getin(void)
cbm_k_talk
void cbm_k_talk(unsigned char dev)
cbm_k_open
unsigned char cbm_k_open(void) __attribute__((leaf))
cbm_k_setnam
void cbm_k_setnam(const char *Name) __attribute__((leaf))
cbm_k_chkin
unsigned char cbm_k_chkin(unsigned char FN) __attribute__((leaf))
cbm_k_readst
unsigned char cbm_k_readst(void)
mode
const void uint16_t uint8_t mode
Definition: memory.h:58
cbm_dirent::name
char name[17]
Definition: cbm.h:157
cbm_k_cint
void cbm_k_cint(void)
pet.h